config: move configs to config.h
[fbpad.git] / font.c
blob4d02ac77564125849d42ee5ddd57648ef8a20899
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 FT_New_Face(library, FONTFACE, 0, &face);
16 FT_Set_Char_Size(face, 0, FONTSIZE << 6, DPI, DPI);
17 rows = face->size->metrics.height >> 6;
18 cols = (face->size->metrics.max_advance >> 6) + WIDTHDIFF;
21 unsigned char *font_bitmap(int c)
23 static unsigned char bits[MAXDOTS];
24 int sr, sc, er, ec;
25 int i;
26 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
27 return NULL;
28 sr = MAX(0, rows + (face->size->metrics.descender >> 6) -
29 (face->glyph->metrics.horiBearingY >> 6));
30 sc = MAX(0, face->glyph->metrics.horiBearingX >> 6);
31 er = MIN(rows, sr + face->glyph->bitmap.rows);
32 ec = MIN(cols, sc + face->glyph->bitmap.width);
33 memset(bits, 0, sr * cols);
34 for (i = sr; i < er; i++) {
35 unsigned char *rowaddr = face->glyph->bitmap.buffer +
36 (i - sr) * face->glyph->bitmap.pitch;
37 memset(&bits[i * cols], 0, sc);
38 memcpy(&bits[i * cols + sc], rowaddr, ec - sc);
39 memset(&bits[i * cols + ec], 0, cols - ec);
41 memset(&bits[er * cols], 0, (rows - er) * cols);
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;