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