Makefile: util.c is no longer needed
[fbpad.git] / font.c
blob0a09ef299304b436c24eece6e15557ff9aeb89f0
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 static void xdie(char *msg)
14 fprintf(stderr, "%s\n", msg);
15 exit(1);
18 void font_init(void)
20 FT_Init_FreeType(&library);
21 if (FT_New_Face(library, FONTFACE, 0, &face))
22 xdie("failed to load font");
23 FT_Set_Char_Size(face, 0, FONTSIZE << 6, DPI, DPI);
24 rows = face->size->metrics.height >> 6;
25 cols = (face->size->metrics.max_advance >> 6) + WIDTHDIFF;
28 unsigned char *font_bitmap(int c)
30 static unsigned char bits[MAXDOTS];
31 int sr, sc, er, ec;
32 int i;
33 unsigned char *src;
34 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
35 return NULL;
36 sr = rows + (face->size->metrics.descender >> 6) -
37 face->glyph->bitmap_top;
38 sc = face->glyph->bitmap_left;
39 er = MIN(rows, sr + face->glyph->bitmap.rows);
40 ec = MIN(cols, sc + face->glyph->bitmap.width);
41 memset(bits, 0, rows * cols);
42 src = face->glyph->bitmap.buffer - MIN(0, sc);
43 sc = MAX(0, sc);
44 for (i = MAX(0, sr); i < er; i++) {
45 int w = face->glyph->bitmap.pitch;
46 memcpy(&bits[i * cols + sc], src + (i - sr) * w, ec - sc);
48 return bits;
51 void font_free(void)
53 FT_Done_Face(face);
54 FT_Done_FreeType(library);
57 int font_rows(void)
59 return rows;
62 int font_cols(void)
64 return cols;