-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont.c
More file actions
412 lines (368 loc) · 12.5 KB
/
Copy pathfont.c
File metadata and controls
412 lines (368 loc) · 12.5 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* GFX library font routines */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "draw.h"
static int oversamp = 24; /* min 20 for no holes in diagonal oversampling */
static int fast_sin_table[180] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, /* 0 */
16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, /* 15 */
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 41, 42, 43, 44, /* 30 */
45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 53, 54, 54, /* 45 */
55, 55, 56, 57, 57, 58, 58, 58, 59, 59, 60, 60, 60, 61, 61, /* 60 */
61, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, /* 75 */
64, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 62, 62, 62, 62, /* 90 */
61, 61, 61, 60, 60, 60, 59, 59, 58, 58, 58, 57, 57, 56, 55, /* 105 */
55, 54, 54, 53, 53, 52, 51, 51, 50, 49, 49, 48, 47, 46, 46, /* 120 */
45, 44, 43, 42, 41, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, /* 135 */
31, 31, 30, 29, 28, 27, 26, 25, 23, 22, 21, 20, 19, 18, 17, /* 150 */
16, 15, 14, 13, 12, 11, 10, 8, 7, 6, 5, 4, 3, 2, 1 /* 165 */
};
/* fast fixed point 26.6 sin/cos for angles in degrees */
static int fast_sin(int angle)
{
//angle %= 360;
if (angle >= 360) angle -= 360;
else if (angle < 0) angle += 360;
if (angle >= 180) /* for angles >= 180 invert sign*/
return -fast_sin_table[angle - 180];
return fast_sin_table[angle];
}
static int fast_cos(int angle)
{
return fast_sin(angle + 90); /* cos is sin plus 90 degrees */
}
/* convert character to font glyph index, return default glyph if not present */
static int glyph_offset(Font *font, unsigned int c)
{
uint16_t first, last, offset = 0;
uint16_t *r = font->range;
if (r) { /* charcode range ordered by glyph index */
do {
first = r[0]; last = r[1];
if (c >= first && c <= last)
return c-first+offset;
r += 2;
offset += last - first + 1;
} while (offset < font->size);
return font->defaultglyph;
}
c -= font->firstchar;
if (c >= font->size) c = font->defaultglyph;
return c;
}
/* draw a character from bitmap font, drawbg=2 means fill bg to max width */
int draw_font_bitmap(Drawable *dp, Font *font, int c, int sx, int sy, int xoff, int yoff,
Pixel fgpixel, Pixel bgpixel, int drawbg, int rotangle)
{
int x, y, minx, maxx, w, zerox, dspan;
int height = font->height;
int bitcount = 0;
uint32_t word;
uint32_t bitmask = 1 << ((font->bits_width << 3) - 1); /* MSB first */
Pixel *dst;
Varptr bits;
int sin_a, cos_a, s; /* for rotated bitmaps */
c = glyph_offset(font, c);
/* get glyph bitmap start */
if (font->offset.ptr8) {
switch (font->offset_width) {
case 1:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr8[c];
break;
case 2:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr16[c];
break;
case 4:
default:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr32[c];
break;
}
} else bits.ptr8 = font->bits.ptr8 + c * font->bits_width * font->height;
if (rotangle) {
sin_a = fast_sin(rotangle);
cos_a = fast_cos(rotangle);
}
x = y = 0;
minx = x;
w = font->width? font->width[c]: font->maxwidth;
/* draw max font width background if proportional font and console output */
if (drawbg == 2 && w != font->maxwidth) {
zerox = minx + w;
maxx = zerox + (font->maxwidth - w);
dspan = dp->pitch - ((maxx - minx) * dp->bytespp);
} else {
maxx = minx + w;
zerox = 9999;
dspan = dp->pitch - (w * dp->bytespp);
}
dst = (uint32_t *)(dp->pixels + (sy+yoff) * dp->pitch + (sx+xoff) * dp->bytespp);
do {
if (bitcount <= 0) {
bitcount = font->bits_width << 3;
switch (font->bits_width) {
case 1:
word = *bits.ptr8++;
break;
case 2:
default:
word = *bits.ptr16++;
break;
case 4:
word = *bits.ptr32++;
break;
}
}
s = 0;
do {
if (rotangle) {
int dx = sx + ((cos_a * (((x+xoff) << 6) + s)
- sin_a * (((y+yoff) << 6) + s) + (1 << 11)) >> 12);
int dy = sy + ((sin_a * (((x+xoff) << 6) + s)
+ cos_a * (((y+yoff) << 6) + s) + (1 << 11)) >> 12);
if (dx < 0 || dx >= dp->width || dy < 0 || dy >= dp->height)
continue;
dst = (uint32_t *)(dp->pixels + dy * dp->pitch + dx * dp->bytespp);
}
else {
int dx = sx + x+xoff;
int dy = sy + y+yoff;
if (dx < 0 || dx >= dp->width || dy < 0 || dy >= dp->height)
continue;
}
/* write destination pixel */
if (word & bitmask)
*dst = fgpixel;
else if (drawbg)
*dst = bgpixel;
} while(rotangle && (s += oversamp) < oversamp+1);
dst++;
word <<= 1;
--bitcount;
if (++x == zerox) { /* start drawing extra background bits? */
word = 0;
bitcount = 9999;
continue;
}
if (x == maxx) { /* finished with bitmap row? */
x = minx;
++y;
bitcount = 0;
dst = (Pixel *)((uint8_t *)dst + dspan);
--height;
}
} while (height > 0);
return w;
}
/* draw a character from an antialiasing font, drawbg=2 means fill bg to max width */
int draw_font_alpha(Drawable *dp, Font *font, int c, int sx, int sy, int xoff, int yoff,
Pixel fgpixel, Pixel bgpixel, int drawbg, int rotangle)
{
int x, y, minx, maxx, w, zerox, dspan;
int height = font->height;
Pixel *dst;
Varptr bits;
int sin_a, cos_a, s; /* for rotated bitmaps */
c = glyph_offset(font, c);
/* get glyph alpha bytes */
if (font->offset.ptr8) {
switch (font->offset_width) {
case 1:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr8[c];
break;
case 2:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr16[c];
break;
case 4:
default:
bits.ptr8 = font->bits.ptr8 + font->offset.ptr32[c];
break;
}
} else bits.ptr8 = font->bits.ptr8 + c * font->bits_width * font->height;
if (rotangle) {
sin_a = fast_sin(rotangle);
cos_a = fast_cos(rotangle);
}
x = y = 0;
minx = x;
w = font->width? font->width[c]: font->maxwidth;
/* draw max font width background if proportional font and console output */
if (drawbg == 2 && w != font->maxwidth) {
zerox = minx + w;
maxx = zerox + (font->maxwidth - w);
dspan = dp->pitch - ((maxx - minx) * dp->bytespp);
} else {
maxx = minx + w;
zerox = 9999;
dspan = dp->pitch - (w * dp->bytespp);
}
dst = (Pixel *)(dp->pixels + (sy+yoff) * dp->pitch + (sx+xoff) * dp->bytespp);
do {
s = 0;
Alpha sa = (x < zerox)? *bits.ptr8++: 0;
do {
if (rotangle) {
int dx = sx + ((cos_a * (((x+xoff) << 6) + s)
- sin_a * (((y+yoff) << 6) + s) + (1 << 11)) >> 12);
int dy = sy + ((sin_a * (((x+xoff) << 6) + s)
+ cos_a * (((y+yoff) << 6) + s) + (1 << 11)) >> 12);
if (dx < 0 || dx >= dp->width || dy < 0 || dy >= dp->height)
continue;
dst = (uint32_t *)(dp->pixels + dy * dp->pitch + dx * dp->bytespp);
if (!s && sa == 255) sa = 192; /* experimental oversampled blend */
}
else {
int dx = sx + x+xoff;
int dy = sy + y+yoff;
if (dx < 0 || dx >= dp->width || dy < 0 || dy >= dp->height)
continue;
}
/* blend src alpha with destination */
if (sa == 0xff) {
*dst = fgpixel;
} else {
if (drawbg) *dst = bgpixel;
if (sa != 0) {
Pixel srb = ((sa * (fgpixel & 0xff00ff)) >> 8) & 0xff00ff;
Pixel sg = ((sa * (fgpixel & 0x00ff00)) >> 8) & 0x00ff00;
Pixel da = 0xff - sa;
Pixel drb = *dst;
Pixel dg = drb & 0x00ff00;
drb = drb & 0xff00ff;
drb = ((drb * da >> 8) & 0xff00ff) + srb;
dg = ((dg * da >> 8) & 0x00ff00) + sg;
*dst = drb + dg;
}
}
} while(rotangle && (s += oversamp) < oversamp+1);
dst++;
if (++x == zerox)
continue;
if (x == maxx) { /* finished with bitmap row? */
x = minx;
++y;
dst = (Pixel *)((uint8_t *)dst + dspan);
--height;
}
} while (height > 0);
return w;
}
int draw_font_char(Drawable *dp, Font *font, int c, int x, int y, int xoff, int yoff,
Pixel fg, Pixel bg, int drawbg, int rotangle)
{
if (font->bpp == 8)
return draw_font_alpha(dp, font, c, x, y, xoff, yoff, fg, bg, drawbg, rotangle);
return draw_font_bitmap(dp, font, c, x, y, xoff, yoff, fg, bg, drawbg, rotangle);
}
int draw_font_string(Drawable *dp, Font *font, char *text, int x, int y,
int xoff, int yoff, Pixel fg, Pixel bg, int drawbg, int rotangle)
{
int c, adv, xstart = xoff;
while (*text) {
c = *text++;
adv = draw_font_char(dp, font, c, x, y, xoff, yoff, fg, bg, drawbg, rotangle);
xoff += adv;
}
return xoff - xstart;
}
#define ARRAYLEN(a) (sizeof(a)/sizeof(a[0]))
extern Font font_rom_8x16_1;
extern Font font_unifont_8x16_1;
extern Font font_mssans_11x13_8;
extern Font font_cour_11x19_8;
extern Font font_cour_20x37_1;
extern Font font_cour_21x37_8;
extern Font font_times_30x37_8;
static Font *fonts[] = { /* first font is default font */
#ifdef NOMAIN
&font_rom_8x16_1,
#else
&font_unifont_8x16_1,
&font_rom_8x16_1,
&font_mssans_11x13_8,
&font_cour_11x19_8,
&font_cour_20x37_1,
&font_cour_21x37_8,
&font_times_30x37_8,
#endif
};
Font *font_load_internal_font(char *name)
{
int i;
for (i = 0; i < ARRAYLEN(fonts); i++) {
if (!strcmp(fonts[i]->name, name))
return fonts[i];
}
return NULL;
}
/*
* load console font, works for:
* *.Fnn ROM font files e.g. VGA-ROM.F16, DOSJ-437.F19
*/
Font *font_load_disk_font(char *path)
{
int width = 8;
int height = 16;
int fd, size;
char *s;
Font *font;
s = strrchr(path, '.');
if (s && s[1] == 'F')
height = atoi(s+2);
size = height * 256;
if (width > 8) size *= 2;
fd = open(path, O_RDONLY);
if (fd < 0)
return 0;
printf("Loading %s %dx%d size %d\n", path, width, height, size);
font = malloc(sizeof(Font)+size);
memset(font, 0, sizeof(Font));
if (read(fd, font->data, size) != size) {
printf("read error");
return 0;
}
close(fd);
font->maxwidth = width;
font->height = height;
font->size = 256;
font->bits.ptr8 = font->data;
font->bits_width = (width > 8)? 2: 1;
return font;
}
/* try loading internal, then disk font. fail returns NULL */
Font *font_load_font(char *path)
{
Font *font = NULL;
char fontdir[80];
if (path) font = font_load_internal_font(path);
if (!font) {
if (path) {
font = font_load_disk_font(path);
if (!font) {
strcpy(fontdir, "fonts/");
strcat(fontdir, path);
font = font_load_disk_font(fontdir);
}
}
}
if (!font) return NULL;
/* older Microwindows MWCFONT struct compatibility */
if (font->bpp == 0) font->bpp = 1; /* mwin default 1 bpp */
if (font->bits_width == 0) font->bits_width = 2; /* mwin default 16 bits */
if (font->offset_width == 0) font->offset_width = 4; /* mwin default 32 bits */
return font;
}
Font *console_load_font(struct console *con, char *path)
{
Font *font = font_load_font(path);
if (!font) {
if (path)
printf("Can't find font '%s', using default %s\n", path, fonts[0]->name);
font = fonts[0];
}
con->font = font;
con->char_height = font->height;
con->char_width = font->maxwidth;
return font;
}