Skip to content

Commit 1575701

Browse files
committed
Optimize nextNearest sibling algo
DEVSIX-8836
1 parent b50c34e commit 1575701

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

layout/src/main/java/com/itextpdf/layout/tagging/LayoutTaggingHelper.java

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
4747
import java.util.List;
4848
import java.util.Map;
4949
import java.util.Set;
50+
5051
import org.slf4j.Logger;
5152
import org.slf4j.LoggerFactory;
5253

@@ -578,9 +579,7 @@ private boolean createSingleTag(TaggingHintKey hintKey, TagTreePointer tagPointe
578579
if (parentHint != null) {
579580
// if parent tag hasn't been created yet - it's ok, kid tags will be moved on it's creation
580581
if (waitingTagsManager.tryMovePointerToWaitingTag(tagPointer, parentHint)) {
581-
List<TaggingHintKey> siblingsHint = getAccessibleKidsHint(parentHint);
582-
int i = siblingsHint.indexOf(hintKey);
583-
ind = getNearestNextSiblingTagIndex(waitingTagsManager, tagPointer, siblingsHint, i);
582+
ind = getNearestNextSiblingIndex(waitingTagsManager, tagPointer, parentHint, hintKey);
584583
}
585584
}
586585

@@ -663,26 +662,11 @@ private void moveKidTagIfCreated(TaggingHintKey parentKey, TaggingHintKey kidKey
663662
return;
664663
}
665664

666-
List<TaggingHintKey> parentKidsHint = getAccessibleKidsHint(parentKey);
667-
int kidIndInParentKidsHint = parentKidsHint.indexOf(kidKey);
668-
int ind = getNearestNextSiblingTagIndex(waitingTagsManager, parentPointer, parentKidsHint, kidIndInParentKidsHint);
669-
665+
int ind = getNearestNextSiblingIndex(waitingTagsManager, parentPointer, parentKey, kidKey);
670666
parentPointer.setNextNewKidIndex(ind);
671667
kidPointer.relocate(parentPointer);
672668
}
673669

674-
private int getNearestNextSiblingTagIndex(WaitingTagsManager waitingTagsManager, TagTreePointer parentPointer, List<TaggingHintKey> siblingsHint, int start) {
675-
int ind = -1;
676-
TagTreePointer nextSiblingPointer = new TagTreePointer(document);
677-
while (++start < siblingsHint.size()) {
678-
if (waitingTagsManager.tryMovePointerToWaitingTag(nextSiblingPointer, siblingsHint.get(start))
679-
&& parentPointer.isPointingToSameTag(new TagTreePointer(nextSiblingPointer).moveToParent())) {
680-
ind = nextSiblingPointer.getIndexInParentKidsList();
681-
break;
682-
}
683-
}
684-
return ind;
685-
}
686670

687671
private static boolean isNonAccessibleHint(TaggingHintKey hintKey) {
688672
return !hintKey.isAccessible();
@@ -767,7 +751,7 @@ private void registerRules(PdfVersion pdfVersion) {
767751
registerSingleRule(StandardRoles.TFOOT, tableRule);
768752
registerSingleRule(StandardRoles.THEAD, tableRule);
769753
registerSingleRule(StandardRoles.TH, new THTaggingRule());
770-
if (pdfVersion.compareTo(PdfVersion.PDF_1_5) < 0 ) {
754+
if (pdfVersion.compareTo(PdfVersion.PDF_1_5) < 0) {
771755
TableTaggingPriorToOneFiveVersionRule priorToOneFiveRule = new TableTaggingPriorToOneFiveVersionRule();
772756
registerSingleRule(StandardRoles.TABLE, priorToOneFiveRule);
773757
registerSingleRule(StandardRoles.THEAD, priorToOneFiveRule);
@@ -783,4 +767,60 @@ private void registerSingleRule(String role, ITaggingRule rule) {
783767
}
784768
rules.add(rule);
785769
}
786-
}
770+
771+
private int getNearestNextSiblingIndex(WaitingTagsManager waitingTagsManager, TagTreePointer parentPointer, TaggingHintKey parentKey, TaggingHintKey kidKey) {
772+
ScanContext scanContext = new ScanContext();
773+
scanContext.waitingTagsManager = waitingTagsManager;
774+
scanContext.startHintKey = kidKey;
775+
scanContext.parentPointer = parentPointer;
776+
scanContext.nextSiblingPointer = new TagTreePointer(document);
777+
return scanForNearestNextSiblingIndex(scanContext, null, parentKey);
778+
}
779+
780+
private int scanForNearestNextSiblingIndex(ScanContext scanContext, TaggingHintKey toCheck, TaggingHintKey parent) {
781+
if (scanContext.startVerifying) {
782+
if (scanContext.waitingTagsManager.tryMovePointerToWaitingTag(scanContext.nextSiblingPointer, toCheck)
783+
&& scanContext.parentPointer.isPointingToSameTag(new TagTreePointer(scanContext.nextSiblingPointer).moveToParent())) {
784+
return scanContext.nextSiblingPointer.getIndexInParentKidsList();
785+
}
786+
}
787+
if (toCheck != null && !isNonAccessibleHint(toCheck)) {
788+
return -1;
789+
}
790+
List<TaggingHintKey> kidsHintList = kidsHints.get(parent);
791+
if (kidsHintList == null) {
792+
return -1;
793+
}
794+
795+
796+
int startIndex = -1;
797+
if (!scanContext.startVerifying) {
798+
for (int i = kidsHintList.size() - 1; i >= 0; i--) {
799+
if (scanContext.startHintKey == kidsHintList.get(i)) {
800+
scanContext.startVerifying = true;
801+
startIndex = i;
802+
break;
803+
}
804+
}
805+
}
806+
807+
808+
for (int j = startIndex + 1; j < kidsHintList.size(); j++) {
809+
final TaggingHintKey kid = kidsHintList.get(j);
810+
final int interMediateResult = scanForNearestNextSiblingIndex(scanContext, kid, kid);
811+
if (interMediateResult != -1) {
812+
return interMediateResult;
813+
}
814+
}
815+
816+
return -1;
817+
}
818+
819+
private static class ScanContext {
820+
WaitingTagsManager waitingTagsManager;
821+
TaggingHintKey startHintKey;
822+
boolean startVerifying;
823+
TagTreePointer parentPointer;
824+
TagTreePointer nextSiblingPointer;
825+
}
826+
}

0 commit comments

Comments
 (0)