Skip to content

Commit 3b203e9

Browse files
BezrukovMiText-CI
authored andcommitted
Add PdfSimpleFontTest
DEVSIX-4515
1 parent e5c5d70 commit 3b203e9

File tree

2 files changed

+273
-1
lines changed

2 files changed

+273
-1
lines changed

kernel/src/main/java/com/itextpdf/kernel/font/PdfSimpleFont.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public int appendAnyGlyph(String text, int from, List<Glyph> glyphs) {
170170
}
171171

172172
/**
173-
* Checks whether the glyph is appendable, i.e. has valid unicode and code values
173+
* Checks whether the glyph is appendable, i.e. has valid unicode and code values.
174174
*
175175
* @param glyph not-null {@link Glyph}
176176
*/
@@ -180,6 +180,11 @@ private boolean isAppendableGlyph(Glyph glyph) {
180180
return glyph.getCode() > 0 || TextUtil.isWhitespaceOrNonPrintable(glyph.getUnicode());
181181
}
182182

183+
/**
184+
* Get the font encoding.
185+
*
186+
* @return the {@link FontEncoding}
187+
*/
183188
public FontEncoding getFontEncoding() {
184189
return fontEncoding;
185190
}
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
package com.itextpdf.kernel.font;
2+
3+
import com.itextpdf.io.font.FontEncoding;
4+
import com.itextpdf.io.font.FontProgram;
5+
import com.itextpdf.io.font.FontProgramFactory;
6+
import com.itextpdf.io.font.constants.StandardFonts;
7+
import com.itextpdf.io.font.otf.Glyph;
8+
import com.itextpdf.io.font.otf.GlyphLine;
9+
import com.itextpdf.kernel.pdf.PdfDictionary;
10+
import com.itextpdf.kernel.pdf.PdfOutputStream;
11+
import com.itextpdf.test.ExtendedITextTest;
12+
import com.itextpdf.test.annotations.type.UnitTest;
13+
14+
import java.io.ByteArrayOutputStream;
15+
import java.io.IOException;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import org.junit.Assert;
19+
import org.junit.BeforeClass;
20+
import org.junit.Test;
21+
import org.junit.experimental.categories.Category;
22+
23+
@Category(UnitTest.class)
24+
public class PdfSimpleFontTest extends ExtendedITextTest {
25+
26+
private static final byte T_CODE = 116;
27+
private static final byte E_CODE = 101;
28+
private static final byte E_CUSTOM_CODE = 103;
29+
private static final byte OPEN_BRACKET_CODE = 40;
30+
private static final byte CLOSE_BRACKET_CODE = 41;
31+
32+
private static Glyph E_GLYPH_FONT_SPECIFIC;
33+
private static Glyph T_GLYPH_FONT_SPECIFIC;
34+
private static Glyph E_GLYPH_CUSTOM_MAPPED;
35+
36+
@BeforeClass
37+
public static void init() {
38+
T_GLYPH_FONT_SPECIFIC = new Glyph(T_CODE, 278, 116, new int[]{14, -7, 257, 669});
39+
T_GLYPH_FONT_SPECIFIC.setChars(new char[]{'t'});
40+
41+
E_GLYPH_FONT_SPECIFIC = new Glyph(E_CODE, 556, 101, new int[]{40, -15, 516, 538});
42+
E_GLYPH_FONT_SPECIFIC.setChars(new char[]{'e'});
43+
44+
E_GLYPH_CUSTOM_MAPPED = new Glyph(E_CUSTOM_CODE, 44, 103);
45+
E_GLYPH_CUSTOM_MAPPED.setChars(new char[]{'e'});
46+
}
47+
48+
@Test
49+
public void createGlyphLineWithSpecificEncodingTest() throws IOException {
50+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
51+
GlyphLine glyphLine = fontToTest.createGlyphLine("te");
52+
53+
List<Glyph> glyphs = new ArrayList<>();
54+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
55+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
56+
GlyphLine expected = new GlyphLine(glyphs, 0, 2);
57+
58+
Assert.assertEquals(expected, glyphLine);
59+
}
60+
61+
@Test
62+
public void createGlyphLineWithEmptyEncodingTest() throws IOException {
63+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
64+
GlyphLine glyphLine = fontToTest.createGlyphLine("te");
65+
66+
List<Glyph> glyphs = new ArrayList<>();
67+
glyphs.add(E_GLYPH_CUSTOM_MAPPED);
68+
GlyphLine expected = new GlyphLine(glyphs, 0, 1);
69+
70+
Assert.assertEquals(expected, glyphLine);
71+
}
72+
73+
@Test
74+
public void appendGlyphsWithSpecificEncodingTest() throws IOException {
75+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
76+
List<Glyph> toAppend = new ArrayList<>();
77+
int processed = fontToTest.appendGlyphs("te", 0, 1, toAppend);
78+
Assert.assertEquals(2, processed);
79+
80+
List<Glyph> glyphs = new ArrayList<>();
81+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
82+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
83+
84+
Assert.assertEquals(glyphs, toAppend);
85+
}
86+
87+
@Test
88+
public void appendGlyphsWithEmptyEncodingTest() throws IOException {
89+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
90+
List<Glyph> toAppend = new ArrayList<>();
91+
int processed = fontToTest.appendGlyphs("e ete", 0, 4, toAppend);
92+
Assert.assertEquals(3, processed);
93+
94+
List<Glyph> glyphs = new ArrayList<>();
95+
glyphs.add(E_GLYPH_CUSTOM_MAPPED);
96+
glyphs.add(E_GLYPH_CUSTOM_MAPPED);
97+
98+
Assert.assertEquals(glyphs, toAppend);
99+
}
100+
101+
@Test
102+
public void appendAnyGlyphWithSpecificEncodingTest() throws IOException {
103+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
104+
List<Glyph> toAppend = new ArrayList<>();
105+
int processed = fontToTest.appendAnyGlyph("te", 0, toAppend);
106+
Assert.assertEquals(1, processed);
107+
108+
List<Glyph> glyphs = new ArrayList<>();
109+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
110+
111+
Assert.assertEquals(glyphs, toAppend);
112+
}
113+
114+
@Test
115+
public void appendAnyGlyphWithEmptyEncodingTest() throws IOException {
116+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
117+
List<Glyph> toAppend = new ArrayList<>();
118+
int processed = fontToTest.appendAnyGlyph("e ete", 0, toAppend);
119+
Assert.assertEquals(1, processed);
120+
121+
List<Glyph> glyphs = new ArrayList<>();
122+
glyphs.add(E_GLYPH_CUSTOM_MAPPED);
123+
124+
Assert.assertEquals(glyphs, toAppend);
125+
}
126+
127+
@Test
128+
public void convertGlyphLineToBytesWithSpecificEncodingTest() throws IOException {
129+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
130+
131+
List<Glyph> glyphs = new ArrayList<>();
132+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
133+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
134+
GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2);
135+
136+
byte[] bytes = fontToTest.convertToBytes(glyphLine);
137+
138+
Assert.assertArrayEquals(new byte[]{T_CODE, E_CODE}, bytes);
139+
}
140+
141+
@Test
142+
public void convertGlyphLineToBytesWithEmptyEncodingTest() throws IOException {
143+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
144+
145+
List<Glyph> glyphs = new ArrayList<>();
146+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
147+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
148+
GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2);
149+
150+
byte[] bytes = fontToTest.convertToBytes(glyphLine);
151+
152+
Assert.assertArrayEquals(new byte[0], bytes);
153+
}
154+
155+
@Test
156+
public void convertToBytesWithNullEntry() throws IOException {
157+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
158+
159+
byte[] bytes = fontToTest.convertToBytes((GlyphLine) null);
160+
161+
Assert.assertArrayEquals(new byte[0], bytes);
162+
}
163+
164+
@Test
165+
public void convertGlyphToBytesWithSpecificEncodingTest() throws IOException {
166+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
167+
168+
byte[] bytes = fontToTest.convertToBytes(E_GLYPH_FONT_SPECIFIC);
169+
170+
Assert.assertArrayEquals(new byte[]{E_CODE}, bytes);
171+
}
172+
173+
@Test
174+
public void convertGlyphToBytesWithCustomEncodingTest() throws IOException {
175+
FontEncoding emptyFontEncoding = FontEncoding.createEmptyFontEncoding();
176+
emptyFontEncoding.addSymbol(E_CUSTOM_CODE, E_CODE);
177+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(emptyFontEncoding);
178+
179+
byte[] bytes = fontToTest.convertToBytes(E_GLYPH_FONT_SPECIFIC);
180+
181+
Assert.assertArrayEquals(new byte[]{E_CUSTOM_CODE}, bytes);
182+
}
183+
184+
@Test
185+
public void convertGlyphToBytesWithEmptyEncodingTest() throws IOException {
186+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
187+
188+
byte[] bytes = fontToTest.convertToBytes(E_GLYPH_FONT_SPECIFIC);
189+
190+
Assert.assertArrayEquals(new byte[0], bytes);
191+
}
192+
193+
@Test
194+
public void writeTextGlyphLineWithSpecificEncodingTest() throws IOException {
195+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding());
196+
197+
List<Glyph> glyphs = new ArrayList<>();
198+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
199+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
200+
GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2);
201+
202+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
203+
try (PdfOutputStream pos = new PdfOutputStream(bos)) {
204+
fontToTest.writeText(glyphLine, 0, 1, pos);
205+
}
206+
207+
Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, T_CODE, E_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray());
208+
}
209+
210+
@Test
211+
public void writeTextGlyphLineWithCustomEncodingTest() throws IOException {
212+
FontEncoding fontEncoding = FontEncoding.createEmptyFontEncoding();
213+
fontEncoding.addSymbol(E_CUSTOM_CODE, E_CODE);
214+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(fontEncoding);
215+
216+
List<Glyph> glyphs = new ArrayList<>();
217+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
218+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
219+
GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2);
220+
221+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
222+
try (PdfOutputStream pos = new PdfOutputStream(bos)) {
223+
fontToTest.writeText(glyphLine, 0, 1, pos);
224+
}
225+
226+
Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, E_CUSTOM_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray());
227+
}
228+
229+
@Test
230+
public void writeTextGlyphLineWithEmptyEncodingTest() throws IOException {
231+
PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding());
232+
233+
List<Glyph> glyphs = new ArrayList<>();
234+
glyphs.add(E_GLYPH_FONT_SPECIFIC);
235+
glyphs.add(T_GLYPH_FONT_SPECIFIC);
236+
GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2);
237+
238+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
239+
try (PdfOutputStream pos = new PdfOutputStream(bos)) {
240+
fontToTest.writeText(glyphLine, 0, 1, pos);
241+
}
242+
243+
Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray());
244+
}
245+
246+
private static class TestSimpleFont extends PdfSimpleFont<FontProgram> {
247+
248+
public TestSimpleFont(FontEncoding fontEncoding) throws IOException {
249+
this.fontEncoding = fontEncoding;
250+
setFontProgram(FontProgramFactory.createFont(StandardFonts.HELVETICA));
251+
}
252+
253+
@Override
254+
public Glyph getGlyph(int unicode) {
255+
if (unicode == E_CODE) {
256+
return E_GLYPH_CUSTOM_MAPPED;
257+
}
258+
return null;
259+
}
260+
261+
@Override
262+
protected void addFontStream(PdfDictionary fontDescriptor) {
263+
264+
}
265+
}
266+
267+
}

0 commit comments

Comments
 (0)