Search in sources :

Example 1 with RtfText

use of com.tutego.jrtf.RtfText in project TranskribusCore by Transkribus.

the class TrpRtfBuilder method writeRtfForDoc.

public static void writeRtfForDoc(TrpDoc doc, boolean wordBased, boolean writeTags, boolean doBlackening, File file, Set<Integer> pageIndices, IProgressMonitor monitor, ExportCache cache) throws JAXBException, IOException {
    exportTags = writeTags;
    tagnames = cache.getSelectedTags();
    TrpRtfBuilder.doBlackening = doBlackening;
    /*
		 * get all names of tags
		 */
    // tagnames = CustomTagFactory.getRegisteredTagNames();
    Rtf rtf = Rtf.rtf();
    List<TrpPage> pages = doc.getPages();
    int totalPages = pageIndices == null ? pages.size() : pageIndices.size();
    if (monitor != null) {
        monitor.beginTask("Exporting to RTF", totalPages);
    }
    int c = 0;
    for (int i = 0; i < pages.size(); ++i) {
        if (pageIndices != null && !pageIndices.contains(i))
            continue;
        if (monitor != null) {
            if (monitor.isCanceled()) {
                logger.debug("RTF export cancelled!");
                return;
            }
            monitor.subTask("Processing page " + (c + 1));
        }
        TrpPage page = pages.get(i);
        TrpTranscriptMetadata md = page.getCurrentTranscript();
        JAXBPageTranscript tr = new JAXBPageTranscript(md);
        tr.build();
        TrpPageType trpPage = tr.getPage();
        logger.debug("writing rtf for page " + (i + 1) + "/" + doc.getNPages());
        // rtf().header(color( 204, 0, 0 ).at( 0 ),
        // color( 0, 0xff, 0 ).at( 1 ),
        // color( 0, 0, 0xff ).at( 2 ),
        // font( "Calibri" ).at( 0 ) );
        // RtfHeaderColor color = RtfHeaderColor.color(0xff, 0, 0);
        rtf.header(color(204, 0, 0).at(0), color(0, 0xff, 0).at(1)).section(getRtfParagraphsForTranscript(trpPage, wordBased));
        ++c;
        if (monitor != null) {
            monitor.worked(c);
        }
    }
    // write tags at end of last page
    if (exportTags) {
        // RtfText headline = RtfText.text("Person names in this document (amount of found persons: " + persons.size() + ")", "\n");
        /*
			 * for all different tagnames:
			 * find all custom tags in doc
			 * create list and 
			 */
        ArrayList<RtfPara> tagParas = new ArrayList<RtfPara>();
        // tagnames = all user choosen tags via export dialog
        for (String currTagname : tagnames) {
            // logger.debug("curr tagname " + currTagname);
            // get all custom tags with currTagname and text
            HashMap<CustomTag, String> allTagsOfThisTagname = cache.getTags(currTagname);
            if (allTagsOfThisTagname.size() > 0) {
                tagParas.add(RtfPara.p(RtfText.text(RtfText.underline(currTagname + " tags in this document: " + allTagsOfThisTagname.size()))));
                // ArrayList<RtfText> tagTexts = new ArrayList<RtfText>();
                Collection<String> valueSet = allTagsOfThisTagname.values();
                RtfText[] tagTexts = new RtfText[valueSet.size()];
                int l = 0;
                for (String currEntry : valueSet) {
                    tagTexts[l++] = RtfText.text(currEntry.concat("\n"));
                // logger.debug("tag value is " + currEntry);
                }
                tagParas.add(RtfPara.p(tagTexts));
            }
        }
        // int parSize = getParsNumber();
        // int k = 0;
        // 
        // if (persons.size() > 0){
        // logger.debug("k is " + k);
        // List<String> newPersonList = new ArrayList<String>(new HashSet<String>(persons));
        // tagParas[k++]=RtfPara.p(RtfText.text("Person names in this document (amount of found persons: " + newPersonList.size() + ")", "\n"));
        // logger.debug("k is " + k);
        // //rtf.p("Person names in this document (amount of found persons: " + persons.size() + ")", "\n");
        // //to make the list contain only unique values
        // 
        // RtfText[] personTexts = new RtfText[newPersonList.size()];
        // for (int j=0; j<newPersonList.size(); ++j) {
        // personTexts[j] = RtfText.text(newPersonList.get(j), "\n");
        // logger.debug("person is " + newPersonList.get(j));
        // }
        // tagParas[k++] = RtfPara.p(personTexts);
        // }
        // 
        // if (places.size() > 0){
        // List<String> newPlaceList = new ArrayList<String>(new HashSet<String>(places));
        // tagParas[k++]=RtfPara.p(RtfText.text("Places in this document (amount of found places " + newPlaceList.size() + ")", "\n"));
        // 
        // RtfText[] placeTexts = new RtfText[newPlaceList.size()];
        // for (int j=0; j<newPlaceList.size(); ++j) {
        // //RtfText.color(0, "red");
        // placeTexts[j] = RtfText.color(0, newPlaceList.get(j).concat("\n"));
        // logger.debug("place is " + newPlaceList.get(j));
        // }
        // RtfPara par2 = RtfPara.p(placeTexts);
        // tagParas[k++] = par2;
        // }
        // 
        // if(addresses.size() > 0){
        // List<String> newAddressList = new ArrayList<String>(new HashSet<String>(addresses));
        // tagParas[k++]=RtfPara.p(RtfText.text("Addresses in this document (amount of found addresses " + newAddressList.size() + ")", "\n"));
        // 
        // RtfText[] addresseTexts = new RtfText[newAddressList.size()];
        // for (int j=0; j<newAddressList.size(); ++j) {
        // addresseTexts[j] = RtfText.text(newAddressList.get(j), "\n");
        // logger.debug("addresse is " + newAddressList.get(j));
        // }
        // RtfPara par3 = RtfPara.p(addresseTexts);
        // tagParas[k++] = par3;
        // }
        // rtf.section(par3);
        rtf.header(color(204, 0, 0).at(0)).section(tagParas);
    }
    rtf.out(new FileWriter(file));
    logger.info("wrote rtf to: " + file.getAbsolutePath());
}
Also used : JAXBPageTranscript(eu.transkribus.core.model.beans.JAXBPageTranscript) Rtf(com.tutego.jrtf.Rtf) TrpPage(eu.transkribus.core.model.beans.TrpPage) RtfText(com.tutego.jrtf.RtfText) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) RtfPara(com.tutego.jrtf.RtfPara) TrpPageType(eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)

Example 2 with RtfText

use of com.tutego.jrtf.RtfText in project TranskribusCore by Transkribus.

the class TrpRtfBuilder method getRtfParagraphsForTranscript.

// public static void writeRtfForElement(Rtf rtf, ITrpShapeType element, boolean wordBased, File file, boolean append) throws IOException, JAXBException {
// element.getUnicodeText();
// CustomTagList cl = element.getCustomTagList();
// 
// RtfText text = RtfText.text(element.getUnicodeText());
// text = formatRtfText(text, element.getTextStyle());
// 
// 
// 
// if (element instanceof TextLineType || element instanceof TextRegionType) {// TODO words vs lines and regions
// rtf.p(text);
// } else if (element instanceof TrpWordType) {
// //			rtf.p(texts);
// }
// 
// 
// //		cl.getCustomTagAndContinuations(tag)
// 
// 
// 
// }
public static RtfPara[] getRtfParagraphsForTranscript(TrpPageType trpPage, boolean wordBased) throws IOException, JAXBException {
    boolean rtl = false;
    List<TrpTextRegionType> textRegions = trpPage.getTextRegions(true);
    // List<TrpTextRegionType> textRegions = trpPage.getTextRegionsAndTextRegionsFromTableRegions(true);
    RtfPara[] paras = new RtfPara[textRegions.size()];
    for (int j = 0; j < textRegions.size(); ++j) {
        TrpTextRegionType r = textRegions.get(j);
        // if (exportTags){
        // getTagsForShapeElement(r);
        // }
        List<TextLineType> lines = r.getTextLine();
        RtfText[] linesTexts = new RtfText[lines.size()];
        for (int i = 0; i < lines.size(); ++i) {
            TrpTextLineType trpL = (TrpTextLineType) lines.get(i);
            linesTexts[i] = (wordBased && trpL.getWord().size() > 0) ? getRtfTextForLineFromWords(trpL) : getRtfTextForShapeElement(trpL);
            linesTexts[i] = RtfText.text(linesTexts[i], "\n");
        }
        // read from right to left -> alignment is right
        if (rtl) {
        // paras[j] = RtfPara.p(linesTexts).footnote("Test").alignRight();
        } else {
            String test = "test";
            paras[j] = RtfPara.p(linesTexts);
        // paras[j] = RtfPara.p(linesTexts, RtfText.footnote("Test")).alignLeft();
        }
    }
    return paras;
// Rtf rtf = Rtf.rtf().section(paras);
// return rtf;
// for (RegionType r : trpPage.getTextRegionOrImageRegionOrLineDrawingRegion()) {
// if (r instanceof GraphicRegionType) {
// GraphicRegionType gr = (GraphicRegionType) r;
// // TODO: how to export images in pdf??
// r.getTextRegions(recursive);
// }
// }
// tr.getPage().getTextRegions(recursive);
// Rtf.rtf();
// RtfWriter;
}
Also used : RtfPara(com.tutego.jrtf.RtfPara) TrpTextLineType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType) 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) RtfText(com.tutego.jrtf.RtfText)

Example 3 with RtfText

use of com.tutego.jrtf.RtfText in project TranskribusCore by Transkribus.

the class TrpRtfBuilder method getRtfTextForLineFromWords.

// private static void getTagsForShapeElement(ITrpShapeType element) throws IOException{
// 
// String textStr = element.getUnicodeText();
// CustomTagList cl = element.getCustomTagList();
// if (textStr == null || cl == null)
// throw new IOException("Element has no text or custom tag list: "+element+", class: "+element.getClass().getName());
// 
// for (CustomTag nonIndexedTag : cl.getNonIndexedTags()) {
// 
// logger.debug("nonindexed tag found ");
// storeCustomTag(nonIndexedTag, textStr);
// 
// }
// for (CustomTag indexedTag : cl.getIndexedTags()) {
// 
// logger.debug("indexed tag found ");
// storeCustomTag(indexedTag, textStr);
// 
// }
// 
// }
// 
// private static void storeCustomTag(CustomTag currTag, String textStr) {
// if (!currTag.getTagName().equals("textStyle")){
// 
// if (currTag.getOffset() != -1 && currTag.getLength() != -1 && (currTag.getOffset()+currTag.getLength() <= textStr.length())){
// tags.put(currTag, textStr.substring(currTag.getOffset(), currTag.getOffset()+currTag.getLength()));
// }
// else{
// tags.put(currTag, textStr);
// }
// logger.debug("++tag name is " + currTag.getTagName());
// logger.debug("text " + tags.get(currTag));
// }
// 
// if (currTag.getTagName().equals("Person")){
// if (currTag.getOffset() != -1 && currTag.getLength() != -1 && (currTag.getOffset()+currTag.getLength() <= textStr.length())){
// persons.add(textStr.substring(currTag.getOffset(), currTag.getOffset()+currTag.getLength()));
// }
// else{
// logger.debug("with index is something wrong: offset " + currTag.getOffset() + " length " + currTag.getLength()) ;
// //throw new Exception("Something wrong with indexed tag for text: " + textStr);
// }
// }
// else if (currTag.getTagName().equals("Place")){
// if (currTag.getOffset() != -1 && currTag.getLength() != -1 && (currTag.getOffset()+currTag.getLength() <= textStr.length())){
// places.add(textStr.substring(currTag.getOffset(), currTag.getOffset()+currTag.getLength()));
// }
// }
// 
// }
private static RtfText getRtfTextForLineFromWords(TrpTextLineType line) throws IOException {
    List<WordType> words = line.getWord();
    RtfText[] wordTexts = new RtfText[words.size()];
    for (int i = 0; i < wordTexts.length; ++i) {
        TrpWordType w = (TrpWordType) words.get(i);
        wordTexts[i] = getRtfTextForShapeElement(w);
    }
    RtfText totalText = RtfText.text(true, wordTexts);
    return totalText;
}
Also used : RtfText(com.tutego.jrtf.RtfText) 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)

Example 4 with RtfText

use of com.tutego.jrtf.RtfText in project TranskribusCore by Transkribus.

the class TrpRtfBuilder method getRtfTextForShapeElement.

private static RtfText getRtfTextForShapeElement(ITrpShapeType element) throws IOException {
    String textStr = element.getUnicodeText();
    CustomTagList cl = element.getCustomTagList();
    if (textStr == null || cl == null)
        throw new IOException("Element has no text or custom tag list: " + element + ", class: " + element.getClass().getName());
    if (doBlackening) {
        // format according to tags:CustomTagList
        for (CustomTag nonIndexedTag : cl.getNonIndexedTags()) {
            if (nonIndexedTag.getTagName().equals(RegionTypeUtil.BLACKENING_REGION.toLowerCase())) {
                // logger.debug("nonindexed tag found ");
                textStr = ExportUtils.blackenString(nonIndexedTag, textStr);
            }
        }
        for (CustomTag indexedTag : cl.getIndexedTags()) {
            if (indexedTag.getTagName().equals(RegionTypeUtil.BLACKENING_REGION.toLowerCase())) {
                // logger.debug("nonindexed tag found ");
                textStr = ExportUtils.blackenString(indexedTag, textStr);
            }
        }
    }
    List<TextStyleTag> textStylesTags = element.getTextStyleTags();
    // if (exportTags){
    // getTagsForShapeElement(element);
    // }
    RtfText[] chars = new RtfText[textStr.length()];
    for (int i = 0; i < textStr.length(); ++i) {
        chars[i] = RtfText.text(textStr.substring(i, i + 1));
        // format according to "global" text style
        chars[i] = formatRtfText(chars[i], element.getTextStyle());
        /*
			 * format according to custom style tag - check for each char in the text if a special style should be set
			 */
        for (TextStyleTag styleTag : textStylesTags) {
            if (i >= styleTag.getOffset() && i < (styleTag.getOffset() + styleTag.getLength())) {
                chars[i] = formatRtfText(chars[i], styleTag.getTextStyle());
            }
        }
    // // format according to tags:
    // for (String nonIndexedTag : cl.getNonIndexedTagNames()) {
    // charText = formatRtfText(charText, element);
    // // TODO
    // }
    // for (String indexedTag : cl.getIndexedTagNames()) {
    // // TODO
    // }
    // TODO: include structure types!! (also possible in custom tags!!)
    // TODO: include reading order!!!
    }
    RtfText totalText = RtfText.text(false, (Object[]) chars);
    return totalText;
}
Also used : TextStyleTag(eu.transkribus.core.model.beans.customtags.TextStyleTag) RtfText(com.tutego.jrtf.RtfText) CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) CustomTagList(eu.transkribus.core.model.beans.customtags.CustomTagList) IOException(java.io.IOException)

Example 5 with RtfText

use of com.tutego.jrtf.RtfText in project ddf by codice.

the class RtfTemplate method appendProperty.

private RtfRow appendProperty(String metacardId, Map.Entry<String, ExportCategory.ExportValue> entry) {
    if (ExportCategory.ValueType.MEDIA.equals(entry.getValue().getType())) {
        ExportCategory.ExportValue<byte[], ExportCategory.ValueType> value = entry.getValue();
        Function<byte[], InputStream> fromByteArrayFunction = memoizeForImageData.apply(metacardId);
        RtfText picture = Optional.of(value.getValue()).map(fromByteArrayFunction).map(this::imageFromStream).orElse(text(EMPTY_VALUE));
        return row(entry.getKey(), picture);
    }
    return row(entry.getKey(), entry.getValue().getValue());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RtfText(com.tutego.jrtf.RtfText) ExportCategory(ddf.catalog.transformer.output.rtf.model.ExportCategory)

Aggregations

RtfText (com.tutego.jrtf.RtfText)5 RtfPara (com.tutego.jrtf.RtfPara)2 CustomTag (eu.transkribus.core.model.beans.customtags.CustomTag)2 Rtf (com.tutego.jrtf.Rtf)1 ExportCategory (ddf.catalog.transformer.output.rtf.model.ExportCategory)1 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)1 TrpPage (eu.transkribus.core.model.beans.TrpPage)1 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)1 CustomTagList (eu.transkribus.core.model.beans.customtags.CustomTagList)1 TextStyleTag (eu.transkribus.core.model.beans.customtags.TextStyleTag)1 TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)1 WordType (eu.transkribus.core.model.beans.pagecontent.WordType)1 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)1 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)1 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)1 TrpWordType (eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1