13 load_font(const char *fname
)
19 size_t ranges_size
, data_size
;
22 fd
= open(fname
, O_RDONLY
);
25 read(fd
, sig
, sizeof(sig
));
26 if (memcmp(sig
, (char []) {'M', 'B', 'F', 0x01}, sizeof(sig
))) {
30 read(fd
, &header
, sizeof(header
));
31 /* stride = ceil(w / 8) = floor(w / 8) + (w % 8 ? 1 : 0) */
32 stride
= (header
.w
>> 3) + !!(header
.w
& 7);
33 ranges_size
= header
.nr
* sizeof(Range
);
34 data_size
= header
.ng
* stride
* header
.h
;
35 font
= malloc(sizeof(Font
) + ranges_size
+ data_size
);
40 font
->stride
= stride
;
41 font
->header
= header
;
42 font
->ranges
= (Range
*) &font
[1];
43 read(fd
, font
->ranges
, ranges_size
);
44 font
->data
= (uint8_t *) &font
->ranges
[header
.nr
];
45 read(fd
, font
->data
, data_size
);
51 search_glyph(Font
*font
, uint16_t code
)
57 for (i
= 0; i
< font
->header
.nr
; i
++) {
61 if (code
< r
.offset
+ r
.length
)
62 return index
+ code
- r
.offset
;
69 get_index(Font
*font
, uint16_t code
)
73 uint16_t codes
[] = {code
, 0xFFFD, 0x003F, 0x0020, 0};
75 for (cur_code
= &codes
[0]; *cur_code
; cur_code
++) {
76 index
= search_glyph(font
, *cur_code
);