MacOS下Brush Script MT字体调用FT_Load_Glyph返回140错误排查
FreeType 2.4.2在MacOS加载Brush Script MT字体时FT_Load_Glyph返回错误码140(函数定义过多)
开发的程序使用FreeType库在屏幕绘制字形,在MacOS系统加载Brush Script MT字体文件时,FT_Load_Glyph返回错误码140,该错误在FreeType文档中描述为“too many function definitions”(函数定义过多)。此问题仅出现在MacOS,Windows系统无异常,且目前未发现其他字体触发该错误。
受此影响,Brush Script MT字体的所有字符均无法在屏幕上渲染,目前临时解决方案是使用占位字体Lucida Grande作为替代。
最小复现代码
以下是使用Xojo编写的最小复现示例(语法与Visual Basic类似),在MacOS上调试时FT_Load_Glyph会返回140错误:
// INITIALIZE FREETYPE LIBRARY dim freetype_library_ptr as ptr soft declare function FT_Init_FreeType lib freetype (byRef alibrary as ptr) as int32 dim err as int32 = FT_Init_FreeType(freetype_library_ptr) // CREATE BRUSH SCRIPT MT FACE dim face_index as integer = 0 dim p as ptr dim brush_script_file as new folderitem("/System/Library/Fonts/Supplemental/Brush Script.ttf") dim path as string = brush_script_file.shellpath #if targetMacOS path=replaceall(path,"\","") #endif soft declare function FT_New_Face lib freetype (library as ptr,filepathname as cstring,face_index as int32,byref aface as ptr) as int32 err=FT_New_Face(freetype_library_ptr,path,face_index,p) // SET CHARACTER SIZE FOR GLYPH dim char_width as int32 = 0 dim char_height as int32 = 262144 dim horz_resolution as uint32 = 72 dim vert_resolution as uint32 = 72 soft declare function FT_Set_Char_Size lib freetype (face as ptr,char_width as int32,char_height as int32,horz_resolution as uint32,vert_resolution as uint32) as int32 err=FT_Set_Char_Size(p,char_width,char_height,horz_resolution,vert_resolution) // RETRIEVE A GLYPH FROM THE FACE dim glyph_index as integer = 75 // Index for K character dim load_flags as integer = 0 soft declare function FT_Load_Glyph lib freetype (face as ptr,glyph_index as uint32,load_flags as int32) as int32 err=FT_Load_Glyph(p,glyph_index,load_flags) // ERROR 140 on MacOS: too many function definitions // FINISHED USING BRUSH SCRIPT MT FACE soft declare function FT_Done_Face lib freetype (face as ptr) as int32 err=FT_Done_Face(p) // FINISHED USING FREETYPE LIBRARY soft declare function FT_Done_FreeType lib freetype (library as ptr) as int32 err=FT_Done_FreeType(freetype_library_ptr)
关键信息
- 使用FreeType 2.4.2版本
- 运行平台:MacOS Monterey(12.6.5版本)
调试发现
以调试模式重新编译FreeType库后,观察加载Brush Script MT字体字形时的日志:调用FT_Load_Glyph时,日志会打印一次“NPUSHB”和11次“FDEF”。
在依赖文件ttinterp.c中找到如下代码,确认是numFDefs超过maxFDefs的限制触发了错误,但不清楚Brush Script MT字体会出现这种情况的原因:
if ( CUR.numFDefs >= CUR.maxFDefs ) { CUR.error = TT_Err_Too_Many_Function_Defs; return; } CUR.numFDefs++;
字体特殊点
Brush Script MT并非MacOS默认系统字体,随Microsoft Word安装而来,且该字体家族仅包含斜体样式。
内容的提问来源于stack exchange,提问作者Shem S
相关产品推荐
相关产品推荐

