forked from ejoy/ejoy2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfont.c
382 lines (349 loc) · 8.58 KB
/
dfont.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "dfont.h"
#include "list.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#define HASH_SIZE 4096
#define TINY_FONT 12
struct hash_rect {
struct hash_rect * next_hash;
struct list_head next_char;
struct list_head time;
int version;
int c;
int line;
int font;
int edge;
struct dfont_rect rect;
};
struct font_line {
int start_line;
int height;
int space;
struct list_head head;
};
struct dfont {
int width;
int height;
int max_line;
int version;
struct list_head time;
struct hash_rect *freelist;
struct font_line *line;
struct hash_rect *hash[HASH_SIZE];
};
static void
init_hash(struct dfont *df, int max) {
int i;
for (i=0;i<max;i++) {
struct hash_rect * hr = &df->freelist[i];
hr->next_hash = &df->freelist[i+1];
}
df->freelist[max-1].next_hash = NULL;
memset(df->hash, 0, sizeof(df->hash));
}
#ifdef FONT_EDGE_HASH
static inline int
hash(int c, int font, int edge) {
if (edge != 0) {
edge = 1;
}
return ((unsigned)(((c ^ (font * 97))<<1)|edge)) % HASH_SIZE;
}
#else
static inline int
hash(int c, int font, int edge) {
return ((unsigned)(c ^ (font * 97))) % HASH_SIZE;
}
#endif
size_t
dfont_data_size(int width, int height) {
int max_line = height / TINY_FONT;
int max_char = max_line * width / TINY_FONT;
size_t ssize = max_char * sizeof(struct hash_rect);
size_t lsize = max_line * sizeof(struct font_line);
return sizeof(struct dfont) + ssize + lsize;
}
void
dfont_init(void* d, int width, int height) {
int max_line = height / TINY_FONT;
int max_char = max_line * width / TINY_FONT;
size_t ssize = max_char * sizeof(struct hash_rect);
struct dfont *df = (struct dfont*)d;
df->width = width;
df->height = height;
df->max_line = 0;
df->version = 0;
INIT_LIST_HEAD(&df->time);
df->freelist = (struct hash_rect *)(df+1);
df->line = (struct font_line *)((intptr_t)df->freelist + ssize);
init_hash(df, max_char);
}
struct dfont *
dfont_create(int width, int height) {
size_t size = dfont_data_size(width, height);
void *df = malloc(size);
dfont_init(df, width, height);
return (struct dfont*)df;
}
void
dfont_release(struct dfont *df) {
free(df);
}
void
dfont_flush(struct dfont *df) {
++df->version;
}
void
dfont_remove(struct dfont *df, int c, int font, int edge) {
int h = hash(c, font, edge);
struct hash_rect *hr = df->hash[h];
while (hr) {
if (hr->c == c && hr->font == font && hr->edge == edge) {
list_move(&hr->time, &df->time);
hr->version = df->version-1;
return;
}
hr = hr->next_hash;
}
}
const struct dfont_rect *
dfont_lookup(struct dfont *df, int c, int font, int edge) {
int h = hash(c, font, edge);
struct hash_rect *hr = df->hash[h];
while (hr) {
if (hr->c == c && hr->font == font && hr->edge == edge) {
list_move_tail(&hr->time, &df->time);
hr->version = df->version;
return &(hr->rect);
}
hr = hr->next_hash;
}
return NULL;
}
static struct font_line *
new_line(struct dfont *df, int height) {
int start_line = 0;
if (df->max_line > 0) {
struct font_line * lastline = &df->line[df->max_line-1];
start_line = lastline->start_line + lastline->height;
}
if (start_line + height > df->height)
return NULL;
int max = df->height / TINY_FONT;
if (df->max_line >= max)
return NULL;
struct font_line * line = &df->line[df->max_line++];
line->start_line = start_line;
line->height = height;
line->space = df->width;
INIT_LIST_HEAD(&line->head);
return line;
}
static struct font_line *
find_line(struct dfont *df, int width, int height) {
int i;
for (i=0;i<df->max_line;i++) {
struct font_line * line = &df->line[i];
if (height == line->height && width <= line->space) {
return line;
}
}
return new_line(df, height);
}
static struct hash_rect *
new_node(struct dfont *df) {
if (df->freelist == NULL)
return NULL;
struct hash_rect *ret = df->freelist;
df->freelist = ret->next_hash;
return ret;
}
static struct hash_rect *
find_space(struct dfont *df, struct font_line *line, int width) {
int start_pos = 0;
struct hash_rect * hr;
int max_space = 0;
list_for_each_entry(hr, struct hash_rect, &line->head, next_char) {
int space = hr->rect.x - start_pos;
if (space >= width) {
struct hash_rect *n = new_node(df);
if (n == NULL)
return NULL;
n->line = line - df->line;
n->rect.x = start_pos;
n->rect.y = line->start_line;
n->rect.w = width;
n->rect.h = line->height;
list_add_tail(&n->next_char, &hr->next_char);
return n;
}
if (space > max_space) {
max_space = space;
}
start_pos = hr->rect.x + hr->rect.w;
}
int space = df->width - start_pos;
if (space < width) {
if (space > max_space) {
line->space = space;
} else {
line->space = max_space;
}
return NULL;
}
struct hash_rect *n = new_node(df);
if (n == NULL)
return NULL;
n->line = line - df->line;
n->rect.x = start_pos;
n->rect.y = line->start_line;
n->rect.w = width;
n->rect.h = line->height;
list_add_tail(&n->next_char, &line->head);
return n;
}
static void
adjust_space(struct dfont *df, struct hash_rect *hr) {
struct font_line *line = &df->line[hr->line];
if (hr->next_char.next == &line->head) {
hr->rect.w = df->width - hr->rect.x;
} else {
struct hash_rect *next = list_entry(hr->next_char.next, struct hash_rect, next_char);
hr->rect.w = next->rect.x - hr->rect.x;
}
if (hr->next_char.prev == &line->head) {
hr->rect.w += hr->rect.x;
hr->rect.x = 0;
} else {
struct hash_rect *prev = list_entry(hr->next_char.prev, struct hash_rect, next_char);
int x = prev->rect.x + prev->rect.w;
hr->rect.w += hr->rect.x - x;
hr->rect.x = x;
}
if (hr->rect.w > line->space) {
line->space = hr->rect.w;
}
}
static struct hash_rect *
release_char(struct dfont *df, int c, int font, int edge) {
int h = hash(c, font, edge);
struct hash_rect *hr = df->hash[h];
if (hr->c == c && hr->font == font && hr->edge == edge) {
df->hash[h] = hr->next_hash;
list_del(&hr->time);
adjust_space(df, hr);
return hr;
}
struct hash_rect * last = hr;
hr = hr->next_hash;
while (hr) {
if (c == hr->c && hr->font == font && hr->edge == edge) {
last->next_hash = hr->next_hash;
list_del(&hr->time);
adjust_space(df, hr);
return hr;
}
last = hr;
hr = hr->next_hash;
}
assert(0);
return NULL;
}
static struct hash_rect *
release_space(struct dfont *df, int width, int height) {
struct hash_rect *hr, *tmp;
list_for_each_entry_safe(hr, struct hash_rect, tmp, &df->time, time) {
if (hr->version == df->version)
continue;
if (hr->rect.h != height) {
continue;
}
struct hash_rect * ret = release_char(df, hr->c, hr->font, hr->edge);
int w = hr->rect.w;
if (w >= width) {
ret->rect.w = width;
return ret;
} else {
list_del(&ret->next_char);
ret->next_hash = df->freelist;
df->freelist = ret;
}
}
return NULL;
}
static struct dfont_rect *
insert_char(struct dfont *df, int c, int font, struct hash_rect *hr, int edge) {
hr->c = c;
hr->font = font;
hr->edge = edge;
hr->version = df->version;
list_add_tail(&hr->time, &df->time);
int h = hash(c, font, edge);
hr->next_hash = df->hash[h];
df->hash[h] = hr;
return &hr->rect;
}
const struct dfont_rect *
dfont_insert(struct dfont *df, int c, int font, int width, int height, int edge) {
if (width > df->width)
return NULL;
assert(dfont_lookup(df,c,font,edge) == NULL);
for (;;) {
struct font_line *line = find_line(df, width, height);
if (line == NULL)
break;
struct hash_rect * hr = find_space(df, line, width);
if (hr) {
return insert_char(df,c,font,hr,edge);
}
}
struct hash_rect * hr = release_space(df, width, height);
if (hr) {
return insert_char(df,c,font,hr,edge);
}
return NULL;
}
static void
dump_node(struct hash_rect *hr) {
printf("(%d/%d : %d %d %d %d) ", hr->c, hr->font, hr->rect.x, hr->rect.y, hr->rect.w, hr->rect.h);
}
void
dfont_dump(struct dfont * df) {
printf("version = %d\n",df->version);
printf("By version : ");
struct hash_rect *hr;
int version = -1;
list_for_each_entry(hr, struct hash_rect, &df->time, time) {
if (hr->version != version) {
version = hr->version;
printf("\nversion %d : ", version);
}
dump_node(hr);
}
printf("\n");
printf("By line : \n");
int i;
for (i=0;i<df->max_line;i++) {
struct font_line *line = &df->line[i];
printf("line (y=%d h=%d space=%d) :",line->start_line, line->height,line->space);
list_for_each_entry(hr, struct hash_rect, &line->head, next_char) {
printf("%d(%d-%d) ",hr->c,hr->rect.x,hr->rect.x+hr->rect.w-1);
}
printf("\n");
}
printf("By hash : \n");
for (i=0;i<HASH_SIZE;i++) {
struct hash_rect *hr = df->hash[i];
if (hr) {
printf("%d : ",i);
while (hr) {
dump_node(hr);
hr = hr->next_hash;
}
printf("\n");
}
}
}