Skip to content

Commit a1a408c

Browse files
ars18wrwiText-CI
authored andcommitted
Consider the actual text while replacing the line's content. Add a test.
DEVSIX-2506
1 parent 4f25802 commit a1a408c

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ This file is part of the iText (R) project.
5252

5353
public class GlyphLine implements Serializable {
5454
private static final long serialVersionUID = 4689818013371677649L;
55-
protected List<Glyph> glyphs;
56-
protected List<ActualText> actualText;
5755
public int start;
5856
public int end;
5957
public int idx;
58+
protected List<Glyph> glyphs;
59+
protected List<ActualText> actualText;
6060

6161
public GlyphLine() {
6262
this.glyphs = new ArrayList<>();
@@ -77,8 +77,8 @@ public GlyphLine(List<Glyph> glyphs) {
7777
* Create a new line of Glyphs from a slice of a List of Glyphs.
7878
*
7979
* @param glyphs list of Glyphs to slice
80-
* @param start starting index of the slice
81-
* @param end terminating index of the slice
80+
* @param start starting index of the slice
81+
* @param end terminating index of the slice
8282
*/
8383
public GlyphLine(List<Glyph> glyphs, int start, int end) {
8484
this.glyphs = glyphs;
@@ -89,10 +89,10 @@ public GlyphLine(List<Glyph> glyphs, int start, int end) {
8989
/**
9090
* Create a new line of Glyphs from a slice of a List of Glyphs, and add the actual text.
9191
*
92-
* @param glyphs list of Glyphs to slice
92+
* @param glyphs list of Glyphs to slice
9393
* @param actualText corresponding list containing the actual text the glyphs represent
94-
* @param start starting index of the slice
95-
* @param end terminating index of the slice
94+
* @param start starting index of the slice
95+
* @param end terminating index of the slice
9696
*/
9797
protected GlyphLine(List<Glyph> glyphs, List<ActualText> actualText, int start, int end) {
9898
this(glyphs, start, end);
@@ -114,9 +114,10 @@ public GlyphLine(GlyphLine other) {
114114

115115
/**
116116
* Copy a slice of a line of Glyphs
117+
*
117118
* @param other line of Glyphs to copy
118119
* @param start starting index of the slice
119-
* @param end terminating index of the slice
120+
* @param end terminating index of the slice
120121
*/
121122
public GlyphLine(GlyphLine other, int start, int end) {
122123
this.glyphs = other.glyphs.subList(start, end);
@@ -130,8 +131,9 @@ public GlyphLine(GlyphLine other, int start, int end) {
130131

131132
/**
132133
* Get the unicode string representation of the GlyphLine slice.
134+
*
133135
* @param start starting index of the slice
134-
* @param end terminating index of the slice
136+
* @param end terminating index of the slice
135137
* @return String containing the unicode representation of the slice.
136138
*/
137139
public String toUnicodeString(int start, int end) {
@@ -158,7 +160,7 @@ public String toString() {
158160
/**
159161
* Copy a slice of this Glyphline.
160162
*
161-
* @param left leftmost index of the slice
163+
* @param left leftmost index of the slice
162164
* @param right rightmost index of the slice
163165
* @return new GlyphLine containing the copied slice
164166
*/
@@ -220,17 +222,23 @@ public void add(GlyphLine other) {
220222
glyphs.addAll(other.glyphs.subList(other.start, other.end));
221223
}
222224

225+
/**
226+
* Replaces the current content with the other line's content.
227+
*
228+
* @param other the line with the content to be set to the current one
229+
*/
223230
public void replaceContent(GlyphLine other) {
224231
glyphs.clear();
225232
glyphs.addAll(other.glyphs);
226-
if (actualText != null) {
227-
actualText.clear();
228-
}
229233
if (other.actualText != null) {
230234
if (actualText == null) {
231235
actualText = new ArrayList<>();
236+
} else {
237+
actualText.clear();
232238
}
233239
actualText.addAll(other.actualText);
240+
} else {
241+
actualText = null;
234242
}
235243
start = other.start;
236244
end = other.end;
@@ -347,7 +355,7 @@ public boolean equals(Object obj) {
347355
if (obj == null || getClass() != obj.getClass()) {
348356
return false;
349357
}
350-
GlyphLine other = (GlyphLine)obj;
358+
GlyphLine other = (GlyphLine) obj;
351359
if (end - start != other.end - other.start) {
352360
return false;
353361
}
@@ -386,6 +394,10 @@ private void addAllGlyphs(int index, List<Glyph> additionalGlyphs) {
386394
}
387395
}
388396

397+
public interface IGlyphLineFilter {
398+
boolean accept(Glyph glyph);
399+
}
400+
389401
public static class GlyphLinePart {
390402
public int start;
391403
public int end;
@@ -409,19 +421,14 @@ public GlyphLinePart setReversed(boolean reversed) {
409421
}
410422
}
411423

412-
public interface IGlyphLineFilter {
413-
boolean accept(Glyph glyph);
414-
}
415-
416424
protected static class ActualText implements Serializable {
417425
private static final long serialVersionUID = 5109920013485372966L;
426+
public String value;
418427

419428
public ActualText(String value) {
420429
this.value = value;
421430
}
422431

423-
public String value;
424-
425432
@Override
426433
public boolean equals(Object obj) {
427434
if (this == obj) {

io/src/test/java/com/itextpdf/io/font/otf/GlyphLineTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void testOtherLinesAddition() throws IOException {
107107
containerLine.end = 40;
108108
Assert.assertEquals(containerLine.glyphs.size(), 40);
109109
}
110-
110+
111111
@Test
112112
public void testOtherLinesWithActualTextAddition() throws IOException {
113113
byte[] ttf = StreamUtil.inputStreamToArray(new FileInputStream("./src/test/resources/com/itextpdf/io/font/otf/FreeSans.ttf"));
@@ -154,4 +154,19 @@ public void testOtherLinesWithActualTextAddition02() throws IOException {
154154
}
155155
Assert.assertEquals("Fide---Viva", containerLine.toString());
156156
}
157+
158+
@Test
159+
public void testContentReplacingWithNullActualText() throws IOException {
160+
byte[] ttf = StreamUtil.inputStreamToArray(new FileInputStream("./src/test/resources/com/itextpdf/io/font/otf/FreeSans.ttf"));
161+
TrueTypeFont font = new TrueTypeFont(ttf);
162+
163+
GlyphLine lineToBeReplaced = new GlyphLine(constructGlyphListFromString("Byelorussia", font));
164+
lineToBeReplaced.setActualText(1, 2, "e");
165+
166+
GlyphLine lineToBeCopied = new GlyphLine(constructGlyphListFromString("Belarus", font));
167+
lineToBeReplaced.replaceContent(lineToBeCopied);
168+
169+
// Test that no exception has been thrown. Also check the content.
170+
Assert.assertEquals("Belarus", lineToBeReplaced.toString());
171+
}
157172
}

0 commit comments

Comments
 (0)