9 int rows
, cols
; /* glyph bitmap rows and columns */
10 int n
; /* number of font glyphs */
11 int *glyphs
; /* glyph unicode character codes */
12 char *data
; /* glyph bitmaps */
16 * This tinyfont header is followed by:
18 * glyphs[n] unicode character codes (int)
19 * bitmaps[n] character bitmaps (char[rows * cols])
22 char sig
[8]; /* tinyfont signature; "tinyfont" */
23 int ver
; /* version; 0 */
24 int n
; /* number of glyphs */
25 int rows
, cols
; /* glyph dimensions */
28 static void *xread(int fd
, int len
)
30 void *buf
= malloc(len
);
31 if (buf
&& read(fd
, buf
, len
) == len
)
37 struct font
*font_open(char *path
)
41 int fd
= open(path
, O_RDONLY
);
42 if (fd
< 0 || read(fd
, &head
, sizeof(head
)) != sizeof(head
)) {
46 font
= malloc(sizeof(*font
));
48 font
->rows
= head
.rows
;
49 font
->cols
= head
.cols
;
50 font
->glyphs
= xread(fd
, font
->n
* sizeof(int));
51 font
->data
= xread(fd
, font
->n
* font
->rows
* font
->cols
);
53 if (!font
->glyphs
|| !font
->data
) {
60 static int find_glyph(struct font
*font
, int c
)
66 if (font
->glyphs
[m
] == c
)
68 if (c
< font
->glyphs
[m
])
76 int font_bitmap(struct font
*font
, void *dst
, int c
)
78 int i
= find_glyph(font
, c
);
79 int len
= font
->rows
* font
->cols
;
82 memcpy(dst
, font
->data
+ i
* len
, len
);
86 void font_free(struct font
*font
)
88 if (font
&& font
->data
)
90 if (font
&& font
->glyphs
)
95 int font_rows(struct font
*font
)
100 int font_cols(struct font
*font
)