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);
}
}
}
}
}
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();
}
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;
}
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);
}
}
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;
}
Aggregations