初探freetype字型庫

2021-06-19 18:47:30 字數 4749 閱讀 1424

文字的顯示依賴於字型字型檔,大致的字型字型檔分為點陣字型檔、筆畫字型檔和輪廓字型檔。

點陣字型檔:缺點比較明顯,縮放存在鋸齒,渲染旋轉等操作相對複雜,且效果不理想,先大多用在嵌入式行業(基本拋棄),常見格式有bdf,pcf,fnt,hbf,hzf等。

筆畫字型:不討論。

輪廓字型:即向量字型,利用字型輪廓及填充實現字型顯示,優勢明顯,渲染縮放較容易,但效率相對低些(相對於嵌入式)

簡單來說,freetype為字型字型檔提供了一套解決方案,支援文字字型渲染等操作,主要還是其為c語言編寫,跨平台,為很多不支援向量字型格式的嵌入式系統提供使用嵌入式字型的可能,且效率不低。

基本流程為:

載入字型字型檔檔案-> 查詢待顯示的文字索引-> 渲染操作(若反走樣處理)->處理為位圖資料->顯示

freetype官網

在view.h中新增標頭檔案宣告

1

2

#include

#include ft_freetype_h

在view.h中新增成員變數

1

2

3

public:

ft_library library;

ft_face face;

在view.cpp的建構函式中新增

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// 初始化庫

boolberror = ft_init_freetype(&library);

if(!berror)

// 載入乙個字型檔檔案,這裡為黑體中文字庫

berror = ft_new_face(library,

"c:\\windows\\fonts\\simhei.ttf",

0, &face);

if(berror == ft_err_unknown_file_format)

elseif(berror)

// 設定為unicode,預設也是

ft_select_charmap(face,ft_encoding_unicode);

// 設定字型字元寬高和解析度

berror = ft_set_char_size(face, 0, 16*64, 300, 300);

在::ondraw(cdc* pdc)中新增**

1

2

3

4

5

6

7

8

9

10

11

12

13

14

boolberror;

wchar_twchar= _t('博');

// 查詢『好』的字元索引

ft_uint glyph_index = ft_get_char_index(face, wchar);

// 裝載字型影象到字形槽

berror = ft_load_glyph(face, glyph_index, ft_load_default);

// 轉換為位圖資料

if(face->glyph->format != ft_glyph_format_bitmap)

這裡便可以通過face->glyph->bitmap獲得字型「博」的點陣圖資料了,bitmap中存放了如位圖的寬高、色深,調色盤等資訊,便可以通過gdi+繪製該影象了 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

//建立位位圖

bitmapinfo bmpinfo = ;

// 初始化位圖結構體

bmpinfo.bmiheader.bisize =sizeof(bitmapinfoheader);

bmpinfo.bmiheader.biwidth = face->glyph->bitmap.width;

bmpinfo.bmiheader.biheight = face->glyph->bitmap.rows;

bmpinfo.bmiheader.bibitcount = 1;// 與渲染模式有關,詳見freetype api手冊的ft_bitmap部分說明

bmpinfo.bmiheader.biclrimportant = 0;

bmpinfo.bmiheader.biclrused = 0;

bmpinfo.bmiheader.bicompression = bi_rgb;

bmpinfo.bmiheader.biplanes = 1;

bmpinfo.bmiheader.bisizeimage = 0;

// 建立記憶體位圖

unsignedchar*pvbits =newunsignedchar[10000];

hbitmaphbitmap =createdibsection(null, &bmpinfo, dib_rgb_colors, (void** )&pvbits, null, 0 );

intilinebytes = (bmpinfo.bmiheader.biwidth + 7) / 8;

for(inti = 0; i != bmpinfo.bmiheader.biheight; ++i)

bitmap *pbitmap = bitmap::fromhbitmap(hbitmap, null);

graphics graphic(pdc->m_hdc);

graphic.drawimage(pbitmap, point(20, 150));

這部分**不多解釋,只是顯示位圖資料,這裡face->glyph->bitmap是沒有調色盤的1位位圖,源於使用ft_render_mode_mono渲染模式

Xcode新增字型庫

新增自定義字型的方法 2 把這個檔案新增到工程裡面 4 新增成功後,就可以使用啦。但是我只拿到這個庫,並不清楚fontname。用下面這段 列印出來系統可用字型。nsarray familynames nsarrayalloc initwitharray uifontfamilynames nsar...

iOS 引入字型庫

方法十分簡單,首先,把字型庫檔案拖入專案當中 使用方法,uifont font uifont fontwithname 你的字型名字 size 12 或者在在storyboard 或xib的控制項font 屬性中直接設定。檢視字型名的方法,在finder中找到字型檔案 顯示簡介中就可以看到。另外可以...

android icu字型庫修改

需要修改的檔案位於external icu icu4c source data locales 1.cd到 external icu icu4c source路徑 2.執行 runconfigureicu linux,這一步主要是生成make檔案 3.執行 make include uni core...