通过Freetype获取字符位图

本文介绍如何从ttf字体中获取某个字符所对应的位图数据

头文件

1
2
3
4
5
6
#include "ft2build.h"
#include "freetype/freetype.h"
#include "freetype/ftoutln.h"
#include <freetype/ftstroke.h>

#include <iostream>

freetype库

使用freetype库来生成数据,

  • 初始化
  • 加载字体
  • 设置编码字符集
  • 设置字号
1
2
3
4
5
6
7
8
9
10
11
12
FT_Library ft;
if(FT_Init_FreeType(&ft)){
std::cout<<"failed to init freetype."<<std::endl;
return -1;
}
FT_Face face;
if(FT_New_Face(ft,"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",0,&face)){
std::cout<<"failed to create new face"<<std::endl;
return -2;
}
FT_Select_Charmap(face,FT_ENCODING_UNICODE);
FT_Set_Pixel_Sizes(face,0,72);

普通字符(实心字符)

加载字符,渲染图形,显示数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FT_UInt glyph_index = FT_Get_Char_Index(face,charIndex);
FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT);//FT_LOAD_NO_HINTING

FT_GlyphSlot g = face->glyph;
FT_Render_Glyph(g,FT_RENDER_MODE_NORMAL);//NORMAL

int width = g->bitmap.width;
int height = g->bitmap.rows;

for(int i=0;i<width*height;i++){
if(i%width==0){
std::cout<<std::endl;
}
printf("%02X ",g->bitmap.buffer[i]);
}
std::cout<<std::endl;

也可以一步到位使用

1
FT_Load_Char();

轮廓字符

加载字符,设置轮廓参数,转换为位图,最后输出数据

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
FT_UInt glyph_index = FT_Get_Char_Index(face,charIndex);
FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT);//HINTING 3行 default 2

FT_Glyph glyph;
FT_Get_Glyph(face->glyph,&glyph);

FT_Stroker stroker;
FT_Stroker_New(ft,&stroker);
FT_Stroker_Set(stroker, 1.25 * 64 , FT_STROKER_LINECAP_BUTT,FT_STROKER_LINEJOIN_ROUND,1.25 * 64);
FT_Glyph_Stroke(&glyph,stroker,true);
FT_Vector v;
v.x=0;
v.y=0;
FT_Glyph_To_Bitmap(&glyph,FT_RENDER_MODE_NORMAL,&v,true);
FT_BitmapGlyph bmGlyph = reinterpret_cast<FT_BitmapGlyph>(glyph);
int width = bmGlyph->bitmap.width;
int height = bmGlyph->bitmap.rows;

for(int i=0;i<width*height;i++){
if(i%width==0){
std::cout<<std::endl;
}
printf("%02X ",bmGlyph->bitmap.buffer[i]);
}
std::cout<<std::endl;

释放内存

1
2
3
4
5
FT_Done_Face(face);
face = NULL;

FT_Done_FreeType(ft);
ft = NULL;

运行效果

测试字符是33号的英文!

轮廓

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
00 00 00 18 1C 1C 1C 1C 1C 1B 01 00 00 
00 00 A5 FF FF FF FF FF FF FF CC 01 00
00 00 F8 FF FF FF FF FF FF FF FF 20 00
00 00 F5 FF B7 64 64 64 9F FF FF 1D 00
00 00 F0 FF 8E 00 00 00 66 FF FF 18 00
00 00 EA FF 94 00 00 00 6B FF FF 13 00
00 00 E5 FF 99 00 00 00 70 FF FF 0E 00
00 00 E0 FF 9F 00 00 00 75 FF FF 09 00
00 00 DA FF A5 00 00 00 7A FF FF 05 00
00 00 D4 FF AB 00 00 00 7F FF FF 00 00
00 00 CE FF B2 00 00 00 87 FF F9 00 00
00 00 C6 FF BA 00 00 00 8E FF F2 00 00
00 00 BE FF C2 00 00 00 95 FF EB 00 00
00 00 B6 FF CA 00 00 00 9C FF E3 00 00
00 00 AE FF D2 00 00 00 A4 FF DC 00 00
00 00 A6 FF D9 00 00 00 AB FF D5 00 00
00 00 9F FF E1 00 00 00 B2 FF CE 00 00
00 00 97 FF E9 00 00 00 B9 FF C7 00 00
00 00 8F FF F1 00 00 00 C1 FF BF 00 00
00 00 87 FF F8 00 00 00 C8 FF B8 00 00
00 00 7F FF FF 01 00 00 CF FF B1 00 00
00 00 77 FF FF 08 00 00 D6 FF AA 00 00
00 00 6F FF FF 10 00 00 DD FF A3 00 00
00 00 68 FF FF 18 00 00 E5 FF 9B 00 00
00 00 60 FF FF 1F 00 00 EC FF 94 00 00
00 00 58 FF FF 27 00 00 F3 FF 8D 00 00
00 00 50 FF FF 2F 00 00 FA FF 86 00 00
00 00 48 FF FF 37 00 02 FF FF 7F 00 00
00 00 40 FF FF 3E 00 09 FF FF 77 00 00
00 00 38 FF FF 46 00 10 FF FF 70 00 00
00 00 31 FF FF 4E 00 17 FF FF 69 00 00
00 00 29 FF FF 56 00 1F FF FF 62 00 00
00 00 21 FF FF 5E 00 26 FF FF 5A 00 00
00 00 19 FF FF 65 00 2D FF FF 53 00 00
00 00 11 FF FF 6D 00 34 FF FF 4C 00 00
00 00 09 FF FF 75 00 3B FF FF 45 00 00
00 00 01 FF FF 7D 00 43 FF FF 3E 00 00
00 00 00 FA FF 84 00 4A FF FF 36 00 00
00 00 00 F2 FF 96 18 61 FF FF 2F 00 00
00 00 00 EA FF FF FF FF FF FF 28 00 00
00 00 00 BF FF FF FF FF FF F2 0C 00 00
00 00 00 14 61 68 68 68 67 28 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 0B 32 11 00 00 00 00 00
00 00 00 46 D0 FC FF FF DD 64 00 00 00
00 00 8D FF FF FF FF FF FF FF B5 05 00
00 4E FF FF EC 7C 4D 70 DB FF FF 7B 00
02 E3 FF E9 1A 00 00 00 0A CB FF F7 19
25 FF FF 68 00 00 00 00 00 3B FF FF 50
53 FF FF 2D 00 00 00 00 00 06 FB FF 7E
56 FF FF 28 00 00 00 00 00 04 F8 FF 82
27 FF FF 60 00 00 00 00 00 36 FF FF 52
02 E7 FF E4 18 00 00 00 09 C6 FF FA 1C
00 53 FF FF EB 78 4C 6C D8 FF FF 82 00
00 00 92 FF FF FF FF FF FF FF BA 06 00
00 00 00 4C D5 FD FF FF E0 68 01 00 00
00 00 00 00 00 0C 33 12 00 00 00 00 00

普通字符

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
00 00 9F DB DB DB DB DB C1 00 00 
00 00 B5 FF FF FF FF FF DC 00 00
00 00 AF FF FF FF FF FF D8 00 00
00 00 AA FF FF FF FF FF D3 00 00
00 00 A5 FF FF FF FF FF CE 00 00
00 00 9F FF FF FF FF FF C9 00 00
00 00 9A FF FF FF FF FF C4 00 00
00 00 94 FF FF FF FF FF C0 00 00
00 00 8D FF FF FF FF FF B9 00 00
00 00 85 FF FF FF FF FF B1 00 00
00 00 7D FF FF FF FF FF AA 00 00
00 00 75 FF FF FF FF FF A3 00 00
00 00 6D FF FF FF FF FF 9C 00 00
00 00 65 FF FF FF FF FF 95 00 00
00 00 5E FF FF FF FF FF 8D 00 00
00 00 56 FF FF FF FF FF 86 00 00
00 00 4E FF FF FF FF FF 7F 00 00
00 00 46 FF FF FF FF FF 78 00 00
00 00 3E FF FF FF FF FF 71 00 00
00 00 36 FF FF FF FF FF 69 00 00
00 00 2E FF FF FF FF FF 62 00 00
00 00 27 FF FF FF FF FF 5B 00 00
00 00 1F FF FF FF FF FF 54 00 00
00 00 17 FF FF FF FF FF 4D 00 00
00 00 0F FF FF FF FF FF 45 00 00
00 00 07 FF FF FF FF FF 3E 00 00
00 00 00 FE FF FF FF FF 37 00 00
00 00 00 F7 FF FF FF FF 30 00 00
00 00 00 EF FF FF FF FF 28 00 00
00 00 00 E8 FF FF FF FF 21 00 00
00 00 00 E0 FF FF FF FF 1A 00 00
00 00 00 D8 FF FF FF FF 13 00 00
00 00 00 D0 FF FF FF FF 0C 00 00
00 00 00 C8 FF FF FF FF 04 00 00
00 00 00 C0 FF FF FF FD 00 00 00
00 00 00 B8 FF FF FF F6 00 00 00
00 00 00 B1 FF FF FF EF 00 00 00
00 00 00 A9 FF FF FF E8 00 00 00
00 00 00 19 27 27 27 23 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
00 00 03 74 C8 F2 D1 8D 0C 00 00
00 05 C1 FF FF FF FF FF DF 18 00
00 7C FF FF FF FF FF FF FF AA 00
00 E3 FF FF FF FF FF FF FF FF 10
12 FF FF FF FF FF FF FF FF FF 3D
16 FF FF FF FF FF FF FF FF FF 42
00 E6 FF FF FF FF FF FF FF FF 13
00 81 FF FF FF FF FF FF FF AF 00
00 06 C4 FF FF FF FF FF E1 1A 00
00 00 03 75 C9 F3 D3 8F 0E 00 00

通过Freetype获取字符位图
https://blog.jackeylea.com/cpp/get-bitmap-data-with-freetype/
作者
JackeyLea
发布于
2023年8月8日
许可协议