Search in sources :

Example 16 with RegionType

use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.

the class PageXmlUtils method copyTextContent.

public static void copyTextContent(PcGtsType origPc, PcGtsType newPc) {
    if (!hasRegions(origPc) || !hasRegions(newPc)) {
        return;
    }
    List<TrpRegionType> origRegs = origPc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
    List<TrpRegionType> newRegs = newPc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
    // map the regions where we want to keep the textContent
    Map<String, TextRegionType> textMap = new HashMap<>();
    // iterate all old regions. Map the ones containing lines
    for (RegionType r : origRegs) {
        if (!(r instanceof TextRegionType)) {
            continue;
        }
        TextRegionType tr = (TextRegionType) r;
        boolean hasTextLines = tr.getTextLine() != null && !tr.getTextLine().isEmpty();
        if (hasTextLines) {
            textMap.put(tr.getId(), tr);
        }
    }
    // iterate the new regions and move all the line contents from the old one
    for (RegionType r : newRegs) {
        if (!(r instanceof TextRegionType) || !textMap.containsKey(r.getId())) {
            continue;
        }
        // this region corresponds with an old one
        TextRegionType newTr = (TextRegionType) r;
        TextRegionType oldTr = textMap.get(newTr.getId());
        copyTextRegionContent(oldTr, newTr);
    }
}
Also used : TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TableRegionType(eu.transkribus.core.model.beans.pagecontent.TableRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) HashMap(java.util.HashMap)

Example 17 with RegionType

use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.

the class TrpElementCoordinatesComparator method compare.

@Override
public int compare(T o1, T o2) {
    // if (!isRegionLineOrWord(o1) || !isRegionLineOrWord(o2))
    // return 0;
    logger.trace("compare in TrpElementCoordinatesComparator");
    // try {
    String coords1 = "", coords2 = "";
    if (o1 instanceof RegionType) {
        RegionType r1 = (RegionType) o1;
        RegionType r2 = (RegionType) o2;
        if (r1.getCoords() != null && r2.getCoords() != null) {
            coords1 = r1.getCoords().getPoints();
            coords2 = r2.getCoords().getPoints();
        }
    } else if (TextLineType.class.isAssignableFrom(o1.getClass())) {
        // if existing, take baseline to compare position of lines
        if (((TextLineType) o1).getBaseline() != null && ((TextLineType) o2).getBaseline() != null) {
            coords1 = ((TextLineType) o1).getBaseline().getPoints();
            coords2 = ((TextLineType) o2).getBaseline().getPoints();
        } else {
            // fall back if there are no baselines
            coords1 = ((TextLineType) o1).getCoords().getPoints();
            coords2 = ((TextLineType) o2).getCoords().getPoints();
        }
    } else if (o1 instanceof TrpBaselineType) {
        coords1 = ((TrpBaselineType) o1).getPoints();
        coords2 = ((TrpBaselineType) o2).getPoints();
    } else if (WordType.class.isAssignableFrom(o1.getClass())) {
        WordType w1 = (WordType) o1;
        WordType w2 = (WordType) o2;
        if (w1.getCoords() != null && w2.getCoords() != null) {
            coords1 = w1.getCoords().getPoints();
            coords2 = w2.getCoords().getPoints();
        }
    }
    // if (coords1.isEmpty() || coords2.isEmpty()) {
    // throw new Exception("No coordinates in one of the objects - should not happen!");
    // }
    // determine orientation of (parent) text regions
    Float orientation = null;
    if (o1 instanceof ITrpShapeType && o2 instanceof ITrpShapeType && !(o1 instanceof RegionType) && !(o2 instanceof RegionType)) {
        TrpTextRegionType tr1 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o1);
        TrpTextRegionType tr2 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o2);
        if (tr1 != null && tr2 != null && StringUtils.equals(tr1.getId(), tr2.getId()) && tr1.getOrientation() != null) {
            orientation = tr1.getOrientation();
        }
    }
    // --------------------------
    java.awt.Polygon p1 = new java.awt.Polygon();
    try {
        for (java.awt.Point p : PointStrUtils.parsePoints(coords1)) p1.addPoint(p.x, p.y);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    java.awt.Polygon p2 = new java.awt.Polygon();
    try {
        for (java.awt.Point p : PointStrUtils.parsePoints(coords2)) p2.addPoint(p.x, p.y);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    Rectangle b1 = p1.getBounds();
    Rectangle b2 = p2.getBounds();
    Point pt1 = new Point(b1.x, b1.y);
    Point pt2 = new Point(b2.x, b2.y);
    if (orientation != null) {
        pt1 = GeomUtils.rotate(pt1, orientation);
        pt2 = GeomUtils.rotate(pt2, orientation);
        logger.trace("orientation set: " + orientation + " rotated points: " + pt1 + ", " + pt2);
    }
    if (compareByYX == null) {
        // if compareByYX was not set by constructor, determine via shape
        compareByYX = !WordType.class.isAssignableFrom(o1.getClass());
    }
    if (!compareByYX) {
        // return compareByXY(b1.x, b2.x, b1.y, b2.y);
        return compareByXY(pt1.x, pt2.x, pt1.y, pt2.y);
    } else {
        return compareByYX(pt1.x, pt2.x, pt1.y, pt2.y);
    // return compareByYX(b1.x, b2.x, b1.y, b2.y);
    // return compareBy_YOverlap_X(b1, b2);
    }
// }
// catch (Exception e) {
// e.printStackTrace();
// return 0;
// }
}
Also used : RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) WordType(eu.transkribus.core.model.beans.pagecontent.WordType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType)

Example 18 with RegionType

use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.

the class TrpPageTypeUtils method assignUniqueIDs.

/**
 * Assigns unique IDs to the elements in the page using the current order of the elements.
 */
public static void assignUniqueIDs(PageType page) {
    int i = 1;
    for (RegionType r : page.getTextRegionOrImageRegionOrLineDrawingRegion()) {
        if (r instanceof TextRegionType) {
            TextRegionType region = (TextRegionType) r;
            String rid = "r" + i;
            region.setId(rid);
            int j = 1;
            for (TextLineType l : region.getTextLine()) {
                String lid = rid + "l" + j;
                l.setId(lid);
                int k = 1;
                for (WordType word : l.getWord()) {
                    String wid = lid + "w" + k;
                    word.setId(wid);
                    k++;
                }
                ++j;
            }
            ++i;
        }
    }
}
Also used : TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) WordType(eu.transkribus.core.model.beans.pagecontent.WordType)

Aggregations

RegionType (eu.transkribus.core.model.beans.pagecontent.RegionType)18 TrpRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)16 TextRegionType (eu.transkribus.core.model.beans.pagecontent.TextRegionType)15 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)15 TrpTableRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType)7 TableRegionType (eu.transkribus.core.model.beans.pagecontent.TableRegionType)6 TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)5 UnknownRegionType (eu.transkribus.core.model.beans.pagecontent.UnknownRegionType)5 WordType (eu.transkribus.core.model.beans.pagecontent.WordType)4 Point (java.awt.Point)4 Rectangle (java.awt.Rectangle)4 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)2 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)2 ITrpShapeType (eu.transkribus.core.model.beans.pagecontent_trp.ITrpShapeType)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Image (com.itextpdf.text.Image)1