Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f9dbd2

Browse files
committed
Fix some segfaults when using newer Microsoft fonts.
1 parent d260be0 commit 3f9dbd2

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/ft2font.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,9 @@ FT2Font::get_glyph_name(const Py::Tuple & args)
16211621

16221622
if (!FT_HAS_GLYPH_NAMES(face))
16231623
{
1624-
snprintf(buffer, 128, "uni%04x", glyph_number);
1624+
/* Note that this generated name must match the name that
1625+
is generated by ttconv in ttfont_CharStrings_getname. */
1626+
snprintf(buffer, 128, "uni%08x", glyph_number);
16251627
} else {
16261628
if (FT_Get_Glyph_Name(face, glyph_number, buffer, 128))
16271629
{
@@ -1745,9 +1747,10 @@ FT2Font::get_sfnt(const Py::Tuple & args)
17451747
key[1] = Py::Int(sfnt.encoding_id);
17461748
key[2] = Py::Int(sfnt.language_id);
17471749
key[3] = Py::Int(sfnt.name_id);
1748-
names[key] = Py::String((char *) sfnt.string,
1749-
(int) sfnt.string_len,
1750-
"latin-1");
1750+
1751+
names[key] = Py::asObject
1752+
(PyBytes_FromStringAndSize(
1753+
(const char *)sfnt.string, sfnt.string_len));
17511754
}
17521755
return names;
17531756
}

ttconv/pprdrv_tt.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ BYTE *GetTable(struct TTFONT *font, const char *name)
166166

167167
} /* end of GetTable() */
168168

169+
static void utf16be_to_ascii(char *dst, char *src, size_t length) {
170+
++src;
171+
for (; *src != 0 && length; dst++, src += 2, --length) {
172+
*dst = *src;
173+
}
174+
}
175+
169176
/*--------------------------------------------------------------------
170177
** Load the 'name' table, get information from it,
171178
** and store that information in the font structure.
@@ -311,6 +318,21 @@ void Read_name(struct TTFONT *font)
311318
font->PostName[length]=(char)NULL;
312319
replace_newlines_with_spaces(font->PostName);
313320

321+
#ifdef DEBUG_TRUETYPE
322+
debug("font->PostName=\"%s\"",font->PostName);
323+
#endif
324+
continue;
325+
}
326+
327+
/* Microsoft-format PostScript name */
328+
if ( platform == 3 && nameid == 6 )
329+
{
330+
free(font->PostName);
331+
font->PostName = (char*)calloc(sizeof(char),length+1);
332+
utf16be_to_ascii(font->PostName, (char *)strings+offset, length);
333+
font->PostName[length/2]=(char)NULL;
334+
replace_newlines_with_spaces(font->PostName);
335+
314336
#ifdef DEBUG_TRUETYPE
315337
debug("font->PostName=\"%s\"",font->PostName);
316338
#endif
@@ -967,6 +989,20 @@ const char *ttfont_CharStrings_getname(struct TTFONT *font, int charindex)
967989
char *ptr;
968990
ULONG len;
969991

992+
Fixed post_format;
993+
994+
/* The 'post' table format number. */
995+
post_format = getFixed( font->post_table );
996+
997+
if ( post_format.whole != 2 || post_format.fraction != 0 )
998+
{
999+
/* We don't have a glyph name table, so generate a name.
1000+
This generated name must match exactly the name that is
1001+
generated by FT2Font in get_glyph_name */
1002+
snprintf(temp, 80, "uni%08x", charindex);
1003+
return temp;
1004+
}
1005+
9701006
GlyphIndex = (int)getUSHORT( font->post_table + 34 + (charindex * 2) );
9711007

9721008
if ( GlyphIndex <= 257 ) /* If a standard Apple name, */
@@ -1011,11 +1047,6 @@ void ttfont_CharStrings(TTStreamWriter& stream, struct TTFONT *font, std::vector
10111047
/* The 'post' table format number. */
10121048
post_format = getFixed( font->post_table );
10131049

1014-
if ( post_format.whole != 2 || post_format.fraction != 0 )
1015-
{
1016-
throw TTException("TrueType fontdoes not have a format 2.0 'post' table");
1017-
}
1018-
10191050
/* Emmit the start of the PostScript code to define the dictionary. */
10201051
stream.printf("/CharStrings %d dict dup begin\n", glyph_ids.size());
10211052

0 commit comments

Comments
 (0)