Skip to content

Commit 172526e

Browse files
Add support for type (0,3) in TTF 'cmap' table
Platform 0, encoding 3 is one of the ways to represent the Unicode BMP, and for our purposes can be handled in approximately the same way as (3, 1). It's used (among other places) in some of the fonts that ship with macOS. DEVSIX-7110
1 parent 546b51d commit 172526e

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ private void readCmapTable() throws java.io.IOException {
819819
int map31 = 0;
820820
int map30 = 0;
821821
int mapExt = 0;
822+
int map03 = 0;
822823
cmaps = new CmapTable();
823824
for (int k = 0; k < num_tables; ++k) {
824825
int platId = raf.readUnsignedShort();
@@ -833,6 +834,8 @@ private void readCmapTable() throws java.io.IOException {
833834
mapExt = offset;
834835
} else if (platId == 1 && platSpecId == 0) {
835836
map10 = offset;
837+
} else if (platId == 0 && platSpecId == 3) {
838+
map03 = offset;
836839
}
837840
}
838841
if (map10 > 0) {
@@ -850,6 +853,22 @@ private void readCmapTable() throws java.io.IOException {
850853
break;
851854
}
852855
}
856+
if (map03 > 0) {
857+
// Unicode platform, Unicode >2.0 semantics, expect format 4 or 6 subtable
858+
raf.seek(table_location[0] + map03);
859+
int format = raf.readUnsignedShort();
860+
861+
// We treat this table as equivalent to (platformId = 3, encodingId = 1)
862+
// for downstream processing, since both are intended to address the Unicode BMP
863+
switch (format) {
864+
case 4:
865+
cmaps.cmap31 = readFormat4(false);
866+
break;
867+
case 6:
868+
cmaps.cmap31 = readFormat6();
869+
break;
870+
}
871+
}
853872
if (map31 > 0) {
854873
raf.seek(table_location[0] + map31);
855874
int format = raf.readUnsignedShort();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,26 @@ public void notoSansScMapGlyphsCidsToGidsTest() throws IOException {
9494
Assertions.assertEquals(1, actualResult.size());
9595
Assertions.assertTrue(actualResult.contains(charGidInFont));
9696
}
97+
98+
@Test
99+
public void cmapPlatform0PlatEnc3Format4Test() throws IOException {
100+
FontProgram fontProgram = FontProgramFactory.createFont(SOURCE_FOLDER + "glyphs.ttf");
101+
checkCmapTableEntry(fontProgram, 'f', 2);
102+
checkCmapTableEntry(fontProgram, 'i', 3);
103+
}
104+
105+
@Test
106+
public void cmapPlatform0PlatEnc3Format6Test() throws IOException {
107+
FontProgram fontProgram = FontProgramFactory.createFont(SOURCE_FOLDER + "glyphs-fmt-6.ttf");
108+
checkCmapTableEntry(fontProgram, 'f', 2);
109+
checkCmapTableEntry(fontProgram, 'i', 3);
110+
}
111+
112+
private void checkCmapTableEntry(FontProgram fontProgram, char uniChar, int expectedGlyphId) {
113+
114+
Glyph glyph = fontProgram.getGlyph(uniChar);
115+
116+
Assertions.assertEquals(expectedGlyphId, glyph.getCode());
117+
Assertions.assertArrayEquals(new char[]{uniChar}, glyph.getUnicodeChars());
118+
}
97119
}

io/src/test/resources/com/itextpdf/io/font/sharedFontsResourceFiles/NOTICE.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ This software uses the following test resources under the following licenses:
33
| NotoSansTC-Regular.otf | OFL-1.1 | OFL.txt | Based on commit 165c01b46ea533872e002e0785ff17e44f6d97d8 (30.04.2021) from repository: "https://github.com/googlefonts/noto-cjk"
44
| NotoSansSC-Regular.otf | OFL-1.1 | OFL.txt | Based on commit 165c01b46ea533872e002e0785ff17e44f6d97d8 (30.04.2021) from repository: "https://github.com/googlefonts/noto-cjk"
55
| NotoSansCJKjp-Bold.otf | OFL-1.1 | OFL.txt | Based on commit 165c01b46ea533872e002e0785ff17e44f6d97d8 (30.04.2021) from repository: "https://github.com/googlefonts/noto-cjk"
6-
| Puritan2 | OFL-1.1 | OFL.txt |
6+
| Puritan2 | OFL-1.1 | OFL.txt |
7+
| glyphs.ttf, glyphs-fmt-6.ttf | MIT | Derived from https://github.com/RazrFalcon/ttf-parser/tree/337e7d1c08b06478a84c4345f4f289fc2cb9210c/tests/fonts-src |
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)