-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.c
4987 lines (4122 loc) · 165 KB
/
render.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
#include "render.h"
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <pthread.h>
#include "macros.h"
#if HAS_INCLUDE(<pthread_np.h>)
#include <pthread_np.h>
#define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)
#elif defined(__NetBSD__)
#define pthread_setname_np(thread, name) pthread_setname_np(thread, "%s", (void *)name)
#endif
#include <wayland-cursor.h>
#include <xdg-shell.h>
#include <presentation-time.h>
#include <fcft/fcft.h>
#define LOG_MODULE "render"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "box-drawing.h"
#include "char32.h"
#include "config.h"
#include "cursor-shape.h"
#include "grid.h"
#include "hsl.h"
#include "ime.h"
#include "quirks.h"
#include "search.h"
#include "selection.h"
#include "shm.h"
#include "sixel.h"
#include "url-mode.h"
#include "util.h"
#include "xmalloc.h"
#define TIME_SCROLL_DAMAGE 0
struct renderer {
struct fdm *fdm;
struct wayland *wayl;
};
static struct {
size_t total;
size_t zero; /* commits presented in less than one frame interval */
size_t one; /* commits presented in one frame interval */
size_t two; /* commits presented in two or more frame intervals */
} presentation_statistics = {0};
static void fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data);
struct renderer *
render_init(struct fdm *fdm, struct wayland *wayl)
{
struct renderer *renderer = malloc(sizeof(*renderer));
if (unlikely(renderer == NULL)) {
LOG_ERRNO("malloc() failed");
return NULL;
}
*renderer = (struct renderer) {
.fdm = fdm,
.wayl = wayl,
};
if (!fdm_hook_add(fdm, &fdm_hook_refresh_pending_terminals, renderer,
FDM_HOOK_PRIORITY_NORMAL))
{
LOG_ERR("failed to register FDM hook");
free(renderer);
return NULL;
}
return renderer;
}
void
render_destroy(struct renderer *renderer)
{
if (renderer == NULL)
return;
fdm_hook_del(renderer->fdm, &fdm_hook_refresh_pending_terminals,
FDM_HOOK_PRIORITY_NORMAL);
free(renderer);
}
static void DESTRUCTOR
log_presentation_statistics(void)
{
if (presentation_statistics.total == 0)
return;
const size_t total = presentation_statistics.total;
LOG_INFO("presentation statistics: zero=%f%%, one=%f%%, two=%f%%",
100. * presentation_statistics.zero / total,
100. * presentation_statistics.one / total,
100. * presentation_statistics.two / total);
}
static void
sync_output(void *data,
struct wp_presentation_feedback *wp_presentation_feedback,
struct wl_output *output)
{
}
struct presentation_context {
struct terminal *term;
struct timeval input;
struct timeval commit;
};
static void
presented(void *data,
struct wp_presentation_feedback *wp_presentation_feedback,
uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
uint32_t refresh, uint32_t seq_hi, uint32_t seq_lo, uint32_t flags)
{
struct presentation_context *ctx = data;
struct terminal *term = ctx->term;
const struct timeval *input = &ctx->input;
const struct timeval *commit = &ctx->commit;
const struct timeval presented = {
.tv_sec = (uint64_t)tv_sec_hi << 32 | tv_sec_lo,
.tv_usec = tv_nsec / 1000,
};
bool use_input = (input->tv_sec > 0 || input->tv_usec > 0) &&
timercmp(&presented, input, >);
char msg[1024];
int chars = 0;
if (use_input && timercmp(&presented, input, <))
return;
else if (timercmp(&presented, commit, <))
return;
LOG_DBG("commit: %lu s %lu µs, presented: %lu s %lu µs",
commit->tv_sec, commit->tv_usec, presented.tv_sec, presented.tv_usec);
if (use_input) {
struct timeval diff;
timersub(commit, input, &diff);
chars += snprintf(
&msg[chars], sizeof(msg) - chars,
"input - %llu µs -> ", (unsigned long long)diff.tv_usec);
}
struct timeval diff;
timersub(&presented, commit, &diff);
chars += snprintf(
&msg[chars], sizeof(msg) - chars,
"commit - %llu µs -> ", (unsigned long long)diff.tv_usec);
if (use_input) {
xassert(timercmp(&presented, input, >));
timersub(&presented, input, &diff);
} else {
xassert(timercmp(&presented, commit, >));
timersub(&presented, commit, &diff);
}
chars += snprintf(
&msg[chars], sizeof(msg) - chars,
"presented (total: %llu µs)", (unsigned long long)diff.tv_usec);
unsigned frame_count = 0;
if (tll_length(term->window->on_outputs) > 0) {
const struct monitor *mon = tll_front(term->window->on_outputs);
frame_count = (diff.tv_sec * 1000000. + diff.tv_usec) / (1000000. / mon->refresh);
}
presentation_statistics.total++;
if (frame_count >= 2)
presentation_statistics.two++;
else if (frame_count >= 1)
presentation_statistics.one++;
else
presentation_statistics.zero++;
#define _log_fmt "%s (more than %u frames)"
if (frame_count >= 2)
LOG_ERR(_log_fmt, msg, frame_count);
else if (frame_count >= 1)
LOG_WARN(_log_fmt, msg, frame_count);
else
LOG_INFO(_log_fmt, msg, frame_count);
#undef _log_fmt
wp_presentation_feedback_destroy(wp_presentation_feedback);
free(ctx);
}
static void
discarded(void *data, struct wp_presentation_feedback *wp_presentation_feedback)
{
struct presentation_context *ctx = data;
wp_presentation_feedback_destroy(wp_presentation_feedback);
free(ctx);
}
static const struct wp_presentation_feedback_listener presentation_feedback_listener = {
.sync_output = &sync_output,
.presented = &presented,
.discarded = &discarded,
};
static struct fcft_font *
attrs_to_font(const struct terminal *term, const struct attributes *attrs)
{
int idx = attrs->italic << 1 | attrs->bold;
return term->fonts[idx];
}
static inline pixman_color_t
color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha)
{
return (pixman_color_t){
.red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)) * alpha / 0xffff,
.green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)) * alpha / 0xffff,
.blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)) * alpha / 0xffff,
.alpha = alpha,
};
}
static inline pixman_color_t
color_hex_to_pixman(uint32_t color)
{
/* Count on the compiler optimizing this */
return color_hex_to_pixman_with_alpha(color, 0xffff);
}
static inline uint32_t
color_decrease_luminance(uint32_t color)
{
uint32_t alpha = color & 0xff000000;
int hue, sat, lum;
rgb_to_hsl(color, &hue, &sat, &lum);
return alpha | hsl_to_rgb(hue, sat, lum / 1.5);
}
static inline uint32_t
color_dim(const struct terminal *term, uint32_t color)
{
const struct config *conf = term->conf;
const uint8_t custom_dim = conf->colors.use_custom.dim;
if (likely(custom_dim == 0))
return color_decrease_luminance(color);
for (size_t i = 0; i < 8; i++) {
if (((custom_dim >> i) & 1) == 0)
continue;
if (term->colors.table[0 + i] == color) {
/* "Regular" color, return the corresponding "dim" */
return conf->colors.dim[i];
}
else if (term->colors.table[8 + i] == color) {
/* "Bright" color, return the corresponding "regular" */
return term->colors.table[i];
}
}
return color_decrease_luminance(color);
}
static inline uint32_t
color_brighten(const struct terminal *term, uint32_t color)
{
/*
* First try to match the color against the base 8 colors. If we
* find a match, return the corresponding bright color.
*/
if (term->conf->bold_in_bright.palette_based) {
for (size_t i = 0; i < 8; i++) {
if (term->colors.table[i] == color)
return term->colors.table[i + 8];
}
return color;
}
int hue, sat, lum;
rgb_to_hsl(color, &hue, &sat, &lum);
lum = (int)roundf(lum * term->conf->bold_in_bright.amount);
return hsl_to_rgb(hue, sat, min(lum, 100));
}
static void
draw_hollow_block(const struct terminal *term, pixman_image_t *pix,
const pixman_color_t *color, int x, int y, int cell_cols)
{
const int scale = (int)roundf(term->scale);
const int width = min(min(scale, term->cell_width), term->cell_height);
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color, 4,
(pixman_rectangle16_t []){
{x, y, cell_cols * term->cell_width, width}, /* top */
{x, y, width, term->cell_height}, /* left */
{x + cell_cols * term->cell_width - width, y, width, term->cell_height}, /* right */
{x, y + term->cell_height - width, cell_cols * term->cell_width, width}, /* bottom */
});
}
static void
draw_beam_cursor(const struct terminal *term, pixman_image_t *pix,
const struct fcft_font *font,
const pixman_color_t *color, int x, int y)
{
int baseline = y + term->font_baseline - term->fonts[0]->ascent;
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color,
1, &(pixman_rectangle16_t){
x, baseline,
term_pt_or_px_as_pixels(term, &term->conf->cursor.beam_thickness),
term->fonts[0]->ascent + term->fonts[0]->descent});
}
static int
underline_offset(const struct terminal *term, const struct fcft_font *font)
{
return term->font_baseline -
(term->conf->use_custom_underline_offset
? -term_pt_or_px_as_pixels(term, &term->conf->underline_offset)
: font->underline.position);
}
static void
draw_underline_cursor(const struct terminal *term, pixman_image_t *pix,
const struct fcft_font *font,
const pixman_color_t *color, int x, int y, int cols)
{
int thickness = term->conf->cursor.underline_thickness.px >= 0
? term_pt_or_px_as_pixels(
term, &term->conf->cursor.underline_thickness)
: font->underline.thickness;
/* Make sure the line isn't positioned below the cell */
const int y_ofs = min(underline_offset(term, font) + thickness,
term->cell_height - thickness);
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color,
1, &(pixman_rectangle16_t){
x, y + y_ofs, cols * term->cell_width, thickness});
}
static void
draw_underline(const struct terminal *term, pixman_image_t *pix,
const struct fcft_font *font,
const pixman_color_t *color, int x, int y, int cols)
{
const int thickness = term->conf->underline_thickness.px >= 0
? term_pt_or_px_as_pixels(
term, &term->conf->underline_thickness)
: font->underline.thickness;
/* Make sure the line isn't positioned below the cell */
const int y_ofs = min(underline_offset(term, font),
term->cell_height - thickness);
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color,
1, &(pixman_rectangle16_t){
x, y + y_ofs, cols * term->cell_width, thickness});
}
static void
draw_styled_underline(const struct terminal *term, pixman_image_t *pix,
const struct fcft_font *font,
const pixman_color_t *color,
enum underline_style style, int x, int y, int cols)
{
xassert(style != UNDERLINE_NONE);
if (style == UNDERLINE_SINGLE) {
draw_underline(term, pix, font, color, x, y, cols);
return;
}
const int thickness = term->conf->underline_thickness.px >= 0
? term_pt_or_px_as_pixels(
term, &term->conf->underline_thickness)
: font->underline.thickness;
int y_ofs;
/* Make sure the line isn't positioned below the cell */
switch (style) {
case UNDERLINE_DOUBLE:
case UNDERLINE_CURLY:
y_ofs = min(underline_offset(term, font),
term->cell_height - thickness * 3);
break;
case UNDERLINE_DASHED:
case UNDERLINE_DOTTED:
y_ofs = min(underline_offset(term, font),
term->cell_height - thickness);
break;
case UNDERLINE_NONE:
case UNDERLINE_SINGLE:
default:
BUG("unexpected underline style: %d", (int)style);
return;
}
const int ceil_w = cols * term->cell_width;
switch (style) {
case UNDERLINE_DOUBLE: {
const pixman_rectangle16_t rects[] = {
{x, y + y_ofs, ceil_w, thickness},
{x, y + y_ofs + thickness * 2, ceil_w, thickness}};
pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, 2, rects);
break;
}
case UNDERLINE_DASHED: {
const int ceil_w = cols * term->cell_width;
const int dash_w = ceil_w / 3 + (ceil_w % 3 > 0);
const pixman_rectangle16_t rects[] = {
{x, y + y_ofs, dash_w, thickness},
{x + dash_w * 2, y + y_ofs, dash_w, thickness},
};
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color, 2, rects);
break;
}
case UNDERLINE_DOTTED: {
/* Number of dots per cell */
int per_cell = (term->cell_width / thickness) / 2;
if (per_cell == 0)
per_cell = 1;
xassert(per_cell >= 1);
/* Spacing between dots; start with the same width as the dots
themselves, then widen them if necessary, to consume unused
pixels */
int spacing[per_cell];
for (int i = 0; i < per_cell; i++)
spacing[i] = thickness;
/* Pixels remaining at the end of the cell */
int remaining = term->cell_width - (per_cell * 2) * thickness;
/* Spread out the left-over pixels across the spacing between
the dots */
for (int i = 0; remaining > 0; i = (i + 1) % per_cell, remaining--)
spacing[i]++;
xassert(remaining <= 0);
pixman_rectangle16_t rects[per_cell];
int dot_x = x;
for (int i = 0; i < per_cell; i++) {
rects[i] = (pixman_rectangle16_t){
dot_x, y + y_ofs, thickness, thickness
};
dot_x += thickness + spacing[i];
}
pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, per_cell, rects);
break;
}
case UNDERLINE_CURLY: {
const int top = y + y_ofs;
const int bot = top + thickness * 3;
const int half_x = x + ceil_w / 2.0, full_x = x + ceil_w;
const double bt_2 = (bot - top) * (bot - top);
const double th_2 = thickness * thickness;
const double hx_2 = ceil_w * ceil_w / 4.0;
const int th = round(sqrt(th_2 + (th_2 * bt_2 / hx_2)) / 2.);
#define I(x) pixman_int_to_fixed(x)
const pixman_trapezoid_t traps[] = {
#if 0 /* characters sit within the "dips" of the curlies */
{
I(top), I(bot),
{{I(x), I(top + th)}, {I(half_x), I(bot + th)}},
{{I(x), I(top - th)}, {I(half_x), I(bot - th)}},
},
{
I(top), I(bot),
{{I(half_x), I(bot - th)}, {I(full_x), I(top - th)}},
{{I(half_x), I(bot + th)}, {I(full_x), I(top + th)}},
}
#else /* characters sit on top of the curlies */
{
I(top), I(bot),
{{I(x), I(bot - th)}, {I(half_x), I(top - th)}},
{{I(x), I(bot + th)}, {I(half_x), I(top + th)}},
},
{
I(top), I(bot),
{{I(half_x), I(top + th)}, {I(full_x), I(bot + th)}},
{{I(half_x), I(top - th)}, {I(full_x), I(bot - th)}},
}
#endif
};
pixman_image_t *fill = pixman_image_create_solid_fill(color);
pixman_composite_trapezoids(
PIXMAN_OP_OVER, fill, pix, PIXMAN_a8, 0, 0, 0, 0,
sizeof(traps) / sizeof(traps[0]), traps);
pixman_image_unref(fill);
break;
}
case UNDERLINE_NONE:
case UNDERLINE_SINGLE:
BUG("underline styles not supposed to be handled here");
break;
}
}
static void
draw_strikeout(const struct terminal *term, pixman_image_t *pix,
const struct fcft_font *font,
const pixman_color_t *color, int x, int y, int cols)
{
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, color,
1, &(pixman_rectangle16_t){
x, y + term->font_baseline - font->strikeout.position,
cols * term->cell_width, font->strikeout.thickness});
}
static void
cursor_colors_for_cell(const struct terminal *term, const struct cell *cell,
const pixman_color_t *fg, const pixman_color_t *bg,
pixman_color_t *cursor_color, pixman_color_t *text_color)
{
if (term->colors.cursor_bg >> 31)
*cursor_color = color_hex_to_pixman(term->colors.cursor_bg);
else
*cursor_color = *fg;
if (term->colors.cursor_fg >> 31)
*text_color = color_hex_to_pixman(term->colors.cursor_fg);
else {
*text_color = *bg;
if (unlikely(text_color->alpha != 0xffff)) {
/* The *only* color that can have transparency is the
* default background color */
*text_color = color_hex_to_pixman(term->colors.bg);
}
}
if (text_color->red == cursor_color->red &&
text_color->green == cursor_color->green &&
text_color->blue == cursor_color->blue)
{
*text_color = color_hex_to_pixman(term->colors.bg);
*cursor_color = color_hex_to_pixman(term->colors.fg);
}
}
static void
draw_cursor(const struct terminal *term, const struct cell *cell,
const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg,
const pixman_color_t *bg, int x, int y, int cols)
{
pixman_color_t cursor_color;
pixman_color_t text_color;
cursor_colors_for_cell(term, cell, fg, bg, &cursor_color, &text_color);
switch (term->cursor_style) {
case CURSOR_BLOCK:
if (unlikely(!term->kbd_focus)) {
switch (term->conf->cursor.unfocused_style) {
case CURSOR_UNFOCUSED_UNCHANGED:
break;
case CURSOR_UNFOCUSED_HOLLOW:
draw_hollow_block(term, pix, &cursor_color, x, y, cols);
return;
case CURSOR_UNFOCUSED_NONE:
return;
}
}
if (likely(term->cursor_blink.state == CURSOR_BLINK_ON) ||
!term->kbd_focus)
{
*fg = text_color;
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, &cursor_color, 1,
&(pixman_rectangle16_t){x, y, cols * term->cell_width, term->cell_height});
}
break;
case CURSOR_BEAM:
if (likely(term->cursor_blink.state == CURSOR_BLINK_ON ||
!term->kbd_focus))
{
draw_beam_cursor(term, pix, font, &cursor_color, x, y);
}
break;
case CURSOR_UNDERLINE:
if (likely(term->cursor_blink.state == CURSOR_BLINK_ON ||
!term->kbd_focus))
{
draw_underline_cursor(term, pix, font, &cursor_color, x, y, cols);
}
break;
}
}
static int
render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damage,
struct row *row, int row_no, int col, bool has_cursor)
{
struct cell *cell = &row->cells[col];
if (cell->attrs.clean)
return 0;
cell->attrs.clean = 1;
cell->attrs.confined = true;
int width = term->cell_width;
int height = term->cell_height;
const int x = term->margins.left + col * width;
const int y = term->margins.top + row_no * height;
bool is_selected = cell->attrs.selected;
uint32_t _fg = 0;
uint32_t _bg = 0;
uint16_t alpha = 0xffff;
if (is_selected && term->colors.use_custom_selection) {
_fg = term->colors.selection_fg;
_bg = term->colors.selection_bg;
} else {
/* Use cell specific color, if set, otherwise the default colors (possible reversed) */
switch (cell->attrs.fg_src) {
case COLOR_RGB:
_fg = cell->attrs.fg;
break;
case COLOR_BASE16:
case COLOR_BASE256:
xassert(cell->attrs.fg < ALEN(term->colors.table));
_fg = term->colors.table[cell->attrs.fg];
break;
case COLOR_DEFAULT:
_fg = term->reverse ? term->colors.bg : term->colors.fg;
break;
}
switch (cell->attrs.bg_src) {
case COLOR_RGB:
_bg = cell->attrs.bg;
break;
case COLOR_BASE16:
case COLOR_BASE256:
xassert(cell->attrs.bg < ALEN(term->colors.table));
_bg = term->colors.table[cell->attrs.bg];
break;
case COLOR_DEFAULT:
_bg = term->reverse ? term->colors.fg : term->colors.bg;
break;
}
if (cell->attrs.reverse ^ is_selected) {
uint32_t swap = _fg;
_fg = _bg;
_bg = swap;
}
else if (cell->attrs.bg_src == COLOR_DEFAULT) {
if (term->window->is_fullscreen) {
/*
* Note: disable transparency when fullscreened.
*
* This is because the wayland protocol mandates no
* screen content is shown behind the fullscreened
* window.
*
* The _intent_ of the specification is that a black
* (or other static color) should be used as
* background.
*
* There's a bit of gray area however, and some
* compositors have chosen to interpret the
* specification in a way that allows wallpapers to be
* seen through a fullscreen window.
*
* Given that a) the intent of the specification, and
* b) we don't know what the compositor will do, we
* simply disable transparency while in fullscreen.
*
* To see why, consider what happens if we keep our
* transparency. For example, if the background color
* is white, and alpha is 0.5, then the window will be
* drawn in a shade of gray while fullscreened.
*
* See
* https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/116
* for a discussion on whether transparent, fullscreen
* windows should be allowed in some way or not.
*
* NOTE: if changing this, also update render_margin()
*/
xassert(alpha == 0xffff);
} else {
alpha = term->colors.alpha;
}
}
}
if (unlikely(is_selected && _fg == _bg)) {
/* Invert bg when selected/highlighted text has same fg/bg */
_bg = ~_bg;
alpha = 0xffff;
}
if (cell->attrs.dim)
_fg = color_dim(term, _fg);
if (term->conf->bold_in_bright.enabled && cell->attrs.bold)
_fg = color_brighten(term, _fg);
if (cell->attrs.blink && term->blink.state == BLINK_OFF)
_fg = color_decrease_luminance(_fg);
pixman_color_t fg = color_hex_to_pixman(_fg);
pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha);
struct fcft_font *font = attrs_to_font(term, &cell->attrs);
const struct composed *composed = NULL;
const struct fcft_grapheme *grapheme = NULL;
const struct fcft_glyph *single = NULL;
const struct fcft_glyph **glyphs = NULL;
unsigned glyph_count = 0;
char32_t base = cell->wc;
int cell_cols = 1;
if (base != 0) {
if (unlikely(
/* Classic box drawings */
(base >= GLYPH_BOX_DRAWING_FIRST &&
base <= GLYPH_BOX_DRAWING_LAST) ||
/* Braille */
(base >= GLYPH_BRAILLE_FIRST &&
base <= GLYPH_BRAILLE_LAST) ||
/*
* Unicode 13 "Symbols for Legacy Computing"
* sub-ranges below.
*
* Note, the full range is U+1FB00 - U+1FBF9
*/
(base >= GLYPH_LEGACY_FIRST &&
base <= GLYPH_LEGACY_LAST)) &&
likely(!term->conf->box_drawings_uses_font_glyphs))
{
struct fcft_glyph ***arr;
size_t count;
size_t idx;
if (base >= GLYPH_LEGACY_FIRST) {
arr = &term->custom_glyphs.legacy;
count = GLYPH_LEGACY_COUNT;
idx = base - GLYPH_LEGACY_FIRST;
} else if (base >= GLYPH_BRAILLE_FIRST) {
arr = &term->custom_glyphs.braille;
count = GLYPH_BRAILLE_COUNT;
idx = base - GLYPH_BRAILLE_FIRST;
} else {
arr = &term->custom_glyphs.box_drawing;
count = GLYPH_BOX_DRAWING_COUNT;
idx = base - GLYPH_BOX_DRAWING_FIRST;
}
if (unlikely(*arr == NULL))
*arr = xcalloc(count, sizeof((*arr)[0]));
if (likely((*arr)[idx] != NULL))
single = (*arr)[idx];
else {
mtx_lock(&term->render.workers.lock);
/* Other thread may have instantiated it while we
* acquired the lock */
single = (*arr)[idx];
if (likely(single == NULL))
single = (*arr)[idx] = box_drawing(term, base);
mtx_unlock(&term->render.workers.lock);
}
if (single != NULL) {
glyph_count = 1;
glyphs = &single;
cell_cols = single->cols;
}
}
else if (base >= CELL_COMB_CHARS_LO && base <= CELL_COMB_CHARS_HI)
{
composed = composed_lookup(term->composed, base - CELL_COMB_CHARS_LO);
base = composed->chars[0];
if (term->conf->can_shape_grapheme && term->conf->tweak.grapheme_shaping) {
grapheme = fcft_rasterize_grapheme_utf32(
font, composed->count, composed->chars, term->font_subpixel);
}
if (grapheme != NULL) {
cell_cols = composed->width;
composed = NULL;
glyphs = grapheme->glyphs;
glyph_count = grapheme->count;
}
}
if (single == NULL && grapheme == NULL) {
if (unlikely(base >= CELL_SPACER)) {
glyph_count = 0;
cell_cols = 1;
} else {
xassert(base != 0);
single = fcft_rasterize_char_utf32(font, base, term->font_subpixel);
if (single == NULL) {
glyph_count = 0;
cell_cols = 1;
} else {
glyph_count = 1;
glyphs = &single;
cell_cols = single->cols;
}
}
}
}
assert(glyph_count == 0 || glyphs != NULL);
const int cols_left = term->cols - col;
cell_cols = max(1, min(cell_cols, cols_left));
/*
* Determine cells that will bleed into their right neighbor and remember
* them for cleanup in the next frame.
*/
int render_width = cell_cols * width;
if (term->conf->tweak.overflowing_glyphs &&
glyph_count > 0 &&
cols_left > cell_cols)
{
int glyph_width = 0, advance = 0;
for (size_t i = 0; i < glyph_count; i++) {
glyph_width = max(glyph_width,
advance + glyphs[i]->x + glyphs[i]->width);
advance += glyphs[i]->advance.x;
}
if (glyph_width > render_width) {
render_width = min(glyph_width, render_width + width);
for (int i = 0; i < cell_cols; i++)
row->cells[col + i].attrs.confined = false;
}
}
pixman_region32_t clip;
pixman_region32_init_rect(
&clip, x, y,
render_width, term->cell_height);
pixman_image_set_clip_region32(pix, &clip);
if (damage != NULL) {
pixman_region32_union_rect(
damage, damage, x, y, render_width, term->cell_height);
}
pixman_region32_fini(&clip);
/* Background */
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, &bg, 1,
&(pixman_rectangle16_t){x, y, cell_cols * width, height});
if (cell->attrs.blink && term->blink.fd < 0) {
/* TODO: use a custom lock for this? */
mtx_lock(&term->render.workers.lock);
term_arm_blink_timer(term);
mtx_unlock(&term->render.workers.lock);
}
if (unlikely(has_cursor && term->cursor_style == CURSOR_BLOCK && term->kbd_focus))
draw_cursor(term, cell, font, pix, &fg, &bg, x, y, cell_cols);
if (cell->wc == 0 || cell->wc >= CELL_SPACER || cell->wc == U'\t' ||
(unlikely(cell->attrs.conceal) && !is_selected))
{
goto draw_cursor;
}
pixman_image_t *clr_pix = pixman_image_create_solid_fill(&fg);
int pen_x = x;
for (unsigned i = 0; i < glyph_count; i++) {
const int letter_x_ofs = i == 0 ? term->font_x_ofs : 0;
const struct fcft_glyph *glyph = glyphs[i];
if (glyph == NULL)
continue;
int g_x = glyph->x;
int g_y = glyph->y;
if (i > 0 && glyph->x >= 0)
g_x -= term->cell_width;
if (unlikely(pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)) {
/* Glyph surface is a pre-rendered image (typically a color emoji...) */
if (!(cell->attrs.blink && term->blink.state == BLINK_OFF)) {
pixman_image_composite32(
PIXMAN_OP_OVER, glyph->pix, NULL, pix, 0, 0, 0, 0,
pen_x + letter_x_ofs + g_x, y + term->font_baseline - g_y,
glyph->width, glyph->height);
}
} else {
pixman_image_composite32(
PIXMAN_OP_OVER, clr_pix, glyph->pix, pix, 0, 0, 0, 0,
pen_x + letter_x_ofs + g_x, y + term->font_baseline - g_y,
glyph->width, glyph->height);
/* Combining characters */
if (composed != NULL) {
assert(glyph_count == 1);
for (size_t i = 1; i < composed->count; i++) {
const struct fcft_glyph *g = fcft_rasterize_char_utf32(
font, composed->chars[i], term->font_subpixel);
if (g == NULL)
continue;
/*
* Fonts _should_ assume the pen position is now
* *after* the base glyph, and thus use negative
* offsets for combining glyphs.
*
* Not all fonts behave like this however, and we
* try to accommodate both variants.
*
* Since we haven't moved our pen position yet, we
* add a full cell width to the offset (or two, in
* case of double-width characters).
*
* If the font does *not* use negative offsets,
* we'd normally use an offset of 0. However, to
* somewhat deal with double-width glyphs we use
* an offset of *one* cell.
*/
int x_ofs = g->x < 0
? cell_cols * term->cell_width
: (cell_cols - 1) * term->cell_width;
pixman_image_composite32(
PIXMAN_OP_OVER, clr_pix, g->pix, pix, 0, 0, 0, 0,
/* Some fonts use a negative offset, while others use a
* "normal" offset */