fbpad: add other tag command
[fbpad.git] / font.c
blobaf2c88b2c1cb0db365a54ed231ce148f45553f3d
1 #include <ft2build.h>
2 #include FT_FREETYPE_H
3 #include FT_BITMAP_H
5 #define FONTFACE "/home/ali/.fonts/monaco.ttf"
6 #define FONTSIZE 10
7 #define DPI 192
8 #define MAXDOTS (1 << 10)
10 static FT_Library library;
11 static FT_Face face;
12 static int rows, cols;
14 void font_init(void)
16 FT_Init_FreeType(&library);
17 FT_New_Face(library, FONTFACE, 0, &face);
18 FT_Set_Char_Size(face, 0, FONTSIZE << 6, DPI, DPI);
19 rows = face->size->metrics.height >> 6;
20 cols = face->size->metrics.max_advance >> 6;
23 unsigned char *font_bitmap(int c, int bold)
25 static unsigned char bits[MAXDOTS];
26 int sr, sc, er, ec;
27 int i;
28 if (FT_Load_Char(face, c, FT_LOAD_RENDER))
29 return NULL;
30 if (bold) {
31 int FT_GlyphSlot_Own_Bitmap(FT_GlyphSlot);
32 FT_GlyphSlot_Own_Bitmap(face->glyph);
33 FT_Bitmap_Embolden(library, &face->glyph->bitmap, 32, 32);
35 sr = rows + (face->size->metrics.descender >> 6) -
36 (face->glyph->metrics.horiBearingY >> 6);
37 sc = face->glyph->metrics.horiBearingX >> 6;
38 er = sr + face->glyph->bitmap.rows;
39 ec = sc + face->glyph->bitmap.width;
40 memset(bits, 0, sr * cols);
41 for (i = sr; i < er; i++) {
42 unsigned char *rowaddr = face->glyph->bitmap.buffer +
43 (i - sr) * face->glyph->bitmap.pitch;
44 memset(&bits[i * cols], 0, sc);
45 memcpy(&bits[i * cols + sc], rowaddr, ec - sc);
46 memset(&bits[i * cols + ec], 0, cols - ec);
48 memset(&bits[er * cols], 0, (rows - er) * cols);
49 return bits;
52 void font_free(void)
56 int font_rows(void)
58 return rows;
61 int font_cols(void)
63 return cols;