-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfxboot_font.c
204 lines (153 loc) · 5.11 KB
/
gfxboot_font.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <gfxboot/gfxboot.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// font
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
obj_id_t gfx_obj_font_new()
{
return gfx_obj_alloc(OTYPE_FONT, OBJ_FONT_SIZE());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
font_t *gfx_obj_font_ptr(obj_id_t id)
{
obj_t *ptr = gfx_obj_ptr(id);
if(!ptr || ptr->base_type != OTYPE_FONT) return 0;
return (font_t *) ptr->data.ptr;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_obj_font_dump(obj_t *ptr, dump_style_t style)
{
if(!ptr) return 1;
font_t *f = ptr->data.ptr;
unsigned len = ptr->data.size;
if(len != OBJ_FONT_SIZE()) {
if(style.ref) {
gfxboot_log(" <invalid data>\n");
}
else {
gfxboot_log("<invalid data>");
}
return 1;
}
if(!style.ref) {
if(!style.inspect) return 0;
gfxboot_log("glyphs %d, size %dx%d, line height %d, base %d", f->glyphs, f->width, f->height, f->line_height, f->baseline);
if(f->parent_id) gfxboot_log(", parent %s", gfx_obj_id2str(f->parent_id));
return 1;
}
if(style.dump) {
canvas_t *glyph = gfx_obj_canvas_ptr(f->glyph_id);
gfxboot_log(" type %u, glyphs %d\n", f->type, f->glyphs);
gfxboot_log(" font size %dx%d, line height %d, baseline %d\n", f->width, f->height, f->line_height, f->baseline);
if(glyph) {
gfxboot_log(" bitmap size %dx%d\n", glyph->max_width, glyph->max_height);
}
gfxboot_log(" bitmap table: offset %u, size %u\n", f->bitmap.offset, f->bitmap.size);
gfxboot_log(" char index: offset %u, size %u\n", f->unimap.offset, f->unimap.size);
gfxboot_log(" data_id %s\n", gfx_obj_id2str(f->data_id));
gfxboot_log(" glyph_id %s\n", gfx_obj_id2str(f->glyph_id));
}
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
unsigned gfx_obj_font_gc(obj_t *ptr)
{
if(!ptr) return 0;
font_t *font = ptr->data.ptr;
unsigned data_size = ptr->data.size;
unsigned more_gc = 0;
if(font && data_size == OBJ_FONT_SIZE()) {
more_gc += gfx_obj_ref_dec_delay_gc(font->parent_id);
more_gc += gfx_obj_ref_dec_delay_gc(font->data_id);
more_gc += gfx_obj_ref_dec_delay_gc(font->glyph_id);
}
return more_gc;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_obj_font_contains(obj_t *ptr, obj_id_t id)
{
if(!ptr || !id) return 0;
font_t *font = ptr->data.ptr;
unsigned data_size = ptr->data.size;
if(font && data_size == OBJ_FONT_SIZE()) {
if(id == font->parent_id || id == font->data_id || id == font->glyph_id) return 1;
}
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
obj_id_t gfx_obj_font_open(obj_id_t font_file)
{
data_t *mem = gfx_obj_mem_ptr(font_file);
unsigned ok = 0;
if(!mem) return 0;
uint8_t *ptr = mem->ptr;
unsigned size = mem->size;
obj_id_t font_id = gfx_obj_font_new();
font_t *font = gfx_obj_font_ptr(font_id);
if(!font) return 0;
font->data_id = font_file;
int max_bitmap_width = 0;
int max_bitmap_height = 0;
if(
size >= 0x20 &&
gfx_read_le32(ptr) == 0x864ab572
) {
unsigned header_size = gfx_read_le32(ptr + 8);
font->glyphs = gfx_read_le32(ptr + 16);
font->glyph_size = gfx_read_le32(ptr + 20);
font->height = (int) gfx_read_le32(ptr + 24);
font->width = (int) gfx_read_le32(ptr + 28);
font->line_height = font->height;
font->bitmap.offset = header_size;
font->bitmap.size = font->glyphs * font->glyph_size;
font->unimap.offset = font->bitmap.offset + font->bitmap.size;
font->unimap.size = size - font->unimap.offset;
max_bitmap_width = font->width;
max_bitmap_height = font->height;
if(
font->height == (int) font->glyph_size &&
font->width <= 8 &&
font->unimap.size > 0
) {
font->type = 1;
ok = 1;
}
}
else if(
size >= 0x10 &&
gfx_read_le32(ptr) == 0xa42a9123
) {
unsigned header_size = 0x10;
font->glyphs = gfx_read_le32(ptr + 12);
font->width = ptr[4];
max_bitmap_width = ptr[7];
max_bitmap_height = ptr[8];
font->height = ptr[9];
font->line_height = ptr[10];
font->baseline = (int8_t) ptr[11];
font->glyphs = gfx_read_le32(ptr + 12);
font->unimap.offset = header_size;
font->unimap.size = 5 * font->glyphs;
font->bitmap.offset = font->unimap.offset + font->unimap.size;
font->bitmap.size = size - font->bitmap.offset;
if(font->width > max_bitmap_width) max_bitmap_width = font->width;
if(font->height > max_bitmap_height) max_bitmap_height = font->height;
if(font->bitmap.size > 0) {
font->type = 2;
ok = 1;
}
}
if(ok) {
font->glyph_id = gfx_obj_canvas_new(max_bitmap_width, max_bitmap_height);
if(!font->glyph_id) ok = 0;
}
if(ok) {
gfx_obj_ref_inc(font->data_id);
}
else {
gfx_obj_ref_dec(font->glyph_id);
gfx_obj_ref_dec(font_id);
font_id = 0;
}
return font_id;
}