Skip to content

Commit 458f366

Browse files
committed
Fix some Unicode font embedding issues:
- Reworked Widths array compression for CID fonts to require at least 4 repeated widths. - Fixed the embedded CMap for Unicode fonts.
1 parent 4165cd2 commit 458f366

2 files changed

Lines changed: 33 additions & 24 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ v1.5.0 - 2025-03-06
1818
- Fixed an output issue for extremely small `double` values with the
1919
`pdfioContent` APIs.
2020
- Fixed a missing Widths array issue for embedded TrueType fonts.
21+
- Fixed some Unicode font embedding issues.
2122

2223

2324
v1.4.1 - 2025-01-24

pdfio-content.c

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,7 @@ pdfioFileCreateFontObjFromFile(
16561656
*to_unicode; // ToUnicode dictionary
16571657
pdfio_obj_t *cid2gid_obj, // CIDToGIDMap object
16581658
*to_unicode_obj;// ToUnicode object
1659-
size_t i, // Looping var
1660-
start, // Start character
1659+
size_t i, j, // Looping vars
16611660
num_cmap; // Number of CMap entries
16621661
const int *cmap; // CMap entries
16631662
int min_glyph, // First glyph
@@ -1709,7 +1708,7 @@ pdfioFileCreateFontObjFromFile(
17091708
for (i = 0, bufptr = buffer, bufend = buffer + sizeof(buffer); i < num_cmap; i ++)
17101709
{
17111710
PDFIO_DEBUG("pdfioFileCreateFontObjFromFile: cmap[%u]=%d\n", (unsigned)i, cmap[i]);
1712-
if (cmap[i] < 0)
1711+
if (cmap[i] < 0 || cmap[i] >= (int)(sizeof(glyphs) / sizeof(glyphs[0])))
17131712
{
17141713
// Map undefined glyph to .notdef...
17151714
*bufptr++ = 0;
@@ -1788,9 +1787,6 @@ pdfioFileCreateFontObjFromFile(
17881787
"1 begincodespacerange\n"
17891788
"<0000> <FFFF>\n"
17901789
"endcodespacerange\n"
1791-
"1 beginbfrange\n"
1792-
"<0000> <FFFF> <0000>\n"
1793-
"endbfrange\n"
17941790
"endcmap\n"
17951791
"CMapName currentdict /CMap defineresource pop\n"
17961792
"end\n"
@@ -1806,39 +1802,51 @@ pdfioFileCreateFontObjFromFile(
18061802
if ((w_array = pdfioArrayCreate(pdf)) == NULL)
18071803
goto done;
18081804

1809-
for (start = 0, w0 = ttfGetWidth(font, 0), w1 = 0, i = 1; i < 65536; start = i, w0 = w1, i ++)
1805+
for (i = 0, w0 = ttfGetWidth(font, 0), w1 = 0; i < 65536; w0 = w1)
18101806
{
1811-
while (i < 65536 && (w1 = ttfGetWidth(font, (int)i)) == w0)
1812-
i ++;
1807+
for (j = 1; (i + j) < 65536; j ++)
1808+
{
1809+
if ((w1 = ttfGetWidth(font, (int)(i + j))) != w0)
1810+
break;
1811+
}
18131812

1814-
if ((i - start) > 1)
1813+
if (j >= 4)
18151814
{
1816-
// Encode a repeating sequence...
1817-
pdfioArrayAppendNumber(w_array, (double)start);
1818-
pdfioArrayAppendNumber(w_array, (double)(i - 1));
1819-
pdfioArrayAppendNumber(w_array, w0);
1815+
// Encode a long sequence of zeros...
1816+
// Encode a repeating sequence...
1817+
pdfioArrayAppendNumber(w_array, (double)i);
1818+
pdfioArrayAppendNumber(w_array, (double)(i + j - 1));
1819+
pdfioArrayAppendNumber(w_array, w0);
1820+
1821+
i += j;
18201822
}
18211823
else
18221824
{
18231825
// Encode a non-repeating sequence...
1824-
pdfioArrayAppendNumber(w_array, (double)start);
1826+
pdfioArrayAppendNumber(w_array, (double)i);
18251827

18261828
if ((temp_array = pdfioArrayCreate(pdf)) == NULL)
18271829
goto done;
18281830

18291831
pdfioArrayAppendNumber(temp_array, w0);
1830-
for (w0 = w1, i ++; i < 65536; w0 = w1, i ++)
1832+
for (i ++; i < 65536 && pdfioArrayGetSize(temp_array) < 8191; i ++, w0 = w1)
18311833
{
1832-
if ((w1 = ttfGetWidth(font, (int)i)) == w0 && i < 65535)
1833-
break;
1834+
if ((w1 = ttfGetWidth(font, (int)i)) == w0 && i < 65530)
1835+
{
1836+
size_t j; // Look-ahead
18341837

1835-
pdfioArrayAppendNumber(temp_array, w0);
1836-
}
1838+
for (j = 1; j < 4; j ++)
1839+
{
1840+
if (ttfGetWidth(font, (int)(i + j)) != w0)
1841+
break;
1842+
}
18371843

1838-
if (i == 65536)
1839-
pdfioArrayAppendNumber(temp_array, w0);
1840-
else
1841-
i --;
1844+
if (j >= 4)
1845+
break;
1846+
}
1847+
1848+
pdfioArrayAppendNumber(temp_array, w1);
1849+
}
18421850

18431851
pdfioArrayAppendArray(w_array, temp_array);
18441852
}

0 commit comments

Comments
 (0)