OSDN Git Service

Add PuTTY 0.61 to contrib directory.
[ffftp/ffftp.git] / contrib / putty / UNIX / GTKFONT.C
diff --git a/contrib/putty/UNIX/GTKFONT.C b/contrib/putty/UNIX/GTKFONT.C
new file mode 100644 (file)
index 0000000..e382183
--- /dev/null
@@ -0,0 +1,2575 @@
+/*\r
+ * Unified font management for GTK.\r
+ * \r
+ * PuTTY is willing to use both old-style X server-side bitmap\r
+ * fonts _and_ GTK2/Pango client-side fonts. This requires us to\r
+ * do a bit of work to wrap the two wildly different APIs into\r
+ * forms the rest of the code can switch between seamlessly, and\r
+ * also requires a custom font selector capable of handling both\r
+ * types of font.\r
+ */\r
+\r
+#include <assert.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <gtk/gtk.h>\r
+#include <gdk/gdkkeysyms.h>\r
+#include <gdk/gdkx.h>\r
+#include <X11/Xlib.h>\r
+#include <X11/Xutil.h>\r
+#include <X11/Xatom.h>\r
+\r
+#include "putty.h"\r
+#include "gtkfont.h"\r
+#include "tree234.h"\r
+\r
+/*\r
+ * Future work:\r
+ * \r
+ *  - it would be nice to have a display of the current font name,\r
+ *    and in particular whether it's client- or server-side,\r
+ *    during the progress of the font selector.\r
+ * \r
+ *  - all the GDK font functions used in the x11font subclass are\r
+ *    deprecated, so one day they may go away. When this happens -\r
+ *    or before, if I'm feeling proactive - it oughtn't to be too\r
+ *    difficult in principle to convert the whole thing to use\r
+ *    actual Xlib font calls.\r
+ * \r
+ *  - it would be nice if we could move the processing of\r
+ *    underline and VT100 double width into this module, so that\r
+ *    instead of using the ghastly pixmap-stretching technique\r
+ *    everywhere we could tell the Pango backend to scale its\r
+ *    fonts to double size properly and at full resolution.\r
+ *    However, this requires me to learn how to make Pango stretch\r
+ *    text to an arbitrary aspect ratio (for double-width only\r
+ *    text, which perversely is harder than DW+DH), and right now\r
+ *    I haven't the energy.\r
+ */\r
+\r
+/*\r
+ * Ad-hoc vtable mechanism to allow font structures to be\r
+ * polymorphic.\r
+ * \r
+ * Any instance of `unifont' used in the vtable functions will\r
+ * actually be the first element of a larger structure containing\r
+ * data specific to the subtype. This is permitted by the ISO C\r
+ * provision that one may safely cast between a pointer to a\r
+ * structure and a pointer to its first element.\r
+ */\r
+\r
+#define FONTFLAG_CLIENTSIDE    0x0001\r
+#define FONTFLAG_SERVERSIDE    0x0002\r
+#define FONTFLAG_SERVERALIAS   0x0004\r
+#define FONTFLAG_NONMONOSPACED 0x0008\r
+\r
+#define FONTFLAG_SORT_MASK     0x0007 /* used to disambiguate font families */\r
+\r
+typedef void (*fontsel_add_entry)(void *ctx, const char *realfontname,\r
+                                 const char *family, const char *charset,\r
+                                 const char *style, const char *stylekey,\r
+                                 int size, int flags,\r
+                                 const struct unifont_vtable *fontclass);\r
+\r
+struct unifont_vtable {\r
+    /*\r
+     * `Methods' of the `class'.\r
+     */\r
+    unifont *(*create)(GtkWidget *widget, const char *name, int wide, int bold,\r
+                      int shadowoffset, int shadowalways);\r
+    void (*destroy)(unifont *font);\r
+    void (*draw_text)(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                     int x, int y, const char *string, int len, int wide,\r
+                     int bold, int cellwidth);\r
+    void (*enum_fonts)(GtkWidget *widget,\r
+                      fontsel_add_entry callback, void *callback_ctx);\r
+    char *(*canonify_fontname)(GtkWidget *widget, const char *name, int *size,\r
+                              int *flags, int resolve_aliases);\r
+    char *(*scale_fontname)(GtkWidget *widget, const char *name, int size);\r
+\r
+    /*\r
+     * `Static data members' of the `class'.\r
+     */\r
+    const char *prefix;\r
+};\r
+\r
+/* ----------------------------------------------------------------------\r
+ * GDK-based X11 font implementation.\r
+ */\r
+\r
+static void x11font_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                             int x, int y, const char *string, int len,\r
+                             int wide, int bold, int cellwidth);\r
+static unifont *x11font_create(GtkWidget *widget, const char *name,\r
+                              int wide, int bold,\r
+                              int shadowoffset, int shadowalways);\r
+static void x11font_destroy(unifont *font);\r
+static void x11font_enum_fonts(GtkWidget *widget,\r
+                              fontsel_add_entry callback, void *callback_ctx);\r
+static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,\r
+                                      int *size, int *flags,\r
+                                      int resolve_aliases);\r
+static char *x11font_scale_fontname(GtkWidget *widget, const char *name,\r
+                                   int size);\r
+\r
+struct x11font {\r
+    struct unifont u;\r
+    /*\r
+     * Actual font objects. We store a number of these, for\r
+     * automatically guessed bold and wide variants.\r
+     * \r
+     * The parallel array `allocated' indicates whether we've\r
+     * tried to fetch a subfont already (thus distinguishing NULL\r
+     * because we haven't tried yet from NULL because we tried and\r
+     * failed, so that we don't keep trying and failing\r
+     * subsequently).\r
+     */\r
+    GdkFont *fonts[4];\r
+    int allocated[4];\r
+    /*\r
+     * `sixteen_bit' is true iff the font object is indexed by\r
+     * values larger than a byte. That is, this flag tells us\r
+     * whether we use gdk_draw_text_wc() or gdk_draw_text().\r
+     */\r
+    int sixteen_bit;\r
+    /*\r
+     * `variable' is true iff the font is non-fixed-pitch. This\r
+     * enables some code which takes greater care over character\r
+     * positioning during text drawing.\r
+     */\r
+    int variable;\r
+    /*\r
+     * Data passed in to unifont_create().\r
+     */\r
+    int wide, bold, shadowoffset, shadowalways;\r
+};\r
+\r
+static const struct unifont_vtable x11font_vtable = {\r
+    x11font_create,\r
+    x11font_destroy,\r
+    x11font_draw_text,\r
+    x11font_enum_fonts,\r
+    x11font_canonify_fontname,\r
+    x11font_scale_fontname,\r
+    "server",\r
+};\r
+\r
+char *x11_guess_derived_font_name(GdkFont *font, int bold, int wide)\r
+{\r
+    XFontStruct *xfs = GDK_FONT_XFONT(font);\r
+    Display *disp = GDK_FONT_XDISPLAY(font);\r
+    Atom fontprop = XInternAtom(disp, "FONT", False);\r
+    unsigned long ret;\r
+    if (XGetFontProperty(xfs, fontprop, &ret)) {\r
+       char *name = XGetAtomName(disp, (Atom)ret);\r
+       if (name && name[0] == '-') {\r
+           char *strings[13];\r
+           char *dupname, *extrafree = NULL, *ret;\r
+           char *p, *q;\r
+           int nstr;\r
+\r
+           p = q = dupname = dupstr(name); /* skip initial minus */\r
+           nstr = 0;\r
+\r
+           while (*p && nstr < lenof(strings)) {\r
+               if (*p == '-') {\r
+                   *p = '\0';\r
+                   strings[nstr++] = p+1;\r
+               }\r
+               p++;\r
+           }\r
+\r
+           if (nstr < lenof(strings))\r
+               return NULL;           /* XLFD was malformed */\r
+\r
+           if (bold)\r
+               strings[2] = "bold";\r
+\r
+           if (wide) {\r
+               /* 4 is `wideness', which obviously may have changed. */\r
+               /* 5 is additional style, which may be e.g. `ja' or `ko'. */\r
+               strings[4] = strings[5] = "*";\r
+               strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));\r
+           }\r
+\r
+           ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],\r
+                        "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],\r
+                        "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],\r
+                        "-", strings[ 9], "-", strings[10], "-", strings[11],\r
+                        "-", strings[12], NULL);\r
+           sfree(extrafree);\r
+           sfree(dupname);\r
+\r
+           return ret;\r
+       }\r
+    }\r
+    return NULL;\r
+}\r
+\r
+static int x11_font_width(GdkFont *font, int sixteen_bit)\r
+{\r
+    if (sixteen_bit) {\r
+       XChar2b space;\r
+       space.byte1 = 0;\r
+       space.byte2 = '0';\r
+       return gdk_text_width(font, (const gchar *)&space, 2);\r
+    } else {\r
+       return gdk_char_width(font, '0');\r
+    }\r
+}\r
+\r
+static unifont *x11font_create(GtkWidget *widget, const char *name,\r
+                              int wide, int bold,\r
+                              int shadowoffset, int shadowalways)\r
+{\r
+    struct x11font *xfont;\r
+    GdkFont *font;\r
+    XFontStruct *xfs;\r
+    Display *disp;\r
+    Atom charset_registry, charset_encoding, spacing;\r
+    unsigned long registry_ret, encoding_ret, spacing_ret;\r
+    int pubcs, realcs, sixteen_bit, variable;\r
+    int i;\r
+\r
+    font = gdk_font_load(name);\r
+    if (!font)\r
+       return NULL;\r
+\r
+    xfs = GDK_FONT_XFONT(font);\r
+    disp = GDK_FONT_XDISPLAY(font);\r
+\r
+    charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);\r
+    charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);\r
+\r
+    pubcs = realcs = CS_NONE;\r
+    sixteen_bit = FALSE;\r
+    variable = TRUE;\r
+\r
+    if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&\r
+       XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {\r
+       char *reg, *enc;\r
+       reg = XGetAtomName(disp, (Atom)registry_ret);\r
+       enc = XGetAtomName(disp, (Atom)encoding_ret);\r
+       if (reg && enc) {\r
+           char *encoding = dupcat(reg, "-", enc, NULL);\r
+           pubcs = realcs = charset_from_xenc(encoding);\r
+\r
+           /*\r
+            * iso10646-1 is the only wide font encoding we\r
+            * support. In this case, we expect clients to give us\r
+            * UTF-8, which this module must internally convert\r
+            * into 16-bit Unicode.\r
+            */\r
+           if (!strcasecmp(encoding, "iso10646-1")) {\r
+               sixteen_bit = TRUE;\r
+               pubcs = realcs = CS_UTF8;\r
+           }\r
+\r
+           /*\r
+            * Hack for X line-drawing characters: if the primary\r
+            * font is encoded as ISO-8859-1, and has valid glyphs\r
+            * in the first 32 char positions, it is assumed that\r
+            * those glyphs are the VT100 line-drawing character\r
+            * set.\r
+            * \r
+            * Actually, we'll hack even harder by only checking\r
+            * position 0x19 (vertical line, VT100 linedrawing\r
+            * `x'). Then we can check it easily by seeing if the\r
+            * ascent and descent differ.\r
+            */\r
+           if (pubcs == CS_ISO8859_1) {\r
+               int lb, rb, wid, asc, desc;\r
+               gchar text[2];\r
+\r
+               text[1] = '\0';\r
+               text[0] = '\x12';\r
+               gdk_string_extents(font, text, &lb, &rb, &wid, &asc, &desc);\r
+               if (asc != desc)\r
+                   realcs = CS_ISO8859_1_X11;\r
+           }\r
+\r
+           sfree(encoding);\r
+       }\r
+    }\r
+\r
+    spacing = XInternAtom(disp, "SPACING", False);\r
+    if (XGetFontProperty(xfs, spacing, &spacing_ret)) {\r
+       char *spc;\r
+       spc = XGetAtomName(disp, (Atom)spacing_ret);\r
+\r
+       if (spc && strchr("CcMm", spc[0]))\r
+           variable = FALSE;\r
+    }\r
+\r
+    xfont = snew(struct x11font);\r
+    xfont->u.vt = &x11font_vtable;\r
+    xfont->u.width = x11_font_width(font, sixteen_bit);\r
+    xfont->u.ascent = font->ascent;\r
+    xfont->u.descent = font->descent;\r
+    xfont->u.height = xfont->u.ascent + xfont->u.descent;\r
+    xfont->u.public_charset = pubcs;\r
+    xfont->u.real_charset = realcs;\r
+    xfont->fonts[0] = font;\r
+    xfont->allocated[0] = TRUE;\r
+    xfont->sixteen_bit = sixteen_bit;\r
+    xfont->variable = variable;\r
+    xfont->wide = wide;\r
+    xfont->bold = bold;\r
+    xfont->shadowoffset = shadowoffset;\r
+    xfont->shadowalways = shadowalways;\r
+\r
+    for (i = 1; i < lenof(xfont->fonts); i++) {\r
+       xfont->fonts[i] = NULL;\r
+       xfont->allocated[i] = FALSE;\r
+    }\r
+\r
+    return (unifont *)xfont;\r
+}\r
+\r
+static void x11font_destroy(unifont *font)\r
+{\r
+    struct x11font *xfont = (struct x11font *)font;\r
+    int i;\r
+\r
+    for (i = 0; i < lenof(xfont->fonts); i++)\r
+       if (xfont->fonts[i])\r
+           gdk_font_unref(xfont->fonts[i]);\r
+    sfree(font);\r
+}\r
+\r
+static void x11_alloc_subfont(struct x11font *xfont, int sfid)\r
+{\r
+    char *derived_name = x11_guess_derived_font_name\r
+       (xfont->fonts[0], sfid & 1, !!(sfid & 2));\r
+    xfont->fonts[sfid] = gdk_font_load(derived_name);   /* may be NULL */\r
+    xfont->allocated[sfid] = TRUE;\r
+    sfree(derived_name);\r
+}\r
+\r
+static void x11font_really_draw_text(GdkDrawable *target, GdkFont *font,\r
+                                    GdkGC *gc, int x, int y,\r
+                                    const gchar *string, int clen, int nchars,\r
+                                    int shadowbold, int shadowoffset,\r
+                                    int fontvariable, int cellwidth)\r
+{\r
+    int step = clen * nchars, nsteps = 1, centre = FALSE;\r
+\r
+    if (fontvariable) {\r
+       /*\r
+        * In a variable-pitch font, we draw one character at a\r
+        * time, and centre it in the character cell.\r
+        */\r
+       step = clen;\r
+       nsteps = nchars;\r
+       centre = TRUE;\r
+    }\r
+\r
+    while (nsteps-- > 0) {\r
+       int X = x;\r
+       if (centre)\r
+           X += (cellwidth - gdk_text_width(font, string, step)) / 2;\r
+\r
+       gdk_draw_text(target, font, gc, X, y, string, step);\r
+       if (shadowbold)\r
+           gdk_draw_text(target, font, gc, X + shadowoffset, y, string, step);\r
+\r
+       x += cellwidth;\r
+       string += step;\r
+    }\r
+}\r
+\r
+static void x11font_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                             int x, int y, const char *string, int len,\r
+                             int wide, int bold, int cellwidth)\r
+{\r
+    struct x11font *xfont = (struct x11font *)font;\r
+    int sfid;\r
+    int shadowbold = FALSE;\r
+    int mult = (wide ? 2 : 1);\r
+\r
+    wide -= xfont->wide;\r
+    bold -= xfont->bold;\r
+\r
+    /*\r
+     * Decide which subfont we're using, and whether we have to\r
+     * use shadow bold.\r
+     */\r
+    if (xfont->shadowalways && bold) {\r
+       shadowbold = TRUE;\r
+       bold = 0;\r
+    }\r
+    sfid = 2 * wide + bold;\r
+    if (!xfont->allocated[sfid])\r
+       x11_alloc_subfont(xfont, sfid);\r
+    if (bold && !xfont->fonts[sfid]) {\r
+       bold = 0;\r
+       shadowbold = TRUE;\r
+       sfid = 2 * wide + bold;\r
+       if (!xfont->allocated[sfid])\r
+           x11_alloc_subfont(xfont, sfid);\r
+    }\r
+\r
+    if (!xfont->fonts[sfid])\r
+       return;                        /* we've tried our best, but no luck */\r
+\r
+    if (xfont->sixteen_bit) {\r
+       /*\r
+        * This X font has 16-bit character indices, which means\r
+        * we expect our string to have been passed in UTF-8.\r
+        */\r
+       XChar2b *xcs;\r
+       wchar_t *wcs;\r
+       int nchars, maxchars, i;\r
+\r
+       /*\r
+        * Convert the input string to wide-character Unicode.\r
+        */\r
+       maxchars = 0;\r
+       for (i = 0; i < len; i++)\r
+           if ((unsigned char)string[i] <= 0x7F ||\r
+               (unsigned char)string[i] >= 0xC0)\r
+               maxchars++;\r
+       wcs = snewn(maxchars+1, wchar_t);\r
+       nchars = charset_to_unicode((char **)&string, &len, wcs, maxchars,\r
+                                   CS_UTF8, NULL, NULL, 0);\r
+       assert(nchars <= maxchars);\r
+       wcs[nchars] = L'\0';\r
+\r
+       xcs = snewn(nchars, XChar2b);\r
+       for (i = 0; i < nchars; i++) {\r
+           xcs[i].byte1 = wcs[i] >> 8;\r
+           xcs[i].byte2 = wcs[i];\r
+       }\r
+\r
+       x11font_really_draw_text(target, xfont->fonts[sfid], gc, x, y,\r
+                                (gchar *)xcs, 2, nchars,\r
+                                shadowbold, xfont->shadowoffset,\r
+                                xfont->variable, cellwidth * mult);\r
+       sfree(xcs);\r
+       sfree(wcs);\r
+    } else {\r
+       x11font_really_draw_text(target, xfont->fonts[sfid], gc, x, y,\r
+                                string, 1, len,\r
+                                shadowbold, xfont->shadowoffset,\r
+                                xfont->variable, cellwidth * mult);\r
+    }\r
+}\r
+\r
+static void x11font_enum_fonts(GtkWidget *widget,\r
+                              fontsel_add_entry callback, void *callback_ctx)\r
+{\r
+    char **fontnames;\r
+    char *tmp = NULL;\r
+    int nnames, i, max, tmpsize;\r
+\r
+    max = 32768;\r
+    while (1) {\r
+       fontnames = XListFonts(GDK_DISPLAY(), "*", max, &nnames);\r
+       if (nnames >= max) {\r
+           XFreeFontNames(fontnames);\r
+           max *= 2;\r
+       } else\r
+           break;\r
+    }\r
+\r
+    tmpsize = 0;\r
+\r
+    for (i = 0; i < nnames; i++) {\r
+       if (fontnames[i][0] == '-') {\r
+           /*\r
+            * Dismember an XLFD and convert it into the format\r
+            * we'll be using in the font selector.\r
+            */\r
+           char *components[14];\r
+           char *p, *font, *style, *stylekey, *charset;\r
+           int j, weightkey, slantkey, setwidthkey;\r
+           int thistmpsize, fontsize, flags;\r
+\r
+           thistmpsize = 4 * strlen(fontnames[i]) + 256;\r
+           if (tmpsize < thistmpsize) {\r
+               tmpsize = thistmpsize;\r
+               tmp = sresize(tmp, tmpsize, char);\r
+           }\r
+           strcpy(tmp, fontnames[i]);\r
+\r
+           p = tmp;\r
+           for (j = 0; j < 14; j++) {\r
+               if (*p)\r
+                   *p++ = '\0';\r
+               components[j] = p;\r
+               while (*p && *p != '-')\r
+                   p++;\r
+           }\r
+           *p++ = '\0';\r
+\r
+           /*\r
+            * Font name is made up of fields 0 and 1, in reverse\r
+            * order with parentheses. (This is what the GTK 1.2 X\r
+            * font selector does, and it seems to come out\r
+            * looking reasonably sensible.)\r
+            */\r
+           font = p;\r
+           p += 1 + sprintf(p, "%s (%s)", components[1], components[0]);\r
+\r
+           /*\r
+            * Charset is made up of fields 12 and 13.\r
+            */\r
+           charset = p;\r
+           p += 1 + sprintf(p, "%s-%s", components[12], components[13]);\r
+\r
+           /*\r
+            * Style is a mixture of quite a lot of the fields,\r
+            * with some strange formatting.\r
+            */\r
+           style = p;\r
+           p += sprintf(p, "%s", components[2][0] ? components[2] :\r
+                        "regular");\r
+           if (!g_strcasecmp(components[3], "i"))\r
+               p += sprintf(p, " italic");\r
+           else if (!g_strcasecmp(components[3], "o"))\r
+               p += sprintf(p, " oblique");\r
+           else if (!g_strcasecmp(components[3], "ri"))\r
+               p += sprintf(p, " reverse italic");\r
+           else if (!g_strcasecmp(components[3], "ro"))\r
+               p += sprintf(p, " reverse oblique");\r
+           else if (!g_strcasecmp(components[3], "ot"))\r
+               p += sprintf(p, " other-slant");\r
+           if (components[4][0] && g_strcasecmp(components[4], "normal"))\r
+               p += sprintf(p, " %s", components[4]);\r
+           if (!g_strcasecmp(components[10], "m"))\r
+               p += sprintf(p, " [M]");\r
+           if (!g_strcasecmp(components[10], "c"))\r
+               p += sprintf(p, " [C]");\r
+           if (components[5][0])\r
+               p += sprintf(p, " %s", components[5]);\r
+\r
+           /*\r
+            * Style key is the same stuff as above, but with a\r
+            * couple of transformations done on it to make it\r
+            * sort more sensibly.\r
+            */\r
+           p++;\r
+           stylekey = p;\r
+           if (!g_strcasecmp(components[2], "medium") ||\r
+               !g_strcasecmp(components[2], "regular") ||\r
+               !g_strcasecmp(components[2], "normal") ||\r
+               !g_strcasecmp(components[2], "book"))\r
+               weightkey = 0;\r
+           else if (!g_strncasecmp(components[2], "demi", 4) ||\r
+                    !g_strncasecmp(components[2], "semi", 4))\r
+               weightkey = 1;\r
+           else\r
+               weightkey = 2;\r
+           if (!g_strcasecmp(components[3], "r"))\r
+               slantkey = 0;\r
+           else if (!g_strncasecmp(components[3], "r", 1))\r
+               slantkey = 2;\r
+           else\r
+               slantkey = 1;\r
+           if (!g_strcasecmp(components[4], "normal"))\r
+               setwidthkey = 0;\r
+           else\r
+               setwidthkey = 1;\r
+\r
+           p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",\r
+                        weightkey,\r
+                        (int)strlen(components[2]), components[2],\r
+                        slantkey,\r
+                        (int)strlen(components[3]), components[3],\r
+                        setwidthkey,\r
+                        (int)strlen(components[4]), components[4],\r
+                        (int)strlen(components[10]), components[10],\r
+                        (int)strlen(components[5]), components[5]);\r
+\r
+           assert(p - tmp < thistmpsize);\r
+\r
+           /*\r
+            * Size is in pixels, for our application, so we\r
+            * derive it directly from the pixel size field,\r
+            * number 6.\r
+            */\r
+           fontsize = atoi(components[6]);\r
+\r
+           /*\r
+            * Flags: we need to know whether this is a monospaced\r
+            * font, which we do by examining the spacing field\r
+            * again.\r
+            */\r
+           flags = FONTFLAG_SERVERSIDE;\r
+           if (!strchr("CcMm", components[10][0]))\r
+               flags |= FONTFLAG_NONMONOSPACED;\r
+\r
+           /*\r
+            * Not sure why, but sometimes the X server will\r
+            * deliver dummy font types in which fontsize comes\r
+            * out as zero. Filter those out.\r
+            */\r
+           if (fontsize)\r
+               callback(callback_ctx, fontnames[i], font, charset,\r
+                        style, stylekey, fontsize, flags, &x11font_vtable);\r
+       } else {\r
+           /*\r
+            * This isn't an XLFD, so it must be an alias.\r
+            * Transmit it with mostly null data.\r
+            * \r
+            * It would be nice to work out if it's monospaced\r
+            * here, but at the moment I can't see that being\r
+            * anything but computationally hideous. Ah well.\r
+            */\r
+           callback(callback_ctx, fontnames[i], fontnames[i], NULL,\r
+                    NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);\r
+       }\r
+    }\r
+    XFreeFontNames(fontnames);\r
+}\r
+\r
+static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,\r
+                                      int *size, int *flags,\r
+                                      int resolve_aliases)\r
+{\r
+    /*\r
+     * When given an X11 font name to try to make sense of for a\r
+     * font selector, we must attempt to load it (to see if it\r
+     * exists), and then canonify it by extracting its FONT\r
+     * property, which should give its full XLFD even if what we\r
+     * originally had was a wildcard.\r
+     * \r
+     * However, we must carefully avoid canonifying font\r
+     * _aliases_, unless specifically asked to, because the font\r
+     * selector treats them as worthwhile in their own right.\r
+     */\r
+    GdkFont *font = gdk_font_load(name);\r
+    XFontStruct *xfs;\r
+    Display *disp;\r
+    Atom fontprop, fontprop2;\r
+    unsigned long ret;\r
+\r
+    if (!font)\r
+       return NULL;                   /* didn't make sense to us, sorry */\r
+\r
+    gdk_font_ref(font);\r
+\r
+    xfs = GDK_FONT_XFONT(font);\r
+    disp = GDK_FONT_XDISPLAY(font);\r
+    fontprop = XInternAtom(disp, "FONT", False);\r
+\r
+    if (XGetFontProperty(xfs, fontprop, &ret)) {\r
+       char *newname = XGetAtomName(disp, (Atom)ret);\r
+       if (newname) {\r
+           unsigned long fsize = 12;\r
+\r
+           fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);\r
+           if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {\r
+               *size = fsize;\r
+               gdk_font_unref(font);\r
+               if (flags) {\r
+                   if (name[0] == '-' || resolve_aliases)\r
+                       *flags = FONTFLAG_SERVERSIDE;\r
+                   else\r
+                       *flags = FONTFLAG_SERVERALIAS;\r
+               }\r
+               return dupstr(name[0] == '-' || resolve_aliases ?\r
+                             newname : name);\r
+           }\r
+       }\r
+    }\r
+\r
+    gdk_font_unref(font);\r
+    return NULL;                      /* something went wrong */\r
+}\r
+\r
+static char *x11font_scale_fontname(GtkWidget *widget, const char *name,\r
+                                   int size)\r
+{\r
+    return NULL;                      /* shan't */\r
+}\r
+\r
+#if GTK_CHECK_VERSION(2,0,0)\r
+\r
+/* ----------------------------------------------------------------------\r
+ * Pango font implementation (for GTK 2 only).\r
+ */\r
+\r
+#if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6\r
+#define PANGO_PRE_1POINT6             /* make life easier for pre-1.4 folk */\r
+#endif\r
+\r
+static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                               int x, int y, const char *string, int len,\r
+                               int wide, int bold, int cellwidth);\r
+static unifont *pangofont_create(GtkWidget *widget, const char *name,\r
+                                int wide, int bold,\r
+                                int shadowoffset, int shadowalways);\r
+static void pangofont_destroy(unifont *font);\r
+static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,\r
+                                void *callback_ctx);\r
+static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,\r
+                                        int *size, int *flags,\r
+                                        int resolve_aliases);\r
+static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,\r
+                                     int size);\r
+\r
+struct pangofont {\r
+    struct unifont u;\r
+    /*\r
+     * Pango objects.\r
+     */\r
+    PangoFontDescription *desc;\r
+    PangoFontset *fset;\r
+    /*\r
+     * The containing widget.\r
+     */\r
+    GtkWidget *widget;\r
+    /*\r
+     * Data passed in to unifont_create().\r
+     */\r
+    int bold, shadowoffset, shadowalways;\r
+};\r
+\r
+static const struct unifont_vtable pangofont_vtable = {\r
+    pangofont_create,\r
+    pangofont_destroy,\r
+    pangofont_draw_text,\r
+    pangofont_enum_fonts,\r
+    pangofont_canonify_fontname,\r
+    pangofont_scale_fontname,\r
+    "client",\r
+};\r
+\r
+/*\r
+ * This function is used to rigorously validate a\r
+ * PangoFontDescription. Later versions of Pango have a nasty\r
+ * habit of accepting _any_ old string as input to\r
+ * pango_font_description_from_string and returning a font\r
+ * description which can actually be used to display text, even if\r
+ * they have to do it by falling back to their most default font.\r
+ * This is doubtless helpful in some situations, but not here,\r
+ * because we need to know if a Pango font string actually _makes\r
+ * sense_ in order to fall back to treating it as an X font name\r
+ * if it doesn't. So we check that the font family is actually one\r
+ * supported by Pango.\r
+ */\r
+static int pangofont_check_desc_makes_sense(PangoContext *ctx,\r
+                                           PangoFontDescription *desc)\r
+{\r
+#ifndef PANGO_PRE_1POINT6\r
+    PangoFontMap *map;\r
+#endif\r
+    PangoFontFamily **families;\r
+    int i, nfamilies, matched;\r
+\r
+    /*\r
+     * Ask Pango for a list of font families, and iterate through\r
+     * them to see if one of them matches the family in the\r
+     * PangoFontDescription.\r
+     */\r
+#ifndef PANGO_PRE_1POINT6\r
+    map = pango_context_get_font_map(ctx);\r
+    if (!map)\r
+       return FALSE;\r
+    pango_font_map_list_families(map, &families, &nfamilies);\r
+#else\r
+    pango_context_list_families(ctx, &families, &nfamilies);\r
+#endif\r
+\r
+    matched = FALSE;\r
+    for (i = 0; i < nfamilies; i++) {\r
+       if (!g_strcasecmp(pango_font_family_get_name(families[i]),\r
+                         pango_font_description_get_family(desc))) {\r
+           matched = TRUE;\r
+           break;\r
+       }\r
+    }\r
+    g_free(families);\r
+\r
+    return matched;\r
+}\r
+\r
+static unifont *pangofont_create(GtkWidget *widget, const char *name,\r
+                                int wide, int bold,\r
+                                int shadowoffset, int shadowalways)\r
+{\r
+    struct pangofont *pfont;\r
+    PangoContext *ctx;\r
+#ifndef PANGO_PRE_1POINT6\r
+    PangoFontMap *map;\r
+#endif\r
+    PangoFontDescription *desc;\r
+    PangoFontset *fset;\r
+    PangoFontMetrics *metrics;\r
+\r
+    desc = pango_font_description_from_string(name);\r
+    if (!desc)\r
+       return NULL;\r
+    ctx = gtk_widget_get_pango_context(widget);\r
+    if (!ctx) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    if (!pangofont_check_desc_makes_sense(ctx, desc)) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+#ifndef PANGO_PRE_1POINT6\r
+    map = pango_context_get_font_map(ctx);\r
+    if (!map) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    fset = pango_font_map_load_fontset(map, ctx, desc,\r
+                                      pango_context_get_language(ctx));\r
+#else\r
+    fset = pango_context_load_fontset(ctx, desc,\r
+                                      pango_context_get_language(ctx));\r
+#endif\r
+    if (!fset) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    metrics = pango_fontset_get_metrics(fset);\r
+    if (!metrics ||\r
+       pango_font_metrics_get_approximate_digit_width(metrics) == 0) {\r
+       pango_font_description_free(desc);\r
+       g_object_unref(fset);\r
+       return NULL;\r
+    }\r
+\r
+    pfont = snew(struct pangofont);\r
+    pfont->u.vt = &pangofont_vtable;\r
+    pfont->u.width =\r
+       PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));\r
+    pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));\r
+    pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics));\r
+    pfont->u.height = pfont->u.ascent + pfont->u.descent;\r
+    /* The Pango API is hardwired to UTF-8 */\r
+    pfont->u.public_charset = CS_UTF8;\r
+    pfont->u.real_charset = CS_UTF8;\r
+    pfont->desc = desc;\r
+    pfont->fset = fset;\r
+    pfont->widget = widget;\r
+    pfont->bold = bold;\r
+    pfont->shadowoffset = shadowoffset;\r
+    pfont->shadowalways = shadowalways;\r
+\r
+    pango_font_metrics_unref(metrics);\r
+\r
+    return (unifont *)pfont;\r
+}\r
+\r
+static void pangofont_destroy(unifont *font)\r
+{\r
+    struct pangofont *pfont = (struct pangofont *)font;\r
+    pango_font_description_free(pfont->desc);\r
+    g_object_unref(pfont->fset);\r
+    sfree(font);\r
+}\r
+\r
+static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                               int x, int y, const char *string, int len,\r
+                               int wide, int bold, int cellwidth)\r
+{\r
+    struct pangofont *pfont = (struct pangofont *)font;\r
+    PangoLayout *layout;\r
+    PangoRectangle rect;\r
+    int shadowbold = FALSE;\r
+\r
+    if (wide)\r
+       cellwidth *= 2;\r
+\r
+    y -= pfont->u.ascent;\r
+\r
+    layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));\r
+    pango_layout_set_font_description(layout, pfont->desc);\r
+    if (bold > pfont->bold) {\r
+       if (pfont->shadowalways)\r
+           shadowbold = TRUE;\r
+       else {\r
+           PangoFontDescription *desc2 =\r
+               pango_font_description_copy_static(pfont->desc);\r
+           pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);\r
+           pango_layout_set_font_description(layout, desc2);\r
+       }\r
+    }\r
+\r
+    while (len > 0) {\r
+       int clen, n;\r
+\r
+       /*\r
+        * We want to display every character from this string in\r
+        * the centre of its own character cell. In the worst case,\r
+        * this requires a separate text-drawing call for each\r
+        * character; but in the common case where the font is\r
+        * properly fixed-width, we can draw many characters in one\r
+        * go which is much faster.\r
+        *\r
+        * This still isn't really ideal. If you look at what\r
+        * happens in the X protocol as a result of all of this, you\r
+        * find - naturally enough - that each call to\r
+        * gdk_draw_layout() generates a separate set of X RENDER\r
+        * operations involving creating a picture, setting a clip\r
+        * rectangle, doing some drawing and undoing the whole lot.\r
+        * In an ideal world, we should _always_ be able to turn the\r
+        * contents of this loop into a single RenderCompositeGlyphs\r
+        * operation which internally specifies inter-character\r
+        * deltas to get the spacing right, which would give us full\r
+        * speed _even_ in the worst case of a non-fixed-width font.\r
+        * However, Pango's architecture and documentation are so\r
+        * unhelpful that I have no idea how if at all to persuade\r
+        * them to do that.\r
+        */\r
+\r
+       /*\r
+        * Start by extracting a single UTF-8 character from the\r
+        * string.\r
+        */\r
+       clen = 1;\r
+       while (clen < len &&\r
+              (unsigned char)string[clen] >= 0x80 &&\r
+              (unsigned char)string[clen] < 0xC0)\r
+           clen++;\r
+       n = 1;\r
+\r
+       /*\r
+        * See if that character has the width we expect.\r
+        */\r
+       pango_layout_set_text(layout, string, clen);\r
+       pango_layout_get_pixel_extents(layout, NULL, &rect);\r
+\r
+       if (rect.width == cellwidth) {\r
+           /*\r
+            * Try extracting more characters, for as long as they\r
+            * stay well-behaved.\r
+            */\r
+           while (clen < len) {\r
+               int oldclen = clen;\r
+               clen++;                /* skip UTF-8 introducer byte */\r
+               while (clen < len &&\r
+                      (unsigned char)string[clen] >= 0x80 &&\r
+                      (unsigned char)string[clen] < 0xC0)\r
+                   clen++;\r
+               n++;\r
+               pango_layout_set_text(layout, string, clen);\r
+               pango_layout_get_pixel_extents(layout, NULL, &rect);\r
+               if (rect.width != n * cellwidth) {\r
+                   clen = oldclen;\r
+                   n--;\r
+                   break;\r
+               }\r
+           }\r
+       }\r
+\r
+       pango_layout_set_text(layout, string, clen);\r
+       pango_layout_get_pixel_extents(layout, NULL, &rect);\r
+       gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2,\r
+                       y + (pfont->u.height - rect.height)/2, layout);\r
+       if (shadowbold)\r
+           gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,\r
+                           y + (pfont->u.height - rect.height)/2, layout);\r
+\r
+       len -= clen;\r
+       string += clen;\r
+       x += n * cellwidth;\r
+    }\r
+\r
+    g_object_unref(layout);\r
+}\r
+\r
+/*\r
+ * Dummy size value to be used when converting a\r
+ * PangoFontDescription of a scalable font to a string for\r
+ * internal use.\r
+ */\r
+#define PANGO_DUMMY_SIZE 12\r
+\r
+static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,\r
+                                void *callback_ctx)\r
+{\r
+    PangoContext *ctx;\r
+#ifndef PANGO_PRE_1POINT6\r
+    PangoFontMap *map;\r
+#endif\r
+    PangoFontFamily **families;\r
+    int i, nfamilies;\r
+\r
+    ctx = gtk_widget_get_pango_context(widget);\r
+    if (!ctx)\r
+       return;\r
+\r
+    /*\r
+     * Ask Pango for a list of font families, and iterate through\r
+     * them.\r
+     */\r
+#ifndef PANGO_PRE_1POINT6\r
+    map = pango_context_get_font_map(ctx);\r
+    if (!map)\r
+       return;\r
+    pango_font_map_list_families(map, &families, &nfamilies);\r
+#else\r
+    pango_context_list_families(ctx, &families, &nfamilies);\r
+#endif\r
+    for (i = 0; i < nfamilies; i++) {\r
+       PangoFontFamily *family = families[i];\r
+       const char *familyname;\r
+       int flags;\r
+       PangoFontFace **faces;\r
+       int j, nfaces;\r
+\r
+       /*\r
+        * Set up our flags for this font family, and get the name\r
+        * string.\r
+        */\r
+       flags = FONTFLAG_CLIENTSIDE;\r
+#ifndef PANGO_PRE_1POINT4\r
+        /*\r
+         * In very early versions of Pango, we can't tell\r
+         * monospaced fonts from non-monospaced.\r
+         */\r
+       if (!pango_font_family_is_monospace(family))\r
+           flags |= FONTFLAG_NONMONOSPACED;\r
+#endif\r
+       familyname = pango_font_family_get_name(family);\r
+\r
+       /*\r
+        * Go through the available font faces in this family.\r
+        */\r
+       pango_font_family_list_faces(family, &faces, &nfaces);\r
+       for (j = 0; j < nfaces; j++) {\r
+           PangoFontFace *face = faces[j];\r
+           PangoFontDescription *desc;\r
+           const char *facename;\r
+           int *sizes;\r
+           int k, nsizes, dummysize;\r
+\r
+           /*\r
+            * Get the face name string.\r
+            */\r
+           facename = pango_font_face_get_face_name(face);\r
+\r
+           /*\r
+            * Set up a font description with what we've got so\r
+            * far. We'll fill in the size field manually and then\r
+            * call pango_font_description_to_string() to give the\r
+            * full real name of the specific font.\r
+            */\r
+           desc = pango_font_face_describe(face);\r
+\r
+           /*\r
+            * See if this font has a list of specific sizes.\r
+            */\r
+#ifndef PANGO_PRE_1POINT4\r
+           pango_font_face_list_sizes(face, &sizes, &nsizes);\r
+#else\r
+            /*\r
+             * In early versions of Pango, that call wasn't\r
+             * supported; we just have to assume everything is\r
+             * scalable.\r
+             */\r
+            sizes = NULL;\r
+#endif\r
+           if (!sizes) {\r
+               /*\r
+                * Write a single entry with a dummy size.\r
+                */\r
+               dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;\r
+               sizes = &dummysize;\r
+               nsizes = 1;\r
+           }\r
+\r
+           /*\r
+            * If so, go through them one by one.\r
+            */\r
+           for (k = 0; k < nsizes; k++) {\r
+               char *fullname;\r
+               char stylekey[128];\r
+\r
+               pango_font_description_set_size(desc, sizes[k]);\r
+\r
+               fullname = pango_font_description_to_string(desc);\r
+\r
+               /*\r
+                * Construct the sorting key for font styles.\r
+                */\r
+               {\r
+                   char *p = stylekey;\r
+                   int n;\r
+\r
+                   n = pango_font_description_get_weight(desc);\r
+                   /* Weight: normal, then lighter, then bolder */\r
+                   if (n <= PANGO_WEIGHT_NORMAL)\r
+                       n = PANGO_WEIGHT_NORMAL - n;\r
+                   p += sprintf(p, "%4d", n);\r
+\r
+                   n = pango_font_description_get_style(desc);\r
+                   p += sprintf(p, " %2d", n);\r
+\r
+                   n = pango_font_description_get_stretch(desc);\r
+                   /* Stretch: closer to normal sorts earlier */\r
+                   n = 2 * abs(PANGO_STRETCH_NORMAL - n) +\r
+                       (n < PANGO_STRETCH_NORMAL);\r
+                   p += sprintf(p, " %2d", n);\r
+\r
+                   n = pango_font_description_get_variant(desc);\r
+                   p += sprintf(p, " %2d", n);\r
+                   \r
+               }\r
+\r
+               /*\r
+                * Got everything. Hand off to the callback.\r
+                * (The charset string is NULL, because only\r
+                * server-side X fonts use it.)\r
+                */\r
+               callback(callback_ctx, fullname, familyname, NULL, facename,\r
+                        stylekey,\r
+                        (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),\r
+                        flags, &pangofont_vtable);\r
+\r
+               g_free(fullname);\r
+           }\r
+           if (sizes != &dummysize)\r
+               g_free(sizes);\r
+\r
+           pango_font_description_free(desc);\r
+       }\r
+       g_free(faces);\r
+    }\r
+    g_free(families);\r
+}\r
+\r
+static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,\r
+                                        int *size, int *flags,\r
+                                        int resolve_aliases)\r
+{\r
+    /*\r
+     * When given a Pango font name to try to make sense of for a\r
+     * font selector, we must normalise it to PANGO_DUMMY_SIZE and\r
+     * extract its original size (in pixels) into the `size' field.\r
+     */\r
+    PangoContext *ctx;\r
+#ifndef PANGO_PRE_1POINT6\r
+    PangoFontMap *map;\r
+#endif\r
+    PangoFontDescription *desc;\r
+    PangoFontset *fset;\r
+    PangoFontMetrics *metrics;\r
+    char *newname, *retname;\r
+\r
+    desc = pango_font_description_from_string(name);\r
+    if (!desc)\r
+       return NULL;\r
+    ctx = gtk_widget_get_pango_context(widget);\r
+    if (!ctx) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    if (!pangofont_check_desc_makes_sense(ctx, desc)) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+#ifndef PANGO_PRE_1POINT6\r
+    map = pango_context_get_font_map(ctx);\r
+    if (!map) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    fset = pango_font_map_load_fontset(map, ctx, desc,\r
+                                      pango_context_get_language(ctx));\r
+#else\r
+    fset = pango_context_load_fontset(ctx, desc,\r
+                                      pango_context_get_language(ctx));\r
+#endif\r
+    if (!fset) {\r
+       pango_font_description_free(desc);\r
+       return NULL;\r
+    }\r
+    metrics = pango_fontset_get_metrics(fset);\r
+    if (!metrics ||\r
+       pango_font_metrics_get_approximate_digit_width(metrics) == 0) {\r
+       pango_font_description_free(desc);\r
+       g_object_unref(fset);\r
+       return NULL;\r
+    }\r
+\r
+    *size = PANGO_PIXELS(pango_font_description_get_size(desc));\r
+    *flags = FONTFLAG_CLIENTSIDE;\r
+    pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);\r
+    newname = pango_font_description_to_string(desc);\r
+    retname = dupstr(newname);\r
+    g_free(newname);\r
+\r
+    pango_font_metrics_unref(metrics);\r
+    pango_font_description_free(desc);\r
+    g_object_unref(fset);\r
+\r
+    return retname;\r
+}\r
+\r
+static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,\r
+                                     int size)\r
+{\r
+    PangoFontDescription *desc;\r
+    char *newname, *retname;\r
+\r
+    desc = pango_font_description_from_string(name);\r
+    if (!desc)\r
+       return NULL;\r
+    pango_font_description_set_size(desc, size * PANGO_SCALE);\r
+    newname = pango_font_description_to_string(desc);\r
+    retname = dupstr(newname);\r
+    g_free(newname);\r
+    pango_font_description_free(desc);\r
+\r
+    return retname;\r
+}\r
+\r
+#endif /* GTK_CHECK_VERSION(2,0,0) */\r
+\r
+/* ----------------------------------------------------------------------\r
+ * Outermost functions which do the vtable dispatch.\r
+ */\r
+\r
+/*\r
+ * Complete list of font-type subclasses. Listed in preference\r
+ * order for unifont_create(). (That is, in the extremely unlikely\r
+ * event that the same font name is valid as both a Pango and an\r
+ * X11 font, it will be interpreted as the former in the absence\r
+ * of an explicit type-disambiguating prefix.)\r
+ */\r
+static const struct unifont_vtable *unifont_types[] = {\r
+#if GTK_CHECK_VERSION(2,0,0)\r
+    &pangofont_vtable,\r
+#endif\r
+    &x11font_vtable,\r
+};\r
+\r
+/*\r
+ * Function which takes a font name and processes the optional\r
+ * scheme prefix. Returns the tail of the font name suitable for\r
+ * passing to individual font scheme functions, and also provides\r
+ * a subrange of the unifont_types[] array above.\r
+ * \r
+ * The return values `start' and `end' denote a half-open interval\r
+ * in unifont_types[]; that is, the correct way to iterate over\r
+ * them is\r
+ * \r
+ *   for (i = start; i < end; i++) {...}\r
+ */\r
+static const char *unifont_do_prefix(const char *name, int *start, int *end)\r
+{\r
+    int colonpos = strcspn(name, ":");\r
+    int i;\r
+\r
+    if (name[colonpos]) {\r
+       /*\r
+        * There's a colon prefix on the font name. Use it to work\r
+        * out which subclass to use.\r
+        */\r
+       for (i = 0; i < lenof(unifont_types); i++) {\r
+           if (strlen(unifont_types[i]->prefix) == colonpos &&\r
+               !strncmp(unifont_types[i]->prefix, name, colonpos)) {\r
+               *start = i;\r
+               *end = i+1;\r
+               return name + colonpos + 1;\r
+           }\r
+       }\r
+       /*\r
+        * None matched, so return an empty scheme list to prevent\r
+        * any scheme from being called at all.\r
+        */\r
+       *start = *end = 0;\r
+       return name + colonpos + 1;\r
+    } else {\r
+       /*\r
+        * No colon prefix, so just use all the subclasses.\r
+        */\r
+       *start = 0;\r
+       *end = lenof(unifont_types);\r
+       return name;\r
+    }\r
+}\r
+\r
+unifont *unifont_create(GtkWidget *widget, const char *name, int wide,\r
+                       int bold, int shadowoffset, int shadowalways)\r
+{\r
+    int i, start, end;\r
+\r
+    name = unifont_do_prefix(name, &start, &end);\r
+\r
+    for (i = start; i < end; i++) {\r
+       unifont *ret = unifont_types[i]->create(widget, name, wide, bold,\r
+                                               shadowoffset, shadowalways);\r
+       if (ret)\r
+           return ret;\r
+    }\r
+    return NULL;                      /* font not found in any scheme */\r
+}\r
+\r
+void unifont_destroy(unifont *font)\r
+{\r
+    font->vt->destroy(font);\r
+}\r
+\r
+void unifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,\r
+                      int x, int y, const char *string, int len,\r
+                      int wide, int bold, int cellwidth)\r
+{\r
+    font->vt->draw_text(target, gc, font, x, y, string, len,\r
+                       wide, bold, cellwidth);\r
+}\r
+\r
+#if GTK_CHECK_VERSION(2,0,0)\r
+\r
+/* ----------------------------------------------------------------------\r
+ * Implementation of a unified font selector. Used on GTK 2 only;\r
+ * for GTK 1 we still use the standard font selector.\r
+ */\r
+\r
+typedef struct fontinfo fontinfo;\r
+\r
+typedef struct unifontsel_internal {\r
+    /* This must be the structure's first element, for cross-casting */\r
+    unifontsel u;\r
+    GtkListStore *family_model, *style_model, *size_model;\r
+    GtkWidget *family_list, *style_list, *size_entry, *size_list;\r
+    GtkWidget *filter_buttons[4];\r
+    GtkWidget *preview_area;\r
+    GdkPixmap *preview_pixmap;\r
+    int preview_width, preview_height;\r
+    GdkColor preview_fg, preview_bg;\r
+    int filter_flags;\r
+    tree234 *fonts_by_realname, *fonts_by_selorder;\r
+    fontinfo *selected;\r
+    int selsize, intendedsize;\r
+    int inhibit_response;  /* inhibit callbacks when we change GUI controls */\r
+} unifontsel_internal;\r
+\r
+/*\r
+ * The structure held in the tree234s. All the string members are\r
+ * part of the same allocated area, so don't need freeing\r
+ * separately.\r
+ */\r
+struct fontinfo {\r
+    char *realname;\r
+    char *family, *charset, *style, *stylekey;\r
+    int size, flags;\r
+    /*\r
+     * Fallback sorting key, to permit multiple identical entries\r
+     * to exist in the selorder tree.\r
+     */\r
+    int index;\r
+    /*\r
+     * Indices mapping fontinfo structures to indices in the list\r
+     * boxes. sizeindex is irrelevant if the font is scalable\r
+     * (size==0).\r
+     */\r
+    int familyindex, styleindex, sizeindex;\r
+    /*\r
+     * The class of font.\r
+     */\r
+    const struct unifont_vtable *fontclass;\r
+};\r
+\r
+struct fontinfo_realname_find {\r
+    const char *realname;\r
+    int flags;\r
+};\r
+\r
+static int strnullcasecmp(const char *a, const char *b)\r
+{\r
+    int i;\r
+\r
+    /*\r
+     * If exactly one of the inputs is NULL, it compares before\r
+     * the other one.\r
+     */\r
+    if ((i = (!b) - (!a)) != 0)\r
+       return i;\r
+\r
+    /*\r
+     * NULL compares equal.\r
+     */\r
+    if (!a)\r
+       return 0;\r
+\r
+    /*\r
+     * Otherwise, ordinary strcasecmp.\r
+     */\r
+    return g_strcasecmp(a, b);\r
+}\r
+\r
+static int fontinfo_realname_compare(void *av, void *bv)\r
+{\r
+    fontinfo *a = (fontinfo *)av;\r
+    fontinfo *b = (fontinfo *)bv;\r
+    int i;\r
+\r
+    if ((i = strnullcasecmp(a->realname, b->realname)) != 0)\r
+       return i;\r
+    if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))\r
+       return ((a->flags & FONTFLAG_SORT_MASK) <\r
+               (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);\r
+    return 0;\r
+}\r
+\r
+static int fontinfo_realname_find(void *av, void *bv)\r
+{\r
+    struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;\r
+    fontinfo *b = (fontinfo *)bv;\r
+    int i;\r
+\r
+    if ((i = strnullcasecmp(a->realname, b->realname)) != 0)\r
+       return i;\r
+    if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))\r
+       return ((a->flags & FONTFLAG_SORT_MASK) <\r
+               (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);\r
+    return 0;\r
+}\r
+\r
+static int fontinfo_selorder_compare(void *av, void *bv)\r
+{\r
+    fontinfo *a = (fontinfo *)av;\r
+    fontinfo *b = (fontinfo *)bv;\r
+    int i;\r
+    if ((i = strnullcasecmp(a->family, b->family)) != 0)\r
+       return i;\r
+    /*\r
+     * Font class comes immediately after family, so that fonts\r
+     * from different classes with the same family\r
+     */\r
+    if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))\r
+       return ((a->flags & FONTFLAG_SORT_MASK) <\r
+               (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);\r
+    if ((i = strnullcasecmp(a->charset, b->charset)) != 0)\r
+       return i;\r
+    if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)\r
+       return i;\r
+    if ((i = strnullcasecmp(a->style, b->style)) != 0)\r
+       return i;\r
+    if (a->size != b->size)\r
+       return (a->size < b->size ? -1 : +1);\r
+    if (a->index != b->index)\r
+       return (a->index < b->index ? -1 : +1);\r
+    return 0;\r
+}\r
+\r
+static void unifontsel_deselect(unifontsel_internal *fs)\r
+{\r
+    fs->selected = NULL;\r
+    gtk_list_store_clear(fs->style_model);\r
+    gtk_list_store_clear(fs->size_model);\r
+    gtk_widget_set_sensitive(fs->u.ok_button, FALSE);\r
+    gtk_widget_set_sensitive(fs->size_entry, FALSE);\r
+}\r
+\r
+static void unifontsel_setup_familylist(unifontsel_internal *fs)\r
+{\r
+    GtkTreeIter iter;\r
+    int i, listindex, minpos = -1, maxpos = -1;\r
+    char *currfamily = NULL;\r
+    int currflags = -1;\r
+    fontinfo *info;\r
+\r
+    gtk_list_store_clear(fs->family_model);\r
+    listindex = 0;\r
+\r
+    /*\r
+     * Search through the font tree for anything matching our\r
+     * current filter criteria. When we find one, add its font\r
+     * name to the list box.\r
+     */\r
+    for (i = 0 ;; i++) {\r
+       info = (fontinfo *)index234(fs->fonts_by_selorder, i);\r
+       /*\r
+        * info may be NULL if we've just run off the end of the\r
+        * tree. We must still do a processing pass in that\r
+        * situation, in case we had an unfinished font record in\r
+        * progress.\r
+        */\r
+       if (info && (info->flags &~ fs->filter_flags)) {\r
+           info->familyindex = -1;\r
+           continue;                  /* we're filtering out this font */\r
+       }\r
+       if (!info || strnullcasecmp(currfamily, info->family) ||\r
+           currflags != (info->flags & FONTFLAG_SORT_MASK)) {\r
+           /*\r
+            * We've either finished a family, or started a new\r
+            * one, or both.\r
+            */\r
+           if (currfamily) {\r
+               gtk_list_store_append(fs->family_model, &iter);\r
+               gtk_list_store_set(fs->family_model, &iter,\r
+                                  0, currfamily, 1, minpos, 2, maxpos+1, -1);\r
+               listindex++;\r
+           }\r
+           if (info) {\r
+               minpos = i;\r
+               currfamily = info->family;\r
+               currflags = info->flags & FONTFLAG_SORT_MASK;\r
+           }\r
+       }\r
+       if (!info)\r
+           break;                     /* now we're done */\r
+       info->familyindex = listindex;\r
+       maxpos = i;\r
+    }\r
+\r
+    /*\r
+     * If we've just filtered out the previously selected font,\r
+     * deselect it thoroughly.\r
+     */\r
+    if (fs->selected && fs->selected->familyindex < 0)\r
+       unifontsel_deselect(fs);\r
+}\r
+\r
+static void unifontsel_setup_stylelist(unifontsel_internal *fs,\r
+                                      int start, int end)\r
+{\r
+    GtkTreeIter iter;\r
+    int i, listindex, minpos = -1, maxpos = -1, started = FALSE;\r
+    char *currcs = NULL, *currstyle = NULL;\r
+    fontinfo *info;\r
+\r
+    gtk_list_store_clear(fs->style_model);\r
+    listindex = 0;\r
+    started = FALSE;\r
+\r
+    /*\r
+     * Search through the font tree for anything matching our\r
+     * current filter criteria. When we find one, add its charset\r
+     * and/or style name to the list box.\r
+     */\r
+    for (i = start; i <= end; i++) {\r
+       if (i == end)\r
+           info = NULL;\r
+       else\r
+           info = (fontinfo *)index234(fs->fonts_by_selorder, i);\r
+       /*\r
+        * info may be NULL if we've just run off the end of the\r
+        * relevant data. We must still do a processing pass in\r
+        * that situation, in case we had an unfinished font\r
+        * record in progress.\r
+        */\r
+       if (info && (info->flags &~ fs->filter_flags)) {\r
+           info->styleindex = -1;\r
+           continue;                  /* we're filtering out this font */\r
+       }\r
+       if (!info || !started || strnullcasecmp(currcs, info->charset) ||\r
+            strnullcasecmp(currstyle, info->style)) {\r
+           /*\r
+            * We've either finished a style/charset, or started a\r
+            * new one, or both.\r
+            */\r
+           started = TRUE;\r
+           if (currstyle) {\r
+               gtk_list_store_append(fs->style_model, &iter);\r
+               gtk_list_store_set(fs->style_model, &iter,\r
+                                  0, currstyle, 1, minpos, 2, maxpos+1,\r
+                                  3, TRUE, -1);\r
+               listindex++;\r
+           }\r
+           if (info) {\r
+               minpos = i;\r
+               if (info->charset && strnullcasecmp(currcs, info->charset)) {\r
+                   gtk_list_store_append(fs->style_model, &iter);\r
+                   gtk_list_store_set(fs->style_model, &iter,\r
+                                      0, info->charset, 1, -1, 2, -1,\r
+                                      3, FALSE, -1);\r
+                   listindex++;\r
+               }\r
+               currcs = info->charset;\r
+               currstyle = info->style;\r
+           }\r
+       }\r
+       if (!info)\r
+           break;                     /* now we're done */\r
+       info->styleindex = listindex;\r
+       maxpos = i;\r
+    }\r
+}\r
+\r
+static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };\r
+\r
+static void unifontsel_setup_sizelist(unifontsel_internal *fs,\r
+                                     int start, int end)\r
+{\r
+    GtkTreeIter iter;\r
+    int i, listindex;\r
+    char sizetext[40];\r
+    fontinfo *info;\r
+\r
+    gtk_list_store_clear(fs->size_model);\r
+    listindex = 0;\r
+\r
+    /*\r
+     * Search through the font tree for anything matching our\r
+     * current filter criteria. When we find one, add its font\r
+     * name to the list box.\r
+     */\r
+    for (i = start; i < end; i++) {\r
+       info = (fontinfo *)index234(fs->fonts_by_selorder, i);\r
+       if (info->flags &~ fs->filter_flags) {\r
+           info->sizeindex = -1;\r
+           continue;                  /* we're filtering out this font */\r
+       }\r
+       if (info->size) {\r
+           sprintf(sizetext, "%d", info->size);\r
+           info->sizeindex = listindex;\r
+           gtk_list_store_append(fs->size_model, &iter);\r
+           gtk_list_store_set(fs->size_model, &iter,\r
+                              0, sizetext, 1, i, 2, info->size, -1);\r
+           listindex++;\r
+       } else {\r
+           int j;\r
+\r
+           assert(i == start);\r
+           assert(i+1 == end);\r
+\r
+           for (j = 0; j < lenof(unifontsel_default_sizes); j++) {\r
+               sprintf(sizetext, "%d", unifontsel_default_sizes[j]);\r
+               gtk_list_store_append(fs->size_model, &iter);\r
+               gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,\r
+                                  2, unifontsel_default_sizes[j], -1);\r
+               listindex++;\r
+           }\r
+       }\r
+    }\r
+}\r
+\r
+static void unifontsel_set_filter_buttons(unifontsel_internal *fs)\r
+{\r
+    int i;\r
+\r
+    for (i = 0; i < lenof(fs->filter_buttons); i++) {\r
+       int flagbit = GPOINTER_TO_INT(gtk_object_get_data\r
+                                     (GTK_OBJECT(fs->filter_buttons[i]),\r
+                                      "user-data"));\r
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),\r
+                                    !!(fs->filter_flags & flagbit));\r
+    }\r
+}\r
+\r
+static void unifontsel_draw_preview_text(unifontsel_internal *fs)\r
+{\r
+    unifont *font;\r
+    char *sizename = NULL;\r
+    fontinfo *info = fs->selected;\r
+\r
+    if (info) {\r
+       sizename = info->fontclass->scale_fontname\r
+           (GTK_WIDGET(fs->u.window), info->realname, fs->selsize);\r
+       font = info->fontclass->create(GTK_WIDGET(fs->u.window),\r
+                                      sizename ? sizename : info->realname,\r
+                                      FALSE, FALSE, 0, 0);\r
+    } else\r
+       font = NULL;\r
+\r
+    if (fs->preview_pixmap) {\r
+       GdkGC *gc = gdk_gc_new(fs->preview_pixmap);\r
+       gdk_gc_set_foreground(gc, &fs->preview_bg);\r
+       gdk_draw_rectangle(fs->preview_pixmap, gc, 1, 0, 0,\r
+                          fs->preview_width, fs->preview_height);\r
+       gdk_gc_set_foreground(gc, &fs->preview_fg);\r
+       if (font) {\r
+           /*\r
+            * The pangram used here is rather carefully\r
+            * constructed: it contains a sequence of very narrow\r
+            * letters (`jil') and a pair of adjacent very wide\r
+            * letters (`wm').\r
+            *\r
+            * If the user selects a proportional font, it will be\r
+            * coerced into fixed-width character cells when used\r
+            * in the actual terminal window. We therefore display\r
+            * it the same way in the preview pane, so as to show\r
+            * it the way it will actually be displayed - and we\r
+            * deliberately pick a pangram which will show the\r
+            * resulting miskerning at its worst.\r
+            *\r
+            * We aren't trying to sell people these fonts; we're\r
+            * trying to let them make an informed choice. Better\r
+            * that they find out the problems with using\r
+            * proportional fonts in terminal windows here than\r
+            * that they go to the effort of selecting their font\r
+            * and _then_ realise it was a mistake.\r
+            */\r
+           info->fontclass->draw_text(fs->preview_pixmap, gc, font,\r
+                                      0, font->ascent,\r
+                                      "bankrupt jilted showmen quiz convex fogey",\r
+                                      41, FALSE, FALSE, font->width);\r
+           info->fontclass->draw_text(fs->preview_pixmap, gc, font,\r
+                                      0, font->ascent + font->height,\r
+                                      "BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",\r
+                                      41, FALSE, FALSE, font->width);\r
+           /*\r
+            * The ordering of punctuation here is also selected\r
+            * with some specific aims in mind. I put ` and '\r
+            * together because some software (and people) still\r
+            * use them as matched quotes no matter what Unicode\r
+            * might say on the matter, so people can quickly\r
+            * check whether they look silly in a candidate font.\r
+            * The sequence #_@ is there to let people judge the\r
+            * suitability of the underscore as an effectively\r
+            * alphabetic character (since that's how it's often\r
+            * used in practice, at least by programmers).\r
+            */\r
+           info->fontclass->draw_text(fs->preview_pixmap, gc, font,\r
+                                      0, font->ascent + font->height * 2,\r
+                                      "0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",\r
+                                      42, FALSE, FALSE, font->width);\r
+       }\r
+       gdk_gc_unref(gc);\r
+       gdk_window_invalidate_rect(fs->preview_area->window, NULL, FALSE);\r
+    }\r
+    if (font)\r
+       info->fontclass->destroy(font);\r
+\r
+    sfree(sizename);\r
+}\r
+\r
+static void unifontsel_select_font(unifontsel_internal *fs,\r
+                                  fontinfo *info, int size, int leftlist,\r
+                                  int size_is_explicit)\r
+{\r
+    int index;\r
+    int minval, maxval;\r
+    GtkTreePath *treepath;\r
+    GtkTreeIter iter;\r
+\r
+    fs->inhibit_response = TRUE;\r
+\r
+    fs->selected = info;\r
+    fs->selsize = size;\r
+    if (size_is_explicit)\r
+       fs->intendedsize = size;\r
+\r
+    gtk_widget_set_sensitive(fs->u.ok_button, TRUE);\r
+\r
+    /*\r
+     * Find the index of this fontinfo in the selorder list. \r
+     */\r
+    index = -1;\r
+    findpos234(fs->fonts_by_selorder, info, NULL, &index);\r
+    assert(index >= 0);\r
+\r
+    /*\r
+     * Adjust the font selector flags and redo the font family\r
+     * list box, if necessary.\r
+     */\r
+    if (leftlist <= 0 &&\r
+       (fs->filter_flags | info->flags) != fs->filter_flags) {\r
+       fs->filter_flags |= info->flags;\r
+       unifontsel_set_filter_buttons(fs);\r
+       unifontsel_setup_familylist(fs);\r
+    }\r
+\r
+    /*\r
+     * Find the appropriate family name and select it in the list.\r
+     */\r
+    assert(info->familyindex >= 0);\r
+    treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);\r
+    gtk_tree_selection_select_path\r
+       (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),\r
+        treepath);\r
+    gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),\r
+                                treepath, NULL, FALSE, 0.0, 0.0);\r
+    gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, treepath);\r
+    gtk_tree_path_free(treepath);\r
+\r
+    /*\r
+     * Now set up the font style list.\r
+     */\r
+    gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,\r
+                      1, &minval, 2, &maxval, -1);\r
+    if (leftlist <= 1)\r
+       unifontsel_setup_stylelist(fs, minval, maxval);\r
+\r
+    /*\r
+     * Find the appropriate style name and select it in the list.\r
+     */\r
+    if (info->style) {\r
+       assert(info->styleindex >= 0);\r
+       treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);\r
+       gtk_tree_selection_select_path\r
+           (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),\r
+            treepath);\r
+       gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),\r
+                                    treepath, NULL, FALSE, 0.0, 0.0);\r
+       gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),\r
+                               &iter, treepath);\r
+       gtk_tree_path_free(treepath);\r
+\r
+       /*\r
+        * And set up the size list.\r
+        */\r
+       gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,\r
+                          1, &minval, 2, &maxval, -1);\r
+       if (leftlist <= 2)\r
+           unifontsel_setup_sizelist(fs, minval, maxval);\r
+\r
+       /*\r
+        * Find the appropriate size, and select it in the list.\r
+        */\r
+       if (info->size) {\r
+           assert(info->sizeindex >= 0);\r
+           treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);\r
+           gtk_tree_selection_select_path\r
+               (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),\r
+                treepath);\r
+           gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),\r
+                                        treepath, NULL, FALSE, 0.0, 0.0);\r
+           gtk_tree_path_free(treepath);\r
+           size = info->size;\r
+       } else {\r
+           int j;\r
+           for (j = 0; j < lenof(unifontsel_default_sizes); j++)\r
+               if (unifontsel_default_sizes[j] == size) {\r
+                   treepath = gtk_tree_path_new_from_indices(j, -1);\r
+                   gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),\r
+                                            treepath, NULL, FALSE);\r
+                   gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),\r
+                                                treepath, NULL, FALSE, 0.0,\r
+                                                0.0);\r
+                   gtk_tree_path_free(treepath);\r
+               }\r
+       }\r
+\r
+       /*\r
+        * And set up the font size text entry box.\r
+        */\r
+       {\r
+           char sizetext[40];\r
+           sprintf(sizetext, "%d", size);\r
+           gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);\r
+       }\r
+    } else {\r
+       if (leftlist <= 2)\r
+           unifontsel_setup_sizelist(fs, 0, 0);\r
+       gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");\r
+    }\r
+\r
+    /*\r
+     * Grey out the font size edit box if we're not using a\r
+     * scalable font.\r
+     */\r
+    gtk_entry_set_editable(GTK_ENTRY(fs->size_entry), fs->selected->size == 0);\r
+    gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);\r
+\r
+    unifontsel_draw_preview_text(fs);\r
+\r
+    fs->inhibit_response = FALSE;\r
+}\r
+\r
+static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    int newstate = gtk_toggle_button_get_active(tb);\r
+    int newflags;\r
+    int flagbit = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(tb),\r
+                                                     "user-data"));\r
+\r
+    if (newstate)\r
+       newflags = fs->filter_flags | flagbit;\r
+    else\r
+       newflags = fs->filter_flags & ~flagbit;\r
+\r
+    if (fs->filter_flags != newflags) {\r
+       fs->filter_flags = newflags;\r
+       unifontsel_setup_familylist(fs);\r
+    }\r
+}\r
+\r
+static void unifontsel_add_entry(void *ctx, const char *realfontname,\r
+                                const char *family, const char *charset,\r
+                                const char *style, const char *stylekey,\r
+                                int size, int flags,\r
+                                const struct unifont_vtable *fontclass)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)ctx;\r
+    fontinfo *info;\r
+    int totalsize;\r
+    char *p;\r
+\r
+    totalsize = sizeof(fontinfo) + strlen(realfontname) +\r
+       (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +\r
+       (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;\r
+    info = (fontinfo *)smalloc(totalsize);\r
+    info->fontclass = fontclass;\r
+    p = (char *)info + sizeof(fontinfo);\r
+    info->realname = p;\r
+    strcpy(p, realfontname);\r
+    p += 1+strlen(p);\r
+    if (family) {\r
+       info->family = p;\r
+       strcpy(p, family);\r
+       p += 1+strlen(p);\r
+    } else\r
+       info->family = NULL;\r
+    if (charset) {\r
+       info->charset = p;\r
+       strcpy(p, charset);\r
+       p += 1+strlen(p);\r
+    } else\r
+       info->charset = NULL;\r
+    if (style) {\r
+       info->style = p;\r
+       strcpy(p, style);\r
+       p += 1+strlen(p);\r
+    } else\r
+       info->style = NULL;\r
+    if (stylekey) {\r
+       info->stylekey = p;\r
+       strcpy(p, stylekey);\r
+       p += 1+strlen(p);\r
+    } else\r
+       info->stylekey = NULL;\r
+    assert(p - (char *)info <= totalsize);\r
+    info->size = size;\r
+    info->flags = flags;\r
+    info->index = count234(fs->fonts_by_selorder);\r
+\r
+    /*\r
+     * It's just conceivable that a misbehaving font enumerator\r
+     * might tell us about the same font real name more than once,\r
+     * in which case we should silently drop the new one.\r
+     */\r
+    if (add234(fs->fonts_by_realname, info) != info) {\r
+       sfree(info);\r
+       return;\r
+    }\r
+    /*\r
+     * However, we should never get a duplicate key in the\r
+     * selorder tree, because the index field carefully\r
+     * disambiguates otherwise identical records.\r
+     */\r
+    add234(fs->fonts_by_selorder, info);\r
+}\r
+\r
+static fontinfo *update_for_intended_size(unifontsel_internal *fs,\r
+                                         fontinfo *info)\r
+{\r
+    fontinfo info2, *below, *above;\r
+    int pos;\r
+\r
+    /*\r
+     * Copy the info structure. This doesn't copy its dynamic\r
+     * string fields, but that's unimportant because all we're\r
+     * going to do is to adjust the size field and use it in one\r
+     * tree search.\r
+     */\r
+    info2 = *info;\r
+    info2.size = fs->intendedsize;\r
+\r
+    /*\r
+     * Search in the tree to find the fontinfo structure which\r
+     * best approximates the size the user last requested.\r
+     */\r
+    below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,\r
+                         REL234_LE, &pos);\r
+    above = index234(fs->fonts_by_selorder, pos+1);\r
+\r
+    /*\r
+     * See if we've found it exactly, which is an easy special\r
+     * case. If we have, it'll be in `below' and not `above',\r
+     * because we did a REL234_LE rather than REL234_LT search.\r
+     */\r
+    if (!fontinfo_selorder_compare(&info2, below))\r
+       return below;\r
+\r
+    /*\r
+     * Now we've either found two suitable fonts, one smaller and\r
+     * one larger, or we're at one or other extreme end of the\r
+     * scale. Find out which, by NULLing out either of below and\r
+     * above if it differs from this one in any respect but size\r
+     * (and the disambiguating index field). Bear in mind, also,\r
+     * that either one might _already_ be NULL if we're at the\r
+     * extreme ends of the font list.\r
+     */\r
+    if (below) {\r
+       info2.size = below->size;\r
+       info2.index = below->index;\r
+       if (fontinfo_selorder_compare(&info2, below))\r
+           below = NULL;\r
+    }\r
+    if (above) {\r
+       info2.size = above->size;\r
+       info2.index = above->index;\r
+       if (fontinfo_selorder_compare(&info2, above))\r
+           above = NULL;\r
+    }\r
+\r
+    /*\r
+     * Now return whichever of above and below is non-NULL, if\r
+     * that's unambiguous.\r
+     */\r
+    if (!above)\r
+       return below;\r
+    if (!below)\r
+       return above;\r
+\r
+    /*\r
+     * And now we really do have to make a choice about whether to\r
+     * round up or down. We'll do it by rounding to nearest,\r
+     * breaking ties by rounding up.\r
+     */\r
+    if (above->size - fs->intendedsize <= fs->intendedsize - below->size)\r
+       return above;\r
+    else\r
+       return below;\r
+}\r
+\r
+static void family_changed(GtkTreeSelection *treeselection, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    GtkTreeModel *treemodel;\r
+    GtkTreeIter treeiter;\r
+    int minval;\r
+    fontinfo *info;\r
+\r
+    if (fs->inhibit_response)         /* we made this change ourselves */\r
+       return;\r
+\r
+    if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))\r
+       return;\r
+\r
+    gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);\r
+    info = (fontinfo *)index234(fs->fonts_by_selorder, minval);\r
+    info = update_for_intended_size(fs, info);\r
+    if (!info)\r
+       return; /* _shouldn't_ happen unless font list is completely funted */\r
+    if (!info->size)\r
+       fs->selsize = fs->intendedsize;   /* font is scalable */\r
+    unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,\r
+                          1, FALSE);\r
+}\r
+\r
+static void style_changed(GtkTreeSelection *treeselection, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    GtkTreeModel *treemodel;\r
+    GtkTreeIter treeiter;\r
+    int minval;\r
+    fontinfo *info;\r
+\r
+    if (fs->inhibit_response)         /* we made this change ourselves */\r
+       return;\r
+\r
+    if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))\r
+       return;\r
+\r
+    gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);\r
+    if (minval < 0)\r
+        return;                    /* somehow a charset heading got clicked */\r
+    info = (fontinfo *)index234(fs->fonts_by_selorder, minval);\r
+    info = update_for_intended_size(fs, info);\r
+    if (!info)\r
+       return; /* _shouldn't_ happen unless font list is completely funted */\r
+    if (!info->size)\r
+       fs->selsize = fs->intendedsize;   /* font is scalable */\r
+    unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,\r
+                          2, FALSE);\r
+}\r
+\r
+static void size_changed(GtkTreeSelection *treeselection, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    GtkTreeModel *treemodel;\r
+    GtkTreeIter treeiter;\r
+    int minval, size;\r
+    fontinfo *info;\r
+\r
+    if (fs->inhibit_response)         /* we made this change ourselves */\r
+       return;\r
+\r
+    if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))\r
+       return;\r
+\r
+    gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);\r
+    info = (fontinfo *)index234(fs->fonts_by_selorder, minval);\r
+    unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE);\r
+}\r
+\r
+static void size_entry_changed(GtkEditable *ed, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    const char *text;\r
+    int size;\r
+\r
+    if (fs->inhibit_response)         /* we made this change ourselves */\r
+       return;\r
+\r
+    text = gtk_entry_get_text(GTK_ENTRY(ed));\r
+    size = atoi(text);\r
+\r
+    if (size > 0) {\r
+       assert(fs->selected->size == 0);\r
+       unifontsel_select_font(fs, fs->selected, size, 3, TRUE);\r
+    }\r
+}\r
+\r
+static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,\r
+                         GtkTreeViewColumn *column, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    GtkTreeIter iter;\r
+    int minval, newsize;\r
+    fontinfo *info, *newinfo;\r
+    char *newname;\r
+\r
+    if (fs->inhibit_response)         /* we made this change ourselves */\r
+       return;\r
+\r
+    gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);\r
+    gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);\r
+    info = (fontinfo *)index234(fs->fonts_by_selorder, minval);\r
+    if (info) {\r
+       int flags;\r
+       struct fontinfo_realname_find f;\r
+\r
+       newname = info->fontclass->canonify_fontname\r
+           (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE);\r
+\r
+       f.realname = newname;\r
+       f.flags = flags;\r
+       newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);\r
+\r
+       sfree(newname);\r
+       if (!newinfo)\r
+           return;                    /* font name not in our index */\r
+       if (newinfo == info)\r
+           return;   /* didn't change under canonification => not an alias */\r
+       unifontsel_select_font(fs, newinfo,\r
+                              newinfo->size ? newinfo->size : newsize,\r
+                              1, TRUE);\r
+    }\r
+}\r
+\r
+static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,\r
+                                  gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+\r
+    if (fs->preview_pixmap) {\r
+       gdk_draw_pixmap(widget->window,\r
+                       widget->style->fg_gc[GTK_WIDGET_STATE(widget)],\r
+                       fs->preview_pixmap,\r
+                       event->area.x, event->area.y,\r
+                       event->area.x, event->area.y,\r
+                       event->area.width, event->area.height);\r
+    }\r
+    return TRUE;\r
+}\r
+\r
+static gint unifontsel_configure_area(GtkWidget *widget,\r
+                                     GdkEventConfigure *event, gpointer data)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)data;\r
+    int ox, oy, nx, ny, x, y;\r
+\r
+    /*\r
+     * Enlarge the pixmap, but never shrink it.\r
+     */\r
+    ox = fs->preview_width;\r
+    oy = fs->preview_height;\r
+    x = event->width;\r
+    y = event->height;\r
+    if (x > ox || y > oy) {\r
+       if (fs->preview_pixmap)\r
+           gdk_pixmap_unref(fs->preview_pixmap);\r
+       \r
+       nx = (x > ox ? x : ox);\r
+       ny = (y > oy ? y : oy);\r
+       fs->preview_pixmap = gdk_pixmap_new(widget->window, nx, ny, -1);\r
+       fs->preview_width = nx;\r
+       fs->preview_height = ny;\r
+\r
+       unifontsel_draw_preview_text(fs);\r
+    }\r
+\r
+    gdk_window_invalidate_rect(widget->window, NULL, FALSE);\r
+\r
+    return TRUE;\r
+}\r
+\r
+unifontsel *unifontsel_new(const char *wintitle)\r
+{\r
+    unifontsel_internal *fs = snew(unifontsel_internal);\r
+    GtkWidget *table, *label, *w, *ww, *scroll;\r
+    GtkListStore *model;\r
+    GtkTreeViewColumn *column;\r
+    int lists_height, preview_height, font_width, style_width, size_width;\r
+    int i;\r
+\r
+    fs->inhibit_response = FALSE;\r
+    fs->selected = NULL;\r
+\r
+    {\r
+       /*\r
+        * Invent some magic size constants.\r
+        */\r
+       GtkRequisition req;\r
+       label = gtk_label_new("Quite Long Font Name (Foundry)");\r
+       gtk_widget_size_request(label, &req);\r
+       font_width = req.width;\r
+       lists_height = 14 * req.height;\r
+       preview_height = 5 * req.height;\r
+       gtk_label_set_text(GTK_LABEL(label), "Italic Extra Condensed");\r
+       gtk_widget_size_request(label, &req);\r
+       style_width = req.width;\r
+       gtk_label_set_text(GTK_LABEL(label), "48000");\r
+       gtk_widget_size_request(label, &req);\r
+       size_width = req.width;\r
+#if GTK_CHECK_VERSION(2,10,0)\r
+       g_object_ref_sink(label);\r
+       g_object_unref(label);\r
+#else\r
+        gtk_object_sink(GTK_OBJECT(label));\r
+#endif\r
+    }\r
+\r
+    /*\r
+     * Create the dialog box and initialise the user-visible\r
+     * fields in the returned structure.\r
+     */\r
+    fs->u.user_data = NULL;\r
+    fs->u.window = GTK_WINDOW(gtk_dialog_new());\r
+    gtk_window_set_title(fs->u.window, wintitle);\r
+    fs->u.cancel_button = gtk_dialog_add_button\r
+       (GTK_DIALOG(fs->u.window), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);\r
+    fs->u.ok_button = gtk_dialog_add_button\r
+       (GTK_DIALOG(fs->u.window), GTK_STOCK_OK, GTK_RESPONSE_OK);\r
+    gtk_widget_grab_default(fs->u.ok_button);\r
+\r
+    /*\r
+     * Now set up the internal fields, including in particular all\r
+     * the controls that actually allow the user to select fonts.\r
+     */\r
+    table = gtk_table_new(8, 3, FALSE);\r
+    gtk_widget_show(table);\r
+    gtk_table_set_col_spacings(GTK_TABLE(table), 8);\r
+#if GTK_CHECK_VERSION(2,4,0)\r
+    /* GtkAlignment seems to be the simplest way to put padding round things */\r
+    w = gtk_alignment_new(0, 0, 1, 1);\r
+    gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);\r
+    gtk_container_add(GTK_CONTAINER(w), table);\r
+    gtk_widget_show(w);\r
+#else\r
+    w = table;\r
+#endif\r
+    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fs->u.window)->vbox),\r
+                      w, TRUE, TRUE, 0);\r
+\r
+    label = gtk_label_new_with_mnemonic("_Font:");\r
+    gtk_widget_show(label);\r
+    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);\r
+    gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);\r
+\r
+    /*\r
+     * The Font list box displays only a string, but additionally\r
+     * stores two integers which give the limits within the\r
+     * tree234 of the font entries covered by this list entry.\r
+     */\r
+    model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);\r
+    w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));\r
+    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);\r
+    gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);\r
+    gtk_widget_show(w);\r
+    column = gtk_tree_view_column_new_with_attributes\r
+       ("Font", gtk_cell_renderer_text_new(),\r
+        "text", 0, (char *)NULL);\r
+    gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);\r
+    gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);\r
+    g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),\r
+                    "changed", G_CALLBACK(family_changed), fs);\r
+    g_signal_connect(G_OBJECT(w), "row-activated",\r
+                    G_CALLBACK(alias_resolve), fs);\r
+\r
+    scroll = gtk_scrolled_window_new(NULL, NULL);\r
+    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),\r
+                                       GTK_SHADOW_IN);\r
+    gtk_container_add(GTK_CONTAINER(scroll), w);\r
+    gtk_widget_show(scroll);\r
+    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),\r
+                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);\r
+    gtk_widget_set_size_request(scroll, font_width, lists_height);\r
+    gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,\r
+                    GTK_EXPAND | GTK_FILL, 0, 0);\r
+    fs->family_model = model;\r
+    fs->family_list = w;\r
+\r
+    label = gtk_label_new_with_mnemonic("_Style:");\r
+    gtk_widget_show(label);\r
+    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);\r
+    gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);\r
+\r
+    /*\r
+     * The Style list box can contain insensitive elements\r
+     * (character set headings for server-side fonts), so we add\r
+     * an extra column to the list store to hold that information.\r
+     */\r
+    model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,\r
+                              G_TYPE_BOOLEAN);\r
+    w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));\r
+    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);\r
+    gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);\r
+    gtk_widget_show(w);\r
+    column = gtk_tree_view_column_new_with_attributes\r
+       ("Style", gtk_cell_renderer_text_new(),\r
+        "text", 0, "sensitive", 3, (char *)NULL);\r
+    gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);\r
+    gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);\r
+    g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),\r
+                    "changed", G_CALLBACK(style_changed), fs);\r
+\r
+    scroll = gtk_scrolled_window_new(NULL, NULL);\r
+    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),\r
+                                       GTK_SHADOW_IN);\r
+    gtk_container_add(GTK_CONTAINER(scroll), w);\r
+    gtk_widget_show(scroll);\r
+    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),\r
+                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);\r
+    gtk_widget_set_size_request(scroll, style_width, lists_height);\r
+    gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,\r
+                    GTK_EXPAND | GTK_FILL, 0, 0);\r
+    fs->style_model = model;\r
+    fs->style_list = w;\r
+\r
+    label = gtk_label_new_with_mnemonic("Si_ze:");\r
+    gtk_widget_show(label);\r
+    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);\r
+    gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);\r
+\r
+    /*\r
+     * The Size label attaches primarily to a text input box so\r
+     * that the user can select a size of their choice. The list\r
+     * of available sizes is secondary.\r
+     */\r
+    fs->size_entry = w = gtk_entry_new();\r
+    gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);\r
+    gtk_widget_set_size_request(w, size_width, -1);\r
+    gtk_widget_show(w);\r
+    gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);\r
+    g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),\r
+                    fs);\r
+\r
+    model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);\r
+    w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));\r
+    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);\r
+    gtk_widget_show(w);\r
+    column = gtk_tree_view_column_new_with_attributes\r
+       ("Size", gtk_cell_renderer_text_new(),\r
+        "text", 0, (char *)NULL);\r
+    gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);\r
+    gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);\r
+    g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),\r
+                    "changed", G_CALLBACK(size_changed), fs);\r
+\r
+    scroll = gtk_scrolled_window_new(NULL, NULL);\r
+    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),\r
+                                       GTK_SHADOW_IN);\r
+    gtk_container_add(GTK_CONTAINER(scroll), w);\r
+    gtk_widget_show(scroll);\r
+    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),\r
+                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);\r
+    gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,\r
+                    GTK_EXPAND | GTK_FILL, 0, 0);\r
+    fs->size_model = model;\r
+    fs->size_list = w;\r
+\r
+    /*\r
+     * Preview widget.\r
+     */\r
+    fs->preview_area = gtk_drawing_area_new();\r
+    fs->preview_pixmap = NULL;\r
+    fs->preview_width = 0;\r
+    fs->preview_height = 0;\r
+    fs->preview_fg.pixel = fs->preview_bg.pixel = 0;\r
+    fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;\r
+    fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;\r
+    gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,\r
+                            FALSE, FALSE);\r
+    gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,\r
+                            FALSE, FALSE);\r
+    gtk_signal_connect(GTK_OBJECT(fs->preview_area), "expose_event",\r
+                      GTK_SIGNAL_FUNC(unifontsel_expose_area), fs);\r
+    gtk_signal_connect(GTK_OBJECT(fs->preview_area), "configure_event",\r
+                      GTK_SIGNAL_FUNC(unifontsel_configure_area), fs);\r
+    gtk_widget_set_size_request(fs->preview_area, 1, preview_height);\r
+    gtk_widget_show(fs->preview_area);\r
+    ww = fs->preview_area;\r
+    w = gtk_frame_new(NULL);\r
+    gtk_container_add(GTK_CONTAINER(w), ww);\r
+    gtk_widget_show(w);\r
+#if GTK_CHECK_VERSION(2,4,0)\r
+    ww = w;\r
+    /* GtkAlignment seems to be the simplest way to put padding round things */\r
+    w = gtk_alignment_new(0, 0, 1, 1);\r
+    gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);\r
+    gtk_container_add(GTK_CONTAINER(w), ww);\r
+    gtk_widget_show(w);\r
+#endif\r
+    ww = w;\r
+    w = gtk_frame_new("Preview of font");\r
+    gtk_container_add(GTK_CONTAINER(w), ww);\r
+    gtk_widget_show(w);\r
+    gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,\r
+                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);\r
+\r
+    i = 0;\r
+    w = gtk_check_button_new_with_label("Show client-side fonts");\r
+    gtk_object_set_data(GTK_OBJECT(w), "user-data",\r
+                       GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));\r
+    gtk_signal_connect(GTK_OBJECT(w), "toggled",\r
+                      GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);\r
+    gtk_widget_show(w);\r
+    fs->filter_buttons[i++] = w;\r
+    gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);\r
+    w = gtk_check_button_new_with_label("Show server-side fonts");\r
+    gtk_object_set_data(GTK_OBJECT(w), "user-data",\r
+                       GINT_TO_POINTER(FONTFLAG_SERVERSIDE));\r
+    gtk_signal_connect(GTK_OBJECT(w), "toggled",\r
+                      GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);\r
+    gtk_widget_show(w);\r
+    fs->filter_buttons[i++] = w;\r
+    gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);\r
+    w = gtk_check_button_new_with_label("Show server-side font aliases");\r
+    gtk_object_set_data(GTK_OBJECT(w), "user-data",\r
+                       GINT_TO_POINTER(FONTFLAG_SERVERALIAS));\r
+    gtk_signal_connect(GTK_OBJECT(w), "toggled",\r
+                      GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);\r
+    gtk_widget_show(w);\r
+    fs->filter_buttons[i++] = w;\r
+    gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);\r
+    w = gtk_check_button_new_with_label("Show non-monospaced fonts");\r
+    gtk_object_set_data(GTK_OBJECT(w), "user-data",\r
+                       GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));\r
+    gtk_signal_connect(GTK_OBJECT(w), "toggled",\r
+                      GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs);\r
+    gtk_widget_show(w);\r
+    fs->filter_buttons[i++] = w;\r
+    gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);\r
+\r
+    assert(i == lenof(fs->filter_buttons));\r
+    fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |\r
+       FONTFLAG_SERVERALIAS;\r
+    unifontsel_set_filter_buttons(fs);\r
+\r
+    /*\r
+     * Go and find all the font names, and set up our master font\r
+     * list.\r
+     */\r
+    fs->fonts_by_realname = newtree234(fontinfo_realname_compare);\r
+    fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);\r
+    for (i = 0; i < lenof(unifont_types); i++)\r
+       unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),\r
+                                    unifontsel_add_entry, fs);\r
+\r
+    /*\r
+     * And set up the initial font names list.\r
+     */\r
+    unifontsel_setup_familylist(fs);\r
+\r
+    fs->selsize = fs->intendedsize = 13;   /* random default */\r
+    gtk_widget_set_sensitive(fs->u.ok_button, FALSE);\r
+\r
+    return (unifontsel *)fs;\r
+}\r
+\r
+void unifontsel_destroy(unifontsel *fontsel)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)fontsel;\r
+    fontinfo *info;\r
+\r
+    if (fs->preview_pixmap)\r
+       gdk_pixmap_unref(fs->preview_pixmap);\r
+\r
+    freetree234(fs->fonts_by_selorder);\r
+    while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)\r
+       sfree(info);\r
+    freetree234(fs->fonts_by_realname);\r
+\r
+    gtk_widget_destroy(GTK_WIDGET(fs->u.window));\r
+    sfree(fs);\r
+}\r
+\r
+void unifontsel_set_name(unifontsel *fontsel, const char *fontname)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)fontsel;\r
+    int i, start, end, size, flags;\r
+    const char *fontname2 = NULL;\r
+    fontinfo *info;\r
+\r
+    /*\r
+     * Provide a default if given an empty or null font name.\r
+     */\r
+    if (!fontname || !*fontname)\r
+       fontname = "server:fixed";\r
+\r
+    /*\r
+     * Call the canonify_fontname function.\r
+     */\r
+    fontname = unifont_do_prefix(fontname, &start, &end);\r
+    for (i = start; i < end; i++) {\r
+       fontname2 = unifont_types[i]->canonify_fontname\r
+           (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE);\r
+       if (fontname2)\r
+           break;\r
+    }\r
+    if (i == end)\r
+       return;                        /* font name not recognised */\r
+\r
+    /*\r
+     * Now look up the canonified font name in our index.\r
+     */\r
+    {\r
+       struct fontinfo_realname_find f;\r
+       f.realname = fontname2;\r
+       f.flags = flags;\r
+       info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);\r
+    }\r
+\r
+    /*\r
+     * If we've found the font, and its size field is either\r
+     * correct or zero (the latter indicating a scalable font),\r
+     * then we're done. Otherwise, try looking up the original\r
+     * font name instead.\r
+     */\r
+    if (!info || (info->size != size && info->size != 0)) {\r
+       struct fontinfo_realname_find f;\r
+       f.realname = fontname;\r
+       f.flags = flags;\r
+\r
+       info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);\r
+       if (!info || info->size != size)\r
+           return;                    /* font name not in our index */\r
+    }\r
+\r
+    /*\r
+     * Now we've got a fontinfo structure and a font size, so we\r
+     * know everything we need to fill in all the fields in the\r
+     * dialog.\r
+     */\r
+    unifontsel_select_font(fs, info, size, 0, TRUE);\r
+}\r
+\r
+char *unifontsel_get_name(unifontsel *fontsel)\r
+{\r
+    unifontsel_internal *fs = (unifontsel_internal *)fontsel;\r
+    char *name;\r
+\r
+    if (!fs->selected)\r
+       return NULL;\r
+\r
+    if (fs->selected->size == 0) {\r
+       name = fs->selected->fontclass->scale_fontname\r
+           (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);\r
+       if (name) {\r
+           char *ret = dupcat(fs->selected->fontclass->prefix, ":",\r
+                              name, NULL);\r
+           sfree(name);\r
+           return ret;\r
+       }\r
+    }\r
+\r
+    return dupcat(fs->selected->fontclass->prefix, ":",\r
+                 fs->selected->realname, NULL);\r
+}\r
+\r
+#endif /* GTK_CHECK_VERSION(2,0,0) */\r