Skip to content

Commit f5b94a8

Browse files
committed
Add freetype example
1 parent d01d0b0 commit f5b94a8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

freetype-render.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// gcc freetype-render.c `pkg-config --libs --cflags freetype2` -Wall && a.out
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#include <ft2build.h>
6+
#include FT_FREETYPE_H
7+
#include FT_GLYPH_H
8+
9+
int main() {
10+
FT_Library library;
11+
FT_Init_FreeType(&library);
12+
FT_Face face;
13+
FT_New_Face(library, "input/Roboto.abc.ttf", 0, &face);
14+
FT_Set_Char_Size(face, 36 * 64, 36 * 64, 0, 0);
15+
16+
FT_Load_Glyph(face, 2 /* gid=3 is 'b' in this particular font */,
17+
FT_LOAD_DEFAULT);
18+
19+
FT_GlyphSlot slot = face->glyph;
20+
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
21+
22+
unsigned width = slot->bitmap.width;
23+
unsigned height = slot->bitmap.rows;
24+
25+
FILE *f = fopen("out.pbm", "wb");
26+
fprintf(f, "P5 %d %d %d\n", width, height, slot->bitmap.num_grays - 1);
27+
fwrite(slot->bitmap.buffer, sizeof(char), width * height, f);
28+
fclose(f);
29+
30+
FT_Done_Face(face);
31+
FT_Done_FreeType(library);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)