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
17 #if !GTK_CHECK_VERSION(3,0,0)
18 #include <gdk/gdkkeysyms.h>
21 #define MAY_REFER_TO_GTK_IN_HEADERS
25 #include "gtkcompat.h"
32 #include <X11/Xutil.h>
33 #include <X11/Xatom.h>
39 * - it would be nice to have a display of the current font name,
40 * and in particular whether it's client- or server-side,
41 * during the progress of the font selector.
44 #if !GLIB_CHECK_VERSION(1,3,7)
45 #define g_ascii_strcasecmp g_strcasecmp
46 #define g_ascii_strncasecmp g_strncasecmp
50 * Ad-hoc vtable mechanism to allow font structures to be
53 * Any instance of `unifont' used in the vtable functions will
54 * actually be the first element of a larger structure containing
55 * data specific to the subtype. This is permitted by the ISO C
56 * provision that one may safely cast between a pointer to a
57 * structure and a pointer to its first element.
60 #define FONTFLAG_CLIENTSIDE 0x0001
61 #define FONTFLAG_SERVERSIDE 0x0002
62 #define FONTFLAG_SERVERALIAS 0x0004
63 #define FONTFLAG_NONMONOSPACED 0x0008
65 #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
67 typedef void (*fontsel_add_entry)(void *ctx, const char *realfontname,
68 const char *family, const char *charset,
69 const char *style, const char *stylekey,
71 const struct unifont_vtable *fontclass);
73 struct unifont_vtable {
75 * `Methods' of the `class'.
77 unifont *(*create)(GtkWidget *widget, const char *name, int wide, int bold,
78 int shadowoffset, int shadowalways);
79 unifont *(*create_fallback)(GtkWidget *widget, int height, int wide,
80 int bold, int shadowoffset, int shadowalways);
81 void (*destroy)(unifont *font);
82 int (*has_glyph)(unifont *font, wchar_t glyph);
83 void (*draw_text)(unifont_drawctx *ctx, unifont *font,
84 int x, int y, const wchar_t *string, int len,
85 int wide, int bold, int cellwidth);
86 void (*draw_combining)(unifont_drawctx *ctx, unifont *font,
87 int x, int y, const wchar_t *string, int len,
88 int wide, int bold, int cellwidth);
89 void (*enum_fonts)(GtkWidget *widget,
90 fontsel_add_entry callback, void *callback_ctx);
91 char *(*canonify_fontname)(GtkWidget *widget, const char *name, int *size,
92 int *flags, int resolve_aliases);
93 char *(*scale_fontname)(GtkWidget *widget, const char *name, int size);
96 * `Static data members' of the `class'.
101 #ifndef NOT_X_WINDOWS
103 /* ----------------------------------------------------------------------
104 * X11 font implementation, directly using Xlib calls. Conditioned out
105 * if X11 fonts aren't available at all (e.g. building with GTK3 for a
106 * back end other than X).
109 static int x11font_has_glyph(unifont *font, wchar_t glyph);
110 static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
111 int x, int y, const wchar_t *string, int len,
112 int wide, int bold, int cellwidth);
113 static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
114 int x, int y, const wchar_t *string,
115 int len, int wide, int bold, int cellwidth);
116 static unifont *x11font_create(GtkWidget *widget, const char *name,
118 int shadowoffset, int shadowalways);
119 static void x11font_destroy(unifont *font);
120 static void x11font_enum_fonts(GtkWidget *widget,
121 fontsel_add_entry callback, void *callback_ctx);
122 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
123 int *size, int *flags,
124 int resolve_aliases);
125 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
128 #ifdef DRAW_TEXT_CAIRO
129 struct cairo_cached_glyph {
130 cairo_surface_t *surface;
131 unsigned char *bitmap;
136 * Structure storing a single physical XFontStruct, plus associated
139 typedef struct x11font_individual {
140 /* The XFontStruct itself. */
144 * The `allocated' flag indicates whether we've tried to fetch
145 * this subfont already (thus distinguishing xfs==NULL because we
146 * haven't tried yet from xfs==NULL because we tried and failed,
147 * so that we don't keep trying and failing subsequently).
151 #ifdef DRAW_TEXT_CAIRO
153 * A cache of glyph bitmaps downloaded from the X server when
154 * we're in Cairo rendering mode. If glyphcache itself is
155 * non-NULL, then entries in [0,nglyphs) are expected to be
156 * initialised to either NULL or a bitmap pointer.
158 struct cairo_cached_glyph *glyphcache;
162 * X server paraphernalia for actually downloading the glyphs.
166 int pixwidth, pixheight, pixoriginx, pixoriginy;
169 * Paraphernalia for loading the resulting bitmaps into Cairo.
171 int rowsize, allsize, indexflip;
174 } x11font_individual;
179 * Individual physical X fonts. We store a number of these, for
180 * automatically guessed bold and wide variants.
182 x11font_individual fonts[4];
184 * `sixteen_bit' is true iff the font object is indexed by
185 * values larger than a byte. That is, this flag tells us
186 * whether we use XDrawString or XDrawString16, etc.
190 * `variable' is true iff the font is non-fixed-pitch. This
191 * enables some code which takes greater care over character
192 * positioning during text drawing.
196 * real_charset is the charset used when translating text into the
197 * font's internal encoding inside draw_text(). This need not be
198 * the same as the public_charset provided to the client; for
199 * example, public_charset might be CS_ISO8859_1 while
200 * real_charset is CS_ISO8859_1_X11.
204 * Data passed in to unifont_create().
206 int wide, bold, shadowoffset, shadowalways;
209 static const struct unifont_vtable x11font_vtable = {
211 NULL, /* no fallback fonts in X11 */
215 x11font_draw_combining,
217 x11font_canonify_fontname,
218 x11font_scale_fontname,
222 static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide)
224 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
225 Atom fontprop = XInternAtom(disp, "FONT", False);
227 if (XGetFontProperty(xfs, fontprop, &ret)) {
228 char *name = XGetAtomName(disp, (Atom)ret);
229 if (name && name[0] == '-') {
230 const char *strings[13];
231 char *dupname, *extrafree = NULL, *ret;
235 p = q = dupname = dupstr(name); /* skip initial minus */
238 while (*p && nstr < lenof(strings)) {
241 strings[nstr++] = p+1;
246 if (nstr < lenof(strings)) {
248 return NULL; /* XLFD was malformed */
255 /* 4 is `wideness', which obviously may have changed. */
256 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
257 strings[4] = strings[5] = "*";
258 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
261 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
262 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
263 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
264 "-", strings[ 9], "-", strings[10], "-", strings[11],
265 "-", strings[12], NULL);
275 static int x11_font_width(XFontStruct *xfs, int sixteen_bit)
281 return XTextWidth16(xfs, &space, 1);
283 return XTextWidth(xfs, "0", 1);
287 static const XCharStruct *x11_char_struct(XFontStruct *xfs,
288 int byte1, int byte2)
293 * The man page for XQueryFont is rather confusing about how the
294 * per_char array in the XFontStruct is laid out, because it gives
295 * formulae for determining the two-byte X character code _from_
296 * an index into the per_char array. Going the other way, it's
299 * The valid character codes have byte1 between min_byte1 and
300 * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
301 * max_char_or_byte2 inclusive. This gives a rectangle of size
302 * (max_byte2-min_byte1+1) by
303 * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
304 * rectangle encoded in the per_char array. Hence, given a
305 * character code which is valid in the sense that it falls
306 * somewhere in that rectangle, its index in per_char is given by
309 * x = byte2 - min_char_or_byte2
310 * y = byte1 - min_byte1
311 * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
313 * If min_byte1 and min_byte2 are both zero, that's a special case
314 * which can be treated as if min_byte2 was 1 instead, i.e. the
315 * per_char array just runs from min_char_or_byte2 to
316 * max_char_or_byte2 inclusive, and byte1 should always be zero.
319 if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2)
322 if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
323 index = byte2 - xfs->min_char_or_byte2;
325 if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1)
327 index = ((byte2 - xfs->min_char_or_byte2) +
328 ((byte1 - xfs->min_byte1) *
329 (xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1)));
332 if (!xfs->per_char) /* per_char NULL => everything in range exists */
333 return &xfs->max_bounds;
335 return &xfs->per_char[index];
338 static int x11_font_has_glyph(XFontStruct *xfs, int byte1, int byte2)
341 * Not to be confused with x11font_has_glyph, which is a method of
342 * the x11font 'class' and hence takes a unifont as argument. This
343 * is the low-level function which grubs about in an actual
344 * XFontStruct to see if a given glyph exists.
346 * We must do this ourselves rather than letting Xlib's
347 * XTextExtents16 do the job, because XTextExtents will helpfully
348 * substitute the font's default_char for any missing glyph and
349 * not tell us it did so, which precisely won't help us find out
350 * which glyphs _are_ missing.
352 const XCharStruct *xcs = x11_char_struct(xfs, byte1, byte2);
353 return xcs && (xcs->ascent + xcs->descent > 0 || xcs->width > 0);
356 static unifont *x11font_create(GtkWidget *widget, const char *name,
358 int shadowoffset, int shadowalways)
360 struct x11font *xfont;
362 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
363 Atom charset_registry, charset_encoding, spacing;
364 unsigned long registry_ret, encoding_ret, spacing_ret;
365 int pubcs, realcs, sixteen_bit, variable;
368 xfs = XLoadQueryFont(disp, name);
372 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
373 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
375 pubcs = realcs = CS_NONE;
379 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
380 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
382 reg = XGetAtomName(disp, (Atom)registry_ret);
383 enc = XGetAtomName(disp, (Atom)encoding_ret);
385 char *encoding = dupcat(reg, "-", enc, NULL);
386 pubcs = realcs = charset_from_xenc(encoding);
389 * iso10646-1 is the only wide font encoding we
390 * support. In this case, we expect clients to give us
391 * UTF-8, which this module must internally convert
392 * into 16-bit Unicode.
394 if (!strcasecmp(encoding, "iso10646-1")) {
396 pubcs = realcs = CS_UTF8;
400 * Hack for X line-drawing characters: if the primary font
401 * is encoded as ISO-8859-1, and has valid glyphs in the
402 * low character positions, it is assumed that those
403 * glyphs are the VT100 line-drawing character set.
405 if (pubcs == CS_ISO8859_1) {
407 for (ch = 1; ch < 32; ch++)
408 if (!x11_font_has_glyph(xfs, 0, ch))
411 realcs = CS_ISO8859_1_X11;
418 spacing = XInternAtom(disp, "SPACING", False);
419 if (XGetFontProperty(xfs, spacing, &spacing_ret)) {
421 spc = XGetAtomName(disp, (Atom)spacing_ret);
423 if (spc && strchr("CcMm", spc[0]))
427 xfont = snew(struct x11font);
428 xfont->u.vt = &x11font_vtable;
429 xfont->u.width = x11_font_width(xfs, sixteen_bit);
430 xfont->u.ascent = xfs->ascent;
431 xfont->u.descent = xfs->descent;
432 xfont->u.height = xfont->u.ascent + xfont->u.descent;
433 xfont->u.public_charset = pubcs;
434 xfont->u.want_fallback = TRUE;
436 xfont->u.preferred_drawtype = DRAWTYPE_GDK;
437 #elif defined DRAW_TEXT_CAIRO
438 xfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
440 #error No drawtype available at all
442 xfont->real_charset = realcs;
443 xfont->sixteen_bit = sixteen_bit;
444 xfont->variable = variable;
447 xfont->shadowoffset = shadowoffset;
448 xfont->shadowalways = shadowalways;
450 for (i = 0; i < lenof(xfont->fonts); i++) {
451 xfont->fonts[i].xfs = NULL;
452 xfont->fonts[i].allocated = FALSE;
453 #ifdef DRAW_TEXT_CAIRO
454 xfont->fonts[i].glyphcache = NULL;
455 xfont->fonts[i].nglyphs = 0;
456 xfont->fonts[i].pixmap = None;
457 xfont->fonts[i].gc = None;
460 xfont->fonts[0].xfs = xfs;
461 xfont->fonts[0].allocated = TRUE;
463 return (unifont *)xfont;
466 static void x11font_destroy(unifont *font)
468 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
469 struct x11font *xfont = (struct x11font *)font;
472 for (i = 0; i < lenof(xfont->fonts); i++) {
473 if (xfont->fonts[i].xfs)
474 XFreeFont(disp, xfont->fonts[i].xfs);
475 #ifdef DRAW_TEXT_CAIRO
476 if (xfont->fonts[i].gc != None)
477 XFreeGC(disp, xfont->fonts[i].gc);
478 if (xfont->fonts[i].pixmap != None)
479 XFreePixmap(disp, xfont->fonts[i].pixmap);
480 if (xfont->fonts[i].glyphcache) {
482 for (j = 0; j < xfont->fonts[i].nglyphs; j++) {
483 cairo_surface_destroy(xfont->fonts[i].glyphcache[j].surface);
484 sfree(xfont->fonts[i].glyphcache[j].bitmap);
486 sfree(xfont->fonts[i].glyphcache);
493 static void x11_alloc_subfont(struct x11font *xfont, int sfid)
495 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
496 char *derived_name = x11_guess_derived_font_name
497 (xfont->fonts[0].xfs, sfid & 1, !!(sfid & 2));
498 xfont->fonts[sfid].xfs = XLoadQueryFont(disp, derived_name);
499 xfont->fonts[sfid].allocated = TRUE;
501 /* Note that xfont->fonts[sfid].xfs may still be NULL, if XLQF failed. */
504 static int x11font_has_glyph(unifont *font, wchar_t glyph)
506 struct x11font *xfont = (struct x11font *)font;
508 if (xfont->sixteen_bit) {
510 * This X font has 16-bit character indices, which means
511 * we can directly use our Unicode input value.
513 return x11_font_has_glyph(xfont->fonts[0].xfs,
514 glyph >> 8, glyph & 0xFF);
517 * This X font has 8-bit indices, so we must convert to the
518 * appropriate character set.
521 int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
522 sbstring, 2, "", NULL, NULL);
523 if (sblen == 0 || !sbstring[0])
524 return FALSE; /* not even in the charset */
526 return x11_font_has_glyph(xfont->fonts[0].xfs, 0,
527 (unsigned char)sbstring[0]);
531 #if !GTK_CHECK_VERSION(2,0,0)
532 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
533 #elif GTK_CHECK_VERSION(3,0,0)
534 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XID(d) /* GTK3's name for this */
537 static int x11font_width_16(unifont_drawctx *ctx, x11font_individual *xfi,
538 const void *vstring, int start, int length)
540 const XChar2b *string = (const XChar2b *)vstring;
541 return XTextWidth16(xfi->xfs, string+start, length);
544 static int x11font_width_8(unifont_drawctx *ctx, x11font_individual *xfi,
545 const void *vstring, int start, int length)
547 const char *string = (const char *)vstring;
548 return XTextWidth(xfi->xfs, string+start, length);
552 static void x11font_gdk_setup(unifont_drawctx *ctx, x11font_individual *xfi)
554 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
555 XSetFont(disp, GDK_GC_XGC(ctx->u.gdk.gc), xfi->xfs->fid);
558 static void x11font_gdk_draw_16(unifont_drawctx *ctx,
559 x11font_individual *xfi, int x, int y,
560 const void *vstring, int start, int length)
562 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
563 const XChar2b *string = (const XChar2b *)vstring;
564 XDrawString16(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
565 GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
568 static void x11font_gdk_draw_8(unifont_drawctx *ctx,
569 x11font_individual *xfi, int x, int y,
570 const void *vstring, int start, int length)
572 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
573 const char *string = (const char *)vstring;
574 XDrawString(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
575 GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
579 #ifdef DRAW_TEXT_CAIRO
580 static void x11font_cairo_setup(unifont_drawctx *ctx, x11font_individual *xfi)
582 if (xfi->pixmap == None) {
583 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
585 GdkWindow *widgetwin = gtk_widget_get_window(ctx->u.cairo.widget);
586 int widgetscr = GDK_SCREEN_XNUMBER(gdk_window_get_screen(widgetwin));
589 xfi->xfs->max_bounds.rbearing - xfi->xfs->min_bounds.lbearing;
591 xfi->xfs->max_bounds.ascent + xfi->xfs->max_bounds.descent;
592 xfi->pixoriginx = -xfi->xfs->min_bounds.lbearing;
593 xfi->pixoriginy = xfi->xfs->max_bounds.ascent;
595 xfi->rowsize = cairo_format_stride_for_width(CAIRO_FORMAT_A1,
597 xfi->allsize = xfi->rowsize * xfi->pixheight;
601 * Test host endianness and use it to set xfi->indexflip,
602 * which is XORed into our left-shift counts in order to
603 * implement the CAIRO_FORMAT_A1 specification, in which
604 * each bitmap byte is oriented LSB-first on little-endian
605 * platforms and MSB-first on big-endian ones.
607 * This is the same technique Cairo itself uses to test
608 * endianness, so hopefully it'll work in any situation
609 * where Cairo is usable at all.
611 static const int endianness_test = 1;
612 xfi->indexflip = (*((char *) &endianness_test) == 1) ? 0 : 7;
615 xfi->pixmap = XCreatePixmap
617 GDK_DRAWABLE_XID(gtk_widget_get_window(ctx->u.cairo.widget)),
618 xfi->pixwidth, xfi->pixheight, 1);
619 gcvals.foreground = WhitePixel(disp, widgetscr);
620 gcvals.background = BlackPixel(disp, widgetscr);
621 gcvals.font = xfi->xfs->fid;
622 xfi->gc = XCreateGC(disp, xfi->pixmap,
623 GCForeground | GCBackground | GCFont, &gcvals);
627 static void x11font_cairo_cache_glyph(x11font_individual *xfi, int glyphindex)
631 unsigned char *bitmap;
632 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
633 const XCharStruct *xcs = x11_char_struct(xfi->xfs, glyphindex >> 8,
636 bitmap = snewn(xfi->allsize, unsigned char);
637 memset(bitmap, 0, xfi->allsize);
639 image = XGetImage(disp, xfi->pixmap, 0, 0,
640 xfi->pixwidth, xfi->pixheight, AllPlanes, XYPixmap);
641 for (y = xfi->pixoriginy - xcs->ascent;
642 y < xfi->pixoriginy + xcs->descent; y++) {
643 for (x = xfi->pixoriginx + xcs->lbearing;
644 x < xfi->pixoriginx + xcs->rbearing; x++) {
645 unsigned long pixel = XGetPixel(image, x, y);
647 int byteindex = y * xfi->rowsize + x/8;
648 int bitindex = (x & 7) ^ xfi->indexflip;
649 bitmap[byteindex] |= 1U << bitindex;
653 XDestroyImage(image);
655 if (xfi->nglyphs <= glyphindex) {
656 /* Round up to the next multiple of 256 on the general
657 * principle that Unicode characters come in contiguous blocks
658 * often used together */
659 int old_nglyphs = xfi->nglyphs;
660 xfi->nglyphs = (glyphindex + 0x100) & ~0xFF;
661 xfi->glyphcache = sresize(xfi->glyphcache, xfi->nglyphs,
662 struct cairo_cached_glyph);
664 while (old_nglyphs < xfi->nglyphs) {
665 xfi->glyphcache[old_nglyphs].surface = NULL;
666 xfi->glyphcache[old_nglyphs].bitmap = NULL;
670 xfi->glyphcache[glyphindex].bitmap = bitmap;
671 xfi->glyphcache[glyphindex].surface = cairo_image_surface_create_for_data
672 (bitmap, CAIRO_FORMAT_A1, xfi->pixwidth, xfi->pixheight, xfi->rowsize);
675 static void x11font_cairo_draw_glyph(unifont_drawctx *ctx,
676 x11font_individual *xfi, int x, int y,
679 if (xfi->glyphcache[glyphindex].surface) {
680 cairo_mask_surface(ctx->u.cairo.cr,
681 xfi->glyphcache[glyphindex].surface,
682 x - xfi->pixoriginx, y - xfi->pixoriginy);
686 static void x11font_cairo_draw_16(unifont_drawctx *ctx,
687 x11font_individual *xfi, int x, int y,
688 const void *vstring, int start, int length)
690 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
691 const XChar2b *string = (const XChar2b *)vstring + start;
693 for (i = 0; i < length; i++) {
694 if (x11_font_has_glyph(xfi->xfs, string[i].byte1, string[i].byte2)) {
695 int glyphindex = (256 * (unsigned char)string[i].byte1 +
696 (unsigned char)string[i].byte2);
697 if (glyphindex >= xfi->nglyphs ||
698 !xfi->glyphcache[glyphindex].surface) {
699 XDrawImageString16(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 += XTextWidth16(xfi->xfs, string+i, 1);
710 static void x11font_cairo_draw_8(unifont_drawctx *ctx,
711 x11font_individual *xfi, int x, int y,
712 const void *vstring, int start, int length)
714 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
715 const char *string = (const char *)vstring + start;
717 for (i = 0; i < length; i++) {
718 if (x11_font_has_glyph(xfi->xfs, 0, string[i])) {
719 int glyphindex = (unsigned char)string[i];
720 if (glyphindex >= xfi->nglyphs ||
721 !xfi->glyphcache[glyphindex].surface) {
722 XDrawImageString(disp, xfi->pixmap, xfi->gc,
723 xfi->pixoriginx, xfi->pixoriginy,
725 x11font_cairo_cache_glyph(xfi, glyphindex);
727 x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
728 x += XTextWidth(xfi->xfs, string+i, 1);
732 #endif /* DRAW_TEXT_CAIRO */
734 struct x11font_drawfuncs {
735 int (*width)(unifont_drawctx *ctx, x11font_individual *xfi,
736 const void *vstring, int start, int length);
737 void (*setup)(unifont_drawctx *ctx, x11font_individual *xfi);
738 void (*draw)(unifont_drawctx *ctx, x11font_individual *xfi, int x, int y,
739 const void *vstring, int start, int length);
743 * This array has two entries per compiled-in drawtype; of each pair,
744 * the first is for an 8-bit font and the second for 16-bit.
746 static const struct x11font_drawfuncs x11font_drawfuncs[2*DRAWTYPE_NTYPES] = {
761 #ifdef DRAW_TEXT_CAIRO
766 x11font_cairo_draw_8,
768 /* [3] cairo, 16-bit */
772 x11font_cairo_draw_16,
777 static void x11font_really_draw_text(const struct x11font_drawfuncs *dfns,
778 unifont_drawctx *ctx,
779 x11font_individual *xfi, int x, int y,
780 const void *string, int nchars,
782 int fontvariable, int cellwidth)
784 int start = 0, step, nsteps, centre;
788 * In a variable-pitch font, we draw one character at a
789 * time, and centre it in the character cell.
796 * In a fixed-pitch font, we can draw the whole lot in one go.
803 dfns->setup(ctx, xfi);
805 while (nsteps-- > 0) {
808 X += (cellwidth - dfns->width(ctx, xfi, string, start, step)) / 2;
810 dfns->draw(ctx, xfi, X, y, string, start, step);
812 dfns->draw(ctx, xfi, X + shadowoffset, y, string, start, step);
819 static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
820 int x, int y, const wchar_t *string, int len,
821 int wide, int bold, int cellwidth)
823 struct x11font *xfont = (struct x11font *)font;
825 int shadowoffset = 0;
826 int mult = (wide ? 2 : 1);
827 int index = 2 * (int)ctx->type;
833 * Decide which subfont we're using, and whether we have to
836 if (xfont->shadowalways && bold) {
837 shadowoffset = xfont->shadowoffset;
840 sfid = 2 * wide + bold;
841 if (!xfont->fonts[sfid].allocated)
842 x11_alloc_subfont(xfont, sfid);
843 if (bold && !xfont->fonts[sfid].xfs) {
845 shadowoffset = xfont->shadowoffset;
846 sfid = 2 * wide + bold;
847 if (!xfont->fonts[sfid].allocated)
848 x11_alloc_subfont(xfont, sfid);
851 if (!xfont->fonts[sfid].xfs)
852 return; /* we've tried our best, but no luck */
854 if (xfont->sixteen_bit) {
856 * This X font has 16-bit character indices, which means
857 * we can directly use our Unicode input string.
862 xcs = snewn(len, XChar2b);
863 for (i = 0; i < len; i++) {
864 xcs[i].byte1 = string[i] >> 8;
865 xcs[i].byte2 = string[i];
868 x11font_really_draw_text(x11font_drawfuncs + index + 1, ctx,
869 &xfont->fonts[sfid], x, y,
870 xcs, len, shadowoffset,
871 xfont->variable, cellwidth * mult);
875 * This X font has 8-bit indices, so we must convert to the
876 * appropriate character set.
878 char *sbstring = snewn(len+1, char);
879 int sblen = wc_to_mb(xfont->real_charset, 0, string, len,
880 sbstring, len+1, ".", NULL, NULL);
881 x11font_really_draw_text(x11font_drawfuncs + index + 0, ctx,
882 &xfont->fonts[sfid], x, y,
883 sbstring, sblen, shadowoffset,
884 xfont->variable, cellwidth * mult);
889 static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
890 int x, int y, const wchar_t *string,
891 int len, int wide, int bold, int cellwidth)
894 * For server-side fonts, there's no sophisticated system for
895 * combining characters intelligently, so the best we can do is to
896 * overprint them on each other in the obvious way.
899 for (i = 0; i < len; i++)
900 x11font_draw_text(ctx, font, x, y, string+i, 1, wide, bold, cellwidth);
903 static void x11font_enum_fonts(GtkWidget *widget,
904 fontsel_add_entry callback, void *callback_ctx)
906 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
909 int nnames, i, max, tmpsize;
913 fontnames = XListFonts(disp, "*", max, &nnames);
915 XFreeFontNames(fontnames);
923 for (i = 0; i < nnames; i++) {
924 if (fontnames[i][0] == '-') {
926 * Dismember an XLFD and convert it into the format
927 * we'll be using in the font selector.
929 char *components[14];
930 char *p, *font, *style, *stylekey, *charset;
931 int j, weightkey, slantkey, setwidthkey;
932 int thistmpsize, fontsize, flags;
934 thistmpsize = 4 * strlen(fontnames[i]) + 256;
935 if (tmpsize < thistmpsize) {
936 tmpsize = thistmpsize;
937 tmp = sresize(tmp, tmpsize, char);
939 strcpy(tmp, fontnames[i]);
942 for (j = 0; j < 14; j++) {
946 while (*p && *p != '-')
952 * Font name is made up of fields 0 and 1, in reverse
953 * order with parentheses. (This is what the GTK 1.2 X
954 * font selector does, and it seems to come out
955 * looking reasonably sensible.)
958 p += 1 + sprintf(p, "%s (%s)", components[1], components[0]);
961 * Charset is made up of fields 12 and 13.
964 p += 1 + sprintf(p, "%s-%s", components[12], components[13]);
967 * Style is a mixture of quite a lot of the fields,
968 * with some strange formatting.
971 p += sprintf(p, "%s", components[2][0] ? components[2] :
973 if (!g_ascii_strcasecmp(components[3], "i"))
974 p += sprintf(p, " italic");
975 else if (!g_ascii_strcasecmp(components[3], "o"))
976 p += sprintf(p, " oblique");
977 else if (!g_ascii_strcasecmp(components[3], "ri"))
978 p += sprintf(p, " reverse italic");
979 else if (!g_ascii_strcasecmp(components[3], "ro"))
980 p += sprintf(p, " reverse oblique");
981 else if (!g_ascii_strcasecmp(components[3], "ot"))
982 p += sprintf(p, " other-slant");
983 if (components[4][0] && g_ascii_strcasecmp(components[4], "normal"))
984 p += sprintf(p, " %s", components[4]);
985 if (!g_ascii_strcasecmp(components[10], "m"))
986 p += sprintf(p, " [M]");
987 if (!g_ascii_strcasecmp(components[10], "c"))
988 p += sprintf(p, " [C]");
989 if (components[5][0])
990 p += sprintf(p, " %s", components[5]);
993 * Style key is the same stuff as above, but with a
994 * couple of transformations done on it to make it
995 * sort more sensibly.
999 if (!g_ascii_strcasecmp(components[2], "medium") ||
1000 !g_ascii_strcasecmp(components[2], "regular") ||
1001 !g_ascii_strcasecmp(components[2], "normal") ||
1002 !g_ascii_strcasecmp(components[2], "book"))
1004 else if (!g_ascii_strncasecmp(components[2], "demi", 4) ||
1005 !g_ascii_strncasecmp(components[2], "semi", 4))
1009 if (!g_ascii_strcasecmp(components[3], "r"))
1011 else if (!g_ascii_strncasecmp(components[3], "r", 1))
1015 if (!g_ascii_strcasecmp(components[4], "normal"))
1020 p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
1022 (int)strlen(components[2]), components[2],
1024 (int)strlen(components[3]), components[3],
1026 (int)strlen(components[4]), components[4],
1027 (int)strlen(components[10]), components[10],
1028 (int)strlen(components[5]), components[5]);
1030 assert(p - tmp < thistmpsize);
1033 * Size is in pixels, for our application, so we
1034 * derive it directly from the pixel size field,
1037 fontsize = atoi(components[6]);
1040 * Flags: we need to know whether this is a monospaced
1041 * font, which we do by examining the spacing field
1044 flags = FONTFLAG_SERVERSIDE;
1045 if (!strchr("CcMm", components[10][0]))
1046 flags |= FONTFLAG_NONMONOSPACED;
1049 * Not sure why, but sometimes the X server will
1050 * deliver dummy font types in which fontsize comes
1051 * out as zero. Filter those out.
1054 callback(callback_ctx, fontnames[i], font, charset,
1055 style, stylekey, fontsize, flags, &x11font_vtable);
1058 * This isn't an XLFD, so it must be an alias.
1059 * Transmit it with mostly null data.
1061 * It would be nice to work out if it's monospaced
1062 * here, but at the moment I can't see that being
1063 * anything but computationally hideous. Ah well.
1065 callback(callback_ctx, fontnames[i], fontnames[i], NULL,
1066 NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);
1069 XFreeFontNames(fontnames);
1072 static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
1073 int *size, int *flags,
1074 int resolve_aliases)
1077 * When given an X11 font name to try to make sense of for a
1078 * font selector, we must attempt to load it (to see if it
1079 * exists), and then canonify it by extracting its FONT
1080 * property, which should give its full XLFD even if what we
1081 * originally had was a wildcard.
1083 * However, we must carefully avoid canonifying font
1084 * _aliases_, unless specifically asked to, because the font
1085 * selector treats them as worthwhile in their own right.
1088 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1089 Atom fontprop, fontprop2;
1092 xfs = XLoadQueryFont(disp, name);
1095 return NULL; /* didn't make sense to us, sorry */
1097 fontprop = XInternAtom(disp, "FONT", False);
1099 if (XGetFontProperty(xfs, fontprop, &ret)) {
1100 char *newname = XGetAtomName(disp, (Atom)ret);
1102 unsigned long fsize = 12;
1104 fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);
1105 if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {
1107 XFreeFont(disp, xfs);
1109 if (name[0] == '-' || resolve_aliases)
1110 *flags = FONTFLAG_SERVERSIDE;
1112 *flags = FONTFLAG_SERVERALIAS;
1114 return dupstr(name[0] == '-' || resolve_aliases ?
1120 XFreeFont(disp, xfs);
1122 return NULL; /* something went wrong */
1125 static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
1128 return NULL; /* shan't */
1131 #endif /* NOT_X_WINDOWS */
1133 #if GTK_CHECK_VERSION(2,0,0)
1135 /* ----------------------------------------------------------------------
1136 * Pango font implementation (for GTK 2 only).
1139 #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
1140 #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
1143 static int pangofont_has_glyph(unifont *font, wchar_t glyph);
1144 static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
1145 int x, int y, const wchar_t *string, int len,
1146 int wide, int bold, int cellwidth);
1147 static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
1148 int x, int y, const wchar_t *string,
1149 int len, int wide, int bold,
1151 static unifont *pangofont_create(GtkWidget *widget, const char *name,
1153 int shadowoffset, int shadowalways);
1154 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
1156 int shadowoffset, int shadowalways);
1157 static void pangofont_destroy(unifont *font);
1158 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
1159 void *callback_ctx);
1160 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
1161 int *size, int *flags,
1162 int resolve_aliases);
1163 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
1171 PangoFontDescription *desc;
1174 * The containing widget.
1178 * Data passed in to unifont_create().
1180 int bold, shadowoffset, shadowalways;
1182 * Cache of character widths, indexed by Unicode code point. In
1183 * pixels; -1 means we haven't asked Pango about this character
1187 unsigned nwidthcache;
1190 static const struct unifont_vtable pangofont_vtable = {
1192 pangofont_create_fallback,
1194 pangofont_has_glyph,
1195 pangofont_draw_text,
1196 pangofont_draw_combining,
1197 pangofont_enum_fonts,
1198 pangofont_canonify_fontname,
1199 pangofont_scale_fontname,
1204 * This function is used to rigorously validate a
1205 * PangoFontDescription. Later versions of Pango have a nasty
1206 * habit of accepting _any_ old string as input to
1207 * pango_font_description_from_string and returning a font
1208 * description which can actually be used to display text, even if
1209 * they have to do it by falling back to their most default font.
1210 * This is doubtless helpful in some situations, but not here,
1211 * because we need to know if a Pango font string actually _makes
1212 * sense_ in order to fall back to treating it as an X font name
1213 * if it doesn't. So we check that the font family is actually one
1214 * supported by Pango.
1216 static int pangofont_check_desc_makes_sense(PangoContext *ctx,
1217 PangoFontDescription *desc)
1219 #ifndef PANGO_PRE_1POINT6
1222 PangoFontFamily **families;
1223 int i, nfamilies, matched;
1226 * Ask Pango for a list of font families, and iterate through
1227 * them to see if one of them matches the family in the
1228 * PangoFontDescription.
1230 #ifndef PANGO_PRE_1POINT6
1231 map = pango_context_get_font_map(ctx);
1234 pango_font_map_list_families(map, &families, &nfamilies);
1236 pango_context_list_families(ctx, &families, &nfamilies);
1240 for (i = 0; i < nfamilies; i++) {
1241 if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]),
1242 pango_font_description_get_family(desc))) {
1252 static unifont *pangofont_create_internal(GtkWidget *widget,
1254 PangoFontDescription *desc,
1256 int shadowoffset, int shadowalways)
1258 struct pangofont *pfont;
1259 #ifndef PANGO_PRE_1POINT6
1263 PangoFontMetrics *metrics;
1265 #ifndef PANGO_PRE_1POINT6
1266 map = pango_context_get_font_map(ctx);
1268 pango_font_description_free(desc);
1271 fset = pango_font_map_load_fontset(map, ctx, desc,
1272 pango_context_get_language(ctx));
1274 fset = pango_context_load_fontset(ctx, desc,
1275 pango_context_get_language(ctx));
1278 pango_font_description_free(desc);
1281 metrics = pango_fontset_get_metrics(fset);
1283 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
1284 pango_font_description_free(desc);
1285 g_object_unref(fset);
1289 pfont = snew(struct pangofont);
1290 pfont->u.vt = &pangofont_vtable;
1292 PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));
1293 pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));
1294 pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics));
1295 pfont->u.height = pfont->u.ascent + pfont->u.descent;
1296 pfont->u.want_fallback = FALSE;
1297 #ifdef DRAW_TEXT_CAIRO
1298 pfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
1299 #elif defined DRAW_TEXT_GDK
1300 pfont->u.preferred_drawtype = DRAWTYPE_GDK;
1302 #error No drawtype available at all
1304 /* The Pango API is hardwired to UTF-8 */
1305 pfont->u.public_charset = CS_UTF8;
1308 pfont->widget = widget;
1310 pfont->shadowoffset = shadowoffset;
1311 pfont->shadowalways = shadowalways;
1312 pfont->widthcache = NULL;
1313 pfont->nwidthcache = 0;
1315 pango_font_metrics_unref(metrics);
1317 return (unifont *)pfont;
1320 static unifont *pangofont_create(GtkWidget *widget, const char *name,
1322 int shadowoffset, int shadowalways)
1325 PangoFontDescription *desc;
1327 desc = pango_font_description_from_string(name);
1330 ctx = gtk_widget_get_pango_context(widget);
1332 pango_font_description_free(desc);
1335 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1336 pango_font_description_free(desc);
1339 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1340 shadowoffset, shadowalways);
1343 static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
1345 int shadowoffset, int shadowalways)
1348 PangoFontDescription *desc;
1350 desc = pango_font_description_from_string("Monospace");
1353 ctx = gtk_widget_get_pango_context(widget);
1355 pango_font_description_free(desc);
1358 pango_font_description_set_absolute_size(desc, height * PANGO_SCALE);
1359 return pangofont_create_internal(widget, ctx, desc, wide, bold,
1360 shadowoffset, shadowalways);
1363 static void pangofont_destroy(unifont *font)
1365 struct pangofont *pfont = (struct pangofont *)font;
1366 pango_font_description_free(pfont->desc);
1367 sfree(pfont->widthcache);
1368 g_object_unref(pfont->fset);
1372 static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont,
1373 wchar_t uchr, const char *utfchr, int utflen)
1376 * Here we check whether a character has the same width as the
1377 * character cell it'll be drawn in. Because profiling showed that
1378 * asking Pango for text sizes was a huge bottleneck when we were
1379 * calling it every time we needed to know this, we instead call
1380 * it only on characters we don't already know about, and cache
1384 if ((unsigned)uchr >= pfont->nwidthcache) {
1385 unsigned newsize = ((int)uchr + 0x100) & ~0xFF;
1386 pfont->widthcache = sresize(pfont->widthcache, newsize, int);
1387 while (pfont->nwidthcache < newsize)
1388 pfont->widthcache[pfont->nwidthcache++] = -1;
1391 if (pfont->widthcache[uchr] < 0) {
1392 PangoRectangle rect;
1393 pango_layout_set_text(layout, utfchr, utflen);
1394 pango_layout_get_extents(layout, NULL, &rect);
1395 pfont->widthcache[uchr] = rect.width;
1398 return pfont->widthcache[uchr];
1401 static int pangofont_has_glyph(unifont *font, wchar_t glyph)
1403 /* Pango implements font fallback, so assume it has everything */
1407 #ifdef DRAW_TEXT_GDK
1408 static void pango_gdk_draw_layout(unifont_drawctx *ctx,
1409 gint x, gint y, PangoLayout *layout)
1411 gdk_draw_layout(ctx->u.gdk.target, ctx->u.gdk.gc, x, y, layout);
1415 #ifdef DRAW_TEXT_CAIRO
1416 static void pango_cairo_draw_layout(unifont_drawctx *ctx,
1417 gint x, gint y, PangoLayout *layout)
1419 cairo_move_to(ctx->u.cairo.cr, x, y);
1420 pango_cairo_show_layout(ctx->u.cairo.cr, layout);
1424 static void pangofont_draw_internal(unifont_drawctx *ctx, unifont *font,
1425 int x, int y, const wchar_t *string,
1426 int len, int wide, int bold, int cellwidth,
1429 struct pangofont *pfont = (struct pangofont *)font;
1430 PangoLayout *layout;
1431 PangoRectangle rect;
1432 char *utfstring, *utfptr;
1434 int shadowbold = FALSE;
1435 void (*draw_layout)(unifont_drawctx *ctx,
1436 gint x, gint y, PangoLayout *layout) = NULL;
1438 #ifdef DRAW_TEXT_GDK
1439 if (ctx->type == DRAWTYPE_GDK) {
1440 draw_layout = pango_gdk_draw_layout;
1443 #ifdef DRAW_TEXT_CAIRO
1444 if (ctx->type == DRAWTYPE_CAIRO) {
1445 draw_layout = pango_cairo_draw_layout;
1452 y -= pfont->u.ascent;
1454 layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));
1455 pango_layout_set_font_description(layout, pfont->desc);
1456 if (bold > pfont->bold) {
1457 if (pfont->shadowalways)
1460 PangoFontDescription *desc2 =
1461 pango_font_description_copy_static(pfont->desc);
1462 pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);
1463 pango_layout_set_font_description(layout, desc2);
1468 * Pango always expects UTF-8, so convert the input wide character
1471 utfstring = snewn(len*6+1, char); /* UTF-8 has max 6 bytes/char */
1472 utflen = wc_to_mb(CS_UTF8, 0, string, len,
1473 utfstring, len*6+1, ".", NULL, NULL);
1476 while (utflen > 0) {
1478 int desired = cellwidth * PANGO_SCALE;
1481 * We want to display every character from this string in
1482 * the centre of its own character cell. In the worst case,
1483 * this requires a separate text-drawing call for each
1484 * character; but in the common case where the font is
1485 * properly fixed-width, we can draw many characters in one
1486 * go which is much faster.
1488 * This still isn't really ideal. If you look at what
1489 * happens in the X protocol as a result of all of this, you
1490 * find - naturally enough - that each call to
1491 * gdk_draw_layout() generates a separate set of X RENDER
1492 * operations involving creating a picture, setting a clip
1493 * rectangle, doing some drawing and undoing the whole lot.
1494 * In an ideal world, we should _always_ be able to turn the
1495 * contents of this loop into a single RenderCompositeGlyphs
1496 * operation which internally specifies inter-character
1497 * deltas to get the spacing right, which would give us full
1498 * speed _even_ in the worst case of a non-fixed-width font.
1499 * However, Pango's architecture and documentation are so
1500 * unhelpful that I have no idea how if at all to persuade
1506 * For a character with combining stuff, we just dump the
1507 * whole lot in one go, and expect it to take up just one
1514 * Start by extracting a single UTF-8 character from the
1518 while (clen < utflen &&
1519 (unsigned char)utfptr[clen] >= 0x80 &&
1520 (unsigned char)utfptr[clen] < 0xC0)
1524 if (is_rtl(string[0]) ||
1525 pangofont_char_width(layout, pfont, string[n-1],
1526 utfptr, clen) != desired) {
1528 * If this character is a right-to-left one, or has an
1529 * unusual width, then we must display it on its own.
1533 * Try to amalgamate a contiguous string of characters
1534 * with the expected sensible width, for the common case
1535 * in which we're using a monospaced font and everything
1536 * works as expected.
1538 while (clen < utflen) {
1540 clen++; /* skip UTF-8 introducer byte */
1541 while (clen < utflen &&
1542 (unsigned char)utfptr[clen] >= 0x80 &&
1543 (unsigned char)utfptr[clen] < 0xC0)
1546 if (is_rtl(string[n-1]) ||
1547 pangofont_char_width(layout, pfont,
1548 string[n-1], utfptr + oldclen,
1549 clen - oldclen) != desired) {
1558 pango_layout_set_text(layout, utfptr, clen);
1559 pango_layout_get_pixel_extents(layout, NULL, &rect);
1562 x + (n*cellwidth - rect.width)/2,
1563 y + (pfont->u.height - rect.height)/2, layout);
1566 x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,
1567 y + (pfont->u.height - rect.height)/2, layout);
1577 g_object_unref(layout);
1580 static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
1581 int x, int y, const wchar_t *string, int len,
1582 int wide, int bold, int cellwidth)
1584 pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
1588 static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
1589 int x, int y, const wchar_t *string,
1590 int len, int wide, int bold,
1593 wchar_t *tmpstring = NULL;
1594 if (mk_wcwidth(string[0]) == 0) {
1596 * If we've been told to draw a sequence of _only_ combining
1597 * characters, prefix a space so that they have something to
1600 tmpstring = snewn(len+1, wchar_t);
1601 memcpy(tmpstring+1, string, len * sizeof(wchar_t));
1602 tmpstring[0] = L' ';
1606 pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
1612 * Dummy size value to be used when converting a
1613 * PangoFontDescription of a scalable font to a string for
1616 #define PANGO_DUMMY_SIZE 12
1618 static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
1622 #ifndef PANGO_PRE_1POINT6
1625 PangoFontFamily **families;
1628 ctx = gtk_widget_get_pango_context(widget);
1633 * Ask Pango for a list of font families, and iterate through
1636 #ifndef PANGO_PRE_1POINT6
1637 map = pango_context_get_font_map(ctx);
1640 pango_font_map_list_families(map, &families, &nfamilies);
1642 pango_context_list_families(ctx, &families, &nfamilies);
1644 for (i = 0; i < nfamilies; i++) {
1645 PangoFontFamily *family = families[i];
1646 const char *familyname;
1648 PangoFontFace **faces;
1652 * Set up our flags for this font family, and get the name
1655 flags = FONTFLAG_CLIENTSIDE;
1656 #ifndef PANGO_PRE_1POINT4
1658 * In very early versions of Pango, we can't tell
1659 * monospaced fonts from non-monospaced.
1661 if (!pango_font_family_is_monospace(family))
1662 flags |= FONTFLAG_NONMONOSPACED;
1664 familyname = pango_font_family_get_name(family);
1667 * Go through the available font faces in this family.
1669 pango_font_family_list_faces(family, &faces, &nfaces);
1670 for (j = 0; j < nfaces; j++) {
1671 PangoFontFace *face = faces[j];
1672 PangoFontDescription *desc;
1673 const char *facename;
1675 int k, nsizes, dummysize;
1678 * Get the face name string.
1680 facename = pango_font_face_get_face_name(face);
1683 * Set up a font description with what we've got so
1684 * far. We'll fill in the size field manually and then
1685 * call pango_font_description_to_string() to give the
1686 * full real name of the specific font.
1688 desc = pango_font_face_describe(face);
1691 * See if this font has a list of specific sizes.
1693 #ifndef PANGO_PRE_1POINT4
1694 pango_font_face_list_sizes(face, &sizes, &nsizes);
1697 * In early versions of Pango, that call wasn't
1698 * supported; we just have to assume everything is
1705 * Write a single entry with a dummy size.
1707 dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;
1713 * If so, go through them one by one.
1715 for (k = 0; k < nsizes; k++) {
1719 pango_font_description_set_size(desc, sizes[k]);
1721 fullname = pango_font_description_to_string(desc);
1724 * Construct the sorting key for font styles.
1730 n = pango_font_description_get_weight(desc);
1731 /* Weight: normal, then lighter, then bolder */
1732 if (n <= PANGO_WEIGHT_NORMAL)
1733 n = PANGO_WEIGHT_NORMAL - n;
1734 p += sprintf(p, "%4d", n);
1736 n = pango_font_description_get_style(desc);
1737 p += sprintf(p, " %2d", n);
1739 n = pango_font_description_get_stretch(desc);
1740 /* Stretch: closer to normal sorts earlier */
1741 n = 2 * abs(PANGO_STRETCH_NORMAL - n) +
1742 (n < PANGO_STRETCH_NORMAL);
1743 p += sprintf(p, " %2d", n);
1745 n = pango_font_description_get_variant(desc);
1746 p += sprintf(p, " %2d", n);
1751 * Got everything. Hand off to the callback.
1752 * (The charset string is NULL, because only
1753 * server-side X fonts use it.)
1755 callback(callback_ctx, fullname, familyname, NULL, facename,
1757 (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
1758 flags, &pangofont_vtable);
1762 if (sizes != &dummysize)
1765 pango_font_description_free(desc);
1772 static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
1773 int *size, int *flags,
1774 int resolve_aliases)
1777 * When given a Pango font name to try to make sense of for a
1778 * font selector, we must normalise it to PANGO_DUMMY_SIZE and
1779 * extract its original size (in pixels) into the `size' field.
1782 #ifndef PANGO_PRE_1POINT6
1785 PangoFontDescription *desc;
1787 PangoFontMetrics *metrics;
1788 char *newname, *retname;
1790 desc = pango_font_description_from_string(name);
1793 ctx = gtk_widget_get_pango_context(widget);
1795 pango_font_description_free(desc);
1798 if (!pangofont_check_desc_makes_sense(ctx, desc)) {
1799 pango_font_description_free(desc);
1802 #ifndef PANGO_PRE_1POINT6
1803 map = pango_context_get_font_map(ctx);
1805 pango_font_description_free(desc);
1808 fset = pango_font_map_load_fontset(map, ctx, desc,
1809 pango_context_get_language(ctx));
1811 fset = pango_context_load_fontset(ctx, desc,
1812 pango_context_get_language(ctx));
1815 pango_font_description_free(desc);
1818 metrics = pango_fontset_get_metrics(fset);
1820 pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
1821 pango_font_description_free(desc);
1822 g_object_unref(fset);
1826 *size = PANGO_PIXELS(pango_font_description_get_size(desc));
1827 *flags = FONTFLAG_CLIENTSIDE;
1828 pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);
1829 newname = pango_font_description_to_string(desc);
1830 retname = dupstr(newname);
1833 pango_font_metrics_unref(metrics);
1834 pango_font_description_free(desc);
1835 g_object_unref(fset);
1840 static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
1843 PangoFontDescription *desc;
1844 char *newname, *retname;
1846 desc = pango_font_description_from_string(name);
1849 pango_font_description_set_size(desc, size * PANGO_SCALE);
1850 newname = pango_font_description_to_string(desc);
1851 retname = dupstr(newname);
1853 pango_font_description_free(desc);
1858 #endif /* GTK_CHECK_VERSION(2,0,0) */
1860 /* ----------------------------------------------------------------------
1861 * Outermost functions which do the vtable dispatch.
1865 * Complete list of font-type subclasses. Listed in preference
1866 * order for unifont_create(). (That is, in the extremely unlikely
1867 * event that the same font name is valid as both a Pango and an
1868 * X11 font, it will be interpreted as the former in the absence
1869 * of an explicit type-disambiguating prefix.)
1871 * The 'multifont' subclass is omitted here, as discussed above.
1873 static const struct unifont_vtable *unifont_types[] = {
1874 #if GTK_CHECK_VERSION(2,0,0)
1877 #ifndef NOT_X_WINDOWS
1883 * Function which takes a font name and processes the optional
1884 * scheme prefix. Returns the tail of the font name suitable for
1885 * passing to individual font scheme functions, and also provides
1886 * a subrange of the unifont_types[] array above.
1888 * The return values `start' and `end' denote a half-open interval
1889 * in unifont_types[]; that is, the correct way to iterate over
1892 * for (i = start; i < end; i++) {...}
1894 static const char *unifont_do_prefix(const char *name, int *start, int *end)
1896 int colonpos = strcspn(name, ":");
1899 if (name[colonpos]) {
1901 * There's a colon prefix on the font name. Use it to work
1902 * out which subclass to use.
1904 for (i = 0; i < lenof(unifont_types); i++) {
1905 if (strlen(unifont_types[i]->prefix) == colonpos &&
1906 !strncmp(unifont_types[i]->prefix, name, colonpos)) {
1909 return name + colonpos + 1;
1913 * None matched, so return an empty scheme list to prevent
1914 * any scheme from being called at all.
1917 return name + colonpos + 1;
1920 * No colon prefix, so just use all the subclasses.
1923 *end = lenof(unifont_types);
1928 unifont *unifont_create(GtkWidget *widget, const char *name, int wide,
1929 int bold, int shadowoffset, int shadowalways)
1933 name = unifont_do_prefix(name, &start, &end);
1935 for (i = start; i < end; i++) {
1936 unifont *ret = unifont_types[i]->create(widget, name, wide, bold,
1937 shadowoffset, shadowalways);
1941 return NULL; /* font not found in any scheme */
1944 void unifont_destroy(unifont *font)
1946 font->vt->destroy(font);
1949 void unifont_draw_text(unifont_drawctx *ctx, unifont *font,
1950 int x, int y, const wchar_t *string, int len,
1951 int wide, int bold, int cellwidth)
1953 font->vt->draw_text(ctx, font, x, y, string, len, wide, bold, cellwidth);
1956 void unifont_draw_combining(unifont_drawctx *ctx, unifont *font,
1957 int x, int y, const wchar_t *string, int len,
1958 int wide, int bold, int cellwidth)
1960 font->vt->draw_combining(ctx, font, x, y, string, len, wide, bold,
1964 /* ----------------------------------------------------------------------
1965 * Multiple-font wrapper. This is a type of unifont which encapsulates
1966 * up to two other unifonts, permitting missing glyphs in the main
1967 * font to be filled in by a fallback font.
1969 * This is a type of unifont just like the previous two, but it has a
1970 * separate constructor which is manually called by the client, so it
1971 * doesn't appear in the list of available font types enumerated by
1972 * unifont_create. This means it's not used by unifontsel either, so
1973 * it doesn't need to support any methods except draw_text and
1977 static void multifont_draw_text(unifont_drawctx *ctx, unifont *font,
1978 int x, int y, const wchar_t *string, int len,
1979 int wide, int bold, int cellwidth);
1980 static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
1981 int x, int y, const wchar_t *string,
1982 int len, int wide, int bold,
1984 static void multifont_destroy(unifont *font);
1992 static const struct unifont_vtable multifont_vtable = {
1993 NULL, /* creation is done specially */
1997 multifont_draw_text,
1998 multifont_draw_combining,
2005 unifont *multifont_create(GtkWidget *widget, const char *name,
2007 int shadowoffset, int shadowalways)
2010 unifont *font, *fallback;
2011 struct multifont *mfont;
2013 font = unifont_create(widget, name, wide, bold,
2014 shadowoffset, shadowalways);
2019 if (font->want_fallback) {
2020 for (i = 0; i < lenof(unifont_types); i++) {
2021 if (unifont_types[i]->create_fallback) {
2022 fallback = unifont_types[i]->create_fallback
2023 (widget, font->height, wide, bold,
2024 shadowoffset, shadowalways);
2032 * Construct our multifont. Public members are all copied from the
2033 * primary font we're wrapping.
2035 mfont = snew(struct multifont);
2036 mfont->u.vt = &multifont_vtable;
2037 mfont->u.width = font->width;
2038 mfont->u.ascent = font->ascent;
2039 mfont->u.descent = font->descent;
2040 mfont->u.height = font->height;
2041 mfont->u.public_charset = font->public_charset;
2042 mfont->u.want_fallback = FALSE; /* shouldn't be needed, but just in case */
2043 mfont->u.preferred_drawtype = font->preferred_drawtype;
2045 mfont->fallback = fallback;
2047 return (unifont *)mfont;
2050 static void multifont_destroy(unifont *font)
2052 struct multifont *mfont = (struct multifont *)font;
2053 unifont_destroy(mfont->main);
2054 if (mfont->fallback)
2055 unifont_destroy(mfont->fallback);
2059 typedef void (*unifont_draw_func_t)(unifont_drawctx *ctx, unifont *font,
2060 int x, int y, const wchar_t *string,
2061 int len, int wide, int bold,
2064 static void multifont_draw_main(unifont_drawctx *ctx, unifont *font, int x,
2065 int y, const wchar_t *string, int len,
2066 int wide, int bold, int cellwidth,
2067 int cellinc, unifont_draw_func_t draw)
2069 struct multifont *mfont = (struct multifont *)font;
2075 * Find a maximal sequence of characters which are, or are
2076 * not, supported by our main font.
2078 ok = mfont->main->vt->has_glyph(mfont->main, string[0]);
2081 !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok;
2087 f = ok ? mfont->main : mfont->fallback;
2089 draw(ctx, f, x, y, string, i, wide, bold, cellwidth);
2096 static void multifont_draw_text(unifont_drawctx *ctx, unifont *font, int x,
2097 int y, const wchar_t *string, int len,
2098 int wide, int bold, int cellwidth)
2100 multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
2101 cellwidth, cellwidth, unifont_draw_text);
2104 static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
2105 int x, int y, const wchar_t *string,
2106 int len, int wide, int bold,
2109 multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
2110 cellwidth, 0, unifont_draw_combining);
2113 #if GTK_CHECK_VERSION(2,0,0)
2115 /* ----------------------------------------------------------------------
2116 * Implementation of a unified font selector. Used on GTK 2 only;
2117 * for GTK 1 we still use the standard font selector.
2120 typedef struct fontinfo fontinfo;
2122 typedef struct unifontsel_internal {
2123 /* This must be the structure's first element, for cross-casting */
2125 GtkListStore *family_model, *style_model, *size_model;
2126 GtkWidget *family_list, *style_list, *size_entry, *size_list;
2127 GtkWidget *filter_buttons[4];
2128 int n_filter_buttons;
2129 GtkWidget *preview_area;
2130 #ifndef NO_BACKING_PIXMAPS
2131 GdkPixmap *preview_pixmap;
2133 int preview_width, preview_height;
2134 GdkColor preview_fg, preview_bg;
2136 tree234 *fonts_by_realname, *fonts_by_selorder;
2138 int selsize, intendedsize;
2139 int inhibit_response; /* inhibit callbacks when we change GUI controls */
2140 } unifontsel_internal;
2143 * The structure held in the tree234s. All the string members are
2144 * part of the same allocated area, so don't need freeing
2149 char *family, *charset, *style, *stylekey;
2152 * Fallback sorting key, to permit multiple identical entries
2153 * to exist in the selorder tree.
2157 * Indices mapping fontinfo structures to indices in the list
2158 * boxes. sizeindex is irrelevant if the font is scalable
2161 int familyindex, styleindex, sizeindex;
2163 * The class of font.
2165 const struct unifont_vtable *fontclass;
2168 struct fontinfo_realname_find {
2169 const char *realname;
2173 static int strnullcasecmp(const char *a, const char *b)
2178 * If exactly one of the inputs is NULL, it compares before
2181 if ((i = (!b) - (!a)) != 0)
2185 * NULL compares equal.
2191 * Otherwise, ordinary strcasecmp.
2193 return g_ascii_strcasecmp(a, b);
2196 static int fontinfo_realname_compare(void *av, void *bv)
2198 fontinfo *a = (fontinfo *)av;
2199 fontinfo *b = (fontinfo *)bv;
2202 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
2204 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2205 return ((a->flags & FONTFLAG_SORT_MASK) <
2206 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2210 static int fontinfo_realname_find(void *av, void *bv)
2212 struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;
2213 fontinfo *b = (fontinfo *)bv;
2216 if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
2218 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2219 return ((a->flags & FONTFLAG_SORT_MASK) <
2220 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2224 static int fontinfo_selorder_compare(void *av, void *bv)
2226 fontinfo *a = (fontinfo *)av;
2227 fontinfo *b = (fontinfo *)bv;
2229 if ((i = strnullcasecmp(a->family, b->family)) != 0)
2232 * Font class comes immediately after family, so that fonts
2233 * from different classes with the same family
2235 if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
2236 return ((a->flags & FONTFLAG_SORT_MASK) <
2237 (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
2238 if ((i = strnullcasecmp(a->charset, b->charset)) != 0)
2240 if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)
2242 if ((i = strnullcasecmp(a->style, b->style)) != 0)
2244 if (a->size != b->size)
2245 return (a->size < b->size ? -1 : +1);
2246 if (a->index != b->index)
2247 return (a->index < b->index ? -1 : +1);
2251 static void unifontsel_draw_preview_text(unifontsel_internal *fs);
2253 static void unifontsel_deselect(unifontsel_internal *fs)
2255 fs->selected = NULL;
2256 gtk_list_store_clear(fs->style_model);
2257 gtk_list_store_clear(fs->size_model);
2258 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
2259 gtk_widget_set_sensitive(fs->size_entry, FALSE);
2260 unifontsel_draw_preview_text(fs);
2263 static void unifontsel_setup_familylist(unifontsel_internal *fs)
2266 int i, listindex, minpos = -1, maxpos = -1;
2267 char *currfamily = NULL;
2271 fs->inhibit_response = TRUE;
2273 gtk_list_store_clear(fs->family_model);
2277 * Search through the font tree for anything matching our
2278 * current filter criteria. When we find one, add its font
2279 * name to the list box.
2281 for (i = 0 ;; i++) {
2282 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2284 * info may be NULL if we've just run off the end of the
2285 * tree. We must still do a processing pass in that
2286 * situation, in case we had an unfinished font record in
2289 if (info && (info->flags &~ fs->filter_flags)) {
2290 info->familyindex = -1;
2291 continue; /* we're filtering out this font */
2293 if (!info || strnullcasecmp(currfamily, info->family) ||
2294 currflags != (info->flags & FONTFLAG_SORT_MASK)) {
2296 * We've either finished a family, or started a new
2300 gtk_list_store_append(fs->family_model, &iter);
2301 gtk_list_store_set(fs->family_model, &iter,
2302 0, currfamily, 1, minpos, 2, maxpos+1, -1);
2307 currfamily = info->family;
2308 currflags = info->flags & FONTFLAG_SORT_MASK;
2312 break; /* now we're done */
2313 info->familyindex = listindex;
2318 * If we've just filtered out the previously selected font,
2319 * deselect it thoroughly.
2321 if (fs->selected && fs->selected->familyindex < 0)
2322 unifontsel_deselect(fs);
2324 fs->inhibit_response = FALSE;
2327 static void unifontsel_setup_stylelist(unifontsel_internal *fs,
2331 int i, listindex, minpos = -1, maxpos = -1, started = FALSE;
2332 char *currcs = NULL, *currstyle = NULL;
2335 gtk_list_store_clear(fs->style_model);
2340 * Search through the font tree for anything matching our
2341 * current filter criteria. When we find one, add its charset
2342 * and/or style name to the list box.
2344 for (i = start; i <= end; i++) {
2348 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2350 * info may be NULL if we've just run off the end of the
2351 * relevant data. We must still do a processing pass in
2352 * that situation, in case we had an unfinished font
2353 * record in progress.
2355 if (info && (info->flags &~ fs->filter_flags)) {
2356 info->styleindex = -1;
2357 continue; /* we're filtering out this font */
2359 if (!info || !started || strnullcasecmp(currcs, info->charset) ||
2360 strnullcasecmp(currstyle, info->style)) {
2362 * We've either finished a style/charset, or started a
2367 gtk_list_store_append(fs->style_model, &iter);
2368 gtk_list_store_set(fs->style_model, &iter,
2369 0, currstyle, 1, minpos, 2, maxpos+1,
2370 3, TRUE, 4, PANGO_WEIGHT_NORMAL, -1);
2375 if (info->charset && strnullcasecmp(currcs, info->charset)) {
2376 gtk_list_store_append(fs->style_model, &iter);
2377 gtk_list_store_set(fs->style_model, &iter,
2378 0, info->charset, 1, -1, 2, -1,
2379 3, FALSE, 4, PANGO_WEIGHT_BOLD, -1);
2382 currcs = info->charset;
2383 currstyle = info->style;
2387 break; /* now we're done */
2388 info->styleindex = listindex;
2393 static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };
2395 static void unifontsel_setup_sizelist(unifontsel_internal *fs,
2403 gtk_list_store_clear(fs->size_model);
2407 * Search through the font tree for anything matching our
2408 * current filter criteria. When we find one, add its font
2409 * name to the list box.
2411 for (i = start; i < end; i++) {
2412 info = (fontinfo *)index234(fs->fonts_by_selorder, i);
2413 if (info->flags &~ fs->filter_flags) {
2414 info->sizeindex = -1;
2415 continue; /* we're filtering out this font */
2418 sprintf(sizetext, "%d", info->size);
2419 info->sizeindex = listindex;
2420 gtk_list_store_append(fs->size_model, &iter);
2421 gtk_list_store_set(fs->size_model, &iter,
2422 0, sizetext, 1, i, 2, info->size, -1);
2430 for (j = 0; j < lenof(unifontsel_default_sizes); j++) {
2431 sprintf(sizetext, "%d", unifontsel_default_sizes[j]);
2432 gtk_list_store_append(fs->size_model, &iter);
2433 gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,
2434 2, unifontsel_default_sizes[j], -1);
2441 static void unifontsel_set_filter_buttons(unifontsel_internal *fs)
2445 for (i = 0; i < fs->n_filter_buttons; i++) {
2446 int flagbit = GPOINTER_TO_INT(g_object_get_data
2447 (G_OBJECT(fs->filter_buttons[i]),
2449 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),
2450 !!(fs->filter_flags & flagbit));
2454 static void unifontsel_draw_preview_text_inner(unifont_drawctx *dctx,
2455 unifontsel_internal *fs)
2458 char *sizename = NULL;
2459 fontinfo *info = fs->selected;
2462 sizename = info->fontclass->scale_fontname
2463 (GTK_WIDGET(fs->u.window), info->realname, fs->selsize);
2464 font = info->fontclass->create(GTK_WIDGET(fs->u.window),
2465 sizename ? sizename : info->realname,
2466 FALSE, FALSE, 0, 0);
2470 #ifdef DRAW_TEXT_GDK
2471 if (dctx->type == DRAWTYPE_GDK) {
2472 gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_bg);
2473 gdk_draw_rectangle(dctx->u.gdk.target, dctx->u.gdk.gc, 1, 0, 0,
2474 fs->preview_width, fs->preview_height);
2475 gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_fg);
2478 #ifdef DRAW_TEXT_CAIRO
2479 if (dctx->type == DRAWTYPE_CAIRO) {
2480 cairo_set_source_rgb(dctx->u.cairo.cr,
2481 fs->preview_bg.red / 65535.0,
2482 fs->preview_bg.green / 65535.0,
2483 fs->preview_bg.blue / 65535.0);
2484 cairo_paint(dctx->u.cairo.cr);
2485 cairo_set_source_rgb(dctx->u.cairo.cr,
2486 fs->preview_fg.red / 65535.0,
2487 fs->preview_fg.green / 65535.0,
2488 fs->preview_fg.blue / 65535.0);
2494 * The pangram used here is rather carefully
2495 * constructed: it contains a sequence of very narrow
2496 * letters (`jil') and a pair of adjacent very wide
2499 * If the user selects a proportional font, it will be
2500 * coerced into fixed-width character cells when used
2501 * in the actual terminal window. We therefore display
2502 * it the same way in the preview pane, so as to show
2503 * it the way it will actually be displayed - and we
2504 * deliberately pick a pangram which will show the
2505 * resulting miskerning at its worst.
2507 * We aren't trying to sell people these fonts; we're
2508 * trying to let them make an informed choice. Better
2509 * that they find out the problems with using
2510 * proportional fonts in terminal windows here than
2511 * that they go to the effort of selecting their font
2512 * and _then_ realise it was a mistake.
2514 info->fontclass->draw_text(dctx, font,
2516 L"bankrupt jilted showmen quiz convex fogey",
2517 41, FALSE, FALSE, font->width);
2518 info->fontclass->draw_text(dctx, font,
2519 0, font->ascent + font->height,
2520 L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
2521 41, FALSE, FALSE, font->width);
2523 * The ordering of punctuation here is also selected
2524 * with some specific aims in mind. I put ` and '
2525 * together because some software (and people) still
2526 * use them as matched quotes no matter what Unicode
2527 * might say on the matter, so people can quickly
2528 * check whether they look silly in a candidate font.
2529 * The sequence #_@ is there to let people judge the
2530 * suitability of the underscore as an effectively
2531 * alphabetic character (since that's how it's often
2532 * used in practice, at least by programmers).
2534 info->fontclass->draw_text(dctx, font,
2535 0, font->ascent + font->height * 2,
2536 L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
2537 42, FALSE, FALSE, font->width);
2539 info->fontclass->destroy(font);
2545 static void unifontsel_draw_preview_text(unifontsel_internal *fs)
2547 unifont_drawctx dctx;
2550 #ifndef NO_BACKING_PIXMAPS
2551 target = fs->preview_pixmap;
2553 target = gtk_widget_get_window(fs->preview_area);
2555 if (!target) /* we may be called when we haven't created everything yet */
2558 dctx.type = DRAWTYPE_DEFAULT;
2559 #ifdef DRAW_TEXT_GDK
2560 if (dctx.type == DRAWTYPE_GDK) {
2561 dctx.u.gdk.target = target;
2562 dctx.u.gdk.gc = gdk_gc_new(target);
2565 #ifdef DRAW_TEXT_CAIRO
2566 if (dctx.type == DRAWTYPE_CAIRO) {
2567 dctx.u.cairo.widget = GTK_WIDGET(fs->preview_area);
2568 dctx.u.cairo.cr = gdk_cairo_create(target);
2572 unifontsel_draw_preview_text_inner(&dctx, fs);
2574 #ifdef DRAW_TEXT_GDK
2575 if (dctx.type == DRAWTYPE_GDK) {
2576 gdk_gc_unref(dctx.u.gdk.gc);
2579 #ifdef DRAW_TEXT_CAIRO
2580 if (dctx.type == DRAWTYPE_CAIRO) {
2581 cairo_destroy(dctx.u.cairo.cr);
2585 gdk_window_invalidate_rect(gtk_widget_get_window(fs->preview_area),
2589 static void unifontsel_select_font(unifontsel_internal *fs,
2590 fontinfo *info, int size, int leftlist,
2591 int size_is_explicit)
2596 GtkTreePath *treepath;
2599 fs->inhibit_response = TRUE;
2601 fs->selected = info;
2603 if (size_is_explicit)
2604 fs->intendedsize = size;
2606 gtk_widget_set_sensitive(fs->u.ok_button, TRUE);
2609 * Find the index of this fontinfo in the selorder list.
2612 findpos234(fs->fonts_by_selorder, info, NULL, &index);
2616 * Adjust the font selector flags and redo the font family
2617 * list box, if necessary.
2619 if (leftlist <= 0 &&
2620 (fs->filter_flags | info->flags) != fs->filter_flags) {
2621 fs->filter_flags |= info->flags;
2622 unifontsel_set_filter_buttons(fs);
2623 unifontsel_setup_familylist(fs);
2627 * Find the appropriate family name and select it in the list.
2629 assert(info->familyindex >= 0);
2630 treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);
2631 gtk_tree_selection_select_path
2632 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),
2634 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),
2635 treepath, NULL, FALSE, 0.0, 0.0);
2636 success = gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model),
2639 gtk_tree_path_free(treepath);
2642 * Now set up the font style list.
2644 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,
2645 1, &minval, 2, &maxval, -1);
2647 unifontsel_setup_stylelist(fs, minval, maxval);
2650 * Find the appropriate style name and select it in the list.
2653 assert(info->styleindex >= 0);
2654 treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);
2655 gtk_tree_selection_select_path
2656 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),
2658 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),
2659 treepath, NULL, FALSE, 0.0, 0.0);
2660 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),
2662 gtk_tree_path_free(treepath);
2665 * And set up the size list.
2667 gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,
2668 1, &minval, 2, &maxval, -1);
2670 unifontsel_setup_sizelist(fs, minval, maxval);
2673 * Find the appropriate size, and select it in the list.
2676 assert(info->sizeindex >= 0);
2677 treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);
2678 gtk_tree_selection_select_path
2679 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),
2681 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2682 treepath, NULL, FALSE, 0.0, 0.0);
2683 gtk_tree_path_free(treepath);
2687 for (j = 0; j < lenof(unifontsel_default_sizes); j++)
2688 if (unifontsel_default_sizes[j] == size) {
2689 treepath = gtk_tree_path_new_from_indices(j, -1);
2690 gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),
2691 treepath, NULL, FALSE);
2692 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
2693 treepath, NULL, FALSE, 0.0,
2695 gtk_tree_path_free(treepath);
2700 * And set up the font size text entry box.
2704 sprintf(sizetext, "%d", size);
2705 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);
2709 unifontsel_setup_sizelist(fs, 0, 0);
2710 gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");
2714 * Grey out the font size edit box if we're not using a
2717 gtk_editable_set_editable(GTK_EDITABLE(fs->size_entry),
2718 fs->selected->size == 0);
2719 gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);
2721 unifontsel_draw_preview_text(fs);
2723 fs->inhibit_response = FALSE;
2726 static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)
2728 unifontsel_internal *fs = (unifontsel_internal *)data;
2729 int newstate = gtk_toggle_button_get_active(tb);
2731 int flagbit = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tb),
2735 newflags = fs->filter_flags | flagbit;
2737 newflags = fs->filter_flags & ~flagbit;
2739 if (fs->filter_flags != newflags) {
2740 fs->filter_flags = newflags;
2741 unifontsel_setup_familylist(fs);
2745 static void unifontsel_add_entry(void *ctx, const char *realfontname,
2746 const char *family, const char *charset,
2747 const char *style, const char *stylekey,
2748 int size, int flags,
2749 const struct unifont_vtable *fontclass)
2751 unifontsel_internal *fs = (unifontsel_internal *)ctx;
2756 totalsize = sizeof(fontinfo) + strlen(realfontname) +
2757 (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +
2758 (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;
2759 info = (fontinfo *)smalloc(totalsize);
2760 info->fontclass = fontclass;
2761 p = (char *)info + sizeof(fontinfo);
2763 strcpy(p, realfontname);
2770 info->family = NULL;
2776 info->charset = NULL;
2785 strcpy(p, stylekey);
2788 info->stylekey = NULL;
2789 assert(p - (char *)info <= totalsize);
2791 info->flags = flags;
2792 info->index = count234(fs->fonts_by_selorder);
2795 * It's just conceivable that a misbehaving font enumerator
2796 * might tell us about the same font real name more than once,
2797 * in which case we should silently drop the new one.
2799 if (add234(fs->fonts_by_realname, info) != info) {
2804 * However, we should never get a duplicate key in the
2805 * selorder tree, because the index field carefully
2806 * disambiguates otherwise identical records.
2808 add234(fs->fonts_by_selorder, info);
2811 static fontinfo *update_for_intended_size(unifontsel_internal *fs,
2814 fontinfo info2, *below, *above;
2818 * Copy the info structure. This doesn't copy its dynamic
2819 * string fields, but that's unimportant because all we're
2820 * going to do is to adjust the size field and use it in one
2824 info2.size = fs->intendedsize;
2827 * Search in the tree to find the fontinfo structure which
2828 * best approximates the size the user last requested.
2830 below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
2834 above = index234(fs->fonts_by_selorder, pos+1);
2837 * See if we've found it exactly, which is an easy special
2838 * case. If we have, it'll be in `below' and not `above',
2839 * because we did a REL234_LE rather than REL234_LT search.
2841 if (below && !fontinfo_selorder_compare(&info2, below))
2845 * Now we've either found two suitable fonts, one smaller and
2846 * one larger, or we're at one or other extreme end of the
2847 * scale. Find out which, by NULLing out either of below and
2848 * above if it differs from this one in any respect but size
2849 * (and the disambiguating index field). Bear in mind, also,
2850 * that either one might _already_ be NULL if we're at the
2851 * extreme ends of the font list.
2854 info2.size = below->size;
2855 info2.index = below->index;
2856 if (fontinfo_selorder_compare(&info2, below))
2860 info2.size = above->size;
2861 info2.index = above->index;
2862 if (fontinfo_selorder_compare(&info2, above))
2867 * Now return whichever of above and below is non-NULL, if
2868 * that's unambiguous.
2876 * And now we really do have to make a choice about whether to
2877 * round up or down. We'll do it by rounding to nearest,
2878 * breaking ties by rounding up.
2880 if (above->size - fs->intendedsize <= fs->intendedsize - below->size)
2886 static void family_changed(GtkTreeSelection *treeselection, gpointer data)
2888 unifontsel_internal *fs = (unifontsel_internal *)data;
2889 GtkTreeModel *treemodel;
2890 GtkTreeIter treeiter;
2894 if (fs->inhibit_response) /* we made this change ourselves */
2897 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2900 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2901 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2902 info = update_for_intended_size(fs, info);
2904 return; /* _shouldn't_ happen unless font list is completely funted */
2906 fs->selsize = fs->intendedsize; /* font is scalable */
2907 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2911 static void style_changed(GtkTreeSelection *treeselection, gpointer data)
2913 unifontsel_internal *fs = (unifontsel_internal *)data;
2914 GtkTreeModel *treemodel;
2915 GtkTreeIter treeiter;
2919 if (fs->inhibit_response) /* we made this change ourselves */
2922 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2925 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
2927 return; /* somehow a charset heading got clicked */
2928 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2929 info = update_for_intended_size(fs, info);
2931 return; /* _shouldn't_ happen unless font list is completely funted */
2933 fs->selsize = fs->intendedsize; /* font is scalable */
2934 unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
2938 static void size_changed(GtkTreeSelection *treeselection, gpointer data)
2940 unifontsel_internal *fs = (unifontsel_internal *)data;
2941 GtkTreeModel *treemodel;
2942 GtkTreeIter treeiter;
2946 if (fs->inhibit_response) /* we made this change ourselves */
2949 if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
2952 gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);
2953 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2954 unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE);
2957 static void size_entry_changed(GtkEditable *ed, gpointer data)
2959 unifontsel_internal *fs = (unifontsel_internal *)data;
2963 if (fs->inhibit_response) /* we made this change ourselves */
2966 text = gtk_entry_get_text(GTK_ENTRY(ed));
2970 assert(fs->selected->size == 0);
2971 unifontsel_select_font(fs, fs->selected, size, 3, TRUE);
2975 static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,
2976 GtkTreeViewColumn *column, gpointer data)
2978 unifontsel_internal *fs = (unifontsel_internal *)data;
2980 int minval, newsize;
2981 fontinfo *info, *newinfo;
2984 if (fs->inhibit_response) /* we made this change ourselves */
2987 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);
2988 gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);
2989 info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
2992 struct fontinfo_realname_find f;
2994 newname = info->fontclass->canonify_fontname
2995 (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE);
2997 f.realname = newname;
2999 newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
3003 return; /* font name not in our index */
3004 if (newinfo == info)
3005 return; /* didn't change under canonification => not an alias */
3006 unifontsel_select_font(fs, newinfo,
3007 newinfo->size ? newinfo->size : newsize,
3012 #if GTK_CHECK_VERSION(3,0,0)
3013 static gint unifontsel_draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
3015 unifontsel_internal *fs = (unifontsel_internal *)data;
3016 unifont_drawctx dctx;
3018 dctx.type = DRAWTYPE_CAIRO;
3019 dctx.u.cairo.widget = widget;
3020 dctx.u.cairo.cr = cr;
3021 unifontsel_draw_preview_text_inner(&dctx, fs);
3026 static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,
3029 unifontsel_internal *fs = (unifontsel_internal *)data;
3031 #ifndef NO_BACKING_PIXMAPS
3032 if (fs->preview_pixmap) {
3033 gdk_draw_pixmap(gtk_widget_get_window(widget),
3034 (gtk_widget_get_style(widget)->fg_gc
3035 [gtk_widget_get_state(widget)]),
3037 event->area.x, event->area.y,
3038 event->area.x, event->area.y,
3039 event->area.width, event->area.height);
3042 unifontsel_draw_preview_text(fs);
3049 static gint unifontsel_configure_area(GtkWidget *widget,
3050 GdkEventConfigure *event, gpointer data)
3052 #ifndef NO_BACKING_PIXMAPS
3053 unifontsel_internal *fs = (unifontsel_internal *)data;
3054 int ox, oy, nx, ny, x, y;
3057 * Enlarge the pixmap, but never shrink it.
3059 ox = fs->preview_width;
3060 oy = fs->preview_height;
3063 if (x > ox || y > oy) {
3064 if (fs->preview_pixmap)
3065 gdk_pixmap_unref(fs->preview_pixmap);
3067 nx = (x > ox ? x : ox);
3068 ny = (y > oy ? y : oy);
3069 fs->preview_pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
3071 fs->preview_width = nx;
3072 fs->preview_height = ny;
3074 unifontsel_draw_preview_text(fs);
3078 gdk_window_invalidate_rect(gtk_widget_get_window(widget), NULL, FALSE);
3083 unifontsel *unifontsel_new(const char *wintitle)
3085 unifontsel_internal *fs = snew(unifontsel_internal);
3086 GtkWidget *table, *label, *w, *ww, *scroll;
3087 GtkListStore *model;
3088 GtkTreeViewColumn *column;
3089 int lists_height, preview_height, font_width, style_width, size_width;
3092 fs->inhibit_response = FALSE;
3093 fs->selected = NULL;
3099 * Invent some magic size constants.
3101 get_label_text_dimensions("Quite Long Font Name (Foundry)",
3104 lists_height = 14 * height;
3105 preview_height = 5 * height;
3107 get_label_text_dimensions("Italic Extra Condensed", &width, &height);
3108 style_width = width;
3110 get_label_text_dimensions("48000", &width, &height);
3115 * Create the dialog box and initialise the user-visible
3116 * fields in the returned structure.
3118 fs->u.user_data = NULL;
3119 fs->u.window = GTK_WINDOW(gtk_dialog_new());
3120 gtk_window_set_title(fs->u.window, wintitle);
3121 fs->u.cancel_button = gtk_dialog_add_button
3122 (GTK_DIALOG(fs->u.window), STANDARD_CANCEL_LABEL, GTK_RESPONSE_CANCEL);
3123 fs->u.ok_button = gtk_dialog_add_button
3124 (GTK_DIALOG(fs->u.window), STANDARD_OK_LABEL, GTK_RESPONSE_OK);
3125 gtk_widget_grab_default(fs->u.ok_button);
3128 * Now set up the internal fields, including in particular all
3129 * the controls that actually allow the user to select fonts.
3131 #if GTK_CHECK_VERSION(3,0,0)
3132 table = gtk_grid_new();
3133 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
3135 table = gtk_table_new(8, 3, FALSE);
3136 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
3138 gtk_widget_show(table);
3140 #if GTK_CHECK_VERSION(3,0,0)
3141 /* GtkAlignment has become deprecated and we use the "margin"
3144 g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
3145 #elif GTK_CHECK_VERSION(2,4,0)
3146 /* GtkAlignment seems to be the simplest way to put padding round things */
3147 w = gtk_alignment_new(0, 0, 1, 1);
3148 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
3149 gtk_container_add(GTK_CONTAINER(w), table);
3152 /* In GTK < 2.4, even that isn't available */
3156 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area
3157 (GTK_DIALOG(fs->u.window))),
3160 label = gtk_label_new_with_mnemonic("_Font:");
3161 gtk_widget_show(label);
3162 align_label_left(GTK_LABEL(label));
3163 #if GTK_CHECK_VERSION(3,0,0)
3164 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
3165 g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
3167 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
3171 * The Font list box displays only a string, but additionally
3172 * stores two integers which give the limits within the
3173 * tree234 of the font entries covered by this list entry.
3175 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
3176 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
3177 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
3178 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
3180 column = gtk_tree_view_column_new_with_attributes
3181 ("Font", gtk_cell_renderer_text_new(),
3182 "text", 0, (char *)NULL);
3183 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3184 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3185 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3186 "changed", G_CALLBACK(family_changed), fs);
3187 g_signal_connect(G_OBJECT(w), "row-activated",
3188 G_CALLBACK(alias_resolve), fs);
3190 scroll = gtk_scrolled_window_new(NULL, NULL);
3191 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3193 gtk_container_add(GTK_CONTAINER(scroll), w);
3194 gtk_widget_show(scroll);
3195 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3196 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3197 gtk_widget_set_size_request(scroll, font_width, lists_height);
3198 #if GTK_CHECK_VERSION(3,0,0)
3199 gtk_grid_attach(GTK_GRID(table), scroll, 0, 1, 1, 2);
3200 g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
3202 gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,
3203 GTK_EXPAND | GTK_FILL, 0, 0);
3205 fs->family_model = model;
3206 fs->family_list = w;
3208 label = gtk_label_new_with_mnemonic("_Style:");
3209 gtk_widget_show(label);
3210 align_label_left(GTK_LABEL(label));
3211 #if GTK_CHECK_VERSION(3,0,0)
3212 gtk_grid_attach(GTK_GRID(table), label, 1, 0, 1, 1);
3213 g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
3215 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
3219 * The Style list box can contain insensitive elements (character
3220 * set headings for server-side fonts), so we add an extra column
3221 * to the list store to hold that information. Also, since GTK3 at
3222 * least doesn't seem to display insensitive elements differently
3223 * by default, we add a further column to change their style.
3225 model = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
3226 G_TYPE_BOOLEAN, G_TYPE_INT);
3227 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
3228 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
3229 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
3231 column = gtk_tree_view_column_new_with_attributes
3232 ("Style", gtk_cell_renderer_text_new(),
3233 "text", 0, "sensitive", 3, "weight", 4, (char *)NULL);
3234 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3235 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3236 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3237 "changed", G_CALLBACK(style_changed), fs);
3239 scroll = gtk_scrolled_window_new(NULL, NULL);
3240 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3242 gtk_container_add(GTK_CONTAINER(scroll), w);
3243 gtk_widget_show(scroll);
3244 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3245 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3246 gtk_widget_set_size_request(scroll, style_width, lists_height);
3247 #if GTK_CHECK_VERSION(3,0,0)
3248 gtk_grid_attach(GTK_GRID(table), scroll, 1, 1, 1, 2);
3249 g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
3251 gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,
3252 GTK_EXPAND | GTK_FILL, 0, 0);
3254 fs->style_model = model;
3257 label = gtk_label_new_with_mnemonic("Si_ze:");
3258 gtk_widget_show(label);
3259 align_label_left(GTK_LABEL(label));
3260 #if GTK_CHECK_VERSION(3,0,0)
3261 gtk_grid_attach(GTK_GRID(table), label, 2, 0, 1, 1);
3262 g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
3264 gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
3268 * The Size label attaches primarily to a text input box so
3269 * that the user can select a size of their choice. The list
3270 * of available sizes is secondary.
3272 fs->size_entry = w = gtk_entry_new();
3273 gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
3274 gtk_widget_set_size_request(w, size_width, -1);
3276 #if GTK_CHECK_VERSION(3,0,0)
3277 gtk_grid_attach(GTK_GRID(table), w, 2, 1, 1, 1);
3278 g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
3280 gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);
3282 g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),
3285 model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
3286 w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
3287 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
3289 column = gtk_tree_view_column_new_with_attributes
3290 ("Size", gtk_cell_renderer_text_new(),
3291 "text", 0, (char *)NULL);
3292 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
3293 gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
3294 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
3295 "changed", G_CALLBACK(size_changed), fs);
3297 scroll = gtk_scrolled_window_new(NULL, NULL);
3298 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
3300 gtk_container_add(GTK_CONTAINER(scroll), w);
3301 gtk_widget_show(scroll);
3302 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
3303 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3304 #if GTK_CHECK_VERSION(3,0,0)
3305 gtk_grid_attach(GTK_GRID(table), scroll, 2, 2, 1, 1);
3306 g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
3308 gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,
3309 GTK_EXPAND | GTK_FILL, 0, 0);
3311 fs->size_model = model;
3317 fs->preview_area = gtk_drawing_area_new();
3318 #ifndef NO_BACKING_PIXMAPS
3319 fs->preview_pixmap = NULL;
3321 fs->preview_width = 0;
3322 fs->preview_height = 0;
3323 fs->preview_fg.pixel = fs->preview_bg.pixel = 0;
3324 fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;
3325 fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;
3326 #if !GTK_CHECK_VERSION(3,0,0)
3327 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,
3329 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,
3332 #if GTK_CHECK_VERSION(3,0,0)
3333 g_signal_connect(G_OBJECT(fs->preview_area), "draw",
3334 G_CALLBACK(unifontsel_draw_area), fs);
3336 g_signal_connect(G_OBJECT(fs->preview_area), "expose_event",
3337 G_CALLBACK(unifontsel_expose_area), fs);
3339 g_signal_connect(G_OBJECT(fs->preview_area), "configure_event",
3340 G_CALLBACK(unifontsel_configure_area), fs);
3341 gtk_widget_set_size_request(fs->preview_area, 1, preview_height);
3342 gtk_widget_show(fs->preview_area);
3343 ww = fs->preview_area;
3344 w = gtk_frame_new(NULL);
3345 gtk_container_add(GTK_CONTAINER(w), ww);
3348 #if GTK_CHECK_VERSION(3,0,0)
3349 /* GtkAlignment has become deprecated and we use the "margin"
3351 g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
3352 #elif GTK_CHECK_VERSION(2,4,0)
3354 /* GtkAlignment seems to be the simplest way to put padding round things */
3355 w = gtk_alignment_new(0, 0, 1, 1);
3356 gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
3357 gtk_container_add(GTK_CONTAINER(w), ww);
3362 w = gtk_frame_new("Preview of font");
3363 gtk_container_add(GTK_CONTAINER(w), ww);
3365 #if GTK_CHECK_VERSION(3,0,0)
3366 gtk_grid_attach(GTK_GRID(table), w, 0, 3, 3, 1);
3367 g_object_set(G_OBJECT(w), "expand", TRUE, (const char *)NULL);
3369 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,
3370 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);
3374 * We only provide the checkboxes for client- and server-side
3375 * fonts if we have the X11 back end available, because that's the
3376 * only situation in which more than one class of font is
3379 fs->n_filter_buttons = 0;
3380 #ifndef NOT_X_WINDOWS
3381 w = gtk_check_button_new_with_label("Show client-side fonts");
3382 g_object_set_data(G_OBJECT(w), "user-data",
3383 GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));
3384 g_signal_connect(G_OBJECT(w), "toggled",
3385 G_CALLBACK(unifontsel_button_toggled), fs);
3387 fs->filter_buttons[fs->n_filter_buttons++] = w;
3388 #if GTK_CHECK_VERSION(3,0,0)
3389 gtk_grid_attach(GTK_GRID(table), w, 0, 4, 3, 1);
3390 g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
3392 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);
3394 w = gtk_check_button_new_with_label("Show server-side fonts");
3395 g_object_set_data(G_OBJECT(w), "user-data",
3396 GINT_TO_POINTER(FONTFLAG_SERVERSIDE));
3397 g_signal_connect(G_OBJECT(w), "toggled",
3398 G_CALLBACK(unifontsel_button_toggled), fs);
3400 fs->filter_buttons[fs->n_filter_buttons++] = w;
3401 #if GTK_CHECK_VERSION(3,0,0)
3402 gtk_grid_attach(GTK_GRID(table), w, 0, 5, 3, 1);
3403 g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
3405 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);
3407 w = gtk_check_button_new_with_label("Show server-side font aliases");
3408 g_object_set_data(G_OBJECT(w), "user-data",
3409 GINT_TO_POINTER(FONTFLAG_SERVERALIAS));
3410 g_signal_connect(G_OBJECT(w), "toggled",
3411 G_CALLBACK(unifontsel_button_toggled), fs);
3413 fs->filter_buttons[fs->n_filter_buttons++] = w;
3414 #if GTK_CHECK_VERSION(3,0,0)
3415 gtk_grid_attach(GTK_GRID(table), w, 0, 6, 3, 1);
3416 g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
3418 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);
3420 #endif /* NOT_X_WINDOWS */
3421 w = gtk_check_button_new_with_label("Show non-monospaced fonts");
3422 g_object_set_data(G_OBJECT(w), "user-data",
3423 GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));
3424 g_signal_connect(G_OBJECT(w), "toggled",
3425 G_CALLBACK(unifontsel_button_toggled), fs);
3427 fs->filter_buttons[fs->n_filter_buttons++] = w;
3428 #if GTK_CHECK_VERSION(3,0,0)
3429 gtk_grid_attach(GTK_GRID(table), w, 0, 7, 3, 1);
3430 g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
3432 gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);
3435 assert(fs->n_filter_buttons <= lenof(fs->filter_buttons));
3436 fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |
3437 FONTFLAG_SERVERALIAS;
3438 unifontsel_set_filter_buttons(fs);
3441 * Go and find all the font names, and set up our master font
3444 fs->fonts_by_realname = newtree234(fontinfo_realname_compare);
3445 fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);
3446 for (i = 0; i < lenof(unifont_types); i++)
3447 unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),
3448 unifontsel_add_entry, fs);
3451 * And set up the initial font names list.
3453 unifontsel_setup_familylist(fs);
3455 fs->selsize = fs->intendedsize = 13; /* random default */
3456 gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
3458 return (unifontsel *)fs;
3461 void unifontsel_destroy(unifontsel *fontsel)
3463 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3466 #ifndef NO_BACKING_PIXMAPS
3467 if (fs->preview_pixmap)
3468 gdk_pixmap_unref(fs->preview_pixmap);
3471 freetree234(fs->fonts_by_selorder);
3472 while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)
3474 freetree234(fs->fonts_by_realname);
3476 gtk_widget_destroy(GTK_WIDGET(fs->u.window));
3480 void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
3482 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3483 int i, start, end, size, flags;
3484 const char *fontname2 = NULL;
3488 * Provide a default if given an empty or null font name.
3490 if (!fontname || !*fontname)
3491 fontname = DEFAULT_GTK_FONT;
3494 * Call the canonify_fontname function.
3496 fontname = unifont_do_prefix(fontname, &start, &end);
3497 for (i = start; i < end; i++) {
3498 fontname2 = unifont_types[i]->canonify_fontname
3499 (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE);
3504 return; /* font name not recognised */
3507 * Now look up the canonified font name in our index.
3510 struct fontinfo_realname_find f;
3511 f.realname = fontname2;
3513 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
3517 * If we've found the font, and its size field is either
3518 * correct or zero (the latter indicating a scalable font),
3519 * then we're done. Otherwise, try looking up the original
3520 * font name instead.
3522 if (!info || (info->size != size && info->size != 0)) {
3523 struct fontinfo_realname_find f;
3524 f.realname = fontname;
3527 info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
3528 if (!info || info->size != size)
3529 return; /* font name not in our index */
3533 * Now we've got a fontinfo structure and a font size, so we
3534 * know everything we need to fill in all the fields in the
3537 unifontsel_select_font(fs, info, size, 0, TRUE);
3540 char *unifontsel_get_name(unifontsel *fontsel)
3542 unifontsel_internal *fs = (unifontsel_internal *)fontsel;
3548 if (fs->selected->size == 0) {
3549 name = fs->selected->fontclass->scale_fontname
3550 (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
3552 char *ret = dupcat(fs->selected->fontclass->prefix, ":",
3559 return dupcat(fs->selected->fontclass->prefix, ":",
3560 fs->selected->realname, NULL);
3563 #endif /* GTK_CHECK_VERSION(2,0,0) */