Search in sources :

Example 6 with CustomTag

use of eu.transkribus.core.model.beans.customtags.CustomTag in project TranskribusCore by Transkribus.

the class TrpPdfDocument method highlightTagsForShape.

private void highlightTagsForShape(ITrpShapeType shape, boolean rtl, ExportCache cache) throws IOException {
    int tagId = 0;
    int k = 1;
    Set<Entry<CustomTag, String>> entrySet = ExportUtils.getAllTagsForShapeElement(shape).entrySet();
    // Set<String> wantedTags = ExportUtils.getOnlyWantedTagnames(CustomTagFactory.getRegisteredTagNames());
    Set<String> wantedTags = cache.getOnlySelectedTagnames(CustomTagFactory.getRegisteredTagNames());
    // logger.debug("wanted tags in TRPPDFDOC " + wantedTags.size());
    int[] prevLength = new int[entrySet.size()];
    int[] prevOffset = new int[entrySet.size()];
    boolean falling = true;
    BaselineType baseline = null;
    if (shape instanceof TrpTextLineType) {
        TrpTextLineType l = (TrpTextLineType) shape;
        baseline = l.getBaseline();
    } else if (shape instanceof TrpWordType) {
        TrpWordType w = (TrpWordType) shape;
        TrpTextLineType l = (TrpTextLineType) w.getParentShape();
        baseline = l.getBaseline();
    }
    try {
        List<Point> ptsList = null;
        if (baseline != null) {
            ptsList = PointStrUtils.parsePoints(baseline.getPoints());
        }
        if (ptsList != null) {
            int size = ptsList.size();
            // logger.debug("l.getBaseline().getPoints() " + l.getBaseline().getPoints());
            if (size >= 2 && ptsList.get(0).y < ptsList.get(size - 1).y) {
                // logger.debug("falling is false ");
                falling = false;
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (Map.Entry<CustomTag, String> currEntry : entrySet) {
        if (wantedTags.contains(currEntry.getKey().getTagName())) {
            String color = CustomTagFactory.getTagColor(currEntry.getKey().getTagName());
            int currLength = currEntry.getKey().getLength();
            int currOffset = currEntry.getKey().getOffset();
            /**
             * if the current tag overlaps one of the previous tags
             * -> increase the distance of the line under the textline
             */
            // if (isOverlaped(prevOffset, prevLength, currOffset, currLength)){
            // k++;
            // }
            // else{
            // k=1;
            // }
            k = getAmountOfOverlaps(prevOffset, prevLength, currOffset, currLength);
            // logger.debug("current tag name "+ currEntry.getKey().getTagName() + " k is " + k);
            // logger.debug("current tag text "+ currEntry.getKey().getContainedText());
            prevOffset[tagId] = currOffset;
            prevLength[tagId] = currLength;
            tagId++;
            float yShift = (lineMeanHeight / 6) * k;
            /*
				 * remember where to draw line with help of a list
				 */
            if (baseline != null) {
                // use lowest point in baseline and move up one half of the distance to the topmost point
                // java.awt.Rectangle baseLineRect = PageXmlUtils.buildPolygon(baseline.getPoints()).getBounds();
                java.awt.Rectangle baseLineRect = ((TrpBaselineType) baseline).getBoundingBox();
                calculateTagLines(baseLineRect, shape, currEntry.getKey().getContainedText(), currOffset, currLength, color, yShift, falling, rtl);
            }
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) Point(java.awt.Point) TrpWordType(eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType) Point(java.awt.Point) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DocumentException(com.itextpdf.text.DocumentException) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) TrpBaselineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType) Entry(java.util.Map.Entry) BaselineType(eu.transkribus.core.model.beans.pagecontent.BaselineType) TrpBaselineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with CustomTag

use of eu.transkribus.core.model.beans.customtags.CustomTag in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addTags.

public void addTags(TrpDoc doc, Set<Integer> pageIndices, boolean useWordLevel2, ExportCache cache) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContentUnder();
    document.newPage();
    int l = 0;
    float posY;
    // BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED, true, null, null);
    Set<String> wantedTags = cache.getOnlySelectedTagnames(CustomTagFactory.getRegisteredTagNames());
    // logger.debug("selectedTags Size " + selectedTags.size());
    for (String currTagname : wantedTags) {
        double lineHeight = 12 / scaleFactorY;
        double lineGap = 4 / scaleFactorY;
        // logger.debug("currTagname " + currTagname);
        // get all custom tags with currTagname and text
        HashMap<CustomTag, String> allTagsOfThisTagname = cache.getTags(currTagname);
        // logger.debug("all Tags Of This Tagname " + currTagname);
        if (allTagsOfThisTagname.size() > 0) {
            posY = (float) (twelfthPoints[1][1] + (lineHeight + lineGap) * l);
            if (posY > twelfthPoints[10][1]) {
                document.newPage();
                posY = twelfthPoints[1][1];
                l = 0;
            }
            l++;
            String color = CustomTagFactory.getTagColor(currTagname);
            addUniformTagList(lineHeight, twelfthPoints[1][0], posY, "", currTagname + " Tags:", "", cb, 0, 0, bfArial, twelfthPoints[1][0], false, color, 0, false);
            // addUniformStringTest(lineMeanHeight, twelfthPoints[1][0], posY, currTagname + " Tags:", cb, 0, 0, bfArial, twelfthPoints[1][0], false, color, 0);
            Collection<String> valueSet = allTagsOfThisTagname.values();
            Collection<CustomTag> keySet = allTagsOfThisTagname.keySet();
            HashSet<String> uniqueValues = new HashSet<String>();
            Iterator<CustomTag> it = keySet.iterator();
            while (it.hasNext()) {
                CustomTag currEntry = it.next();
                String currValue = allTagsOfThisTagname.get(currEntry);
                // case for gap tag
                if (currValue == null) {
                    currValue = "";
                }
                String expansion = "";
                // handles continued tags over several lines
                while (currEntry.isContinued() && it.hasNext()) {
                    currEntry = it.next();
                    if (currEntry.isContinued()) {
                        String continued = allTagsOfThisTagname.get(currEntry);
                        currValue = currValue.concat(continued);
                        // soft hyphen
                        currValue = currValue.replaceAll("\u00AD", "");
                        // minus
                        currValue = currValue.replaceAll("\u002D", "");
                        // not sign
                        currValue = currValue.replaceAll("\u00AC", "");
                    // char c = 0xFFFA; String.valueOf(c).replaceAll("\\p{C}", "?");
                    }
                }
                boolean rtl = false;
                if (!currValue.isEmpty() && textIsRTL(currValue)) {
                    rtl = true;
                    // logger.debug("rtl tag found " + currValue);
                    currValue = reverseString(currValue);
                }
                String searchText = currValue;
                if (currTagname.equals(CommentTag.TAG_NAME)) {
                    CommentTag ct = (CommentTag) currEntry;
                    if (ct.getComment() != "") {
                        if (!rtl)
                            expansion = ": " + ct.getComment();
                        else
                            expansion = ct.getComment() + " :";
                    }
                // currValue = currValue.concat(": " + ct.getComment());
                // logger.debug("comment " + currValue);
                } else if (currTagname.equals(AbbrevTag.TAG_NAME)) {
                    AbbrevTag at = (AbbrevTag) currEntry;
                    if (at.getExpansion() != "")
                        if (!rtl)
                            expansion = ": " + at.getExpansion();
                        else
                            expansion = at.getExpansion() + " :";
                } else if (currTagname.equals(GapTag.TAG_NAME)) {
                    GapTag at = (GapTag) currEntry;
                    currValue = currEntry.getTextOfShape();
                    searchText = currValue;
                    int offset = Math.max(at.getOffset(), currValue.length() - 1);
                    String sub1 = currValue.substring(0, offset);
                    String sub2 = currValue.substring(offset);
                    String exp = (String) at.getAttributeValue("supplied");
                    if (exp != null && exp != "") {
                        currValue = sub1.concat("[" + exp + "]").concat(sub2);
                    // expansion = "[" + (String) at.getAttributeValue("supplied") + "]";
                    } else // no supplied attribute - gap must not be in the tag list
                    {
                        continue;
                    }
                } else if (currTagname.equals(SuppliedTag.TAG_NAME)) {
                }
                // make sure that similar tags are only exported once
                if (!uniqueValues.contains(currValue)) {
                    uniqueValues.add(currValue);
                    posY = (float) (twelfthPoints[1][1] + (lineHeight + lineGap) * l);
                    if (posY > twelfthPoints[11][1]) {
                        document.newPage();
                        posY = twelfthPoints[1][1];
                        l = 1;
                    }
                    addUniformTagList(lineHeight, twelfthPoints[1][0], posY, searchText, currValue, expansion, cb, 0, 0, bfArial, twelfthPoints[1][0], true, null, 0, rtl);
                    // logger.debug("tag value is " + currValue);
                    l++;
                }
            }
            l++;
        }
    }
}
Also used : CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) Point(java.awt.Point) CommentTag(eu.transkribus.core.model.beans.customtags.CommentTag) GapTag(eu.transkribus.core.model.beans.customtags.GapTag) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) AbbrevTag(eu.transkribus.core.model.beans.customtags.AbbrevTag) HashSet(java.util.HashSet)

Example 8 with CustomTag

use of eu.transkribus.core.model.beans.customtags.CustomTag in project TranskribusCore by Transkribus.

the class TrpPdfDocument method highlightUniformString.

// not used anymore
private void highlightUniformString(Set<Entry<CustomTag, String>> entrySet, float tmpLineStartX, float lineStartY, TrpTextLineType l, PdfContentByte cb, int cutoffLeft, int cutoffTop, BaseFont bf) throws IOException {
    int k = 1;
    int tagId = 0;
    int[] prevLength = new int[entrySet.size()];
    int[] prevOffset = new int[entrySet.size()];
    for (Map.Entry<CustomTag, String> currEntry : entrySet) {
        Set<String> wantedTags = ExportUtils.getOnlyWantedTagnames(CustomTagFactory.getRegisteredTagNames());
        if (wantedTags.contains(currEntry.getKey().getTagName())) {
            // logger.debug("current tag text "+ currEntry.getKey().getContainedText());
            String color = CustomTagFactory.getTagColor(currEntry.getKey().getTagName());
            int currLength = currEntry.getKey().getLength();
            int currOffset = currEntry.getKey().getOffset();
            /**
             * if the current tag overlaps one of the previous tags
             * -> increase the distance of the line under the textline
             */
            if (isOverlaped(prevOffset, prevLength, currOffset, currLength)) {
                k++;
            } else {
                k = 1;
            }
            prevOffset[tagId] = currOffset;
            prevLength[tagId] = currLength;
            tagId++;
            // yShift -> vertical shift of underline if several tags are at the same position
            float yShift = (lineMeanHeight / 6) * k;
            highlightUniformTagString(lineMeanHeight, tmpLineStartX, lineStartY, l.getUnicodeText(), currEntry.getKey().getContainedText(), cb, cutoffLeft, cutoffTop, bf, twelfthPoints[1][0], color, yShift, currOffset);
        }
    }
}
Also used : CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) Map(java.util.Map) HashMap(java.util.HashMap) Point(java.awt.Point)

Example 9 with CustomTag

use of eu.transkribus.core.model.beans.customtags.CustomTag in project TranskribusCore by Transkribus.

the class TrpTeiStringBuilder method getTaggedContent.

String getTaggedContent(ITrpShapeType shape) {
    CustomTagList cl = shape.getCustomTagList();
    List<CustomTag> ctList = new ArrayList<CustomTag>();
    // for (CustomTag t : cl.getTags()) {
    for (CustomTag t : cl.getIndexedTags()) {
        ctList.add(t.copy());
    }
    Collections.sort(ctList);
    String text = shape.getUnicodeText();
    // escape the shape text here - later on the tag elements would be escaped too
    String escapedText = escapeShapeText(text, ctList);
    logger.trace("ShapeText = " + text + " escaped: " + escapedText);
    for (CustomTag t : ctList) {
        if (commonPars.isTagSelected(t.getTagName()) || (commonPars.isDoBlackening() && t.getTagName().equals(BlackeningTag.TAG_NAME)) || t.getTagName().equals(TextStyleTag.TAG_NAME)) {
            escapedText = insertTag(escapedText, t, ctList);
        }
    }
    logger.trace("escaped text after tag insertion: " + escapedText);
    // replace blackened text:
    if (commonPars.isDoBlackening()) {
        escapedText = hideBlackenedText(escapedText);
    }
    return escapedText;
}
Also used : ArrayList(java.util.ArrayList) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) CustomTagList(eu.transkribus.core.model.beans.customtags.CustomTagList)

Example 10 with CustomTag

use of eu.transkribus.core.model.beans.customtags.CustomTag in project TranskribusCore by Transkribus.

the class TrpPageUnmarshalListener method syncTags.

/**
 * sync tags with registry for each and every shape
 */
private void syncTags(Object target) {
    if (!(target instanceof ITrpShapeType)) {
        return;
    }
    ITrpShapeType st = (ITrpShapeType) target;
    // manually call setter method for custom tag as JAXB does not call setters!
    st.setCustom(st.getCustom());
    if (st.getCustomTagList() != null) {
        // try registering (possibly new) tags:
        for (CustomTag t : st.getCustomTagList().getTags()) {
            try {
                boolean mergeAttributes = false;
                CustomTagFactory.addToRegistry(t, null, mergeAttributes);
            } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) {
                logger.error("Could not register the tag: " + t.getCssStr() + ", reason: " + e.getMessage(), e);
            }
        }
    }
}
Also used : CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) ITrpShapeType(eu.transkribus.core.model.beans.pagecontent_trp.ITrpShapeType) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CustomTag (eu.transkribus.core.model.beans.customtags.CustomTag)19 CustomTagList (eu.transkribus.core.model.beans.customtags.CustomTagList)9 TextStyleTag (eu.transkribus.core.model.beans.customtags.TextStyleTag)5 IOException (java.io.IOException)5 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)4 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)4 Point (java.awt.Point)4 ArrayList (java.util.ArrayList)4 AbbrevTag (eu.transkribus.core.model.beans.customtags.AbbrevTag)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 RtfText (com.tutego.jrtf.RtfText)2 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)2 TrpPage (eu.transkribus.core.model.beans.TrpPage)2 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)2 CommentTag (eu.transkribus.core.model.beans.customtags.CommentTag)2 GapTag (eu.transkribus.core.model.beans.customtags.GapTag)2 TrpBaselineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType)2 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)2