Search in sources :

Example 6 with TextLineType

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

the class TrpPdfDocument method addTextFromTextRegion.

private void addTextFromTextRegion(final TextRegionType tr, final PdfContentByte cb, int cutoffLeft, int cutoffTop, BaseFont bf, ExportCache cache) throws IOException {
    List<TextLineType> lines = tr.getTextLine();
    boolean firstLine;
    if (lines != null && !lines.isEmpty()) {
        // sort according to reading order
        Collections.sort(lines, new TrpElementReadingOrderComparator<TextLineType>(true));
        double baseLineMeanY = 0;
        double baseLineMeanYPrev = 0;
        double baseLineMeanGap = 0;
        // logger.debug("Processing " + lines.size() + " lines in TextRegion " + tr.getId());
        for (TextLineType lt : lines) {
            TrpTextLineType l = (TrpTextLineType) lt;
            // java.awt.Rectangle lineRect = PageXmlUtils.buildPolygon(l.getCoords().getPoints()).getBounds();
            // compute rotation of text, if rotation higher PI/16 than rotate otherwise even text
            TrpBaselineType baseline = (TrpBaselineType) l.getBaseline();
            double rotation = (baseline != null ? computeRotation(baseline) : 0);
            // if (lineRect.height > 0){
            // float lineHeight = lineRect.height /3;
            // 
            // logger.debug("line height: "+ lineHeight);
            // 
            // //ignore actual lineHeigth if three times the size of the actual line mean heigth
            // if (!(lineHeight > lineMeanHeight*4) || lineMeanHeight == 0){
            // //calculate line mean Height
            // lineMeanHeight = (lineMeanHeight == 0 ? lineHeight : (lineMeanHeight + lineHeight)/2);
            // logger.debug("lineMeanHeight: "+ lineMeanHeight);
            // }
            // }
            // get the mean baseline y-value
            baseLineMeanYPrev = baseLineMeanY;
            if (baseline != null) {
                // use lowest point in baseline and move up one half of the distance to the topmost point
                java.awt.Rectangle baseLineRect = l.getBoundingBox();
                baseLineMeanY = baseLineRect.getMaxY() - ((baseLineRect.getMaxY() - baseLineRect.getMinY()) / 2);
                if (baseLineMeanYPrev != 0) {
                    baseLineMeanGap = baseLineMeanY - baseLineMeanYPrev;
                }
            }
            boolean rtl = false;
            if ((l.getUnicodeText().isEmpty() || useWordLevel) && !l.getWord().isEmpty()) {
                List<WordType> words = l.getWord();
                for (WordType wt : words) {
                    TrpWordType w = (TrpWordType) wt;
                    if (!w.getUnicodeText().isEmpty()) {
                        // java.awt.Rectangle boundRect = PageXmlUtils.buildPolygon(w.getCoords()).getBounds();
                        java.awt.Rectangle boundRect = w.getBoundingBox();
                        String text = w.getUnicodeText();
                        rtl = textIsRTL(text.trim());
                        addString(boundRect, baseLineMeanY, text, cb, cutoffLeft, cutoffTop, bf, rotation, rtl);
                    } else {
                    // logger.info("No text content in word: " + w.getId());
                    }
                }
            } else if (!l.getUnicodeText().isEmpty()) {
                String lineTextTmp = l.getUnicodeText();
                // get surrounding rectangle coords of this line
                java.awt.Rectangle boundRect = l.getBoundingBox();
                Set<Entry<CustomTag, String>> blackSet = ExportUtils.getAllTagsOfThisTypeForShapeElement(l, RegionTypeUtil.BLACKENING_REGION.toLowerCase()).entrySet();
                if (doBlackening && blackSet.size() > 0) {
                    // for all blackening regions replace text with ****
                    for (Map.Entry<CustomTag, String> currEntry : blackSet) {
                        if (!currEntry.getKey().isIndexed()) {
                            // logger.debug("line not indexed : " + lineTextTmp);
                            lineTextTmp = lineTextTmp.replaceAll(".", "*");
                        } else {
                            // logger.debug("lineText before blackened : " + lineTextTmp);
                            lineTextTmp = blackenString(currEntry, lineTextTmp);
                        // logger.debug("lineText after blackened : " + lineTextTmp);
                        }
                    }
                }
                rtl = textIsRTL(lineTextTmp.trim());
                addString(boundRect, baseLineMeanY, lineTextTmp, cb, cutoffLeft, cutoffTop, bf, rotation, rtl);
            /*
					 * highlight all tags of this text line if property is set
					 */
            // if (highlightTags){
            // highlightTagsForShape(l);
            // 
            // }
            } else {
            // logger.info("No text content in line: " + l.getId());
            }
            if (highlightTags) {
                if ((l.getUnicodeText().isEmpty() || useWordLevel) && !l.getWord().isEmpty()) {
                    List<WordType> words = l.getWord();
                    for (WordType wt : words) {
                        TrpWordType w = (TrpWordType) wt;
                        highlightTagsForShape(w, rtl, cache);
                    }
                } else {
                    highlightTagsForShape(l, rtl, cache);
                }
            }
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) Set(java.util.Set) HashSet(java.util.HashSet) Rectangle(java.awt.Rectangle) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) TrpWordType(eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType) WordType(eu.transkribus.core.model.beans.pagecontent.WordType) TrpWordType(eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TrpBaselineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType) Entry(java.util.Map.Entry) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)

Example 7 with TextLineType

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

the class TrpPdfDocument method getAverageBeginningOfBaselines.

/*
	 * calculate where the line alignment should be placed
	 * take average of starting points of all lines
	 * problem when some lines are on the right
	 * So take only lines starting within the first 1/10 of the text region width
	 */
private float getAverageBeginningOfBaselines(TextRegionType tr) {
    // logger.debug("calculate average beginning of baselines ");
    float avgStartOfLines = 0;
    double width = tr.getBoundingBox().getWidth();
    float firstTenth = (float) (width / 10);
    int nrOfLines = 0;
    for (TextLineType l : tr.getTextLine()) {
        TrpBaselineType bl = (TrpBaselineType) l.getBaseline();
        if (bl.getBoundingBox().getMinX() <= firstTenth) {
            avgStartOfLines += bl.getBoundingBox().getMinX();
            nrOfLines += 1;
        }
    }
    if (nrOfLines > 0) {
        avgStartOfLines = avgStartOfLines / nrOfLines;
        return avgStartOfLines;
    }
    return (float) tr.getBoundingBox().getMinX();
}
Also used : TrpBaselineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) Point(java.awt.Point)

Example 8 with TextLineType

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

the class TrpPdfDocument method isOnlyRegionInThisRow.

private boolean isOnlyRegionInThisRow(List<TrpRegionType> regions, TextRegionType regionToCompare) {
    float minX = 0;
    float minY = 0;
    float maxX = 0;
    float maxY = 0;
    float meanX = 0;
    float meanY = 0;
    java.awt.Rectangle compareBlock = regionToCompare.getBoundingBox();
    float compareMinX = (float) compareBlock.getMinX();
    float compareMinY = (float) compareBlock.getMinY();
    float compareMaxX = (float) compareBlock.getMaxX();
    float compareMaxY = (float) compareBlock.getMaxY();
    float compareMeanX = compareMinX + (compareMaxX - compareMinX) / 2;
    float compareMeanY = compareMinY + (compareMaxY - compareMinY) / 2;
    boolean foundSmallerColumn = false;
    if (regions.size() == 1) {
        return true;
    } else {
        for (RegionType r : regions) {
            // TODO add paths for tables etc.
            if (r instanceof TextRegionType && r.getId() != regionToCompare.getId()) {
                TextRegionType tr = (TextRegionType) r;
                // empty region can be ignored
                if (tr.getTextLine().isEmpty())
                    continue;
                else {
                    // region with empty lines can also be ignored
                    boolean textFound = false;
                    for (TextLineType tlt : tr.getTextLine()) {
                        TrpTextLineType l = (TrpTextLineType) tlt;
                        textFound = !l.getUnicodeText().isEmpty();
                        if (textFound) {
                            break;
                        }
                    }
                    // no text in region -> go to next region
                    if (!textFound) {
                        continue;
                    }
                }
                // logger.debug("tr id " + tr.getId());
                // compute average text region start
                // java.awt.Rectangle block = PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds();
                java.awt.Rectangle block = tr.getBoundingBox();
                minX = (float) block.getMinX();
                maxX = (float) block.getMaxX();
                minY = (float) block.getMinY();
                maxY = (float) block.getMaxY();
                // meanX = minX+(maxX - minX)/2;
                meanY = minY + (maxY - minY) / 2;
                if (((meanY > compareMinY && meanY < compareMaxY) || (compareMeanY > minY && compareMeanY < maxY))) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) Rectangle(java.awt.Rectangle) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) UnknownRegionType(eu.transkribus.core.model.beans.pagecontent.UnknownRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) TrpTableRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)

Example 9 with TextLineType

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

the class TrpTextRegionType method clearTextForAllWordsinLines.

public void clearTextForAllWordsinLines(Object who) {
    for (TextLineType tl : getTextLine()) {
        TrpTextLineType trpTl = (TrpTextLineType) tl;
        trpTl.clearTextForAllWords(who);
    }
}
Also used : TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType)

Example 10 with TextLineType

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

the class TrpTextRegionType method getRegionTextFromLines.

public String getRegionTextFromLines() {
    String textAll = "";
    for (TextLineType l : getTextLine()) {
        TrpTextLineType trp_l = (TrpTextLineType) l;
        textAll += trp_l.getUnicodeText() + "\n";
    }
    textAll = StringUtils.removeEnd(textAll, "\n");
    return textAll;
}
Also used : TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType)

Aggregations

TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)27 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)19 WordType (eu.transkribus.core.model.beans.pagecontent.WordType)13 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)13 TextRegionType (eu.transkribus.core.model.beans.pagecontent.TextRegionType)9 TrpWordType (eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType)9 RegionType (eu.transkribus.core.model.beans.pagecontent.RegionType)6 Rectangle (java.awt.Rectangle)6 ArrayList (java.util.ArrayList)5 TrpPage (eu.transkribus.core.model.beans.TrpPage)4 TrpElementCoordinatesComparator (eu.transkribus.core.model.beans.pagecontent_trp.TrpElementCoordinatesComparator)4 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)4 TrpRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)4 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)3 TrpBaselineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType)3 Point (java.awt.Point)3 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)2 TrpTranscriptStatistics (eu.transkribus.core.model.beans.TrpTranscriptStatistics)2 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)2 TextEquivType (eu.transkribus.core.model.beans.pagecontent.TextEquivType)2