-
Notifications
You must be signed in to change notification settings - Fork 23
/
gtktsm-terminal.c
2023 lines (1731 loc) · 46.2 KB
/
gtktsm-terminal.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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GtkTsm - Terminal Emulator
*
* Copyright (c) 2011-2014 David Herrmann <dh.herrmann@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define G_LOG_DOMAIN "GtkTsm"
#include <cairo.h>
#include <errno.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <libtsm.h>
#include <math.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <xkbcommon/xkbcommon.h>
#include "gtktsm-terminal.h"
#include "shl-htable.h"
#include "shl-llog.h"
#include "shl-macro.h"
#include "shl-pty.h"
/*
* Glyph Renderer
* With terminal-emulators, we have the problem that our grid is fixed.
* Existing text-renderers want to apply kerning and other heuristics to
* render fonts properly. We cannot do that. Therefore, we render each glyph
* separately and provide them as single-glyph objects to the upper layers.
*
* We also do some heuristics to calculate the real font-metrics. Most fonts
* are not mono-space, so they don't provide any generic metrics. We therefore
* render the ASCII glyphs and some more one-column glyphs to get a proper
* global metric for the font.
*/
struct gtktsm_font {
unsigned long ref;
PangoFontMap *map;
};
struct gtktsm_face {
unsigned long ref;
struct gtktsm_font *font;
PangoContext *ctx;
cairo_antialias_t aa;
cairo_subpixel_order_t subpixel;
struct shl_htable glyphs;
unsigned int width;
unsigned int height;
unsigned int baseline;
unsigned int line_thickness;
unsigned int underline_pos;
unsigned int strikethrough_pos;
};
enum gtktsm_glyph_format {
GTKTSM_GLYPH_INVALID,
GTKTSM_GLYPH_A1,
GTKTSM_GLYPH_A8,
GTKTSM_GLYPH_XRGB32,
};
struct gtktsm_glyph {
unsigned long id;
unsigned int cwidth;
unsigned int format;
unsigned int width;
int stride;
unsigned int height;
uint8_t *buffer;
cairo_surface_t *surface;
};
#define gtktsm_glyph_from_id(_id) \
shl_htable_offsetof((_id), struct gtktsm_glyph, id)
static void gtktsm_face_free(struct gtktsm_face *face);
static void gtktsm_glyph_free(struct gtktsm_glyph *glyph);
static int gtktsm_font_new(struct gtktsm_font **out)
{
_shl_free_ struct gtktsm_font *font = NULL;
if (!out)
return -EINVAL;
font = calloc(1, sizeof(*font));
if (!font)
return -ENOMEM;
font->ref = 1;
font->map = pango_cairo_font_map_get_default();
if (font->map) {
g_object_ref(font->map);
} else {
font->map = pango_cairo_font_map_new();
if (!font->map)
return -ENOMEM;
}
*out = font;
font = NULL;
return 0;
}
static void gtktsm_font_ref(struct gtktsm_font *font)
{
if (!font || !font->ref)
return;
++font->ref;
}
static void gtktsm_font_unref(struct gtktsm_font *font)
{
if (!font || !font->ref || --font->ref)
return;
g_object_unref(font->map);
free(font);
}
static void init_pango_desc(PangoFontDescription *desc,
int desc_size,
int desc_bold,
int desc_italic)
{
PangoFontMask mask;
int v;
if (desc_size >= 0) {
v = desc_size * PANGO_SCALE;
if (desc_size > 0 && v > 0)
pango_font_description_set_absolute_size(desc, v);
}
if (desc_bold >= 0) {
v = desc_bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL;
pango_font_description_set_weight(desc, v);
}
if (desc_italic >= 0) {
v = desc_italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
pango_font_description_set_style(desc, v);
}
pango_font_description_set_variant(desc, PANGO_VARIANT_NORMAL);
pango_font_description_set_stretch(desc, PANGO_STRETCH_NORMAL);
pango_font_description_set_gravity(desc, PANGO_GRAVITY_SOUTH);
mask = pango_font_description_get_set_fields(desc);
if (!(mask & PANGO_FONT_MASK_FAMILY))
pango_font_description_set_family(desc, "monospace");
if (!(mask & PANGO_FONT_MASK_WEIGHT))
pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
if (!(mask & PANGO_FONT_MASK_STYLE))
pango_font_description_set_style(desc, PANGO_STYLE_NORMAL);
if (!(mask & PANGO_FONT_MASK_SIZE))
pango_font_description_set_size(desc, 10 * PANGO_SCALE);
}
static void measure_pango(struct gtktsm_face *face)
{
static const char str[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"@!\"$%&/()=?\\}][{°^~+*#'<>|-_.:,;`´";
static const size_t str_len = sizeof(str) - 1;
PangoLayout *layout;
PangoRectangle rec;
unsigned int thick, upos, spos;
/*
* There is no way to check whether a font is a monospace font.
* Moreover, there is no "monospace extents" field of fonts that we can
* use to calculate a suitable cell size. Any bounding-boxes provided
* by the fonts are mostly useless for cell-size computations.
* Therefore, we simply render a bunch of ASCII characters and compute
* the cell-size from these. If you passed a monospace font, it will
* work out greatly. If you passed some other font, you will get a
* suitable tradeoff (well, don't do that..).
*/
layout = pango_layout_new(face->ctx);
pango_layout_set_height(layout, 0);
pango_layout_set_spacing(layout, 0);
pango_layout_set_text(layout, str, str_len);
pango_layout_get_pixel_extents(layout, NULL, &rec);
/* We use an example layout to render a bunch of ASCII characters in a
* single line. The height and baseline of the resulting extents can be
* copied unchanged into the face. For the width we calculate the
* average (rounding up). */
face->width = (rec.width + (str_len - 1)) / str_len;
face->height = rec.height;
face->baseline = PANGO_PIXELS_CEIL(pango_layout_get_baseline(layout));
/* heuristics to calculate underline/strikethrough positions */
thick = shl_min((face->height - face->baseline) / 2, face->height / 14);
thick = shl_max(thick, 1U);
upos = shl_min(face->baseline + thick, face->height - thick);
spos = face->baseline - face->height / 4;
face->line_thickness = thick;
face->underline_pos = upos;
face->strikethrough_pos = spos;
g_object_unref(layout);
}
static int init_pango(struct gtktsm_face *face,
const char *desc_str,
int desc_size,
int desc_bold,
int desc_italic,
cairo_antialias_t aa,
cairo_subpixel_order_t subpixel)
{
PangoFontDescription *desc;
cairo_font_options_t *options;
/* set context options */
pango_context_set_base_dir(face->ctx, PANGO_DIRECTION_LTR);
pango_context_set_language(face->ctx, pango_language_get_default());
/* set font description */
desc = pango_font_description_from_string(desc_str);
init_pango_desc(desc, desc_size, desc_bold, desc_italic);
pango_context_set_font_description(face->ctx, desc);
pango_font_description_free(desc);
/* set anti-aliasing */
options = cairo_font_options_create();
if (cairo_font_options_status(options) != CAIRO_STATUS_SUCCESS)
return -ENOMEM;
cairo_font_options_set_antialias(options, aa);
cairo_font_options_set_subpixel_order(options, subpixel);
pango_cairo_context_set_font_options(face->ctx, options);
cairo_font_options_destroy(options);
/* measure font */
measure_pango(face);
if (!face->width || !face->height)
return -EINVAL;
return 0;
}
static int gtktsm_face_new(struct gtktsm_face **out,
struct gtktsm_font *font,
const char *desc_str,
int desc_size,
int desc_bold,
int desc_italic,
cairo_antialias_t aa,
cairo_subpixel_order_t subpixel)
{
struct gtktsm_face *face;
int r;
if (!out || !font || !desc_str)
return -EINVAL;
face = calloc(1, sizeof(*face));
if (!face)
return -ENOMEM;
face->font = font;
gtktsm_font_ref(face->font);
shl_htable_init_ulong(&face->glyphs);
face->ctx = pango_font_map_create_context(font->map);
face->aa = aa;
face->subpixel = subpixel;
r = init_pango(face,
desc_str,
desc_size,
desc_bold,
desc_italic,
aa,
subpixel);
if (r < 0)
goto error;
*out = face;
return 0;
error:
gtktsm_face_free(face);
return r;
}
static void free_glyph(unsigned long *elem, void *ctx)
{
gtktsm_glyph_free(gtktsm_glyph_from_id(elem));
}
static void gtktsm_face_free(struct gtktsm_face *face)
{
if (!face)
return;
if (face->ctx)
g_object_unref(face->ctx);
shl_htable_clear_ulong(&face->glyphs, free_glyph, NULL);
gtktsm_font_unref(face->font);
free(face);
}
static unsigned int c2f(cairo_format_t format)
{
switch (format) {
case CAIRO_FORMAT_A1:
return GTKTSM_GLYPH_A1;
case CAIRO_FORMAT_A8:
return GTKTSM_GLYPH_A8;
case CAIRO_FORMAT_RGB24:
return GTKTSM_GLYPH_XRGB32;
default:
return GTKTSM_GLYPH_INVALID;
}
}
static int create_glyph(struct gtktsm_face *face,
struct gtktsm_glyph *glyph,
const uint32_t *ch,
size_t len)
{
PangoLayoutLine *line;
cairo_surface_t *surface;
PangoRectangle rec;
cairo_format_t format;
PangoLayout *layout;
cairo_t *cr;
size_t cnt;
glong ulen;
char *val;
int r;
switch (face->aa) {
case CAIRO_ANTIALIAS_NONE:
format = CAIRO_FORMAT_A1;
break;
case CAIRO_ANTIALIAS_GRAY:
format = CAIRO_FORMAT_A8;
break;
case CAIRO_ANTIALIAS_SUBPIXEL:
/* fallthrough */
default:
format = CAIRO_FORMAT_RGB24;
break;
}
glyph->format = c2f(format);
glyph->width = face->width * glyph->cwidth;
glyph->stride = cairo_format_stride_for_width(format, glyph->width);
glyph->height = face->height;
glyph->buffer = calloc(1, glyph->stride * glyph->height);
if (!glyph->buffer)
return -ENOMEM;
surface = cairo_image_surface_create_for_data(glyph->buffer,
format,
glyph->width,
glyph->height,
glyph->stride);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
r = -ENOMEM;
goto err_buffer;
}
cr = cairo_create(surface);
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
r = -ENOMEM;
goto err_surface;
}
pango_cairo_update_context(cr, face->ctx);
layout = pango_layout_new(face->ctx);
val = g_ucs4_to_utf8(ch, len, NULL, &ulen, NULL);
if (!val) {
r = -ERANGE;
goto err_layout;
}
/* render one line only */
pango_layout_set_height(layout, 0);
/* no line spacing */
pango_layout_set_spacing(layout, 0);
/* set text to char [+combining-chars] */
pango_layout_set_text(layout, val, ulen);
g_free(val);
cnt = pango_layout_get_line_count(layout);
if (cnt == 0) {
r = -ERANGE;
goto err_layout;
}
line = pango_layout_get_line_readonly(layout, 0);
pango_layout_line_get_pixel_extents(line, NULL, &rec);
cairo_move_to(cr, -rec.x, face->baseline),
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
pango_cairo_show_layout_line(cr, line);
g_object_unref(layout);
cairo_destroy(cr);
glyph->surface = surface;
return 0;
err_layout:
g_object_unref(layout);
cairo_destroy(cr);
err_surface:
cairo_surface_destroy(surface);
err_buffer:
free(glyph->buffer);
glyph->buffer = NULL;
return r;
}
static int gtktsm_face_render(struct gtktsm_face *face,
struct gtktsm_glyph **out,
unsigned long id,
const uint32_t *ch,
size_t len,
size_t cwidth)
{
struct gtktsm_glyph *glyph;
unsigned long *gid;
bool b;
int r;
if (!face || !out)
return -EINVAL;
b = shl_htable_lookup_ulong(&face->glyphs, id, &gid);
if (b) {
*out = gtktsm_glyph_from_id(gid);
return 0;
}
if (!len || !ch || !cwidth)
return -EINVAL;
glyph = calloc(1, sizeof(*glyph));
if (!glyph)
return -ENOMEM;
glyph->id = id;
glyph->cwidth = cwidth;
r = create_glyph(face, glyph, ch, len);
if (r < 0)
goto error;
r = shl_htable_insert_ulong(&face->glyphs, &glyph->id);
if (r < 0)
goto error;
*out = glyph;
return 0;
error:
gtktsm_glyph_free(glyph);
return r;
}
static void gtktsm_glyph_free(struct gtktsm_glyph *glyph)
{
if (glyph->surface)
cairo_surface_destroy(glyph->surface);
free(glyph->buffer);
free(glyph);
}
/*
* Cell Renderer
* Gtk uses cairo for rendering. Unfortunately, cairo isn't very suitable for
* terminal/cell rendering. Rendering each glyph separately causes like 10
* function calls per cell. Therefore, we render our terminal into a shadow
* buffer and only tell cairo to blit it onto the widget-buffer.
*/
struct gtktsm_renderer {
unsigned int width;
unsigned int height;
int stride;
uint8_t *data;
cairo_surface_t *surface;
tsm_age_t age;
};
struct gtktsm_renderer_ctx {
struct gtktsm_renderer *rend;
cairo_t *cr;
struct tsm_screen *screen;
struct tsm_vte *vte;
struct gtktsm_face *face_regular;
struct gtktsm_face *face_bold;
struct gtktsm_face *face_italic;
struct gtktsm_face *face_bold_italic;
unsigned int cell_width;
unsigned int cell_height;
bool debug;
};
static int renderer_realloc(struct gtktsm_renderer *rend,
unsigned int width,
unsigned int height)
{
int stride;
uint8_t *data;
cairo_surface_t *surface;
if (!width)
width = 1;
if (!height)
height = 1;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
data = malloc(abs(stride * height));
if (!data)
return -ENOMEM;
surface = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_ARGB32,
width,
height,
stride);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(surface);
free(data);
return -ENOMEM;
}
if (rend->data) {
cairo_surface_destroy(rend->surface);
free(rend->data);
}
rend->width = width;
rend->height = height;
rend->stride = stride;
rend->data = data;
rend->surface = surface;
rend->age = 0;
return 0;
}
static int gtktsm_renderer_new(struct gtktsm_renderer **out,
unsigned int width,
unsigned int height)
{
struct gtktsm_renderer *rend;
int r;
if (!out)
return -EINVAL;
rend = calloc(1, sizeof(*rend));
if (!rend)
return -ENOMEM;
r = renderer_realloc(rend, width, height);
if (r < 0)
goto err_rend;
*out = rend;
return 0;
err_rend:
free(rend);
return r;
}
static void gtktsm_renderer_free(struct gtktsm_renderer *rend)
{
if (!rend)
return;
cairo_surface_destroy(rend->surface);
free(rend->data);
free(rend);
}
static int gtktsm_renderer_resize(struct gtktsm_renderer *rend,
unsigned int width,
unsigned int height)
{
if (!rend)
return -EINVAL;
return renderer_realloc(rend, width, height);
}
static void renderer_fill(struct gtktsm_renderer *rend,
unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height,
uint8_t br, uint8_t bg, uint8_t bb)
{
unsigned int i, tmp;
uint8_t *dst;
uint32_t out;
/* clip width */
tmp = x + width;
if (tmp <= x || x >= rend->width)
return;
if (tmp > rend->width)
width = rend->width - x;
/* clip height */
tmp = y + height;
if (tmp <= y || y >= rend->height)
return;
if (tmp > rend->height)
height = rend->height - y;
/* prepare */
dst = rend->data;
dst = &dst[y * rend->stride + x * 4];
out = (0xff << 24) | (br << 16) | (bg << 8) | bb;
/* fill buffer */
while (height--) {
for (i = 0; i < width; ++i)
((uint32_t*)dst)[i] = out;
dst += rend->stride;
}
}
/* used for debugging; draws a border on the given rectangle */
static void renderer_highlight(struct gtktsm_renderer *rend,
unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height)
{
unsigned int i, j, tmp;
uint8_t *dst;
uint32_t out;
/* clip width */
tmp = x + width;
if (tmp <= x || x >= rend->width)
return;
if (tmp > rend->width)
width = rend->width - x;
/* clip height */
tmp = y + height;
if (tmp <= y || y >= rend->height)
return;
if (tmp > rend->height)
height = rend->height - y;
/* prepare */
dst = rend->data;
dst = &dst[y * rend->stride + x * 4];
out = (0xff << 24) | (0xd0 << 16) | (0x10 << 8) | 0x10;
/* draw outline into buffer */
for (i = 0; i < height; ++i) {
((uint32_t*)dst)[0] = out;
((uint32_t*)dst)[width - 1] = out;
if (!i || i + 1 == height) {
for (j = 0; j < width; ++j)
((uint32_t*)dst)[j] = out;
}
dst += rend->stride;
}
}
static void renderer_blend_a1(uint8_t *dst,
unsigned int dst_stride,
const uint8_t *src,
unsigned int src_stride,
unsigned int width,
unsigned int height,
uint8_t fr, uint8_t fg, uint8_t fb,
uint8_t br, uint8_t bg, uint8_t bb)
{
unsigned int i;
uint32_t out;
uint_fast32_t r, g, b;
while (height--) {
for (i = 0; i < width; ++i) {
if (src[i / 8] & (1 << (i % 8))) {
r = fr;
g = fg;
b = fb;
} else {
r = br;
g = bg;
b = bb;
}
out = (0xff << 24) | (r << 16) | (g << 8) | b;
((uint32_t*)dst)[i] = out;
}
dst += dst_stride;
src += src_stride;
}
}
static void renderer_blend_a8(uint8_t *dst,
unsigned int dst_stride,
const uint8_t *src,
unsigned int src_stride,
unsigned int width,
unsigned int height,
uint8_t fr, uint8_t fg, uint8_t fb,
uint8_t br, uint8_t bg, uint8_t bb)
{
unsigned int i;
uint32_t out;
uint_fast32_t r, g, b;
while (height--) {
for (i = 0; i < width; ++i) {
if (src[i] == 0) {
r = br;
g = bg;
b = bb;
} else if (src[i] == 255) {
r = fr;
g = fg;
b = fb;
} else {
/* Division by 255 (t /= 255) is done with:
* t += 0x80
* t = (t + (t >> 8)) >> 8
* This speeds up the computation by ~20% as
* the division is skipped. */
r = fr * src[i] + br * (255 - src[i]);
r += 0x80;
r = (r + (r >> 8)) >> 8;
g = fg * src[i] + bg * (255 - src[i]);
g += 0x80;
g = (g + (g >> 8)) >> 8;
b = fb * src[i] + bb * (255 - src[i]);
b += 0x80;
b = (b + (b >> 8)) >> 8;
}
out = (0xff << 24) | (r << 16) | (g << 8) | b;
((uint32_t*)dst)[i] = out;
}
dst += dst_stride;
src += src_stride;
}
}
static void renderer_blend_xrgb32(uint8_t *dst,
unsigned int dst_stride,
const uint8_t *src,
unsigned int src_stride,
unsigned int width,
unsigned int height,
uint8_t fr, uint8_t fg, uint8_t fb,
uint8_t br, uint8_t bg, uint8_t bb)
{
unsigned int i;
uint32_t out, mask;
uint_fast32_t r, g, b;
uint_fast8_t rm, gm, bm;
while (height--) {
for (i = 0; i < width; ++i) {
mask = *(uint32_t*)&src[i * 4];
rm = (mask & 0x00ff0000) >> 16;
gm = (mask & 0x0000ff00) >> 8;
bm = mask & 0x000000ff;
/* Division by 255 (t /= 255) is done with:
* t += 0x80
* t = (t + (t >> 8)) >> 8
* This speeds up the computation by ~20% as
* the division is skipped. */
if (rm == 0) {
r = br;
} else if (rm == 255) {
r = fr;
} else {
r = fr * rm + br * (255 - rm);
r += 0x80;
r = (r + (r >> 8)) >> 8;
}
if (gm == 0) {
g = bg;
} else if (gm == 255) {
g = fg;
} else {
g = fg * gm + bg * (255 - gm);
g += 0x80;
g = (g + (g >> 8)) >> 8;
}
if (bm == 0) {
b = bb;
} else if (bm == 255) {
b = fb;
} else {
b = fb * bm + bb * (255 - bm);
b += 0x80;
b = (b + (b >> 8)) >> 8;
}
out = (0xff << 24) | (r << 16) | (g << 8) | b;
((uint32_t*)dst)[i] = out;
}
dst += dst_stride;
src += src_stride;
}
}
static void renderer_blend(struct gtktsm_renderer *rend,
const struct gtktsm_glyph *glyph,
unsigned int x,
unsigned int y,
uint8_t fr, uint8_t fg, uint8_t fb,
uint8_t br, uint8_t bg, uint8_t bb)
{
unsigned int tmp, width, height;
const uint8_t *src;
uint8_t *dst;
/* clip width */
tmp = x + glyph->width;
if (tmp <= x || x >= rend->width)
return;
if (tmp > rend->width)
width = rend->width - x;
else
width = glyph->width;
/* clip height */
tmp = y + glyph->height;
if (tmp <= y || y >= rend->height)
return;
if (tmp > rend->height)
height = rend->height - y;
else
height = glyph->height;
/* prepare */
dst = rend->data;
dst = &dst[y * rend->stride + x * 4];
src = glyph->buffer;
switch (glyph->format) {
case GTKTSM_GLYPH_A1:
renderer_blend_a1(dst,
rend->stride,
src,
glyph->stride,
width,
height,
fr, fg, fb,
br, bg, bb);
break;
case GTKTSM_GLYPH_A8:
renderer_blend_a8(dst,
rend->stride,
src,
glyph->stride,
width,
height,
fr, fg, fb,
br, bg, bb);
break;
case GTKTSM_GLYPH_XRGB32:
renderer_blend_xrgb32(dst,
rend->stride,
src,
glyph->stride,
width,
height,
fr, fg, fb,
br, bg, bb);
break;
default:
g_error("invalid glyph format: %d", glyph->format);
break;
}
}
static int renderer_draw_cell(struct tsm_screen *screen,
uint64_t id,
const uint32_t *ch,
size_t len,
unsigned int cwidth,
unsigned int posx,
unsigned int posy,
const struct tsm_screen_attr *attr,
tsm_age_t age,
void *data)
{
const struct gtktsm_renderer_ctx *ctx = data;
struct gtktsm_renderer *rend = ctx->rend;
struct gtktsm_face *face;
uint8_t fr, fg, fb, br, bg, bb;
unsigned int x, y;
struct gtktsm_glyph *glyph;
bool skip;
int r;
/* Skip if our age and the cell age is non-zero *and* the cell-age is
* smaller than our age. */
skip = age && rend->age && age <= rend->age;
if (skip && !ctx->debug)
return 0;
x = posx * ctx->cell_width;
y = posy * ctx->cell_height;
/* invert colors if requested */
if (attr->inverse) {
fr = attr->br;
fg = attr->bg;
fb = attr->bb;
br = attr->fr;
bg = attr->fg;
bb = attr->fb;
} else {
fr = attr->fr;
fg = attr->fg;
fb = attr->fb;
br = attr->br;
bg = attr->bg;
bb = attr->bb;
}
/* select correct font */
if (attr->bold && ctx->face_bold)
face = ctx->face_bold;
else if (attr->italic && ctx->face_italic)
face = ctx->face_italic;
else
face = ctx->face_regular;
/* !len means background-only */
if (!len) {
renderer_fill(rend,
x,
y,
ctx->cell_width * cwidth,
ctx->cell_height,