Suppress Pango's bidi, by displaying RTL characters one at a time. I
[u/mdw/putty] / unix / gtkfont.c
1 /*
2 * Unified font management for GTK.
3 *
4 * PuTTY is willing to use both old-style X server-side bitmap
5 * fonts _and_ GTK2/Pango client-side fonts. This requires us to
6 * do a bit of work to wrap the two wildly different APIs into
7 * forms the rest of the code can switch between seamlessly, and
8 * also requires a custom font selector capable of handling both
9 * types of font.
10 */
11
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <gtk/gtk.h>
16 #include <gdk/gdkkeysyms.h>
17 #include <gdk/gdkx.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xutil.h>
20 #include <X11/Xatom.h>
21
22 #include "putty.h"
23 #include "gtkfont.h"
24 #include "tree234.h"
25
26 /*
27 * Future work:
28 *
29 * - it would be nice to have a display of the current font name,
30 * and in particular whether it's client- or server-side,
31 * during the progress of the font selector.
32 *
33 * - it would be nice if we could move the processing of
34 * underline and VT100 double width into this module, so that
35 * instead of using the ghastly pixmap-stretching technique
36 * everywhere we could tell the Pango backend to scale its
37 * fonts to double size properly and at full resolution.
38 * However, this requires me to learn how to make Pango stretch
39 * text to an arbitrary aspect ratio (for double-width only
40 * text, which perversely is harder than DW+DH), and right now
41 * I haven't the energy.
42 */
43
44 /*
45 * Ad-hoc vtable mechanism to allow font structures to be
46 * polymorphic.
47 *
48 * Any instance of `unifont' used in the vtable functions will
49 * actually be the first element of a larger structure containing
50 * data specific to the subtype. This is permitted by the ISO C
51 * provision that one may safely cast between a pointer to a
52 * structure and a pointer to its first element.
53 */
54
55 #define FONTFLAG_CLIENTSIDE 0x0001
56 #define FONTFLAG_SERVERSIDE 0x0002
57 #define FONTFLAG_SERVERALIAS 0x0004
58 #define FONTFLAG_NONMONOSPACED 0x0008
59
60 #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
61
62 typedef void (*fontsel_add_entry)(void *ctx, const char *realfontname,
63 const char *family, const char *charset,
64 const char *style, const char *stylekey,
65 int size, int flags,
66 const struct unifont_vtable *fontclass);
67
68 struct unifont_vtable {
69 /*
70 * `Methods' of the `class'.
71 */
72 unifont *(*create)(GtkWidget *widget, const char *name, int wide, int bold,
73 int shadowoffset, int shadowalways);
74 unifont *(*create_fallback)(GtkWidget *widget, int height, int wide,
75 int bold, int shadowoffset, int shadowalways);
76 void (*destroy)(unifont *font);
77 int (*has_glyph)(unifont *font, wchar_t glyph);
78 void (*draw_text)(GdkDrawable *target, GdkGC *gc, unifont *font,
79 int x, int y, const wchar_t *string, int len, int wide,
80 int bold, int cellwidth);
81 void (*enum_fonts)(GtkWidget *widget,
82 fontsel_add_entry callback, void *callback_ctx);
83 char *(*canonify_fontname)(GtkWidget *widget, const char *name, int *size,
84 int *flags, int resolve_aliases);
85 char *(*scale_fontname)(GtkWidget *widget, const char *name, int size);
86
87 /*
88 * `Static data members' of the `class'.
89 */
90 const char *prefix;
91 };
92
93 /* ----------------------------------------------------------------------
94 * X11 font implementation, directly using Xlib calls.
95 */
96
97 static int x11font_has_glyph(unifont *font, wchar_t glyph);
98 static void x11font_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
99 int x, int y, const wchar_t *string, int len,
100 int wide, int bold, int cellwidth);
101 static unifont *x11font_create(GtkWidget *widget, const char *name,
102 int wide, int bold,
103 int shadowoffset, int shadowalways);
104 static void x11font_destroy(unifont *font);
105 static void x11font_enum_fonts(GtkWidget *widget,
106 fontsel_add_entry callback, void *callback_ctx);
107 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
108 int *size, int *flags,
109 int resolve_aliases);
110 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
111 int size);
112
113 struct x11font {
114 struct unifont u;
115 /*
116 * Actual font objects. We store a number of these, for
117 * automatically guessed bold and wide variants.
118 *
119 * The parallel array `allocated' indicates whether we've
120 * tried to fetch a subfont already (thus distinguishing NULL
121 * because we haven't tried yet from NULL because we tried and
122 * failed, so that we don't keep trying and failing
123 * subsequently).
124 */
125 XFontStruct *fonts[4];
126 int allocated[4];
127 /*
128 * `sixteen_bit' is true iff the font object is indexed by
129 * values larger than a byte. That is, this flag tells us
130 * whether we use XDrawString or XDrawString16, etc.
131 */
132 int sixteen_bit;
133 /*
134 * `variable' is true iff the font is non-fixed-pitch. This
135 * enables some code which takes greater care over character
136 * positioning during text drawing.
137 */
138 int variable;
139 /*
140 * real_charset is the charset used when translating text into the
141 * font's internal encoding inside draw_text(). This need not be
142 * the same as the public_charset provided to the client; for
143 * example, public_charset might be CS_ISO8859_1 while
144 * real_charset is CS_ISO8859_1_X11.
145 */
146 int real_charset;
147 /*
148 * Data passed in to unifont_create().
149 */
150 int wide, bold, shadowoffset, shadowalways;
151 };
152
153 static const struct unifont_vtable x11font_vtable = {
154 x11font_create,
155 NULL, /* no fallback fonts in X11 */
156 x11font_destroy,
157 x11font_has_glyph,
158 x11font_draw_text,
159 x11font_enum_fonts,
160 x11font_canonify_fontname,
161 x11font_scale_fontname,
162 "server",
163 };
164
165 static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide)
166 {
167 Display *disp = GDK_DISPLAY();
168 Atom fontprop = XInternAtom(disp, "FONT", False);
169 unsigned long ret;
170 if (XGetFontProperty(xfs, fontprop, &ret)) {
171 char *name = XGetAtomName(disp, (Atom)ret);
172 if (name && name[0] == '-') {
173 char *strings[13];
174 char *dupname, *extrafree = NULL, *ret;
175 char *p, *q;
176 int nstr;
177
178 p = q = dupname = dupstr(name); /* skip initial minus */
179 nstr = 0;
180
181 while (*p && nstr < lenof(strings)) {
182 if (*p == '-') {
183 *p = '\0';
184 strings[nstr++] = p+1;
185 }
186 p++;
187 }
188
189 if (nstr < lenof(strings))
190 return NULL; /* XLFD was malformed */
191
192 if (bold)
193 strings[2] = "bold";
194
195 if (wide) {
196 /* 4 is `wideness', which obviously may have changed. */
197 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
198 strings[4] = strings[5] = "*";
199 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
200 }
201
202 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
203 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
204 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
205 "-", strings[ 9], "-", strings[10], "-", strings[11],
206 "-", strings[12], NULL);
207 sfree(extrafree);
208 sfree(dupname);
209
210 return ret;
211 }
212 }
213 return NULL;
214 }
215
216 static int x11_font_width(XFontStruct *xfs, int sixteen_bit)
217 {
218 if (sixteen_bit) {
219 XChar2b space;
220 space.byte1 = 0;
221 space.byte2 = '0';
222 return XTextWidth16(xfs, &space, 1);
223 } else {
224 return XTextWidth(xfs, "0", 1);
225 }
226 }
227
228 static int x11_font_has_glyph(XFontStruct *xfs, int byte1, int byte2)
229 {
230 int index;
231
232 /*
233 * Not to be confused with x11font_has_glyph, which is a method of
234 * the x11font 'class' and hence takes a unifont as argument. This
235 * is the low-level function which grubs about in an actual
236 * XFontStruct to see if a given glyph exists.
237 *
238 * We must do this ourselves rather than letting Xlib's
239 * XTextExtents16 do the job, because XTextExtents will helpfully
240 * substitute the font's default_char for any missing glyph and
241 * not tell us it did so, which precisely won't help us find out
242 * which glyphs _are_ missing.
243 *
244 * The man page for XQueryFont is rather confusing about how the
245 * per_char array in the XFontStruct is laid out, because it gives
246 * formulae for determining the two-byte X character code _from_
247 * an index into the per_char array. Going the other way, it's
248 * rather simpler:
249 *
250 * The valid character codes have byte1 between min_byte1 and
251 * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
252 * max_char_or_byte2 inclusive. This gives a rectangle of size
253 * (max_byte2-min_byte1+1) by
254 * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
255 * rectangle encoded in the per_char array. Hence, given a
256 * character code which is valid in the sense that it falls
257 * somewhere in that rectangle, its index in per_char is given by
258 * setting
259 *
260 * x = byte2 - min_char_or_byte2
261 * y = byte1 - min_byte1
262 * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
263 *
264 * If min_byte1 and min_byte2 are both zero, that's a special case
265 * which can be treated as if min_byte2 was 1 instead, i.e. the
266 * per_char array just runs from min_char_or_byte2 to
267 * max_char_or_byte2 inclusive, and byte1 should always be zero.
268 */
269
270 if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2)
271 return FALSE;
272
273 if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
274 index = byte2 - xfs->min_char_or_byte2;
275 } else {
276 if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1)
277 return FALSE;
278 index = ((byte2 - xfs->min_char_or_byte2) +
279 ((byte1 - xfs->min_byte1) *
280 (xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1)));
281 }
282
283 return (xfs->per_char[index].ascent + xfs->per_char[index].descent > 0 ||
284 xfs->per_char[index].width > 0);
285 }
286
287 static unifont *x11font_create(GtkWidget *widget, const char *name,
288 int wide, int bold,
289 int shadowoffset, int shadowalways)
290 {
291 struct x11font *xfont;
292 XFontStruct *xfs;
293 Display *disp = GDK_DISPLAY();
294 Atom charset_registry, charset_encoding, spacing;
295 unsigned long registry_ret, encoding_ret, spacing_ret;
296 int pubcs, realcs, sixteen_bit, variable;
297 int i;
298
299 xfs = XLoadQueryFont(disp, name);
300 if (!xfs)
301 return NULL;
302
303 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
304 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
305
306 pubcs = realcs = CS_NONE;
307 sixteen_bit = FALSE;
308 variable = TRUE;
309
310 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
311 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
312 char *reg, *enc;
313 reg = XGetAtomName(disp, (Atom)registry_ret);
314 enc = XGetAtomName(disp, (Atom)encoding_ret);
315 if (reg && enc) {
316 char *encoding = dupcat(reg, "-", enc, NULL);
317 pubcs = realcs = charset_from_xenc(encoding);
318
319 /*
320 * iso10646-1 is the only wide font encoding we
321 * support. In this case, we expect clients to give us
322 * UTF-8, which this module must internally convert
323 * into 16-bit Unicode.
324 */
325 if (!strcasecmp(encoding, "iso10646-1")) {
326 sixteen_bit = TRUE;
327 pubcs = realcs = CS_UTF8;
328 }
329
330 /*
331 * Hack for X line-drawing characters: if the primary font
332 * is encoded as ISO-8859-1, and has valid glyphs in the
333 * low character positions, it is assumed that those
334 * glyphs are the VT100 line-drawing character set.
335 */
336 if (pubcs == CS_ISO8859_1) {
337 int ch;
338 for (ch = 1; ch < 32; ch++)
339 if (!x11_font_has_glyph(xfs, 0, ch))
340 break;
341 if (ch == 32)
342 realcs = CS_ISO8859_1_X11;
343 }
344
345 sfree(encoding);
346 }
347 }
348
349 spacing = XInternAtom(disp, "SPACING", False);
350 if (XGetFontProperty(xfs, spacing, &spacing_ret)) {
351 char *spc;
352 spc = XGetAtomName(disp, (Atom)spacing_ret);
353
354 if (spc && strchr("CcMm", spc[0]))
355 variable = FALSE;
356 }
357
358 xfont = snew(struct x11font);
359 xfont->u.vt = &x11font_vtable;
360 xfont->u.width = x11_font_width(xfs, sixteen_bit);
361 xfont->u.ascent = xfs->ascent;
362 xfont->u.descent = xfs->descent;
363 xfont->u.height = xfont->u.ascent + xfont->u.descent;
364 xfont->u.public_charset = pubcs;
365 xfont->u.want_fallback = TRUE;
366 xfont->real_charset = realcs;
367 xfont->fonts[0] = xfs;
368 xfont->allocated[0] = TRUE;
369 xfont->sixteen_bit = sixteen_bit;
370 xfont->variable = variable;
371 xfont->wide = wide;
372 xfont->bold = bold;
373 xfont->shadowoffset = shadowoffset;
374 xfont->shadowalways = shadowalways;
375
376 for (i = 1; i < lenof(xfont->fonts); i++) {
377 xfont->fonts[i] = NULL;
378 xfont->allocated[i] = FALSE;
379 }
380
381 return (unifont *)xfont;
382 }
383
384 static void x11font_destroy(unifont *font)
385 {
386 Display *disp = GDK_DISPLAY();
387 struct x11font *xfont = (struct x11font *)font;
388 int i;
389
390 for (i = 0; i < lenof(xfont->fonts); i++)
391 if (xfont->fonts[i])
392 XFreeFont(disp, xfont->fonts[i]);
393 sfree(font);
394 }
395
396 static void x11_alloc_subfont(struct x11font *xfont, int sfid)
397 {
398 Display *disp = GDK_DISPLAY();
399 char *derived_name = x11_guess_derived_font_name
400 (xfont->fonts[0], sfid & 1, !!(sfid & 2));
401 xfont->fonts[sfid] = XLoadQueryFont(disp, derived_name);
402 xfont->allocated[sfid] = TRUE;
403 sfree(derived_name);
404 /* Note that xfont->fonts[sfid] may still be NULL, if XLQF failed. */
405 }
406
407 static int x11font_has_glyph(unifont *font, wchar_t glyph)
408 {
409 struct x11font *xfont = (struct x11font *)font;
410
411 if (xfont->sixteen_bit) {
412 /*
413 * This X font has 16-bit character indices, which means
414 * we can directly use our Unicode input value.
415 */
416 return x11_font_has_glyph(xfont->fonts[0], glyph >> 8, glyph & 0xFF);
417 } else {
418 /*
419 * This X font has 8-bit indices, so we must convert to the
420 * appropriate character set.
421 */
422 char sbstring[2];
423 int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
424 sbstring, 2, "", NULL, NULL);
425 if (!sbstring[0])
426 return FALSE; /* not even in the charset */
427
428 return x11_font_has_glyph(xfont->fonts[0], 0, sbstring[0]);
429 }
430 }
431
432 #if !GTK_CHECK_VERSION(2,0,0)
433 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
434 #endif
435
436 static void x11font_really_draw_text_16(GdkDrawable *target, XFontStruct *xfs,
437 GC gc, int x, int y,
438 const XChar2b *string, int nchars,
439 int shadowoffset,
440 int fontvariable, int cellwidth)
441 {
442 Display *disp = GDK_DISPLAY();
443 int step, nsteps, centre;
444
445 if (fontvariable) {
446 /*
447 * In a variable-pitch font, we draw one character at a
448 * time, and centre it in the character cell.
449 */
450 step = 1;
451 nsteps = nchars;
452 centre = TRUE;
453 } else {
454 /*
455 * In a fixed-pitch font, we can draw the whole lot in one go.
456 */
457 step = nchars;
458 nsteps = 1;
459 centre = FALSE;
460 }
461
462 while (nsteps-- > 0) {
463 int X = x;
464 if (centre)
465 X += (cellwidth - XTextWidth16(xfs, string, step)) / 2;
466
467 XDrawString16(disp, GDK_DRAWABLE_XID(target), gc,
468 X, y, string, step);
469 if (shadowoffset)
470 XDrawString16(disp, GDK_DRAWABLE_XID(target), gc,
471 X + shadowoffset, y, string, step);
472
473 x += cellwidth;
474 string += step;
475 }
476 }
477
478 static void x11font_really_draw_text(GdkDrawable *target, XFontStruct *xfs,
479 GC gc, int x, int y,
480 const char *string, int nchars,
481 int shadowoffset,
482 int fontvariable, int cellwidth)
483 {
484 Display *disp = GDK_DISPLAY();
485 int step, nsteps, centre;
486
487 if (fontvariable) {
488 /*
489 * In a variable-pitch font, we draw one character at a
490 * time, and centre it in the character cell.
491 */
492 step = 1;
493 nsteps = nchars;
494 centre = TRUE;
495 } else {
496 /*
497 * In a fixed-pitch font, we can draw the whole lot in one go.
498 */
499 step = nchars;
500 nsteps = 1;
501 centre = FALSE;
502 }
503
504 while (nsteps-- > 0) {
505 int X = x;
506 if (centre)
507 X += (cellwidth - XTextWidth(xfs, string, step)) / 2;
508
509 XDrawString(disp, GDK_DRAWABLE_XID(target), gc,
510 X, y, string, step);
511 if (shadowoffset)
512 XDrawString(disp, GDK_DRAWABLE_XID(target), gc,
513 X + shadowoffset, y, string, step);
514
515 x += cellwidth;
516 string += step;
517 }
518 }
519
520 static void x11font_draw_text(GdkDrawable *target, GdkGC *gdkgc, unifont *font,
521 int x, int y, const wchar_t *string, int len,
522 int wide, int bold, int cellwidth)
523 {
524 Display *disp = GDK_DISPLAY();
525 struct x11font *xfont = (struct x11font *)font;
526 GC gc = GDK_GC_XGC(gdkgc);
527 int sfid;
528 int shadowoffset = 0;
529 int mult = (wide ? 2 : 1);
530
531 wide -= xfont->wide;
532 bold -= xfont->bold;
533
534 /*
535 * Decide which subfont we're using, and whether we have to
536 * use shadow bold.
537 */
538 if (xfont->shadowalways && bold) {
539 shadowoffset = xfont->shadowoffset;
540 bold = 0;
541 }
542 sfid = 2 * wide + bold;
543 if (!xfont->allocated[sfid])
544 x11_alloc_subfont(xfont, sfid);
545 if (bold && !xfont->fonts[sfid]) {
546 bold = 0;
547 shadowoffset = xfont->shadowoffset;
548 sfid = 2 * wide + bold;
549 if (!xfont->allocated[sfid])
550 x11_alloc_subfont(xfont, sfid);
551 }
552
553 if (!xfont->fonts[sfid])
554 return; /* we've tried our best, but no luck */
555
556 XSetFont(disp, gc, xfont->fonts[sfid]->fid);
557
558 if (xfont->sixteen_bit) {
559 /*
560 * This X font has 16-bit character indices, which means
561 * we can directly use our Unicode input string.
562 */
563 XChar2b *xcs;
564 int i;
565
566 xcs = snewn(len, XChar2b);
567 for (i = 0; i < len; i++) {
568 xcs[i].byte1 = string[i] >> 8;
569 xcs[i].byte2 = string[i];
570 }
571
572 x11font_really_draw_text_16(target, xfont->fonts[sfid], gc, x, y,
573 xcs, len, shadowoffset,
574 xfont->variable, cellwidth * mult);
575 sfree(xcs);
576 } else {
577 /*
578 * This X font has 8-bit indices, so we must convert to the
579 * appropriate character set.
580 */
581 char *sbstring = snewn(len+1, char);
582 int sblen = wc_to_mb(xfont->real_charset, 0, string, len,
583 sbstring, len+1, ".", NULL, NULL);
584 x11font_really_draw_text(target, xfont->fonts[sfid], gc, x, y,
585 sbstring, sblen, shadowoffset,
586 xfont->variable, cellwidth * mult);
587 sfree(sbstring);
588 }
589 }
590
591 static void x11font_enum_fonts(GtkWidget *widget,
592 fontsel_add_entry callback, void *callback_ctx)
593 {
594 char **fontnames;
595 char *tmp = NULL;
596 int nnames, i, max, tmpsize;
597
598 max = 32768;
599 while (1) {
600 fontnames = XListFonts(GDK_DISPLAY(), "*", max, &nnames);
601 if (nnames >= max) {
602 XFreeFontNames(fontnames);
603 max *= 2;
604 } else
605 break;
606 }
607
608 tmpsize = 0;
609
610 for (i = 0; i < nnames; i++) {
611 if (fontnames[i][0] == '-') {
612 /*
613 * Dismember an XLFD and convert it into the format
614 * we'll be using in the font selector.
615 */
616 char *components[14];
617 char *p, *font, *style, *stylekey, *charset;
618 int j, weightkey, slantkey, setwidthkey;
619 int thistmpsize, fontsize, flags;
620
621 thistmpsize = 4 * strlen(fontnames[i]) + 256;
622 if (tmpsize < thistmpsize) {
623 tmpsize = thistmpsize;
624 tmp = sresize(tmp, tmpsize, char);
625 }
626 strcpy(tmp, fontnames[i]);
627
628 p = tmp;
629 for (j = 0; j < 14; j++) {
630 if (*p)
631 *p++ = '\0';
632 components[j] = p;
633 while (*p && *p != '-')
634 p++;
635 }
636 *p++ = '\0';
637
638 /*
639 * Font name is made up of fields 0 and 1, in reverse
640 * order with parentheses. (This is what the GTK 1.2 X
641 * font selector does, and it seems to come out
642 * looking reasonably sensible.)
643 */
644 font = p;
645 p += 1 + sprintf(p, "%s (%s)", components[1], components[0]);
646
647 /*
648 * Charset is made up of fields 12 and 13.
649 */
650 charset = p;
651 p += 1 + sprintf(p, "%s-%s", components[12], components[13]);
652
653 /*
654 * Style is a mixture of quite a lot of the fields,
655 * with some strange formatting.
656 */
657 style = p;
658 p += sprintf(p, "%s", components[2][0] ? components[2] :
659 "regular");
660 if (!g_strcasecmp(components[3], "i"))
661 p += sprintf(p, " italic");
662 else if (!g_strcasecmp(components[3], "o"))
663 p += sprintf(p, " oblique");
664 else if (!g_strcasecmp(components[3], "ri"))
665 p += sprintf(p, " reverse italic");
666 else if (!g_strcasecmp(components[3], "ro"))
667 p += sprintf(p, " reverse oblique");
668 else if (!g_strcasecmp(components[3], "ot"))
669 p += sprintf(p, " other-slant");
670 if (components[4][0] && g_strcasecmp(components[4], "normal"))
671 p += sprintf(p, " %s", components[4]);
672 if (!g_strcasecmp(components[10], "m"))
673 p += sprintf(p, " [M]");
674 if (!g_strcasecmp(components[10], "c"))
675 p += sprintf(p, " [C]");
676 if (components[5][0])
677 p += sprintf(p, " %s", components[5]);
678
679 /*
680 * Style key is the same stuff as above, but with a
681 * couple of transformations done on it to make it
682 * sort more sensibly.
683 */
684 p++;
685 stylekey = p;
686 if (!g_strcasecmp(components[2], "medium") ||
687 !g_strcasecmp(components[2], "regular") ||
688 !g_strcasecmp(components[2], "normal") ||
689 !g_strcasecmp(components[2], "book"))
690 weightkey = 0;
691 else if (!g_strncasecmp(components[2], "demi", 4) ||
692 !g_strncasecmp(components[2], "semi", 4))
693 weightkey = 1;
694 else
695 weightkey = 2;
696 if (!g_strcasecmp(components[3], "r"))
697 slantkey = 0;
698 else if (!g_strncasecmp(components[3], "r", 1))
699 slantkey = 2;
700 else
701 slantkey = 1;
702 if (!g_strcasecmp(components[4], "normal"))
703 setwidthkey = 0;
704 else
705 setwidthkey = 1;
706
707 p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
708 weightkey,
709 (int)strlen(components[2]), components[2],
710 slantkey,
711 (int)strlen(components[3]), components[3],
712 setwidthkey,
713 (int)strlen(components[4]), components[4],
714 (int)strlen(components[10]), components[10],
715 (int)strlen(components[5]), components[5]);
716
717 assert(p - tmp < thistmpsize);
718
719 /*
720 * Size is in pixels, for our application, so we
721 * derive it directly from the pixel size field,
722 * number 6.
723 */
724 fontsize = atoi(components[6]);
725
726 /*
727 * Flags: we need to know whether this is a monospaced
728 * font, which we do by examining the spacing field
729 * again.
730 */
731 flags = FONTFLAG_SERVERSIDE;
732 if (!strchr("CcMm", components[10][0]))
733 flags |= FONTFLAG_NONMONOSPACED;
734
735 /*
736 * Not sure why, but sometimes the X server will
737 * deliver dummy font types in which fontsize comes
738 * out as zero. Filter those out.
739 */
740 if (fontsize)
741 callback(callback_ctx, fontnames[i], font, charset,
742 style, stylekey, fontsize, flags, &x11font_vtable);
743 } else {
744 /*
745 * This isn't an XLFD, so it must be an alias.
746 * Transmit it with mostly null data.
747 *
748 * It would be nice to work out if it's monospaced
749 * here, but at the moment I can't see that being
750 * anything but computationally hideous. Ah well.
751 */
752 callback(callback_ctx, fontnames[i], fontnames[i], NULL,
753 NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);
754 }
755 }
756 XFreeFontNames(fontnames);
757 }
758
759 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
760 int *size, int *flags,
761 int resolve_aliases)
762 {
763 /*
764 * When given an X11 font name to try to make sense of for a
765 * font selector, we must attempt to load it (to see if it
766 * exists), and then canonify it by extracting its FONT
767 * property, which should give its full XLFD even if what we
768 * originally had was a wildcard.
769 *
770 * However, we must carefully avoid canonifying font
771 * _aliases_, unless specifically asked to, because the font
772 * selector treats them as worthwhile in their own right.
773 */
774 XFontStruct *xfs;
775 Display *disp = GDK_DISPLAY();
776 Atom fontprop, fontprop2;
777 unsigned long ret;
778
779 xfs = XLoadQueryFont(disp, name);
780
781 if (!xfs)
782 return NULL; /* didn't make sense to us, sorry */
783
784 fontprop = XInternAtom(disp, "FONT", False);
785
786 if (XGetFontProperty(xfs, fontprop, &ret)) {
787 char *newname = XGetAtomName(disp, (Atom)ret);
788 if (newname) {
789 unsigned long fsize = 12;
790
791 fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);
792 if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {
793 *size = fsize;
794 XFreeFont(disp, xfs);
795 if (flags) {
796 if (name[0] == '-' || resolve_aliases)
797 *flags = FONTFLAG_SERVERSIDE;
798 else
799 *flags = FONTFLAG_SERVERALIAS;
800 }
801 return dupstr(name[0] == '-' || resolve_aliases ?
802 newname : name);
803 }
804 }
805 }
806
807 XFreeFont(disp, xfs);
808
809 return NULL; /* something went wrong */
810 }
811
812 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
813 int size)
814 {
815 return NULL; /* shan't */
816 }
817
818 #if GTK_CHECK_VERSION(2,0,0)
819
820 /* ----------------------------------------------------------------------
821 * Pango font implementation (for GTK 2 only).
822 */
823
824 #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
825 #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
826 #endif
827
828 static int pangofont_has_glyph(unifont *font, wchar_t glyph);
829 static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
830 int x, int y, const wchar_t *string, int len,
831 int wide, int bold, int cellwidth);
832 static unifont *pangofont_create(GtkWidget *widget, const char *name,
833 int wide, int bold,
834 int shadowoffset, int shadowalways);
835 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
836 int wide, int bold,
837 int shadowoffset, int shadowalways);
838 static void pangofont_destroy(unifont *font);
839 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
840 void *callback_ctx);
841 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
842 int *size, int *flags,
843 int resolve_aliases);
844 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
845 int size);
846
847 struct pangofont {
848 struct unifont u;
849 /*
850 * Pango objects.
851 */
852 PangoFontDescription *desc;
853 PangoFontset *fset;
854 /*
855 * The containing widget.
856 */
857 GtkWidget *widget;
858 /*
859 * Data passed in to unifont_create().
860 */
861 int bold, shadowoffset, shadowalways;
862 };
863
864 static const struct unifont_vtable pangofont_vtable = {
865 pangofont_create,
866 pangofont_create_fallback,
867 pangofont_destroy,
868 pangofont_has_glyph,
869 pangofont_draw_text,
870 pangofont_enum_fonts,
871 pangofont_canonify_fontname,
872 pangofont_scale_fontname,
873 "client",
874 };
875
876 /*
877 * This function is used to rigorously validate a
878 * PangoFontDescription. Later versions of Pango have a nasty
879 * habit of accepting _any_ old string as input to
880 * pango_font_description_from_string and returning a font
881 * description which can actually be used to display text, even if
882 * they have to do it by falling back to their most default font.
883 * This is doubtless helpful in some situations, but not here,
884 * because we need to know if a Pango font string actually _makes
885 * sense_ in order to fall back to treating it as an X font name
886 * if it doesn't. So we check that the font family is actually one
887 * supported by Pango.
888 */
889 static int pangofont_check_desc_makes_sense(PangoContext *ctx,
890 PangoFontDescription *desc)
891 {
892 #ifndef PANGO_PRE_1POINT6
893 PangoFontMap *map;
894 #endif
895 PangoFontFamily **families;
896 int i, nfamilies, matched;
897
898 /*
899 * Ask Pango for a list of font families, and iterate through
900 * them to see if one of them matches the family in the
901 * PangoFontDescription.
902 */
903 #ifndef PANGO_PRE_1POINT6
904 map = pango_context_get_font_map(ctx);
905 if (!map)
906 return FALSE;
907 pango_font_map_list_families(map, &families, &nfamilies);
908 #else
909 pango_context_list_families(ctx, &families, &nfamilies);
910 #endif
911
912 matched = FALSE;
913 for (i = 0; i < nfamilies; i++) {
914 if (!g_strcasecmp(pango_font_family_get_name(families[i]),
915 pango_font_description_get_family(desc))) {
916 matched = TRUE;
917 break;
918 }
919 }
920 g_free(families);
921
922 return matched;
923 }
924
925 static unifont *pangofont_create_internal(GtkWidget *widget,
926 PangoContext *ctx,
927 PangoFontDescription *desc,
928 int wide, int bold,
929 int shadowoffset, int shadowalways)
930 {
931 struct pangofont *pfont;
932 #ifndef PANGO_PRE_1POINT6
933 PangoFontMap *map;
934 #endif
935 PangoFontset *fset;
936 PangoFontMetrics *metrics;
937
938 #ifndef PANGO_PRE_1POINT6
939 map = pango_context_get_font_map(ctx);
940 if (!map) {
941 pango_font_description_free(desc);
942 return NULL;
943 }
944 fset = pango_font_map_load_fontset(map, ctx, desc,
945 pango_context_get_language(ctx));
946 #else
947 fset = pango_context_load_fontset(ctx, desc,
948 pango_context_get_language(ctx));
949 #endif
950 if (!fset) {
951 pango_font_description_free(desc);
952 return NULL;
953 }
954 metrics = pango_fontset_get_metrics(fset);
955 if (!metrics ||
956 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
957 pango_font_description_free(desc);
958 g_object_unref(fset);
959 return NULL;
960 }
961
962 pfont = snew(struct pangofont);
963 pfont->u.vt = &pangofont_vtable;
964 pfont->u.width =
965 PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));
966 pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));
967 pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics));
968 pfont->u.height = pfont->u.ascent + pfont->u.descent;
969 pfont->u.want_fallback = FALSE;
970 /* The Pango API is hardwired to UTF-8 */
971 pfont->u.public_charset = CS_UTF8;
972 pfont->desc = desc;
973 pfont->fset = fset;
974 pfont->widget = widget;
975 pfont->bold = bold;
976 pfont->shadowoffset = shadowoffset;
977 pfont->shadowalways = shadowalways;
978
979 pango_font_metrics_unref(metrics);
980
981 return (unifont *)pfont;
982 }
983
984 static unifont *pangofont_create(GtkWidget *widget, const char *name,
985 int wide, int bold,
986 int shadowoffset, int shadowalways)
987 {
988 PangoContext *ctx;
989 PangoFontDescription *desc;
990
991 desc = pango_font_description_from_string(name);
992 if (!desc)
993 return NULL;
994 ctx = gtk_widget_get_pango_context(widget);
995 if (!ctx) {
996 pango_font_description_free(desc);
997 return NULL;
998 }
999 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1000 pango_font_description_free(desc);
1001 return NULL;
1002 }
1003 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1004 shadowoffset, shadowalways);
1005 }
1006
1007 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
1008 int wide, int bold,
1009 int shadowoffset, int shadowalways)
1010 {
1011 PangoContext *ctx;
1012 PangoFontDescription *desc;
1013
1014 desc = pango_font_description_from_string("Monospace");
1015 if (!desc)
1016 return NULL;
1017 ctx = gtk_widget_get_pango_context(widget);
1018 if (!ctx) {
1019 pango_font_description_free(desc);
1020 return NULL;
1021 }
1022 pango_font_description_set_absolute_size(desc, height * PANGO_SCALE);
1023 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1024 shadowoffset, shadowalways);
1025 }
1026
1027 static void pangofont_destroy(unifont *font)
1028 {
1029 struct pangofont *pfont = (struct pangofont *)font;
1030 pango_font_description_free(pfont->desc);
1031 g_object_unref(pfont->fset);
1032 sfree(font);
1033 }
1034
1035 static int pangofont_has_glyph(unifont *font, wchar_t glyph)
1036 {
1037 /* Pango implements font fallback, so assume it has everything */
1038 return TRUE;
1039 }
1040
1041 static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
1042 int x, int y, const wchar_t *string, int len,
1043 int wide, int bold, int cellwidth)
1044 {
1045 struct pangofont *pfont = (struct pangofont *)font;
1046 PangoLayout *layout;
1047 PangoRectangle rect;
1048 char *utfstring, *utfptr;
1049 int utflen;
1050 int shadowbold = FALSE;
1051
1052 if (wide)
1053 cellwidth *= 2;
1054
1055 y -= pfont->u.ascent;
1056
1057 layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));
1058 pango_layout_set_font_description(layout, pfont->desc);
1059 if (bold > pfont->bold) {
1060 if (pfont->shadowalways)
1061 shadowbold = TRUE;
1062 else {
1063 PangoFontDescription *desc2 =
1064 pango_font_description_copy_static(pfont->desc);
1065 pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);
1066 pango_layout_set_font_description(layout, desc2);
1067 }
1068 }
1069
1070 /*
1071 * Pango always expects UTF-8, so convert the input wide character
1072 * string to UTF-8.
1073 */
1074 utfstring = snewn(len*6+1, char); /* UTF-8 has max 6 bytes/char */
1075 utflen = wc_to_mb(CS_UTF8, 0, string, len,
1076 utfstring, len*6+1, ".", NULL, NULL);
1077
1078 utfptr = utfstring;
1079 while (utflen > 0) {
1080 int clen, n;
1081
1082 /*
1083 * We want to display every character from this string in
1084 * the centre of its own character cell. In the worst case,
1085 * this requires a separate text-drawing call for each
1086 * character; but in the common case where the font is
1087 * properly fixed-width, we can draw many characters in one
1088 * go which is much faster.
1089 *
1090 * This still isn't really ideal. If you look at what
1091 * happens in the X protocol as a result of all of this, you
1092 * find - naturally enough - that each call to
1093 * gdk_draw_layout() generates a separate set of X RENDER
1094 * operations involving creating a picture, setting a clip
1095 * rectangle, doing some drawing and undoing the whole lot.
1096 * In an ideal world, we should _always_ be able to turn the
1097 * contents of this loop into a single RenderCompositeGlyphs
1098 * operation which internally specifies inter-character
1099 * deltas to get the spacing right, which would give us full
1100 * speed _even_ in the worst case of a non-fixed-width font.
1101 * However, Pango's architecture and documentation are so
1102 * unhelpful that I have no idea how if at all to persuade
1103 * them to do that.
1104 */
1105
1106 /*
1107 * Start by extracting a single UTF-8 character from the
1108 * string.
1109 */
1110 clen = 1;
1111 while (clen < utflen &&
1112 (unsigned char)utfptr[clen] >= 0x80 &&
1113 (unsigned char)utfptr[clen] < 0xC0)
1114 clen++;
1115 n = 1;
1116
1117 /*
1118 * If it's a right-to-left character, we must display it on
1119 * its own, to stop Pango helpfully re-reversing our already
1120 * reversed text.
1121 */
1122 if (!is_rtl(string[0])) {
1123
1124 /*
1125 * See if that character has the width we expect.
1126 */
1127 pango_layout_set_text(layout, utfptr, clen);
1128 pango_layout_get_pixel_extents(layout, NULL, &rect);
1129
1130 if (rect.width == cellwidth) {
1131 /*
1132 * Try extracting more characters, for as long as they
1133 * stay well-behaved.
1134 */
1135 while (clen < utflen) {
1136 int oldclen = clen;
1137 clen++; /* skip UTF-8 introducer byte */
1138 while (clen < utflen &&
1139 (unsigned char)utfptr[clen] >= 0x80 &&
1140 (unsigned char)utfptr[clen] < 0xC0)
1141 clen++;
1142 n++;
1143 pango_layout_set_text(layout, utfptr, clen);
1144 pango_layout_get_pixel_extents(layout, NULL, &rect);
1145 if (rect.width != n * cellwidth) {
1146 clen = oldclen;
1147 n--;
1148 break;
1149 }
1150 }
1151 }
1152 }
1153
1154 pango_layout_set_text(layout, utfptr, clen);
1155 pango_layout_get_pixel_extents(layout, NULL, &rect);
1156 gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2,
1157 y + (pfont->u.height - rect.height)/2, layout);
1158 if (shadowbold)
1159 gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,
1160 y + (pfont->u.height - rect.height)/2, layout);
1161
1162 utflen -= clen;
1163 utfptr += clen;
1164 string += n;
1165 x += n * cellwidth;
1166 }
1167
1168 sfree(utfstring);
1169
1170 g_object_unref(layout);
1171 }
1172
1173 /*
1174 * Dummy size value to be used when converting a
1175 * PangoFontDescription of a scalable font to a string for
1176 * internal use.
1177 */
1178 #define PANGO_DUMMY_SIZE 12
1179
1180 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
1181 void *callback_ctx)
1182 {
1183 PangoContext *ctx;
1184 #ifndef PANGO_PRE_1POINT6
1185 PangoFontMap *map;
1186 #endif
1187 PangoFontFamily **families;
1188 int i, nfamilies;
1189
1190 ctx = gtk_widget_get_pango_context(widget);
1191 if (!ctx)
1192 return;
1193
1194 /*
1195 * Ask Pango for a list of font families, and iterate through
1196 * them.
1197 */
1198 #ifndef PANGO_PRE_1POINT6
1199 map = pango_context_get_font_map(ctx);
1200 if (!map)
1201 return;
1202 pango_font_map_list_families(map, &families, &nfamilies);
1203 #else
1204 pango_context_list_families(ctx, &families, &nfamilies);
1205 #endif
1206 for (i = 0; i < nfamilies; i++) {
1207 PangoFontFamily *family = families[i];
1208 const char *familyname;
1209 int flags;
1210 PangoFontFace **faces;
1211 int j, nfaces;
1212
1213 /*
1214 * Set up our flags for this font family, and get the name
1215 * string.
1216 */
1217 flags = FONTFLAG_CLIENTSIDE;
1218 #ifndef PANGO_PRE_1POINT4
1219 /*
1220 * In very early versions of Pango, we can't tell
1221 * monospaced fonts from non-monospaced.
1222 */
1223 if (!pango_font_family_is_monospace(family))
1224 flags |= FONTFLAG_NONMONOSPACED;
1225 #endif
1226 familyname = pango_font_family_get_name(family);
1227
1228 /*
1229 * Go through the available font faces in this family.
1230 */
1231 pango_font_family_list_faces(family, &faces, &nfaces);
1232 for (j = 0; j < nfaces; j++) {
1233 PangoFontFace *face = faces[j];
1234 PangoFontDescription *desc;
1235 const char *facename;
1236 int *sizes;
1237 int k, nsizes, dummysize;
1238
1239 /*
1240 * Get the face name string.
1241 */
1242 facename = pango_font_face_get_face_name(face);
1243
1244 /*
1245 * Set up a font description with what we've got so
1246 * far. We'll fill in the size field manually and then
1247 * call pango_font_description_to_string() to give the
1248 * full real name of the specific font.
1249 */
1250 desc = pango_font_face_describe(face);
1251
1252 /*
1253 * See if this font has a list of specific sizes.
1254 */
1255 #ifndef PANGO_PRE_1POINT4
1256 pango_font_face_list_sizes(face, &sizes, &nsizes);
1257 #else
1258 /*
1259 * In early versions of Pango, that call wasn't
1260 * supported; we just have to assume everything is
1261 * scalable.
1262 */
1263 sizes = NULL;
1264 #endif
1265 if (!sizes) {
1266 /*
1267 * Write a single entry with a dummy size.
1268 */
1269 dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;
1270 sizes = &dummysize;
1271 nsizes = 1;
1272 }
1273
1274 /*
1275 * If so, go through them one by one.
1276 */
1277 for (k = 0; k < nsizes; k++) {
1278 char *fullname;
1279 char stylekey[128];
1280
1281 pango_font_description_set_size(desc, sizes[k]);
1282
1283 fullname = pango_font_description_to_string(desc);
1284
1285 /*
1286 * Construct the sorting key for font styles.
1287 */
1288 {
1289 char *p = stylekey;
1290 int n;
1291
1292 n = pango_font_description_get_weight(desc);
1293 /* Weight: normal, then lighter, then bolder */
1294 if (n <= PANGO_WEIGHT_NORMAL)
1295 n = PANGO_WEIGHT_NORMAL - n;
1296 p += sprintf(p, "%4d", n);
1297
1298 n = pango_font_description_get_style(desc);
1299 p += sprintf(p, " %2d", n);
1300
1301 n = pango_font_description_get_stretch(desc);
1302 /* Stretch: closer to normal sorts earlier */
1303 n = 2 * abs(PANGO_STRETCH_NORMAL - n) +
1304 (n < PANGO_STRETCH_NORMAL);
1305 p += sprintf(p, " %2d", n);
1306
1307 n = pango_font_description_get_variant(desc);
1308 p += sprintf(p, " %2d", n);
1309
1310 }
1311
1312 /*
1313 * Got everything. Hand off to the callback.
1314 * (The charset string is NULL, because only
1315 * server-side X fonts use it.)
1316 */
1317 callback(callback_ctx, fullname, familyname, NULL, facename,
1318 stylekey,
1319 (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
1320 flags, &pangofont_vtable);
1321
1322 g_free(fullname);
1323 }
1324 if (sizes != &dummysize)
1325 g_free(sizes);
1326
1327 pango_font_description_free(desc);
1328 }
1329 g_free(faces);
1330 }
1331 g_free(families);
1332 }
1333
1334 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
1335 int *size, int *flags,
1336 int resolve_aliases)
1337 {
1338 /*
1339 * When given a Pango font name to try to make sense of for a
1340 * font selector, we must normalise it to PANGO_DUMMY_SIZE and
1341 * extract its original size (in pixels) into the `size' field.
1342 */
1343 PangoContext *ctx;
1344 #ifndef PANGO_PRE_1POINT6
1345 PangoFontMap *map;
1346 #endif
1347 PangoFontDescription *desc;
1348 PangoFontset *fset;
1349 PangoFontMetrics *metrics;
1350 char *newname, *retname;
1351
1352 desc = pango_font_description_from_string(name);
1353 if (!desc)
1354 return NULL;
1355 ctx = gtk_widget_get_pango_context(widget);
1356 if (!ctx) {
1357 pango_font_description_free(desc);
1358 return NULL;
1359 }
1360 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1361 pango_font_description_free(desc);
1362 return NULL;
1363 }
1364 #ifndef PANGO_PRE_1POINT6
1365 map = pango_context_get_font_map(ctx);
1366 if (!map) {
1367 pango_font_description_free(desc);
1368 return NULL;
1369 }
1370 fset = pango_font_map_load_fontset(map, ctx, desc,
1371 pango_context_get_language(ctx));
1372 #else
1373 fset = pango_context_load_fontset(ctx, desc,
1374 pango_context_get_language(ctx));
1375 #endif
1376 if (!fset) {
1377 pango_font_description_free(desc);
1378 return NULL;
1379 }
1380 metrics = pango_fontset_get_metrics(fset);
1381 if (!metrics ||
1382 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
1383 pango_font_description_free(desc);
1384 g_object_unref(fset);
1385 return NULL;
1386 }
1387
1388 *size = PANGO_PIXELS(pango_font_description_get_size(desc));
1389 *flags = FONTFLAG_CLIENTSIDE;
1390 pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);
1391 newname = pango_font_description_to_string(desc);
1392 retname = dupstr(newname);
1393 g_free(newname);
1394
1395 pango_font_metrics_unref(metrics);
1396 pango_font_description_free(desc);
1397 g_object_unref(fset);
1398
1399 return retname;
1400 }
1401
1402 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
1403 int size)
1404 {
1405 PangoFontDescription *desc;
1406 char *newname, *retname;
1407
1408 desc = pango_font_description_from_string(name);
1409 if (!desc)
1410 return NULL;
1411 pango_font_description_set_size(desc, size * PANGO_SCALE);
1412 newname = pango_font_description_to_string(desc);
1413 retname = dupstr(newname);
1414 g_free(newname);
1415 pango_font_description_free(desc);
1416
1417 return retname;
1418 }
1419
1420 #endif /* GTK_CHECK_VERSION(2,0,0) */
1421
1422 /* ----------------------------------------------------------------------
1423 * Outermost functions which do the vtable dispatch.
1424 */
1425
1426 /*
1427 * Complete list of font-type subclasses. Listed in preference
1428 * order for unifont_create(). (That is, in the extremely unlikely
1429 * event that the same font name is valid as both a Pango and an
1430 * X11 font, it will be interpreted as the former in the absence
1431 * of an explicit type-disambiguating prefix.)
1432 *
1433 * The 'multifont' subclass is omitted here, as discussed above.
1434 */
1435 static const struct unifont_vtable *unifont_types[] = {
1436 #if GTK_CHECK_VERSION(2,0,0)
1437 &pangofont_vtable,
1438 #endif
1439 &x11font_vtable,
1440 };
1441
1442 /*
1443 * Function which takes a font name and processes the optional
1444 * scheme prefix. Returns the tail of the font name suitable for
1445 * passing to individual font scheme functions, and also provides
1446 * a subrange of the unifont_types[] array above.
1447 *
1448 * The return values `start' and `end' denote a half-open interval
1449 * in unifont_types[]; that is, the correct way to iterate over
1450 * them is
1451 *
1452 * for (i = start; i < end; i++) {...}
1453 */
1454 static const char *unifont_do_prefix(const char *name, int *start, int *end)
1455 {
1456 int colonpos = strcspn(name, ":");
1457 int i;
1458
1459 if (name[colonpos]) {
1460 /*
1461 * There's a colon prefix on the font name. Use it to work
1462 * out which subclass to use.
1463 */
1464 for (i = 0; i < lenof(unifont_types); i++) {
1465 if (strlen(unifont_types[i]->prefix) == colonpos &&
1466 !strncmp(unifont_types[i]->prefix, name, colonpos)) {
1467 *start = i;
1468 *end = i+1;
1469 return name + colonpos + 1;
1470 }
1471 }
1472 /*
1473 * None matched, so return an empty scheme list to prevent
1474 * any scheme from being called at all.
1475 */
1476 *start = *end = 0;
1477 return name + colonpos + 1;
1478 } else {
1479 /*
1480 * No colon prefix, so just use all the subclasses.
1481 */
1482 *start = 0;
1483 *end = lenof(unifont_types);
1484 return name;
1485 }
1486 }
1487
1488 unifont *unifont_create(GtkWidget *widget, const char *name, int wide,
1489 int bold, int shadowoffset, int shadowalways)
1490 {
1491 int i, start, end;
1492
1493 name = unifont_do_prefix(name, &start, &end);
1494
1495 for (i = start; i < end; i++) {
1496 unifont *ret = unifont_types[i]->create(widget, name, wide, bold,
1497 shadowoffset, shadowalways);
1498 if (ret)
1499 return ret;
1500 }
1501 return NULL; /* font not found in any scheme */
1502 }
1503
1504 void unifont_destroy(unifont *font)
1505 {
1506 font->vt->destroy(font);
1507 }
1508
1509 void unifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
1510 int x, int y, const wchar_t *string, int len,
1511 int wide, int bold, int cellwidth)
1512 {
1513 font->vt->draw_text(target, gc, font, x, y, string, len,
1514 wide, bold, cellwidth);
1515 }
1516
1517 /* ----------------------------------------------------------------------
1518 * Multiple-font wrapper. This is a type of unifont which encapsulates
1519 * up to two other unifonts, permitting missing glyphs in the main
1520 * font to be filled in by a fallback font.
1521 *
1522 * This is a type of unifont just like the previous two, but it has a
1523 * separate constructor which is manually called by the client, so it
1524 * doesn't appear in the list of available font types enumerated by
1525 * unifont_create. This means it's not used by unifontsel either, so
1526 * it doesn't need to support any methods except draw_text and
1527 * destroy.
1528 */
1529
1530 static void multifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
1531 int x, int y, const wchar_t *string, int len,
1532 int wide, int bold, int cellwidth);
1533 static void multifont_destroy(unifont *font);
1534
1535 struct multifont {
1536 struct unifont u;
1537 unifont *main;
1538 unifont *fallback;
1539 };
1540
1541 static const struct unifont_vtable multifont_vtable = {
1542 NULL, /* creation is done specially */
1543 NULL,
1544 multifont_destroy,
1545 NULL,
1546 multifont_draw_text,
1547 NULL,
1548 NULL,
1549 NULL,
1550 "client",
1551 };
1552
1553 unifont *multifont_create(GtkWidget *widget, const char *name,
1554 int wide, int bold,
1555 int shadowoffset, int shadowalways)
1556 {
1557 int i;
1558 unifont *font, *fallback;
1559 struct multifont *mfont;
1560
1561 font = unifont_create(widget, name, wide, bold,
1562 shadowoffset, shadowalways);
1563 if (!font)
1564 return NULL;
1565
1566 if (font->want_fallback) {
1567 for (i = 0; i < lenof(unifont_types); i++) {
1568 if (unifont_types[i]->create_fallback) {
1569 fallback = unifont_types[i]->create_fallback
1570 (widget, font->height, wide, bold,
1571 shadowoffset, shadowalways);
1572 if (fallback)
1573 break;
1574 }
1575 }
1576 }
1577
1578 /*
1579 * Construct our multifont. Public members are all copied from the
1580 * primary font we're wrapping.
1581 */
1582 mfont = snew(struct multifont);
1583 mfont->u.vt = &multifont_vtable;
1584 mfont->u.width = font->width;
1585 mfont->u.ascent = font->ascent;
1586 mfont->u.descent = font->descent;
1587 mfont->u.height = font->height;
1588 mfont->u.public_charset = font->public_charset;
1589 mfont->u.want_fallback = FALSE; /* shouldn't be needed, but just in case */
1590 mfont->main = font;
1591 mfont->fallback = fallback;
1592
1593 return (unifont *)mfont;
1594 }
1595
1596 static void multifont_destroy(unifont *font)
1597 {
1598 struct multifont *mfont = (struct multifont *)font;
1599 unifont_destroy(mfont->main);
1600 if (mfont->fallback)
1601 unifont_destroy(mfont->fallback);
1602 sfree(font);
1603 }
1604
1605 static void multifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
1606 int x, int y, const wchar_t *string, int len,
1607 int wide, int bold, int cellwidth)
1608 {
1609 struct multifont *mfont = (struct multifont *)font;
1610 int ok, i;
1611
1612 while (len > 0) {
1613 /*
1614 * Find a maximal sequence of characters which are, or are
1615 * not, supported by our main font.
1616 */
1617 ok = mfont->main->vt->has_glyph(mfont->main, string[0]);
1618 for (i = 1;
1619 i < len &&
1620 !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok;
1621 i++);
1622
1623 /*
1624 * Now display it.
1625 */
1626 unifont_draw_text(target, gc, ok ? mfont->main : mfont->fallback,
1627 x, y, string, i, wide, bold, cellwidth);
1628 string += i;
1629 len -= i;
1630 x += i * cellwidth;
1631 }
1632 }
1633
1634 #if GTK_CHECK_VERSION(2,0,0)
1635
1636 /* ----------------------------------------------------------------------
1637 * Implementation of a unified font selector. Used on GTK 2 only;
1638 * for GTK 1 we still use the standard font selector.
1639 */
1640
1641 typedef struct fontinfo fontinfo;
1642
1643 typedef struct unifontsel_internal {
1644 /* This must be the structure's first element, for cross-casting */
1645 unifontsel u;
1646 GtkListStore *family_model, *style_model, *size_model;
1647 GtkWidget *family_list, *style_list, *size_entry, *size_list;
1648 GtkWidget *filter_buttons[4];
1649 GtkWidget *preview_area;
1650 GdkPixmap *preview_pixmap;
1651 int preview_width, preview_height;
1652 GdkColor preview_fg, preview_bg;
1653 int filter_flags;
1654 tree234 *fonts_by_realname, *fonts_by_selorder;
1655 fontinfo *selected;
1656 int selsize, intendedsize;
1657 int inhibit_response; /* inhibit callbacks when we change GUI controls */
1658 } unifontsel_internal;
1659
1660 /*
1661 * The structure held in the tree234s. All the string members are
1662 * part of the same allocated area, so don't need freeing
1663 * separately.
1664 */
1665 struct fontinfo {
1666 char *realname;
1667 char *family, *charset, *style, *stylekey;
1668 int size, flags;
1669 /*
1670 * Fallback sorting key, to permit multiple identical entries
1671 * to exist in the selorder tree.
1672 */
1673 int index;
1674 /*
1675 * Indices mapping fontinfo structures to indices in the list
1676 * boxes. sizeindex is irrelevant if the font is scalable
1677 * (size==0).
1678 */
1679 int familyindex, styleindex, sizeindex;
1680 /*
1681 * The class of font.
1682 */
1683 const struct unifont_vtable *fontclass;
1684 };
1685
1686 struct fontinfo_realname_find {
1687 const char *realname;
1688 int flags;
1689 };
1690
1691 static int strnullcasecmp(const char *a, const char *b)
1692 {
1693 int i;
1694
1695 /*
1696 * If exactly one of the inputs is NULL, it compares before
1697 * the other one.
1698 */
1699 if ((i = (!b) - (!a)) != 0)
1700 return i;
1701
1702 /*
1703 * NULL compares equal.
1704 */
1705 if (!a)
1706 return 0;
1707
1708 /*
1709 * Otherwise, ordinary strcasecmp.
1710 */
1711 return g_strcasecmp(a, b);
1712 }
1713
1714 static int fontinfo_realname_compare(void *av, void *bv)
1715 {
1716 fontinfo *a = (fontinfo *)av;
1717 fontinfo *b = (fontinfo *)bv;
1718 int i;
1719
1720 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
1721 return i;
1722 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
1723 return ((a->flags & FONTFLAG_SORT_MASK) <
1724 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
1725 return 0;
1726 }
1727
1728 static int fontinfo_realname_find(void *av, void *bv)
1729 {
1730 struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;
1731 fontinfo *b = (fontinfo *)bv;
1732 int i;
1733
1734 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
1735 return i;
1736 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
1737 return ((a->flags & FONTFLAG_SORT_MASK) <
1738 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
1739 return 0;
1740 }
1741
1742 static int fontinfo_selorder_compare(void *av, void *bv)
1743 {
1744 fontinfo *a = (fontinfo *)av;
1745 fontinfo *b = (fontinfo *)bv;
1746 int i;
1747 if ((i = strnullcasecmp(a->family, b->family)) != 0)
1748 return i;
1749 /*
1750 * Font class comes immediately after family, so that fonts
1751 * from different classes with the same family
1752 */
1753 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
1754 return ((a->flags & FONTFLAG_SORT_MASK) <
1755 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
1756 if ((i = strnullcasecmp(a->charset, b->charset)) != 0)
1757 return i;
1758 if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)
1759 return i;
1760 if ((i = strnullcasecmp(a->style, b->style)) != 0)
1761 return i;
1762 if (a->size != b->size)
1763 return (a->size < b->size ? -1 : +1);
1764 if (a->index != b->index)
1765 return (a->index < b->index ? -1 : +1);
1766 return 0;
1767 }
1768
1769 static void unifontsel_deselect(unifontsel_internal *fs)
1770 {
1771 fs->selected = NULL;
1772 gtk_list_store_clear(fs->style_model);
1773 gtk_list_store_clear(fs->size_model);
1774 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
1775 gtk_widget_set_sensitive(fs->size_entry, FALSE);
1776 }
1777
1778 static void unifontsel_setup_familylist(unifontsel_internal *fs)
1779 {
1780 GtkTreeIter iter;
1781 int i, listindex, minpos = -1, maxpos = -1;
1782 char *currfamily = NULL;
1783 int currflags = -1;
1784 fontinfo *info;
1785
1786 gtk_list_store_clear(fs->family_model);
1787 listindex = 0;
1788
1789 /*
1790 * Search through the font tree for anything matching our
1791 * current filter criteria. When we find one, add its font
1792 * name to the list box.
1793 */
1794 for (i = 0 ;; i++) {
1795 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
1796 /*
1797 * info may be NULL if we've just run off the end of the
1798 * tree. We must still do a processing pass in that
1799 * situation, in case we had an unfinished font record in
1800 * progress.
1801 */
1802 if (info && (info->flags &~ fs->filter_flags)) {
1803 info->familyindex = -1;
1804 continue; /* we're filtering out this font */
1805 }
1806 if (!info || strnullcasecmp(currfamily, info->family) ||
1807 currflags != (info->flags & FONTFLAG_SORT_MASK)) {
1808 /*
1809 * We've either finished a family, or started a new
1810 * one, or both.
1811 */
1812 if (currfamily) {
1813 gtk_list_store_append(fs->family_model, &iter);
1814 gtk_list_store_set(fs->family_model, &iter,
1815 0, currfamily, 1, minpos, 2, maxpos+1, -1);
1816 listindex++;
1817 }
1818 if (info) {
1819 minpos = i;
1820 currfamily = info->family;
1821 currflags = info->flags & FONTFLAG_SORT_MASK;
1822 }
1823 }
1824 if (!info)
1825 break; /* now we're done */
1826 info->familyindex = listindex;
1827 maxpos = i;
1828 }
1829
1830 /*
1831 * If we've just filtered out the previously selected font,
1832 * deselect it thoroughly.
1833 */
1834 if (fs->selected && fs->selected->familyindex < 0)
1835 unifontsel_deselect(fs);
1836 }
1837
1838 static void unifontsel_setup_stylelist(unifontsel_internal *fs,
1839 int start, int end)
1840 {
1841 GtkTreeIter iter;
1842 int i, listindex, minpos = -1, maxpos = -1, started = FALSE;
1843 char *currcs = NULL, *currstyle = NULL;
1844 fontinfo *info;
1845
1846 gtk_list_store_clear(fs->style_model);
1847 listindex = 0;
1848 started = FALSE;
1849
1850 /*
1851 * Search through the font tree for anything matching our
1852 * current filter criteria. When we find one, add its charset
1853 * and/or style name to the list box.
1854 */
1855 for (i = start; i <= end; i++) {
1856 if (i == end)
1857 info = NULL;
1858 else
1859 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
1860 /*
1861 * info may be NULL if we've just run off the end of the
1862 * relevant data. We must still do a processing pass in
1863 * that situation, in case we had an unfinished font
1864 * record in progress.
1865 */
1866 if (info && (info->flags &~ fs->filter_flags)) {
1867 info->styleindex = -1;
1868 continue; /* we're filtering out this font */
1869 }
1870 if (!info || !started || strnullcasecmp(currcs, info->charset) ||
1871 strnullcasecmp(currstyle, info->style)) {
1872 /*
1873 * We've either finished a style/charset, or started a
1874 * new one, or both.
1875 */
1876 started = TRUE;
1877 if (currstyle) {
1878 gtk_list_store_append(fs->style_model, &iter);
1879 gtk_list_store_set(fs->style_model, &iter,
1880 0, currstyle, 1, minpos, 2, maxpos+1,
1881 3, TRUE, -1);
1882 listindex++;
1883 }
1884 if (info) {
1885 minpos = i;
1886 if (info->charset && strnullcasecmp(currcs, info->charset)) {
1887 gtk_list_store_append(fs->style_model, &iter);
1888 gtk_list_store_set(fs->style_model, &iter,
1889 0, info->charset, 1, -1, 2, -1,
1890 3, FALSE, -1);
1891 listindex++;
1892 }
1893 currcs = info->charset;
1894 currstyle = info->style;
1895 }
1896 }
1897 if (!info)
1898 break; /* now we're done */
1899 info->styleindex = listindex;
1900 maxpos = i;
1901 }
1902 }
1903
1904 static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };
1905
1906 static void unifontsel_setup_sizelist(unifontsel_internal *fs,
1907 int start, int end)
1908 {
1909 GtkTreeIter iter;
1910 int i, listindex;
1911 char sizetext[40];
1912 fontinfo *info;
1913
1914 gtk_list_store_clear(fs->size_model);
1915 listindex = 0;
1916
1917 /*
1918 * Search through the font tree for anything matching our
1919 * current filter criteria. When we find one, add its font
1920 * name to the list box.
1921 */
1922 for (i = start; i < end; i++) {
1923 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
1924 if (info->flags &~ fs->filter_flags) {
1925 info->sizeindex = -1;
1926 continue; /* we're filtering out this font */
1927 }
1928 if (info->size) {
1929 sprintf(sizetext, "%d", info->size);
1930 info->sizeindex = listindex;
1931 gtk_list_store_append(fs->size_model, &iter);
1932 gtk_list_store_set(fs->size_model, &iter,
1933 0, sizetext, 1, i, 2, info->size, -1);
1934 listindex++;
1935 } else {
1936 int j;
1937
1938 assert(i == start);
1939 assert(i+1 == end);
1940
1941 for (j = 0; j < lenof(unifontsel_default_sizes); j++) {
1942 sprintf(sizetext, "%d", unifontsel_default_sizes[j]);
1943 gtk_list_store_append(fs->size_model, &iter);
1944 gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,
1945 2, unifontsel_default_sizes[j], -1);
1946 listindex++;
1947 }
1948 }
1949 }
1950 }
1951
1952 static void unifontsel_set_filter_buttons(unifontsel_internal *fs)
1953 {
1954 int i;
1955
1956 for (i = 0; i < lenof(fs->filter_buttons); i++) {
1957 int flagbit = GPOINTER_TO_INT(gtk_object_get_data
1958 (GTK_OBJECT(fs->filter_buttons[i]),
1959 "user-data"));
1960 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),
1961 !!(fs->filter_flags & flagbit));
1962 }
1963 }
1964
1965 static void unifontsel_draw_preview_text(unifontsel_internal *fs)
1966 {
1967 unifont *font;
1968 char *sizename = NULL;
1969 fontinfo *info = fs->selected;
1970
1971 if (info) {
1972 sizename = info->fontclass->scale_fontname
1973 (GTK_WIDGET(fs->u.window), info->realname, fs->selsize);
1974 font = info->fontclass->create(GTK_WIDGET(fs->u.window),
1975 sizename ? sizename : info->realname,
1976 FALSE, FALSE, 0, 0);
1977 } else
1978 font = NULL;
1979
1980 if (fs->preview_pixmap) {
1981 GdkGC *gc = gdk_gc_new(fs->preview_pixmap);
1982 gdk_gc_set_foreground(gc, &fs->preview_bg);
1983 gdk_draw_rectangle(fs->preview_pixmap, gc, 1, 0, 0,
1984 fs->preview_width, fs->preview_height);
1985 gdk_gc_set_foreground(gc, &fs->preview_fg);
1986 if (font) {
1987 /*
1988 * The pangram used here is rather carefully
1989 * constructed: it contains a sequence of very narrow
1990 * letters (`jil') and a pair of adjacent very wide
1991 * letters (`wm').
1992 *
1993 * If the user selects a proportional font, it will be
1994 * coerced into fixed-width character cells when used
1995 * in the actual terminal window. We therefore display
1996 * it the same way in the preview pane, so as to show
1997 * it the way it will actually be displayed - and we
1998 * deliberately pick a pangram which will show the
1999 * resulting miskerning at its worst.
2000 *
2001 * We aren't trying to sell people these fonts; we're
2002 * trying to let them make an informed choice. Better
2003 * that they find out the problems with using
2004 * proportional fonts in terminal windows here than
2005 * that they go to the effort of selecting their font
2006 * and _then_ realise it was a mistake.
2007 */
2008 info->fontclass->draw_text(fs->preview_pixmap, gc, font,
2009 0, font->ascent,
2010 L"bankrupt jilted showmen quiz convex fogey",
2011 41, FALSE, FALSE, font->width);
2012 info->fontclass->draw_text(fs->preview_pixmap, gc, font,
2013 0, font->ascent + font->height,
2014 L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
2015 41, FALSE, FALSE, font->width);
2016 /*
2017 * The ordering of punctuation here is also selected
2018 * with some specific aims in mind. I put ` and '
2019 * together because some software (and people) still
2020 * use them as matched quotes no matter what Unicode
2021 * might say on the matter, so people can quickly
2022 * check whether they look silly in a candidate font.
2023 * The sequence #_@ is there to let people judge the
2024 * suitability of the underscore as an effectively
2025 * alphabetic character (since that's how it's often
2026 * used in practice, at least by programmers).
2027 */
2028 info->fontclass->draw_text(fs->preview_pixmap, gc, font,
2029 0, font->ascent + font->height * 2,
2030 L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
2031 42, FALSE, FALSE, font->width);
2032 }
2033 gdk_gc_unref(gc);
2034 gdk_window_invalidate_rect(fs->preview_area->window, NULL, FALSE);
2035 }
2036 if (font)
2037 info->fontclass->destroy(font);
2038
2039 sfree(sizename);
2040 }
2041
2042 static void unifontsel_select_font(unifontsel_internal *fs,
2043 fontinfo *info, int size, int leftlist,
2044 int size_is_explicit)
2045 {
2046 int index;
2047 int minval, maxval;
2048 GtkTreePath *treepath;
2049 GtkTreeIter iter;
2050
2051 fs->inhibit_response = TRUE;
2052
2053 fs->selected = info;
2054 fs->selsize = size;
2055 if (size_is_explicit)
2056 fs->intendedsize = size;
2057
2058 gtk_widget_set_sensitive(fs->u.ok_button, TRUE);
2059
2060 /*
2061 * Find the index of this fontinfo in the selorder list.
2062 */
2063 index = -1;
2064 findpos234(fs->fonts_by_selorder, info, NULL, &index);
2065 assert(index >= 0);
2066
2067 /*
2068 * Adjust the font selector flags and redo the font family
2069 * list box, if necessary.
2070 */
2071 if (leftlist <= 0 &&
2072 (fs->filter_flags | info->flags) != fs->filter_flags) {
2073 fs->filter_flags |= info->flags;
2074 unifontsel_set_filter_buttons(fs);
2075 unifontsel_setup_familylist(fs);
2076 }
2077
2078 /*
2079 * Find the appropriate family name and select it in the list.
2080 */
2081 assert(info->familyindex >= 0);
2082 treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);
2083 gtk_tree_selection_select_path
2084 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),
2085 treepath);
2086 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),
2087 treepath, NULL, FALSE, 0.0, 0.0);
2088 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, treepath);
2089 gtk_tree_path_free(treepath);
2090
2091 /*
2092 * Now set up the font style list.
2093 */
2094 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,
2095 1, &minval, 2, &maxval, -1);
2096 if (leftlist <= 1)
2097 unifontsel_setup_stylelist(fs, minval, maxval);
2098
2099 /*
2100 * Find the appropriate style name and select it in the list.
2101 */
2102 if (info->style) {
2103 assert(info->styleindex >= 0);
2104 treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);
2105 gtk_tree_selection_select_path
2106 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),
2107 treepath);
2108 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),
2109 treepath, NULL, FALSE, 0.0, 0.0);
2110 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),
2111 &iter, treepath);
2112 gtk_tree_path_free(treepath);
2113
2114 /*
2115 * And set up the size list.
2116 */
2117 gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,
2118 1, &minval, 2, &maxval, -1);
2119 if (leftlist <= 2)
2120 unifontsel_setup_sizelist(fs, minval, maxval);
2121
2122 /*
2123 * Find the appropriate size, and select it in the list.
2124 */
2125 if (info->size) {
2126 assert(info->sizeindex >= 0);
2127 treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);
2128 gtk_tree_selection_select_path
2129 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),
2130 treepath);
2131 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2132 treepath, NULL, FALSE, 0.0, 0.0);
2133 gtk_tree_path_free(treepath);
2134 size = info->size;
2135 } else {
2136 int j;
2137 for (j = 0; j < lenof(unifontsel_default_sizes); j++)
2138 if (unifontsel_default_sizes[j] == size) {
2139 treepath = gtk_tree_path_new_from_indices(j, -1);
2140 gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),
2141 treepath, NULL, FALSE);
2142 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2143 treepath, NULL, FALSE, 0.0,
2144 0.0);
2145 gtk_tree_path_free(treepath);
2146 }
2147 }
2148
2149 /*
2150 * And set up the font size text entry box.
2151 */
2152 {
2153 char sizetext[40];
2154 sprintf(sizetext, "%d", size);
2155 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);
2156 }
2157 } else {
2158 if (leftlist <= 2)
2159 unifontsel_setup_sizelist(fs, 0, 0);
2160 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");
2161 }
2162
2163 /*
2164 * Grey out the font size edit box if we're not using a
2165 * scalable font.
2166 */
2167 gtk_entry_set_editable(GTK_ENTRY(fs->size_entry), fs->selected->size == 0);
2168 gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);
2169
2170 unifontsel_draw_preview_text(fs);
2171
2172 fs->inhibit_response = FALSE;
2173 }
2174
2175 static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)
2176 {
2177 unifontsel_internal *fs = (unifontsel_internal *)data;
2178 int newstate = gtk_toggle_button_get_active(tb);
2179 int newflags;
2180 int flagbit = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(tb),
2181 "user-data"));
2182
2183 if (newstate)
2184 newflags = fs->filter_flags | flagbit;
2185 else
2186 newflags = fs->filter_flags & ~flagbit;
2187
2188 if (fs->filter_flags != newflags) {
2189 fs->filter_flags = newflags;
2190 unifontsel_setup_familylist(fs);
2191 }
2192 }
2193
2194 static void unifontsel_add_entry(void *ctx, const char *realfontname,
2195 const char *family, const char *charset,
2196 const char *style, const char *stylekey,
2197 int size, int flags,
2198 const struct unifont_vtable *fontclass)
2199 {
2200 unifontsel_internal *fs = (unifontsel_internal *)ctx;
2201 fontinfo *info;
2202 int totalsize;
2203 char *p;
2204
2205 totalsize = sizeof(fontinfo) + strlen(realfontname) +
2206 (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +
2207 (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;
2208 info = (fontinfo *)smalloc(totalsize);
2209 info->fontclass = fontclass;
2210 p = (char *)info + sizeof(fontinfo);
2211 info->realname = p;
2212 strcpy(p, realfontname);
2213 p += 1+strlen(p);
2214 if (family) {
2215 info->family = p;
2216 strcpy(p, family);
2217 p += 1+strlen(p);
2218 } else
2219 info->family = NULL;
2220 if (charset) {
2221 info->charset = p;
2222 strcpy(p, charset);
2223 p += 1+strlen(p);
2224 } else
2225 info->charset = NULL;
2226 if (style) {
2227 info->style = p;
2228 strcpy(p, style);
2229 p += 1+strlen(p);
2230 } else
2231 info->style = NULL;
2232 if (stylekey) {
2233 info->stylekey = p;
2234 strcpy(p, stylekey);
2235 p += 1+strlen(p);
2236 } else
2237 info->stylekey = NULL;
2238 assert(p - (char *)info <= totalsize);
2239 info->size = size;
2240 info->flags = flags;
2241 info->index = count234(fs->fonts_by_selorder);
2242
2243 /*
2244 * It's just conceivable that a misbehaving font enumerator
2245 * might tell us about the same font real name more than once,
2246 * in which case we should silently drop the new one.
2247 */
2248 if (add234(fs->fonts_by_realname, info) != info) {
2249 sfree(info);
2250 return;
2251 }
2252 /*
2253 * However, we should never get a duplicate key in the
2254 * selorder tree, because the index field carefully
2255 * disambiguates otherwise identical records.
2256 */
2257 add234(fs->fonts_by_selorder, info);
2258 }
2259
2260 static fontinfo *update_for_intended_size(unifontsel_internal *fs,
2261 fontinfo *info)
2262 {
2263 fontinfo info2, *below, *above;
2264 int pos;
2265
2266 /*
2267 * Copy the info structure. This doesn't copy its dynamic
2268 * string fields, but that's unimportant because all we're
2269 * going to do is to adjust the size field and use it in one
2270 * tree search.
2271 */
2272 info2 = *info;
2273 info2.size = fs->intendedsize;
2274
2275 /*
2276 * Search in the tree to find the fontinfo structure which
2277 * best approximates the size the user last requested.
2278 */
2279 below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
2280 REL234_LE, &pos);
2281 above = index234(fs->fonts_by_selorder, pos+1);
2282
2283 /*
2284 * See if we've found it exactly, which is an easy special
2285 * case. If we have, it'll be in `below' and not `above',
2286 * because we did a REL234_LE rather than REL234_LT search.
2287 */
2288 if (!fontinfo_selorder_compare(&info2, below))
2289 return below;
2290
2291 /*
2292 * Now we've either found two suitable fonts, one smaller and
2293 * one larger, or we're at one or other extreme end of the
2294 * scale. Find out which, by NULLing out either of below and
2295 * above if it differs from this one in any respect but size
2296 * (and the disambiguating index field). Bear in mind, also,
2297 * that either one might _already_ be NULL if we're at the
2298 * extreme ends of the font list.
2299 */
2300 if (below) {
2301 info2.size = below->size;
2302 info2.index = below->index;
2303 if (fontinfo_selorder_compare(&info2, below))
2304 below = NULL;
2305 }
2306 if (above) {
2307 info2.size = above->size;
2308 info2.index = above->index;
2309 if (fontinfo_selorder_compare(&info2, above))
2310 above = NULL;
2311 }
2312
2313 /*
2314 * Now return whichever of above and below is non-NULL, if
2315 * that's unambiguous.
2316 */
2317 if (!above)
2318 return below;
2319 if (!below)
2320 return above;
2321
2322 /*
2323 * And now we really do have to make a choice about whether to
2324 * round up or down. We'll do it by rounding to nearest,
2325 * breaking ties by rounding up.
2326 */
2327 if (above->size - fs->intendedsize <= fs->intendedsize - below->size)
2328 return above;
2329 else
2330 return below;
2331 }
2332
2333 static void family_changed(GtkTreeSelection *treeselection, gpointer data)
2334 {
2335 unifontsel_internal *fs = (unifontsel_internal *)data;
2336 GtkTreeModel *treemodel;
2337 GtkTreeIter treeiter;
2338 int minval;
2339 fontinfo *info;
2340
2341 if (fs->inhibit_response) /* we made this change ourselves */
2342 return;
2343
2344 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2345 return;
2346
2347 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2348 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2349 info = update_for_intended_size(fs, info);
2350 if (!info)
2351 return; /* _shouldn't_ happen unless font list is completely funted */
2352 if (!info->size)
2353 fs->selsize = fs->intendedsize; /* font is scalable */
2354 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2355 1, FALSE);
2356 }
2357
2358 static void style_changed(GtkTreeSelection *treeselection, gpointer data)
2359 {
2360 unifontsel_internal *fs = (unifontsel_internal *)data;
2361 GtkTreeModel *treemodel;
2362 GtkTreeIter treeiter;
2363 int minval;
2364 fontinfo *info;
2365
2366 if (fs->inhibit_response) /* we made this change ourselves */
2367 return;
2368
2369 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2370 return;
2371
2372 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2373 if (minval < 0)
2374 return; /* somehow a charset heading got clicked */
2375 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2376 info = update_for_intended_size(fs, info);
2377 if (!info)
2378 return; /* _shouldn't_ happen unless font list is completely funted */
2379 if (!info->size)
2380 fs->selsize = fs->intendedsize; /* font is scalable */
2381 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2382 2, FALSE);
2383 }
2384
2385 static void size_changed(GtkTreeSelection *treeselection, gpointer data)
2386 {
2387 unifontsel_internal *fs = (unifontsel_internal *)data;
2388 GtkTreeModel *treemodel;
2389 GtkTreeIter treeiter;
2390 int minval, size;
2391 fontinfo *info;
2392
2393 if (fs->inhibit_response) /* we made this change ourselves */
2394 return;
2395
2396 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2397 return;
2398
2399 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);
2400 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2401 unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE);
2402 }
2403
2404 static void size_entry_changed(GtkEditable *ed, gpointer data)
2405 {
2406 unifontsel_internal *fs = (unifontsel_internal *)data;
2407 const char *text;
2408 int size;
2409
2410 if (fs->inhibit_response) /* we made this change ourselves */
2411 return;
2412
2413 text = gtk_entry_get_text(GTK_ENTRY(ed));
2414 size = atoi(text);
2415
2416 if (size > 0) {
2417 assert(fs->selected->size == 0);
2418 unifontsel_select_font(fs, fs->selected, size, 3, TRUE);
2419 }
2420 }
2421
2422 static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,
2423 GtkTreeViewColumn *column, gpointer data)
2424 {
2425 unifontsel_internal *fs = (unifontsel_internal *)data;
2426 GtkTreeIter iter;
2427 int minval, newsize;
2428 fontinfo *info, *newinfo;
2429 char *newname;
2430
2431 if (fs->inhibit_response) /* we made this change ourselves */
2432 return;
2433
2434 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);
2435 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);
2436 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2437 if (info) {
2438 int flags;
2439 struct fontinfo_realname_find f;
2440
2441 newname = info->fontclass->canonify_fontname
2442 (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE);
2443
2444 f.realname = newname;
2445 f.flags = flags;
2446 newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
2447
2448 sfree(newname);
2449 if (!newinfo)
2450 return; /* font name not in our index */
2451 if (newinfo == info)
2452 return; /* didn't change under canonification => not an alias */
2453 unifontsel_select_font(fs, newinfo,
2454 newinfo->size ? newinfo->size : newsize,
2455 1, TRUE);
2456 }
2457 }
2458
2459 static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,
2460 gpointer data)
2461 {
2462 unifontsel_internal *fs = (unifontsel_internal *)data;
2463
2464 if (fs->preview_pixmap) {
2465 gdk_draw_pixmap(widget->window,
2466 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
2467 fs->preview_pixmap,
2468 event->area.x, event->area.y,
2469 event->area.x, event->area.y,
2470 event->area.width, event->area.height);
2471 }
2472 return TRUE;
2473 }
2474
2475 static gint unifontsel_configure_area(GtkWidget *widget,
2476 GdkEventConfigure *event, gpointer data)
2477 {
2478 unifontsel_internal *fs = (unifontsel_internal *)data;
2479 int ox, oy, nx, ny, x, y;
2480
2481 /*
2482 * Enlarge the pixmap, but never shrink it.
2483 */
2484 ox = fs->preview_width;
2485 oy = fs->preview_height;
2486 x = event->width;
2487 y = event->height;
2488 if (x > ox || y > oy) {
2489 if (fs->preview_pixmap)
2490 gdk_pixmap_unref(fs->preview_pixmap);
2491
2492 nx = (x > ox ? x : ox);
2493 ny = (y > oy ? y : oy);
2494 fs->preview_pixmap = gdk_pixmap_new(widget->window, nx, ny, -1);
2495 fs->preview_width = nx;
2496 fs->preview_height = ny;
2497
2498 unifontsel_draw_preview_text(fs);
2499 }
2500
2501 gdk_window_invalidate_rect(widget->window, NULL, FALSE);
2502
2503 return TRUE;
2504 }
2505
2506 unifontsel *unifontsel_new(const char *wintitle)
2507 {
2508 unifontsel_internal *fs = snew(unifontsel_internal);
2509 GtkWidget *table, *label, *w, *ww, *scroll;
2510 GtkListStore *model;
2511 GtkTreeViewColumn *column;
2512 int lists_height, preview_height, font_width, style_width, size_width;
2513 int i;
2514
2515 fs->inhibit_response = FALSE;
2516 fs->selected = NULL;
2517
2518 {
2519 /*
2520 * Invent some magic size constants.
2521 */
2522 GtkRequisition req;
2523 label = gtk_label_new("Quite Long Font Name (Foundry)");
2524 gtk_widget_size_request(label, &req);
2525 font_width = req.width;
2526 lists_height = 14 * req.height;
2527 preview_height = 5 * req.height;
2528 gtk_label_set_text(GTK_LABEL(label), "Italic Extra Condensed");
2529 gtk_widget_size_request(label, &req);
2530 style_width = req.width;
2531 gtk_label_set_text(GTK_LABEL(label), "48000");
2532 gtk_widget_size_request(label, &req);
2533 size_width = req.width;
2534 #if GTK_CHECK_VERSION(2,10,0)
2535 g_object_ref_sink(label);
2536 g_object_unref(label);
2537 #else
2538 gtk_object_sink(GTK_OBJECT(label));
2539 #endif
2540 }
2541
2542 /*
2543 * Create the dialog box and initialise the user-visible
2544 * fields in the returned structure.
2545 */
2546 fs->u.user_data = NULL;
2547 fs->u.window = GTK_WINDOW(gtk_dialog_new());
2548 gtk_window_set_title(fs->u.window, wintitle);
2549 fs->u.cancel_button = gtk_dialog_add_button
2550 (GTK_DIALOG(fs->u.window), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
2551 fs->u.ok_button = gtk_dialog_add_button
2552 (GTK_DIALOG(fs->u.window), GTK_STOCK_OK, GTK_RESPONSE_OK);
2553 gtk_widget_grab_default(fs->u.ok_button);
2554
2555 /*
2556 * Now set up the internal fields, including in particular all
2557 * the controls that actually allow the user to select fonts.
2558 */
2559 table = gtk_table_new(8, 3, FALSE);
2560 gtk_widget_show(table);
2561 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
2562 #if GTK_CHECK_VERSION(2,4,0)
2563 /* GtkAlignment seems to be the simplest way to put padding round things */
2564 w = gtk_alignment_new(0, 0, 1, 1);
2565 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
2566 gtk_container_add(GTK_CONTAINER(w), table);
2567 gtk_widget_show(w);
2568 #else
2569 w = table;
2570 #endif
2571 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fs->u.window)->vbox),
2572 w, TRUE, TRUE, 0);
2573
2574 label = gtk_label_new_with_mnemonic("_Font:");
2575 gtk_widget_show(label);
2576 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
2577 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
2578
2579 /*
2580 * The Font list box displays only a string, but additionally
2581 * stores two integers which give the limits within the
2582 * tree234 of the font entries covered by this list entry.
2583 */
2584 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
2585 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
2586 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2587 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
2588 gtk_widget_show(w);
2589 column = gtk_tree_view_column_new_with_attributes
2590 ("Font", gtk_cell_renderer_text_new(),
2591 "text", 0, (char *)NULL);
2592 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
2593 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2594 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
2595 "changed", G_CALLBACK(family_changed), fs);
2596 g_signal_connect(G_OBJECT(w), "row-activated",
2597 G_CALLBACK(alias_resolve), fs);
2598
2599 scroll = gtk_scrolled_window_new(NULL, NULL);
2600 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
2601 GTK_SHADOW_IN);
2602 gtk_container_add(GTK_CONTAINER(scroll), w);
2603 gtk_widget_show(scroll);
2604 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2605 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2606 gtk_widget_set_size_request(scroll, font_width, lists_height);
2607 gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,
2608 GTK_EXPAND | GTK_FILL, 0, 0);
2609 fs->family_model = model;
2610 fs->family_list = w;
2611
2612 label = gtk_label_new_with_mnemonic("_Style:");
2613 gtk_widget_show(label);
2614 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
2615 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
2616
2617 /*
2618 * The Style list box can contain insensitive elements
2619 * (character set headings for server-side fonts), so we add
2620 * an extra column to the list store to hold that information.
2621 */
2622 model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
2623 G_TYPE_BOOLEAN);
2624 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
2625 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2626 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
2627 gtk_widget_show(w);
2628 column = gtk_tree_view_column_new_with_attributes
2629 ("Style", gtk_cell_renderer_text_new(),
2630 "text", 0, "sensitive", 3, (char *)NULL);
2631 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
2632 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2633 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
2634 "changed", G_CALLBACK(style_changed), fs);
2635
2636 scroll = gtk_scrolled_window_new(NULL, NULL);
2637 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
2638 GTK_SHADOW_IN);
2639 gtk_container_add(GTK_CONTAINER(scroll), w);
2640 gtk_widget_show(scroll);
2641 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2642 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2643 gtk_widget_set_size_request(scroll, style_width, lists_height);
2644 gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,
2645 GTK_EXPAND | GTK_FILL, 0, 0);
2646 fs->style_model = model;
2647 fs->style_list = w;
2648
2649 label = gtk_label_new_with_mnemonic("Si_ze:");
2650 gtk_widget_show(label);
2651 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
2652 gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
2653
2654 /*
2655 * The Size label attaches primarily to a text input box so
2656 * that the user can select a size of their choice. The list
2657 * of available sizes is secondary.
2658 */
2659 fs->size_entry = w = gtk_entry_new();
2660 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
2661 gtk_widget_set_size_request(w, size_width, -1);
2662 gtk_widget_show(w);
2663 gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);
2664 g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),
2665 fs);
2666
2667 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
2668 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
2669 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2670 gtk_widget_show(w);
2671 column = gtk_tree_view_column_new_with_attributes
2672 ("Size", gtk_cell_renderer_text_new(),
2673 "text", 0, (char *)NULL);
2674 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
2675 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
2676 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
2677 "changed", G_CALLBACK(size_changed), fs);
2678
2679 scroll = gtk_scrolled_window_new(NULL, NULL);
2680 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
2681 GTK_SHADOW_IN);
2682 gtk_container_add(GTK_CONTAINER(scroll), w);
2683 gtk_widget_show(scroll);
2684 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
2685 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2686 gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,
2687 GTK_EXPAND | GTK_FILL, 0, 0);
2688 fs->size_model = model;
2689 fs->size_list = w;
2690
2691 /*
2692 * Preview widget.
2693 */
2694 fs->preview_area = gtk_drawing_area_new();
2695 fs->preview_pixmap = NULL;
2696 fs->preview_width = 0;
2697 fs->preview_height = 0;
2698 fs->preview_fg.pixel = fs->preview_bg.pixel = 0;
2699 fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;
2700 fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;
2701 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,
2702 FALSE, FALSE);
2703 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,
2704 FALSE, FALSE);
2705 gtk_signal_connect(GTK_OBJECT(fs->preview_area), "expose_event",
2706 GTK_SIGNAL_FUNC(unifontsel_expose_area), fs);
2707 gtk_signal_connect(GTK_OBJECT(fs->preview_area), "configure_event",
2708 GTK_SIGNAL_FUNC(unifontsel_configure_area), fs);
2709 gtk_widget_set_size_request(fs->preview_area, 1, preview_height);
2710 gtk_widget_show(fs->preview_area);
2711 ww = fs->preview_area;
2712 w = gtk_frame_new(NULL);
2713 gtk_container_add(GTK_CONTAINER(w), ww);
2714 gtk_widget_show(w);
2715 #if GTK_CHECK_VERSION(2,4,0)
2716 ww = w;
2717 /* GtkAlignment seems to be the simplest way to put padding round things */
2718 w = gtk_alignment_new(0, 0, 1, 1);
2719 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
2720 gtk_container_add(GTK_CONTAINER(w), ww);
2721 gtk_widget_show(w);
2722 #endif
2723 ww = w;
2724 w = gtk_frame_new("Preview of font");
2725 gtk_container_add(GTK_CONTAINER(w), ww);
2726 gtk_widget_show(w);
2727 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,
2728 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);
2729
2730 i = 0;
2731 w = gtk_check_button_new_with_label("Show client-side fonts");
2732 gtk_object_set_data(GTK_OBJECT(w), "user-data",
2733 GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));
2734 gtk_signal_connect(GTK_OBJECT(w), "toggled",
2735 GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);
2736 gtk_widget_show(w);
2737 fs->filter_buttons[i++] = w;
2738 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);
2739 w = gtk_check_button_new_with_label("Show server-side fonts");
2740 gtk_object_set_data(GTK_OBJECT(w), "user-data",
2741 GINT_TO_POINTER(FONTFLAG_SERVERSIDE));
2742 gtk_signal_connect(GTK_OBJECT(w), "toggled",
2743 GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);
2744 gtk_widget_show(w);
2745 fs->filter_buttons[i++] = w;
2746 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);
2747 w = gtk_check_button_new_with_label("Show server-side font aliases");
2748 gtk_object_set_data(GTK_OBJECT(w), "user-data",
2749 GINT_TO_POINTER(FONTFLAG_SERVERALIAS));
2750 gtk_signal_connect(GTK_OBJECT(w), "toggled",
2751 GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);
2752 gtk_widget_show(w);
2753 fs->filter_buttons[i++] = w;
2754 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);
2755 w = gtk_check_button_new_with_label("Show non-monospaced fonts");
2756 gtk_object_set_data(GTK_OBJECT(w), "user-data",
2757 GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));
2758 gtk_signal_connect(GTK_OBJECT(w), "toggled",
2759 GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);
2760 gtk_widget_show(w);
2761 fs->filter_buttons[i++] = w;
2762 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);
2763
2764 assert(i == lenof(fs->filter_buttons));
2765 fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |
2766 FONTFLAG_SERVERALIAS;
2767 unifontsel_set_filter_buttons(fs);
2768
2769 /*
2770 * Go and find all the font names, and set up our master font
2771 * list.
2772 */
2773 fs->fonts_by_realname = newtree234(fontinfo_realname_compare);
2774 fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);
2775 for (i = 0; i < lenof(unifont_types); i++)
2776 unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),
2777 unifontsel_add_entry, fs);
2778
2779 /*
2780 * And set up the initial font names list.
2781 */
2782 unifontsel_setup_familylist(fs);
2783
2784 fs->selsize = fs->intendedsize = 13; /* random default */
2785 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
2786
2787 return (unifontsel *)fs;
2788 }
2789
2790 void unifontsel_destroy(unifontsel *fontsel)
2791 {
2792 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
2793 fontinfo *info;
2794
2795 if (fs->preview_pixmap)
2796 gdk_pixmap_unref(fs->preview_pixmap);
2797
2798 freetree234(fs->fonts_by_selorder);
2799 while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)
2800 sfree(info);
2801 freetree234(fs->fonts_by_realname);
2802
2803 gtk_widget_destroy(GTK_WIDGET(fs->u.window));
2804 sfree(fs);
2805 }
2806
2807 void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
2808 {
2809 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
2810 int i, start, end, size, flags;
2811 const char *fontname2 = NULL;
2812 fontinfo *info;
2813
2814 /*
2815 * Provide a default if given an empty or null font name.
2816 */
2817 if (!fontname || !*fontname)
2818 fontname = "server:fixed";
2819
2820 /*
2821 * Call the canonify_fontname function.
2822 */
2823 fontname = unifont_do_prefix(fontname, &start, &end);
2824 for (i = start; i < end; i++) {
2825 fontname2 = unifont_types[i]->canonify_fontname
2826 (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE);
2827 if (fontname2)
2828 break;
2829 }
2830 if (i == end)
2831 return; /* font name not recognised */
2832
2833 /*
2834 * Now look up the canonified font name in our index.
2835 */
2836 {
2837 struct fontinfo_realname_find f;
2838 f.realname = fontname2;
2839 f.flags = flags;
2840 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
2841 }
2842
2843 /*
2844 * If we've found the font, and its size field is either
2845 * correct or zero (the latter indicating a scalable font),
2846 * then we're done. Otherwise, try looking up the original
2847 * font name instead.
2848 */
2849 if (!info || (info->size != size && info->size != 0)) {
2850 struct fontinfo_realname_find f;
2851 f.realname = fontname;
2852 f.flags = flags;
2853
2854 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
2855 if (!info || info->size != size)
2856 return; /* font name not in our index */
2857 }
2858
2859 /*
2860 * Now we've got a fontinfo structure and a font size, so we
2861 * know everything we need to fill in all the fields in the
2862 * dialog.
2863 */
2864 unifontsel_select_font(fs, info, size, 0, TRUE);
2865 }
2866
2867 char *unifontsel_get_name(unifontsel *fontsel)
2868 {
2869 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
2870 char *name;
2871
2872 if (!fs->selected)
2873 return NULL;
2874
2875 if (fs->selected->size == 0) {
2876 name = fs->selected->fontclass->scale_fontname
2877 (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
2878 if (name) {
2879 char *ret = dupcat(fs->selected->fontclass->prefix, ":",
2880 name, NULL);
2881 sfree(name);
2882 return ret;
2883 }
2884 }
2885
2886 return dupcat(fs->selected->fontclass->prefix, ":",
2887 fs->selected->realname, NULL);
2888 }
2889
2890 #endif /* GTK_CHECK_VERSION(2,0,0) */