-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtkfont.c
3808 lines (3399 loc) · 131 KB
/
gtkfont.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
/*
* Unified font management for GTK.
*
* PuTTY is willing to use both old-style X server-side bitmap
* fonts _and_ GTK2/Pango client-side fonts. This requires us to
* do a bit of work to wrap the two wildly different APIs into
* forms the rest of the code can switch between seamlessly, and
* also requires a custom font selector capable of handling both
* types of font.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#if !GTK_CHECK_VERSION(3,0,0)
#include <gdk/gdkkeysyms.h>
#endif
#define MAY_REFER_TO_GTK_IN_HEADERS
#include "putty.h"
#include "gtkfont.h"
#include "gtkcompat.h"
#include "gtkmisc.h"
#include "tree234.h"
#ifndef NOT_X_WINDOWS
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include "x11misc.h"
#endif
/*
* Future work:
*
* - it would be nice to have a display of the current font name,
* and in particular whether it's client- or server-side,
* during the progress of the font selector.
*/
#if !GLIB_CHECK_VERSION(1,3,7)
#define g_ascii_strcasecmp g_strcasecmp
#define g_ascii_strncasecmp g_strncasecmp
#endif
/*
* Ad-hoc vtable mechanism to allow font structures to be
* polymorphic.
*
* Any instance of `unifont' used in the vtable functions will
* actually be an element of a larger structure containing data
* specific to the subtype.
*/
#define FONTFLAG_CLIENTSIDE 0x0001
#define FONTFLAG_SERVERSIDE 0x0002
#define FONTFLAG_SERVERALIAS 0x0004
#define FONTFLAG_NONMONOSPACED 0x0008
#define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
typedef void (*fontsel_add_entry)(void *ctx, const char *realfontname,
const char *family, const char *charset,
const char *style, const char *stylekey,
int size, int flags,
const struct UnifontVtable *fontclass);
struct UnifontVtable {
/*
* `Methods' of the `class'.
*/
unifont *(*create)(GtkWidget *widget, const char *name, bool wide,
bool bold, int shadowoffset, bool shadowalways);
unifont *(*create_fallback)(GtkWidget *widget, int height, bool wide,
bool bold, int shadowoffset,
bool shadowalways);
void (*destroy)(unifont *font);
bool (*has_glyph)(unifont *font, wchar_t glyph);
void (*draw_text)(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth);
void (*draw_combining)(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth);
void (*enum_fonts)(GtkWidget *widget,
fontsel_add_entry callback, void *callback_ctx);
char *(*canonify_fontname)(GtkWidget *widget, const char *name, int *size,
int *flags, bool resolve_aliases);
char *(*scale_fontname)(GtkWidget *widget, const char *name, int size);
char *(*size_increment)(unifont *font, int increment);
/*
* `Static data members' of the `class'.
*/
const char *prefix;
};
#ifndef NOT_X_WINDOWS
/* ----------------------------------------------------------------------
* X11 font implementation, directly using Xlib calls. Conditioned out
* if X11 fonts aren't available at all (e.g. building with GTK3 for a
* back end other than X).
*/
static bool x11font_has_glyph(unifont *font, wchar_t glyph);
static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth);
static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string,
int len, bool wide, bool bold,
int cellwidth);
static unifont *x11font_create(GtkWidget *widget, const char *name,
bool wide, bool bold,
int shadowoffset, bool shadowalways);
static void x11font_destroy(unifont *font);
static void x11font_enum_fonts(GtkWidget *widget,
fontsel_add_entry callback, void *callback_ctx);
static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
int *size, int *flags,
bool resolve_aliases);
static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
int size);
static char *x11font_size_increment(unifont *font, int increment);
#ifdef DRAW_TEXT_CAIRO
struct cairo_cached_glyph {
cairo_surface_t *surface;
unsigned char *bitmap;
};
#endif
/*
* Structure storing a single physical XFontStruct, plus associated
* data.
*/
typedef struct x11font_individual {
/* The XFontStruct itself. */
XFontStruct *xfs;
/*
* The `allocated' flag indicates whether we've tried to fetch
* this subfont already (thus distinguishing xfs==NULL because we
* haven't tried yet from xfs==NULL because we tried and failed,
* so that we don't keep trying and failing subsequently).
*/
bool allocated;
#ifdef DRAW_TEXT_CAIRO
/*
* A cache of glyph bitmaps downloaded from the X server when
* we're in Cairo rendering mode. If glyphcache itself is
* non-NULL, then entries in [0,nglyphs) are expected to be
* initialised to either NULL or a bitmap pointer.
*/
struct cairo_cached_glyph *glyphcache;
int nglyphs;
/*
* X server paraphernalia for actually downloading the glyphs.
*/
Pixmap pixmap;
GC gc;
int pixwidth, pixheight, pixoriginx, pixoriginy;
/*
* Paraphernalia for loading the resulting bitmaps into Cairo.
*/
int rowsize, allsize, indexflip;
#endif
} x11font_individual;
struct x11font {
/*
* Copy of the X display handle, so we don't have to keep
* extracting it from GDK.
*/
Display *disp;
/*
* Individual physical X fonts. We store a number of these, for
* automatically guessed bold and wide variants.
*/
x11font_individual fonts[4];
/*
* `sixteen_bit' is true iff the font object is indexed by
* values larger than a byte. That is, this flag tells us
* whether we use XDrawString or XDrawString16, etc.
*/
bool sixteen_bit;
/*
* `variable' is true iff the font is non-fixed-pitch. This
* enables some code which takes greater care over character
* positioning during text drawing.
*/
bool variable;
/*
* real_charset is the charset used when translating text into the
* font's internal encoding inside draw_text(). This need not be
* the same as the public_charset provided to the client; for
* example, public_charset might be CS_ISO8859_1 while
* real_charset is CS_ISO8859_1_X11.
*/
int real_charset;
/*
* Data passed in to unifont_create().
*/
int shadowoffset;
bool wide, bold, shadowalways;
unifont u;
};
static const UnifontVtable x11font_vtable = {
.create = x11font_create,
.create_fallback = NULL, /* no fallback fonts in X11 */
.destroy = x11font_destroy,
.has_glyph = x11font_has_glyph,
.draw_text = x11font_draw_text,
.draw_combining = x11font_draw_combining,
.enum_fonts = x11font_enum_fonts,
.canonify_fontname = x11font_canonify_fontname,
.scale_fontname = x11font_scale_fontname,
.size_increment = x11font_size_increment,
.prefix = "server",
};
#define XLFD_STRING_PARTS_LIST(S,I) \
S(foundry) \
S(family_name) \
S(weight_name) \
S(slant) \
S(setwidth_name) \
S(add_style_name) \
I(pixel_size) \
I(point_size) \
I(resolution_x) \
I(resolution_y) \
S(spacing) \
I(average_width) \
S(charset_registry) \
S(charset_encoding) \
/* end of list */
/* Special value for int fields that xlfd_recompose will render as "*" */
#define XLFD_INT_WILDCARD INT_MIN
struct xlfd_decomposed {
#define STR_FIELD(f) const char *f;
#define INT_FIELD(f) int f;
XLFD_STRING_PARTS_LIST(STR_FIELD, INT_FIELD)
#undef STR_FIELD
#undef INT_FIELD
};
static struct xlfd_decomposed *xlfd_decompose(const char *xlfd)
{
char *p, *components[14];
struct xlfd_decomposed *dec;
int i;
if (!xlfd)
return NULL;
dec = snew_plus(struct xlfd_decomposed, strlen(xlfd) + 1);
p = snew_plus_get_aux(dec);
strcpy(p, xlfd);
for (i = 0; i < 14; i++) {
if (*p != '-') {
/* Malformed XLFD: not enough '-' */
sfree(dec);
return NULL;
}
*p++ = '\0';
components[i] = p;
p += strcspn(p, "-");
}
if (*p) {
/* Malformed XLFD: too many '-' */
sfree(dec);
return NULL;
}
i = 0;
#define STORE_STR(f) dec->f = components[i++];
#define STORE_INT(f) dec->f = atoi(components[i++]);
XLFD_STRING_PARTS_LIST(STORE_STR, STORE_INT)
#undef STORE_STR
#undef STORE_INT
return dec;
}
static char *xlfd_recompose(const struct xlfd_decomposed *dec)
{
#define FMT_STR(f) "-%s"
#define ARG_STR(f) , dec->f
#define FMT_INT(f) "%s%.*d"
#define ARG_INT(f) \
, dec->f == XLFD_INT_WILDCARD ? "-*" : "-" \
, dec->f == XLFD_INT_WILDCARD ? 0 : 1 \
, dec->f == XLFD_INT_WILDCARD ? 0 : dec->f
return dupprintf(XLFD_STRING_PARTS_LIST(FMT_STR, FMT_INT)
XLFD_STRING_PARTS_LIST(ARG_STR, ARG_INT));
#undef FMT_STR
#undef ARG_STR
#undef FMT_INT
#undef ARG_INT
}
static char *x11_guess_derived_font_name(Display *disp, XFontStruct *xfs,
bool bold, bool wide)
{
Atom fontprop = XInternAtom(disp, "FONT", False);
unsigned long ret;
if (XGetFontProperty(xfs, fontprop, &ret)) {
char *name = XGetAtomName(disp, (Atom)ret);
struct xlfd_decomposed *xlfd = xlfd_decompose(name);
if (!xlfd)
return NULL;
if (bold)
xlfd->weight_name = "bold";
if (wide) {
/* Width name obviously may have changed. */
/* Additional style may now become e.g. `ja' or `ko'. */
xlfd->setwidth_name = xlfd->add_style_name = "*";
/* Expect to double the average width. */
xlfd->average_width *= 2;
}
{
char *ret = xlfd_recompose(xlfd);
sfree(xlfd);
return ret;
}
}
return NULL;
}
static int x11_font_width(XFontStruct *xfs, bool sixteen_bit)
{
if (sixteen_bit) {
XChar2b space;
space.byte1 = 0;
space.byte2 = '0';
return XTextWidth16(xfs, &space, 1);
} else {
return XTextWidth(xfs, "0", 1);
}
}
static const XCharStruct *x11_char_struct(
XFontStruct *xfs, unsigned char byte1, unsigned char byte2)
{
int index;
/*
* The man page for XQueryFont is rather confusing about how the
* per_char array in the XFontStruct is laid out, because it gives
* formulae for determining the two-byte X character code _from_
* an index into the per_char array. Going the other way, it's
* rather simpler:
*
* The valid character codes have byte1 between min_byte1 and
* max_byte1 inclusive, and byte2 between min_char_or_byte2 and
* max_char_or_byte2 inclusive. This gives a rectangle of size
* (max_byte2-min_byte1+1) by
* (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
* rectangle encoded in the per_char array. Hence, given a
* character code which is valid in the sense that it falls
* somewhere in that rectangle, its index in per_char is given by
* setting
*
* x = byte2 - min_char_or_byte2
* y = byte1 - min_byte1
* index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
*
* If min_byte1 and min_byte2 are both zero, that's a special case
* which can be treated as if min_byte2 was 1 instead, i.e. the
* per_char array just runs from min_char_or_byte2 to
* max_char_or_byte2 inclusive, and byte1 should always be zero.
*/
if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2)
return NULL;
if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
index = byte2 - xfs->min_char_or_byte2;
} else {
if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1)
return NULL;
index = ((byte2 - xfs->min_char_or_byte2) +
((byte1 - xfs->min_byte1) *
(xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1)));
}
if (!xfs->per_char) /* per_char NULL => everything in range exists */
return &xfs->max_bounds;
return &xfs->per_char[index];
}
static bool x11_font_has_glyph(
XFontStruct *xfs, unsigned char byte1, unsigned char byte2)
{
/*
* Not to be confused with x11font_has_glyph, which is a method of
* the x11font 'class' and hence takes a unifont as argument. This
* is the low-level function which grubs about in an actual
* XFontStruct to see if a given glyph exists.
*
* We must do this ourselves rather than letting Xlib's
* XTextExtents16 do the job, because XTextExtents will helpfully
* substitute the font's default_char for any missing glyph and
* not tell us it did so, which precisely won't help us find out
* which glyphs _are_ missing.
*/
const XCharStruct *xcs = x11_char_struct(xfs, byte1, byte2);
return xcs && (xcs->ascent + xcs->descent > 0 || xcs->width > 0);
}
static unifont *x11font_create(GtkWidget *widget, const char *name,
bool wide, bool bold,
int shadowoffset, bool shadowalways)
{
struct x11font *xfont;
XFontStruct *xfs;
Display *disp;
Atom charset_registry, charset_encoding, spacing;
unsigned long registry_ret, encoding_ret, spacing_ret;
int pubcs, realcs;
bool sixteen_bit, variable;
int i;
if ((disp = get_x11_display()) == NULL)
return NULL;
xfs = XLoadQueryFont(disp, name);
if (!xfs)
return NULL;
charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
pubcs = realcs = CS_NONE;
sixteen_bit = false;
variable = true;
if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
char *reg, *enc;
reg = XGetAtomName(disp, (Atom)registry_ret);
enc = XGetAtomName(disp, (Atom)encoding_ret);
if (reg && enc) {
char *encoding = dupcat(reg, "-", enc);
pubcs = realcs = charset_from_xenc(encoding);
/*
* iso10646-1 is the only wide font encoding we
* support. In this case, we expect clients to give us
* UTF-8, which this module must internally convert
* into 16-bit Unicode.
*/
if (!strcasecmp(encoding, "iso10646-1")) {
sixteen_bit = true;
pubcs = realcs = CS_UTF8;
}
/*
* Hack for X line-drawing characters: if the primary font
* is encoded as ISO-8859-1, and has valid glyphs in the
* low character positions, it is assumed that those
* glyphs are the VT100 line-drawing character set.
*/
if (pubcs == CS_ISO8859_1) {
int ch;
for (ch = 1; ch < 32; ch++)
if (!x11_font_has_glyph(xfs, 0, ch))
break;
if (ch == 32)
realcs = CS_ISO8859_1_X11;
}
sfree(encoding);
}
}
spacing = XInternAtom(disp, "SPACING", False);
if (XGetFontProperty(xfs, spacing, &spacing_ret)) {
char *spc;
spc = XGetAtomName(disp, (Atom)spacing_ret);
if (spc && strchr("CcMm", spc[0]))
variable = false;
}
xfont = snew(struct x11font);
xfont->u.vt = &x11font_vtable;
xfont->u.width = x11_font_width(xfs, sixteen_bit);
xfont->u.ascent = xfs->ascent;
xfont->u.descent = xfs->descent;
xfont->u.height = xfont->u.ascent + xfont->u.descent;
xfont->u.public_charset = pubcs;
xfont->u.want_fallback = true;
xfont->u.strikethrough_y = xfont->u.ascent - (xfont->u.ascent * 3 / 8);
#ifdef DRAW_TEXT_GDK
xfont->u.preferred_drawtype = DRAWTYPE_GDK;
#elif defined DRAW_TEXT_CAIRO
xfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
#else
#error No drawtype available at all
#endif
xfont->disp = disp;
xfont->real_charset = realcs;
xfont->sixteen_bit = sixteen_bit;
xfont->variable = variable;
xfont->wide = wide;
xfont->bold = bold;
xfont->shadowoffset = shadowoffset;
xfont->shadowalways = shadowalways;
for (i = 0; i < lenof(xfont->fonts); i++) {
xfont->fonts[i].xfs = NULL;
xfont->fonts[i].allocated = false;
#ifdef DRAW_TEXT_CAIRO
xfont->fonts[i].glyphcache = NULL;
xfont->fonts[i].nglyphs = 0;
xfont->fonts[i].pixmap = None;
xfont->fonts[i].gc = None;
#endif
}
xfont->fonts[0].xfs = xfs;
xfont->fonts[0].allocated = true;
return &xfont->u;
}
static void x11font_destroy(unifont *font)
{
struct x11font *xfont = container_of(font, struct x11font, u);
Display *disp = xfont->disp;
int i;
for (i = 0; i < lenof(xfont->fonts); i++) {
if (xfont->fonts[i].xfs)
XFreeFont(disp, xfont->fonts[i].xfs);
#ifdef DRAW_TEXT_CAIRO
if (xfont->fonts[i].gc != None)
XFreeGC(disp, xfont->fonts[i].gc);
if (xfont->fonts[i].pixmap != None)
XFreePixmap(disp, xfont->fonts[i].pixmap);
if (xfont->fonts[i].glyphcache) {
int j;
for (j = 0; j < xfont->fonts[i].nglyphs; j++) {
cairo_surface_destroy(xfont->fonts[i].glyphcache[j].surface);
sfree(xfont->fonts[i].glyphcache[j].bitmap);
}
sfree(xfont->fonts[i].glyphcache);
}
#endif
}
sfree(xfont);
}
static void x11_alloc_subfont(struct x11font *xfont, int sfid)
{
Display *disp = xfont->disp;
char *derived_name = x11_guess_derived_font_name
(disp, xfont->fonts[0].xfs, sfid & 1, !!(sfid & 2));
xfont->fonts[sfid].xfs = XLoadQueryFont(disp, derived_name);
xfont->fonts[sfid].allocated = true;
sfree(derived_name);
/* Note that xfont->fonts[sfid].xfs may still be NULL, if XLQF failed. */
}
static bool x11font_has_glyph(unifont *font, wchar_t glyph)
{
struct x11font *xfont = container_of(font, struct x11font, u);
if (xfont->sixteen_bit) {
/*
* This X font has 16-bit character indices, which means
* we can directly use our Unicode input value.
*/
return x11_font_has_glyph(xfont->fonts[0].xfs,
glyph >> 8, glyph & 0xFF);
} else {
/*
* This X font has 8-bit indices, so we must convert to the
* appropriate character set.
*/
char sbstring[2];
int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
sbstring, 2, "", NULL);
if (sblen == 0 || !sbstring[0])
return false; /* not even in the charset */
return x11_font_has_glyph(xfont->fonts[0].xfs, 0,
(unsigned char)sbstring[0]);
}
}
#if !GTK_CHECK_VERSION(2,0,0)
#define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
#elif GTK_CHECK_VERSION(3,0,0)
#define GDK_DRAWABLE_XID(d) GDK_WINDOW_XID(d) /* GTK3's name for this */
#endif
static int x11font_width_16(unifont_drawctx *ctx, x11font_individual *xfi,
const void *vstring, int start, int length)
{
const XChar2b *string = (const XChar2b *)vstring;
return XTextWidth16(xfi->xfs, string+start, length);
}
static int x11font_width_8(unifont_drawctx *ctx, x11font_individual *xfi,
const void *vstring, int start, int length)
{
const char *string = (const char *)vstring;
return XTextWidth(xfi->xfs, string+start, length);
}
#ifdef DRAW_TEXT_GDK
static void x11font_gdk_setup(unifont_drawctx *ctx, x11font_individual *xfi,
Display *disp)
{
XSetFont(disp, GDK_GC_XGC(ctx->u.gdk.gc), xfi->xfs->fid);
}
static void x11font_gdk_draw_16(unifont_drawctx *ctx, x11font_individual *xfi,
Display *disp, int x, int y,
const void *vstring, int start, int length)
{
const XChar2b *string = (const XChar2b *)vstring;
XDrawString16(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
}
static void x11font_gdk_draw_8(unifont_drawctx *ctx, x11font_individual *xfi,
Display *disp, int x, int y,
const void *vstring, int start, int length)
{
const char *string = (const char *)vstring;
XDrawString(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
}
#endif
#ifdef DRAW_TEXT_CAIRO
static void x11font_cairo_setup(
unifont_drawctx *ctx, x11font_individual *xfi, Display *disp)
{
if (xfi->pixmap == None) {
XGCValues gcvals;
GdkWindow *widgetwin = gtk_widget_get_window(ctx->u.cairo.widget);
int widgetscr = GDK_SCREEN_XNUMBER(gdk_window_get_screen(widgetwin));
xfi->pixwidth =
xfi->xfs->max_bounds.rbearing - xfi->xfs->min_bounds.lbearing;
xfi->pixheight =
xfi->xfs->max_bounds.ascent + xfi->xfs->max_bounds.descent;
xfi->pixoriginx = -xfi->xfs->min_bounds.lbearing;
xfi->pixoriginy = xfi->xfs->max_bounds.ascent;
xfi->rowsize = cairo_format_stride_for_width(CAIRO_FORMAT_A1,
xfi->pixwidth);
xfi->allsize = xfi->rowsize * xfi->pixheight;
{
/*
* Test host endianness and use it to set xfi->indexflip,
* which is XORed into our left-shift counts in order to
* implement the CAIRO_FORMAT_A1 specification, in which
* each bitmap byte is oriented LSB-first on little-endian
* platforms and MSB-first on big-endian ones.
*
* This is the same technique Cairo itself uses to test
* endianness, so hopefully it'll work in any situation
* where Cairo is usable at all.
*/
static const int endianness_test = 1;
xfi->indexflip = (*((char *) &endianness_test) == 1) ? 0 : 7;
}
xfi->pixmap = XCreatePixmap
(disp,
GDK_DRAWABLE_XID(gtk_widget_get_window(ctx->u.cairo.widget)),
xfi->pixwidth, xfi->pixheight, 1);
gcvals.foreground = WhitePixel(disp, widgetscr);
gcvals.background = BlackPixel(disp, widgetscr);
gcvals.font = xfi->xfs->fid;
xfi->gc = XCreateGC(disp, xfi->pixmap,
GCForeground | GCBackground | GCFont, &gcvals);
}
}
static void x11font_cairo_cache_glyph(
Display *disp, x11font_individual *xfi, int glyphindex)
{
XImage *image;
int x, y;
unsigned char *bitmap;
const XCharStruct *xcs = x11_char_struct(xfi->xfs, glyphindex >> 8,
glyphindex & 0xFF);
bitmap = snewn(xfi->allsize, unsigned char);
memset(bitmap, 0, xfi->allsize);
image = XGetImage(disp, xfi->pixmap, 0, 0,
xfi->pixwidth, xfi->pixheight, AllPlanes, XYPixmap);
for (y = xfi->pixoriginy - xcs->ascent;
y < xfi->pixoriginy + xcs->descent; y++) {
for (x = xfi->pixoriginx + xcs->lbearing;
x < xfi->pixoriginx + xcs->rbearing; x++) {
unsigned long pixel = XGetPixel(image, x, y);
if (pixel) {
int byteindex = y * xfi->rowsize + x/8;
int bitindex = (x & 7) ^ xfi->indexflip;
bitmap[byteindex] |= 1U << bitindex;
}
}
}
XDestroyImage(image);
if (xfi->nglyphs <= glyphindex) {
/* Round up to the next multiple of 256 on the general
* principle that Unicode characters come in contiguous blocks
* often used together */
int old_nglyphs = xfi->nglyphs;
xfi->nglyphs = (glyphindex + 0x100) & ~0xFF;
xfi->glyphcache = sresize(xfi->glyphcache, xfi->nglyphs,
struct cairo_cached_glyph);
while (old_nglyphs < xfi->nglyphs) {
xfi->glyphcache[old_nglyphs].surface = NULL;
xfi->glyphcache[old_nglyphs].bitmap = NULL;
old_nglyphs++;
}
}
xfi->glyphcache[glyphindex].bitmap = bitmap;
xfi->glyphcache[glyphindex].surface = cairo_image_surface_create_for_data
(bitmap, CAIRO_FORMAT_A1, xfi->pixwidth, xfi->pixheight, xfi->rowsize);
}
static void x11font_cairo_draw_glyph(unifont_drawctx *ctx,
x11font_individual *xfi, int x, int y,
int glyphindex)
{
if (xfi->glyphcache[glyphindex].surface) {
cairo_mask_surface(ctx->u.cairo.cr,
xfi->glyphcache[glyphindex].surface,
x - xfi->pixoriginx, y - xfi->pixoriginy);
}
}
static void x11font_cairo_draw_16(
unifont_drawctx *ctx, x11font_individual *xfi, Display *disp,
int x, int y, const void *vstring, int start, int length)
{
const XChar2b *string = (const XChar2b *)vstring + start;
int i;
for (i = 0; i < length; i++) {
if (x11_font_has_glyph(xfi->xfs, string[i].byte1, string[i].byte2)) {
int glyphindex = (256 * (unsigned char)string[i].byte1 +
(unsigned char)string[i].byte2);
if (glyphindex >= xfi->nglyphs ||
!xfi->glyphcache[glyphindex].surface) {
XDrawImageString16(disp, xfi->pixmap, xfi->gc,
xfi->pixoriginx, xfi->pixoriginy,
string+i, 1);
x11font_cairo_cache_glyph(disp, xfi, glyphindex);
}
x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
x += XTextWidth16(xfi->xfs, string+i, 1);
}
}
}
static void x11font_cairo_draw_8(
unifont_drawctx *ctx, x11font_individual *xfi, Display *disp,
int x, int y, const void *vstring, int start, int length)
{
const char *string = (const char *)vstring + start;
int i;
for (i = 0; i < length; i++) {
if (x11_font_has_glyph(xfi->xfs, 0, string[i])) {
int glyphindex = (unsigned char)string[i];
if (glyphindex >= xfi->nglyphs ||
!xfi->glyphcache[glyphindex].surface) {
XDrawImageString(disp, xfi->pixmap, xfi->gc,
xfi->pixoriginx, xfi->pixoriginy,
string+i, 1);
x11font_cairo_cache_glyph(disp, xfi, glyphindex);
}
x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
x += XTextWidth(xfi->xfs, string+i, 1);
}
}
}
#endif /* DRAW_TEXT_CAIRO */
struct x11font_drawfuncs {
int (*width)(unifont_drawctx *ctx, x11font_individual *xfi,
const void *vstring, int start, int length);
void (*setup)(unifont_drawctx *ctx, x11font_individual *xfi,
Display *disp);
void (*draw)(unifont_drawctx *ctx, x11font_individual *xfi, Display *disp,
int x, int y, const void *vstring, int start, int length);
};
/*
* This array has two entries per compiled-in drawtype; of each pair,
* the first is for an 8-bit font and the second for 16-bit.
*/
static const struct x11font_drawfuncs x11font_drawfuncs[2*DRAWTYPE_NTYPES] = {
#ifdef DRAW_TEXT_GDK
/* gdk, 8-bit */
{
x11font_width_8,
x11font_gdk_setup,
x11font_gdk_draw_8,
},
/* gdk, 16-bit */
{
x11font_width_16,
x11font_gdk_setup,
x11font_gdk_draw_16,
},
#endif
#ifdef DRAW_TEXT_CAIRO
/* cairo, 8-bit */
{
x11font_width_8,
x11font_cairo_setup,
x11font_cairo_draw_8,
},
/* [3] cairo, 16-bit */
{
x11font_width_16,
x11font_cairo_setup,
x11font_cairo_draw_16,
},
#endif
};
static void x11font_really_draw_text(
const struct x11font_drawfuncs *dfns, unifont_drawctx *ctx,
x11font_individual *xfi, Display *disp,
int x, int y, const void *string, int nchars,
int shadowoffset, bool fontvariable, int cellwidth)
{
int start = 0, step, nsteps;
bool centre;
if (fontvariable) {
/*
* In a variable-pitch font, we draw one character at a
* time, and centre it in the character cell.
*/
step = 1;
nsteps = nchars;
centre = true;
} else {
/*
* In a fixed-pitch font, we can draw the whole lot in one go.
*/
step = nchars;
nsteps = 1;
centre = false;
}
dfns->setup(ctx, xfi, disp);
while (nsteps-- > 0) {
int X = x;
if (centre)
X += (cellwidth - dfns->width(ctx, xfi, string, start, step)) / 2;
dfns->draw(ctx, xfi, disp, X, y, string, start, step);
if (shadowoffset)
dfns->draw(ctx, xfi, disp, X + shadowoffset, y,
string, start, step);
x += cellwidth;
start += step;
}
}
static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth)
{
struct x11font *xfont = container_of(font, struct x11font, u);
int sfid;
int shadowoffset = 0;
int mult = (wide ? 2 : 1);
int index = 2 * (int)ctx->type;
wide = wide && !xfont->wide;
bold = bold && !xfont->bold;
/*
* Decide which subfont we're using, and whether we have to
* use shadow bold.
*/
if (xfont->shadowalways && bold) {
shadowoffset = xfont->shadowoffset;
bold = false;
}
sfid = 2 * wide + bold;
if (!xfont->fonts[sfid].allocated)
x11_alloc_subfont(xfont, sfid);
if (bold && !xfont->fonts[sfid].xfs) {
bold = false;
shadowoffset = xfont->shadowoffset;
sfid = 2 * wide + bold;
if (!xfont->fonts[sfid].allocated)
x11_alloc_subfont(xfont, sfid);
}
if (!xfont->fonts[sfid].xfs)
return; /* we've tried our best, but no luck */
if (xfont->sixteen_bit) {
/*
* This X font has 16-bit character indices, which means
* we can directly use our Unicode input string.
*/
XChar2b *xcs;
int i;
xcs = snewn(len, XChar2b);
for (i = 0; i < len; i++) {
xcs[i].byte1 = string[i] >> 8;
xcs[i].byte2 = string[i];
}
x11font_really_draw_text(x11font_drawfuncs + index + 1, ctx,
&xfont->fonts[sfid], xfont->disp, x, y,
xcs, len, shadowoffset,
xfont->variable, cellwidth * mult);
sfree(xcs);
} else {
/*
* This X font has 8-bit indices, so we must convert to the
* appropriate character set.
*/
char *sbstring = snewn(len+1, char);
int sblen = wc_to_mb(xfont->real_charset, 0, string, len,
sbstring, len+1, ".", NULL);
x11font_really_draw_text(x11font_drawfuncs + index + 0, ctx,
&xfont->fonts[sfid], xfont->disp, x, y,
sbstring, sblen, shadowoffset,
xfont->variable, cellwidth * mult);
sfree(sbstring);
}
}
static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string,
int len, bool wide, bool bold,
int cellwidth)
{
/*
* For server-side fonts, there's no sophisticated system for
* combining characters intelligently, so the best we can do is to
* overprint them on each other in the obvious way.
*/
int i;
for (i = 0; i < len; i++)
x11font_draw_text(ctx, font, x, y, string+i, 1, wide, bold, cellwidth);
}
static void x11font_enum_fonts(GtkWidget *widget,
fontsel_add_entry callback, void *callback_ctx)
{
Display *disp;
char **fontnames;
char *tmp = NULL;
int nnames, i, max, tmpsize;
if ((disp = get_x11_display()) == NULL)
return;
max = 32768;
while (1) {
fontnames = XListFonts(disp, "*", max, &nnames);
if (nnames >= max) {
XFreeFontNames(fontnames);
max *= 2;
} else