2 * Unified font management for GTK.
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
16 #if !GTK_CHECK_VERSION(3,0,0)
17 #include <gdk/gdkkeysyms.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
28 #include "gtkcompat.h"
34 * - it would be nice to have a display of the current font name,
35 * and in particular whether it's client- or server-side,
36 * during the progress of the font selector.
39 #if !GLIB_CHECK_VERSION(1,3,7)
40 #define g_ascii_strcasecmp g_strcasecmp
41 #define g_ascii_strncasecmp g_strncasecmp
45 * Ad-hoc vtable mechanism to allow font structures to be
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.
55 #define FONTFLAG_CLIENTSIDE 0x0001
56 #define FONTFLAG_SERVERSIDE 0x0002
57 #define FONTFLAG_SERVERALIAS 0x0004
58 #define FONTFLAG_NONMONOSPACED 0x0008
60 #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
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,
66 const struct unifont_vtable *fontclass);
68 struct unifont_vtable {
70 * `Methods' of the `class'.
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)(unifont_drawctx *ctx, unifont *font,
79 int x, int y, const wchar_t *string, int len,
80 int wide, 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);
88 * `Static data members' of the `class'.
95 /* ----------------------------------------------------------------------
96 * X11 font implementation, directly using Xlib calls. Conditioned out
97 * if X11 fonts aren't available at all (e.g. building with GTK3 for a
98 * back end other than X).
101 static int x11font_has_glyph(unifont *font, wchar_t glyph);
102 static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
103 int x, int y, const wchar_t *string, int len,
104 int wide, int bold, int cellwidth);
105 static unifont *x11font_create(GtkWidget *widget, const char *name,
107 int shadowoffset, int shadowalways);
108 static void x11font_destroy(unifont *font);
109 static void x11font_enum_fonts(GtkWidget *widget,
110 fontsel_add_entry callback, void *callback_ctx);
111 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
112 int *size, int *flags,
113 int resolve_aliases);
114 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
117 #ifdef DRAW_TEXT_CAIRO
118 struct cairo_cached_glyph {
119 cairo_surface_t *surface;
120 unsigned char *bitmap;
125 * Structure storing a single physical XFontStruct, plus associated
128 typedef struct x11font_individual {
129 /* The XFontStruct itself. */
133 * The `allocated' flag indicates whether we've tried to fetch
134 * this subfont already (thus distinguishing xfs==NULL because we
135 * haven't tried yet from xfs==NULL because we tried and failed,
136 * so that we don't keep trying and failing subsequently).
140 #ifdef DRAW_TEXT_CAIRO
142 * A cache of glyph bitmaps downloaded from the X server when
143 * we're in Cairo rendering mode. If glyphcache itself is
144 * non-NULL, then entries in [0,nglyphs) are expected to be
145 * initialised to either NULL or a bitmap pointer.
147 struct cairo_cached_glyph *glyphcache;
151 * X server paraphernalia for actually downloading the glyphs.
155 int pixwidth, pixheight, pixoriginx, pixoriginy;
158 * Paraphernalia for loading the resulting bitmaps into Cairo.
160 int rowsize, allsize, indexflip;
163 } x11font_individual;
168 * Individual physical X fonts. We store a number of these, for
169 * automatically guessed bold and wide variants.
171 x11font_individual fonts[4];
173 * `sixteen_bit' is true iff the font object is indexed by
174 * values larger than a byte. That is, this flag tells us
175 * whether we use XDrawString or XDrawString16, etc.
179 * `variable' is true iff the font is non-fixed-pitch. This
180 * enables some code which takes greater care over character
181 * positioning during text drawing.
185 * real_charset is the charset used when translating text into the
186 * font's internal encoding inside draw_text(). This need not be
187 * the same as the public_charset provided to the client; for
188 * example, public_charset might be CS_ISO8859_1 while
189 * real_charset is CS_ISO8859_1_X11.
193 * Data passed in to unifont_create().
195 int wide, bold, shadowoffset, shadowalways;
198 static const struct unifont_vtable x11font_vtable = {
200 NULL, /* no fallback fonts in X11 */
205 x11font_canonify_fontname,
206 x11font_scale_fontname,
210 static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide)
212 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
213 Atom fontprop = XInternAtom(disp, "FONT", False);
215 if (XGetFontProperty(xfs, fontprop, &ret)) {
216 char *name = XGetAtomName(disp, (Atom)ret);
217 if (name && name[0] == '-') {
218 const char *strings[13];
219 char *dupname, *extrafree = NULL, *ret;
223 p = q = dupname = dupstr(name); /* skip initial minus */
226 while (*p && nstr < lenof(strings)) {
229 strings[nstr++] = p+1;
234 if (nstr < lenof(strings)) {
236 return NULL; /* XLFD was malformed */
243 /* 4 is `wideness', which obviously may have changed. */
244 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
245 strings[4] = strings[5] = "*";
246 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
249 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
250 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
251 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
252 "-", strings[ 9], "-", strings[10], "-", strings[11],
253 "-", strings[12], NULL);
263 static int x11_font_width(XFontStruct *xfs, int sixteen_bit)
269 return XTextWidth16(xfs, &space, 1);
271 return XTextWidth(xfs, "0", 1);
275 static int x11_font_has_glyph(XFontStruct *xfs, int byte1, int byte2)
280 * Not to be confused with x11font_has_glyph, which is a method of
281 * the x11font 'class' and hence takes a unifont as argument. This
282 * is the low-level function which grubs about in an actual
283 * XFontStruct to see if a given glyph exists.
285 * We must do this ourselves rather than letting Xlib's
286 * XTextExtents16 do the job, because XTextExtents will helpfully
287 * substitute the font's default_char for any missing glyph and
288 * not tell us it did so, which precisely won't help us find out
289 * which glyphs _are_ missing.
291 * The man page for XQueryFont is rather confusing about how the
292 * per_char array in the XFontStruct is laid out, because it gives
293 * formulae for determining the two-byte X character code _from_
294 * an index into the per_char array. Going the other way, it's
297 * The valid character codes have byte1 between min_byte1 and
298 * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
299 * max_char_or_byte2 inclusive. This gives a rectangle of size
300 * (max_byte2-min_byte1+1) by
301 * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
302 * rectangle encoded in the per_char array. Hence, given a
303 * character code which is valid in the sense that it falls
304 * somewhere in that rectangle, its index in per_char is given by
307 * x = byte2 - min_char_or_byte2
308 * y = byte1 - min_byte1
309 * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
311 * If min_byte1 and min_byte2 are both zero, that's a special case
312 * which can be treated as if min_byte2 was 1 instead, i.e. the
313 * per_char array just runs from min_char_or_byte2 to
314 * max_char_or_byte2 inclusive, and byte1 should always be zero.
317 if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2)
320 if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
321 index = byte2 - xfs->min_char_or_byte2;
323 if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1)
325 index = ((byte2 - xfs->min_char_or_byte2) +
326 ((byte1 - xfs->min_byte1) *
327 (xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1)));
330 if (!xfs->per_char) /* per_char NULL => everything in range exists */
333 return (xfs->per_char[index].ascent + xfs->per_char[index].descent > 0 ||
334 xfs->per_char[index].width > 0);
337 static unifont *x11font_create(GtkWidget *widget, const char *name,
339 int shadowoffset, int shadowalways)
341 struct x11font *xfont;
343 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
344 Atom charset_registry, charset_encoding, spacing;
345 unsigned long registry_ret, encoding_ret, spacing_ret;
346 int pubcs, realcs, sixteen_bit, variable;
349 xfs = XLoadQueryFont(disp, name);
353 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
354 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
356 pubcs = realcs = CS_NONE;
360 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
361 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
363 reg = XGetAtomName(disp, (Atom)registry_ret);
364 enc = XGetAtomName(disp, (Atom)encoding_ret);
366 char *encoding = dupcat(reg, "-", enc, NULL);
367 pubcs = realcs = charset_from_xenc(encoding);
370 * iso10646-1 is the only wide font encoding we
371 * support. In this case, we expect clients to give us
372 * UTF-8, which this module must internally convert
373 * into 16-bit Unicode.
375 if (!strcasecmp(encoding, "iso10646-1")) {
377 pubcs = realcs = CS_UTF8;
381 * Hack for X line-drawing characters: if the primary font
382 * is encoded as ISO-8859-1, and has valid glyphs in the
383 * low character positions, it is assumed that those
384 * glyphs are the VT100 line-drawing character set.
386 if (pubcs == CS_ISO8859_1) {
388 for (ch = 1; ch < 32; ch++)
389 if (!x11_font_has_glyph(xfs, 0, ch))
392 realcs = CS_ISO8859_1_X11;
399 spacing = XInternAtom(disp, "SPACING", False);
400 if (XGetFontProperty(xfs, spacing, &spacing_ret)) {
402 spc = XGetAtomName(disp, (Atom)spacing_ret);
404 if (spc && strchr("CcMm", spc[0]))
408 xfont = snew(struct x11font);
409 xfont->u.vt = &x11font_vtable;
410 xfont->u.width = x11_font_width(xfs, sixteen_bit);
411 xfont->u.ascent = xfs->ascent;
412 xfont->u.descent = xfs->descent;
413 xfont->u.height = xfont->u.ascent + xfont->u.descent;
414 xfont->u.public_charset = pubcs;
415 xfont->u.want_fallback = TRUE;
417 xfont->u.preferred_drawtype = DRAWTYPE_GDK;
418 #elif defined DRAW_TEXT_CAIRO
419 xfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
421 #error No drawtype available at all
423 xfont->real_charset = realcs;
424 xfont->sixteen_bit = sixteen_bit;
425 xfont->variable = variable;
428 xfont->shadowoffset = shadowoffset;
429 xfont->shadowalways = shadowalways;
431 for (i = 0; i < lenof(xfont->fonts); i++) {
432 xfont->fonts[i].xfs = NULL;
433 xfont->fonts[i].allocated = FALSE;
434 #ifdef DRAW_TEXT_CAIRO
435 xfont->fonts[i].glyphcache = NULL;
436 xfont->fonts[i].nglyphs = 0;
437 xfont->fonts[i].pixmap = None;
438 xfont->fonts[i].gc = None;
441 xfont->fonts[0].xfs = xfs;
442 xfont->fonts[0].allocated = TRUE;
444 return (unifont *)xfont;
447 static void x11font_destroy(unifont *font)
449 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
450 struct x11font *xfont = (struct x11font *)font;
453 for (i = 0; i < lenof(xfont->fonts); i++) {
454 if (xfont->fonts[i].xfs)
455 XFreeFont(disp, xfont->fonts[i].xfs);
456 #ifdef DRAW_TEXT_CAIRO
457 if (xfont->fonts[i].gc != None)
458 XFreeGC(disp, xfont->fonts[i].gc);
459 if (xfont->fonts[i].pixmap != None)
460 XFreePixmap(disp, xfont->fonts[i].pixmap);
461 if (xfont->fonts[i].glyphcache) {
463 for (j = 0; j < xfont->fonts[i].nglyphs; j++) {
464 cairo_surface_destroy(xfont->fonts[i].glyphcache[j].surface);
465 sfree(xfont->fonts[i].glyphcache[j].bitmap);
467 sfree(xfont->fonts[i].glyphcache);
474 static void x11_alloc_subfont(struct x11font *xfont, int sfid)
476 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
477 char *derived_name = x11_guess_derived_font_name
478 (xfont->fonts[0].xfs, sfid & 1, !!(sfid & 2));
479 xfont->fonts[sfid].xfs = XLoadQueryFont(disp, derived_name);
480 xfont->fonts[sfid].allocated = TRUE;
482 /* Note that xfont->fonts[sfid].xfs may still be NULL, if XLQF failed. */
485 static int x11font_has_glyph(unifont *font, wchar_t glyph)
487 struct x11font *xfont = (struct x11font *)font;
489 if (xfont->sixteen_bit) {
491 * This X font has 16-bit character indices, which means
492 * we can directly use our Unicode input value.
494 return x11_font_has_glyph(xfont->fonts[0].xfs,
495 glyph >> 8, glyph & 0xFF);
498 * This X font has 8-bit indices, so we must convert to the
499 * appropriate character set.
502 int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
503 sbstring, 2, "", NULL, NULL);
504 if (sblen == 0 || !sbstring[0])
505 return FALSE; /* not even in the charset */
507 return x11_font_has_glyph(xfont->fonts[0].xfs, 0,
508 (unsigned char)sbstring[0]);
512 #if !GTK_CHECK_VERSION(2,0,0)
513 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
514 #elif GTK_CHECK_VERSION(3,0,0)
515 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XID(d) /* GTK3's name for this */
518 static int x11font_width_16(unifont_drawctx *ctx, x11font_individual *xfi,
519 const void *vstring, int start, int length)
521 const XChar2b *string = (const XChar2b *)vstring;
522 return XTextWidth16(xfi->xfs, string+start, length);
525 static int x11font_width_8(unifont_drawctx *ctx, x11font_individual *xfi,
526 const void *vstring, int start, int length)
528 const char *string = (const char *)vstring;
529 return XTextWidth(xfi->xfs, string+start, length);
533 static void x11font_gdk_setup(unifont_drawctx *ctx, x11font_individual *xfi)
535 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
536 XSetFont(disp, GDK_GC_XGC(ctx->u.gdk.gc), xfi->xfs->fid);
539 static void x11font_gdk_draw_16(unifont_drawctx *ctx,
540 x11font_individual *xfi, int x, int y,
541 const void *vstring, int start, int length)
543 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
544 const XChar2b *string = (const XChar2b *)vstring;
545 XDrawString16(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
546 GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
549 static void x11font_gdk_draw_8(unifont_drawctx *ctx,
550 x11font_individual *xfi, int x, int y,
551 const void *vstring, int start, int length)
553 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
554 const char *string = (const char *)vstring;
555 XDrawString(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
556 GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
560 #ifdef DRAW_TEXT_CAIRO
561 static void x11font_cairo_setup(unifont_drawctx *ctx, x11font_individual *xfi)
563 if (xfi->pixmap == None) {
564 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
566 GdkWindow *widgetwin = gtk_widget_get_window(ctx->u.cairo.widget);
567 int widgetscr = GDK_SCREEN_XNUMBER(gdk_window_get_screen(widgetwin));
570 xfi->xfs->max_bounds.rbearing - xfi->xfs->min_bounds.lbearing;
572 xfi->xfs->max_bounds.ascent + xfi->xfs->max_bounds.descent;
573 xfi->pixoriginx = -xfi->xfs->min_bounds.lbearing;
574 xfi->pixoriginy = xfi->xfs->max_bounds.ascent;
576 xfi->rowsize = cairo_format_stride_for_width(CAIRO_FORMAT_A1,
578 xfi->allsize = xfi->rowsize * xfi->pixheight;
582 * Test host endianness and use it to set xfi->indexflip,
583 * which is XORed into our left-shift counts in order to
584 * implement the CAIRO_FORMAT_A1 specification, in which
585 * each bitmap byte is oriented LSB-first on little-endian
586 * platforms and MSB-first on big-endian ones.
588 * This is the same technique Cairo itself uses to test
589 * endianness, so hopefully it'll work in any situation
590 * where Cairo is usable at all.
592 static const int endianness_test = 1;
593 xfi->indexflip = (*((char *) &endianness_test) == 1) ? 0 : 7;
596 xfi->pixmap = XCreatePixmap
598 GDK_DRAWABLE_XID(gtk_widget_get_window(ctx->u.cairo.widget)),
599 xfi->pixwidth, xfi->pixheight, 1);
600 gcvals.foreground = WhitePixel(disp, widgetscr);
601 gcvals.background = BlackPixel(disp, widgetscr);
602 gcvals.font = xfi->xfs->fid;
603 xfi->gc = XCreateGC(disp, xfi->pixmap,
604 GCForeground | GCBackground | GCFont, &gcvals);
608 static void x11font_cairo_cache_glyph(x11font_individual *xfi, int glyphindex)
612 unsigned char *bitmap;
613 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
615 bitmap = snewn(xfi->allsize, unsigned char);
616 memset(bitmap, 0, xfi->allsize);
618 image = XGetImage(disp, xfi->pixmap, 0, 0,
619 xfi->pixwidth, xfi->pixheight, AllPlanes, XYPixmap);
620 for (y = 0; y < xfi->pixheight; y++) {
621 for (x = 0; x < xfi->pixwidth; x++) {
622 unsigned long pixel = XGetPixel(image, x, y);
624 int byteindex = y * xfi->rowsize + x/8;
625 int bitindex = (x & 7) ^ xfi->indexflip;
626 bitmap[byteindex] |= 1U << bitindex;
630 XDestroyImage(image);
632 if (xfi->nglyphs <= glyphindex) {
633 /* Round up to the next multiple of 256 on the general
634 * principle that Unicode characters come in contiguous blocks
635 * often used together */
636 int old_nglyphs = xfi->nglyphs;
637 xfi->nglyphs = (glyphindex + 0xFF) & ~0xFF;
638 xfi->glyphcache = sresize(xfi->glyphcache, xfi->nglyphs,
639 struct cairo_cached_glyph);
641 while (old_nglyphs < xfi->nglyphs) {
642 xfi->glyphcache[old_nglyphs].surface = NULL;
643 xfi->glyphcache[old_nglyphs].bitmap = NULL;
647 xfi->glyphcache[glyphindex].bitmap = bitmap;
648 xfi->glyphcache[glyphindex].surface = cairo_image_surface_create_for_data
649 (bitmap, CAIRO_FORMAT_A1, xfi->pixwidth, xfi->pixheight, xfi->rowsize);
652 static void x11font_cairo_draw_glyph(unifont_drawctx *ctx,
653 x11font_individual *xfi, int x, int y,
656 if (xfi->glyphcache[glyphindex].surface) {
657 cairo_mask_surface(ctx->u.cairo.cr,
658 xfi->glyphcache[glyphindex].surface,
659 x - xfi->pixoriginx, y - xfi->pixoriginy);
663 static void x11font_cairo_draw_16(unifont_drawctx *ctx,
664 x11font_individual *xfi, int x, int y,
665 const void *vstring, int start, int length)
667 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
668 const XChar2b *string = (const XChar2b *)vstring + start;
670 for (i = 0; i < length; i++) {
671 if (x11_font_has_glyph(xfi->xfs, string[i].byte1, string[i].byte2)) {
672 int glyphindex = (256 * (unsigned char)string[i].byte1 +
673 (unsigned char)string[i].byte2);
674 if (glyphindex >= xfi->nglyphs ||
675 !xfi->glyphcache[glyphindex].surface) {
676 XDrawImageString16(disp, xfi->pixmap, xfi->gc,
677 xfi->pixoriginx, xfi->pixoriginy,
679 x11font_cairo_cache_glyph(xfi, glyphindex);
681 x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
682 x += XTextWidth16(xfi->xfs, string+i, 1);
687 static void x11font_cairo_draw_8(unifont_drawctx *ctx,
688 x11font_individual *xfi, int x, int y,
689 const void *vstring, int start, int length)
691 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
692 const char *string = (const char *)vstring + start;
694 for (i = 0; i < length; i++) {
695 if (x11_font_has_glyph(xfi->xfs, 0, string[i])) {
696 int glyphindex = (unsigned char)string[i];
697 if (glyphindex >= xfi->nglyphs ||
698 !xfi->glyphcache[glyphindex].surface) {
699 XDrawImageString(disp, xfi->pixmap, xfi->gc,
700 xfi->pixoriginx, xfi->pixoriginy,
702 x11font_cairo_cache_glyph(xfi, glyphindex);
704 x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
705 x += XTextWidth(xfi->xfs, string+i, 1);
709 #endif /* DRAW_TEXT_CAIRO */
711 struct x11font_drawfuncs {
712 int (*width)(unifont_drawctx *ctx, x11font_individual *xfi,
713 const void *vstring, int start, int length);
714 void (*setup)(unifont_drawctx *ctx, x11font_individual *xfi);
715 void (*draw)(unifont_drawctx *ctx, x11font_individual *xfi, int x, int y,
716 const void *vstring, int start, int length);
720 * This array has two entries per compiled-in drawtype; of each pair,
721 * the first is for an 8-bit font and the second for 16-bit.
723 static const struct x11font_drawfuncs x11font_drawfuncs[2*DRAWTYPE_NTYPES] = {
738 #ifdef DRAW_TEXT_CAIRO
743 x11font_cairo_draw_8,
745 /* [3] cairo, 16-bit */
749 x11font_cairo_draw_16,
754 static void x11font_really_draw_text(const struct x11font_drawfuncs *dfns,
755 unifont_drawctx *ctx,
756 x11font_individual *xfi, int x, int y,
757 const void *string, int nchars,
759 int fontvariable, int cellwidth)
761 int start = 0, step, nsteps, centre;
765 * In a variable-pitch font, we draw one character at a
766 * time, and centre it in the character cell.
773 * In a fixed-pitch font, we can draw the whole lot in one go.
780 dfns->setup(ctx, xfi);
782 while (nsteps-- > 0) {
785 X += (cellwidth - dfns->width(ctx, xfi, string, start, step)) / 2;
787 dfns->draw(ctx, xfi, X, y, string, start, step);
789 dfns->draw(ctx, xfi, X + shadowoffset, y, string, start, step);
796 static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
797 int x, int y, const wchar_t *string, int len,
798 int wide, int bold, int cellwidth)
800 struct x11font *xfont = (struct x11font *)font;
802 int shadowoffset = 0;
803 int mult = (wide ? 2 : 1);
804 int index = 2 * (int)ctx->type;
810 * Decide which subfont we're using, and whether we have to
813 if (xfont->shadowalways && bold) {
814 shadowoffset = xfont->shadowoffset;
817 sfid = 2 * wide + bold;
818 if (!xfont->fonts[sfid].allocated)
819 x11_alloc_subfont(xfont, sfid);
820 if (bold && !xfont->fonts[sfid].xfs) {
822 shadowoffset = xfont->shadowoffset;
823 sfid = 2 * wide + bold;
824 if (!xfont->fonts[sfid].allocated)
825 x11_alloc_subfont(xfont, sfid);
828 if (!xfont->fonts[sfid].xfs)
829 return; /* we've tried our best, but no luck */
831 if (xfont->sixteen_bit) {
833 * This X font has 16-bit character indices, which means
834 * we can directly use our Unicode input string.
839 xcs = snewn(len, XChar2b);
840 for (i = 0; i < len; i++) {
841 xcs[i].byte1 = string[i] >> 8;
842 xcs[i].byte2 = string[i];
845 x11font_really_draw_text(x11font_drawfuncs + index + 1, ctx,
846 &xfont->fonts[sfid], x, y,
847 xcs, len, shadowoffset,
848 xfont->variable, cellwidth * mult);
852 * This X font has 8-bit indices, so we must convert to the
853 * appropriate character set.
855 char *sbstring = snewn(len+1, char);
856 int sblen = wc_to_mb(xfont->real_charset, 0, string, len,
857 sbstring, len+1, ".", NULL, NULL);
858 x11font_really_draw_text(x11font_drawfuncs + index + 0, ctx,
859 &xfont->fonts[sfid], x, y,
860 sbstring, sblen, shadowoffset,
861 xfont->variable, cellwidth * mult);
866 static void x11font_enum_fonts(GtkWidget *widget,
867 fontsel_add_entry callback, void *callback_ctx)
869 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
872 int nnames, i, max, tmpsize;
876 fontnames = XListFonts(disp, "*", max, &nnames);
878 XFreeFontNames(fontnames);
886 for (i = 0; i < nnames; i++) {
887 if (fontnames[i][0] == '-') {
889 * Dismember an XLFD and convert it into the format
890 * we'll be using in the font selector.
892 char *components[14];
893 char *p, *font, *style, *stylekey, *charset;
894 int j, weightkey, slantkey, setwidthkey;
895 int thistmpsize, fontsize, flags;
897 thistmpsize = 4 * strlen(fontnames[i]) + 256;
898 if (tmpsize < thistmpsize) {
899 tmpsize = thistmpsize;
900 tmp = sresize(tmp, tmpsize, char);
902 strcpy(tmp, fontnames[i]);
905 for (j = 0; j < 14; j++) {
909 while (*p && *p != '-')
915 * Font name is made up of fields 0 and 1, in reverse
916 * order with parentheses. (This is what the GTK 1.2 X
917 * font selector does, and it seems to come out
918 * looking reasonably sensible.)
921 p += 1 + sprintf(p, "%s (%s)", components[1], components[0]);
924 * Charset is made up of fields 12 and 13.
927 p += 1 + sprintf(p, "%s-%s", components[12], components[13]);
930 * Style is a mixture of quite a lot of the fields,
931 * with some strange formatting.
934 p += sprintf(p, "%s", components[2][0] ? components[2] :
936 if (!g_ascii_strcasecmp(components[3], "i"))
937 p += sprintf(p, " italic");
938 else if (!g_ascii_strcasecmp(components[3], "o"))
939 p += sprintf(p, " oblique");
940 else if (!g_ascii_strcasecmp(components[3], "ri"))
941 p += sprintf(p, " reverse italic");
942 else if (!g_ascii_strcasecmp(components[3], "ro"))
943 p += sprintf(p, " reverse oblique");
944 else if (!g_ascii_strcasecmp(components[3], "ot"))
945 p += sprintf(p, " other-slant");
946 if (components[4][0] && g_ascii_strcasecmp(components[4], "normal"))
947 p += sprintf(p, " %s", components[4]);
948 if (!g_ascii_strcasecmp(components[10], "m"))
949 p += sprintf(p, " [M]");
950 if (!g_ascii_strcasecmp(components[10], "c"))
951 p += sprintf(p, " [C]");
952 if (components[5][0])
953 p += sprintf(p, " %s", components[5]);
956 * Style key is the same stuff as above, but with a
957 * couple of transformations done on it to make it
958 * sort more sensibly.
962 if (!g_ascii_strcasecmp(components[2], "medium") ||
963 !g_ascii_strcasecmp(components[2], "regular") ||
964 !g_ascii_strcasecmp(components[2], "normal") ||
965 !g_ascii_strcasecmp(components[2], "book"))
967 else if (!g_ascii_strncasecmp(components[2], "demi", 4) ||
968 !g_ascii_strncasecmp(components[2], "semi", 4))
972 if (!g_ascii_strcasecmp(components[3], "r"))
974 else if (!g_ascii_strncasecmp(components[3], "r", 1))
978 if (!g_ascii_strcasecmp(components[4], "normal"))
983 p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
985 (int)strlen(components[2]), components[2],
987 (int)strlen(components[3]), components[3],
989 (int)strlen(components[4]), components[4],
990 (int)strlen(components[10]), components[10],
991 (int)strlen(components[5]), components[5]);
993 assert(p - tmp < thistmpsize);
996 * Size is in pixels, for our application, so we
997 * derive it directly from the pixel size field,
1000 fontsize = atoi(components[6]);
1003 * Flags: we need to know whether this is a monospaced
1004 * font, which we do by examining the spacing field
1007 flags = FONTFLAG_SERVERSIDE;
1008 if (!strchr("CcMm", components[10][0]))
1009 flags |= FONTFLAG_NONMONOSPACED;
1012 * Not sure why, but sometimes the X server will
1013 * deliver dummy font types in which fontsize comes
1014 * out as zero. Filter those out.
1017 callback(callback_ctx, fontnames[i], font, charset,
1018 style, stylekey, fontsize, flags, &x11font_vtable);
1021 * This isn't an XLFD, so it must be an alias.
1022 * Transmit it with mostly null data.
1024 * It would be nice to work out if it's monospaced
1025 * here, but at the moment I can't see that being
1026 * anything but computationally hideous. Ah well.
1028 callback(callback_ctx, fontnames[i], fontnames[i], NULL,
1029 NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);
1032 XFreeFontNames(fontnames);
1035 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
1036 int *size, int *flags,
1037 int resolve_aliases)
1040 * When given an X11 font name to try to make sense of for a
1041 * font selector, we must attempt to load it (to see if it
1042 * exists), and then canonify it by extracting its FONT
1043 * property, which should give its full XLFD even if what we
1044 * originally had was a wildcard.
1046 * However, we must carefully avoid canonifying font
1047 * _aliases_, unless specifically asked to, because the font
1048 * selector treats them as worthwhile in their own right.
1051 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1052 Atom fontprop, fontprop2;
1055 xfs = XLoadQueryFont(disp, name);
1058 return NULL; /* didn't make sense to us, sorry */
1060 fontprop = XInternAtom(disp, "FONT", False);
1062 if (XGetFontProperty(xfs, fontprop, &ret)) {
1063 char *newname = XGetAtomName(disp, (Atom)ret);
1065 unsigned long fsize = 12;
1067 fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);
1068 if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {
1070 XFreeFont(disp, xfs);
1072 if (name[0] == '-' || resolve_aliases)
1073 *flags = FONTFLAG_SERVERSIDE;
1075 *flags = FONTFLAG_SERVERALIAS;
1077 return dupstr(name[0] == '-' || resolve_aliases ?
1083 XFreeFont(disp, xfs);
1085 return NULL; /* something went wrong */
1088 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
1091 return NULL; /* shan't */
1094 #endif /* NOT_X_WINDOWS */
1096 #if GTK_CHECK_VERSION(2,0,0)
1098 /* ----------------------------------------------------------------------
1099 * Pango font implementation (for GTK 2 only).
1102 #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
1103 #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
1106 static int pangofont_has_glyph(unifont *font, wchar_t glyph);
1107 static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
1108 int x, int y, const wchar_t *string, int len,
1109 int wide, int bold, int cellwidth);
1110 static unifont *pangofont_create(GtkWidget *widget, const char *name,
1112 int shadowoffset, int shadowalways);
1113 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
1115 int shadowoffset, int shadowalways);
1116 static void pangofont_destroy(unifont *font);
1117 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
1118 void *callback_ctx);
1119 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
1120 int *size, int *flags,
1121 int resolve_aliases);
1122 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
1130 PangoFontDescription *desc;
1133 * The containing widget.
1137 * Data passed in to unifont_create().
1139 int bold, shadowoffset, shadowalways;
1142 static const struct unifont_vtable pangofont_vtable = {
1144 pangofont_create_fallback,
1146 pangofont_has_glyph,
1147 pangofont_draw_text,
1148 pangofont_enum_fonts,
1149 pangofont_canonify_fontname,
1150 pangofont_scale_fontname,
1155 * This function is used to rigorously validate a
1156 * PangoFontDescription. Later versions of Pango have a nasty
1157 * habit of accepting _any_ old string as input to
1158 * pango_font_description_from_string and returning a font
1159 * description which can actually be used to display text, even if
1160 * they have to do it by falling back to their most default font.
1161 * This is doubtless helpful in some situations, but not here,
1162 * because we need to know if a Pango font string actually _makes
1163 * sense_ in order to fall back to treating it as an X font name
1164 * if it doesn't. So we check that the font family is actually one
1165 * supported by Pango.
1167 static int pangofont_check_desc_makes_sense(PangoContext *ctx,
1168 PangoFontDescription *desc)
1170 #ifndef PANGO_PRE_1POINT6
1173 PangoFontFamily **families;
1174 int i, nfamilies, matched;
1177 * Ask Pango for a list of font families, and iterate through
1178 * them to see if one of them matches the family in the
1179 * PangoFontDescription.
1181 #ifndef PANGO_PRE_1POINT6
1182 map = pango_context_get_font_map(ctx);
1185 pango_font_map_list_families(map, &families, &nfamilies);
1187 pango_context_list_families(ctx, &families, &nfamilies);
1191 for (i = 0; i < nfamilies; i++) {
1192 if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]),
1193 pango_font_description_get_family(desc))) {
1203 static unifont *pangofont_create_internal(GtkWidget *widget,
1205 PangoFontDescription *desc,
1207 int shadowoffset, int shadowalways)
1209 struct pangofont *pfont;
1210 #ifndef PANGO_PRE_1POINT6
1214 PangoFontMetrics *metrics;
1216 #ifndef PANGO_PRE_1POINT6
1217 map = pango_context_get_font_map(ctx);
1219 pango_font_description_free(desc);
1222 fset = pango_font_map_load_fontset(map, ctx, desc,
1223 pango_context_get_language(ctx));
1225 fset = pango_context_load_fontset(ctx, desc,
1226 pango_context_get_language(ctx));
1229 pango_font_description_free(desc);
1232 metrics = pango_fontset_get_metrics(fset);
1234 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
1235 pango_font_description_free(desc);
1236 g_object_unref(fset);
1240 pfont = snew(struct pangofont);
1241 pfont->u.vt = &pangofont_vtable;
1243 PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));
1244 pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));
1245 pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics));
1246 pfont->u.height = pfont->u.ascent + pfont->u.descent;
1247 pfont->u.want_fallback = FALSE;
1248 #ifdef DRAW_TEXT_CAIRO
1249 pfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
1250 #elif defined DRAW_TEXT_GDK
1251 pfont->u.preferred_drawtype = DRAWTYPE_GDK;
1253 #error No drawtype available at all
1255 /* The Pango API is hardwired to UTF-8 */
1256 pfont->u.public_charset = CS_UTF8;
1259 pfont->widget = widget;
1261 pfont->shadowoffset = shadowoffset;
1262 pfont->shadowalways = shadowalways;
1264 pango_font_metrics_unref(metrics);
1266 return (unifont *)pfont;
1269 static unifont *pangofont_create(GtkWidget *widget, const char *name,
1271 int shadowoffset, int shadowalways)
1274 PangoFontDescription *desc;
1276 desc = pango_font_description_from_string(name);
1279 ctx = gtk_widget_get_pango_context(widget);
1281 pango_font_description_free(desc);
1284 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1285 pango_font_description_free(desc);
1288 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1289 shadowoffset, shadowalways);
1292 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
1294 int shadowoffset, int shadowalways)
1297 PangoFontDescription *desc;
1299 desc = pango_font_description_from_string("Monospace");
1302 ctx = gtk_widget_get_pango_context(widget);
1304 pango_font_description_free(desc);
1307 pango_font_description_set_absolute_size(desc, height * PANGO_SCALE);
1308 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1309 shadowoffset, shadowalways);
1312 static void pangofont_destroy(unifont *font)
1314 struct pangofont *pfont = (struct pangofont *)font;
1315 pango_font_description_free(pfont->desc);
1316 g_object_unref(pfont->fset);
1320 static int pangofont_has_glyph(unifont *font, wchar_t glyph)
1322 /* Pango implements font fallback, so assume it has everything */
1326 #ifdef DRAW_TEXT_GDK
1327 static void pango_gdk_draw_layout(unifont_drawctx *ctx,
1328 gint x, gint y, PangoLayout *layout)
1330 gdk_draw_layout(ctx->u.gdk.target, ctx->u.gdk.gc, x, y, layout);
1334 #ifdef DRAW_TEXT_CAIRO
1335 static void pango_cairo_draw_layout(unifont_drawctx *ctx,
1336 gint x, gint y, PangoLayout *layout)
1338 cairo_move_to(ctx->u.cairo.cr, x, y);
1339 pango_cairo_show_layout(ctx->u.cairo.cr, layout);
1343 static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
1344 int x, int y, const wchar_t *string, int len,
1345 int wide, int bold, int cellwidth)
1347 struct pangofont *pfont = (struct pangofont *)font;
1348 PangoLayout *layout;
1349 PangoRectangle rect;
1350 char *utfstring, *utfptr;
1352 int shadowbold = FALSE;
1353 void (*draw_layout)(unifont_drawctx *ctx,
1354 gint x, gint y, PangoLayout *layout) = NULL;
1356 #ifdef DRAW_TEXT_GDK
1357 if (ctx->type == DRAWTYPE_GDK) {
1358 draw_layout = pango_gdk_draw_layout;
1361 #ifdef DRAW_TEXT_CAIRO
1362 if (ctx->type == DRAWTYPE_CAIRO) {
1363 draw_layout = pango_cairo_draw_layout;
1370 y -= pfont->u.ascent;
1372 layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));
1373 pango_layout_set_font_description(layout, pfont->desc);
1374 if (bold > pfont->bold) {
1375 if (pfont->shadowalways)
1378 PangoFontDescription *desc2 =
1379 pango_font_description_copy_static(pfont->desc);
1380 pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);
1381 pango_layout_set_font_description(layout, desc2);
1386 * Pango always expects UTF-8, so convert the input wide character
1389 utfstring = snewn(len*6+1, char); /* UTF-8 has max 6 bytes/char */
1390 utflen = wc_to_mb(CS_UTF8, 0, string, len,
1391 utfstring, len*6+1, ".", NULL, NULL);
1394 while (utflen > 0) {
1398 * We want to display every character from this string in
1399 * the centre of its own character cell. In the worst case,
1400 * this requires a separate text-drawing call for each
1401 * character; but in the common case where the font is
1402 * properly fixed-width, we can draw many characters in one
1403 * go which is much faster.
1405 * This still isn't really ideal. If you look at what
1406 * happens in the X protocol as a result of all of this, you
1407 * find - naturally enough - that each call to
1408 * gdk_draw_layout() generates a separate set of X RENDER
1409 * operations involving creating a picture, setting a clip
1410 * rectangle, doing some drawing and undoing the whole lot.
1411 * In an ideal world, we should _always_ be able to turn the
1412 * contents of this loop into a single RenderCompositeGlyphs
1413 * operation which internally specifies inter-character
1414 * deltas to get the spacing right, which would give us full
1415 * speed _even_ in the worst case of a non-fixed-width font.
1416 * However, Pango's architecture and documentation are so
1417 * unhelpful that I have no idea how if at all to persuade
1422 * Start by extracting a single UTF-8 character from the
1426 while (clen < utflen &&
1427 (unsigned char)utfptr[clen] >= 0x80 &&
1428 (unsigned char)utfptr[clen] < 0xC0)
1433 * If it's a right-to-left character, we must display it on
1434 * its own, to stop Pango helpfully re-reversing our already
1437 if (!is_rtl(string[0])) {
1440 * See if that character has the width we expect.
1442 pango_layout_set_text(layout, utfptr, clen);
1443 pango_layout_get_pixel_extents(layout, NULL, &rect);
1445 if (rect.width == cellwidth) {
1447 * Try extracting more characters, for as long as they
1448 * stay well-behaved.
1450 while (clen < utflen) {
1452 clen++; /* skip UTF-8 introducer byte */
1453 while (clen < utflen &&
1454 (unsigned char)utfptr[clen] >= 0x80 &&
1455 (unsigned char)utfptr[clen] < 0xC0)
1458 pango_layout_set_text(layout, utfptr, clen);
1459 pango_layout_get_pixel_extents(layout, NULL, &rect);
1460 if (rect.width != n * cellwidth) {
1469 pango_layout_set_text(layout, utfptr, clen);
1470 pango_layout_get_pixel_extents(layout, NULL, &rect);
1473 x + (n*cellwidth - rect.width)/2,
1474 y + (pfont->u.height - rect.height)/2, layout);
1477 x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,
1478 y + (pfont->u.height - rect.height)/2, layout);
1488 g_object_unref(layout);
1492 * Dummy size value to be used when converting a
1493 * PangoFontDescription of a scalable font to a string for
1496 #define PANGO_DUMMY_SIZE 12
1498 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
1502 #ifndef PANGO_PRE_1POINT6
1505 PangoFontFamily **families;
1508 ctx = gtk_widget_get_pango_context(widget);
1513 * Ask Pango for a list of font families, and iterate through
1516 #ifndef PANGO_PRE_1POINT6
1517 map = pango_context_get_font_map(ctx);
1520 pango_font_map_list_families(map, &families, &nfamilies);
1522 pango_context_list_families(ctx, &families, &nfamilies);
1524 for (i = 0; i < nfamilies; i++) {
1525 PangoFontFamily *family = families[i];
1526 const char *familyname;
1528 PangoFontFace **faces;
1532 * Set up our flags for this font family, and get the name
1535 flags = FONTFLAG_CLIENTSIDE;
1536 #ifndef PANGO_PRE_1POINT4
1538 * In very early versions of Pango, we can't tell
1539 * monospaced fonts from non-monospaced.
1541 if (!pango_font_family_is_monospace(family))
1542 flags |= FONTFLAG_NONMONOSPACED;
1544 familyname = pango_font_family_get_name(family);
1547 * Go through the available font faces in this family.
1549 pango_font_family_list_faces(family, &faces, &nfaces);
1550 for (j = 0; j < nfaces; j++) {
1551 PangoFontFace *face = faces[j];
1552 PangoFontDescription *desc;
1553 const char *facename;
1555 int k, nsizes, dummysize;
1558 * Get the face name string.
1560 facename = pango_font_face_get_face_name(face);
1563 * Set up a font description with what we've got so
1564 * far. We'll fill in the size field manually and then
1565 * call pango_font_description_to_string() to give the
1566 * full real name of the specific font.
1568 desc = pango_font_face_describe(face);
1571 * See if this font has a list of specific sizes.
1573 #ifndef PANGO_PRE_1POINT4
1574 pango_font_face_list_sizes(face, &sizes, &nsizes);
1577 * In early versions of Pango, that call wasn't
1578 * supported; we just have to assume everything is
1585 * Write a single entry with a dummy size.
1587 dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;
1593 * If so, go through them one by one.
1595 for (k = 0; k < nsizes; k++) {
1599 pango_font_description_set_size(desc, sizes[k]);
1601 fullname = pango_font_description_to_string(desc);
1604 * Construct the sorting key for font styles.
1610 n = pango_font_description_get_weight(desc);
1611 /* Weight: normal, then lighter, then bolder */
1612 if (n <= PANGO_WEIGHT_NORMAL)
1613 n = PANGO_WEIGHT_NORMAL - n;
1614 p += sprintf(p, "%4d", n);
1616 n = pango_font_description_get_style(desc);
1617 p += sprintf(p, " %2d", n);
1619 n = pango_font_description_get_stretch(desc);
1620 /* Stretch: closer to normal sorts earlier */
1621 n = 2 * abs(PANGO_STRETCH_NORMAL - n) +
1622 (n < PANGO_STRETCH_NORMAL);
1623 p += sprintf(p, " %2d", n);
1625 n = pango_font_description_get_variant(desc);
1626 p += sprintf(p, " %2d", n);
1631 * Got everything. Hand off to the callback.
1632 * (The charset string is NULL, because only
1633 * server-side X fonts use it.)
1635 callback(callback_ctx, fullname, familyname, NULL, facename,
1637 (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
1638 flags, &pangofont_vtable);
1642 if (sizes != &dummysize)
1645 pango_font_description_free(desc);
1652 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
1653 int *size, int *flags,
1654 int resolve_aliases)
1657 * When given a Pango font name to try to make sense of for a
1658 * font selector, we must normalise it to PANGO_DUMMY_SIZE and
1659 * extract its original size (in pixels) into the `size' field.
1662 #ifndef PANGO_PRE_1POINT6
1665 PangoFontDescription *desc;
1667 PangoFontMetrics *metrics;
1668 char *newname, *retname;
1670 desc = pango_font_description_from_string(name);
1673 ctx = gtk_widget_get_pango_context(widget);
1675 pango_font_description_free(desc);
1678 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1679 pango_font_description_free(desc);
1682 #ifndef PANGO_PRE_1POINT6
1683 map = pango_context_get_font_map(ctx);
1685 pango_font_description_free(desc);
1688 fset = pango_font_map_load_fontset(map, ctx, desc,
1689 pango_context_get_language(ctx));
1691 fset = pango_context_load_fontset(ctx, desc,
1692 pango_context_get_language(ctx));
1695 pango_font_description_free(desc);
1698 metrics = pango_fontset_get_metrics(fset);
1700 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
1701 pango_font_description_free(desc);
1702 g_object_unref(fset);
1706 *size = PANGO_PIXELS(pango_font_description_get_size(desc));
1707 *flags = FONTFLAG_CLIENTSIDE;
1708 pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);
1709 newname = pango_font_description_to_string(desc);
1710 retname = dupstr(newname);
1713 pango_font_metrics_unref(metrics);
1714 pango_font_description_free(desc);
1715 g_object_unref(fset);
1720 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
1723 PangoFontDescription *desc;
1724 char *newname, *retname;
1726 desc = pango_font_description_from_string(name);
1729 pango_font_description_set_size(desc, size * PANGO_SCALE);
1730 newname = pango_font_description_to_string(desc);
1731 retname = dupstr(newname);
1733 pango_font_description_free(desc);
1738 #endif /* GTK_CHECK_VERSION(2,0,0) */
1740 /* ----------------------------------------------------------------------
1741 * Outermost functions which do the vtable dispatch.
1745 * Complete list of font-type subclasses. Listed in preference
1746 * order for unifont_create(). (That is, in the extremely unlikely
1747 * event that the same font name is valid as both a Pango and an
1748 * X11 font, it will be interpreted as the former in the absence
1749 * of an explicit type-disambiguating prefix.)
1751 * The 'multifont' subclass is omitted here, as discussed above.
1753 static const struct unifont_vtable *unifont_types[] = {
1754 #if GTK_CHECK_VERSION(2,0,0)
1757 #ifndef NOT_X_WINDOWS
1763 * Function which takes a font name and processes the optional
1764 * scheme prefix. Returns the tail of the font name suitable for
1765 * passing to individual font scheme functions, and also provides
1766 * a subrange of the unifont_types[] array above.
1768 * The return values `start' and `end' denote a half-open interval
1769 * in unifont_types[]; that is, the correct way to iterate over
1772 * for (i = start; i < end; i++) {...}
1774 static const char *unifont_do_prefix(const char *name, int *start, int *end)
1776 int colonpos = strcspn(name, ":");
1779 if (name[colonpos]) {
1781 * There's a colon prefix on the font name. Use it to work
1782 * out which subclass to use.
1784 for (i = 0; i < lenof(unifont_types); i++) {
1785 if (strlen(unifont_types[i]->prefix) == colonpos &&
1786 !strncmp(unifont_types[i]->prefix, name, colonpos)) {
1789 return name + colonpos + 1;
1793 * None matched, so return an empty scheme list to prevent
1794 * any scheme from being called at all.
1797 return name + colonpos + 1;
1800 * No colon prefix, so just use all the subclasses.
1803 *end = lenof(unifont_types);
1808 unifont *unifont_create(GtkWidget *widget, const char *name, int wide,
1809 int bold, int shadowoffset, int shadowalways)
1813 name = unifont_do_prefix(name, &start, &end);
1815 for (i = start; i < end; i++) {
1816 unifont *ret = unifont_types[i]->create(widget, name, wide, bold,
1817 shadowoffset, shadowalways);
1821 return NULL; /* font not found in any scheme */
1824 void unifont_destroy(unifont *font)
1826 font->vt->destroy(font);
1829 void unifont_draw_text(unifont_drawctx *ctx, unifont *font,
1830 int x, int y, const wchar_t *string, int len,
1831 int wide, int bold, int cellwidth)
1833 font->vt->draw_text(ctx, font, x, y, string, len, wide, bold, cellwidth);
1836 /* ----------------------------------------------------------------------
1837 * Multiple-font wrapper. This is a type of unifont which encapsulates
1838 * up to two other unifonts, permitting missing glyphs in the main
1839 * font to be filled in by a fallback font.
1841 * This is a type of unifont just like the previous two, but it has a
1842 * separate constructor which is manually called by the client, so it
1843 * doesn't appear in the list of available font types enumerated by
1844 * unifont_create. This means it's not used by unifontsel either, so
1845 * it doesn't need to support any methods except draw_text and
1849 static void multifont_draw_text(unifont_drawctx *ctx, unifont *font,
1850 int x, int y, const wchar_t *string, int len,
1851 int wide, int bold, int cellwidth);
1852 static void multifont_destroy(unifont *font);
1860 static const struct unifont_vtable multifont_vtable = {
1861 NULL, /* creation is done specially */
1865 multifont_draw_text,
1872 unifont *multifont_create(GtkWidget *widget, const char *name,
1874 int shadowoffset, int shadowalways)
1877 unifont *font, *fallback;
1878 struct multifont *mfont;
1880 font = unifont_create(widget, name, wide, bold,
1881 shadowoffset, shadowalways);
1886 if (font->want_fallback) {
1887 for (i = 0; i < lenof(unifont_types); i++) {
1888 if (unifont_types[i]->create_fallback) {
1889 fallback = unifont_types[i]->create_fallback
1890 (widget, font->height, wide, bold,
1891 shadowoffset, shadowalways);
1899 * Construct our multifont. Public members are all copied from the
1900 * primary font we're wrapping.
1902 mfont = snew(struct multifont);
1903 mfont->u.vt = &multifont_vtable;
1904 mfont->u.width = font->width;
1905 mfont->u.ascent = font->ascent;
1906 mfont->u.descent = font->descent;
1907 mfont->u.height = font->height;
1908 mfont->u.public_charset = font->public_charset;
1909 mfont->u.want_fallback = FALSE; /* shouldn't be needed, but just in case */
1910 mfont->u.preferred_drawtype = font->preferred_drawtype;
1912 mfont->fallback = fallback;
1914 return (unifont *)mfont;
1917 static void multifont_destroy(unifont *font)
1919 struct multifont *mfont = (struct multifont *)font;
1920 unifont_destroy(mfont->main);
1921 if (mfont->fallback)
1922 unifont_destroy(mfont->fallback);
1926 static void multifont_draw_text(unifont_drawctx *ctx, unifont *font, int x,
1927 int y, const wchar_t *string, int len,
1928 int wide, int bold, int cellwidth)
1930 struct multifont *mfont = (struct multifont *)font;
1936 * Find a maximal sequence of characters which are, or are
1937 * not, supported by our main font.
1939 ok = mfont->main->vt->has_glyph(mfont->main, string[0]);
1942 !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok;
1948 f = ok ? mfont->main : mfont->fallback;
1950 unifont_draw_text(ctx, f, x, y, string, i, wide, bold, cellwidth);
1957 #if GTK_CHECK_VERSION(2,0,0)
1959 /* ----------------------------------------------------------------------
1960 * Implementation of a unified font selector. Used on GTK 2 only;
1961 * for GTK 1 we still use the standard font selector.
1964 typedef struct fontinfo fontinfo;
1966 typedef struct unifontsel_internal {
1967 /* This must be the structure's first element, for cross-casting */
1969 GtkListStore *family_model, *style_model, *size_model;
1970 GtkWidget *family_list, *style_list, *size_entry, *size_list;
1971 GtkWidget *filter_buttons[4];
1972 int n_filter_buttons;
1973 GtkWidget *preview_area;
1974 #ifndef NO_BACKING_PIXMAPS
1975 GdkPixmap *preview_pixmap;
1977 int preview_width, preview_height;
1978 GdkColor preview_fg, preview_bg;
1980 tree234 *fonts_by_realname, *fonts_by_selorder;
1982 int selsize, intendedsize;
1983 int inhibit_response; /* inhibit callbacks when we change GUI controls */
1984 } unifontsel_internal;
1987 * The structure held in the tree234s. All the string members are
1988 * part of the same allocated area, so don't need freeing
1993 char *family, *charset, *style, *stylekey;
1996 * Fallback sorting key, to permit multiple identical entries
1997 * to exist in the selorder tree.
2001 * Indices mapping fontinfo structures to indices in the list
2002 * boxes. sizeindex is irrelevant if the font is scalable
2005 int familyindex, styleindex, sizeindex;
2007 * The class of font.
2009 const struct unifont_vtable *fontclass;
2012 struct fontinfo_realname_find {
2013 const char *realname;
2017 static int strnullcasecmp(const char *a, const char *b)
2022 * If exactly one of the inputs is NULL, it compares before
2025 if ((i = (!b) - (!a)) != 0)
2029 * NULL compares equal.
2035 * Otherwise, ordinary strcasecmp.
2037 return g_ascii_strcasecmp(a, b);
2040 static int fontinfo_realname_compare(void *av, void *bv)
2042 fontinfo *a = (fontinfo *)av;
2043 fontinfo *b = (fontinfo *)bv;
2046 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
2048 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2049 return ((a->flags & FONTFLAG_SORT_MASK) <
2050 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2054 static int fontinfo_realname_find(void *av, void *bv)
2056 struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;
2057 fontinfo *b = (fontinfo *)bv;
2060 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
2062 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2063 return ((a->flags & FONTFLAG_SORT_MASK) <
2064 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2068 static int fontinfo_selorder_compare(void *av, void *bv)
2070 fontinfo *a = (fontinfo *)av;
2071 fontinfo *b = (fontinfo *)bv;
2073 if ((i = strnullcasecmp(a->family, b->family)) != 0)
2076 * Font class comes immediately after family, so that fonts
2077 * from different classes with the same family
2079 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2080 return ((a->flags & FONTFLAG_SORT_MASK) <
2081 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2082 if ((i = strnullcasecmp(a->charset, b->charset)) != 0)
2084 if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)
2086 if ((i = strnullcasecmp(a->style, b->style)) != 0)
2088 if (a->size != b->size)
2089 return (a->size < b->size ? -1 : +1);
2090 if (a->index != b->index)
2091 return (a->index < b->index ? -1 : +1);
2095 static void unifontsel_deselect(unifontsel_internal *fs)
2097 fs->selected = NULL;
2098 gtk_list_store_clear(fs->style_model);
2099 gtk_list_store_clear(fs->size_model);
2100 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
2101 gtk_widget_set_sensitive(fs->size_entry, FALSE);
2104 static void unifontsel_setup_familylist(unifontsel_internal *fs)
2107 int i, listindex, minpos = -1, maxpos = -1;
2108 char *currfamily = NULL;
2112 gtk_list_store_clear(fs->family_model);
2116 * Search through the font tree for anything matching our
2117 * current filter criteria. When we find one, add its font
2118 * name to the list box.
2120 for (i = 0 ;; i++) {
2121 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2123 * info may be NULL if we've just run off the end of the
2124 * tree. We must still do a processing pass in that
2125 * situation, in case we had an unfinished font record in
2128 if (info && (info->flags &~ fs->filter_flags)) {
2129 info->familyindex = -1;
2130 continue; /* we're filtering out this font */
2132 if (!info || strnullcasecmp(currfamily, info->family) ||
2133 currflags != (info->flags & FONTFLAG_SORT_MASK)) {
2135 * We've either finished a family, or started a new
2139 gtk_list_store_append(fs->family_model, &iter);
2140 gtk_list_store_set(fs->family_model, &iter,
2141 0, currfamily, 1, minpos, 2, maxpos+1, -1);
2146 currfamily = info->family;
2147 currflags = info->flags & FONTFLAG_SORT_MASK;
2151 break; /* now we're done */
2152 info->familyindex = listindex;
2157 * If we've just filtered out the previously selected font,
2158 * deselect it thoroughly.
2160 if (fs->selected && fs->selected->familyindex < 0)
2161 unifontsel_deselect(fs);
2164 static void unifontsel_setup_stylelist(unifontsel_internal *fs,
2168 int i, listindex, minpos = -1, maxpos = -1, started = FALSE;
2169 char *currcs = NULL, *currstyle = NULL;
2172 gtk_list_store_clear(fs->style_model);
2177 * Search through the font tree for anything matching our
2178 * current filter criteria. When we find one, add its charset
2179 * and/or style name to the list box.
2181 for (i = start; i <= end; i++) {
2185 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2187 * info may be NULL if we've just run off the end of the
2188 * relevant data. We must still do a processing pass in
2189 * that situation, in case we had an unfinished font
2190 * record in progress.
2192 if (info && (info->flags &~ fs->filter_flags)) {
2193 info->styleindex = -1;
2194 continue; /* we're filtering out this font */
2196 if (!info || !started || strnullcasecmp(currcs, info->charset) ||
2197 strnullcasecmp(currstyle, info->style)) {
2199 * We've either finished a style/charset, or started a
2204 gtk_list_store_append(fs->style_model, &iter);
2205 gtk_list_store_set(fs->style_model, &iter,
2206 0, currstyle, 1, minpos, 2, maxpos+1,
2212 if (info->charset && strnullcasecmp(currcs, info->charset)) {
2213 gtk_list_store_append(fs->style_model, &iter);
2214 gtk_list_store_set(fs->style_model, &iter,
2215 0, info->charset, 1, -1, 2, -1,
2219 currcs = info->charset;
2220 currstyle = info->style;
2224 break; /* now we're done */
2225 info->styleindex = listindex;
2230 static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };
2232 static void unifontsel_setup_sizelist(unifontsel_internal *fs,
2240 gtk_list_store_clear(fs->size_model);
2244 * Search through the font tree for anything matching our
2245 * current filter criteria. When we find one, add its font
2246 * name to the list box.
2248 for (i = start; i < end; i++) {
2249 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2250 if (info->flags &~ fs->filter_flags) {
2251 info->sizeindex = -1;
2252 continue; /* we're filtering out this font */
2255 sprintf(sizetext, "%d", info->size);
2256 info->sizeindex = listindex;
2257 gtk_list_store_append(fs->size_model, &iter);
2258 gtk_list_store_set(fs->size_model, &iter,
2259 0, sizetext, 1, i, 2, info->size, -1);
2267 for (j = 0; j < lenof(unifontsel_default_sizes); j++) {
2268 sprintf(sizetext, "%d", unifontsel_default_sizes[j]);
2269 gtk_list_store_append(fs->size_model, &iter);
2270 gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,
2271 2, unifontsel_default_sizes[j], -1);
2278 static void unifontsel_set_filter_buttons(unifontsel_internal *fs)
2282 for (i = 0; i < fs->n_filter_buttons; i++) {
2283 int flagbit = GPOINTER_TO_INT(g_object_get_data
2284 (G_OBJECT(fs->filter_buttons[i]),
2286 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),
2287 !!(fs->filter_flags & flagbit));
2291 static void unifontsel_draw_preview_text_inner(unifont_drawctx *dctx,
2292 unifontsel_internal *fs)
2295 char *sizename = NULL;
2296 fontinfo *info = fs->selected;
2299 sizename = info->fontclass->scale_fontname
2300 (GTK_WIDGET(fs->u.window), info->realname, fs->selsize);
2301 font = info->fontclass->create(GTK_WIDGET(fs->u.window),
2302 sizename ? sizename : info->realname,
2303 FALSE, FALSE, 0, 0);
2308 #ifdef DRAW_TEXT_GDK
2309 if (dctx->type == DRAWTYPE_GDK) {
2310 gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_bg);
2311 gdk_draw_rectangle(dctx->u.gdk.target, dctx->u.gdk.gc, 1, 0, 0,
2312 fs->preview_width, fs->preview_height);
2313 gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_fg);
2316 #ifdef DRAW_TEXT_CAIRO
2317 if (dctx->type == DRAWTYPE_CAIRO) {
2318 cairo_set_source_rgb(dctx->u.cairo.cr,
2319 fs->preview_bg.red / 65535.0,
2320 fs->preview_bg.green / 65535.0,
2321 fs->preview_bg.blue / 65535.0);
2322 cairo_paint(dctx->u.cairo.cr);
2323 cairo_set_source_rgb(dctx->u.cairo.cr,
2324 fs->preview_fg.red / 65535.0,
2325 fs->preview_fg.green / 65535.0,
2326 fs->preview_fg.blue / 65535.0);
2331 * The pangram used here is rather carefully
2332 * constructed: it contains a sequence of very narrow
2333 * letters (`jil') and a pair of adjacent very wide
2336 * If the user selects a proportional font, it will be
2337 * coerced into fixed-width character cells when used
2338 * in the actual terminal window. We therefore display
2339 * it the same way in the preview pane, so as to show
2340 * it the way it will actually be displayed - and we
2341 * deliberately pick a pangram which will show the
2342 * resulting miskerning at its worst.
2344 * We aren't trying to sell people these fonts; we're
2345 * trying to let them make an informed choice. Better
2346 * that they find out the problems with using
2347 * proportional fonts in terminal windows here than
2348 * that they go to the effort of selecting their font
2349 * and _then_ realise it was a mistake.
2351 info->fontclass->draw_text(dctx, font,
2353 L"bankrupt jilted showmen quiz convex fogey",
2354 41, FALSE, FALSE, font->width);
2355 info->fontclass->draw_text(dctx, font,
2356 0, font->ascent + font->height,
2357 L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
2358 41, FALSE, FALSE, font->width);
2360 * The ordering of punctuation here is also selected
2361 * with some specific aims in mind. I put ` and '
2362 * together because some software (and people) still
2363 * use them as matched quotes no matter what Unicode
2364 * might say on the matter, so people can quickly
2365 * check whether they look silly in a candidate font.
2366 * The sequence #_@ is there to let people judge the
2367 * suitability of the underscore as an effectively
2368 * alphabetic character (since that's how it's often
2369 * used in practice, at least by programmers).
2371 info->fontclass->draw_text(dctx, font,
2372 0, font->ascent + font->height * 2,
2373 L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
2374 42, FALSE, FALSE, font->width);
2376 info->fontclass->destroy(font);
2382 static void unifontsel_draw_preview_text(unifontsel_internal *fs)
2384 unifont_drawctx dctx;
2387 #ifndef NO_BACKING_PIXMAPS
2388 target = fs->preview_pixmap;
2390 target = gtk_widget_get_window(fs->preview_area);
2392 if (!target) /* we may be called when we haven't created everything yet */
2395 dctx.type = DRAWTYPE_DEFAULT;
2396 #ifdef DRAW_TEXT_GDK
2397 if (dctx.type == DRAWTYPE_GDK) {
2398 dctx.u.gdk.target = target;
2399 dctx.u.gdk.gc = gdk_gc_new(target);
2402 #ifdef DRAW_TEXT_CAIRO
2403 if (dctx.type == DRAWTYPE_CAIRO) {
2404 dctx.u.cairo.widget = GTK_WIDGET(fs->preview_area);
2405 dctx.u.cairo.cr = gdk_cairo_create(target);
2409 unifontsel_draw_preview_text_inner(&dctx, fs);
2411 #ifdef DRAW_TEXT_GDK
2412 if (dctx.type == DRAWTYPE_GDK) {
2413 gdk_gc_unref(dctx.u.gdk.gc);
2416 #ifdef DRAW_TEXT_CAIRO
2417 if (dctx.type == DRAWTYPE_CAIRO) {
2418 cairo_destroy(dctx.u.cairo.cr);
2422 gdk_window_invalidate_rect(gtk_widget_get_window(fs->preview_area),
2426 static void unifontsel_select_font(unifontsel_internal *fs,
2427 fontinfo *info, int size, int leftlist,
2428 int size_is_explicit)
2432 GtkTreePath *treepath;
2435 fs->inhibit_response = TRUE;
2437 fs->selected = info;
2439 if (size_is_explicit)
2440 fs->intendedsize = size;
2442 gtk_widget_set_sensitive(fs->u.ok_button, TRUE);
2445 * Find the index of this fontinfo in the selorder list.
2448 findpos234(fs->fonts_by_selorder, info, NULL, &index);
2452 * Adjust the font selector flags and redo the font family
2453 * list box, if necessary.
2455 if (leftlist <= 0 &&
2456 (fs->filter_flags | info->flags) != fs->filter_flags) {
2457 fs->filter_flags |= info->flags;
2458 unifontsel_set_filter_buttons(fs);
2459 unifontsel_setup_familylist(fs);
2463 * Find the appropriate family name and select it in the list.
2465 assert(info->familyindex >= 0);
2466 treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);
2467 gtk_tree_selection_select_path
2468 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),
2470 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),
2471 treepath, NULL, FALSE, 0.0, 0.0);
2472 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, treepath);
2473 gtk_tree_path_free(treepath);
2476 * Now set up the font style list.
2478 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,
2479 1, &minval, 2, &maxval, -1);
2481 unifontsel_setup_stylelist(fs, minval, maxval);
2484 * Find the appropriate style name and select it in the list.
2487 assert(info->styleindex >= 0);
2488 treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);
2489 gtk_tree_selection_select_path
2490 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),
2492 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),
2493 treepath, NULL, FALSE, 0.0, 0.0);
2494 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),
2496 gtk_tree_path_free(treepath);
2499 * And set up the size list.
2501 gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,
2502 1, &minval, 2, &maxval, -1);
2504 unifontsel_setup_sizelist(fs, minval, maxval);
2507 * Find the appropriate size, and select it in the list.
2510 assert(info->sizeindex >= 0);
2511 treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);
2512 gtk_tree_selection_select_path
2513 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),
2515 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2516 treepath, NULL, FALSE, 0.0, 0.0);
2517 gtk_tree_path_free(treepath);
2521 for (j = 0; j < lenof(unifontsel_default_sizes); j++)
2522 if (unifontsel_default_sizes[j] == size) {
2523 treepath = gtk_tree_path_new_from_indices(j, -1);
2524 gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),
2525 treepath, NULL, FALSE);
2526 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2527 treepath, NULL, FALSE, 0.0,
2529 gtk_tree_path_free(treepath);
2534 * And set up the font size text entry box.
2538 sprintf(sizetext, "%d", size);
2539 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);
2543 unifontsel_setup_sizelist(fs, 0, 0);
2544 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");
2548 * Grey out the font size edit box if we're not using a
2551 gtk_editable_set_editable(GTK_EDITABLE(fs->size_entry),
2552 fs->selected->size == 0);
2553 gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);
2555 unifontsel_draw_preview_text(fs);
2557 fs->inhibit_response = FALSE;
2560 static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)
2562 unifontsel_internal *fs = (unifontsel_internal *)data;
2563 int newstate = gtk_toggle_button_get_active(tb);
2565 int flagbit = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tb),
2569 newflags = fs->filter_flags | flagbit;
2571 newflags = fs->filter_flags & ~flagbit;
2573 if (fs->filter_flags != newflags) {
2574 fs->filter_flags = newflags;
2575 unifontsel_setup_familylist(fs);
2579 static void unifontsel_add_entry(void *ctx, const char *realfontname,
2580 const char *family, const char *charset,
2581 const char *style, const char *stylekey,
2582 int size, int flags,
2583 const struct unifont_vtable *fontclass)
2585 unifontsel_internal *fs = (unifontsel_internal *)ctx;
2590 totalsize = sizeof(fontinfo) + strlen(realfontname) +
2591 (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +
2592 (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;
2593 info = (fontinfo *)smalloc(totalsize);
2594 info->fontclass = fontclass;
2595 p = (char *)info + sizeof(fontinfo);
2597 strcpy(p, realfontname);
2604 info->family = NULL;
2610 info->charset = NULL;
2619 strcpy(p, stylekey);
2622 info->stylekey = NULL;
2623 assert(p - (char *)info <= totalsize);
2625 info->flags = flags;
2626 info->index = count234(fs->fonts_by_selorder);
2629 * It's just conceivable that a misbehaving font enumerator
2630 * might tell us about the same font real name more than once,
2631 * in which case we should silently drop the new one.
2633 if (add234(fs->fonts_by_realname, info) != info) {
2638 * However, we should never get a duplicate key in the
2639 * selorder tree, because the index field carefully
2640 * disambiguates otherwise identical records.
2642 add234(fs->fonts_by_selorder, info);
2645 static fontinfo *update_for_intended_size(unifontsel_internal *fs,
2648 fontinfo info2, *below, *above;
2652 * Copy the info structure. This doesn't copy its dynamic
2653 * string fields, but that's unimportant because all we're
2654 * going to do is to adjust the size field and use it in one
2658 info2.size = fs->intendedsize;
2661 * Search in the tree to find the fontinfo structure which
2662 * best approximates the size the user last requested.
2664 below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
2668 above = index234(fs->fonts_by_selorder, pos+1);
2671 * See if we've found it exactly, which is an easy special
2672 * case. If we have, it'll be in `below' and not `above',
2673 * because we did a REL234_LE rather than REL234_LT search.
2675 if (below && !fontinfo_selorder_compare(&info2, below))
2679 * Now we've either found two suitable fonts, one smaller and
2680 * one larger, or we're at one or other extreme end of the
2681 * scale. Find out which, by NULLing out either of below and
2682 * above if it differs from this one in any respect but size
2683 * (and the disambiguating index field). Bear in mind, also,
2684 * that either one might _already_ be NULL if we're at the
2685 * extreme ends of the font list.
2688 info2.size = below->size;
2689 info2.index = below->index;
2690 if (fontinfo_selorder_compare(&info2, below))
2694 info2.size = above->size;
2695 info2.index = above->index;
2696 if (fontinfo_selorder_compare(&info2, above))
2701 * Now return whichever of above and below is non-NULL, if
2702 * that's unambiguous.
2710 * And now we really do have to make a choice about whether to
2711 * round up or down. We'll do it by rounding to nearest,
2712 * breaking ties by rounding up.
2714 if (above->size - fs->intendedsize <= fs->intendedsize - below->size)
2720 static void family_changed(GtkTreeSelection *treeselection, gpointer data)
2722 unifontsel_internal *fs = (unifontsel_internal *)data;
2723 GtkTreeModel *treemodel;
2724 GtkTreeIter treeiter;
2728 if (fs->inhibit_response) /* we made this change ourselves */
2731 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2734 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2735 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2736 info = update_for_intended_size(fs, info);
2738 return; /* _shouldn't_ happen unless font list is completely funted */
2740 fs->selsize = fs->intendedsize; /* font is scalable */
2741 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2745 static void style_changed(GtkTreeSelection *treeselection, gpointer data)
2747 unifontsel_internal *fs = (unifontsel_internal *)data;
2748 GtkTreeModel *treemodel;
2749 GtkTreeIter treeiter;
2753 if (fs->inhibit_response) /* we made this change ourselves */
2756 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2759 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2761 return; /* somehow a charset heading got clicked */
2762 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2763 info = update_for_intended_size(fs, info);
2765 return; /* _shouldn't_ happen unless font list is completely funted */
2767 fs->selsize = fs->intendedsize; /* font is scalable */
2768 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2772 static void size_changed(GtkTreeSelection *treeselection, gpointer data)
2774 unifontsel_internal *fs = (unifontsel_internal *)data;
2775 GtkTreeModel *treemodel;
2776 GtkTreeIter treeiter;
2780 if (fs->inhibit_response) /* we made this change ourselves */
2783 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2786 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);
2787 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2788 unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE);
2791 static void size_entry_changed(GtkEditable *ed, gpointer data)
2793 unifontsel_internal *fs = (unifontsel_internal *)data;
2797 if (fs->inhibit_response) /* we made this change ourselves */
2800 text = gtk_entry_get_text(GTK_ENTRY(ed));
2804 assert(fs->selected->size == 0);
2805 unifontsel_select_font(fs, fs->selected, size, 3, TRUE);
2809 static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,
2810 GtkTreeViewColumn *column, gpointer data)
2812 unifontsel_internal *fs = (unifontsel_internal *)data;
2814 int minval, newsize;
2815 fontinfo *info, *newinfo;
2818 if (fs->inhibit_response) /* we made this change ourselves */
2821 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);
2822 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);
2823 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2826 struct fontinfo_realname_find f;
2828 newname = info->fontclass->canonify_fontname
2829 (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE);
2831 f.realname = newname;
2833 newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
2837 return; /* font name not in our index */
2838 if (newinfo == info)
2839 return; /* didn't change under canonification => not an alias */
2840 unifontsel_select_font(fs, newinfo,
2841 newinfo->size ? newinfo->size : newsize,
2846 #if GTK_CHECK_VERSION(3,0,0)
2847 static gint unifontsel_draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
2849 unifontsel_internal *fs = (unifontsel_internal *)data;
2850 unifont_drawctx dctx;
2852 dctx.type = DRAWTYPE_CAIRO;
2853 dctx.u.cairo.widget = widget;
2854 dctx.u.cairo.cr = cr;
2855 unifontsel_draw_preview_text_inner(&dctx, fs);
2860 static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,
2863 unifontsel_internal *fs = (unifontsel_internal *)data;
2865 #ifndef NO_BACKING_PIXMAPS
2866 if (fs->preview_pixmap) {
2867 gdk_draw_pixmap(gtk_widget_get_window(widget),
2868 (gtk_widget_get_style(widget)->fg_gc
2869 [gtk_widget_get_state(widget)]),
2871 event->area.x, event->area.y,
2872 event->area.x, event->area.y,
2873 event->area.width, event->area.height);
2876 unifontsel_draw_preview_text(fs);
2883 static gint unifontsel_configure_area(GtkWidget *widget,
2884 GdkEventConfigure *event, gpointer data)
2886 #ifndef NO_BACKING_PIXMAPS
2887 unifontsel_internal *fs = (unifontsel_internal *)data;
2888 int ox, oy, nx, ny, x, y;
2891 * Enlarge the pixmap, but never shrink it.
2893 ox = fs->preview_width;
2894 oy = fs->preview_height;
2897 if (x > ox || y > oy) {
2898 if (fs->preview_pixmap)
2899 gdk_pixmap_unref(fs->preview_pixmap);
2901 nx = (x > ox ? x : ox);
2902 ny = (y > oy ? y : oy);
2903 fs->preview_pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
2905 fs->preview_width = nx;
2906 fs->preview_height = ny;
2908 unifontsel_draw_preview_text(fs);
2912 gdk_window_invalidate_rect(gtk_widget_get_window(widget), NULL, FALSE);
2917 unifontsel *unifontsel_new(const char *wintitle)
2919 unifontsel_internal *fs = snew(unifontsel_internal);
2920 GtkWidget *table, *label, *w, *ww, *scroll;
2921 GtkListStore *model;
2922 GtkTreeViewColumn *column;
2923 int lists_height, preview_height, font_width, style_width, size_width;
2926 fs->inhibit_response = FALSE;
2927 fs->selected = NULL;
2931 * Invent some magic size constants.
2934 label = gtk_label_new("Quite Long Font Name (Foundry)");
2935 gtk_widget_size_request(label, &req);
2936 font_width = req.width;
2937 lists_height = 14 * req.height;
2938 preview_height = 5 * req.height;
2939 gtk_label_set_text(GTK_LABEL(label), "Italic Extra Condensed");
2940 gtk_widget_size_request(label, &req);
2941 style_width = req.width;
2942 gtk_label_set_text(GTK_LABEL(label), "48000");
2943 gtk_widget_size_request(label, &req);
2944 size_width = req.width;
2945 g_object_ref_sink(G_OBJECT(label));
2946 #if GTK_CHECK_VERSION(2,10,0)
2947 g_object_unref(label);
2952 * Create the dialog box and initialise the user-visible
2953 * fields in the returned structure.
2955 fs->u.user_data = NULL;
2956 fs->u.window = GTK_WINDOW(gtk_dialog_new());
2957 gtk_window_set_title(fs->u.window, wintitle);
2958 fs->u.cancel_button = gtk_dialog_add_button
2959 (GTK_DIALOG(fs->u.window), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
2960 fs->u.ok_button = gtk_dialog_add_button
2961 (GTK_DIALOG(fs->u.window), GTK_STOCK_OK, GTK_RESPONSE_OK);
2962 gtk_widget_grab_default(fs->u.ok_button);
2965 * Now set up the internal fields, including in particular all
2966 * the controls that actually allow the user to select fonts.
2968 table = gtk_table_new(8, 3, FALSE);
2969 gtk_widget_show(table);
2970 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
2971 #if GTK_CHECK_VERSION(2,4,0)
2972 /* GtkAlignment seems to be the simplest way to put padding round things */
2973 w = gtk_alignment_new(0, 0, 1, 1);
2974 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
2975 gtk_container_add(GTK_CONTAINER(w), table);
2980 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area
2981 (GTK_DIALOG(fs->u.window))),
2984 label = gtk_label_new_with_mnemonic("_Font:");
2985 gtk_widget_show(label);
2986 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
2987 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
2990 * The Font list box displays only a string, but additionally
2991 * stores two integers which give the limits within the
2992 * tree234 of the font entries covered by this list entry.
2994 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
2995 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
2996 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
2997 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
2999 column = gtk_tree_view_column_new_with_attributes
3000 ("Font", gtk_cell_renderer_text_new(),
3001 "text", 0, (char *)NULL);
3002 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3003 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3004 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3005 "changed", G_CALLBACK(family_changed), fs);
3006 g_signal_connect(G_OBJECT(w), "row-activated",
3007 G_CALLBACK(alias_resolve), fs);
3009 scroll = gtk_scrolled_window_new(NULL, NULL);
3010 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3012 gtk_container_add(GTK_CONTAINER(scroll), w);
3013 gtk_widget_show(scroll);
3014 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3015 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3016 gtk_widget_set_size_request(scroll, font_width, lists_height);
3017 gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,
3018 GTK_EXPAND | GTK_FILL, 0, 0);
3019 fs->family_model = model;
3020 fs->family_list = w;
3022 label = gtk_label_new_with_mnemonic("_Style:");
3023 gtk_widget_show(label);
3024 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
3025 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
3028 * The Style list box can contain insensitive elements
3029 * (character set headings for server-side fonts), so we add
3030 * an extra column to the list store to hold that information.
3032 model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
3034 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
3035 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
3036 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
3038 column = gtk_tree_view_column_new_with_attributes
3039 ("Style", gtk_cell_renderer_text_new(),
3040 "text", 0, "sensitive", 3, (char *)NULL);
3041 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3042 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3043 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3044 "changed", G_CALLBACK(style_changed), fs);
3046 scroll = gtk_scrolled_window_new(NULL, NULL);
3047 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3049 gtk_container_add(GTK_CONTAINER(scroll), w);
3050 gtk_widget_show(scroll);
3051 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3052 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3053 gtk_widget_set_size_request(scroll, style_width, lists_height);
3054 gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,
3055 GTK_EXPAND | GTK_FILL, 0, 0);
3056 fs->style_model = model;
3059 label = gtk_label_new_with_mnemonic("Si_ze:");
3060 gtk_widget_show(label);
3061 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
3062 gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
3065 * The Size label attaches primarily to a text input box so
3066 * that the user can select a size of their choice. The list
3067 * of available sizes is secondary.
3069 fs->size_entry = w = gtk_entry_new();
3070 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
3071 gtk_widget_set_size_request(w, size_width, -1);
3073 gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);
3074 g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),
3077 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
3078 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
3079 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
3081 column = gtk_tree_view_column_new_with_attributes
3082 ("Size", gtk_cell_renderer_text_new(),
3083 "text", 0, (char *)NULL);
3084 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3085 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3086 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3087 "changed", G_CALLBACK(size_changed), fs);
3089 scroll = gtk_scrolled_window_new(NULL, NULL);
3090 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3092 gtk_container_add(GTK_CONTAINER(scroll), w);
3093 gtk_widget_show(scroll);
3094 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3095 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3096 gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,
3097 GTK_EXPAND | GTK_FILL, 0, 0);
3098 fs->size_model = model;
3104 fs->preview_area = gtk_drawing_area_new();
3105 #ifndef NO_BACKING_PIXMAPS
3106 fs->preview_pixmap = NULL;
3108 fs->preview_width = 0;
3109 fs->preview_height = 0;
3110 fs->preview_fg.pixel = fs->preview_bg.pixel = 0;
3111 fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;
3112 fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;
3113 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,
3115 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,
3117 #if GTK_CHECK_VERSION(3,0,0)
3118 g_signal_connect(G_OBJECT(fs->preview_area), "draw",
3119 G_CALLBACK(unifontsel_draw_area), fs);
3121 g_signal_connect(G_OBJECT(fs->preview_area), "expose_event",
3122 G_CALLBACK(unifontsel_expose_area), fs);
3124 g_signal_connect(G_OBJECT(fs->preview_area), "configure_event",
3125 G_CALLBACK(unifontsel_configure_area), fs);
3126 gtk_widget_set_size_request(fs->preview_area, 1, preview_height);
3127 gtk_widget_show(fs->preview_area);
3128 ww = fs->preview_area;
3129 w = gtk_frame_new(NULL);
3130 gtk_container_add(GTK_CONTAINER(w), ww);
3132 #if GTK_CHECK_VERSION(2,4,0)
3134 /* GtkAlignment seems to be the simplest way to put padding round things */
3135 w = gtk_alignment_new(0, 0, 1, 1);
3136 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
3137 gtk_container_add(GTK_CONTAINER(w), ww);
3141 w = gtk_frame_new("Preview of font");
3142 gtk_container_add(GTK_CONTAINER(w), ww);
3144 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,
3145 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);
3148 * We only provide the checkboxes for client- and server-side
3149 * fonts if we have the X11 back end available, because that's the
3150 * only situation in which more than one class of font is
3153 fs->n_filter_buttons = 0;
3154 #ifndef NOT_X_WINDOWS
3155 w = gtk_check_button_new_with_label("Show client-side fonts");
3156 g_object_set_data(G_OBJECT(w), "user-data",
3157 GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));
3158 g_signal_connect(G_OBJECT(w), "toggled",
3159 G_CALLBACK(unifontsel_button_toggled), fs);
3161 fs->filter_buttons[fs->n_filter_buttons++] = w;
3162 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);
3163 w = gtk_check_button_new_with_label("Show server-side fonts");
3164 g_object_set_data(G_OBJECT(w), "user-data",
3165 GINT_TO_POINTER(FONTFLAG_SERVERSIDE));
3166 g_signal_connect(G_OBJECT(w), "toggled",
3167 G_CALLBACK(unifontsel_button_toggled), fs);
3169 fs->filter_buttons[fs->n_filter_buttons++] = w;
3170 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);
3171 w = gtk_check_button_new_with_label("Show server-side font aliases");
3172 g_object_set_data(G_OBJECT(w), "user-data",
3173 GINT_TO_POINTER(FONTFLAG_SERVERALIAS));
3174 g_signal_connect(G_OBJECT(w), "toggled",
3175 G_CALLBACK(unifontsel_button_toggled), fs);
3177 fs->filter_buttons[fs->n_filter_buttons++] = w;
3178 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);
3180 w = gtk_check_button_new_with_label("Show non-monospaced fonts");
3181 g_object_set_data(G_OBJECT(w), "user-data",
3182 GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));
3183 g_signal_connect(G_OBJECT(w), "toggled",
3184 G_CALLBACK(unifontsel_button_toggled), fs);
3186 fs->filter_buttons[fs->n_filter_buttons++] = w;
3187 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);
3189 assert(fs->n_filter_buttons <= lenof(fs->filter_buttons));
3190 fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |
3191 FONTFLAG_SERVERALIAS;
3192 unifontsel_set_filter_buttons(fs);
3195 * Go and find all the font names, and set up our master font
3198 fs->fonts_by_realname = newtree234(fontinfo_realname_compare);
3199 fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);
3200 for (i = 0; i < lenof(unifont_types); i++)
3201 unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),
3202 unifontsel_add_entry, fs);
3205 * And set up the initial font names list.
3207 unifontsel_setup_familylist(fs);
3209 fs->selsize = fs->intendedsize = 13; /* random default */
3210 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
3212 return (unifontsel *)fs;
3215 void unifontsel_destroy(unifontsel *fontsel)
3217 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3220 #ifndef NO_BACKING_PIXMAPS
3221 if (fs->preview_pixmap)
3222 gdk_pixmap_unref(fs->preview_pixmap);
3225 freetree234(fs->fonts_by_selorder);
3226 while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)
3228 freetree234(fs->fonts_by_realname);
3230 gtk_widget_destroy(GTK_WIDGET(fs->u.window));
3234 void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
3236 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3237 int i, start, end, size, flags;
3238 const char *fontname2 = NULL;
3242 * Provide a default if given an empty or null font name.
3244 if (!fontname || !*fontname)
3245 fontname = "server:fixed";
3248 * Call the canonify_fontname function.
3250 fontname = unifont_do_prefix(fontname, &start, &end);
3251 for (i = start; i < end; i++) {
3252 fontname2 = unifont_types[i]->canonify_fontname
3253 (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE);
3258 return; /* font name not recognised */
3261 * Now look up the canonified font name in our index.
3264 struct fontinfo_realname_find f;
3265 f.realname = fontname2;
3267 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
3271 * If we've found the font, and its size field is either
3272 * correct or zero (the latter indicating a scalable font),
3273 * then we're done. Otherwise, try looking up the original
3274 * font name instead.
3276 if (!info || (info->size != size && info->size != 0)) {
3277 struct fontinfo_realname_find f;
3278 f.realname = fontname;
3281 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
3282 if (!info || info->size != size)
3283 return; /* font name not in our index */
3287 * Now we've got a fontinfo structure and a font size, so we
3288 * know everything we need to fill in all the fields in the
3291 unifontsel_select_font(fs, info, size, 0, TRUE);
3294 char *unifontsel_get_name(unifontsel *fontsel)
3296 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3302 if (fs->selected->size == 0) {
3303 name = fs->selected->fontclass->scale_fontname
3304 (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
3306 char *ret = dupcat(fs->selected->fontclass->prefix, ":",
3313 return dupcat(fs->selected->fontclass->prefix, ":",
3314 fs->selected->realname, NULL);
3317 #endif /* GTK_CHECK_VERSION(2,0,0) */