font: clean up freetype library before exiting
[fbpad.git] / font.c
blob7cd9d1f1c870ed529c5c3de241e711127887166f
1 #include <ft2build.h>
2 #include FT_FREETYPE_H
3 #include FT_BITMAP_H
4 #include "util.h"
6 #define FONTFACE "/usr/lib/X11/fonts/TTF/DejaVuSansMono.ttf"
7 #define FONTSIZE 10
8 #define DPI 192
9 #define MAXDOTS (1 << 10)
10 #define WIDTHDIFF 1
12 static FT_Library library;
13 static FT_Face face;
14 static int rows, cols;
16 void font_init(void)
18 FT_Init_FreeType(&library);
19 FT_New_Face(library, FONTFACE, 0, &face);
20 FT_Set_Char_Size(face, 0, FONTSIZE << 6, DPI, DPI);
21 rows = face->size->metrics.height >> 6;
22 cols = (face->size->metrics.max_advance >> 6) + WIDTHDIFF;
25 unsigned char *font_bitmap(int c, int bold)
27 static unsigned char bits[MAXDOTS];
28 int sr, sc, er, ec;
29 int i;
30 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
31 return NULL;
32 if (bold) {
33 int FT_GlyphSlot_Own_Bitmap(FT_GlyphSlot);
34 FT_GlyphSlot_Own_Bitmap(face->glyph);
35 FT_Bitmap_Embolden(library, &face->glyph->bitmap, 32, 32);
37 sr = rows + (face->size->metrics.descender >> 6) -
38 (face->glyph->metrics.horiBearingY >> 6);
39 sc = face->glyph->metrics.horiBearingX >> 6;
40 er = MIN(rows, sr + face->glyph->bitmap.rows);
41 ec = MIN(cols, sc + face->glyph->bitmap.width);
42 memset(bits, 0, sr * cols);
43 for (i = sr; i < er; i++) {
44 unsigned char *rowaddr = face->glyph->bitmap.buffer +
45 (i - sr) * face->glyph->bitmap.pitch;
46 memset(&bits[i * cols], 0, sc);
47 memcpy(&bits[i * cols + sc], rowaddr, ec - sc);
48 memset(&bits[i * cols + ec], 0, cols - ec);
50 memset(&bits[er * cols], 0, (rows - er) * cols);
51 return bits;
54 void font_free(void)
56 FT_Done_Face(face);
57 FT_Done_FreeType(library);
60 int font_rows(void)
62 return rows;
65 int font_cols(void)
67 return cols;