use of com.itextpdf.awt.geom.Line2D in project TranskribusCore by Transkribus.
the class TrpPdfDocument method calculateTagLines.
private void calculateTagLines(java.awt.Rectangle baseLineRect, ITrpShapeType shape, String tagText, int offset, int length, String color, float yShift, boolean falling, boolean rtl) {
String lineText = shape.getUnicodeText();
java.awt.Rectangle shapeRect = null;
if (shape instanceof TrpWordType) {
shapeRect = ((TrpWordType) shape).getBoundingBox();
} else {
shapeRect = baseLineRect;
}
float shapeMinX = (float) shapeRect.getMinX();
float shapeMaxX = (float) shapeRect.getMaxX();
float minX = (float) baseLineRect.getMinX();
float maxX = (float) baseLineRect.getMaxX();
float minY = (float) baseLineRect.getMinY();
float maxY = (float) baseLineRect.getMaxY();
float a = maxY - minY;
float b = maxX - minX;
float angleAlpha = (float) Math.atan(a / b);
// logger.debug("line Text " + lineText);
// logger.debug("tag text " + tagText);
// logger.debug("angle alpha " + angleAlpha);
//
// logger.debug("offset " + offset);
// logger.debug("lineText.length() " + lineText.length());
// logger.debug("offset+length " + offset+length);
// relation of tagStart to entire text length
float ratioOfTagStart = 0;
if (offset != 0) {
ratioOfTagStart = (float) offset / (float) lineText.length();
}
float ratioOfTagEnd = (float) (offset + length) / (float) lineText.length();
float tagStartX;
float tagEndX;
if (!rtl) {
tagStartX = shapeMinX + (ratioOfTagStart * baseLineRect.width);
tagEndX = shapeMinX + (ratioOfTagEnd * shapeRect.width);
} else {
tagStartX = shapeMaxX - (ratioOfTagStart * baseLineRect.width);
tagEndX = shapeMaxX - (ratioOfTagEnd * shapeRect.width);
}
float tagStartHeight = 0;
if (tagStartX != shapeMinX && !rtl) {
tagStartHeight = (float) (Math.tan(angleAlpha) * (tagStartX - shapeMinX));
} else if (tagStartX != shapeMaxX && rtl) {
tagStartHeight = (float) (Math.tan(angleAlpha) * (tagStartX - shapeMinX));
}
float tagEndHeight = (float) (Math.tan(angleAlpha) * (tagEndX - shapeMinX));
float tagStartY;
float tagEndY;
if (falling) {
// logger.debug("tagStartHeight > tagEndHeight; tagStartY = maxY - tagStartHeight;" + (maxY - tagStartHeight));
// logger.debug("tagStartHeight > tagEndHeight; tagEndY = maxY - tagEndHeight;" + (maxY - tagEndHeight));
tagStartY = maxY - tagStartHeight;
tagEndY = maxY - tagEndHeight;
} else {
tagStartY = maxY - tagEndHeight;
tagEndY = maxY - tagStartHeight;
}
// logger.debug("tag startX " + tagStartX);
// logger.debug("tag endX " + tagEndX);
//
// logger.debug("tag startY " + tagStartY);
// logger.debug("tag endY " + tagEndY);
Line2D line = new Line2D.Double(tagStartX, tagStartY + yShift, tagEndX, tagEndY + yShift);
java.util.Map.Entry<Line2D, String> pair = new java.util.AbstractMap.SimpleEntry<>(line, color);
lineAndColorList.add(pair);
}
Aggregations