FreeTypeを使用する開発環境設定

C++

ライブラリのインストールとコンパイルの設定を行います。

Windows + Visutal Studio

https://freetype.org/
Download ページから github repository に移動し、zip をダウンロードして展開します。

インクルードの設定

ライブラリの設定
ここではスタティックライブラリをリンクしています。ダイナミックライブラリをリンクする場合は、別途実行時に DLL が必要になります。

MacOS +  HomeBrew + Xcode

$ brew install freetype

$ pkg-config --cflags freetype2
-I/usr/local/opt/freetype/include/freetype2

$ pkg-config --libs freetype2
-L/usr/local/opt/freetype/lib -lfreetype

Xcode 上から
左上のプロジェクト名をクリック
Build Settings > All > Search Paths > Header Search Paths > /usr/local/opt/freetype/include/freetype2, non-recursive
ここでは dynlib をリンクしています。
Build Phases > Link Binary With Libraries
/usr/local/Cellar/freetype/2.11.0/lib/libfreetype.6.dylib をドロップ

Ubuntu

$ sudo apt update

$ sudo apt install libfreetype6-dev

$ pkg-config --cflags freetype2
-I/usr/include/freetype2 -I/usr/include/libpng16

$ pkg-config --libs freetype2
-lfreetype

サンプルコード

#include <ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <assert.h>

int main() {
	constexpr int ch_width = 48, ch_count = 128, str_length = 256;
	FT_Library ft;
	FT_Face face;
	FT_Error ft_error;
	ft_error = FT_Init_FreeType(&ft);
	assert(!ft_error && "init_resources: FT_Init_FreeType();");
	std::string font_name = "FreeSans.ttf";
	ft_error = FT_New_Face(ft, font_name.c_str(), 0, &face);
	assert(!ft_error && "init_resources: FT_New_Face();");
	FT_Set_Pixel_Sizes(face, 0, ch_width);
	for (unsigned char c = 32; c < ch_count; ++c) {
		ft_error = FT_Load_Char(face, c, FT_LOAD_RENDER);
		if (ft_error) {
			assert(!ft_error && "init_resources: FT_LOAD_RENDER();");
			continue;
		}
		std::cout << (char)c << ": width = " << face->glyph->bitmap.width << " rows = " << face->glyph->bitmap.rows;
		std::cout << " left = " << face->glyph->bitmap_left << " top = " << face->glyph->bitmap_top;
		std::cout << " advance.x = " << face->glyph->advance.x << std::endl;
	}
	FT_Done_Face(face);
	FT_Done_FreeType(ft);
	return 0;
}

コンパイル・実行

$ g++ freetype_test.cpp `pkg-config --cflags --libs freetype2`

$ ./a.out
 : width = 0 rows = 0 left = 0 top = 0 advance.x = 832
!: width = 5 rows = 35 left = 6 top = 35 advance.x = 896
": width = 12 rows = 12 left = 2 top = 34 advance.x = 1024
#: width = 26 rows = 35 left = 1 top = 34 advance.x = 1792
$: width = 24 rows = 43 left = 1 top = 37 advance.x = 1728
%: width = 40 rows = 35 left = 1 top = 34 advance.x = 2688
&: width = 29 rows = 36 left = 2 top = 35 advance.x = 2048
': width = 5 rows = 12 left = 2 top = 34 advance.x = 576
(: width = 11 rows = 46 left = 3 top = 35 advance.x = 1024
): width = 12 rows = 46 left = 1 top = 35 advance.x = 1024
*: width = 15 rows = 14 left = 2 top = 35 advance.x = 1216
+: width = 23 rows = 23 left = 2 top = 23 advance.x = 1728
,: width = 6 rows = 12 left = 4 top = 5 advance.x = 832
-: width = 11 rows = 4 left = 2 top = 16 advance.x = 960
.: width = 5 rows = 5 left = 4 top = 5 advance.x = 832
/: width = 15 rows = 36 left = -1 top = 35 advance.x = 832
0: width = 23 rows = 35 left = 2 top = 34 advance.x = 1728
1: width = 13 rows = 34 left = 4 top = 34 advance.x = 1728
2: width = 23 rows = 34 left = 2 top = 34 advance.x = 1728
3: width = 23 rows = 35 left = 2 top = 34 advance.x = 1728
4: width = 25 rows = 34 left = 1 top = 34 advance.x = 1728
5: width = 24 rows = 35 left = 1 top = 34 advance.x = 1728
6: width = 23 rows = 35 left = 2 top = 34 advance.x = 1728
7: width = 23 rows = 34 left = 2 top = 34 advance.x = 1728
8: width = 23 rows = 35 left = 2 top = 34 advance.x = 1728
9: width = 23 rows = 35 left = 2 top = 34 advance.x = 1728
:: width = 5 rows = 25 left = 5 top = 25 advance.x = 832
;: width = 5 rows = 32 left = 5 top = 25 advance.x = 832
<: width = 24 rows = 24 left = 2 top = 23 advance.x = 1792
=: width = 23 rows = 12 left = 2 top = 17 advance.x = 1728
>: width = 24 rows = 24 left = 2 top = 23 advance.x = 1792
?: width = 21 rows = 36 left = 4 top = 35 advance.x = 1728
@: width = 44 rows = 42 left = 2 top = 35 advance.x = 3136
A: width = 32 rows = 35 left = 0 top = 35 advance.x = 2048
B: width = 26 rows = 35 left = 4 top = 35 advance.x = 2048
C: width = 30 rows = 37 left = 2 top = 35 advance.x = 2176
D: width = 28 rows = 35 left = 4 top = 35 advance.x = 2240
E: width = 25 rows = 35 left = 4 top = 35 advance.x = 2048
F: width = 24 rows = 35 left = 4 top = 35 advance.x = 1856
G: width = 32 rows = 37 left = 2 top = 35 advance.x = 2368
H: width = 27 rows = 35 left = 4 top = 35 advance.x = 2240
I: width = 5 rows = 35 left = 5 top = 35 advance.x = 832
J: width = 20 rows = 36 left = 1 top = 35 advance.x = 1536
K: width = 28 rows = 35 left = 4 top = 35 advance.x = 2048
L: width = 22 rows = 35 left = 4 top = 35 advance.x = 1728
M: width = 33 rows = 35 left = 4 top = 35 advance.x = 2560
N: width = 28 rows = 35 left = 4 top = 35 advance.x = 2240
O: width = 34 rows = 37 left = 2 top = 35 advance.x = 2432
P: width = 25 rows = 35 left = 4 top = 35 advance.x = 1984
Q: width = 34 rows = 39 left = 2 top = 35 advance.x = 2432
R: width = 28 rows = 35 left = 4 top = 35 advance.x = 2176
S: width = 28 rows = 37 left = 2 top = 35 advance.x = 1984
T: width = 27 rows = 35 left = 1 top = 35 advance.x = 1856
U: width = 27 rows = 36 left = 4 top = 35 advance.x = 2240
V: width = 30 rows = 35 left = 1 top = 35 advance.x = 2048
W: width = 44 rows = 35 left = 1 top = 35 advance.x = 2880
X: width = 31 rows = 35 left = 1 top = 35 advance.x = 2048
Y: width = 32 rows = 35 left = 0 top = 35 advance.x = 2048
Z: width = 26 rows = 35 left = 1 top = 35 advance.x = 1792
[: width = 9 rows = 46 left = 3 top = 35 advance.x = 832
\: width = 15 rows = 36 left = -1 top = 35 advance.x = 832
]: width = 10 rows = 46 left = 1 top = 35 advance.x = 832
^: width = 19 rows = 19 left = 2 top = 35 advance.x = 1472
_: width = 29 rows = 3 left = -1 top = -5 advance.x = 1728
`: width = 11 rows = 7 left = 1 top = 36 advance.x = 1024
a: width = 24 rows = 28 left = 2 top = 26 advance.x = 1728
b: width = 23 rows = 36 left = 3 top = 35 advance.x = 1728
c: width = 22 rows = 28 left = 1 top = 26 advance.x = 1536
d: width = 23 rows = 36 left = 1 top = 35 advance.x = 1728
e: width = 23 rows = 28 left = 2 top = 26 advance.x = 1728
f: width = 12 rows = 36 left = 1 top = 35 advance.x = 896
g: width = 23 rows = 37 left = 1 top = 26 advance.x = 1664
h: width = 20 rows = 35 left = 3 top = 35 advance.x = 1664
i: width = 5 rows = 35 left = 3 top = 35 advance.x = 704
j: width = 9 rows = 46 left = -1 top = 35 advance.x = 640
k: width = 22 rows = 35 left = 3 top = 35 advance.x = 1536
l: width = 5 rows = 35 left = 3 top = 35 advance.x = 704
m: width = 34 rows = 26 left = 3 top = 26 advance.x = 2560
n: width = 21 rows = 26 left = 3 top = 26 advance.x = 1664
o: width = 23 rows = 28 left = 2 top = 26 advance.x = 1728
p: width = 23 rows = 37 left = 3 top = 26 advance.x = 1728
q: width = 23 rows = 37 left = 1 top = 26 advance.x = 1728
r: width = 12 rows = 26 left = 3 top = 26 advance.x = 1024
s: width = 21 rows = 28 left = 1 top = 26 advance.x = 1536
t: width = 12 rows = 34 left = 1 top = 32 advance.x = 896
u: width = 21 rows = 26 left = 3 top = 25 advance.x = 1728
v: width = 24 rows = 26 left = 0 top = 25 advance.x = 1536
w: width = 34 rows = 26 left = 0 top = 25 advance.x = 2240
x: width = 23 rows = 25 left = 0 top = 25 advance.x = 1536
y: width = 23 rows = 36 left = 1 top = 25 advance.x = 1536
z: width = 20 rows = 26 left = 1 top = 25 advance.x = 1472
{: width = 12 rows = 46 left = 2 top = 35 advance.x = 1024
|: width = 3 rows = 46 left = 5 top = 35 advance.x = 832
}: width = 12 rows = 46 left = 1 top = 35 advance.x = 1024
~: width = 21 rows = 9 left = 4 top = 22 advance.x = 1792
: width = 25 rows = 35 left = 4 top = 35 advance.x = 2176

コメント

タイトルとURLをコピーしました