write screenshots to a file
[fbpad.git] / font.c
blobaa48146bb0cde84efda073b746eff481466299e0
1 #include <ft2build.h>
2 #include FT_FREETYPE_H
3 #include FT_BITMAP_H
4 #include "config.h"
5 #include "font.h"
6 #include "util.h"
8 static FT_Library library;
9 static FT_Face face;
10 static int rows, cols;
12 void font_init(void)
14 FT_Init_FreeType(&library);
15 if (FT_New_Face(library, FONTFACE, 0, &face))
16 xerror("failed to load font");
17 FT_Set_Char_Size(face, 0, FONTSIZE << 6, DPI, DPI);
18 rows = face->size->metrics.height >> 6;
19 cols = (face->size->metrics.max_advance >> 6) + WIDTHDIFF;
22 unsigned char *font_bitmap(int c)
24 static unsigned char bits[MAXDOTS];
25 int sr, sc, er, ec;
26 int i;
27 unsigned char *src;
28 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
29 return NULL;
30 sr = rows + (face->size->metrics.descender >> 6) -
31 face->glyph->bitmap_top;
32 sc = face->glyph->bitmap_left;
33 er = MIN(rows, sr + face->glyph->bitmap.rows);
34 ec = MIN(cols, sc + face->glyph->bitmap.width);
35 memset(bits, 0, rows * cols);
36 src = face->glyph->bitmap.buffer - MIN(0, sc);
37 sc = MAX(0, sc);
38 for (i = MAX(0, sr); i < er; i++) {
39 int w = face->glyph->bitmap.pitch;
40 memcpy(&bits[i * cols + sc], src + (i - sr) * w, ec - sc);
42 return bits;
45 void font_free(void)
47 FT_Done_Face(face);
48 FT_Done_FreeType(library);
51 int font_rows(void)
53 return rows;
56 int font_cols(void)
58 return cols;