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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <cairo.h>
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
#include "vt.h"
#include "fbtools.h"
#include "fb-gui.h"
static int ys = 3;
static int xs = 10;
/* ---------------------------------------------------------------------- */
/* shadow framebuffer -- internals */
static int32_t s_lut_transp[256], s_lut_red[256], s_lut_green[256], s_lut_blue[256];
static unsigned char **shadow;
static unsigned int *sdirty,swidth,sheight;
static cairo_t *context;
static cairo_surface_t *surface;
static unsigned char *framebuffer;
static void shadow_lut_init_one(int32_t *lut, int bits, int shift)
{
int i;
if (bits > 8)
for (i = 0; i < 256; i++)
lut[i] = (i << (bits + shift - 8));
else
for (i = 0; i < 256; i++)
lut[i] = (i >> (8 - bits)) << shift;
}
static void shadow_lut_init(gfxstate *gfx)
{
shadow_lut_init_one(s_lut_transp, gfx->tlen, gfx->toff);
shadow_lut_init_one(s_lut_red, gfx->rlen, gfx->roff);
shadow_lut_init_one(s_lut_green, gfx->glen, gfx->goff);
shadow_lut_init_one(s_lut_blue, gfx->blen, gfx->boff);
}
static void shadow_render_line(gfxstate *gfx, int line,
unsigned char *dest, char unsigned *buffer)
{
uint8_t *ptr = (void*)dest;
uint16_t *ptr2 = (void*)dest;
uint32_t *ptr4 = (void*)dest;
int x;
switch (gfx->bits_per_pixel) {
case 15:
case 16:
for (x = 0; x < swidth; x++) {
ptr2[x] =
s_lut_red[buffer[x*4+2]] |
s_lut_green[buffer[x*4+1]] |
s_lut_blue[buffer[x*4+0]];
}
break;
case 24:
for (x = 0; x < swidth; x++) {
ptr[3*x+2] = buffer[4*x+2];
ptr[3*x+1] = buffer[4*x+1];
ptr[3*x+0] = buffer[4*x+0];
}
break;
case 32:
for (x = 0; x < swidth; x++) {
ptr4[x] = s_lut_transp[255] |
s_lut_red[buffer[x*4+2]] |
s_lut_green[buffer[x*4+1]] |
s_lut_blue[buffer[x*4+0]];
}
break;
}
}
/* ---------------------------------------------------------------------- */
/* shadow framebuffer -- management interface */
void shadow_render(gfxstate *gfx)
{
unsigned int offset = 0;
int i;
if (!console_visible)
return;
for (i = 0; i < sheight; i++, offset += gfx->stride) {
if (0 == sdirty[i])
continue;
shadow_render_line(gfx, i, gfx->mem + offset, shadow[i]);
sdirty[i] = 0;
}
if (gfx->flush_display)
gfx->flush_display(false);
}
void shadow_clear_lines(int first, int last)
{
int i;
for (i = first; i <= last; i++) {
memset(shadow[i],0,4*swidth);
sdirty[i]++;
}
}
void shadow_clear(void)
{
shadow_clear_lines(0, sheight-1);
}
void shadow_set_dirty_range(int y, int h)
{
int i;
for (i = y; i < y + h; i++)
sdirty[i]++;
}
void shadow_set_dirty(void)
{
shadow_set_dirty_range(0, sheight);
}
void shadow_init(gfxstate *gfx)
{
int i;
/* init shadow fb */
swidth = gfx->hdisplay;
sheight = gfx->vdisplay;
shadow = malloc(sizeof(unsigned char*) * sheight);
sdirty = malloc(sizeof(unsigned int) * sheight);
memset(sdirty,0, sizeof(unsigned int) * sheight);
framebuffer = malloc(swidth*sheight*4);
for (i = 0; i < sheight; i++)
shadow[i] = framebuffer + i*swidth*4;
shadow_clear();
surface = cairo_image_surface_create_for_data(framebuffer,
CAIRO_FORMAT_RGB24,
swidth, sheight,
swidth * 4);
context = cairo_create(surface);
/* init rendering */
switch (gfx->bits_per_pixel) {
case 15:
case 16:
case 24:
case 32:
shadow_lut_init(gfx);
break;
default:
fprintf(stderr, "Oops: %i bit/pixel ???\n",
gfx->bits_per_pixel);
exit(1);
}
}
void shadow_fini(void)
{
if (!shadow)
return;
free(shadow);
free(sdirty);
free(framebuffer);
}
/* ---------------------------------------------------------------------- */
/* shadow framebuffer -- drawing interface */
void shadow_draw_line(int x1, int x2, int y1,int y2)
{
cairo_set_source_rgb(context, 1, 1, 1);
cairo_set_line_width(context, 1);
cairo_move_to(context, x1 + 0.5, y1 + 0.5);
cairo_line_to(context, x2 + 0.5, y2 + 0.5);
cairo_stroke(context);
shadow_set_dirty_range(y1, (y2 - y1) + 1);
}
void shadow_draw_rect(int x1, int x2, int y1, int y2)
{
cairo_set_source_rgb(context, 1, 1, 1);
cairo_set_line_width(context, 1);
cairo_move_to(context, x1 + 0.5, y1 + 0.5);
cairo_line_to(context, x2 + 0.5, y1 + 0.5);
cairo_line_to(context, x2 + 0.5, y2 + 0.5);
cairo_line_to(context, x1 + 0.5, y2 + 0.5);
cairo_line_to(context, x1 + 0.5, y1 + 0.5);
cairo_stroke(context);
shadow_set_dirty_range(y1, (y2 - y1) + 1);
}
void shadow_draw_rgbdata(int x, int y, int pixels, unsigned char *rgb)
{
unsigned char *dest = shadow[y] + 4*x;
int i;
for (i = 0; i < pixels; i++) {
dest[0] = rgb[2];
dest[1] = rgb[1];
dest[2] = rgb[0];
dest += 4;
rgb += 3;
}
sdirty[y]++;
}
void shadow_merge_rgbdata(int x, int y, int pixels, int weight,
unsigned char *rgb)
{
unsigned char *dest = shadow[y] + 4*x;
int i;
weight = weight * 256 / 100;
for (i = 0; i < pixels; i++) {
dest[0] += rgb[2] * weight >> 8;
dest[1] += rgb[1] * weight >> 8;
dest[2] += rgb[0] * weight >> 8;
dest += 4;
rgb += 3;
}
sdirty[y]++;
}
void shadow_darkify(int x1, int x2, int y1,int y2, int percent)
{
cairo_rectangle(context, x1, y1,
x2 - x1 + 1,
y2 - y1 + 1);
cairo_set_source_rgba(context, 0, 0, 0, percent * 0.01);
cairo_fill(context);
}
void shadow_reverse(int x1, int x2, int y1,int y2)
{
unsigned char *ptr;
int x,y,h;
if (x2 < x1)
h = x2, x2 = x1, x1 = h;
if (y2 < y1)
h = y2, y2 = y1, y1 = h;
if (x1 < 0)
x1 = 0;
if (x2 >= swidth)
x2 = swidth;
if (y1 < 0)
y1 = 0;
if (y2 >= sheight)
y2 = sheight;
for (y = y1; y <= y2; y++) {
sdirty[y]++;
ptr = shadow[y];
for (x = x1; x <= x2; x++) {
ptr[4*x+0] = 255-ptr[4*x+0];
ptr[4*x+1] = 255-ptr[4*x+1];
ptr[4*x+2] = 255-ptr[4*x+2];
}
}
}
/* ---------------------------------------------------------------------- */
/* shadow framebuffer -- text rendering */
static void shadow_draw_glyph(FT_Bitmap *bitmap, int sx, int sy)
{
unsigned char *src,*dst;
unsigned int bit;
int x,y;
src = bitmap->buffer;
for (y = 0; y < bitmap->rows; y++, src += bitmap->pitch) {
if (sy+y < 0)
continue;
if (sy+y >= sheight)
continue;
sdirty[sy+y]++;
dst = shadow[sy+y] + sx*4;
switch (bitmap->pixel_mode) {
case FT_PIXEL_MODE_MONO:
for (x = 0; x < bitmap->width; x++, dst += 4) {
if (sx+x < 0)
continue;
if (sx+x >= swidth)
continue;
bit = (1 << (7-(x&7)));
if (bit & (src[x >> 3])) {
dst[0] = 255;
dst[1] = 255;
dst[2] = 255;
}
}
break;
case FT_PIXEL_MODE_GRAY:
for (x = 0; x < bitmap->width; x++, dst += 4) {
if (sx+x < 0)
continue;
if (sx+x >= swidth)
continue;
if (src[x]) {
dst[0] += (255-dst[0]) * src[x] / 255;
dst[1] += (255-dst[1]) * src[x] / 255;
dst[2] += (255-dst[2]) * src[x] / 255;
}
}
break;
}
}
}
struct glyph {
FT_Glyph glyph;
int pos;
};
int shadow_draw_string(FT_Face face, int x, int y, wchar_t *str, int align)
{
struct glyph *glyphs;
FT_UInt gi,pgi;
FT_Vector delta,pen;
FT_Glyph image;
FT_BitmapGlyph bit;
size_t len;
int i,ng,pos;
int kerning;
len = wcslen(str);
glyphs = malloc(sizeof(*glyphs) * len);
memset(glyphs,0,sizeof(*glyphs) * len);
kerning = FT_HAS_KERNING(face);
pgi = 0;
for (ng = 0, pos = 0, i = 0; str[i] != 0; i++) {
gi = FT_Get_Char_Index(face, str[i]);
if (kerning && pgi && gi) {
FT_Get_Kerning(face,pgi,gi,FT_KERNING_DEFAULT,&delta);
pos += delta.x;
}
glyphs[ng].pos = pos;
if (0 != FT_Load_Glyph(face, gi, FT_LOAD_DEFAULT))
continue;
if (0 != FT_Get_Glyph(face->glyph, &glyphs[ng].glyph))
continue;
pos += face->glyph->advance.x;
pgi = gi;
ng++;
}
switch(align) {
case -1: /* left */
break;
case 0: /* center */
x -= pos >> 7;
break;
case 1: /* right */
x -= pos >> 6;
break;
}
pen.x = 0;
pen.y = 0;
for (i = 0; i < ng; i++) {
image = glyphs[i].glyph;
if (0 != FT_Glyph_To_Bitmap(&image,FT_RENDER_MODE_NORMAL,&pen,0))
continue;
bit = (FT_BitmapGlyph)image;
shadow_draw_glyph(&bit->bitmap,
x + bit->left + (glyphs[i].pos >> 6),
y - bit->top);
if (image != glyphs[i].glyph)
FT_Done_Glyph(image);
}
for (i = 0; i < ng; i++)
FT_Done_Glyph(glyphs[i].glyph);
free(glyphs);
return pos >> 6;
}
void shadow_draw_string_cursor(FT_Face face, int x, int y, wchar_t *str, int pos)
{
wchar_t save;
int len, left, width, y1, y2;
len = wcslen(str);
if (pos >= len) {
left = shadow_draw_string(face, x, y, str, -1);
width = shadow_draw_string(face, x+left, y, L" ", -1);
} else {
save = str[pos];
str[pos] = 0;
left = shadow_draw_string(face, x, y, str, -1);
str[pos] = save;
save = str[pos+1];
str[pos+1] = 0;
width = shadow_draw_string(face, x+left, y, str+pos, -1);
str[pos+1] = save;
shadow_draw_string(face, x+left+width, y, str+pos+1, -1);
}
y2 = y - (face->size->metrics.descender >> 6) -1;
y1 = y2 - (face->size->metrics.height >> 6) +1;
shadow_reverse(left,left+width,y1,y2);
}
void shadow_draw_text_box(FT_Face face, int x, int y, int percent, wchar_t *lines[], unsigned int count)
{
unsigned int i,len,max, x1, x2, y1, y2;
if (!console_visible)
return;
max = 0;
for (i = 0; i < count; i++) {
len = wcslen(lines[i]);
if (max < len)
max = len;
}
FT_Load_Glyph(face, FT_Get_Char_Index(face, 'x'), FT_LOAD_DEFAULT);
x1 = x;
x2 = x + max * (face->glyph->advance.x >> 6);
y1 = y;
y2 = y + count * (face->size->metrics.height >> 6);
x += xs; x2 += 2*xs;
y += ys; y2 += 2*ys;
y += (face->size->metrics.height >> 6);
y += (face->size->metrics.descender >> 6);
shadow_darkify(x1, x2, y1, y2, percent);
shadow_draw_rect(x1, x2, y1, y2);
for (i = 0; i < count; i++) {
shadow_draw_string(face, x, y, lines[i], -1);
y += (face->size->metrics.height >> 6);
}
}
/* ---------------------------------------------------------------------- */
/* fontconfig + freetype font rendering */
static FT_Library freetype;
void font_init(void)
{
int rc;
FcInit();
rc = FT_Init_FreeType(&freetype);
if (rc) {
fprintf(stderr,"FT_Init_FreeType() failed\n");
exit(1);
}
}
FT_Face font_open(char *fcname)
{
FcResult result = 0;
FT_Face face = NULL;
FcPattern *pattern,*match;
char *fontname,*h;
FcChar8 *filename;
double pixelsize;
int rc;
/* parse + match font name */
pattern = FcNameParse(fcname);
FcConfigSubstitute(NULL, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
match = FcFontMatch (0, pattern, &result);
FcPatternDestroy(pattern);
if (FcResultMatch != result)
return NULL;
fontname = FcNameUnparse(match);
h = strchr(fontname, ':');
if (h)
*h = 0;
/* try get the face directly */
result = FcPatternGetFTFace(match, FC_FT_FACE, 0, &face);
if (FcResultMatch == result) {
fprintf(stderr,"using \"%s\", face=%p\n",fontname,face);
return face;
}
/* failing that use the filename */
result = FcPatternGetString (match, FC_FILE, 0, &filename);
if (FcResultMatch == result) {
result = FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixelsize);
if (FcResultMatch != result)
pixelsize = 16;
fprintf(stderr,"using \"%s\", pixelsize=%.2lf file=%s\n",
fontname,pixelsize,filename);
rc = FT_New_Face (freetype, filename, 0, &face);
if (rc)
return NULL;
FT_Set_Pixel_Sizes(face, 0, (int)pixelsize);
return face;
}
/* oops, didn't work */
return NULL;
}
|