Repository navigation
Expand file tree
/
Copy pathfrontend-cairo.c
More file actions
2183 lines (1973 loc) · 56.9 KB
/
Copy pathfrontend-cairo.c
File metadata and controls
2183 lines (1973 loc) · 56.9 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
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 <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <cairo.h>
#include <cairo-ft.h>
#include <cairo-xcb.h>
#include "config.h"
#include "frontend.h"
#include "region.h"
#include "utf8.h"
#define GRAD_LIGHT 1.08
#define GRAD_DARK 0.92
#define FLASH_DECAY 0.88
#define FLASH_MIN 0.01
#define GLOW_LAYERS 3
#define OUTLINE_DIRS 8
#define OUTLINE_SHIFT 0.35f
#define GLYPH_DIRECT 256
#define GLYPH_CACHE_SIZE 2048
#define GLYPH_CACHE_PROBE 4
static void argb_to_rgb(Argb c, double *r, double *g, double *b)
{
*r = (double) ((c >> 16) & COLOUR_CHANNEL_MASK) / COLOUR_CHANNEL_MAX;
*g = (double) ((c >> 8) & COLOUR_CHANNEL_MASK) / COLOUR_CHANNEL_MAX;
*b = (double) (c & COLOUR_CHANNEL_MASK) / COLOUR_CHANNEL_MAX;
}
static Argb apply_sepia(Argb c)
{
double r = (c >> 16) & COLOUR_CHANNEL_MASK;
double g = (c >> 8) & COLOUR_CHANNEL_MASK;
double b = c & COLOUR_CHANNEL_MASK;
double nr = r * 0.393 + g * 0.769 + b * 0.189;
double ng = r * 0.349 + g * 0.686 + b * 0.168;
double nb = r * 0.272 + g * 0.534 + b * 0.131;
if (nr > 255) nr = 255;
if (ng > 255) ng = 255;
if (nb > 255) nb = 255;
return ((Argb)(nr + 0.5) << 16) | ((Argb)(ng + 0.5) << 8) | (Argb)(nb + 0.5);
}
static int attr_faint(Attr a)
{
return (a & ATTR_BOLD_FAINT) == ATTR_FAINT;
}
typedef struct {
Rune key;
unsigned int index;
} GlyphCacheEnt;
typedef struct {
FT_Face face;
cairo_font_face_t *cairo_face;
int pixel_size;
cairo_glyph_t *glyph_buf;
int glyph_cap;
unsigned int gdirect[GLYPH_DIRECT];
GlyphCacheEnt gcache[GLYPH_CACHE_SIZE];
} FontResources;
struct Overlay {
cairo_surface_t *surface;
cairo_t *cr;
int x, y, w, h;
ShadowParams shadow;
unsigned flags;
};
enum {
OVERLAY_HAS_SHADOW = 1 << 0,
};
struct CairoBackend {
cairo_surface_t *output;
cairo_t *cr;
const Palette *pal;
int cols, rows;
int char_width, char_height;
int char_ascent;
int border_px;
int dpi;
int win_width, win_height;
char *font_string;
FT_Library ft_lib;
double font_size;
double font_scale;
ColourCorrection colour_cc;
cairo_surface_t *backbuf;
cairo_t *backcr;
double flash_alpha;
int expected_width, expected_height;
double font_outline_alpha;
Overlay *overlays;
int noverlays, overlay_cap;
Argb bg_grad_top;
int border_gen;
unsigned border_sig;
uint8_t *border_visited;
Region *border_cache;
int border_cache_n;
Cell *drawn;
uint8_t *repaint;
int prev_sel_start_x, prev_sel_start_y;
int prev_sel_end_x, prev_sel_end_y;
double cursor_alpha;
int prev_cur_x, prev_cur_y, prev_cur_w, prev_cur_h;
cairo_pattern_t *dim_left;
cairo_pattern_t *dim_right;
double dim_cache_w;
Argb dim_cache_bell;
cairo_pattern_t *grad_cache;
Argb grad_bg;
int grad_h;
double dim_cache_alpha;
FontResources normal;
FontResources bold;
Argb cc_cache[PAL_SIZE];
unsigned cc_cache_gen;
unsigned flags;
};
typedef struct CairoBackend CairoBackend;
static void
draw_region_gradient(CairoBackend *b, cairo_t *draw, int xa, int ya,
int xb, int yb, Argb bg_c)
{
int h = yb - ya;
if (h <= 0)
return;
if (!b->grad_cache || b->grad_bg != bg_c || b->grad_h != h) {
if (b->grad_cache)
cairo_pattern_destroy(b->grad_cache);
double r0, g0, b0;
argb_to_rgb(bg_c, &r0, &g0, &b0);
cairo_pattern_t *grad = cairo_pattern_create_linear(0, 0, 0, h);
cairo_pattern_add_color_stop_rgba(grad, 0.0, r0 * GRAD_LIGHT,
g0 * GRAD_LIGHT, b0 * GRAD_LIGHT,
1.0);
cairo_pattern_add_color_stop_rgba(grad, 1.0, r0 * GRAD_DARK,
g0 * GRAD_DARK, b0 * GRAD_DARK,
1.0);
b->grad_cache = grad;
b->grad_bg = bg_c;
b->grad_h = h;
}
cairo_matrix_t m;
cairo_matrix_init_translate(&m, -xa, -ya);
cairo_pattern_set_matrix(b->grad_cache, &m);
cairo_save(draw);
cairo_rectangle(draw, xa, ya, xb - xa, h);
cairo_clip(draw);
cairo_set_source(draw, b->grad_cache);
cairo_paint(draw);
cairo_restore(draw);
}
enum {
CAIRO_FOCUS_DIM = 1 << 0,
CAIRO_SCALE_TO_FIT = 1 << 1,
CAIRO_FONT_OUTLINE = 1 << 2,
CAIRO_BORDER_BLOCKS = 1 << 3,
CAIRO_PREV_SEL_ACTIVE = 1 << 4,
CAIRO_PREV_MODE_BLINK = 1 << 5,
CAIRO_FORCE_FULL = 1 << 6,
CAIRO_WINDOW_DAMAGED = 1 << 7,
CAIRO_CC_CACHE_VALID = 1 << 8,
CAIRO_BG_VALID = 1 << 9,
CAIRO_BORDERS_MOVED = 1 << 10,
};
static FcPattern *try_font_match(const char *family, FcPattern *base_pat,
int bold, double pt_size)
{
FcPattern *pat = FcPatternDuplicate(base_pat);
FcPatternDel(pat, FC_FAMILY);
FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) family);
if (bold) {
FcPatternDel(pat, FC_WEIGHT);
FcPatternAddInteger(pat, FC_WEIGHT, FC_WEIGHT_BOLD);
}
FcConfigSubstitute(0, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
int pixel_size = 0;
FcPatternGetInteger(pat, FC_PIXEL_SIZE, 0, &pixel_size);
if (pixel_size > 0 || pt_size > 0) {
FcPatternDel(pat, FC_PIXEL_SIZE);
FcPatternAddInteger(pat, FC_PIXEL_SIZE, (int) (pt_size + 0.5));
}
FcResult res_fc;
FcPattern *match = FcFontMatch(0, pat, &res_fc);
FcPatternDestroy(pat);
return match;
}
static int family_matches(FcPattern *match, const char *family)
{
FcChar8 *matched_family = NULL;
if (FcPatternGetString(match, FC_FAMILY, 0, &matched_family) !=
FcResultMatch)
return 0;
return strcasecmp((const char *) matched_family, family) == 0;
}
static FontResources
font_resources_init(FT_Library lib, const char *font_string, int bold,
double pt_size)
{
FontResources res = { 0 };
FcPattern *base_pat = FcNameParse((const FcChar8 *) font_string);
if (!base_pat)
return res;
FcChar8 *requested_family_raw = NULL;
FcPatternGetString(base_pat, FC_FAMILY, 0, &requested_family_raw);
char *requested_family =
requested_family_raw ? strdup((char *) requested_family_raw) :
NULL;
FcPattern *match = NULL;
if (requested_family) {
match = try_font_match(requested_family, base_pat, bold, pt_size);
if (match && !family_matches(match, requested_family)) {
FcPatternDestroy(match);
match = NULL;
}
}
if (!match) {
for (int i = 0;
i < sizeof(preferred_fonts) / sizeof(preferred_fonts[0]); i++)
{
match =
try_font_match(preferred_fonts[i], base_pat, bold,
pt_size);
if (match && family_matches(match, preferred_fonts[i]))
break;
if (match) {
FcPatternDestroy(match);
match = NULL;
}
}
}
FcPatternDestroy(base_pat);
free(requested_family);
if (!match) {
fprintf(stderr,
"font_resources_init: FcFontMatch failed for \"%s\"\n",
font_string);
return res;
}
FcChar8 *family = NULL;
FcChar8 *style = NULL;
FcChar8 *file = NULL;
int index = 0;
FcPatternGetString(match, FC_FAMILY, 0, &family);
FcPatternGetString(match, FC_STYLE, 0, &style);
FcPatternGetString(match, FC_FILE, 0, &file);
FcPatternGetInteger(match, FC_INDEX, 0, &index);
if (!file) {
FcPatternDestroy(match);
return res;
}
FT_Face face = NULL;
if (FT_New_Face(lib, (const char *) file, index, &face) != 0) {
fprintf(stderr, "font_resources_init: FT_New_Face failed for %s\n",
(char *) file);
FcPatternDestroy(match);
return res;
}
if (face->num_fixed_sizes > 0) {
int want = (int) (pt_size + 0.5);
int best = 0;
for (int i = 0; i < face->num_fixed_sizes; i++) {
if (best == 0 || abs(face->available_sizes[i].height - want)
< abs(face->available_sizes[best - 1].height - want))
best = i + 1;
}
if (best > 0)
FT_Set_Pixel_Sizes(face, 0,
face->available_sizes[best - 1].height);
}
res.pixel_size = face->size ? face->size->metrics.height / 64 : 0;
cairo_font_face_t *cairo_face =
cairo_ft_font_face_create_for_ft_face(face, 0);
if (cairo_font_face_status(cairo_face) != CAIRO_STATUS_SUCCESS) {
cairo_font_face_destroy(cairo_face);
FT_Done_Face(face);
FcPatternDestroy(match);
return res;
}
(void) pt_size;
res.face = face;
res.cairo_face = cairo_face;
res.glyph_cap = 256;
res.glyph_buf = malloc(res.glyph_cap * sizeof(cairo_glyph_t));
FcPatternDestroy(match);
return res;
}
static void font_resources_destroy(FontResources *res)
{
if (!res)
return;
if (res->cairo_face)
cairo_font_face_destroy(res->cairo_face);
if (res->face)
FT_Done_Face(res->face);
free(res->glyph_buf);
res->face = NULL;
res->cairo_face = NULL;
res->glyph_buf = NULL;
}
static double parse_font_size(const char *font_string, double default_size)
{
double size = default_size;
if (!font_string)
return size;
FcPattern *pat = FcNameParse((const FcChar8 *) font_string);
if (pat) {
double s = 0;
if (FcPatternGetDouble(pat, FC_SIZE, 0, &s) == FcResultMatch)
size = s;
FcPatternDestroy(pat);
}
return size;
}
static int parse_font_pixel_size(const char *font_string)
{
if (!font_string)
return 0;
FcPattern *pat = FcNameParse((const FcChar8 *) font_string);
if (!pat)
return 0;
int px = 0;
FcPatternGetInteger(pat, FC_PIXEL_SIZE, 0, &px);
FcPatternDestroy(pat);
return px;
}
static CairoBackend *backend_new(cairo_surface_t *sfc,
const char *font_string,
const Palette *pal, int dpi)
{
CairoBackend *b = calloc(1, sizeof(*b));
if (!b)
return NULL;
b->output = sfc;
b->backbuf = NULL;
b->backcr = NULL;
b->cr = cairo_create(sfc);
if (cairo_status(b->cr) != CAIRO_STATUS_SUCCESS) {
cairo_destroy(b->cr);
free(b);
return NULL;
}
b->pal = pal;
b->border_px = 2;
b->font_string =
strdup(font_string ? font_string :
"Terminus:size=12:antialias=false");
if (dpi <= 0)
dpi = 96;
b->dpi = dpi;
double pt_size = parse_font_size(b->font_string, 12);
int pixel_size = parse_font_pixel_size(b->font_string);
b->font_size = pixel_size > 0 ? pixel_size : (pt_size * dpi / 72.0);
b->font_scale = frontend_font_scale;
b->flags &= ~CAIRO_FOCUS_DIM;
b->flash_alpha = 0;
b->flags &= ~CAIRO_SCALE_TO_FIT;
b->expected_width = 0;
b->expected_height = 0;
b->flags = flag_set(b->flags, CAIRO_FONT_OUTLINE, frontend_font_outline);
b->font_outline_alpha = frontend_font_outline_alpha;
b->flags = flag_set(b->flags, CAIRO_BORDER_BLOCKS, frontend_border_blocks);
b->border_visited = NULL;
b->border_cache = NULL;
b->border_cache_n = 0;
b->border_gen = 0;
b->dim_left = NULL;
b->dim_right = NULL;
b->dim_cache_w = 0;
b->dim_cache_bell = 0;
b->dim_cache_alpha = -1.0;
b->cursor_alpha = 1.0;
if (FT_Init_FreeType(&b->ft_lib) != 0) {
cairo_destroy(b->cr);
free(b->font_string);
free(b);
return NULL;
}
b->normal =
font_resources_init(b->ft_lib, b->font_string, 0, b->font_size);
b->bold =
font_resources_init(b->ft_lib, b->font_string, 1, b->font_size);
if (!b->normal.cairo_face || !b->bold.cairo_face) {
font_resources_destroy(&b->normal);
font_resources_destroy(&b->bold);
cairo_destroy(b->cr);
free(b->font_string);
FT_Done_FreeType(b->ft_lib);
free(b);
return NULL;
}
if (b->normal.pixel_size > 0) {
b->font_size = (double) b->normal.pixel_size;
}
cairo_save(b->cr);
cairo_set_font_face(b->cr, b->normal.cairo_face);
cairo_set_font_size(b->cr, b->font_size);
cairo_font_extents_t fe;
cairo_font_extents(b->cr, &fe);
b->char_height = (int) ceilf(fe.ascent + fe.descent);
cairo_text_extents_t te;
cairo_text_extents(b->cr, "W", &te);
b->char_width = MAX(1, (int) roundf(te.x_advance));
b->char_ascent = (int) ceilf(fe.ascent);
cairo_restore(b->cr);
FT_Face nf = b->normal.face;
fprintf(stderr,
"font: \"%s\" \"%s\" px=%.0f dpi=%d cw=%d ch=%d bitmap=%s\n",
nf && nf->family_name ? nf->family_name : "?",
nf && nf->style_name ? nf->style_name : "?",
b->font_size, dpi, b->char_width, b->char_height,
b->normal.pixel_size > 0 ? "yes" : "no");
b->border_px = (int) (b->char_width * 1.8 + 0.5);
if (b->border_px < 2)
b->border_px = 2;
b->colour_cc = COLOUR_CORRECTION_DEFAULT;
return b;
}
static void resolve_palette(CairoBackend * b, int idx, Argb * c);
static void set_source_argb(cairo_t * cr, Argb c);
static void cairo_free(void *ctx);
static void cairo_frame(void *ctx, const Screen * s,
int cx, int cy,
int cursor_shape, int mode,
int sel_active, int sel_start_x, int sel_start_y,
int sel_end_x, int sel_end_y);
static void cairo_resize(void *ctx, int cols, int rows);
static void cairo_resize_window(void *ctx, int winw, int winh);
static int cairo_char_width(void *ctx);
static int cairo_char_height(void *ctx);
static void cairo_set_cc(void *ctx, const ColourCorrection * cc);
static void cairo_set_font_scale(void *ctx, double scale);
static void cairo_set_cursor_alpha(void *ctx, double alpha);
static void cairo_damage(void *ctx);
static void cairo_invalidate(void *ctx);
static Overlay *cairo_overlay_push(void *ctx, int x, int y, int w, int h);
static void cairo_overlay_pop(void *ctx);
static void cairo_overlay_shadow(Overlay * o, const ShadowParams * sp);
static void cairo_bell(void *ctx);
static void cairo_focus(void *ctx, int focused);
static int cairo_border(void *ctx);
static int cairo_expected_width(void *ctx);
static int cairo_expected_height(void *ctx);
static int cairo_actual_width(void *ctx);
static int cairo_actual_height(void *ctx);
static void cairo_flush(void *ctx);
static const FrontendProto cairo_proto = {
.frame = cairo_frame,
.resize = cairo_resize,
.resize_window = cairo_resize_window,
.char_width = cairo_char_width,
.char_height = cairo_char_height,
.border = cairo_border,
.expected_width = cairo_expected_width,
.expected_height = cairo_expected_height,
.actual_width = cairo_actual_width,
.actual_height = cairo_actual_height,
.set_colour_correction = cairo_set_cc,
.set_font_scale = cairo_set_font_scale,
.set_cursor_alpha = cairo_set_cursor_alpha,
.damage = cairo_damage,
.invalidate = cairo_invalidate,
.overlay_push = cairo_overlay_push,
.overlay_pop = cairo_overlay_pop,
.overlay_set_shadow = cairo_overlay_shadow,
.bell = cairo_bell,
.focus = cairo_focus,
.flush = cairo_flush,
.free = cairo_free,
};
FrontendProto
frontend_cairo_temp(const char *font_string, const Palette *pal, int dpi)
{
cairo_surface_t *tmp =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
CairoBackend *b = backend_new(tmp, font_string, pal, dpi);
if (b && b->output == tmp) {
cairo_surface_destroy(tmp);
} else if (!b) {
cairo_surface_destroy(tmp);
}
FrontendProto p = cairo_proto;
p.ctx = b;
return p;
}
FrontendProto
frontend_cairo_new(const FrontendBackendInfo *info,
int winw, int winh,
const char *font_string, const Palette *pal, int dpi)
{
if (!info)
return (FrontendProto) {
.ctx = NULL};
cairo_surface_t *sfc =
cairo_xcb_surface_create((xcb_connection_t *) info->conn,
(xcb_drawable_t) info->window,
(xcb_visualtype_t *) info->visual,
winw, winh);
if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
return (FrontendProto) {
.ctx = NULL};
CairoBackend *b = backend_new(sfc, font_string, pal, dpi);
if (b) {
if (info->scale_to_fit)
b->flags |= CAIRO_SCALE_TO_FIT;
else
b->flags &= ~CAIRO_SCALE_TO_FIT;
b->win_width = winw;
b->win_height = winh;
}
FrontendProto p = cairo_proto;
p.ctx = b;
return p;
}
static void cairo_free(void *ctx)
{
CairoBackend *b = ctx;
if (!b)
return;
for (int i = 0; i < b->noverlays; i++) {
cairo_destroy(b->overlays[i].cr);
cairo_surface_destroy(b->overlays[i].surface);
}
free(b->overlays);
if (b->backcr)
cairo_destroy(b->backcr);
if (b->backbuf)
cairo_surface_destroy(b->backbuf);
if (b->dim_left)
cairo_pattern_destroy(b->dim_left);
if (b->dim_right)
cairo_pattern_destroy(b->dim_right);
if (b->grad_cache)
cairo_pattern_destroy(b->grad_cache);
cairo_destroy(b->cr);
font_resources_destroy(&b->normal);
font_resources_destroy(&b->bold);
if (b->ft_lib)
FT_Done_FreeType(b->ft_lib);
free(b->font_string);
if (b->border_visited)
free(b->border_visited);
free(b->border_cache);
free(b->drawn);
free(b->repaint);
free(b);
}
static int cell_width(const CairoBackend *b)
{
int w = (int) roundf((float) b->char_width * b->font_scale);
return w < 1 ? 1 : w;
}
static int cell_height(const CairoBackend *b)
{
int h = (int) roundf((float) b->char_height * b->font_scale);
return h < 1 ? 1 : h;
}
static void cairo_resize(void *ctx, int cols, int rows)
{
CairoBackend *b = ctx;
b->cols = cols;
b->rows = rows;
int cw = cell_width(b);
int ch = cell_height(b);
b->expected_width = 2 * b->border_px + cols * cw;
b->expected_height = 2 * b->border_px + rows * ch;
cairo_surface_t *old_backbuf = b->backbuf;
cairo_t *old_backcr = b->backcr;
b->backbuf = cairo_surface_create_similar(b->output,
CAIRO_CONTENT_COLOR,
b->expected_width,
b->expected_height);
if (cairo_surface_status(b->backbuf) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(b->backbuf);
b->backbuf = NULL;
b->backcr = NULL;
} else {
b->backcr = cairo_create(b->backbuf);
if (cairo_status(b->backcr) != CAIRO_STATUS_SUCCESS) {
cairo_destroy(b->backcr);
cairo_surface_destroy(b->backbuf);
b->backcr = NULL;
b->backbuf = NULL;
}
}
if (b->backcr) {
Argb bgc;
resolve_palette(b, PAL_DEFAULT_BG, &bgc);
set_source_argb(b->backcr, bgc);
cairo_paint(b->backcr);
}
if (old_backcr)
cairo_destroy(old_backcr);
if (old_backbuf)
cairo_surface_destroy(old_backbuf);
b->border_gen = 0;
if (b->dim_left) {
cairo_pattern_destroy(b->dim_left);
b->dim_left = NULL;
}
if (b->dim_right) {
cairo_pattern_destroy(b->dim_right);
b->dim_right = NULL;
}
if (b->grad_cache) {
cairo_pattern_destroy(b->grad_cache);
b->grad_cache = NULL;
}
free(b->border_visited);
b->border_visited = NULL;
free(b->border_cache);
b->border_cache = NULL;
b->border_cache_n = 0;
free(b->drawn);
b->drawn = malloc((size_t) b->cols * b->rows * sizeof(Cell));
if (b->drawn) {
Cell sentinel = { .r = (Rune) - 1, .fg = 0, .bg = 0, .attr = 0 };
for (int i = 0; i < b->cols * b->rows; i++)
b->drawn[i] = sentinel;
}
free(b->repaint);
b->repaint = malloc((size_t) b->cols * b->rows);
if (b->repaint)
memset(b->repaint, 0, (size_t) b->cols * b->rows);
b->prev_cur_x = 0;
b->prev_cur_y = 0;
b->prev_cur_w = 0;
b->prev_cur_h = 0;
b->bg_grad_top = 0;
b->flags |= CAIRO_FORCE_FULL;
b->flags &= ~CAIRO_BG_VALID;
}
static void cairo_resize_window(void *ctx, int winw, int winh)
{
CairoBackend *b = ctx;
b->win_width = winw;
b->win_height = winh;
cairo_xcb_surface_set_size(b->output, winw, winh);
b->bg_grad_top = 0;
b->flags &= ~CAIRO_BG_VALID;
}
static int cairo_char_width(void *ctx)
{
return cell_width(ctx);
}
static int cairo_char_height(void *ctx)
{
return cell_height(ctx);
}
static int cairo_border(void *ctx)
{
return ((CairoBackend *) ctx)->border_px;
}
static int cairo_expected_width(void *ctx)
{
CairoBackend *b = ctx;
return 2 * b->border_px + b->cols * cell_width(b);
}
static int cairo_expected_height(void *ctx)
{
CairoBackend *b = ctx;
return 2 * b->border_px + b->rows * cell_height(b);
}
static int cairo_actual_width(void *ctx)
{
return ((CairoBackend *) ctx)->win_width;
}
static int cairo_actual_height(void *ctx)
{
return ((CairoBackend *) ctx)->win_height;
}
static void cairo_set_cc(void *ctx, const ColourCorrection *cc)
{
CairoBackend *b = ctx;
b->colour_cc = *cc;
b->flags &= ~CAIRO_CC_CACHE_VALID;
}
static void cairo_damage(void *ctx)
{
((CairoBackend *) ctx)->flags |= CAIRO_WINDOW_DAMAGED;
}
static void cairo_invalidate(void *ctx)
{
CairoBackend *b = ctx;
b->flags |= CAIRO_FORCE_FULL;
b->flags &= ~(CAIRO_CC_CACHE_VALID | CAIRO_BG_VALID);
b->bg_grad_top = 0;
b->border_gen = 0;
if (b->grad_cache) {
cairo_pattern_destroy(b->grad_cache);
b->grad_cache = NULL;
}
if (b->dim_left) {
cairo_pattern_destroy(b->dim_left);
b->dim_left = NULL;
}
if (b->dim_right) {
cairo_pattern_destroy(b->dim_right);
b->dim_right = NULL;
}
if (b->drawn && b->cols > 0 && b->rows > 0)
for (int i = 0; i < b->cols * b->rows; i++)
b->drawn[i].r = (Rune) - 1;
}
static void cairo_set_cursor_alpha(void *ctx, double alpha)
{
CairoBackend *b = ctx;
if (alpha < 0.0)
alpha = 0.0;
if (alpha > 1.0)
alpha = 1.0;
b->cursor_alpha = alpha;
}
static void cairo_set_font_scale(void *ctx, double scale)
{
CairoBackend *b = ctx;
if (b->font_scale == scale)
return;
b->font_scale = scale;
if (b->cols > 0 && b->rows > 0)
cairo_resize(b, b->cols, b->rows);
}
static void cairo_overlay_shadow(Overlay *o, const ShadowParams *sp)
{
if (o) {
o->flags |= OVERLAY_HAS_SHADOW;
o->shadow = *sp;
}
}
static void cairo_flush(void *ctx)
{
CairoBackend *b = ctx;
if (b->output)
cairo_surface_flush(b->output);
}
static void cairo_bell(void *ctx)
{
CairoBackend *b = ctx;
b->flash_alpha = 0.4;
}
static void cairo_focus(void *ctx, int focused)
{
CairoBackend *b = ctx;
b->flags = flag_set(b->flags, CAIRO_FOCUS_DIM, !focused);
}
static void resolve_palette(CairoBackend * b, int idx, Argb * c);
static void set_source_argb(cairo_t * cr, Argb c);
static void cairo_draw_block_borders(CairoBackend * b, cairo_t * draw,
const Screen * s);
static void border_cache_refresh(CairoBackend * b, const Screen * s);
static void cell_effective(Cell c, int *fg, int *bg)
{
int f = (int) c.fg, g = (int) c.bg;
if (c.attr & ATTR_REVERSE) {
int t = f;
f = g;
g = t;
}
*fg = f;
*bg = g;
}
static unsigned int glyph_index_for(FontResources *res, Rune r)
{
if (r < GLYPH_DIRECT) {
unsigned int v = res->gdirect[r];
if (v)
return v - 1;
unsigned int gi = FT_Get_Char_Index(res->face, r);
res->gdirect[r] = gi + 1;
return gi;
}
unsigned int mask = GLYPH_CACHE_SIZE - 1;
unsigned int h = ((unsigned int) (r * 2654435761u) >> 20) & mask;
for (unsigned int i = 0; i < GLYPH_CACHE_PROBE; i++) {
GlyphCacheEnt *e = &res->gcache[(h + i) & mask];
if (e->key == r)
return e->index;
if (!e->key) {
e->key = r;
e->index = FT_Get_Char_Index(res->face, r);
return e->index;
}
}
return FT_Get_Char_Index(res->face, r);
}
static Argb outline_colour(Argb bg)
{
int r = (bg >> 16) & 0xFF;
int g = (bg >> 8) & 0xFF;
int b = bg & 0xFF;
if (colour_luma(bg) < 0.5f) {
r = r + (int) ((255 - r) * OUTLINE_SHIFT);
g = g + (int) ((255 - g) * OUTLINE_SHIFT);
b = b + (int) ((255 - b) * OUTLINE_SHIFT);
} else {
r = (int) (r * (1.0f - OUTLINE_SHIFT));
g = (int) (g * (1.0f - OUTLINE_SHIFT));
b = (int) (b * (1.0f - OUTLINE_SHIFT));
}
return ((Argb) r << 16) | ((Argb) g << 8) | (Argb) b;
}
static void
draw_span(CairoBackend *b, cairo_t *cr, int px, int py,
const char *text, const uint16_t *byte_cell, int text_len,
int bold, Argb bg)
{
FontResources *res = bold ? &b->bold : &b->normal;
if (!res->cairo_face)
return;
unsigned int count;
double scaled_w = cell_width(b);
double base_y = py + b->char_ascent * b->font_scale;
count = 0;
{
const char *p = text;
const char *end = text + text_len;
Rune r;
while (p < end) {
if (utf8_decode(&p, &r) == 0)
break;
count++;
}
}
int glyph_needed = (b->flags & CAIRO_FONT_OUTLINE) ? (int) count * 9 : (int) count;
if (glyph_needed > res->glyph_cap) {
int newcap = res->glyph_cap ? res->glyph_cap * 2 : 256;
while (newcap < glyph_needed)
newcap *= 2;
cairo_glyph_t *ng =
realloc(res->glyph_buf, newcap * sizeof(cairo_glyph_t));
if (!ng)
return;
res->glyph_buf = ng;
res->glyph_cap = newcap;
}
{
const char *p = text;
Rune r;
unsigned int i = 0;
while (p < text + text_len && i < count) {
int byteoff = (int) (p - text);
if (utf8_decode(&p, &r) == 0)
break;
int rel_cell = byteoff < text_len ? byte_cell[byteoff] : 0;
res->glyph_buf[i].index = glyph_index_for(res, r);
res->glyph_buf[i].x = px + rel_cell * scaled_w;
res->glyph_buf[i].y = base_y;
i++;
}
}
if (b->flags & CAIRO_FONT_OUTLINE) {
static const double off_x[OUTLINE_DIRS] = { -1, 0, 1, -1, 1, -1, 0, 1 };
static const double off_y[OUTLINE_DIRS] = { -1, -1, -1, 0, 0, 1, 1, 1 };
Argb oc = outline_colour(bg);
double or, og, ob;
argb_to_rgb(oc, &or, &og, &ob);
double od = (double) b->char_height / BASE_CHAR_HEIGHT;
if (od < 1.0)
od = 1.0;
for (int k = 0; k < OUTLINE_DIRS; k++) {
int base = (int) count + k * (int) count;
double dx = off_x[k] * od;
double dy = off_y[k] * od;
for (unsigned int i = 0; i < count; i++) {
res->glyph_buf[base + i].index = res->glyph_buf[i].index;
res->glyph_buf[base + i].x = res->glyph_buf[i].x + dx;
res->glyph_buf[base + i].y = res->glyph_buf[i].y + dy;
}
}
cairo_save(cr);
cairo_set_source_rgba(cr, or, og, ob, b->font_outline_alpha);
cairo_show_glyphs(cr, res->glyph_buf + count, count * 8);
cairo_restore(cr);
}
cairo_show_glyphs(cr, res->glyph_buf, count);
}