Search in sources :

Example 6 with TrpTextRegionType

use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addUniformTextFromTextRegion.

private void addUniformTextFromTextRegion(final TextRegionType tr, final PdfContentByte cb, int cutoffLeft, int cutoffTop, BaseFont bf, float lineStartX, ExportCache cache) throws IOException, DocumentException {
    List<TextLineType> lines = tr.getTextLine();
    if (lines != null && !lines.isEmpty()) {
        int i = 0;
        float lineStartY = 0;
        // sort according to reading order
        Collections.sort(lines, new TrpElementReadingOrderComparator<TextLineType>(true));
        double minY = 0;
        double maxY = 0;
        // get min and max values of region y direction for later calculation of textline height
        // java.awt.Rectangle regionRect = PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds();
        int maxIdx = lines.size() - 1;
        // java.awt.Rectangle firstLineRectOld = PageXmlUtils.buildPolygon(lines.get(0).getCoords().getPoints()).getBounds();
        // logger.debug("OLDDDDD: firstLineRectOld minX = " + firstLineRectOld.getMinX());
        java.awt.Rectangle firstLineRect = ((TrpTextLineType) lines.get(0)).getBoundingBox();
        // logger.debug("NEWWWWW: firstLineRect minX = " + firstLineRect.getMinX());
        // java.awt.Rectangle lastLineRect = PageXmlUtils.buildPolygon(lines.get(maxIdx).getCoords().getPoints()).getBounds();
        java.awt.Rectangle lastLineRect = ((TrpTextLineType) lines.get(maxIdx)).getBoundingBox();
        double firstLineRotation = computeRotation((TrpBaselineType) lines.get(0).getBaseline());
        double lastLineRotation = computeRotation((TrpBaselineType) lines.get(maxIdx).getBaseline());
        boolean isVerticalRegion = false;
        // use X coords to compute the total line gap
        if (firstLineRotation == 90 && lastLineRotation == 90) {
            // since the reading order is not clear if the text is vertically -> could be right to left or vice versa
            double tmpMinX1 = firstLineRect.getMinX();
            double tmpMinX2 = lastLineRect.getMinX();
            double tmpMaxX1 = firstLineRect.getMaxX();
            double tmpMaxX2 = lastLineRect.getMaxX();
            minY = Math.min(tmpMinX1, tmpMinX2);
            maxY = Math.max(tmpMaxX1, tmpMaxX2);
            isVerticalRegion = true;
        } else {
            minY = firstLineRect.getMinY();
            maxY = lastLineRect.getMaxY();
        }
        /*
			 * if start of line is too tight on the upper bound - set to the first 1/12 of t page from above
			 * BUT: Is not good since page number and other informations are often in this section
			 */
        // if (minY < twelfthPoints[1][1]){
        // minY = twelfthPoints[1][1];
        // }
        // for(TextLineType lt : lines){
        // 
        // TrpTextLineType l = (TrpTextLineType)lt;
        // java.awt.Rectangle lineRect = PageXmlUtils.buildPolygon(l.getCoords().getPoints()).getBounds();
        // 
        // 
        // 
        // if (lines.size() == 1){
        // minY = lineRect.getMinY();
        // maxY = lineRect.getMaxY();
        // 
        // }
        // else if (l.getIndex() == 0){
        // minY = lineRect.getMinY();
        // }
        // else if (l.getIndex() == lines.size()-1){
        // maxY = lineRect.getMaxY();
        // }
        // 
        // }
        double lineGap = (maxY - minY) / lines.size();
        // use default values if only one line  and no previous line mean height computed
        if (lines.size() == 1) {
            lineMeanHeight = (prevLineMeanHeight != 0 ? prevLineMeanHeight : lineMeanHeight);
        } else if (lines.size() > 1) {
            lineMeanHeight = (float) (2 * (lineGap / 3));
            leading = (int) (lineGap / 3);
            prevLineMeanHeight = lineMeanHeight;
        // logger.debug("Line Mean Height for Export " + lineMeanHeight);
        // overallLineMeanHeight = ( (overallLineMeanHeight != 0) ? overallLineMeanHeight+lineMeanHeight/2 : lineMeanHeight);
        }
        for (TextLineType lt : lines) {
            wordOffset = 0;
            TrpTextLineType l = (TrpTextLineType) lt;
            TrpBaselineType baseline = (TrpBaselineType) l.getBaseline();
            // PageXmlUtils.buildPolygon(l.getCoords().getPoints()).getBounds();
            java.awt.Rectangle lineRect = l.getBoundingBox();
            // PageXmlUtils.buildPolygon(baseline.getPoints()).getBounds();
            java.awt.Rectangle baseLineRect = baseline == null ? null : baseline.getBoundingBox();
            if (baseLineRect == null) {
                logger.debug("Baseline is null - ignore this line");
                continue;
            }
            float tmpLineStartX = lineStartX;
            // PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
            float regionStartMinX = (float) tr.getBoundingBox().getMinX();
            double regionWidth = tr.getBoundingBox().getWidth();
            // first line
            if (i == 0) {
                lineStartY = (float) (minY + lineMeanHeight);
                /*
					 * if first line of a text region is indented then take this into account in printed text
					 */
                if (lineRect.getMinX() > regionStartMinX) {
                    if (lineRect.getMinX() - regionStartMinX > regionWidth / 4) {
                        // tmpLineStartX = (float) lineStartX + twelfthPoints[1][0];
                        tmpLineStartX = (float) baseLineRect.getMinX();
                    }
                }
            } else // for subsequent lines
            {
                if (lineRect.getMinX() > regionStartMinX) {
                    if (lineRect.getMinX() - regionStartMinX > regionWidth / 4) {
                        // tmpLineStartX = (float) lineStartX + twelfthPoints[1][0];
                        tmpLineStartX = (float) baseLineRect.getMinX();
                    }
                }
                // tmpLineStartX = getLinePositionInTextregionGrid(twelfthRegion, lineRect.getMinX());
                lineStartY = lineStartY + lineMeanHeight + leading;
            // for (TrpTextRegionType region : tr.getPage().getTextRegions(true)){
            // 
            // double regionMinX = PageXmlUtils.buildPolygon(region.getCoords().getPoints()).getBounds().getMinX();
            // double regionMaxX = PageXmlUtils.buildPolygon(region.getCoords().getPoints()).getBounds().getMaxX();
            // Rectangle rec = PageXmlUtils.buildPolygon(region.getCoords().getPoints()).getBounds();
            // 
            // if (rec.contains(tmpLineStartX, lineStartY) && !tr.getId().equals(region.getId()) && tmpLineStartX < regionMaxX){
            // logger.debug("region contains point " + tr.getId() + " region ID " + region.getId());
            // tmpLineStartX = (float) regionMaxX;
            // break;
            // }
            // 
            // 
            // }
            // if (lineRect.getMinX() > lineStartX){
            // if (lineRect.getMinX() - lineStartX > twelfthPoints[1][0]){
            // tmpLineStartX = (float) lineRect.getMinX();
            // }
            // }
            }
            if (baseLineRect != null && regionStartMinX < baseLineRect.getMinX() && (baseLineRect.getMinX() - regionStartMinX) > twelfthPoints[1][0]) {
                // logger.debug("try to find smaller region for baseline !!!!!!! " );
                for (TrpTextRegionType region : tr.getPage().getTextRegions(false)) {
                    if (!region.getId().equals(tr.getId())) {
                        // PageXmlUtils.buildPolygon(region.getCoords().getPoints()).getBounds().getMinX();
                        double regionMinX = region.getBoundingBox().getMinX();
                        double regionMaxX = region.getBoundingBox().getMaxX();
                        double regionMinY = region.getBoundingBox().getMinY();
                        double regionMaxY = region.getBoundingBox().getMaxY();
                        double meanX = regionMinX + (regionMaxX - regionMinX) / 2;
                        // another region before the lines
                        if (meanX > regionStartMinX && meanX < baseLineRect.getMinX() && baseLineRect.getMinY() < regionMaxY && baseLineRect.getMinY() > regionMinY) {
                            tmpLineStartX = (float) regionMaxX + lineMeanHeight;
                            logger.debug("region " + region.getId() + " overlaps this other region " + tr.getId());
                            // logger.debug("new tmplineStartX is " + regionMaxX);
                            break;
                        }
                    }
                }
            // tmpLineStartX = (float) baseLineRect.getMinX();
            }
            i++;
            /*
				 * word level bei uniform output nicht sinnvoll?
				 * besser nur ganze lines ausgeben
				 */
            // if(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();
            // 
            // addUniformString(boundRect, lineMeanHeight, lineStartX, lineStartY, w.getUnicodeText(), cb, cutoffLeft, cutoffTop, bf);
            // } else {
            // logger.info("No text content in word: " + w.getId());
            // }
            // }
            // } else if(!l.getUnicodeText().isEmpty()){
            /*
				 * make chunks out of the lineText
				 * so it is possible to have differnt fonts, underlines and other text styles in one line
				 * 
				 * possible text styles are:
				 * 		new CustomTagAttribute("fontFamily", true, "Font family", "Font family"),
						new CustomTagAttribute("serif", true, "Serif", "Is this a serif font?"),
						new CustomTagAttribute("monospace",true, "Monospace", "Is this a monospace (i.e. equals width characters) font?"),
						new CustomTagAttribute("fontSize", true, "Font size", "The size of the font in points"),
						new CustomTagAttribute("kerning", true, "Kerning", "The kerning of the font, see: http://en.wikipedia.org/wiki/Kerning"),
						new CustomTagAttribute("textColour", true, "Text colour", "The foreground colour of the text"),
						new CustomTagAttribute("bgColour", true, "Background colour", "The background colour of the text"),
						new CustomTagAttribute("reverseVideo", true, "Reverse video", "http://en.wikipedia.org/wiki/Reverse_video"),
						new CustomTagAttribute("bold", true, "Bold", "Bold font"),
						new CustomTagAttribute("italic", true, "Italic", "Italic font"),
						new CustomTagAttribute("underlined", true, "Underlined", "Underlined"),
						new CustomTagAttribute("subscript", true, "Subscript", "Subscript"),
						new CustomTagAttribute("superscript", true, "Superscript", "Superscript"),
						new CustomTagAttribute("strikethrough", true, "Strikethrough", "Strikethrough"),
						new CustomTagAttribute("smallCaps", true, "Small caps", "Small capital letters at the height as lowercase letters, see: http://en.wikipedia.org/wiki/Small_caps"),
						new CustomTagAttribute("letterSpaced", true, "Letter spaced", "Equals distance between characters, see: http://en.wikipedia.org/wiki/Letter-spacing"),
				 */
            List<Chunk> chunkList = new ArrayList<Chunk>();
            /*
				 * if line is empty -> use the words of this line as line text
				 * otherwise take the text in the line
				 */
            List<TextStyleTag> styleTags = new ArrayList<TextStyleTag>();
            String shapeText = "";
            if (l.getUnicodeText().isEmpty() || useWordLevel) {
                // logger.debug("in word based path " + useWordLevel);
                List<WordType> words = l.getWord();
                int chunkIndex = 0;
                for (WordType wt : words) {
                    TrpWordType w = (TrpWordType) wt;
                    String wordText = "";
                    // add empty space after each word
                    if (chunkIndex > 0) {
                        chunkList.add(chunkIndex, new Chunk(" "));
                        chunkIndex++;
                    }
                    if (!w.getUnicodeText().isEmpty()) {
                        // remember all style tags for text formatting later on
                        styleTags.addAll(w.getTextStyleTags());
                        if (!shapeText.equals("")) {
                            shapeText = shapeText.concat(" ");
                        }
                        wordText = wordText.concat(w.getUnicodeText());
                        shapeText = shapeText.concat(w.getUnicodeText());
                        for (int j = 0; j < wordText.length(); ++j) {
                            String currentCharacter = wordText.substring(j, j + 1);
                            chunkList.add(chunkIndex, formatText(currentCharacter, styleTags, j, w, cache));
                            chunkIndex++;
                        }
                        styleTags.clear();
                    }
                }
            } else if (!l.getUnicodeText().isEmpty()) {
                String lineText = l.getUnicodeText();
                shapeText = lineText;
                // logger.debug("line Text is " + lineText);
                styleTags.addAll(l.getTextStyleTags());
                for (int j = 0; j < lineText.length(); ++j) {
                    String currentCharacter = lineText.substring(j, j + 1);
                    chunkList.add(j, formatText(currentCharacter, styleTags, j, l, cache));
                }
            } else // empty shape
            {
                logger.debug("empty shape ");
                continue;
            }
            Phrase phrase = new Phrase();
            // trim is important to get the 'real' first char for rtl definition
            boolean rtl = textIsRTL(shapeText.trim());
            if (rtl) {
                logger.debug("&&&&&&&& STRING IS RTL : ");
            }
            for (int j = chunkList.size() - 1; j >= 0; j--) {
                if (rtl) {
                    phrase.add(chunkList.get(j));
                } else {
                    phrase.addAll(chunkList);
                    break;
                }
            }
            // phrase.addAll(chunkList);
            // logger.debug("curr phrase is: " + phrase.getContent());
            // compute rotation of text, if rotation higher PI/16 than rotate otherwise even text
            /*
				 * No rotation for single lines in a overall horizontal text region 
				 * Reason: Vertical line uses too much space - calculated for horizontal
				 */
            double rotation = 0;
            if (isVerticalRegion) {
                rotation = (baseline != null ? computeRotation(baseline) : 0);
                if (rotation != 0) {
                    /*
						 * if we rotate e.g. 90° than we should use the actual x location of the line
						 * so vertical text must be treated different than horizontal text 
						 */
                    if (baseLineRect != null) {
                        if (rtl) {
                            tmpLineStartX = (float) baseLineRect.getMaxX();
                        } else {
                            tmpLineStartX = (float) baseLineRect.getMinX();
                        }
                        lineStartY = (float) baseLineRect.getMaxY();
                    } else if (lineRect != null) {
                        tmpLineStartX = lineRect.x;
                        lineStartY = (float) lineRect.getMaxY();
                    }
                }
            }
            // blacken Strings if wanted
            // Set<Entry<CustomTag, String>> blackSet = CustomTagUtils.getAllTagsOfThisTypeForShapeElement(l, RegionTypeUtil.BLACKENING_REGION.toLowerCase()).entrySet();
            // 
            // if (!lineText.equals("") && 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 : " + lineText);
            // lineText = lineText.replaceAll(".", "*");
            // }
            // else{
            // lineText = blackenString(currEntry, lineText);
            // //logger.debug("lineText after blackened : " + lineText);
            // }
            // }
            // }
            // for rtl export
            float lineEndX = 0;
            float width = 0;
            if (baseLineRect != null) {
                lineEndX = (float) baseLineRect.getMaxX();
                width = (float) baseLineRect.getWidth();
            // this leads to an extra start for each line instead of having a combined start for all lines in a region
            // tmpLineStartX = (float) (lineEndX - baseLineRect.getWidth());
            } else if (lineRect != null) {
                lineEndX = lineRect.x + lineRect.width;
                width = (float) lineRect.getWidth();
            }
            // mainly for very small regions at the very left of a page
            if (tmpLineStartX > lineEndX) {
                lineEndX = tmpLineStartX + width;
            }
            // logger.debug("width " + width);
            // logger.debug("lineEndX " + lineEndX);
            // first add uniform String (=line), ,after that eventaully highlight the tags in this line using the current line information like x/y position,
            // addUniformString(lineMeanHeight, tmpLineStartX, lineStartY, lineText, cb, cutoffLeft, cutoffTop, bf, twelfthPoints[1][0], false, null, rotation);
            addUniformString(tr.getBoundingBox(), lineMeanHeight, tmpLineStartX, lineStartY, lineEndX, phrase, cb, cutoffLeft, cutoffTop, bf, twelfthPoints[1][0], false, null, rotation, rtl);
        /*
				 * old:
				 * highlight all tags of this text line if property is set
				 * no highlighting is done during chunk formatting and not in an extra step
				 */
        // if (highlightTags){
        // 
        // 
        // Set<Entry<CustomTag, String>> entrySet = CustomTagUtils.getAllTagsForShapeElement(l).entrySet();
        // 
        // highlightUniformString(entrySet, tmpLineStartX, lineStartY, l, cb, cutoffLeft, cutoffTop, bf);
        // 
        // List<WordType> words = l.getWord();
        // for(WordType wt : words){
        // TrpWordType w = (TrpWordType)wt;
        // 
        // Set<Entry<CustomTag, String>> entrySet2 = CustomTagUtils.getAllTagsForShapeElement(w).entrySet();
        // 
        // highlightUniformString(entrySet2, tmpLineStartX, lineStartY, l, cb, cutoffLeft, cutoffTop, bf);
        // }
        // 
        // }
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Phrase(com.itextpdf.text.Phrase) Chunk(com.itextpdf.text.Chunk) TrpWordType(eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType) Point(java.awt.Point) 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) TextStyleTag(eu.transkribus.core.model.beans.customtags.TextStyleTag) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)

Example 7 with TrpTextRegionType

use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addUniformText.

private void addUniformText(PcGtsType pc, int cutoffLeft, int cutoffTop, ExportCache cache) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContentUnder();
    cb.setColorFill(BaseColor.BLACK);
    cb.setColorStroke(BaseColor.BLACK);
    /**
     * The path to the font.
     */
    // FontFactory.register("c:/windows/fonts/arialbd.ttf");
    // BaseFont bf = BaseFont.createFont("/fonts/arialbd.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    cb.beginLayer(ocrLayer);
    // FontFactory.register("arialbd.ttf", "my_bold_font");
    // Font fontTest = FontFactory.getFont("arialbd.ttf", Font.BOLDITALIC);
    cb.setFontAndSize(bfArial, 10);
    List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
    /*
		 * use reading order comparator for sorting since at this time reading order is more trustable
		 * other sorting is not transitive and seldomly produces "Comparison violates its general contract" exception
		 */
    Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true));
    // Collections.sort(regions, new TrpElementCoordinatesComparator<RegionType>());
    float textBlockXStart = 0;
    int i = 0;
    for (TrpRegionType r : regions) {
        // TODO add paths for tables etc.
        if (r instanceof TrpTableRegionType) {
            exportTable(r, cb, cutoffLeft, cutoffTop, true, cache);
        } else if (r instanceof TrpTextRegionType) {
            TrpTextRegionType tr = (TrpTextRegionType) r;
            // compute average text region start
            // textBlockXStart = (float) (PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX());
            // double minX = PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
            // this should result in the the same value as the method in the line above which is deprecated
            double minX = tr.getBoundingBox().getMinX();
            double maxX = tr.getBoundingBox().getMaxX();
            double trWidth = tr.getBoundingBox().getWidth();
            // if (hasSmallerColumn(regions, tr)){
            if (isOnlyRegionInThisRow(regions, tr)) {
                // if (regions.size() == 1){
                logger.debug("only one region in this row!!");
                // indent start of text block under certain preconditions
                if (minX < twelfthPoints[1][0] && (twelfthPoints[1][0] < maxX && trWidth > twelfthPoints[2][0])) {
                    textBlockXStart = twelfthPoints[1][0];
                } else // if textregion contains only one line this is probably a headline
                if (tr.getTextLine().size() == 1) {
                    // logger.debug("tr.getTextLine().size() == 1 ");
                    textBlockXStart = getPrintregionStartX((float) (minX), tr.getBoundingBox().getMaxX());
                } else if (twelfthPoints[2][0] < maxX && trWidth > twelfthPoints[3][0]) {
                    // logger.debug("twelfthPoints[2][0] < tr.getBoundingBox().getMaxX() ");
                    textBlockXStart = twelfthPoints[2][0];
                } else {
                    textBlockXStart = (float) minX;
                }
            } else {
                logger.debug("several columns found, minX of text region is : " + minX);
                // float startWithThisX = (float) (minX < smallerRegionMaxX ? smallerRegionMaxX : minX);
                // textBlockXStart = getPrintregionStartX((float) (startWithThisX));
                /*
					 * this is then used for all lines of a region as start point
					 */
                textBlockXStart = getAverageBeginningOfBaselines(tr);
                textBlockXStart += 40;
            }
            // logger.debug("textBlockXStart " + textBlockXStart);
            addUniformTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, bfArial, textBlockXStart, cache);
        }
    }
    cb.endLayer();
// addTocLinks(doc, page,cutoffTop);
}
Also used : 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) TrpTableRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Point(java.awt.Point)

Example 8 with TrpTextRegionType

use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType in project TranskribusCore by Transkribus.

the class CustomTagListTest method testCommonTag.

@Test
public void testCommonTag() {
    TrpTextLineType line = new TrpTextLineType(new TrpTextRegionType(new TrpPageType()));
    line.setUnicodeText("Hello world!", null);
    CustomTagList tl = new CustomTagList(line);
    TextStyleTag ts1 = new TextStyleTag(0, 10);
    ts1.setBold(true);
    tl.addOrMergeTag(ts1, null);
    logger.debug("ts1 = " + tl);
    TextStyleTag ts2 = new TextStyleTag(3, 3);
    ts2.setItalic(true);
    tl.addOrMergeTag(ts2, "italic");
    logger.debug("ts2 = " + tl);
    // ts2.setFontFamily("hello");
    // tl.addOrMergeTag(ts2, null);
    // TextStyleTag t1 = tl.getCommonIndexedCustomTag(TextStyleTag.TAG_NAME, 3, 3);
    // logger.debug("t1 = "+t1);
    Assert.assertEquals("Nr. of merged elements must be 3", 3, tl.getTags().size());
    TextStyleTag common = tl.getCommonIndexedCustomTag(TextStyleTag.TAG_NAME, 2, 6);
    TextStyleTag check = new TextStyleTag();
    check.setBold(true);
    check.setOffset(2);
    check.setLength(6);
    logger.debug("common = " + common);
    logger.debug("check = " + common);
    Assert.assertTrue("", common.equalsEffectiveValues(check, true));
}
Also used : TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TextStyleTag(eu.transkribus.core.model.beans.customtags.TextStyleTag) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) CustomTagList(eu.transkribus.core.model.beans.customtags.CustomTagList) TrpPageType(eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType) Test(org.junit.Test)

Example 9 with TrpTextRegionType

use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType in project TranskribusCore by Transkribus.

the class CustomTagListTest method testMultipleRandomIndexedAddOrMergeTag.

// @Ignore
@Test
public void testMultipleRandomIndexedAddOrMergeTag() {
    TrpTextLineType line = new TrpTextLineType(new TrpTextRegionType(new TrpPageType()));
    line.setUnicodeText("Hello world!", null);
    CustomTagList tl = new CustomTagList(line);
    int textLength = tl.getTextLength();
    CustomTag wholeRangeTag = new CustomTag("test", 0, textLength);
    String[] nonIndexedTags = new String[] { "a_non_indexed", "b_ni", "c_balbla_non_indexed" };
    // + overlap to test exceptions when index out of bounds!
    int rangeOfTags = textLength + 5;
    final int N = (int) 1e3;
    for (int i = 0; i < N; ++i) {
        // int sizeBefore = tl.getIndexedTags("test").size();
        int o = rand.nextInt(rangeOfTags);
        int l = rand.nextInt(rangeOfTags - o) + 1;
        CustomTag ct = null;
        int d = rand.nextInt(3);
        if (d == 0) {
            // structure tag
            ct = new CustomTag("a_test_indexed", o, l);
        } else if (d == 1) {
            // text style tag
            ct = new TextStyleTag(o, l);
            ((TextStyleTag) ct).setBold(rand.nextBoolean());
            ((TextStyleTag) ct).setItalic(rand.nextBoolean());
            ((TextStyleTag) ct).setMonospace(rand.nextBoolean());
        } else {
            ct = new CustomTag(nonIndexedTags[rand.nextInt(3)]);
        // ct = new CustomTag("non-indexed");
        }
        // logger.info("range: "+o+","+l);
        // CustomTag ct = new CustomTag("test", o, l);
        logger.trace("i=" + i + "/" + N);
        logger.trace("adding custom tag: " + ct);
        logger.trace("list before = " + tl);
        try {
            tl.addOrMergeTag(ct, null);
            Assert.assertTrue("Indexed CustomTag was not inside but no exception thrown: " + ct, !ct.isIndexed() || wholeRangeTag.getOverlapType(ct) == OverlapType.INSIDE);
        } catch (IndexOutOfBoundsException ie) {
            Assert.assertTrue("CustomTag was inside but exception thrown: " + ct, wholeRangeTag.getOverlapType(ct) != OverlapType.INSIDE);
            logger.trace("Exception for tag not inside: " + ct);
        }
        logger.trace("list after = " + tl);
        checkIntegrity(tl);
    // int sizeAfter = tl.getIndexedTags("test").size();
    // logger.debug("sizeAfter = "+sizeAfter);
    }
    logger.info("list = " + tl);
}
Also used : TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TextStyleTag(eu.transkribus.core.model.beans.customtags.TextStyleTag) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) CustomTagList(eu.transkribus.core.model.beans.customtags.CustomTagList) TrpPageType(eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType) Test(org.junit.Test)

Example 10 with TrpTextRegionType

use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType in project TranskribusCore by Transkribus.

the class CustomTagListTest method testAddBothOverlap.

// @Ignore
@Test
public void testAddBothOverlap() {
    TrpTextLineType line = new TrpTextLineType(new TrpTextRegionType(new TrpPageType()));
    line.setUnicodeText("Hello world and all parallel worlds!", null);
    // line.setUnicodeText("hello", who);
    CustomTagList tl = new CustomTagList(line);
    // int l=10;
    TextStyleTag ts1 = new TextStyleTag(2, 3);
    ts1.setBold(true);
    ts1.setItalic(true);
    tl.addOrMergeTag(ts1, null);
    logger.debug("tl1 = " + tl);
    TextStyleTag ts2 = new TextStyleTag(6, 2);
    ts2.setBold(true);
    ts2.setSuperscript(true);
    tl.addOrMergeTag(ts2, null);
    logger.debug("tl2 = " + tl);
    TextStyleTag ts3 = new TextStyleTag(9, 11);
    tl.addOrMergeTag(ts3, null);
    logger.debug("tl3 = " + tl);
    Assert.assertEquals("Nr. of merged elements must be 2 --> empty tags shall be removed!", 2, tl.getTags().size());
}
Also used : TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TextStyleTag(eu.transkribus.core.model.beans.customtags.TextStyleTag) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) CustomTagList(eu.transkribus.core.model.beans.customtags.CustomTagList) TrpPageType(eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType) Test(org.junit.Test)

Aggregations

TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)20 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)14 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)9 TrpWordType (eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType)7 CustomTagList (eu.transkribus.core.model.beans.customtags.CustomTagList)5 TextStyleTag (eu.transkribus.core.model.beans.customtags.TextStyleTag)5 TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)5 WordType (eu.transkribus.core.model.beans.pagecontent.WordType)5 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)4 RegionType (eu.transkribus.core.model.beans.pagecontent.RegionType)4 TrpRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 TrpPage (eu.transkribus.core.model.beans.TrpPage)3 TrpTableRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType)3 Point (java.awt.Point)3 Rectangle (java.awt.Rectangle)3 ArrayList (java.util.ArrayList)3 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)2 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)2