From e3530bda5682116b1b63dd7c7197c3c52e89185c Mon Sep 17 00:00:00 2001 From: maxnth Date: Tue, 12 Apr 2022 17:01:10 +0200 Subject: [PATCH] allow saving the removal of subtypes from TextRegion elements Signed-off-by: maxnth --- .../java/de/uniwue/web/io/PageXMLWriter.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/uniwue/web/io/PageXMLWriter.java b/src/main/java/de/uniwue/web/io/PageXMLWriter.java index 829d0292..7f837740 100644 --- a/src/main/java/de/uniwue/web/io/PageXMLWriter.java +++ b/src/main/java/de/uniwue/web/io/PageXMLWriter.java @@ -237,15 +237,20 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa Region physicalRegion = layout.getRegion(id); String physicalRegionType = physicalRegion.getType().toString(); - if(isTextRegion(elementType) && isTextRegion(physicalRegionType)){ + if(isTextRegion(elementType, true) && isTextRegion(physicalRegionType, true)){ TextRegion textRegion = (TextRegion) physicalRegion; textRegion.setCoords(elementCoords); + String physicalSubtype = textRegion.getTextType(); if(isTextRegionSubtype(elementType)){ String subType = TypeConverter.stringToStringSubType(elementType); textRegion.setTextType(subType); } + if(physicalSubtype != null && isTextRegion(elementType, false)){ + textRegion.setTextType(null); + } + Map physicalTextLines = getTextLines(textRegion); for(Map.Entry _textLine : element.getTextlines().entrySet()){ @@ -256,7 +261,7 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa physicalTextLine.setCoords(textLineCoords); Map textLineContent = textLine.getText().entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - + // Sync existing TextContentVariants with content from annotation for(int i = 0; i < physicalTextLine.getTextContentVariantCount(); i++){ TextContent textContent = physicalTextLine.getTextContentVariant(i); @@ -269,7 +274,7 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa textContent.setText(textLineContent.get(textContentIndex)); textLineContent.remove(textContentIndex); }else{ - // If index 0 isn't contained in the annotation but exists physically it was + // If index 0 isn't contained in the annotation but exists physically it was // discarded in the UI and therefore should set to empty if(textContentIndex == 0){ textContent.setText(""); @@ -277,7 +282,7 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa } } } - + // Add TextContentVariants which exist in annotation but not physically for(Integer index : textLineContent.keySet()){ TextContent textContent = physicalTextLine.addTextContentVariant(); @@ -493,14 +498,17 @@ private static boolean isRegion(String type, boolean includeSubtypes) { /** * Returns whether a type is a TextRegion (including subtypes) * @param type + * @param includeSubtypes * @return */ - private static boolean isTextRegion(String type) { + private static boolean isTextRegion(String type, boolean includeSubtypes) { if(type.equals(RegionType.TextRegion.getName())) return true; - for (de.uniwue.algorithm.geometry.regions.type.RegionSubType c : de.uniwue.algorithm.geometry.regions.type.RegionSubType.values()) { - if (c.name().equals(type)) { - return true; + if(includeSubtypes) { + for (de.uniwue.algorithm.geometry.regions.type.RegionSubType c : de.uniwue.algorithm.geometry.regions.type.RegionSubType.values()) { + if (c.name().equals(type)) { + return true; + } } } return false;