term: fix a few vt102 bugs
[fbpad.git] / font.c
blob0d66b3c2422bcf79b7103e1c1cbd7a2f791650ab
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 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
28 return NULL;
29 sr = MAX(0, rows + (face->size->metrics.descender >> 6) -
30 (face->glyph->metrics.horiBearingY >> 6));
31 sc = MAX(0, face->glyph->metrics.horiBearingX >> 6);
32 er = MIN(rows, sr + face->glyph->bitmap.rows);
33 ec = MIN(cols, sc + face->glyph->bitmap.width);
34 memset(bits, 0, sr * cols);
35 for (i = sr; i < er; i++) {
36 unsigned char *rowaddr = face->glyph->bitmap.buffer +
37 (i - sr) * face->glyph->bitmap.pitch;
38 memset(&bits[i * cols], 0, sc);
39 memcpy(&bits[i * cols + sc], rowaddr, ec - sc);
40 memset(&bits[i * cols + ec], 0, cols - ec);
42 memset(&bits[er * cols], 0, (rows - er) * cols);
43 return bits;
46 void font_free(void)
48 FT_Done_Face(face);
49 FT_Done_FreeType(library);
52 int font_rows(void)
54 return rows;
57 int font_cols(void)
59 return cols;