1. 程式人生 > >freetype使用日記 -- 解決宋體,楷體中文在某些字號下無法正常顯示的問題

freetype使用日記 -- 解決宋體,楷體中文在某些字號下無法正常顯示的問題

問題:

在使用freetype過程中發現,從window下拷貝來的simsun.ttc, simkai.ttf兩個字型,

在呼叫

FT_Set_Pixel_Sizes(face, 12, 0);

將字型大小設定為12×12pixels,或者16×16pixels等時,中文無法正常畫出,英文可以正常畫出。

而設定為10×10pixepixels, 17×17pixels時,卻能正常畫出中文。

其實解決方法是比較簡單的,不過還是貼出來供新手有個參考。

分析:

後來經過跟蹤發現在設定為12×12pixels時,

FT_Load_Glyph(face, FT_Get_Char_Index(face, szText), FT_LOAD_DEFAULT);

函式在load時,從字型檔案中load的時bitmapped image,而不是outline,而可能是字型中的bitmapped image有問題,從而造成無法正常畫出中文。

聯想到freetype幫助文件中有如下的描述

Loading a glyph image into the slot is performed by calling FT_Load_Glyph as in

error = FT_Load_Glyph( face, /* handle to face object */ glyph_index, /* glyph index */ load_flags ); /* load flags, see below */

The load_flags value is a set of bit flags used to indicate some special operations. The default value FT_LOAD_DEFAULT is 0.

This function will try to load the corresponding glyph image from the face:

  • If a bitmap is found for the corresponding glyph and pixel size, it will be loaded into the slot. Embedded bitmaps are always favored over native image formats, because we assume that they are higher-quality versions of the same glyph. This can be changed by using the FT_LOAD_NO_BITMAP

    flag.

  • Otherwise, a native image for the glyph will be loaded. It will also be scaled to the current pixel size, as well as hinted for certain formats like TrueType and Type 1.

解決辦法:

error = FT_Load_Glyph(face, FT_Get_Char_Index(face, szText), FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP);

這樣強制在load時,loadoutline,然後再根據outline得到bitmapped image。

通過這樣修改後,上面提到的問題得到解決。