Skip to content

Commit d208739

Browse files
committed
Add small content stream optimization for ActualTextIterator
DEVSIX-1983
1 parent b73b292 commit d208739

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

io/src/main/java/com/itextpdf/io/font/otf/ActualTextIterator.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,18 @@ public GlyphLine.GlyphLinePart next() {
7979
return null;
8080
}
8181
pos = currentResult.end;
82-
while (pos < glyphLine.end && !glyphLinePartNeedsActualText(currentResult)) {
82+
83+
if (!glyphLinePartNeedsActualText(currentResult)) {
8384
currentResult.actualText = null;
84-
GlyphLine.GlyphLinePart nextResult = nextGlyphLinePart(pos);
85-
if (nextResult != null && !glyphLinePartNeedsActualText(nextResult)) {
86-
currentResult.end = nextResult.end;
87-
pos = nextResult.end;
88-
} else {
89-
break;
85+
// Try to add more pieces without "actual text"
86+
while (pos < glyphLine.end) {
87+
GlyphLine.GlyphLinePart nextResult = nextGlyphLinePart(pos);
88+
if (nextResult != null && !glyphLinePartNeedsActualText(nextResult)) {
89+
currentResult.end = nextResult.end;
90+
pos = nextResult.end;
91+
} else {
92+
break;
93+
}
9094
}
9195
}
9296
return currentResult;

io/src/main/java/com/itextpdf/io/font/otf/GlyphLine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ private void addAllGlyphs(int index, List<Glyph> additionalGlyphs) {
369369
public static class GlyphLinePart {
370370
public int start;
371371
public int end;
372+
// Might be null if it's not necessary
372373
public String actualText;
373374
public boolean reversed;
374375

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.itextpdf.io.font.otf;
2+
3+
import com.itextpdf.test.annotations.type.UnitTest;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.experimental.categories.Category;
7+
8+
import java.util.Arrays;
9+
10+
@Category(UnitTest.class)
11+
public class ActualTextIteratorTest {
12+
13+
@Test
14+
public void testActualTestParts() {
15+
Glyph glyph = new Glyph(200, 200, '\u002d');
16+
GlyphLine glyphLine = new GlyphLine(Arrays.asList(glyph));
17+
glyphLine.setActualText(0, 1, "\u002d");
18+
ActualTextIterator actualTextIterator = new ActualTextIterator(glyphLine);
19+
GlyphLine.GlyphLinePart part = actualTextIterator.next();
20+
// When actual text is the same as the result by text extraction, we should omit redundant actual text in the content stream
21+
Assert.assertNull(part.actualText);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)