@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
47
47
import java .util .List ;
48
48
import java .util .Map ;
49
49
import java .util .Set ;
50
+
50
51
import org .slf4j .Logger ;
51
52
import org .slf4j .LoggerFactory ;
52
53
@@ -578,9 +579,7 @@ private boolean createSingleTag(TaggingHintKey hintKey, TagTreePointer tagPointe
578
579
if (parentHint != null ) {
579
580
// if parent tag hasn't been created yet - it's ok, kid tags will be moved on it's creation
580
581
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 );
584
583
}
585
584
}
586
585
@@ -663,26 +662,11 @@ private void moveKidTagIfCreated(TaggingHintKey parentKey, TaggingHintKey kidKey
663
662
return ;
664
663
}
665
664
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 );
670
666
parentPointer .setNextNewKidIndex (ind );
671
667
kidPointer .relocate (parentPointer );
672
668
}
673
669
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
- }
686
670
687
671
private static boolean isNonAccessibleHint (TaggingHintKey hintKey ) {
688
672
return !hintKey .isAccessible ();
@@ -767,7 +751,7 @@ private void registerRules(PdfVersion pdfVersion) {
767
751
registerSingleRule (StandardRoles .TFOOT , tableRule );
768
752
registerSingleRule (StandardRoles .THEAD , tableRule );
769
753
registerSingleRule (StandardRoles .TH , new THTaggingRule ());
770
- if (pdfVersion .compareTo (PdfVersion .PDF_1_5 ) < 0 ) {
754
+ if (pdfVersion .compareTo (PdfVersion .PDF_1_5 ) < 0 ) {
771
755
TableTaggingPriorToOneFiveVersionRule priorToOneFiveRule = new TableTaggingPriorToOneFiveVersionRule ();
772
756
registerSingleRule (StandardRoles .TABLE , priorToOneFiveRule );
773
757
registerSingleRule (StandardRoles .THEAD , priorToOneFiveRule );
@@ -783,4 +767,60 @@ private void registerSingleRule(String role, ITaggingRule rule) {
783
767
}
784
768
rules .add (rule );
785
769
}
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