2 * Unified font management for GTK.
4 * PuTTY is willing to use both old-style X server-side bitmap
5 * fonts _and_ GTK2/Pango client-side fonts. This requires us to
6 * do a bit of work to wrap the two wildly different APIs into
7 * forms the rest of the code can switch between seamlessly, and
8 * also requires a custom font selector capable of handling both
16 #include <gdk/gdkkeysyms.h>
19 #include <X11/Xutil.h>
20 #include <X11/Xatom.h>
29 * - it would be nice to have a display of the current font name,
30 * and in particular whether it's client- or server-side,
31 * during the progress of the font selector.
33 * - it would be nice if we could move the processing of
34 * underline and VT100 double width into this module, so that
35 * instead of using the ghastly pixmap-stretching technique
36 * everywhere we could tell the Pango backend to scale its
37 * fonts to double size properly and at full resolution.
38 * However, this requires me to learn how to make Pango stretch
39 * text to an arbitrary aspect ratio (for double-width only
40 * text, which perversely is harder than DW+DH), and right now
41 * I haven't the energy.
45 * Ad-hoc vtable mechanism to allow font structures to be
48 * Any instance of `unifont' used in the vtable functions will
49 * actually be the first element of a larger structure containing
50 * data specific to the subtype. This is permitted by the ISO C
51 * provision that one may safely cast between a pointer to a
52 * structure and a pointer to its first element.
55 #define FONTFLAG_CLIENTSIDE 0x0001
56 #define FONTFLAG_SERVERSIDE 0x0002
57 #define FONTFLAG_SERVERALIAS 0x0004
58 #define FONTFLAG_NONMONOSPACED 0x0008
60 #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
62 typedef void (*fontsel_add_entry
)(void *ctx
, const char *realfontname
,
63 const char *family
, const char *charset
,
64 const char *style
, const char *stylekey
,
66 const struct unifont_vtable
*fontclass
);
68 struct unifont_vtable
{
70 * `Methods' of the `class'.
72 unifont
*(*create
)(GtkWidget
*widget
, const char *name
, int wide
, int bold
,
73 int shadowoffset
, int shadowalways
);
74 unifont
*(*create_fallback
)(GtkWidget
*widget
, int height
, int wide
,
75 int bold
, int shadowoffset
, int shadowalways
);
76 void (*destroy
)(unifont
*font
);
77 int (*has_glyph
)(unifont
*font
, wchar_t glyph
);
78 void (*draw_text
)(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
79 int x
, int y
, const wchar_t *string
, int len
, int wide
,
80 int bold
, int cellwidth
);
81 void (*enum_fonts
)(GtkWidget
*widget
,
82 fontsel_add_entry callback
, void *callback_ctx
);
83 char *(*canonify_fontname
)(GtkWidget
*widget
, const char *name
, int *size
,
84 int *flags
, int resolve_aliases
);
85 char *(*scale_fontname
)(GtkWidget
*widget
, const char *name
, int size
);
88 * `Static data members' of the `class'.
93 /* ----------------------------------------------------------------------
94 * X11 font implementation, directly using Xlib calls.
97 static int x11font_has_glyph(unifont
*font
, wchar_t glyph
);
98 static void x11font_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
99 int x
, int y
, const wchar_t *string
, int len
,
100 int wide
, int bold
, int cellwidth
);
101 static unifont
*x11font_create(GtkWidget
*widget
, const char *name
,
103 int shadowoffset
, int shadowalways
);
104 static void x11font_destroy(unifont
*font
);
105 static void x11font_enum_fonts(GtkWidget
*widget
,
106 fontsel_add_entry callback
, void *callback_ctx
);
107 static char *x11font_canonify_fontname(GtkWidget
*widget
, const char *name
,
108 int *size
, int *flags
,
109 int resolve_aliases
);
110 static char *x11font_scale_fontname(GtkWidget
*widget
, const char *name
,
116 * Actual font objects. We store a number of these, for
117 * automatically guessed bold and wide variants.
119 * The parallel array `allocated' indicates whether we've
120 * tried to fetch a subfont already (thus distinguishing NULL
121 * because we haven't tried yet from NULL because we tried and
122 * failed, so that we don't keep trying and failing
125 XFontStruct
*fonts
[4];
128 * `sixteen_bit' is true iff the font object is indexed by
129 * values larger than a byte. That is, this flag tells us
130 * whether we use XDrawString or XDrawString16, etc.
134 * `variable' is true iff the font is non-fixed-pitch. This
135 * enables some code which takes greater care over character
136 * positioning during text drawing.
140 * real_charset is the charset used when translating text into the
141 * font's internal encoding inside draw_text(). This need not be
142 * the same as the public_charset provided to the client; for
143 * example, public_charset might be CS_ISO8859_1 while
144 * real_charset is CS_ISO8859_1_X11.
148 * Data passed in to unifont_create().
150 int wide
, bold
, shadowoffset
, shadowalways
;
153 static const struct unifont_vtable x11font_vtable
= {
155 NULL
, /* no fallback fonts in X11 */
160 x11font_canonify_fontname
,
161 x11font_scale_fontname
,
165 static char *x11_guess_derived_font_name(XFontStruct
*xfs
, int bold
, int wide
)
167 Display
*disp
= GDK_DISPLAY();
168 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
170 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
171 char *name
= XGetAtomName(disp
, (Atom
)ret
);
172 if (name
&& name
[0] == '-') {
174 char *dupname
, *extrafree
= NULL
, *ret
;
178 p
= q
= dupname
= dupstr(name
); /* skip initial minus */
181 while (*p
&& nstr
< lenof(strings
)) {
184 strings
[nstr
++] = p
+1;
189 if (nstr
< lenof(strings
))
190 return NULL
; /* XLFD was malformed */
196 /* 4 is `wideness', which obviously may have changed. */
197 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
198 strings
[4] = strings
[5] = "*";
199 strings
[11] = extrafree
= dupprintf("%d", 2*atoi(strings
[11]));
202 ret
= dupcat("-", strings
[ 0], "-", strings
[ 1], "-", strings
[ 2],
203 "-", strings
[ 3], "-", strings
[ 4], "-", strings
[ 5],
204 "-", strings
[ 6], "-", strings
[ 7], "-", strings
[ 8],
205 "-", strings
[ 9], "-", strings
[10], "-", strings
[11],
206 "-", strings
[12], NULL
);
216 static int x11_font_width(XFontStruct
*xfs
, int sixteen_bit
)
222 return XTextWidth16(xfs
, &space
, 1);
224 return XTextWidth(xfs
, "0", 1);
228 static int x11_font_has_glyph(XFontStruct
*xfs
, int byte1
, int byte2
)
233 * Not to be confused with x11font_has_glyph, which is a method of
234 * the x11font 'class' and hence takes a unifont as argument. This
235 * is the low-level function which grubs about in an actual
236 * XFontStruct to see if a given glyph exists.
238 * We must do this ourselves rather than letting Xlib's
239 * XTextExtents16 do the job, because XTextExtents will helpfully
240 * substitute the font's default_char for any missing glyph and
241 * not tell us it did so, which precisely won't help us find out
242 * which glyphs _are_ missing.
244 * The man page for XQueryFont is rather confusing about how the
245 * per_char array in the XFontStruct is laid out, because it gives
246 * formulae for determining the two-byte X character code _from_
247 * an index into the per_char array. Going the other way, it's
250 * The valid character codes have byte1 between min_byte1 and
251 * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
252 * max_char_or_byte2 inclusive. This gives a rectangle of size
253 * (max_byte2-min_byte1+1) by
254 * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
255 * rectangle encoded in the per_char array. Hence, given a
256 * character code which is valid in the sense that it falls
257 * somewhere in that rectangle, its index in per_char is given by
260 * x = byte2 - min_char_or_byte2
261 * y = byte1 - min_byte1
262 * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
264 * If min_byte1 and min_byte2 are both zero, that's a special case
265 * which can be treated as if min_byte2 was 1 instead, i.e. the
266 * per_char array just runs from min_char_or_byte2 to
267 * max_char_or_byte2 inclusive, and byte1 should always be zero.
270 if (byte2
< xfs
->min_char_or_byte2
|| byte2
> xfs
->max_char_or_byte2
)
273 if (xfs
->min_byte1
== 0 && xfs
->max_byte1
== 0) {
274 index
= byte2
- xfs
->min_char_or_byte2
;
276 if (byte1
< xfs
->min_byte1
|| byte1
> xfs
->max_byte1
)
278 index
= ((byte2
- xfs
->min_char_or_byte2
) +
279 ((byte1
- xfs
->min_byte1
) *
280 (xfs
->max_char_or_byte2
- xfs
->min_char_or_byte2
+ 1)));
283 return (xfs
->per_char
[index
].ascent
+ xfs
->per_char
[index
].descent
> 0 ||
284 xfs
->per_char
[index
].width
> 0);
287 static unifont
*x11font_create(GtkWidget
*widget
, const char *name
,
289 int shadowoffset
, int shadowalways
)
291 struct x11font
*xfont
;
293 Display
*disp
= GDK_DISPLAY();
294 Atom charset_registry
, charset_encoding
, spacing
;
295 unsigned long registry_ret
, encoding_ret
, spacing_ret
;
296 int pubcs
, realcs
, sixteen_bit
, variable
;
299 xfs
= XLoadQueryFont(disp
, name
);
303 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
304 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
306 pubcs
= realcs
= CS_NONE
;
310 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
311 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
313 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
314 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
316 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
317 pubcs
= realcs
= charset_from_xenc(encoding
);
320 * iso10646-1 is the only wide font encoding we
321 * support. In this case, we expect clients to give us
322 * UTF-8, which this module must internally convert
323 * into 16-bit Unicode.
325 if (!strcasecmp(encoding
, "iso10646-1")) {
327 pubcs
= realcs
= CS_UTF8
;
331 * Hack for X line-drawing characters: if the primary font
332 * is encoded as ISO-8859-1, and has valid glyphs in the
333 * low character positions, it is assumed that those
334 * glyphs are the VT100 line-drawing character set.
336 if (pubcs
== CS_ISO8859_1
) {
338 for (ch
= 1; ch
< 32; ch
++)
339 if (!x11_font_has_glyph(xfs
, 0, ch
))
342 realcs
= CS_ISO8859_1_X11
;
349 spacing
= XInternAtom(disp
, "SPACING", False
);
350 if (XGetFontProperty(xfs
, spacing
, &spacing_ret
)) {
352 spc
= XGetAtomName(disp
, (Atom
)spacing_ret
);
354 if (spc
&& strchr("CcMm", spc
[0]))
358 xfont
= snew(struct x11font
);
359 xfont
->u
.vt
= &x11font_vtable
;
360 xfont
->u
.width
= x11_font_width(xfs
, sixteen_bit
);
361 xfont
->u
.ascent
= xfs
->ascent
;
362 xfont
->u
.descent
= xfs
->descent
;
363 xfont
->u
.height
= xfont
->u
.ascent
+ xfont
->u
.descent
;
364 xfont
->u
.public_charset
= pubcs
;
365 xfont
->u
.want_fallback
= TRUE
;
366 xfont
->real_charset
= realcs
;
367 xfont
->fonts
[0] = xfs
;
368 xfont
->allocated
[0] = TRUE
;
369 xfont
->sixteen_bit
= sixteen_bit
;
370 xfont
->variable
= variable
;
373 xfont
->shadowoffset
= shadowoffset
;
374 xfont
->shadowalways
= shadowalways
;
376 for (i
= 1; i
< lenof(xfont
->fonts
); i
++) {
377 xfont
->fonts
[i
] = NULL
;
378 xfont
->allocated
[i
] = FALSE
;
381 return (unifont
*)xfont
;
384 static void x11font_destroy(unifont
*font
)
386 Display
*disp
= GDK_DISPLAY();
387 struct x11font
*xfont
= (struct x11font
*)font
;
390 for (i
= 0; i
< lenof(xfont
->fonts
); i
++)
392 XFreeFont(disp
, xfont
->fonts
[i
]);
396 static void x11_alloc_subfont(struct x11font
*xfont
, int sfid
)
398 Display
*disp
= GDK_DISPLAY();
399 char *derived_name
= x11_guess_derived_font_name
400 (xfont
->fonts
[0], sfid
& 1, !!(sfid
& 2));
401 xfont
->fonts
[sfid
] = XLoadQueryFont(disp
, derived_name
);
402 xfont
->allocated
[sfid
] = TRUE
;
404 /* Note that xfont->fonts[sfid] may still be NULL, if XLQF failed. */
407 static int x11font_has_glyph(unifont
*font
, wchar_t glyph
)
409 struct x11font
*xfont
= (struct x11font
*)font
;
411 if (xfont
->sixteen_bit
) {
413 * This X font has 16-bit character indices, which means
414 * we can directly use our Unicode input value.
416 return x11_font_has_glyph(xfont
->fonts
[0], glyph
>> 8, glyph
& 0xFF);
419 * This X font has 8-bit indices, so we must convert to the
420 * appropriate character set.
423 int sblen
= wc_to_mb(xfont
->real_charset
, 0, &glyph
, 1,
424 sbstring
, 2, "", NULL
, NULL
);
426 return FALSE
; /* not even in the charset */
428 return x11_font_has_glyph(xfont
->fonts
[0], 0, sbstring
[0]);
432 #if !GTK_CHECK_VERSION(2,0,0)
433 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
436 static void x11font_really_draw_text_16(GdkDrawable
*target
, XFontStruct
*xfs
,
438 const XChar2b
*string
, int nchars
,
440 int fontvariable
, int cellwidth
)
442 Display
*disp
= GDK_DISPLAY();
443 int step
, nsteps
, centre
;
447 * In a variable-pitch font, we draw one character at a
448 * time, and centre it in the character cell.
455 * In a fixed-pitch font, we can draw the whole lot in one go.
462 while (nsteps
-- > 0) {
465 X
+= (cellwidth
- XTextWidth16(xfs
, string
, step
)) / 2;
467 XDrawString16(disp
, GDK_DRAWABLE_XID(target
), gc
,
470 XDrawString16(disp
, GDK_DRAWABLE_XID(target
), gc
,
471 X
+ shadowoffset
, y
, string
, step
);
478 static void x11font_really_draw_text(GdkDrawable
*target
, XFontStruct
*xfs
,
480 const char *string
, int nchars
,
482 int fontvariable
, int cellwidth
)
484 Display
*disp
= GDK_DISPLAY();
485 int step
, nsteps
, centre
;
489 * In a variable-pitch font, we draw one character at a
490 * time, and centre it in the character cell.
497 * In a fixed-pitch font, we can draw the whole lot in one go.
504 while (nsteps
-- > 0) {
507 X
+= (cellwidth
- XTextWidth(xfs
, string
, step
)) / 2;
509 XDrawString(disp
, GDK_DRAWABLE_XID(target
), gc
,
512 XDrawString(disp
, GDK_DRAWABLE_XID(target
), gc
,
513 X
+ shadowoffset
, y
, string
, step
);
520 static void x11font_draw_text(GdkDrawable
*target
, GdkGC
*gdkgc
, unifont
*font
,
521 int x
, int y
, const wchar_t *string
, int len
,
522 int wide
, int bold
, int cellwidth
)
524 Display
*disp
= GDK_DISPLAY();
525 struct x11font
*xfont
= (struct x11font
*)font
;
526 GC gc
= GDK_GC_XGC(gdkgc
);
528 int shadowoffset
= 0;
529 int mult
= (wide ?
2 : 1);
535 * Decide which subfont we're using, and whether we have to
538 if (xfont
->shadowalways
&& bold
) {
539 shadowoffset
= xfont
->shadowoffset
;
542 sfid
= 2 * wide
+ bold
;
543 if (!xfont
->allocated
[sfid
])
544 x11_alloc_subfont(xfont
, sfid
);
545 if (bold
&& !xfont
->fonts
[sfid
]) {
547 shadowoffset
= xfont
->shadowoffset
;
548 sfid
= 2 * wide
+ bold
;
549 if (!xfont
->allocated
[sfid
])
550 x11_alloc_subfont(xfont
, sfid
);
553 if (!xfont
->fonts
[sfid
])
554 return; /* we've tried our best, but no luck */
556 XSetFont(disp
, gc
, xfont
->fonts
[sfid
]->fid
);
558 if (xfont
->sixteen_bit
) {
560 * This X font has 16-bit character indices, which means
561 * we can directly use our Unicode input string.
566 xcs
= snewn(len
, XChar2b
);
567 for (i
= 0; i
< len
; i
++) {
568 xcs
[i
].byte1
= string
[i
] >> 8;
569 xcs
[i
].byte2
= string
[i
];
572 x11font_really_draw_text_16(target
, xfont
->fonts
[sfid
], gc
, x
, y
,
573 xcs
, len
, shadowoffset
,
574 xfont
->variable
, cellwidth
* mult
);
578 * This X font has 8-bit indices, so we must convert to the
579 * appropriate character set.
581 char *sbstring
= snewn(len
+1, char);
582 int sblen
= wc_to_mb(xfont
->real_charset
, 0, string
, len
,
583 sbstring
, len
+1, ".", NULL
, NULL
);
584 x11font_really_draw_text(target
, xfont
->fonts
[sfid
], gc
, x
, y
,
585 sbstring
, sblen
, shadowoffset
,
586 xfont
->variable
, cellwidth
* mult
);
591 static void x11font_enum_fonts(GtkWidget
*widget
,
592 fontsel_add_entry callback
, void *callback_ctx
)
596 int nnames
, i
, max
, tmpsize
;
600 fontnames
= XListFonts(GDK_DISPLAY(), "*", max
, &nnames
);
602 XFreeFontNames(fontnames
);
610 for (i
= 0; i
< nnames
; i
++) {
611 if (fontnames
[i
][0] == '-') {
613 * Dismember an XLFD and convert it into the format
614 * we'll be using in the font selector.
616 char *components
[14];
617 char *p
, *font
, *style
, *stylekey
, *charset
;
618 int j
, weightkey
, slantkey
, setwidthkey
;
619 int thistmpsize
, fontsize
, flags
;
621 thistmpsize
= 4 * strlen(fontnames
[i
]) + 256;
622 if (tmpsize
< thistmpsize
) {
623 tmpsize
= thistmpsize
;
624 tmp
= sresize(tmp
, tmpsize
, char);
626 strcpy(tmp
, fontnames
[i
]);
629 for (j
= 0; j
< 14; j
++) {
633 while (*p
&& *p
!= '-')
639 * Font name is made up of fields 0 and 1, in reverse
640 * order with parentheses. (This is what the GTK 1.2 X
641 * font selector does, and it seems to come out
642 * looking reasonably sensible.)
645 p
+= 1 + sprintf(p
, "%s (%s)", components
[1], components
[0]);
648 * Charset is made up of fields 12 and 13.
651 p
+= 1 + sprintf(p
, "%s-%s", components
[12], components
[13]);
654 * Style is a mixture of quite a lot of the fields,
655 * with some strange formatting.
658 p
+= sprintf(p
, "%s", components
[2][0] ? components
[2] :
660 if (!g_strcasecmp(components
[3], "i"))
661 p
+= sprintf(p
, " italic");
662 else if (!g_strcasecmp(components
[3], "o"))
663 p
+= sprintf(p
, " oblique");
664 else if (!g_strcasecmp(components
[3], "ri"))
665 p
+= sprintf(p
, " reverse italic");
666 else if (!g_strcasecmp(components
[3], "ro"))
667 p
+= sprintf(p
, " reverse oblique");
668 else if (!g_strcasecmp(components
[3], "ot"))
669 p
+= sprintf(p
, " other-slant");
670 if (components
[4][0] && g_strcasecmp(components
[4], "normal"))
671 p
+= sprintf(p
, " %s", components
[4]);
672 if (!g_strcasecmp(components
[10], "m"))
673 p
+= sprintf(p
, " [M]");
674 if (!g_strcasecmp(components
[10], "c"))
675 p
+= sprintf(p
, " [C]");
676 if (components
[5][0])
677 p
+= sprintf(p
, " %s", components
[5]);
680 * Style key is the same stuff as above, but with a
681 * couple of transformations done on it to make it
682 * sort more sensibly.
686 if (!g_strcasecmp(components
[2], "medium") ||
687 !g_strcasecmp(components
[2], "regular") ||
688 !g_strcasecmp(components
[2], "normal") ||
689 !g_strcasecmp(components
[2], "book"))
691 else if (!g_strncasecmp(components
[2], "demi", 4) ||
692 !g_strncasecmp(components
[2], "semi", 4))
696 if (!g_strcasecmp(components
[3], "r"))
698 else if (!g_strncasecmp(components
[3], "r", 1))
702 if (!g_strcasecmp(components
[4], "normal"))
707 p
+= sprintf(p
, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
709 (int)strlen(components
[2]), components
[2],
711 (int)strlen(components
[3]), components
[3],
713 (int)strlen(components
[4]), components
[4],
714 (int)strlen(components
[10]), components
[10],
715 (int)strlen(components
[5]), components
[5]);
717 assert(p
- tmp
< thistmpsize
);
720 * Size is in pixels, for our application, so we
721 * derive it directly from the pixel size field,
724 fontsize
= atoi(components
[6]);
727 * Flags: we need to know whether this is a monospaced
728 * font, which we do by examining the spacing field
731 flags
= FONTFLAG_SERVERSIDE
;
732 if (!strchr("CcMm", components
[10][0]))
733 flags
|= FONTFLAG_NONMONOSPACED
;
736 * Not sure why, but sometimes the X server will
737 * deliver dummy font types in which fontsize comes
738 * out as zero. Filter those out.
741 callback(callback_ctx
, fontnames
[i
], font
, charset
,
742 style
, stylekey
, fontsize
, flags
, &x11font_vtable
);
745 * This isn't an XLFD, so it must be an alias.
746 * Transmit it with mostly null data.
748 * It would be nice to work out if it's monospaced
749 * here, but at the moment I can't see that being
750 * anything but computationally hideous. Ah well.
752 callback(callback_ctx
, fontnames
[i
], fontnames
[i
], NULL
,
753 NULL
, NULL
, 0, FONTFLAG_SERVERALIAS
, &x11font_vtable
);
756 XFreeFontNames(fontnames
);
759 static char *x11font_canonify_fontname(GtkWidget
*widget
, const char *name
,
760 int *size
, int *flags
,
764 * When given an X11 font name to try to make sense of for a
765 * font selector, we must attempt to load it (to see if it
766 * exists), and then canonify it by extracting its FONT
767 * property, which should give its full XLFD even if what we
768 * originally had was a wildcard.
770 * However, we must carefully avoid canonifying font
771 * _aliases_, unless specifically asked to, because the font
772 * selector treats them as worthwhile in their own right.
775 Display
*disp
= GDK_DISPLAY();
776 Atom fontprop
, fontprop2
;
779 xfs
= XLoadQueryFont(disp
, name
);
782 return NULL
; /* didn't make sense to us, sorry */
784 fontprop
= XInternAtom(disp
, "FONT", False
);
786 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
787 char *newname
= XGetAtomName(disp
, (Atom
)ret
);
789 unsigned long fsize
= 12;
791 fontprop2
= XInternAtom(disp
, "PIXEL_SIZE", False
);
792 if (XGetFontProperty(xfs
, fontprop2
, &fsize
) && fsize
> 0) {
794 XFreeFont(disp
, xfs
);
796 if (name
[0] == '-' || resolve_aliases
)
797 *flags
= FONTFLAG_SERVERSIDE
;
799 *flags
= FONTFLAG_SERVERALIAS
;
801 return dupstr(name
[0] == '-' || resolve_aliases ?
807 XFreeFont(disp
, xfs
);
809 return NULL
; /* something went wrong */
812 static char *x11font_scale_fontname(GtkWidget
*widget
, const char *name
,
815 return NULL
; /* shan't */
818 #if GTK_CHECK_VERSION(2,0,0)
820 /* ----------------------------------------------------------------------
821 * Pango font implementation (for GTK 2 only).
824 #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
825 #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
828 static int pangofont_has_glyph(unifont
*font
, wchar_t glyph
);
829 static void pangofont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
830 int x
, int y
, const wchar_t *string
, int len
,
831 int wide
, int bold
, int cellwidth
);
832 static unifont
*pangofont_create(GtkWidget
*widget
, const char *name
,
834 int shadowoffset
, int shadowalways
);
835 static unifont
*pangofont_create_fallback(GtkWidget
*widget
, int height
,
837 int shadowoffset
, int shadowalways
);
838 static void pangofont_destroy(unifont
*font
);
839 static void pangofont_enum_fonts(GtkWidget
*widget
, fontsel_add_entry callback
,
841 static char *pangofont_canonify_fontname(GtkWidget
*widget
, const char *name
,
842 int *size
, int *flags
,
843 int resolve_aliases
);
844 static char *pangofont_scale_fontname(GtkWidget
*widget
, const char *name
,
852 PangoFontDescription
*desc
;
855 * The containing widget.
859 * Data passed in to unifont_create().
861 int bold
, shadowoffset
, shadowalways
;
864 static const struct unifont_vtable pangofont_vtable
= {
866 pangofont_create_fallback
,
870 pangofont_enum_fonts
,
871 pangofont_canonify_fontname
,
872 pangofont_scale_fontname
,
877 * This function is used to rigorously validate a
878 * PangoFontDescription. Later versions of Pango have a nasty
879 * habit of accepting _any_ old string as input to
880 * pango_font_description_from_string and returning a font
881 * description which can actually be used to display text, even if
882 * they have to do it by falling back to their most default font.
883 * This is doubtless helpful in some situations, but not here,
884 * because we need to know if a Pango font string actually _makes
885 * sense_ in order to fall back to treating it as an X font name
886 * if it doesn't. So we check that the font family is actually one
887 * supported by Pango.
889 static int pangofont_check_desc_makes_sense(PangoContext
*ctx
,
890 PangoFontDescription
*desc
)
892 #ifndef PANGO_PRE_1POINT6
895 PangoFontFamily
**families
;
896 int i
, nfamilies
, matched
;
899 * Ask Pango for a list of font families, and iterate through
900 * them to see if one of them matches the family in the
901 * PangoFontDescription.
903 #ifndef PANGO_PRE_1POINT6
904 map
= pango_context_get_font_map(ctx
);
907 pango_font_map_list_families(map
, &families
, &nfamilies
);
909 pango_context_list_families(ctx
, &families
, &nfamilies
);
913 for (i
= 0; i
< nfamilies
; i
++) {
914 if (!g_strcasecmp(pango_font_family_get_name(families
[i
]),
915 pango_font_description_get_family(desc
))) {
925 static unifont
*pangofont_create_internal(GtkWidget
*widget
,
927 PangoFontDescription
*desc
,
929 int shadowoffset
, int shadowalways
)
931 struct pangofont
*pfont
;
932 #ifndef PANGO_PRE_1POINT6
936 PangoFontMetrics
*metrics
;
938 #ifndef PANGO_PRE_1POINT6
939 map
= pango_context_get_font_map(ctx
);
941 pango_font_description_free(desc
);
944 fset
= pango_font_map_load_fontset(map
, ctx
, desc
,
945 pango_context_get_language(ctx
));
947 fset
= pango_context_load_fontset(ctx
, desc
,
948 pango_context_get_language(ctx
));
951 pango_font_description_free(desc
);
954 metrics
= pango_fontset_get_metrics(fset
);
956 pango_font_metrics_get_approximate_digit_width(metrics
) == 0) {
957 pango_font_description_free(desc
);
958 g_object_unref(fset
);
962 pfont
= snew(struct pangofont
);
963 pfont
->u
.vt
= &pangofont_vtable
;
965 PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics
));
966 pfont
->u
.ascent
= PANGO_PIXELS(pango_font_metrics_get_ascent(metrics
));
967 pfont
->u
.descent
= PANGO_PIXELS(pango_font_metrics_get_descent(metrics
));
968 pfont
->u
.height
= pfont
->u
.ascent
+ pfont
->u
.descent
;
969 pfont
->u
.want_fallback
= FALSE
;
970 /* The Pango API is hardwired to UTF-8 */
971 pfont
->u
.public_charset
= CS_UTF8
;
974 pfont
->widget
= widget
;
976 pfont
->shadowoffset
= shadowoffset
;
977 pfont
->shadowalways
= shadowalways
;
979 pango_font_metrics_unref(metrics
);
981 return (unifont
*)pfont
;
984 static unifont
*pangofont_create(GtkWidget
*widget
, const char *name
,
986 int shadowoffset
, int shadowalways
)
989 PangoFontDescription
*desc
;
991 desc
= pango_font_description_from_string(name
);
994 ctx
= gtk_widget_get_pango_context(widget
);
996 pango_font_description_free(desc
);
999 if (!pangofont_check_desc_makes_sense(ctx
, desc
)) {
1000 pango_font_description_free(desc
);
1003 return pangofont_create_internal(widget
, ctx
, desc
, wide
, bold
,
1004 shadowoffset
, shadowalways
);
1007 static unifont
*pangofont_create_fallback(GtkWidget
*widget
, int height
,
1009 int shadowoffset
, int shadowalways
)
1012 PangoFontDescription
*desc
;
1014 desc
= pango_font_description_from_string("Monospace");
1017 ctx
= gtk_widget_get_pango_context(widget
);
1019 pango_font_description_free(desc
);
1022 pango_font_description_set_absolute_size(desc
, height
* PANGO_SCALE
);
1023 return pangofont_create_internal(widget
, ctx
, desc
, wide
, bold
,
1024 shadowoffset
, shadowalways
);
1027 static void pangofont_destroy(unifont
*font
)
1029 struct pangofont
*pfont
= (struct pangofont
*)font
;
1030 pango_font_description_free(pfont
->desc
);
1031 g_object_unref(pfont
->fset
);
1035 static int pangofont_has_glyph(unifont
*font
, wchar_t glyph
)
1037 /* Pango implements font fallback, so assume it has everything */
1041 static void pangofont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1042 int x
, int y
, const wchar_t *string
, int len
,
1043 int wide
, int bold
, int cellwidth
)
1045 struct pangofont
*pfont
= (struct pangofont
*)font
;
1046 PangoLayout
*layout
;
1047 PangoRectangle rect
;
1048 char *utfstring
, *utfptr
;
1050 int shadowbold
= FALSE
;
1055 y
-= pfont
->u
.ascent
;
1057 layout
= pango_layout_new(gtk_widget_get_pango_context(pfont
->widget
));
1058 pango_layout_set_font_description(layout
, pfont
->desc
);
1059 if (bold
> pfont
->bold
) {
1060 if (pfont
->shadowalways
)
1063 PangoFontDescription
*desc2
=
1064 pango_font_description_copy_static(pfont
->desc
);
1065 pango_font_description_set_weight(desc2
, PANGO_WEIGHT_BOLD
);
1066 pango_layout_set_font_description(layout
, desc2
);
1071 * Pango always expects UTF-8, so convert the input wide character
1074 utfstring
= snewn(len
*6+1, char); /* UTF-8 has max 6 bytes/char */
1075 utflen
= wc_to_mb(CS_UTF8
, 0, string
, len
,
1076 utfstring
, len
*6+1, ".", NULL
, NULL
);
1079 while (utflen
> 0) {
1083 * We want to display every character from this string in
1084 * the centre of its own character cell. In the worst case,
1085 * this requires a separate text-drawing call for each
1086 * character; but in the common case where the font is
1087 * properly fixed-width, we can draw many characters in one
1088 * go which is much faster.
1090 * This still isn't really ideal. If you look at what
1091 * happens in the X protocol as a result of all of this, you
1092 * find - naturally enough - that each call to
1093 * gdk_draw_layout() generates a separate set of X RENDER
1094 * operations involving creating a picture, setting a clip
1095 * rectangle, doing some drawing and undoing the whole lot.
1096 * In an ideal world, we should _always_ be able to turn the
1097 * contents of this loop into a single RenderCompositeGlyphs
1098 * operation which internally specifies inter-character
1099 * deltas to get the spacing right, which would give us full
1100 * speed _even_ in the worst case of a non-fixed-width font.
1101 * However, Pango's architecture and documentation are so
1102 * unhelpful that I have no idea how if at all to persuade
1107 * Start by extracting a single UTF-8 character from the
1111 while (clen
< utflen
&&
1112 (unsigned char)utfptr
[clen
] >= 0x80 &&
1113 (unsigned char)utfptr
[clen
] < 0xC0)
1118 * See if that character has the width we expect.
1120 pango_layout_set_text(layout
, utfptr
, clen
);
1121 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1123 if (rect
.width
== cellwidth
) {
1125 * Try extracting more characters, for as long as they
1126 * stay well-behaved.
1128 while (clen
< utflen
) {
1130 clen
++; /* skip UTF-8 introducer byte */
1131 while (clen
< utflen
&&
1132 (unsigned char)utfptr
[clen
] >= 0x80 &&
1133 (unsigned char)utfptr
[clen
] < 0xC0)
1136 pango_layout_set_text(layout
, utfptr
, clen
);
1137 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1138 if (rect
.width
!= n
* cellwidth
) {
1146 pango_layout_set_text(layout
, utfptr
, clen
);
1147 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1148 gdk_draw_layout(target
, gc
, x
+ (n
*cellwidth
- rect
.width
)/2,
1149 y
+ (pfont
->u
.height
- rect
.height
)/2, layout
);
1151 gdk_draw_layout(target
, gc
, x
+ (n
*cellwidth
- rect
.width
)/2 + pfont
->shadowoffset
,
1152 y
+ (pfont
->u
.height
- rect
.height
)/2, layout
);
1161 g_object_unref(layout
);
1165 * Dummy size value to be used when converting a
1166 * PangoFontDescription of a scalable font to a string for
1169 #define PANGO_DUMMY_SIZE 12
1171 static void pangofont_enum_fonts(GtkWidget
*widget
, fontsel_add_entry callback
,
1175 #ifndef PANGO_PRE_1POINT6
1178 PangoFontFamily
**families
;
1181 ctx
= gtk_widget_get_pango_context(widget
);
1186 * Ask Pango for a list of font families, and iterate through
1189 #ifndef PANGO_PRE_1POINT6
1190 map
= pango_context_get_font_map(ctx
);
1193 pango_font_map_list_families(map
, &families
, &nfamilies
);
1195 pango_context_list_families(ctx
, &families
, &nfamilies
);
1197 for (i
= 0; i
< nfamilies
; i
++) {
1198 PangoFontFamily
*family
= families
[i
];
1199 const char *familyname
;
1201 PangoFontFace
**faces
;
1205 * Set up our flags for this font family, and get the name
1208 flags
= FONTFLAG_CLIENTSIDE
;
1209 #ifndef PANGO_PRE_1POINT4
1211 * In very early versions of Pango, we can't tell
1212 * monospaced fonts from non-monospaced.
1214 if (!pango_font_family_is_monospace(family
))
1215 flags
|= FONTFLAG_NONMONOSPACED
;
1217 familyname
= pango_font_family_get_name(family
);
1220 * Go through the available font faces in this family.
1222 pango_font_family_list_faces(family
, &faces
, &nfaces
);
1223 for (j
= 0; j
< nfaces
; j
++) {
1224 PangoFontFace
*face
= faces
[j
];
1225 PangoFontDescription
*desc
;
1226 const char *facename
;
1228 int k
, nsizes
, dummysize
;
1231 * Get the face name string.
1233 facename
= pango_font_face_get_face_name(face
);
1236 * Set up a font description with what we've got so
1237 * far. We'll fill in the size field manually and then
1238 * call pango_font_description_to_string() to give the
1239 * full real name of the specific font.
1241 desc
= pango_font_face_describe(face
);
1244 * See if this font has a list of specific sizes.
1246 #ifndef PANGO_PRE_1POINT4
1247 pango_font_face_list_sizes(face
, &sizes
, &nsizes
);
1250 * In early versions of Pango, that call wasn't
1251 * supported; we just have to assume everything is
1258 * Write a single entry with a dummy size.
1260 dummysize
= PANGO_DUMMY_SIZE
* PANGO_SCALE
;
1266 * If so, go through them one by one.
1268 for (k
= 0; k
< nsizes
; k
++) {
1272 pango_font_description_set_size(desc
, sizes
[k
]);
1274 fullname
= pango_font_description_to_string(desc
);
1277 * Construct the sorting key for font styles.
1283 n
= pango_font_description_get_weight(desc
);
1284 /* Weight: normal, then lighter, then bolder */
1285 if (n
<= PANGO_WEIGHT_NORMAL
)
1286 n
= PANGO_WEIGHT_NORMAL
- n
;
1287 p
+= sprintf(p
, "%4d", n
);
1289 n
= pango_font_description_get_style(desc
);
1290 p
+= sprintf(p
, " %2d", n
);
1292 n
= pango_font_description_get_stretch(desc
);
1293 /* Stretch: closer to normal sorts earlier */
1294 n
= 2 * abs(PANGO_STRETCH_NORMAL
- n
) +
1295 (n
< PANGO_STRETCH_NORMAL
);
1296 p
+= sprintf(p
, " %2d", n
);
1298 n
= pango_font_description_get_variant(desc
);
1299 p
+= sprintf(p
, " %2d", n
);
1304 * Got everything. Hand off to the callback.
1305 * (The charset string is NULL, because only
1306 * server-side X fonts use it.)
1308 callback(callback_ctx
, fullname
, familyname
, NULL
, facename
,
1310 (sizes
== &dummysize ?
0 : PANGO_PIXELS(sizes
[k
])),
1311 flags
, &pangofont_vtable
);
1315 if (sizes
!= &dummysize
)
1318 pango_font_description_free(desc
);
1325 static char *pangofont_canonify_fontname(GtkWidget
*widget
, const char *name
,
1326 int *size
, int *flags
,
1327 int resolve_aliases
)
1330 * When given a Pango font name to try to make sense of for a
1331 * font selector, we must normalise it to PANGO_DUMMY_SIZE and
1332 * extract its original size (in pixels) into the `size' field.
1335 #ifndef PANGO_PRE_1POINT6
1338 PangoFontDescription
*desc
;
1340 PangoFontMetrics
*metrics
;
1341 char *newname
, *retname
;
1343 desc
= pango_font_description_from_string(name
);
1346 ctx
= gtk_widget_get_pango_context(widget
);
1348 pango_font_description_free(desc
);
1351 if (!pangofont_check_desc_makes_sense(ctx
, desc
)) {
1352 pango_font_description_free(desc
);
1355 #ifndef PANGO_PRE_1POINT6
1356 map
= pango_context_get_font_map(ctx
);
1358 pango_font_description_free(desc
);
1361 fset
= pango_font_map_load_fontset(map
, ctx
, desc
,
1362 pango_context_get_language(ctx
));
1364 fset
= pango_context_load_fontset(ctx
, desc
,
1365 pango_context_get_language(ctx
));
1368 pango_font_description_free(desc
);
1371 metrics
= pango_fontset_get_metrics(fset
);
1373 pango_font_metrics_get_approximate_digit_width(metrics
) == 0) {
1374 pango_font_description_free(desc
);
1375 g_object_unref(fset
);
1379 *size
= PANGO_PIXELS(pango_font_description_get_size(desc
));
1380 *flags
= FONTFLAG_CLIENTSIDE
;
1381 pango_font_description_set_size(desc
, PANGO_DUMMY_SIZE
* PANGO_SCALE
);
1382 newname
= pango_font_description_to_string(desc
);
1383 retname
= dupstr(newname
);
1386 pango_font_metrics_unref(metrics
);
1387 pango_font_description_free(desc
);
1388 g_object_unref(fset
);
1393 static char *pangofont_scale_fontname(GtkWidget
*widget
, const char *name
,
1396 PangoFontDescription
*desc
;
1397 char *newname
, *retname
;
1399 desc
= pango_font_description_from_string(name
);
1402 pango_font_description_set_size(desc
, size
* PANGO_SCALE
);
1403 newname
= pango_font_description_to_string(desc
);
1404 retname
= dupstr(newname
);
1406 pango_font_description_free(desc
);
1411 #endif /* GTK_CHECK_VERSION(2,0,0) */
1413 /* ----------------------------------------------------------------------
1414 * Outermost functions which do the vtable dispatch.
1418 * Complete list of font-type subclasses. Listed in preference
1419 * order for unifont_create(). (That is, in the extremely unlikely
1420 * event that the same font name is valid as both a Pango and an
1421 * X11 font, it will be interpreted as the former in the absence
1422 * of an explicit type-disambiguating prefix.)
1424 * The 'multifont' subclass is omitted here, as discussed above.
1426 static const struct unifont_vtable
*unifont_types
[] = {
1427 #if GTK_CHECK_VERSION(2,0,0)
1434 * Function which takes a font name and processes the optional
1435 * scheme prefix. Returns the tail of the font name suitable for
1436 * passing to individual font scheme functions, and also provides
1437 * a subrange of the unifont_types[] array above.
1439 * The return values `start' and `end' denote a half-open interval
1440 * in unifont_types[]; that is, the correct way to iterate over
1443 * for (i = start; i < end; i++) {...}
1445 static const char *unifont_do_prefix(const char *name
, int *start
, int *end
)
1447 int colonpos
= strcspn(name
, ":");
1450 if (name
[colonpos
]) {
1452 * There's a colon prefix on the font name. Use it to work
1453 * out which subclass to use.
1455 for (i
= 0; i
< lenof(unifont_types
); i
++) {
1456 if (strlen(unifont_types
[i
]->prefix
) == colonpos
&&
1457 !strncmp(unifont_types
[i
]->prefix
, name
, colonpos
)) {
1460 return name
+ colonpos
+ 1;
1464 * None matched, so return an empty scheme list to prevent
1465 * any scheme from being called at all.
1468 return name
+ colonpos
+ 1;
1471 * No colon prefix, so just use all the subclasses.
1474 *end
= lenof(unifont_types
);
1479 unifont
*unifont_create(GtkWidget
*widget
, const char *name
, int wide
,
1480 int bold
, int shadowoffset
, int shadowalways
)
1484 name
= unifont_do_prefix(name
, &start
, &end
);
1486 for (i
= start
; i
< end
; i
++) {
1487 unifont
*ret
= unifont_types
[i
]->create(widget
, name
, wide
, bold
,
1488 shadowoffset
, shadowalways
);
1492 return NULL
; /* font not found in any scheme */
1495 void unifont_destroy(unifont
*font
)
1497 font
->vt
->destroy(font
);
1500 void unifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1501 int x
, int y
, const wchar_t *string
, int len
,
1502 int wide
, int bold
, int cellwidth
)
1504 font
->vt
->draw_text(target
, gc
, font
, x
, y
, string
, len
,
1505 wide
, bold
, cellwidth
);
1508 /* ----------------------------------------------------------------------
1509 * Multiple-font wrapper. This is a type of unifont which encapsulates
1510 * up to two other unifonts, permitting missing glyphs in the main
1511 * font to be filled in by a fallback font.
1513 * This is a type of unifont just like the previous two, but it has a
1514 * separate constructor which is manually called by the client, so it
1515 * doesn't appear in the list of available font types enumerated by
1516 * unifont_create. This means it's not used by unifontsel either, so
1517 * it doesn't need to support any methods except draw_text and
1521 static void multifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1522 int x
, int y
, const wchar_t *string
, int len
,
1523 int wide
, int bold
, int cellwidth
);
1524 static void multifont_destroy(unifont
*font
);
1532 static const struct unifont_vtable multifont_vtable
= {
1533 NULL
, /* creation is done specially */
1537 multifont_draw_text
,
1544 unifont
*multifont_create(GtkWidget
*widget
, const char *name
,
1546 int shadowoffset
, int shadowalways
)
1549 unifont
*font
, *fallback
;
1550 struct multifont
*mfont
;
1552 font
= unifont_create(widget
, name
, wide
, bold
,
1553 shadowoffset
, shadowalways
);
1557 if (font
->want_fallback
) {
1558 for (i
= 0; i
< lenof(unifont_types
); i
++) {
1559 if (unifont_types
[i
]->create_fallback
) {
1560 fallback
= unifont_types
[i
]->create_fallback
1561 (widget
, font
->height
, wide
, bold
,
1562 shadowoffset
, shadowalways
);
1570 * Construct our multifont. Public members are all copied from the
1571 * primary font we're wrapping.
1573 mfont
= snew(struct multifont
);
1574 mfont
->u
.vt
= &multifont_vtable
;
1575 mfont
->u
.width
= font
->width
;
1576 mfont
->u
.ascent
= font
->ascent
;
1577 mfont
->u
.descent
= font
->descent
;
1578 mfont
->u
.height
= font
->height
;
1579 mfont
->u
.public_charset
= font
->public_charset
;
1580 mfont
->u
.want_fallback
= FALSE
; /* shouldn't be needed, but just in case */
1582 mfont
->fallback
= fallback
;
1584 return (unifont
*)mfont
;
1587 static void multifont_destroy(unifont
*font
)
1589 struct multifont
*mfont
= (struct multifont
*)font
;
1590 unifont_destroy(mfont
->main
);
1591 if (mfont
->fallback
)
1592 unifont_destroy(mfont
->fallback
);
1596 static void multifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1597 int x
, int y
, const wchar_t *string
, int len
,
1598 int wide
, int bold
, int cellwidth
)
1600 struct multifont
*mfont
= (struct multifont
*)font
;
1605 * Find a maximal sequence of characters which are, or are
1606 * not, supported by our main font.
1608 ok
= mfont
->main
->vt
->has_glyph(mfont
->main
, string
[0]);
1611 !mfont
->main
->vt
->has_glyph(mfont
->main
, string
[i
]) == !ok
;
1617 unifont_draw_text(target
, gc
, ok ? mfont
->main
: mfont
->fallback
,
1618 x
, y
, string
, i
, wide
, bold
, cellwidth
);
1625 #if GTK_CHECK_VERSION(2,0,0)
1627 /* ----------------------------------------------------------------------
1628 * Implementation of a unified font selector. Used on GTK 2 only;
1629 * for GTK 1 we still use the standard font selector.
1632 typedef struct fontinfo fontinfo
;
1634 typedef struct unifontsel_internal
{
1635 /* This must be the structure's first element, for cross-casting */
1637 GtkListStore
*family_model
, *style_model
, *size_model
;
1638 GtkWidget
*family_list
, *style_list
, *size_entry
, *size_list
;
1639 GtkWidget
*filter_buttons
[4];
1640 GtkWidget
*preview_area
;
1641 GdkPixmap
*preview_pixmap
;
1642 int preview_width
, preview_height
;
1643 GdkColor preview_fg
, preview_bg
;
1645 tree234
*fonts_by_realname
, *fonts_by_selorder
;
1647 int selsize
, intendedsize
;
1648 int inhibit_response
; /* inhibit callbacks when we change GUI controls */
1649 } unifontsel_internal
;
1652 * The structure held in the tree234s. All the string members are
1653 * part of the same allocated area, so don't need freeing
1658 char *family
, *charset
, *style
, *stylekey
;
1661 * Fallback sorting key, to permit multiple identical entries
1662 * to exist in the selorder tree.
1666 * Indices mapping fontinfo structures to indices in the list
1667 * boxes. sizeindex is irrelevant if the font is scalable
1670 int familyindex
, styleindex
, sizeindex
;
1672 * The class of font.
1674 const struct unifont_vtable
*fontclass
;
1677 struct fontinfo_realname_find
{
1678 const char *realname
;
1682 static int strnullcasecmp(const char *a
, const char *b
)
1687 * If exactly one of the inputs is NULL, it compares before
1690 if ((i
= (!b
) - (!a
)) != 0)
1694 * NULL compares equal.
1700 * Otherwise, ordinary strcasecmp.
1702 return g_strcasecmp(a
, b
);
1705 static int fontinfo_realname_compare(void *av
, void *bv
)
1707 fontinfo
*a
= (fontinfo
*)av
;
1708 fontinfo
*b
= (fontinfo
*)bv
;
1711 if ((i
= strnullcasecmp(a
->realname
, b
->realname
)) != 0)
1713 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1714 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1715 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1719 static int fontinfo_realname_find(void *av
, void *bv
)
1721 struct fontinfo_realname_find
*a
= (struct fontinfo_realname_find
*)av
;
1722 fontinfo
*b
= (fontinfo
*)bv
;
1725 if ((i
= strnullcasecmp(a
->realname
, b
->realname
)) != 0)
1727 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1728 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1729 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1733 static int fontinfo_selorder_compare(void *av
, void *bv
)
1735 fontinfo
*a
= (fontinfo
*)av
;
1736 fontinfo
*b
= (fontinfo
*)bv
;
1738 if ((i
= strnullcasecmp(a
->family
, b
->family
)) != 0)
1741 * Font class comes immediately after family, so that fonts
1742 * from different classes with the same family
1744 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1745 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1746 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1747 if ((i
= strnullcasecmp(a
->charset
, b
->charset
)) != 0)
1749 if ((i
= strnullcasecmp(a
->stylekey
, b
->stylekey
)) != 0)
1751 if ((i
= strnullcasecmp(a
->style
, b
->style
)) != 0)
1753 if (a
->size
!= b
->size
)
1754 return (a
->size
< b
->size ?
-1 : +1);
1755 if (a
->index
!= b
->index
)
1756 return (a
->index
< b
->index ?
-1 : +1);
1760 static void unifontsel_deselect(unifontsel_internal
*fs
)
1762 fs
->selected
= NULL
;
1763 gtk_list_store_clear(fs
->style_model
);
1764 gtk_list_store_clear(fs
->size_model
);
1765 gtk_widget_set_sensitive(fs
->u
.ok_button
, FALSE
);
1766 gtk_widget_set_sensitive(fs
->size_entry
, FALSE
);
1769 static void unifontsel_setup_familylist(unifontsel_internal
*fs
)
1772 int i
, listindex
, minpos
= -1, maxpos
= -1;
1773 char *currfamily
= NULL
;
1777 gtk_list_store_clear(fs
->family_model
);
1781 * Search through the font tree for anything matching our
1782 * current filter criteria. When we find one, add its font
1783 * name to the list box.
1785 for (i
= 0 ;; i
++) {
1786 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1788 * info may be NULL if we've just run off the end of the
1789 * tree. We must still do a processing pass in that
1790 * situation, in case we had an unfinished font record in
1793 if (info
&& (info
->flags
&~ fs
->filter_flags
)) {
1794 info
->familyindex
= -1;
1795 continue; /* we're filtering out this font */
1797 if (!info
|| strnullcasecmp(currfamily
, info
->family
) ||
1798 currflags
!= (info
->flags
& FONTFLAG_SORT_MASK
)) {
1800 * We've either finished a family, or started a new
1804 gtk_list_store_append(fs
->family_model
, &iter
);
1805 gtk_list_store_set(fs
->family_model
, &iter
,
1806 0, currfamily
, 1, minpos
, 2, maxpos
+1, -1);
1811 currfamily
= info
->family
;
1812 currflags
= info
->flags
& FONTFLAG_SORT_MASK
;
1816 break; /* now we're done */
1817 info
->familyindex
= listindex
;
1822 * If we've just filtered out the previously selected font,
1823 * deselect it thoroughly.
1825 if (fs
->selected
&& fs
->selected
->familyindex
< 0)
1826 unifontsel_deselect(fs
);
1829 static void unifontsel_setup_stylelist(unifontsel_internal
*fs
,
1833 int i
, listindex
, minpos
= -1, maxpos
= -1, started
= FALSE
;
1834 char *currcs
= NULL
, *currstyle
= NULL
;
1837 gtk_list_store_clear(fs
->style_model
);
1842 * Search through the font tree for anything matching our
1843 * current filter criteria. When we find one, add its charset
1844 * and/or style name to the list box.
1846 for (i
= start
; i
<= end
; i
++) {
1850 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1852 * info may be NULL if we've just run off the end of the
1853 * relevant data. We must still do a processing pass in
1854 * that situation, in case we had an unfinished font
1855 * record in progress.
1857 if (info
&& (info
->flags
&~ fs
->filter_flags
)) {
1858 info
->styleindex
= -1;
1859 continue; /* we're filtering out this font */
1861 if (!info
|| !started
|| strnullcasecmp(currcs
, info
->charset
) ||
1862 strnullcasecmp(currstyle
, info
->style
)) {
1864 * We've either finished a style/charset, or started a
1869 gtk_list_store_append(fs
->style_model
, &iter
);
1870 gtk_list_store_set(fs
->style_model
, &iter
,
1871 0, currstyle
, 1, minpos
, 2, maxpos
+1,
1877 if (info
->charset
&& strnullcasecmp(currcs
, info
->charset
)) {
1878 gtk_list_store_append(fs
->style_model
, &iter
);
1879 gtk_list_store_set(fs
->style_model
, &iter
,
1880 0, info
->charset
, 1, -1, 2, -1,
1884 currcs
= info
->charset
;
1885 currstyle
= info
->style
;
1889 break; /* now we're done */
1890 info
->styleindex
= listindex
;
1895 static const int unifontsel_default_sizes
[] = { 10, 12, 14, 16, 20, 24, 32 };
1897 static void unifontsel_setup_sizelist(unifontsel_internal
*fs
,
1905 gtk_list_store_clear(fs
->size_model
);
1909 * Search through the font tree for anything matching our
1910 * current filter criteria. When we find one, add its font
1911 * name to the list box.
1913 for (i
= start
; i
< end
; i
++) {
1914 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1915 if (info
->flags
&~ fs
->filter_flags
) {
1916 info
->sizeindex
= -1;
1917 continue; /* we're filtering out this font */
1920 sprintf(sizetext
, "%d", info
->size
);
1921 info
->sizeindex
= listindex
;
1922 gtk_list_store_append(fs
->size_model
, &iter
);
1923 gtk_list_store_set(fs
->size_model
, &iter
,
1924 0, sizetext
, 1, i
, 2, info
->size
, -1);
1932 for (j
= 0; j
< lenof(unifontsel_default_sizes
); j
++) {
1933 sprintf(sizetext
, "%d", unifontsel_default_sizes
[j
]);
1934 gtk_list_store_append(fs
->size_model
, &iter
);
1935 gtk_list_store_set(fs
->size_model
, &iter
, 0, sizetext
, 1, i
,
1936 2, unifontsel_default_sizes
[j
], -1);
1943 static void unifontsel_set_filter_buttons(unifontsel_internal
*fs
)
1947 for (i
= 0; i
< lenof(fs
->filter_buttons
); i
++) {
1948 int flagbit
= GPOINTER_TO_INT(gtk_object_get_data
1949 (GTK_OBJECT(fs
->filter_buttons
[i
]),
1951 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs
->filter_buttons
[i
]),
1952 !!(fs
->filter_flags
& flagbit
));
1956 static void unifontsel_draw_preview_text(unifontsel_internal
*fs
)
1959 char *sizename
= NULL
;
1960 fontinfo
*info
= fs
->selected
;
1963 sizename
= info
->fontclass
->scale_fontname
1964 (GTK_WIDGET(fs
->u
.window
), info
->realname
, fs
->selsize
);
1965 font
= info
->fontclass
->create(GTK_WIDGET(fs
->u
.window
),
1966 sizename ? sizename
: info
->realname
,
1967 FALSE
, FALSE
, 0, 0);
1971 if (fs
->preview_pixmap
) {
1972 GdkGC
*gc
= gdk_gc_new(fs
->preview_pixmap
);
1973 gdk_gc_set_foreground(gc
, &fs
->preview_bg
);
1974 gdk_draw_rectangle(fs
->preview_pixmap
, gc
, 1, 0, 0,
1975 fs
->preview_width
, fs
->preview_height
);
1976 gdk_gc_set_foreground(gc
, &fs
->preview_fg
);
1979 * The pangram used here is rather carefully
1980 * constructed: it contains a sequence of very narrow
1981 * letters (`jil') and a pair of adjacent very wide
1984 * If the user selects a proportional font, it will be
1985 * coerced into fixed-width character cells when used
1986 * in the actual terminal window. We therefore display
1987 * it the same way in the preview pane, so as to show
1988 * it the way it will actually be displayed - and we
1989 * deliberately pick a pangram which will show the
1990 * resulting miskerning at its worst.
1992 * We aren't trying to sell people these fonts; we're
1993 * trying to let them make an informed choice. Better
1994 * that they find out the problems with using
1995 * proportional fonts in terminal windows here than
1996 * that they go to the effort of selecting their font
1997 * and _then_ realise it was a mistake.
1999 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2001 L
"bankrupt jilted showmen quiz convex fogey",
2002 41, FALSE
, FALSE
, font
->width
);
2003 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2004 0, font
->ascent
+ font
->height
,
2005 L
"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
2006 41, FALSE
, FALSE
, font
->width
);
2008 * The ordering of punctuation here is also selected
2009 * with some specific aims in mind. I put ` and '
2010 * together because some software (and people) still
2011 * use them as matched quotes no matter what Unicode
2012 * might say on the matter, so people can quickly
2013 * check whether they look silly in a candidate font.
2014 * The sequence #_@ is there to let people judge the
2015 * suitability of the underscore as an effectively
2016 * alphabetic character (since that's how it's often
2017 * used in practice, at least by programmers).
2019 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2020 0, font
->ascent
+ font
->height
* 2,
2021 L
"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
2022 42, FALSE
, FALSE
, font
->width
);
2025 gdk_window_invalidate_rect(fs
->preview_area
->window
, NULL
, FALSE
);
2028 info
->fontclass
->destroy(font
);
2033 static void unifontsel_select_font(unifontsel_internal
*fs
,
2034 fontinfo
*info
, int size
, int leftlist
,
2035 int size_is_explicit
)
2039 GtkTreePath
*treepath
;
2042 fs
->inhibit_response
= TRUE
;
2044 fs
->selected
= info
;
2046 if (size_is_explicit
)
2047 fs
->intendedsize
= size
;
2049 gtk_widget_set_sensitive(fs
->u
.ok_button
, TRUE
);
2052 * Find the index of this fontinfo in the selorder list.
2055 findpos234(fs
->fonts_by_selorder
, info
, NULL
, &index
);
2059 * Adjust the font selector flags and redo the font family
2060 * list box, if necessary.
2062 if (leftlist
<= 0 &&
2063 (fs
->filter_flags
| info
->flags
) != fs
->filter_flags
) {
2064 fs
->filter_flags
|= info
->flags
;
2065 unifontsel_set_filter_buttons(fs
);
2066 unifontsel_setup_familylist(fs
);
2070 * Find the appropriate family name and select it in the list.
2072 assert(info
->familyindex
>= 0);
2073 treepath
= gtk_tree_path_new_from_indices(info
->familyindex
, -1);
2074 gtk_tree_selection_select_path
2075 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->family_list
)),
2077 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->family_list
),
2078 treepath
, NULL
, FALSE
, 0.0, 0.0);
2079 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->family_model
), &iter
, treepath
);
2080 gtk_tree_path_free(treepath
);
2083 * Now set up the font style list.
2085 gtk_tree_model_get(GTK_TREE_MODEL(fs
->family_model
), &iter
,
2086 1, &minval
, 2, &maxval
, -1);
2088 unifontsel_setup_stylelist(fs
, minval
, maxval
);
2091 * Find the appropriate style name and select it in the list.
2094 assert(info
->styleindex
>= 0);
2095 treepath
= gtk_tree_path_new_from_indices(info
->styleindex
, -1);
2096 gtk_tree_selection_select_path
2097 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->style_list
)),
2099 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->style_list
),
2100 treepath
, NULL
, FALSE
, 0.0, 0.0);
2101 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->style_model
),
2103 gtk_tree_path_free(treepath
);
2106 * And set up the size list.
2108 gtk_tree_model_get(GTK_TREE_MODEL(fs
->style_model
), &iter
,
2109 1, &minval
, 2, &maxval
, -1);
2111 unifontsel_setup_sizelist(fs
, minval
, maxval
);
2114 * Find the appropriate size, and select it in the list.
2117 assert(info
->sizeindex
>= 0);
2118 treepath
= gtk_tree_path_new_from_indices(info
->sizeindex
, -1);
2119 gtk_tree_selection_select_path
2120 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->size_list
)),
2122 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->size_list
),
2123 treepath
, NULL
, FALSE
, 0.0, 0.0);
2124 gtk_tree_path_free(treepath
);
2128 for (j
= 0; j
< lenof(unifontsel_default_sizes
); j
++)
2129 if (unifontsel_default_sizes
[j
] == size
) {
2130 treepath
= gtk_tree_path_new_from_indices(j
, -1);
2131 gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs
->size_list
),
2132 treepath
, NULL
, FALSE
);
2133 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->size_list
),
2134 treepath
, NULL
, FALSE
, 0.0,
2136 gtk_tree_path_free(treepath
);
2141 * And set up the font size text entry box.
2145 sprintf(sizetext
, "%d", size
);
2146 gtk_entry_set_text(GTK_ENTRY(fs
->size_entry
), sizetext
);
2150 unifontsel_setup_sizelist(fs
, 0, 0);
2151 gtk_entry_set_text(GTK_ENTRY(fs
->size_entry
), "");
2155 * Grey out the font size edit box if we're not using a
2158 gtk_entry_set_editable(GTK_ENTRY(fs
->size_entry
), fs
->selected
->size
== 0);
2159 gtk_widget_set_sensitive(fs
->size_entry
, fs
->selected
->size
== 0);
2161 unifontsel_draw_preview_text(fs
);
2163 fs
->inhibit_response
= FALSE
;
2166 static void unifontsel_button_toggled(GtkToggleButton
*tb
, gpointer data
)
2168 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2169 int newstate
= gtk_toggle_button_get_active(tb
);
2171 int flagbit
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(tb
),
2175 newflags
= fs
->filter_flags
| flagbit
;
2177 newflags
= fs
->filter_flags
& ~flagbit
;
2179 if (fs
->filter_flags
!= newflags
) {
2180 fs
->filter_flags
= newflags
;
2181 unifontsel_setup_familylist(fs
);
2185 static void unifontsel_add_entry(void *ctx
, const char *realfontname
,
2186 const char *family
, const char *charset
,
2187 const char *style
, const char *stylekey
,
2188 int size
, int flags
,
2189 const struct unifont_vtable
*fontclass
)
2191 unifontsel_internal
*fs
= (unifontsel_internal
*)ctx
;
2196 totalsize
= sizeof(fontinfo
) + strlen(realfontname
) +
2197 (family ?
strlen(family
) : 0) + (charset ?
strlen(charset
) : 0) +
2198 (style ?
strlen(style
) : 0) + (stylekey ?
strlen(stylekey
) : 0) + 10;
2199 info
= (fontinfo
*)smalloc(totalsize
);
2200 info
->fontclass
= fontclass
;
2201 p
= (char *)info
+ sizeof(fontinfo
);
2203 strcpy(p
, realfontname
);
2210 info
->family
= NULL
;
2216 info
->charset
= NULL
;
2225 strcpy(p
, stylekey
);
2228 info
->stylekey
= NULL
;
2229 assert(p
- (char *)info
<= totalsize
);
2231 info
->flags
= flags
;
2232 info
->index
= count234(fs
->fonts_by_selorder
);
2235 * It's just conceivable that a misbehaving font enumerator
2236 * might tell us about the same font real name more than once,
2237 * in which case we should silently drop the new one.
2239 if (add234(fs
->fonts_by_realname
, info
) != info
) {
2244 * However, we should never get a duplicate key in the
2245 * selorder tree, because the index field carefully
2246 * disambiguates otherwise identical records.
2248 add234(fs
->fonts_by_selorder
, info
);
2251 static fontinfo
*update_for_intended_size(unifontsel_internal
*fs
,
2254 fontinfo info2
, *below
, *above
;
2258 * Copy the info structure. This doesn't copy its dynamic
2259 * string fields, but that's unimportant because all we're
2260 * going to do is to adjust the size field and use it in one
2264 info2
.size
= fs
->intendedsize
;
2267 * Search in the tree to find the fontinfo structure which
2268 * best approximates the size the user last requested.
2270 below
= findrelpos234(fs
->fonts_by_selorder
, &info2
, NULL
,
2272 above
= index234(fs
->fonts_by_selorder
, pos
+1);
2275 * See if we've found it exactly, which is an easy special
2276 * case. If we have, it'll be in `below' and not `above',
2277 * because we did a REL234_LE rather than REL234_LT search.
2279 if (!fontinfo_selorder_compare(&info2
, below
))
2283 * Now we've either found two suitable fonts, one smaller and
2284 * one larger, or we're at one or other extreme end of the
2285 * scale. Find out which, by NULLing out either of below and
2286 * above if it differs from this one in any respect but size
2287 * (and the disambiguating index field). Bear in mind, also,
2288 * that either one might _already_ be NULL if we're at the
2289 * extreme ends of the font list.
2292 info2
.size
= below
->size
;
2293 info2
.index
= below
->index
;
2294 if (fontinfo_selorder_compare(&info2
, below
))
2298 info2
.size
= above
->size
;
2299 info2
.index
= above
->index
;
2300 if (fontinfo_selorder_compare(&info2
, above
))
2305 * Now return whichever of above and below is non-NULL, if
2306 * that's unambiguous.
2314 * And now we really do have to make a choice about whether to
2315 * round up or down. We'll do it by rounding to nearest,
2316 * breaking ties by rounding up.
2318 if (above
->size
- fs
->intendedsize
<= fs
->intendedsize
- below
->size
)
2324 static void family_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2326 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2327 GtkTreeModel
*treemodel
;
2328 GtkTreeIter treeiter
;
2332 if (fs
->inhibit_response
) /* we made this change ourselves */
2335 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2338 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, -1);
2339 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2340 info
= update_for_intended_size(fs
, info
);
2342 return; /* _shouldn't_ happen unless font list is completely funted */
2344 fs
->selsize
= fs
->intendedsize
; /* font is scalable */
2345 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: fs
->selsize
,
2349 static void style_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2351 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2352 GtkTreeModel
*treemodel
;
2353 GtkTreeIter treeiter
;
2357 if (fs
->inhibit_response
) /* we made this change ourselves */
2360 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2363 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, -1);
2365 return; /* somehow a charset heading got clicked */
2366 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2367 info
= update_for_intended_size(fs
, info
);
2369 return; /* _shouldn't_ happen unless font list is completely funted */
2371 fs
->selsize
= fs
->intendedsize
; /* font is scalable */
2372 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: fs
->selsize
,
2376 static void size_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2378 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2379 GtkTreeModel
*treemodel
;
2380 GtkTreeIter treeiter
;
2384 if (fs
->inhibit_response
) /* we made this change ourselves */
2387 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2390 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, 2, &size
, -1);
2391 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2392 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: size
, 3, TRUE
);
2395 static void size_entry_changed(GtkEditable
*ed
, gpointer data
)
2397 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2401 if (fs
->inhibit_response
) /* we made this change ourselves */
2404 text
= gtk_entry_get_text(GTK_ENTRY(ed
));
2408 assert(fs
->selected
->size
== 0);
2409 unifontsel_select_font(fs
, fs
->selected
, size
, 3, TRUE
);
2413 static void alias_resolve(GtkTreeView
*treeview
, GtkTreePath
*path
,
2414 GtkTreeViewColumn
*column
, gpointer data
)
2416 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2418 int minval
, newsize
;
2419 fontinfo
*info
, *newinfo
;
2422 if (fs
->inhibit_response
) /* we made this change ourselves */
2425 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->family_model
), &iter
, path
);
2426 gtk_tree_model_get(GTK_TREE_MODEL(fs
->family_model
), &iter
, 1,&minval
, -1);
2427 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2430 struct fontinfo_realname_find f
;
2432 newname
= info
->fontclass
->canonify_fontname
2433 (GTK_WIDGET(fs
->u
.window
), info
->realname
, &newsize
, &flags
, TRUE
);
2435 f
.realname
= newname
;
2437 newinfo
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2441 return; /* font name not in our index */
2442 if (newinfo
== info
)
2443 return; /* didn't change under canonification => not an alias */
2444 unifontsel_select_font(fs
, newinfo
,
2445 newinfo
->size ? newinfo
->size
: newsize
,
2450 static gint
unifontsel_expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
2453 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2455 if (fs
->preview_pixmap
) {
2456 gdk_draw_pixmap(widget
->window
,
2457 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
2459 event
->area
.x
, event
->area
.y
,
2460 event
->area
.x
, event
->area
.y
,
2461 event
->area
.width
, event
->area
.height
);
2466 static gint
unifontsel_configure_area(GtkWidget
*widget
,
2467 GdkEventConfigure
*event
, gpointer data
)
2469 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2470 int ox
, oy
, nx
, ny
, x
, y
;
2473 * Enlarge the pixmap, but never shrink it.
2475 ox
= fs
->preview_width
;
2476 oy
= fs
->preview_height
;
2479 if (x
> ox
|| y
> oy
) {
2480 if (fs
->preview_pixmap
)
2481 gdk_pixmap_unref(fs
->preview_pixmap
);
2483 nx
= (x
> ox ? x
: ox
);
2484 ny
= (y
> oy ? y
: oy
);
2485 fs
->preview_pixmap
= gdk_pixmap_new(widget
->window
, nx
, ny
, -1);
2486 fs
->preview_width
= nx
;
2487 fs
->preview_height
= ny
;
2489 unifontsel_draw_preview_text(fs
);
2492 gdk_window_invalidate_rect(widget
->window
, NULL
, FALSE
);
2497 unifontsel
*unifontsel_new(const char *wintitle
)
2499 unifontsel_internal
*fs
= snew(unifontsel_internal
);
2500 GtkWidget
*table
, *label
, *w
, *ww
, *scroll
;
2501 GtkListStore
*model
;
2502 GtkTreeViewColumn
*column
;
2503 int lists_height
, preview_height
, font_width
, style_width
, size_width
;
2506 fs
->inhibit_response
= FALSE
;
2507 fs
->selected
= NULL
;
2511 * Invent some magic size constants.
2514 label
= gtk_label_new("Quite Long Font Name (Foundry)");
2515 gtk_widget_size_request(label
, &req
);
2516 font_width
= req
.width
;
2517 lists_height
= 14 * req
.height
;
2518 preview_height
= 5 * req
.height
;
2519 gtk_label_set_text(GTK_LABEL(label
), "Italic Extra Condensed");
2520 gtk_widget_size_request(label
, &req
);
2521 style_width
= req
.width
;
2522 gtk_label_set_text(GTK_LABEL(label
), "48000");
2523 gtk_widget_size_request(label
, &req
);
2524 size_width
= req
.width
;
2525 #if GTK_CHECK_VERSION(2,10,0)
2526 g_object_ref_sink(label
);
2527 g_object_unref(label
);
2529 gtk_object_sink(GTK_OBJECT(label
));
2534 * Create the dialog box and initialise the user-visible
2535 * fields in the returned structure.
2537 fs
->u
.user_data
= NULL
;
2538 fs
->u
.window
= GTK_WINDOW(gtk_dialog_new());
2539 gtk_window_set_title(fs
->u
.window
, wintitle
);
2540 fs
->u
.cancel_button
= gtk_dialog_add_button
2541 (GTK_DIALOG(fs
->u
.window
), GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
);
2542 fs
->u
.ok_button
= gtk_dialog_add_button
2543 (GTK_DIALOG(fs
->u
.window
), GTK_STOCK_OK
, GTK_RESPONSE_OK
);
2544 gtk_widget_grab_default(fs
->u
.ok_button
);
2547 * Now set up the internal fields, including in particular all
2548 * the controls that actually allow the user to select fonts.
2550 table
= gtk_table_new(8, 3, FALSE
);
2551 gtk_widget_show(table
);
2552 gtk_table_set_col_spacings(GTK_TABLE(table
), 8);
2553 #if GTK_CHECK_VERSION(2,4,0)
2554 /* GtkAlignment seems to be the simplest way to put padding round things */
2555 w
= gtk_alignment_new(0, 0, 1, 1);
2556 gtk_alignment_set_padding(GTK_ALIGNMENT(w
), 8, 8, 8, 8);
2557 gtk_container_add(GTK_CONTAINER(w
), table
);
2562 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fs
->u
.window
)->vbox
),
2565 label
= gtk_label_new_with_mnemonic("_Font:");
2566 gtk_widget_show(label
);
2567 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2568 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1, GTK_FILL
, 0, 0, 0);
2571 * The Font list box displays only a string, but additionally
2572 * stores two integers which give the limits within the
2573 * tree234 of the font entries covered by this list entry.
2575 model
= gtk_list_store_new(3, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
);
2576 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2577 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2578 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2580 column
= gtk_tree_view_column_new_with_attributes
2581 ("Font", gtk_cell_renderer_text_new(),
2582 "text", 0, (char *)NULL
);
2583 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2584 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2585 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2586 "changed", G_CALLBACK(family_changed
), fs
);
2587 g_signal_connect(G_OBJECT(w
), "row-activated",
2588 G_CALLBACK(alias_resolve
), fs
);
2590 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2591 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2593 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2594 gtk_widget_show(scroll
);
2595 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2596 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2597 gtk_widget_set_size_request(scroll
, font_width
, lists_height
);
2598 gtk_table_attach(GTK_TABLE(table
), scroll
, 0, 1, 1, 3, GTK_FILL
,
2599 GTK_EXPAND
| GTK_FILL
, 0, 0);
2600 fs
->family_model
= model
;
2601 fs
->family_list
= w
;
2603 label
= gtk_label_new_with_mnemonic("_Style:");
2604 gtk_widget_show(label
);
2605 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2606 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 0, 1, GTK_FILL
, 0, 0, 0);
2609 * The Style list box can contain insensitive elements
2610 * (character set headings for server-side fonts), so we add
2611 * an extra column to the list store to hold that information.
2613 model
= gtk_list_store_new(4, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
,
2615 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2616 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2617 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2619 column
= gtk_tree_view_column_new_with_attributes
2620 ("Style", gtk_cell_renderer_text_new(),
2621 "text", 0, "sensitive", 3, (char *)NULL
);
2622 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2623 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2624 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2625 "changed", G_CALLBACK(style_changed
), fs
);
2627 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2628 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2630 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2631 gtk_widget_show(scroll
);
2632 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2633 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2634 gtk_widget_set_size_request(scroll
, style_width
, lists_height
);
2635 gtk_table_attach(GTK_TABLE(table
), scroll
, 1, 2, 1, 3, GTK_FILL
,
2636 GTK_EXPAND
| GTK_FILL
, 0, 0);
2637 fs
->style_model
= model
;
2640 label
= gtk_label_new_with_mnemonic("Si_ze:");
2641 gtk_widget_show(label
);
2642 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2643 gtk_table_attach(GTK_TABLE(table
), label
, 2, 3, 0, 1, GTK_FILL
, 0, 0, 0);
2646 * The Size label attaches primarily to a text input box so
2647 * that the user can select a size of their choice. The list
2648 * of available sizes is secondary.
2650 fs
->size_entry
= w
= gtk_entry_new();
2651 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2652 gtk_widget_set_size_request(w
, size_width
, -1);
2654 gtk_table_attach(GTK_TABLE(table
), w
, 2, 3, 1, 2, GTK_FILL
, 0, 0, 0);
2655 g_signal_connect(G_OBJECT(w
), "changed", G_CALLBACK(size_entry_changed
),
2658 model
= gtk_list_store_new(3, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
);
2659 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2660 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2662 column
= gtk_tree_view_column_new_with_attributes
2663 ("Size", gtk_cell_renderer_text_new(),
2664 "text", 0, (char *)NULL
);
2665 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2666 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2667 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2668 "changed", G_CALLBACK(size_changed
), fs
);
2670 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2671 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2673 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2674 gtk_widget_show(scroll
);
2675 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2676 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2677 gtk_table_attach(GTK_TABLE(table
), scroll
, 2, 3, 2, 3, GTK_FILL
,
2678 GTK_EXPAND
| GTK_FILL
, 0, 0);
2679 fs
->size_model
= model
;
2685 fs
->preview_area
= gtk_drawing_area_new();
2686 fs
->preview_pixmap
= NULL
;
2687 fs
->preview_width
= 0;
2688 fs
->preview_height
= 0;
2689 fs
->preview_fg
.pixel
= fs
->preview_bg
.pixel
= 0;
2690 fs
->preview_fg
.red
= fs
->preview_fg
.green
= fs
->preview_fg
.blue
= 0x0000;
2691 fs
->preview_bg
.red
= fs
->preview_bg
.green
= fs
->preview_bg
.blue
= 0xFFFF;
2692 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs
->preview_fg
,
2694 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs
->preview_bg
,
2696 gtk_signal_connect(GTK_OBJECT(fs
->preview_area
), "expose_event",
2697 GTK_SIGNAL_FUNC(unifontsel_expose_area
), fs
);
2698 gtk_signal_connect(GTK_OBJECT(fs
->preview_area
), "configure_event",
2699 GTK_SIGNAL_FUNC(unifontsel_configure_area
), fs
);
2700 gtk_widget_set_size_request(fs
->preview_area
, 1, preview_height
);
2701 gtk_widget_show(fs
->preview_area
);
2702 ww
= fs
->preview_area
;
2703 w
= gtk_frame_new(NULL
);
2704 gtk_container_add(GTK_CONTAINER(w
), ww
);
2706 #if GTK_CHECK_VERSION(2,4,0)
2708 /* GtkAlignment seems to be the simplest way to put padding round things */
2709 w
= gtk_alignment_new(0, 0, 1, 1);
2710 gtk_alignment_set_padding(GTK_ALIGNMENT(w
), 8, 8, 8, 8);
2711 gtk_container_add(GTK_CONTAINER(w
), ww
);
2715 w
= gtk_frame_new("Preview of font");
2716 gtk_container_add(GTK_CONTAINER(w
), ww
);
2718 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 3, 4,
2719 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
| GTK_FILL
, 0, 8);
2722 w
= gtk_check_button_new_with_label("Show client-side fonts");
2723 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2724 GINT_TO_POINTER(FONTFLAG_CLIENTSIDE
));
2725 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2726 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2728 fs
->filter_buttons
[i
++] = w
;
2729 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 4, 5, GTK_FILL
, 0, 0, 0);
2730 w
= gtk_check_button_new_with_label("Show server-side fonts");
2731 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2732 GINT_TO_POINTER(FONTFLAG_SERVERSIDE
));
2733 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2734 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2736 fs
->filter_buttons
[i
++] = w
;
2737 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 5, 6, GTK_FILL
, 0, 0, 0);
2738 w
= gtk_check_button_new_with_label("Show server-side font aliases");
2739 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2740 GINT_TO_POINTER(FONTFLAG_SERVERALIAS
));
2741 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2742 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2744 fs
->filter_buttons
[i
++] = w
;
2745 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 6, 7, GTK_FILL
, 0, 0, 0);
2746 w
= gtk_check_button_new_with_label("Show non-monospaced fonts");
2747 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2748 GINT_TO_POINTER(FONTFLAG_NONMONOSPACED
));
2749 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2750 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2752 fs
->filter_buttons
[i
++] = w
;
2753 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 7, 8, GTK_FILL
, 0, 0, 0);
2755 assert(i
== lenof(fs
->filter_buttons
));
2756 fs
->filter_flags
= FONTFLAG_CLIENTSIDE
| FONTFLAG_SERVERSIDE
|
2757 FONTFLAG_SERVERALIAS
;
2758 unifontsel_set_filter_buttons(fs
);
2761 * Go and find all the font names, and set up our master font
2764 fs
->fonts_by_realname
= newtree234(fontinfo_realname_compare
);
2765 fs
->fonts_by_selorder
= newtree234(fontinfo_selorder_compare
);
2766 for (i
= 0; i
< lenof(unifont_types
); i
++)
2767 unifont_types
[i
]->enum_fonts(GTK_WIDGET(fs
->u
.window
),
2768 unifontsel_add_entry
, fs
);
2771 * And set up the initial font names list.
2773 unifontsel_setup_familylist(fs
);
2775 fs
->selsize
= fs
->intendedsize
= 13; /* random default */
2776 gtk_widget_set_sensitive(fs
->u
.ok_button
, FALSE
);
2778 return (unifontsel
*)fs
;
2781 void unifontsel_destroy(unifontsel
*fontsel
)
2783 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2786 if (fs
->preview_pixmap
)
2787 gdk_pixmap_unref(fs
->preview_pixmap
);
2789 freetree234(fs
->fonts_by_selorder
);
2790 while ((info
= delpos234(fs
->fonts_by_realname
, 0)) != NULL
)
2792 freetree234(fs
->fonts_by_realname
);
2794 gtk_widget_destroy(GTK_WIDGET(fs
->u
.window
));
2798 void unifontsel_set_name(unifontsel
*fontsel
, const char *fontname
)
2800 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2801 int i
, start
, end
, size
, flags
;
2802 const char *fontname2
= NULL
;
2806 * Provide a default if given an empty or null font name.
2808 if (!fontname
|| !*fontname
)
2809 fontname
= "server:fixed";
2812 * Call the canonify_fontname function.
2814 fontname
= unifont_do_prefix(fontname
, &start
, &end
);
2815 for (i
= start
; i
< end
; i
++) {
2816 fontname2
= unifont_types
[i
]->canonify_fontname
2817 (GTK_WIDGET(fs
->u
.window
), fontname
, &size
, &flags
, FALSE
);
2822 return; /* font name not recognised */
2825 * Now look up the canonified font name in our index.
2828 struct fontinfo_realname_find f
;
2829 f
.realname
= fontname2
;
2831 info
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2835 * If we've found the font, and its size field is either
2836 * correct or zero (the latter indicating a scalable font),
2837 * then we're done. Otherwise, try looking up the original
2838 * font name instead.
2840 if (!info
|| (info
->size
!= size
&& info
->size
!= 0)) {
2841 struct fontinfo_realname_find f
;
2842 f
.realname
= fontname
;
2845 info
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2846 if (!info
|| info
->size
!= size
)
2847 return; /* font name not in our index */
2851 * Now we've got a fontinfo structure and a font size, so we
2852 * know everything we need to fill in all the fields in the
2855 unifontsel_select_font(fs
, info
, size
, 0, TRUE
);
2858 char *unifontsel_get_name(unifontsel
*fontsel
)
2860 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2866 if (fs
->selected
->size
== 0) {
2867 name
= fs
->selected
->fontclass
->scale_fontname
2868 (GTK_WIDGET(fs
->u
.window
), fs
->selected
->realname
, fs
->selsize
);
2870 char *ret
= dupcat(fs
->selected
->fontclass
->prefix
, ":",
2877 return dupcat(fs
->selected
->fontclass
->prefix
, ":",
2878 fs
->selected
->realname
, NULL
);
2881 #endif /* GTK_CHECK_VERSION(2,0,0) */