Skip to content

Commit b06cb5e

Browse files
committed
Improve and fix TrueType font merging logic
DEVSIX-9257
1 parent bc40acc commit b06cb5e

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

io/src/main/java/com/itextpdf/io/font/OpenTypeParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static class CmapTable {
197197

198198
private int[] glyphWidthsByIndex;
199199
private int[] locaTable;
200-
// In case of lenient mode parsing 'name' table can be missed
200+
// In case of lenient mode parsing 'name' and 'OS/2' table can be missed
201201
private boolean isLenientMode = false;
202202

203203
protected HeaderTable head;
@@ -910,14 +910,17 @@ private void readHeadTable() throws java.io.IOException {
910910
*/
911911
private void readOs_2Table() throws java.io.IOException {
912912
int[] table_location = tables.get("OS/2");
913+
os_2 = new WindowsMetrics();
913914
if (table_location == null) {
915+
if (isLenientMode) {
916+
return;
917+
}
914918
if (fileName != null) {
915919
throw new IOException(IoExceptionMessageConstant.TABLE_DOES_NOT_EXISTS_IN).setMessageParams("os/2", fileName);
916920
} else {
917921
throw new IOException(IoExceptionMessageConstant.TABLE_DOES_NOT_EXIST).setMessageParams("os/2");
918922
}
919923
}
920-
os_2 = new WindowsMetrics();
921924
raf.seek(table_location[0]);
922925
int version = raf.readUnsignedShort();
923926
os_2.xAvgCharWidth = raf.readShort();

io/src/main/java/com/itextpdf/io/font/TrueTypeFont.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ This file is part of the iText (R) project.
3535
import com.itextpdf.io.util.IntHashtable;
3636

3737
import java.util.ArrayList;
38-
import java.util.HashMap;
3938
import java.util.LinkedHashMap;
4039
import java.util.List;
4140
import java.util.Map;
@@ -222,7 +221,7 @@ public byte[] getSubset(Set<Integer> glyphs, boolean subsetTables) {
222221
*/
223222
public static byte[] merge(Map<TrueTypeFont, Set<Integer>> toMerge, String fontName) {
224223
try {
225-
Map<OpenTypeParser, Set<Integer>> toMergeWithParsers = new HashMap<OpenTypeParser, Set<Integer>>();
224+
Map<OpenTypeParser, Set<Integer>> toMergeWithParsers = new LinkedHashMap<>();
226225
for (Map.Entry<TrueTypeFont, Set<Integer>> entry : toMerge.entrySet()) {
227226
toMergeWithParsers.put(entry.getKey().fontParser, entry.getValue());
228227
}

io/src/test/java/com/itextpdf/io/font/TrueTypeFontIntegrationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.commons.utils.MessageFormatUtil;
2626
import com.itextpdf.io.exceptions.IoExceptionMessageConstant;
27+
import com.itextpdf.test.AssertUtil;
2728
import com.itextpdf.test.ExtendedITextTest;
2829

2930
import java.io.IOException;
@@ -117,4 +118,20 @@ public void tryToReadFontSubsetWithoutGlyfTableTest() throws IOException {
117118
String exp = MessageFormatUtil.format(IoExceptionMessageConstant.TABLE_DOES_NOT_EXIST, "glyf");
118119
Assertions.assertEquals(exp, e.getMessage());
119120
}
121+
122+
@Test
123+
public void readFontSubsetWithoutOs2TableTest() throws IOException {
124+
byte[] fontBytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "subsetWithoutOsTable.ttf"));
125+
AssertUtil.doesNotThrow(() -> FontProgramFactory.createTrueTypeFont(fontBytes, true));
126+
}
127+
128+
@Test
129+
public void tryToReadFontSubsetWithoutOs2TableTest() throws IOException {
130+
byte[] fontBytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "subsetWithoutOsTable.ttf"));
131+
132+
Exception e = Assertions.assertThrows(com.itextpdf.io.exceptions.IOException.class, () ->
133+
FontProgramFactory.createTrueTypeFont(fontBytes, false));
134+
String exp = MessageFormatUtil.format(IoExceptionMessageConstant.TABLE_DOES_NOT_EXIST, "os/2");
135+
Assertions.assertEquals(exp, e.getMessage());
136+
}
120137
}
Binary file not shown.

0 commit comments

Comments
 (0)