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.
44 #if !GLIB_CHECK_VERSION(1,3,7)
45 #define g_ascii_strcasecmp g_strcasecmp
46 #define g_ascii_strncasecmp g_strncasecmp
50 * Ad-hoc vtable mechanism to allow font structures to be
53 * Any instance of `unifont' used in the vtable functions will
54 * actually be the first element of a larger structure containing
55 * data specific to the subtype. This is permitted by the ISO C
56 * provision that one may safely cast between a pointer to a
57 * structure and a pointer to its first element.
60 #define FONTFLAG_CLIENTSIDE 0x0001
61 #define FONTFLAG_SERVERSIDE 0x0002
62 #define FONTFLAG_SERVERALIAS 0x0004
63 #define FONTFLAG_NONMONOSPACED 0x0008
65 #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
67 typedef void (*fontsel_add_entry
)(void *ctx
, const char *realfontname
,
68 const char *family
, const char *charset
,
69 const char *style
, const char *stylekey
,
71 const struct unifont_vtable
*fontclass
);
73 struct unifont_vtable
{
75 * `Methods' of the `class'.
77 unifont
*(*create
)(GtkWidget
*widget
, const char *name
, int wide
, int bold
,
78 int shadowoffset
, int shadowalways
);
79 unifont
*(*create_fallback
)(GtkWidget
*widget
, int height
, int wide
,
80 int bold
, int shadowoffset
, int shadowalways
);
81 void (*destroy
)(unifont
*font
);
82 int (*has_glyph
)(unifont
*font
, wchar_t glyph
);
83 void (*draw_text
)(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
84 int x
, int y
, const wchar_t *string
, int len
, int wide
,
85 int bold
, int cellwidth
);
86 void (*enum_fonts
)(GtkWidget
*widget
,
87 fontsel_add_entry callback
, void *callback_ctx
);
88 char *(*canonify_fontname
)(GtkWidget
*widget
, const char *name
, int *size
,
89 int *flags
, int resolve_aliases
);
90 char *(*scale_fontname
)(GtkWidget
*widget
, const char *name
, int size
);
93 * `Static data members' of the `class'.
98 /* ----------------------------------------------------------------------
99 * X11 font implementation, directly using Xlib calls.
102 static int x11font_has_glyph(unifont
*font
, wchar_t glyph
);
103 static void x11font_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
104 int x
, int y
, const wchar_t *string
, int len
,
105 int wide
, int bold
, int cellwidth
);
106 static unifont
*x11font_create(GtkWidget
*widget
, const char *name
,
108 int shadowoffset
, int shadowalways
);
109 static void x11font_destroy(unifont
*font
);
110 static void x11font_enum_fonts(GtkWidget
*widget
,
111 fontsel_add_entry callback
, void *callback_ctx
);
112 static char *x11font_canonify_fontname(GtkWidget
*widget
, const char *name
,
113 int *size
, int *flags
,
114 int resolve_aliases
);
115 static char *x11font_scale_fontname(GtkWidget
*widget
, const char *name
,
121 * Actual font objects. We store a number of these, for
122 * automatically guessed bold and wide variants.
124 * The parallel array `allocated' indicates whether we've
125 * tried to fetch a subfont already (thus distinguishing NULL
126 * because we haven't tried yet from NULL because we tried and
127 * failed, so that we don't keep trying and failing
130 XFontStruct
*fonts
[4];
133 * `sixteen_bit' is true iff the font object is indexed by
134 * values larger than a byte. That is, this flag tells us
135 * whether we use XDrawString or XDrawString16, etc.
139 * `variable' is true iff the font is non-fixed-pitch. This
140 * enables some code which takes greater care over character
141 * positioning during text drawing.
145 * real_charset is the charset used when translating text into the
146 * font's internal encoding inside draw_text(). This need not be
147 * the same as the public_charset provided to the client; for
148 * example, public_charset might be CS_ISO8859_1 while
149 * real_charset is CS_ISO8859_1_X11.
153 * Data passed in to unifont_create().
155 int wide
, bold
, shadowoffset
, shadowalways
;
158 static const struct unifont_vtable x11font_vtable
= {
160 NULL
, /* no fallback fonts in X11 */
165 x11font_canonify_fontname
,
166 x11font_scale_fontname
,
170 static char *x11_guess_derived_font_name(XFontStruct
*xfs
, int bold
, int wide
)
172 Display
*disp
= GDK_DISPLAY();
173 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
175 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
176 char *name
= XGetAtomName(disp
, (Atom
)ret
);
177 if (name
&& name
[0] == '-') {
179 char *dupname
, *extrafree
= NULL
, *ret
;
183 p
= q
= dupname
= dupstr(name
); /* skip initial minus */
186 while (*p
&& nstr
< lenof(strings
)) {
189 strings
[nstr
++] = p
+1;
194 if (nstr
< lenof(strings
)) {
196 return NULL
; /* XLFD was malformed */
203 /* 4 is `wideness', which obviously may have changed. */
204 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
205 strings
[4] = strings
[5] = "*";
206 strings
[11] = extrafree
= dupprintf("%d", 2*atoi(strings
[11]));
209 ret
= dupcat("-", strings
[ 0], "-", strings
[ 1], "-", strings
[ 2],
210 "-", strings
[ 3], "-", strings
[ 4], "-", strings
[ 5],
211 "-", strings
[ 6], "-", strings
[ 7], "-", strings
[ 8],
212 "-", strings
[ 9], "-", strings
[10], "-", strings
[11],
213 "-", strings
[12], NULL
);
223 static int x11_font_width(XFontStruct
*xfs
, int sixteen_bit
)
229 return XTextWidth16(xfs
, &space
, 1);
231 return XTextWidth(xfs
, "0", 1);
235 static int x11_font_has_glyph(XFontStruct
*xfs
, int byte1
, int byte2
)
240 * Not to be confused with x11font_has_glyph, which is a method of
241 * the x11font 'class' and hence takes a unifont as argument. This
242 * is the low-level function which grubs about in an actual
243 * XFontStruct to see if a given glyph exists.
245 * We must do this ourselves rather than letting Xlib's
246 * XTextExtents16 do the job, because XTextExtents will helpfully
247 * substitute the font's default_char for any missing glyph and
248 * not tell us it did so, which precisely won't help us find out
249 * which glyphs _are_ missing.
251 * The man page for XQueryFont is rather confusing about how the
252 * per_char array in the XFontStruct is laid out, because it gives
253 * formulae for determining the two-byte X character code _from_
254 * an index into the per_char array. Going the other way, it's
257 * The valid character codes have byte1 between min_byte1 and
258 * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
259 * max_char_or_byte2 inclusive. This gives a rectangle of size
260 * (max_byte2-min_byte1+1) by
261 * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
262 * rectangle encoded in the per_char array. Hence, given a
263 * character code which is valid in the sense that it falls
264 * somewhere in that rectangle, its index in per_char is given by
267 * x = byte2 - min_char_or_byte2
268 * y = byte1 - min_byte1
269 * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
271 * If min_byte1 and min_byte2 are both zero, that's a special case
272 * which can be treated as if min_byte2 was 1 instead, i.e. the
273 * per_char array just runs from min_char_or_byte2 to
274 * max_char_or_byte2 inclusive, and byte1 should always be zero.
277 if (byte2
< xfs
->min_char_or_byte2
|| byte2
> xfs
->max_char_or_byte2
)
280 if (xfs
->min_byte1
== 0 && xfs
->max_byte1
== 0) {
281 index
= byte2
- xfs
->min_char_or_byte2
;
283 if (byte1
< xfs
->min_byte1
|| byte1
> xfs
->max_byte1
)
285 index
= ((byte2
- xfs
->min_char_or_byte2
) +
286 ((byte1
- xfs
->min_byte1
) *
287 (xfs
->max_char_or_byte2
- xfs
->min_char_or_byte2
+ 1)));
290 if (!xfs
->per_char
) /* per_char NULL => everything in range exists */
293 return (xfs
->per_char
[index
].ascent
+ xfs
->per_char
[index
].descent
> 0 ||
294 xfs
->per_char
[index
].width
> 0);
297 static unifont
*x11font_create(GtkWidget
*widget
, const char *name
,
299 int shadowoffset
, int shadowalways
)
301 struct x11font
*xfont
;
303 Display
*disp
= GDK_DISPLAY();
304 Atom charset_registry
, charset_encoding
, spacing
;
305 unsigned long registry_ret
, encoding_ret
, spacing_ret
;
306 int pubcs
, realcs
, sixteen_bit
, variable
;
309 xfs
= XLoadQueryFont(disp
, name
);
313 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
314 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
316 pubcs
= realcs
= CS_NONE
;
320 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
321 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
323 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
324 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
326 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
327 pubcs
= realcs
= charset_from_xenc(encoding
);
330 * iso10646-1 is the only wide font encoding we
331 * support. In this case, we expect clients to give us
332 * UTF-8, which this module must internally convert
333 * into 16-bit Unicode.
335 if (!strcasecmp(encoding
, "iso10646-1")) {
337 pubcs
= realcs
= CS_UTF8
;
341 * Hack for X line-drawing characters: if the primary font
342 * is encoded as ISO-8859-1, and has valid glyphs in the
343 * low character positions, it is assumed that those
344 * glyphs are the VT100 line-drawing character set.
346 if (pubcs
== CS_ISO8859_1
) {
348 for (ch
= 1; ch
< 32; ch
++)
349 if (!x11_font_has_glyph(xfs
, 0, ch
))
352 realcs
= CS_ISO8859_1_X11
;
359 spacing
= XInternAtom(disp
, "SPACING", False
);
360 if (XGetFontProperty(xfs
, spacing
, &spacing_ret
)) {
362 spc
= XGetAtomName(disp
, (Atom
)spacing_ret
);
364 if (spc
&& strchr("CcMm", spc
[0]))
368 xfont
= snew(struct x11font
);
369 xfont
->u
.vt
= &x11font_vtable
;
370 xfont
->u
.width
= x11_font_width(xfs
, sixteen_bit
);
371 xfont
->u
.ascent
= xfs
->ascent
;
372 xfont
->u
.descent
= xfs
->descent
;
373 xfont
->u
.height
= xfont
->u
.ascent
+ xfont
->u
.descent
;
374 xfont
->u
.public_charset
= pubcs
;
375 xfont
->u
.want_fallback
= TRUE
;
376 xfont
->real_charset
= realcs
;
377 xfont
->fonts
[0] = xfs
;
378 xfont
->allocated
[0] = TRUE
;
379 xfont
->sixteen_bit
= sixteen_bit
;
380 xfont
->variable
= variable
;
383 xfont
->shadowoffset
= shadowoffset
;
384 xfont
->shadowalways
= shadowalways
;
386 for (i
= 1; i
< lenof(xfont
->fonts
); i
++) {
387 xfont
->fonts
[i
] = NULL
;
388 xfont
->allocated
[i
] = FALSE
;
391 return (unifont
*)xfont
;
394 static void x11font_destroy(unifont
*font
)
396 Display
*disp
= GDK_DISPLAY();
397 struct x11font
*xfont
= (struct x11font
*)font
;
400 for (i
= 0; i
< lenof(xfont
->fonts
); i
++)
402 XFreeFont(disp
, xfont
->fonts
[i
]);
406 static void x11_alloc_subfont(struct x11font
*xfont
, int sfid
)
408 Display
*disp
= GDK_DISPLAY();
409 char *derived_name
= x11_guess_derived_font_name
410 (xfont
->fonts
[0], sfid
& 1, !!(sfid
& 2));
411 xfont
->fonts
[sfid
] = XLoadQueryFont(disp
, derived_name
);
412 xfont
->allocated
[sfid
] = TRUE
;
414 /* Note that xfont->fonts[sfid] may still be NULL, if XLQF failed. */
417 static int x11font_has_glyph(unifont
*font
, wchar_t glyph
)
419 struct x11font
*xfont
= (struct x11font
*)font
;
421 if (xfont
->sixteen_bit
) {
423 * This X font has 16-bit character indices, which means
424 * we can directly use our Unicode input value.
426 return x11_font_has_glyph(xfont
->fonts
[0], glyph
>> 8, glyph
& 0xFF);
429 * This X font has 8-bit indices, so we must convert to the
430 * appropriate character set.
433 int sblen
= wc_to_mb(xfont
->real_charset
, 0, &glyph
, 1,
434 sbstring
, 2, "", NULL
, NULL
);
435 if (sblen
== 0 || !sbstring
[0])
436 return FALSE
; /* not even in the charset */
438 return x11_font_has_glyph(xfont
->fonts
[0], 0,
439 (unsigned char)sbstring
[0]);
443 #if !GTK_CHECK_VERSION(2,0,0)
444 #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
447 static void x11font_really_draw_text_16(GdkDrawable
*target
, XFontStruct
*xfs
,
449 const XChar2b
*string
, int nchars
,
451 int fontvariable
, int cellwidth
)
453 Display
*disp
= GDK_DISPLAY();
454 int step
, nsteps
, centre
;
458 * In a variable-pitch font, we draw one character at a
459 * time, and centre it in the character cell.
466 * In a fixed-pitch font, we can draw the whole lot in one go.
473 while (nsteps
-- > 0) {
476 X
+= (cellwidth
- XTextWidth16(xfs
, string
, step
)) / 2;
478 XDrawString16(disp
, GDK_DRAWABLE_XID(target
), gc
,
481 XDrawString16(disp
, GDK_DRAWABLE_XID(target
), gc
,
482 X
+ shadowoffset
, y
, string
, step
);
489 static void x11font_really_draw_text(GdkDrawable
*target
, XFontStruct
*xfs
,
491 const char *string
, int nchars
,
493 int fontvariable
, int cellwidth
)
495 Display
*disp
= GDK_DISPLAY();
496 int step
, nsteps
, centre
;
500 * In a variable-pitch font, we draw one character at a
501 * time, and centre it in the character cell.
508 * In a fixed-pitch font, we can draw the whole lot in one go.
515 while (nsteps
-- > 0) {
518 X
+= (cellwidth
- XTextWidth(xfs
, string
, step
)) / 2;
520 XDrawString(disp
, GDK_DRAWABLE_XID(target
), gc
,
523 XDrawString(disp
, GDK_DRAWABLE_XID(target
), gc
,
524 X
+ shadowoffset
, y
, string
, step
);
531 static void x11font_draw_text(GdkDrawable
*target
, GdkGC
*gdkgc
, unifont
*font
,
532 int x
, int y
, const wchar_t *string
, int len
,
533 int wide
, int bold
, int cellwidth
)
535 Display
*disp
= GDK_DISPLAY();
536 struct x11font
*xfont
= (struct x11font
*)font
;
537 GC gc
= GDK_GC_XGC(gdkgc
);
539 int shadowoffset
= 0;
540 int mult
= (wide ?
2 : 1);
546 * Decide which subfont we're using, and whether we have to
549 if (xfont
->shadowalways
&& bold
) {
550 shadowoffset
= xfont
->shadowoffset
;
553 sfid
= 2 * wide
+ bold
;
554 if (!xfont
->allocated
[sfid
])
555 x11_alloc_subfont(xfont
, sfid
);
556 if (bold
&& !xfont
->fonts
[sfid
]) {
558 shadowoffset
= xfont
->shadowoffset
;
559 sfid
= 2 * wide
+ bold
;
560 if (!xfont
->allocated
[sfid
])
561 x11_alloc_subfont(xfont
, sfid
);
564 if (!xfont
->fonts
[sfid
])
565 return; /* we've tried our best, but no luck */
567 XSetFont(disp
, gc
, xfont
->fonts
[sfid
]->fid
);
569 if (xfont
->sixteen_bit
) {
571 * This X font has 16-bit character indices, which means
572 * we can directly use our Unicode input string.
577 xcs
= snewn(len
, XChar2b
);
578 for (i
= 0; i
< len
; i
++) {
579 xcs
[i
].byte1
= string
[i
] >> 8;
580 xcs
[i
].byte2
= string
[i
];
583 x11font_really_draw_text_16(target
, xfont
->fonts
[sfid
], gc
, x
, y
,
584 xcs
, len
, shadowoffset
,
585 xfont
->variable
, cellwidth
* mult
);
589 * This X font has 8-bit indices, so we must convert to the
590 * appropriate character set.
592 char *sbstring
= snewn(len
+1, char);
593 int sblen
= wc_to_mb(xfont
->real_charset
, 0, string
, len
,
594 sbstring
, len
+1, ".", NULL
, NULL
);
595 x11font_really_draw_text(target
, xfont
->fonts
[sfid
], gc
, x
, y
,
596 sbstring
, sblen
, shadowoffset
,
597 xfont
->variable
, cellwidth
* mult
);
602 static void x11font_enum_fonts(GtkWidget
*widget
,
603 fontsel_add_entry callback
, void *callback_ctx
)
607 int nnames
, i
, max
, tmpsize
;
611 fontnames
= XListFonts(GDK_DISPLAY(), "*", max
, &nnames
);
613 XFreeFontNames(fontnames
);
621 for (i
= 0; i
< nnames
; i
++) {
622 if (fontnames
[i
][0] == '-') {
624 * Dismember an XLFD and convert it into the format
625 * we'll be using in the font selector.
627 char *components
[14];
628 char *p
, *font
, *style
, *stylekey
, *charset
;
629 int j
, weightkey
, slantkey
, setwidthkey
;
630 int thistmpsize
, fontsize
, flags
;
632 thistmpsize
= 4 * strlen(fontnames
[i
]) + 256;
633 if (tmpsize
< thistmpsize
) {
634 tmpsize
= thistmpsize
;
635 tmp
= sresize(tmp
, tmpsize
, char);
637 strcpy(tmp
, fontnames
[i
]);
640 for (j
= 0; j
< 14; j
++) {
644 while (*p
&& *p
!= '-')
650 * Font name is made up of fields 0 and 1, in reverse
651 * order with parentheses. (This is what the GTK 1.2 X
652 * font selector does, and it seems to come out
653 * looking reasonably sensible.)
656 p
+= 1 + sprintf(p
, "%s (%s)", components
[1], components
[0]);
659 * Charset is made up of fields 12 and 13.
662 p
+= 1 + sprintf(p
, "%s-%s", components
[12], components
[13]);
665 * Style is a mixture of quite a lot of the fields,
666 * with some strange formatting.
669 p
+= sprintf(p
, "%s", components
[2][0] ? components
[2] :
671 if (!g_ascii_strcasecmp(components
[3], "i"))
672 p
+= sprintf(p
, " italic");
673 else if (!g_ascii_strcasecmp(components
[3], "o"))
674 p
+= sprintf(p
, " oblique");
675 else if (!g_ascii_strcasecmp(components
[3], "ri"))
676 p
+= sprintf(p
, " reverse italic");
677 else if (!g_ascii_strcasecmp(components
[3], "ro"))
678 p
+= sprintf(p
, " reverse oblique");
679 else if (!g_ascii_strcasecmp(components
[3], "ot"))
680 p
+= sprintf(p
, " other-slant");
681 if (components
[4][0] && g_ascii_strcasecmp(components
[4], "normal"))
682 p
+= sprintf(p
, " %s", components
[4]);
683 if (!g_ascii_strcasecmp(components
[10], "m"))
684 p
+= sprintf(p
, " [M]");
685 if (!g_ascii_strcasecmp(components
[10], "c"))
686 p
+= sprintf(p
, " [C]");
687 if (components
[5][0])
688 p
+= sprintf(p
, " %s", components
[5]);
691 * Style key is the same stuff as above, but with a
692 * couple of transformations done on it to make it
693 * sort more sensibly.
697 if (!g_ascii_strcasecmp(components
[2], "medium") ||
698 !g_ascii_strcasecmp(components
[2], "regular") ||
699 !g_ascii_strcasecmp(components
[2], "normal") ||
700 !g_ascii_strcasecmp(components
[2], "book"))
702 else if (!g_ascii_strncasecmp(components
[2], "demi", 4) ||
703 !g_ascii_strncasecmp(components
[2], "semi", 4))
707 if (!g_ascii_strcasecmp(components
[3], "r"))
709 else if (!g_ascii_strncasecmp(components
[3], "r", 1))
713 if (!g_ascii_strcasecmp(components
[4], "normal"))
718 p
+= sprintf(p
, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
720 (int)strlen(components
[2]), components
[2],
722 (int)strlen(components
[3]), components
[3],
724 (int)strlen(components
[4]), components
[4],
725 (int)strlen(components
[10]), components
[10],
726 (int)strlen(components
[5]), components
[5]);
728 assert(p
- tmp
< thistmpsize
);
731 * Size is in pixels, for our application, so we
732 * derive it directly from the pixel size field,
735 fontsize
= atoi(components
[6]);
738 * Flags: we need to know whether this is a monospaced
739 * font, which we do by examining the spacing field
742 flags
= FONTFLAG_SERVERSIDE
;
743 if (!strchr("CcMm", components
[10][0]))
744 flags
|= FONTFLAG_NONMONOSPACED
;
747 * Not sure why, but sometimes the X server will
748 * deliver dummy font types in which fontsize comes
749 * out as zero. Filter those out.
752 callback(callback_ctx
, fontnames
[i
], font
, charset
,
753 style
, stylekey
, fontsize
, flags
, &x11font_vtable
);
756 * This isn't an XLFD, so it must be an alias.
757 * Transmit it with mostly null data.
759 * It would be nice to work out if it's monospaced
760 * here, but at the moment I can't see that being
761 * anything but computationally hideous. Ah well.
763 callback(callback_ctx
, fontnames
[i
], fontnames
[i
], NULL
,
764 NULL
, NULL
, 0, FONTFLAG_SERVERALIAS
, &x11font_vtable
);
767 XFreeFontNames(fontnames
);
770 static char *x11font_canonify_fontname(GtkWidget
*widget
, const char *name
,
771 int *size
, int *flags
,
775 * When given an X11 font name to try to make sense of for a
776 * font selector, we must attempt to load it (to see if it
777 * exists), and then canonify it by extracting its FONT
778 * property, which should give its full XLFD even if what we
779 * originally had was a wildcard.
781 * However, we must carefully avoid canonifying font
782 * _aliases_, unless specifically asked to, because the font
783 * selector treats them as worthwhile in their own right.
786 Display
*disp
= GDK_DISPLAY();
787 Atom fontprop
, fontprop2
;
790 xfs
= XLoadQueryFont(disp
, name
);
793 return NULL
; /* didn't make sense to us, sorry */
795 fontprop
= XInternAtom(disp
, "FONT", False
);
797 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
798 char *newname
= XGetAtomName(disp
, (Atom
)ret
);
800 unsigned long fsize
= 12;
802 fontprop2
= XInternAtom(disp
, "PIXEL_SIZE", False
);
803 if (XGetFontProperty(xfs
, fontprop2
, &fsize
) && fsize
> 0) {
805 XFreeFont(disp
, xfs
);
807 if (name
[0] == '-' || resolve_aliases
)
808 *flags
= FONTFLAG_SERVERSIDE
;
810 *flags
= FONTFLAG_SERVERALIAS
;
812 return dupstr(name
[0] == '-' || resolve_aliases ?
818 XFreeFont(disp
, xfs
);
820 return NULL
; /* something went wrong */
823 static char *x11font_scale_fontname(GtkWidget
*widget
, const char *name
,
826 return NULL
; /* shan't */
829 #if GTK_CHECK_VERSION(2,0,0)
831 /* ----------------------------------------------------------------------
832 * Pango font implementation (for GTK 2 only).
835 #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
836 #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
839 static int pangofont_has_glyph(unifont
*font
, wchar_t glyph
);
840 static void pangofont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
841 int x
, int y
, const wchar_t *string
, int len
,
842 int wide
, int bold
, int cellwidth
);
843 static unifont
*pangofont_create(GtkWidget
*widget
, const char *name
,
845 int shadowoffset
, int shadowalways
);
846 static unifont
*pangofont_create_fallback(GtkWidget
*widget
, int height
,
848 int shadowoffset
, int shadowalways
);
849 static void pangofont_destroy(unifont
*font
);
850 static void pangofont_enum_fonts(GtkWidget
*widget
, fontsel_add_entry callback
,
852 static char *pangofont_canonify_fontname(GtkWidget
*widget
, const char *name
,
853 int *size
, int *flags
,
854 int resolve_aliases
);
855 static char *pangofont_scale_fontname(GtkWidget
*widget
, const char *name
,
863 PangoFontDescription
*desc
;
866 * The containing widget.
870 * Data passed in to unifont_create().
872 int bold
, shadowoffset
, shadowalways
;
875 static const struct unifont_vtable pangofont_vtable
= {
877 pangofont_create_fallback
,
881 pangofont_enum_fonts
,
882 pangofont_canonify_fontname
,
883 pangofont_scale_fontname
,
888 * This function is used to rigorously validate a
889 * PangoFontDescription. Later versions of Pango have a nasty
890 * habit of accepting _any_ old string as input to
891 * pango_font_description_from_string and returning a font
892 * description which can actually be used to display text, even if
893 * they have to do it by falling back to their most default font.
894 * This is doubtless helpful in some situations, but not here,
895 * because we need to know if a Pango font string actually _makes
896 * sense_ in order to fall back to treating it as an X font name
897 * if it doesn't. So we check that the font family is actually one
898 * supported by Pango.
900 static int pangofont_check_desc_makes_sense(PangoContext
*ctx
,
901 PangoFontDescription
*desc
)
903 #ifndef PANGO_PRE_1POINT6
906 PangoFontFamily
**families
;
907 int i
, nfamilies
, matched
;
910 * Ask Pango for a list of font families, and iterate through
911 * them to see if one of them matches the family in the
912 * PangoFontDescription.
914 #ifndef PANGO_PRE_1POINT6
915 map
= pango_context_get_font_map(ctx
);
918 pango_font_map_list_families(map
, &families
, &nfamilies
);
920 pango_context_list_families(ctx
, &families
, &nfamilies
);
924 for (i
= 0; i
< nfamilies
; i
++) {
925 if (!g_ascii_strcasecmp(pango_font_family_get_name(families
[i
]),
926 pango_font_description_get_family(desc
))) {
936 static unifont
*pangofont_create_internal(GtkWidget
*widget
,
938 PangoFontDescription
*desc
,
940 int shadowoffset
, int shadowalways
)
942 struct pangofont
*pfont
;
943 #ifndef PANGO_PRE_1POINT6
947 PangoFontMetrics
*metrics
;
949 #ifndef PANGO_PRE_1POINT6
950 map
= pango_context_get_font_map(ctx
);
952 pango_font_description_free(desc
);
955 fset
= pango_font_map_load_fontset(map
, ctx
, desc
,
956 pango_context_get_language(ctx
));
958 fset
= pango_context_load_fontset(ctx
, desc
,
959 pango_context_get_language(ctx
));
962 pango_font_description_free(desc
);
965 metrics
= pango_fontset_get_metrics(fset
);
967 pango_font_metrics_get_approximate_digit_width(metrics
) == 0) {
968 pango_font_description_free(desc
);
969 g_object_unref(fset
);
973 pfont
= snew(struct pangofont
);
974 pfont
->u
.vt
= &pangofont_vtable
;
976 PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics
));
977 pfont
->u
.ascent
= PANGO_PIXELS(pango_font_metrics_get_ascent(metrics
));
978 pfont
->u
.descent
= PANGO_PIXELS(pango_font_metrics_get_descent(metrics
));
979 pfont
->u
.height
= pfont
->u
.ascent
+ pfont
->u
.descent
;
980 pfont
->u
.want_fallback
= FALSE
;
981 /* The Pango API is hardwired to UTF-8 */
982 pfont
->u
.public_charset
= CS_UTF8
;
985 pfont
->widget
= widget
;
987 pfont
->shadowoffset
= shadowoffset
;
988 pfont
->shadowalways
= shadowalways
;
990 pango_font_metrics_unref(metrics
);
992 return (unifont
*)pfont
;
995 static unifont
*pangofont_create(GtkWidget
*widget
, const char *name
,
997 int shadowoffset
, int shadowalways
)
1000 PangoFontDescription
*desc
;
1002 desc
= pango_font_description_from_string(name
);
1005 ctx
= gtk_widget_get_pango_context(widget
);
1007 pango_font_description_free(desc
);
1010 if (!pangofont_check_desc_makes_sense(ctx
, desc
)) {
1011 pango_font_description_free(desc
);
1014 return pangofont_create_internal(widget
, ctx
, desc
, wide
, bold
,
1015 shadowoffset
, shadowalways
);
1018 static unifont
*pangofont_create_fallback(GtkWidget
*widget
, int height
,
1020 int shadowoffset
, int shadowalways
)
1023 PangoFontDescription
*desc
;
1025 desc
= pango_font_description_from_string("Monospace");
1028 ctx
= gtk_widget_get_pango_context(widget
);
1030 pango_font_description_free(desc
);
1033 pango_font_description_set_absolute_size(desc
, height
* PANGO_SCALE
);
1034 return pangofont_create_internal(widget
, ctx
, desc
, wide
, bold
,
1035 shadowoffset
, shadowalways
);
1038 static void pangofont_destroy(unifont
*font
)
1040 struct pangofont
*pfont
= (struct pangofont
*)font
;
1041 pango_font_description_free(pfont
->desc
);
1042 g_object_unref(pfont
->fset
);
1046 static int pangofont_has_glyph(unifont
*font
, wchar_t glyph
)
1048 /* Pango implements font fallback, so assume it has everything */
1052 static void pangofont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1053 int x
, int y
, const wchar_t *string
, int len
,
1054 int wide
, int bold
, int cellwidth
)
1056 struct pangofont
*pfont
= (struct pangofont
*)font
;
1057 PangoLayout
*layout
;
1058 PangoRectangle rect
;
1059 char *utfstring
, *utfptr
;
1061 int shadowbold
= FALSE
;
1066 y
-= pfont
->u
.ascent
;
1068 layout
= pango_layout_new(gtk_widget_get_pango_context(pfont
->widget
));
1069 pango_layout_set_font_description(layout
, pfont
->desc
);
1070 if (bold
> pfont
->bold
) {
1071 if (pfont
->shadowalways
)
1074 PangoFontDescription
*desc2
=
1075 pango_font_description_copy_static(pfont
->desc
);
1076 pango_font_description_set_weight(desc2
, PANGO_WEIGHT_BOLD
);
1077 pango_layout_set_font_description(layout
, desc2
);
1082 * Pango always expects UTF-8, so convert the input wide character
1085 utfstring
= snewn(len
*6+1, char); /* UTF-8 has max 6 bytes/char */
1086 utflen
= wc_to_mb(CS_UTF8
, 0, string
, len
,
1087 utfstring
, len
*6+1, ".", NULL
, NULL
);
1090 while (utflen
> 0) {
1094 * We want to display every character from this string in
1095 * the centre of its own character cell. In the worst case,
1096 * this requires a separate text-drawing call for each
1097 * character; but in the common case where the font is
1098 * properly fixed-width, we can draw many characters in one
1099 * go which is much faster.
1101 * This still isn't really ideal. If you look at what
1102 * happens in the X protocol as a result of all of this, you
1103 * find - naturally enough - that each call to
1104 * gdk_draw_layout() generates a separate set of X RENDER
1105 * operations involving creating a picture, setting a clip
1106 * rectangle, doing some drawing and undoing the whole lot.
1107 * In an ideal world, we should _always_ be able to turn the
1108 * contents of this loop into a single RenderCompositeGlyphs
1109 * operation which internally specifies inter-character
1110 * deltas to get the spacing right, which would give us full
1111 * speed _even_ in the worst case of a non-fixed-width font.
1112 * However, Pango's architecture and documentation are so
1113 * unhelpful that I have no idea how if at all to persuade
1118 * Start by extracting a single UTF-8 character from the
1122 while (clen
< utflen
&&
1123 (unsigned char)utfptr
[clen
] >= 0x80 &&
1124 (unsigned char)utfptr
[clen
] < 0xC0)
1129 * If it's a right-to-left character, we must display it on
1130 * its own, to stop Pango helpfully re-reversing our already
1133 if (!is_rtl(string
[0])) {
1136 * See if that character has the width we expect.
1138 pango_layout_set_text(layout
, utfptr
, clen
);
1139 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1141 if (rect
.width
== cellwidth
) {
1143 * Try extracting more characters, for as long as they
1144 * stay well-behaved.
1146 while (clen
< utflen
) {
1148 clen
++; /* skip UTF-8 introducer byte */
1149 while (clen
< utflen
&&
1150 (unsigned char)utfptr
[clen
] >= 0x80 &&
1151 (unsigned char)utfptr
[clen
] < 0xC0)
1154 pango_layout_set_text(layout
, utfptr
, clen
);
1155 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1156 if (rect
.width
!= n
* cellwidth
) {
1165 pango_layout_set_text(layout
, utfptr
, clen
);
1166 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
1167 gdk_draw_layout(target
, gc
, x
+ (n
*cellwidth
- rect
.width
)/2,
1168 y
+ (pfont
->u
.height
- rect
.height
)/2, layout
);
1170 gdk_draw_layout(target
, gc
, x
+ (n
*cellwidth
- rect
.width
)/2 + pfont
->shadowoffset
,
1171 y
+ (pfont
->u
.height
- rect
.height
)/2, layout
);
1181 g_object_unref(layout
);
1185 * Dummy size value to be used when converting a
1186 * PangoFontDescription of a scalable font to a string for
1189 #define PANGO_DUMMY_SIZE 12
1191 static void pangofont_enum_fonts(GtkWidget
*widget
, fontsel_add_entry callback
,
1195 #ifndef PANGO_PRE_1POINT6
1198 PangoFontFamily
**families
;
1201 ctx
= gtk_widget_get_pango_context(widget
);
1206 * Ask Pango for a list of font families, and iterate through
1209 #ifndef PANGO_PRE_1POINT6
1210 map
= pango_context_get_font_map(ctx
);
1213 pango_font_map_list_families(map
, &families
, &nfamilies
);
1215 pango_context_list_families(ctx
, &families
, &nfamilies
);
1217 for (i
= 0; i
< nfamilies
; i
++) {
1218 PangoFontFamily
*family
= families
[i
];
1219 const char *familyname
;
1221 PangoFontFace
**faces
;
1225 * Set up our flags for this font family, and get the name
1228 flags
= FONTFLAG_CLIENTSIDE
;
1229 #ifndef PANGO_PRE_1POINT4
1231 * In very early versions of Pango, we can't tell
1232 * monospaced fonts from non-monospaced.
1234 if (!pango_font_family_is_monospace(family
))
1235 flags
|= FONTFLAG_NONMONOSPACED
;
1237 familyname
= pango_font_family_get_name(family
);
1240 * Go through the available font faces in this family.
1242 pango_font_family_list_faces(family
, &faces
, &nfaces
);
1243 for (j
= 0; j
< nfaces
; j
++) {
1244 PangoFontFace
*face
= faces
[j
];
1245 PangoFontDescription
*desc
;
1246 const char *facename
;
1248 int k
, nsizes
, dummysize
;
1251 * Get the face name string.
1253 facename
= pango_font_face_get_face_name(face
);
1256 * Set up a font description with what we've got so
1257 * far. We'll fill in the size field manually and then
1258 * call pango_font_description_to_string() to give the
1259 * full real name of the specific font.
1261 desc
= pango_font_face_describe(face
);
1264 * See if this font has a list of specific sizes.
1266 #ifndef PANGO_PRE_1POINT4
1267 pango_font_face_list_sizes(face
, &sizes
, &nsizes
);
1270 * In early versions of Pango, that call wasn't
1271 * supported; we just have to assume everything is
1278 * Write a single entry with a dummy size.
1280 dummysize
= PANGO_DUMMY_SIZE
* PANGO_SCALE
;
1286 * If so, go through them one by one.
1288 for (k
= 0; k
< nsizes
; k
++) {
1292 pango_font_description_set_size(desc
, sizes
[k
]);
1294 fullname
= pango_font_description_to_string(desc
);
1297 * Construct the sorting key for font styles.
1303 n
= pango_font_description_get_weight(desc
);
1304 /* Weight: normal, then lighter, then bolder */
1305 if (n
<= PANGO_WEIGHT_NORMAL
)
1306 n
= PANGO_WEIGHT_NORMAL
- n
;
1307 p
+= sprintf(p
, "%4d", n
);
1309 n
= pango_font_description_get_style(desc
);
1310 p
+= sprintf(p
, " %2d", n
);
1312 n
= pango_font_description_get_stretch(desc
);
1313 /* Stretch: closer to normal sorts earlier */
1314 n
= 2 * abs(PANGO_STRETCH_NORMAL
- n
) +
1315 (n
< PANGO_STRETCH_NORMAL
);
1316 p
+= sprintf(p
, " %2d", n
);
1318 n
= pango_font_description_get_variant(desc
);
1319 p
+= sprintf(p
, " %2d", n
);
1324 * Got everything. Hand off to the callback.
1325 * (The charset string is NULL, because only
1326 * server-side X fonts use it.)
1328 callback(callback_ctx
, fullname
, familyname
, NULL
, facename
,
1330 (sizes
== &dummysize ?
0 : PANGO_PIXELS(sizes
[k
])),
1331 flags
, &pangofont_vtable
);
1335 if (sizes
!= &dummysize
)
1338 pango_font_description_free(desc
);
1345 static char *pangofont_canonify_fontname(GtkWidget
*widget
, const char *name
,
1346 int *size
, int *flags
,
1347 int resolve_aliases
)
1350 * When given a Pango font name to try to make sense of for a
1351 * font selector, we must normalise it to PANGO_DUMMY_SIZE and
1352 * extract its original size (in pixels) into the `size' field.
1355 #ifndef PANGO_PRE_1POINT6
1358 PangoFontDescription
*desc
;
1360 PangoFontMetrics
*metrics
;
1361 char *newname
, *retname
;
1363 desc
= pango_font_description_from_string(name
);
1366 ctx
= gtk_widget_get_pango_context(widget
);
1368 pango_font_description_free(desc
);
1371 if (!pangofont_check_desc_makes_sense(ctx
, desc
)) {
1372 pango_font_description_free(desc
);
1375 #ifndef PANGO_PRE_1POINT6
1376 map
= pango_context_get_font_map(ctx
);
1378 pango_font_description_free(desc
);
1381 fset
= pango_font_map_load_fontset(map
, ctx
, desc
,
1382 pango_context_get_language(ctx
));
1384 fset
= pango_context_load_fontset(ctx
, desc
,
1385 pango_context_get_language(ctx
));
1388 pango_font_description_free(desc
);
1391 metrics
= pango_fontset_get_metrics(fset
);
1393 pango_font_metrics_get_approximate_digit_width(metrics
) == 0) {
1394 pango_font_description_free(desc
);
1395 g_object_unref(fset
);
1399 *size
= PANGO_PIXELS(pango_font_description_get_size(desc
));
1400 *flags
= FONTFLAG_CLIENTSIDE
;
1401 pango_font_description_set_size(desc
, PANGO_DUMMY_SIZE
* PANGO_SCALE
);
1402 newname
= pango_font_description_to_string(desc
);
1403 retname
= dupstr(newname
);
1406 pango_font_metrics_unref(metrics
);
1407 pango_font_description_free(desc
);
1408 g_object_unref(fset
);
1413 static char *pangofont_scale_fontname(GtkWidget
*widget
, const char *name
,
1416 PangoFontDescription
*desc
;
1417 char *newname
, *retname
;
1419 desc
= pango_font_description_from_string(name
);
1422 pango_font_description_set_size(desc
, size
* PANGO_SCALE
);
1423 newname
= pango_font_description_to_string(desc
);
1424 retname
= dupstr(newname
);
1426 pango_font_description_free(desc
);
1431 #endif /* GTK_CHECK_VERSION(2,0,0) */
1433 /* ----------------------------------------------------------------------
1434 * Outermost functions which do the vtable dispatch.
1438 * Complete list of font-type subclasses. Listed in preference
1439 * order for unifont_create(). (That is, in the extremely unlikely
1440 * event that the same font name is valid as both a Pango and an
1441 * X11 font, it will be interpreted as the former in the absence
1442 * of an explicit type-disambiguating prefix.)
1444 * The 'multifont' subclass is omitted here, as discussed above.
1446 static const struct unifont_vtable
*unifont_types
[] = {
1447 #if GTK_CHECK_VERSION(2,0,0)
1454 * Function which takes a font name and processes the optional
1455 * scheme prefix. Returns the tail of the font name suitable for
1456 * passing to individual font scheme functions, and also provides
1457 * a subrange of the unifont_types[] array above.
1459 * The return values `start' and `end' denote a half-open interval
1460 * in unifont_types[]; that is, the correct way to iterate over
1463 * for (i = start; i < end; i++) {...}
1465 static const char *unifont_do_prefix(const char *name
, int *start
, int *end
)
1467 int colonpos
= strcspn(name
, ":");
1470 if (name
[colonpos
]) {
1472 * There's a colon prefix on the font name. Use it to work
1473 * out which subclass to use.
1475 for (i
= 0; i
< lenof(unifont_types
); i
++) {
1476 if (strlen(unifont_types
[i
]->prefix
) == colonpos
&&
1477 !strncmp(unifont_types
[i
]->prefix
, name
, colonpos
)) {
1480 return name
+ colonpos
+ 1;
1484 * None matched, so return an empty scheme list to prevent
1485 * any scheme from being called at all.
1488 return name
+ colonpos
+ 1;
1491 * No colon prefix, so just use all the subclasses.
1494 *end
= lenof(unifont_types
);
1499 unifont
*unifont_create(GtkWidget
*widget
, const char *name
, int wide
,
1500 int bold
, int shadowoffset
, int shadowalways
)
1504 name
= unifont_do_prefix(name
, &start
, &end
);
1506 for (i
= start
; i
< end
; i
++) {
1507 unifont
*ret
= unifont_types
[i
]->create(widget
, name
, wide
, bold
,
1508 shadowoffset
, shadowalways
);
1512 return NULL
; /* font not found in any scheme */
1515 void unifont_destroy(unifont
*font
)
1517 font
->vt
->destroy(font
);
1520 void unifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1521 int x
, int y
, const wchar_t *string
, int len
,
1522 int wide
, int bold
, int cellwidth
)
1524 font
->vt
->draw_text(target
, gc
, font
, x
, y
, string
, len
,
1525 wide
, bold
, cellwidth
);
1528 /* ----------------------------------------------------------------------
1529 * Multiple-font wrapper. This is a type of unifont which encapsulates
1530 * up to two other unifonts, permitting missing glyphs in the main
1531 * font to be filled in by a fallback font.
1533 * This is a type of unifont just like the previous two, but it has a
1534 * separate constructor which is manually called by the client, so it
1535 * doesn't appear in the list of available font types enumerated by
1536 * unifont_create. This means it's not used by unifontsel either, so
1537 * it doesn't need to support any methods except draw_text and
1541 static void multifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1542 int x
, int y
, const wchar_t *string
, int len
,
1543 int wide
, int bold
, int cellwidth
);
1544 static void multifont_destroy(unifont
*font
);
1552 static const struct unifont_vtable multifont_vtable
= {
1553 NULL
, /* creation is done specially */
1557 multifont_draw_text
,
1564 unifont
*multifont_create(GtkWidget
*widget
, const char *name
,
1566 int shadowoffset
, int shadowalways
)
1569 unifont
*font
, *fallback
;
1570 struct multifont
*mfont
;
1572 font
= unifont_create(widget
, name
, wide
, bold
,
1573 shadowoffset
, shadowalways
);
1578 if (font
->want_fallback
) {
1579 for (i
= 0; i
< lenof(unifont_types
); i
++) {
1580 if (unifont_types
[i
]->create_fallback
) {
1581 fallback
= unifont_types
[i
]->create_fallback
1582 (widget
, font
->height
, wide
, bold
,
1583 shadowoffset
, shadowalways
);
1591 * Construct our multifont. Public members are all copied from the
1592 * primary font we're wrapping.
1594 mfont
= snew(struct multifont
);
1595 mfont
->u
.vt
= &multifont_vtable
;
1596 mfont
->u
.width
= font
->width
;
1597 mfont
->u
.ascent
= font
->ascent
;
1598 mfont
->u
.descent
= font
->descent
;
1599 mfont
->u
.height
= font
->height
;
1600 mfont
->u
.public_charset
= font
->public_charset
;
1601 mfont
->u
.want_fallback
= FALSE
; /* shouldn't be needed, but just in case */
1603 mfont
->fallback
= fallback
;
1605 return (unifont
*)mfont
;
1608 static void multifont_destroy(unifont
*font
)
1610 struct multifont
*mfont
= (struct multifont
*)font
;
1611 unifont_destroy(mfont
->main
);
1612 if (mfont
->fallback
)
1613 unifont_destroy(mfont
->fallback
);
1617 static void multifont_draw_text(GdkDrawable
*target
, GdkGC
*gc
, unifont
*font
,
1618 int x
, int y
, const wchar_t *string
, int len
,
1619 int wide
, int bold
, int cellwidth
)
1621 struct multifont
*mfont
= (struct multifont
*)font
;
1626 * Find a maximal sequence of characters which are, or are
1627 * not, supported by our main font.
1629 ok
= mfont
->main
->vt
->has_glyph(mfont
->main
, string
[0]);
1632 !mfont
->main
->vt
->has_glyph(mfont
->main
, string
[i
]) == !ok
;
1638 unifont_draw_text(target
, gc
, ok ? mfont
->main
: mfont
->fallback
,
1639 x
, y
, string
, i
, wide
, bold
, cellwidth
);
1646 #if GTK_CHECK_VERSION(2,0,0)
1648 /* ----------------------------------------------------------------------
1649 * Implementation of a unified font selector. Used on GTK 2 only;
1650 * for GTK 1 we still use the standard font selector.
1653 typedef struct fontinfo fontinfo
;
1655 typedef struct unifontsel_internal
{
1656 /* This must be the structure's first element, for cross-casting */
1658 GtkListStore
*family_model
, *style_model
, *size_model
;
1659 GtkWidget
*family_list
, *style_list
, *size_entry
, *size_list
;
1660 GtkWidget
*filter_buttons
[4];
1661 GtkWidget
*preview_area
;
1662 GdkPixmap
*preview_pixmap
;
1663 int preview_width
, preview_height
;
1664 GdkColor preview_fg
, preview_bg
;
1666 tree234
*fonts_by_realname
, *fonts_by_selorder
;
1668 int selsize
, intendedsize
;
1669 int inhibit_response
; /* inhibit callbacks when we change GUI controls */
1670 } unifontsel_internal
;
1673 * The structure held in the tree234s. All the string members are
1674 * part of the same allocated area, so don't need freeing
1679 char *family
, *charset
, *style
, *stylekey
;
1682 * Fallback sorting key, to permit multiple identical entries
1683 * to exist in the selorder tree.
1687 * Indices mapping fontinfo structures to indices in the list
1688 * boxes. sizeindex is irrelevant if the font is scalable
1691 int familyindex
, styleindex
, sizeindex
;
1693 * The class of font.
1695 const struct unifont_vtable
*fontclass
;
1698 struct fontinfo_realname_find
{
1699 const char *realname
;
1703 static int strnullcasecmp(const char *a
, const char *b
)
1708 * If exactly one of the inputs is NULL, it compares before
1711 if ((i
= (!b
) - (!a
)) != 0)
1715 * NULL compares equal.
1721 * Otherwise, ordinary strcasecmp.
1723 return g_ascii_strcasecmp(a
, b
);
1726 static int fontinfo_realname_compare(void *av
, void *bv
)
1728 fontinfo
*a
= (fontinfo
*)av
;
1729 fontinfo
*b
= (fontinfo
*)bv
;
1732 if ((i
= strnullcasecmp(a
->realname
, b
->realname
)) != 0)
1734 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1735 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1736 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1740 static int fontinfo_realname_find(void *av
, void *bv
)
1742 struct fontinfo_realname_find
*a
= (struct fontinfo_realname_find
*)av
;
1743 fontinfo
*b
= (fontinfo
*)bv
;
1746 if ((i
= strnullcasecmp(a
->realname
, b
->realname
)) != 0)
1748 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1749 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1750 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1754 static int fontinfo_selorder_compare(void *av
, void *bv
)
1756 fontinfo
*a
= (fontinfo
*)av
;
1757 fontinfo
*b
= (fontinfo
*)bv
;
1759 if ((i
= strnullcasecmp(a
->family
, b
->family
)) != 0)
1762 * Font class comes immediately after family, so that fonts
1763 * from different classes with the same family
1765 if ((a
->flags
& FONTFLAG_SORT_MASK
) != (b
->flags
& FONTFLAG_SORT_MASK
))
1766 return ((a
->flags
& FONTFLAG_SORT_MASK
) <
1767 (b
->flags
& FONTFLAG_SORT_MASK
) ?
-1 : +1);
1768 if ((i
= strnullcasecmp(a
->charset
, b
->charset
)) != 0)
1770 if ((i
= strnullcasecmp(a
->stylekey
, b
->stylekey
)) != 0)
1772 if ((i
= strnullcasecmp(a
->style
, b
->style
)) != 0)
1774 if (a
->size
!= b
->size
)
1775 return (a
->size
< b
->size ?
-1 : +1);
1776 if (a
->index
!= b
->index
)
1777 return (a
->index
< b
->index ?
-1 : +1);
1781 static void unifontsel_deselect(unifontsel_internal
*fs
)
1783 fs
->selected
= NULL
;
1784 gtk_list_store_clear(fs
->style_model
);
1785 gtk_list_store_clear(fs
->size_model
);
1786 gtk_widget_set_sensitive(fs
->u
.ok_button
, FALSE
);
1787 gtk_widget_set_sensitive(fs
->size_entry
, FALSE
);
1790 static void unifontsel_setup_familylist(unifontsel_internal
*fs
)
1793 int i
, listindex
, minpos
= -1, maxpos
= -1;
1794 char *currfamily
= NULL
;
1798 gtk_list_store_clear(fs
->family_model
);
1802 * Search through the font tree for anything matching our
1803 * current filter criteria. When we find one, add its font
1804 * name to the list box.
1806 for (i
= 0 ;; i
++) {
1807 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1809 * info may be NULL if we've just run off the end of the
1810 * tree. We must still do a processing pass in that
1811 * situation, in case we had an unfinished font record in
1814 if (info
&& (info
->flags
&~ fs
->filter_flags
)) {
1815 info
->familyindex
= -1;
1816 continue; /* we're filtering out this font */
1818 if (!info
|| strnullcasecmp(currfamily
, info
->family
) ||
1819 currflags
!= (info
->flags
& FONTFLAG_SORT_MASK
)) {
1821 * We've either finished a family, or started a new
1825 gtk_list_store_append(fs
->family_model
, &iter
);
1826 gtk_list_store_set(fs
->family_model
, &iter
,
1827 0, currfamily
, 1, minpos
, 2, maxpos
+1, -1);
1832 currfamily
= info
->family
;
1833 currflags
= info
->flags
& FONTFLAG_SORT_MASK
;
1837 break; /* now we're done */
1838 info
->familyindex
= listindex
;
1843 * If we've just filtered out the previously selected font,
1844 * deselect it thoroughly.
1846 if (fs
->selected
&& fs
->selected
->familyindex
< 0)
1847 unifontsel_deselect(fs
);
1850 static void unifontsel_setup_stylelist(unifontsel_internal
*fs
,
1854 int i
, listindex
, minpos
= -1, maxpos
= -1, started
= FALSE
;
1855 char *currcs
= NULL
, *currstyle
= NULL
;
1858 gtk_list_store_clear(fs
->style_model
);
1863 * Search through the font tree for anything matching our
1864 * current filter criteria. When we find one, add its charset
1865 * and/or style name to the list box.
1867 for (i
= start
; i
<= end
; i
++) {
1871 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1873 * info may be NULL if we've just run off the end of the
1874 * relevant data. We must still do a processing pass in
1875 * that situation, in case we had an unfinished font
1876 * record in progress.
1878 if (info
&& (info
->flags
&~ fs
->filter_flags
)) {
1879 info
->styleindex
= -1;
1880 continue; /* we're filtering out this font */
1882 if (!info
|| !started
|| strnullcasecmp(currcs
, info
->charset
) ||
1883 strnullcasecmp(currstyle
, info
->style
)) {
1885 * We've either finished a style/charset, or started a
1890 gtk_list_store_append(fs
->style_model
, &iter
);
1891 gtk_list_store_set(fs
->style_model
, &iter
,
1892 0, currstyle
, 1, minpos
, 2, maxpos
+1,
1898 if (info
->charset
&& strnullcasecmp(currcs
, info
->charset
)) {
1899 gtk_list_store_append(fs
->style_model
, &iter
);
1900 gtk_list_store_set(fs
->style_model
, &iter
,
1901 0, info
->charset
, 1, -1, 2, -1,
1905 currcs
= info
->charset
;
1906 currstyle
= info
->style
;
1910 break; /* now we're done */
1911 info
->styleindex
= listindex
;
1916 static const int unifontsel_default_sizes
[] = { 10, 12, 14, 16, 20, 24, 32 };
1918 static void unifontsel_setup_sizelist(unifontsel_internal
*fs
,
1926 gtk_list_store_clear(fs
->size_model
);
1930 * Search through the font tree for anything matching our
1931 * current filter criteria. When we find one, add its font
1932 * name to the list box.
1934 for (i
= start
; i
< end
; i
++) {
1935 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, i
);
1936 if (info
->flags
&~ fs
->filter_flags
) {
1937 info
->sizeindex
= -1;
1938 continue; /* we're filtering out this font */
1941 sprintf(sizetext
, "%d", info
->size
);
1942 info
->sizeindex
= listindex
;
1943 gtk_list_store_append(fs
->size_model
, &iter
);
1944 gtk_list_store_set(fs
->size_model
, &iter
,
1945 0, sizetext
, 1, i
, 2, info
->size
, -1);
1953 for (j
= 0; j
< lenof(unifontsel_default_sizes
); j
++) {
1954 sprintf(sizetext
, "%d", unifontsel_default_sizes
[j
]);
1955 gtk_list_store_append(fs
->size_model
, &iter
);
1956 gtk_list_store_set(fs
->size_model
, &iter
, 0, sizetext
, 1, i
,
1957 2, unifontsel_default_sizes
[j
], -1);
1964 static void unifontsel_set_filter_buttons(unifontsel_internal
*fs
)
1968 for (i
= 0; i
< lenof(fs
->filter_buttons
); i
++) {
1969 int flagbit
= GPOINTER_TO_INT(gtk_object_get_data
1970 (GTK_OBJECT(fs
->filter_buttons
[i
]),
1972 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs
->filter_buttons
[i
]),
1973 !!(fs
->filter_flags
& flagbit
));
1977 static void unifontsel_draw_preview_text(unifontsel_internal
*fs
)
1980 char *sizename
= NULL
;
1981 fontinfo
*info
= fs
->selected
;
1984 sizename
= info
->fontclass
->scale_fontname
1985 (GTK_WIDGET(fs
->u
.window
), info
->realname
, fs
->selsize
);
1986 font
= info
->fontclass
->create(GTK_WIDGET(fs
->u
.window
),
1987 sizename ? sizename
: info
->realname
,
1988 FALSE
, FALSE
, 0, 0);
1992 if (fs
->preview_pixmap
) {
1993 GdkGC
*gc
= gdk_gc_new(fs
->preview_pixmap
);
1994 gdk_gc_set_foreground(gc
, &fs
->preview_bg
);
1995 gdk_draw_rectangle(fs
->preview_pixmap
, gc
, 1, 0, 0,
1996 fs
->preview_width
, fs
->preview_height
);
1997 gdk_gc_set_foreground(gc
, &fs
->preview_fg
);
2000 * The pangram used here is rather carefully
2001 * constructed: it contains a sequence of very narrow
2002 * letters (`jil') and a pair of adjacent very wide
2005 * If the user selects a proportional font, it will be
2006 * coerced into fixed-width character cells when used
2007 * in the actual terminal window. We therefore display
2008 * it the same way in the preview pane, so as to show
2009 * it the way it will actually be displayed - and we
2010 * deliberately pick a pangram which will show the
2011 * resulting miskerning at its worst.
2013 * We aren't trying to sell people these fonts; we're
2014 * trying to let them make an informed choice. Better
2015 * that they find out the problems with using
2016 * proportional fonts in terminal windows here than
2017 * that they go to the effort of selecting their font
2018 * and _then_ realise it was a mistake.
2020 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2022 L
"bankrupt jilted showmen quiz convex fogey",
2023 41, FALSE
, FALSE
, font
->width
);
2024 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2025 0, font
->ascent
+ font
->height
,
2026 L
"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
2027 41, FALSE
, FALSE
, font
->width
);
2029 * The ordering of punctuation here is also selected
2030 * with some specific aims in mind. I put ` and '
2031 * together because some software (and people) still
2032 * use them as matched quotes no matter what Unicode
2033 * might say on the matter, so people can quickly
2034 * check whether they look silly in a candidate font.
2035 * The sequence #_@ is there to let people judge the
2036 * suitability of the underscore as an effectively
2037 * alphabetic character (since that's how it's often
2038 * used in practice, at least by programmers).
2040 info
->fontclass
->draw_text(fs
->preview_pixmap
, gc
, font
,
2041 0, font
->ascent
+ font
->height
* 2,
2042 L
"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
2043 42, FALSE
, FALSE
, font
->width
);
2046 gdk_window_invalidate_rect(fs
->preview_area
->window
, NULL
, FALSE
);
2049 info
->fontclass
->destroy(font
);
2054 static void unifontsel_select_font(unifontsel_internal
*fs
,
2055 fontinfo
*info
, int size
, int leftlist
,
2056 int size_is_explicit
)
2060 GtkTreePath
*treepath
;
2063 fs
->inhibit_response
= TRUE
;
2065 fs
->selected
= info
;
2067 if (size_is_explicit
)
2068 fs
->intendedsize
= size
;
2070 gtk_widget_set_sensitive(fs
->u
.ok_button
, TRUE
);
2073 * Find the index of this fontinfo in the selorder list.
2076 findpos234(fs
->fonts_by_selorder
, info
, NULL
, &index
);
2080 * Adjust the font selector flags and redo the font family
2081 * list box, if necessary.
2083 if (leftlist
<= 0 &&
2084 (fs
->filter_flags
| info
->flags
) != fs
->filter_flags
) {
2085 fs
->filter_flags
|= info
->flags
;
2086 unifontsel_set_filter_buttons(fs
);
2087 unifontsel_setup_familylist(fs
);
2091 * Find the appropriate family name and select it in the list.
2093 assert(info
->familyindex
>= 0);
2094 treepath
= gtk_tree_path_new_from_indices(info
->familyindex
, -1);
2095 gtk_tree_selection_select_path
2096 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->family_list
)),
2098 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->family_list
),
2099 treepath
, NULL
, FALSE
, 0.0, 0.0);
2100 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->family_model
), &iter
, treepath
);
2101 gtk_tree_path_free(treepath
);
2104 * Now set up the font style list.
2106 gtk_tree_model_get(GTK_TREE_MODEL(fs
->family_model
), &iter
,
2107 1, &minval
, 2, &maxval
, -1);
2109 unifontsel_setup_stylelist(fs
, minval
, maxval
);
2112 * Find the appropriate style name and select it in the list.
2115 assert(info
->styleindex
>= 0);
2116 treepath
= gtk_tree_path_new_from_indices(info
->styleindex
, -1);
2117 gtk_tree_selection_select_path
2118 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->style_list
)),
2120 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->style_list
),
2121 treepath
, NULL
, FALSE
, 0.0, 0.0);
2122 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->style_model
),
2124 gtk_tree_path_free(treepath
);
2127 * And set up the size list.
2129 gtk_tree_model_get(GTK_TREE_MODEL(fs
->style_model
), &iter
,
2130 1, &minval
, 2, &maxval
, -1);
2132 unifontsel_setup_sizelist(fs
, minval
, maxval
);
2135 * Find the appropriate size, and select it in the list.
2138 assert(info
->sizeindex
>= 0);
2139 treepath
= gtk_tree_path_new_from_indices(info
->sizeindex
, -1);
2140 gtk_tree_selection_select_path
2141 (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs
->size_list
)),
2143 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->size_list
),
2144 treepath
, NULL
, FALSE
, 0.0, 0.0);
2145 gtk_tree_path_free(treepath
);
2149 for (j
= 0; j
< lenof(unifontsel_default_sizes
); j
++)
2150 if (unifontsel_default_sizes
[j
] == size
) {
2151 treepath
= gtk_tree_path_new_from_indices(j
, -1);
2152 gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs
->size_list
),
2153 treepath
, NULL
, FALSE
);
2154 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs
->size_list
),
2155 treepath
, NULL
, FALSE
, 0.0,
2157 gtk_tree_path_free(treepath
);
2162 * And set up the font size text entry box.
2166 sprintf(sizetext
, "%d", size
);
2167 gtk_entry_set_text(GTK_ENTRY(fs
->size_entry
), sizetext
);
2171 unifontsel_setup_sizelist(fs
, 0, 0);
2172 gtk_entry_set_text(GTK_ENTRY(fs
->size_entry
), "");
2176 * Grey out the font size edit box if we're not using a
2179 gtk_entry_set_editable(GTK_ENTRY(fs
->size_entry
), fs
->selected
->size
== 0);
2180 gtk_widget_set_sensitive(fs
->size_entry
, fs
->selected
->size
== 0);
2182 unifontsel_draw_preview_text(fs
);
2184 fs
->inhibit_response
= FALSE
;
2187 static void unifontsel_button_toggled(GtkToggleButton
*tb
, gpointer data
)
2189 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2190 int newstate
= gtk_toggle_button_get_active(tb
);
2192 int flagbit
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(tb
),
2196 newflags
= fs
->filter_flags
| flagbit
;
2198 newflags
= fs
->filter_flags
& ~flagbit
;
2200 if (fs
->filter_flags
!= newflags
) {
2201 fs
->filter_flags
= newflags
;
2202 unifontsel_setup_familylist(fs
);
2206 static void unifontsel_add_entry(void *ctx
, const char *realfontname
,
2207 const char *family
, const char *charset
,
2208 const char *style
, const char *stylekey
,
2209 int size
, int flags
,
2210 const struct unifont_vtable
*fontclass
)
2212 unifontsel_internal
*fs
= (unifontsel_internal
*)ctx
;
2217 totalsize
= sizeof(fontinfo
) + strlen(realfontname
) +
2218 (family ?
strlen(family
) : 0) + (charset ?
strlen(charset
) : 0) +
2219 (style ?
strlen(style
) : 0) + (stylekey ?
strlen(stylekey
) : 0) + 10;
2220 info
= (fontinfo
*)smalloc(totalsize
);
2221 info
->fontclass
= fontclass
;
2222 p
= (char *)info
+ sizeof(fontinfo
);
2224 strcpy(p
, realfontname
);
2231 info
->family
= NULL
;
2237 info
->charset
= NULL
;
2246 strcpy(p
, stylekey
);
2249 info
->stylekey
= NULL
;
2250 assert(p
- (char *)info
<= totalsize
);
2252 info
->flags
= flags
;
2253 info
->index
= count234(fs
->fonts_by_selorder
);
2256 * It's just conceivable that a misbehaving font enumerator
2257 * might tell us about the same font real name more than once,
2258 * in which case we should silently drop the new one.
2260 if (add234(fs
->fonts_by_realname
, info
) != info
) {
2265 * However, we should never get a duplicate key in the
2266 * selorder tree, because the index field carefully
2267 * disambiguates otherwise identical records.
2269 add234(fs
->fonts_by_selorder
, info
);
2272 static fontinfo
*update_for_intended_size(unifontsel_internal
*fs
,
2275 fontinfo info2
, *below
, *above
;
2279 * Copy the info structure. This doesn't copy its dynamic
2280 * string fields, but that's unimportant because all we're
2281 * going to do is to adjust the size field and use it in one
2285 info2
.size
= fs
->intendedsize
;
2288 * Search in the tree to find the fontinfo structure which
2289 * best approximates the size the user last requested.
2291 below
= findrelpos234(fs
->fonts_by_selorder
, &info2
, NULL
,
2295 above
= index234(fs
->fonts_by_selorder
, pos
+1);
2298 * See if we've found it exactly, which is an easy special
2299 * case. If we have, it'll be in `below' and not `above',
2300 * because we did a REL234_LE rather than REL234_LT search.
2302 if (below
&& !fontinfo_selorder_compare(&info2
, below
))
2306 * Now we've either found two suitable fonts, one smaller and
2307 * one larger, or we're at one or other extreme end of the
2308 * scale. Find out which, by NULLing out either of below and
2309 * above if it differs from this one in any respect but size
2310 * (and the disambiguating index field). Bear in mind, also,
2311 * that either one might _already_ be NULL if we're at the
2312 * extreme ends of the font list.
2315 info2
.size
= below
->size
;
2316 info2
.index
= below
->index
;
2317 if (fontinfo_selorder_compare(&info2
, below
))
2321 info2
.size
= above
->size
;
2322 info2
.index
= above
->index
;
2323 if (fontinfo_selorder_compare(&info2
, above
))
2328 * Now return whichever of above and below is non-NULL, if
2329 * that's unambiguous.
2337 * And now we really do have to make a choice about whether to
2338 * round up or down. We'll do it by rounding to nearest,
2339 * breaking ties by rounding up.
2341 if (above
->size
- fs
->intendedsize
<= fs
->intendedsize
- below
->size
)
2347 static void family_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2349 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2350 GtkTreeModel
*treemodel
;
2351 GtkTreeIter treeiter
;
2355 if (fs
->inhibit_response
) /* we made this change ourselves */
2358 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2361 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, -1);
2362 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2363 info
= update_for_intended_size(fs
, info
);
2365 return; /* _shouldn't_ happen unless font list is completely funted */
2367 fs
->selsize
= fs
->intendedsize
; /* font is scalable */
2368 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: fs
->selsize
,
2372 static void style_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2374 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2375 GtkTreeModel
*treemodel
;
2376 GtkTreeIter treeiter
;
2380 if (fs
->inhibit_response
) /* we made this change ourselves */
2383 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2386 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, -1);
2388 return; /* somehow a charset heading got clicked */
2389 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2390 info
= update_for_intended_size(fs
, info
);
2392 return; /* _shouldn't_ happen unless font list is completely funted */
2394 fs
->selsize
= fs
->intendedsize
; /* font is scalable */
2395 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: fs
->selsize
,
2399 static void size_changed(GtkTreeSelection
*treeselection
, gpointer data
)
2401 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2402 GtkTreeModel
*treemodel
;
2403 GtkTreeIter treeiter
;
2407 if (fs
->inhibit_response
) /* we made this change ourselves */
2410 if (!gtk_tree_selection_get_selected(treeselection
, &treemodel
, &treeiter
))
2413 gtk_tree_model_get(treemodel
, &treeiter
, 1, &minval
, 2, &size
, -1);
2414 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2415 unifontsel_select_font(fs
, info
, info
->size ? info
->size
: size
, 3, TRUE
);
2418 static void size_entry_changed(GtkEditable
*ed
, gpointer data
)
2420 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2424 if (fs
->inhibit_response
) /* we made this change ourselves */
2427 text
= gtk_entry_get_text(GTK_ENTRY(ed
));
2431 assert(fs
->selected
->size
== 0);
2432 unifontsel_select_font(fs
, fs
->selected
, size
, 3, TRUE
);
2436 static void alias_resolve(GtkTreeView
*treeview
, GtkTreePath
*path
,
2437 GtkTreeViewColumn
*column
, gpointer data
)
2439 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2441 int minval
, newsize
;
2442 fontinfo
*info
, *newinfo
;
2445 if (fs
->inhibit_response
) /* we made this change ourselves */
2448 gtk_tree_model_get_iter(GTK_TREE_MODEL(fs
->family_model
), &iter
, path
);
2449 gtk_tree_model_get(GTK_TREE_MODEL(fs
->family_model
), &iter
, 1,&minval
, -1);
2450 info
= (fontinfo
*)index234(fs
->fonts_by_selorder
, minval
);
2453 struct fontinfo_realname_find f
;
2455 newname
= info
->fontclass
->canonify_fontname
2456 (GTK_WIDGET(fs
->u
.window
), info
->realname
, &newsize
, &flags
, TRUE
);
2458 f
.realname
= newname
;
2460 newinfo
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2464 return; /* font name not in our index */
2465 if (newinfo
== info
)
2466 return; /* didn't change under canonification => not an alias */
2467 unifontsel_select_font(fs
, newinfo
,
2468 newinfo
->size ? newinfo
->size
: newsize
,
2473 static gint
unifontsel_expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
2476 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2478 if (fs
->preview_pixmap
) {
2479 gdk_draw_pixmap(widget
->window
,
2480 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
2482 event
->area
.x
, event
->area
.y
,
2483 event
->area
.x
, event
->area
.y
,
2484 event
->area
.width
, event
->area
.height
);
2489 static gint
unifontsel_configure_area(GtkWidget
*widget
,
2490 GdkEventConfigure
*event
, gpointer data
)
2492 unifontsel_internal
*fs
= (unifontsel_internal
*)data
;
2493 int ox
, oy
, nx
, ny
, x
, y
;
2496 * Enlarge the pixmap, but never shrink it.
2498 ox
= fs
->preview_width
;
2499 oy
= fs
->preview_height
;
2502 if (x
> ox
|| y
> oy
) {
2503 if (fs
->preview_pixmap
)
2504 gdk_pixmap_unref(fs
->preview_pixmap
);
2506 nx
= (x
> ox ? x
: ox
);
2507 ny
= (y
> oy ? y
: oy
);
2508 fs
->preview_pixmap
= gdk_pixmap_new(widget
->window
, nx
, ny
, -1);
2509 fs
->preview_width
= nx
;
2510 fs
->preview_height
= ny
;
2512 unifontsel_draw_preview_text(fs
);
2515 gdk_window_invalidate_rect(widget
->window
, NULL
, FALSE
);
2520 unifontsel
*unifontsel_new(const char *wintitle
)
2522 unifontsel_internal
*fs
= snew(unifontsel_internal
);
2523 GtkWidget
*table
, *label
, *w
, *ww
, *scroll
;
2524 GtkListStore
*model
;
2525 GtkTreeViewColumn
*column
;
2526 int lists_height
, preview_height
, font_width
, style_width
, size_width
;
2529 fs
->inhibit_response
= FALSE
;
2530 fs
->selected
= NULL
;
2534 * Invent some magic size constants.
2537 label
= gtk_label_new("Quite Long Font Name (Foundry)");
2538 gtk_widget_size_request(label
, &req
);
2539 font_width
= req
.width
;
2540 lists_height
= 14 * req
.height
;
2541 preview_height
= 5 * req
.height
;
2542 gtk_label_set_text(GTK_LABEL(label
), "Italic Extra Condensed");
2543 gtk_widget_size_request(label
, &req
);
2544 style_width
= req
.width
;
2545 gtk_label_set_text(GTK_LABEL(label
), "48000");
2546 gtk_widget_size_request(label
, &req
);
2547 size_width
= req
.width
;
2548 #if GTK_CHECK_VERSION(2,10,0)
2549 g_object_ref_sink(label
);
2550 g_object_unref(label
);
2552 gtk_object_sink(GTK_OBJECT(label
));
2557 * Create the dialog box and initialise the user-visible
2558 * fields in the returned structure.
2560 fs
->u
.user_data
= NULL
;
2561 fs
->u
.window
= GTK_WINDOW(gtk_dialog_new());
2562 gtk_window_set_title(fs
->u
.window
, wintitle
);
2563 fs
->u
.cancel_button
= gtk_dialog_add_button
2564 (GTK_DIALOG(fs
->u
.window
), GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
);
2565 fs
->u
.ok_button
= gtk_dialog_add_button
2566 (GTK_DIALOG(fs
->u
.window
), GTK_STOCK_OK
, GTK_RESPONSE_OK
);
2567 gtk_widget_grab_default(fs
->u
.ok_button
);
2570 * Now set up the internal fields, including in particular all
2571 * the controls that actually allow the user to select fonts.
2573 table
= gtk_table_new(8, 3, FALSE
);
2574 gtk_widget_show(table
);
2575 gtk_table_set_col_spacings(GTK_TABLE(table
), 8);
2576 #if GTK_CHECK_VERSION(2,4,0)
2577 /* GtkAlignment seems to be the simplest way to put padding round things */
2578 w
= gtk_alignment_new(0, 0, 1, 1);
2579 gtk_alignment_set_padding(GTK_ALIGNMENT(w
), 8, 8, 8, 8);
2580 gtk_container_add(GTK_CONTAINER(w
), table
);
2585 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fs
->u
.window
)->vbox
),
2588 label
= gtk_label_new_with_mnemonic("_Font:");
2589 gtk_widget_show(label
);
2590 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2591 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1, GTK_FILL
, 0, 0, 0);
2594 * The Font list box displays only a string, but additionally
2595 * stores two integers which give the limits within the
2596 * tree234 of the font entries covered by this list entry.
2598 model
= gtk_list_store_new(3, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
);
2599 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2600 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2601 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2603 column
= gtk_tree_view_column_new_with_attributes
2604 ("Font", gtk_cell_renderer_text_new(),
2605 "text", 0, (char *)NULL
);
2606 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2607 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2608 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2609 "changed", G_CALLBACK(family_changed
), fs
);
2610 g_signal_connect(G_OBJECT(w
), "row-activated",
2611 G_CALLBACK(alias_resolve
), fs
);
2613 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2614 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2616 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2617 gtk_widget_show(scroll
);
2618 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2619 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2620 gtk_widget_set_size_request(scroll
, font_width
, lists_height
);
2621 gtk_table_attach(GTK_TABLE(table
), scroll
, 0, 1, 1, 3, GTK_FILL
,
2622 GTK_EXPAND
| GTK_FILL
, 0, 0);
2623 fs
->family_model
= model
;
2624 fs
->family_list
= w
;
2626 label
= gtk_label_new_with_mnemonic("_Style:");
2627 gtk_widget_show(label
);
2628 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2629 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 0, 1, GTK_FILL
, 0, 0, 0);
2632 * The Style list box can contain insensitive elements
2633 * (character set headings for server-side fonts), so we add
2634 * an extra column to the list store to hold that information.
2636 model
= gtk_list_store_new(4, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
,
2638 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2639 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2640 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2642 column
= gtk_tree_view_column_new_with_attributes
2643 ("Style", gtk_cell_renderer_text_new(),
2644 "text", 0, "sensitive", 3, (char *)NULL
);
2645 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2646 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2647 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2648 "changed", G_CALLBACK(style_changed
), fs
);
2650 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2651 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2653 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2654 gtk_widget_show(scroll
);
2655 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2656 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2657 gtk_widget_set_size_request(scroll
, style_width
, lists_height
);
2658 gtk_table_attach(GTK_TABLE(table
), scroll
, 1, 2, 1, 3, GTK_FILL
,
2659 GTK_EXPAND
| GTK_FILL
, 0, 0);
2660 fs
->style_model
= model
;
2663 label
= gtk_label_new_with_mnemonic("Si_ze:");
2664 gtk_widget_show(label
);
2665 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
2666 gtk_table_attach(GTK_TABLE(table
), label
, 2, 3, 0, 1, GTK_FILL
, 0, 0, 0);
2669 * The Size label attaches primarily to a text input box so
2670 * that the user can select a size of their choice. The list
2671 * of available sizes is secondary.
2673 fs
->size_entry
= w
= gtk_entry_new();
2674 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), w
);
2675 gtk_widget_set_size_request(w
, size_width
, -1);
2677 gtk_table_attach(GTK_TABLE(table
), w
, 2, 3, 1, 2, GTK_FILL
, 0, 0, 0);
2678 g_signal_connect(G_OBJECT(w
), "changed", G_CALLBACK(size_entry_changed
),
2681 model
= gtk_list_store_new(3, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_INT
);
2682 w
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(model
));
2683 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w
), FALSE
);
2685 column
= gtk_tree_view_column_new_with_attributes
2686 ("Size", gtk_cell_renderer_text_new(),
2687 "text", 0, (char *)NULL
);
2688 gtk_tree_view_column_set_sizing(column
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
2689 gtk_tree_view_append_column(GTK_TREE_VIEW(w
), column
);
2690 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w
))),
2691 "changed", G_CALLBACK(size_changed
), fs
);
2693 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
2694 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
2696 gtk_container_add(GTK_CONTAINER(scroll
), w
);
2697 gtk_widget_show(scroll
);
2698 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
2699 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2700 gtk_table_attach(GTK_TABLE(table
), scroll
, 2, 3, 2, 3, GTK_FILL
,
2701 GTK_EXPAND
| GTK_FILL
, 0, 0);
2702 fs
->size_model
= model
;
2708 fs
->preview_area
= gtk_drawing_area_new();
2709 fs
->preview_pixmap
= NULL
;
2710 fs
->preview_width
= 0;
2711 fs
->preview_height
= 0;
2712 fs
->preview_fg
.pixel
= fs
->preview_bg
.pixel
= 0;
2713 fs
->preview_fg
.red
= fs
->preview_fg
.green
= fs
->preview_fg
.blue
= 0x0000;
2714 fs
->preview_bg
.red
= fs
->preview_bg
.green
= fs
->preview_bg
.blue
= 0xFFFF;
2715 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs
->preview_fg
,
2717 gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs
->preview_bg
,
2719 gtk_signal_connect(GTK_OBJECT(fs
->preview_area
), "expose_event",
2720 GTK_SIGNAL_FUNC(unifontsel_expose_area
), fs
);
2721 gtk_signal_connect(GTK_OBJECT(fs
->preview_area
), "configure_event",
2722 GTK_SIGNAL_FUNC(unifontsel_configure_area
), fs
);
2723 gtk_widget_set_size_request(fs
->preview_area
, 1, preview_height
);
2724 gtk_widget_show(fs
->preview_area
);
2725 ww
= fs
->preview_area
;
2726 w
= gtk_frame_new(NULL
);
2727 gtk_container_add(GTK_CONTAINER(w
), ww
);
2729 #if GTK_CHECK_VERSION(2,4,0)
2731 /* GtkAlignment seems to be the simplest way to put padding round things */
2732 w
= gtk_alignment_new(0, 0, 1, 1);
2733 gtk_alignment_set_padding(GTK_ALIGNMENT(w
), 8, 8, 8, 8);
2734 gtk_container_add(GTK_CONTAINER(w
), ww
);
2738 w
= gtk_frame_new("Preview of font");
2739 gtk_container_add(GTK_CONTAINER(w
), ww
);
2741 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 3, 4,
2742 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
| GTK_FILL
, 0, 8);
2745 w
= gtk_check_button_new_with_label("Show client-side fonts");
2746 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2747 GINT_TO_POINTER(FONTFLAG_CLIENTSIDE
));
2748 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2749 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2751 fs
->filter_buttons
[i
++] = w
;
2752 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 4, 5, GTK_FILL
, 0, 0, 0);
2753 w
= gtk_check_button_new_with_label("Show server-side fonts");
2754 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2755 GINT_TO_POINTER(FONTFLAG_SERVERSIDE
));
2756 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2757 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2759 fs
->filter_buttons
[i
++] = w
;
2760 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 5, 6, GTK_FILL
, 0, 0, 0);
2761 w
= gtk_check_button_new_with_label("Show server-side font aliases");
2762 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2763 GINT_TO_POINTER(FONTFLAG_SERVERALIAS
));
2764 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2765 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2767 fs
->filter_buttons
[i
++] = w
;
2768 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 6, 7, GTK_FILL
, 0, 0, 0);
2769 w
= gtk_check_button_new_with_label("Show non-monospaced fonts");
2770 gtk_object_set_data(GTK_OBJECT(w
), "user-data",
2771 GINT_TO_POINTER(FONTFLAG_NONMONOSPACED
));
2772 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
2773 GTK_SIGNAL_FUNC(unifontsel_button_toggled
), fs
);
2775 fs
->filter_buttons
[i
++] = w
;
2776 gtk_table_attach(GTK_TABLE(table
), w
, 0, 3, 7, 8, GTK_FILL
, 0, 0, 0);
2778 assert(i
== lenof(fs
->filter_buttons
));
2779 fs
->filter_flags
= FONTFLAG_CLIENTSIDE
| FONTFLAG_SERVERSIDE
|
2780 FONTFLAG_SERVERALIAS
;
2781 unifontsel_set_filter_buttons(fs
);
2784 * Go and find all the font names, and set up our master font
2787 fs
->fonts_by_realname
= newtree234(fontinfo_realname_compare
);
2788 fs
->fonts_by_selorder
= newtree234(fontinfo_selorder_compare
);
2789 for (i
= 0; i
< lenof(unifont_types
); i
++)
2790 unifont_types
[i
]->enum_fonts(GTK_WIDGET(fs
->u
.window
),
2791 unifontsel_add_entry
, fs
);
2794 * And set up the initial font names list.
2796 unifontsel_setup_familylist(fs
);
2798 fs
->selsize
= fs
->intendedsize
= 13; /* random default */
2799 gtk_widget_set_sensitive(fs
->u
.ok_button
, FALSE
);
2801 return (unifontsel
*)fs
;
2804 void unifontsel_destroy(unifontsel
*fontsel
)
2806 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2809 if (fs
->preview_pixmap
)
2810 gdk_pixmap_unref(fs
->preview_pixmap
);
2812 freetree234(fs
->fonts_by_selorder
);
2813 while ((info
= delpos234(fs
->fonts_by_realname
, 0)) != NULL
)
2815 freetree234(fs
->fonts_by_realname
);
2817 gtk_widget_destroy(GTK_WIDGET(fs
->u
.window
));
2821 void unifontsel_set_name(unifontsel
*fontsel
, const char *fontname
)
2823 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2824 int i
, start
, end
, size
, flags
;
2825 const char *fontname2
= NULL
;
2829 * Provide a default if given an empty or null font name.
2831 if (!fontname
|| !*fontname
)
2832 fontname
= "server:fixed";
2835 * Call the canonify_fontname function.
2837 fontname
= unifont_do_prefix(fontname
, &start
, &end
);
2838 for (i
= start
; i
< end
; i
++) {
2839 fontname2
= unifont_types
[i
]->canonify_fontname
2840 (GTK_WIDGET(fs
->u
.window
), fontname
, &size
, &flags
, FALSE
);
2845 return; /* font name not recognised */
2848 * Now look up the canonified font name in our index.
2851 struct fontinfo_realname_find f
;
2852 f
.realname
= fontname2
;
2854 info
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2858 * If we've found the font, and its size field is either
2859 * correct or zero (the latter indicating a scalable font),
2860 * then we're done. Otherwise, try looking up the original
2861 * font name instead.
2863 if (!info
|| (info
->size
!= size
&& info
->size
!= 0)) {
2864 struct fontinfo_realname_find f
;
2865 f
.realname
= fontname
;
2868 info
= find234(fs
->fonts_by_realname
, &f
, fontinfo_realname_find
);
2869 if (!info
|| info
->size
!= size
)
2870 return; /* font name not in our index */
2874 * Now we've got a fontinfo structure and a font size, so we
2875 * know everything we need to fill in all the fields in the
2878 unifontsel_select_font(fs
, info
, size
, 0, TRUE
);
2881 char *unifontsel_get_name(unifontsel
*fontsel
)
2883 unifontsel_internal
*fs
= (unifontsel_internal
*)fontsel
;
2889 if (fs
->selected
->size
== 0) {
2890 name
= fs
->selected
->fontclass
->scale_fontname
2891 (GTK_WIDGET(fs
->u
.window
), fs
->selected
->realname
, fs
->selsize
);
2893 char *ret
= dupcat(fs
->selected
->fontclass
->prefix
, ":",
2900 return dupcat(fs
->selected
->fontclass
->prefix
, ":",
2901 fs
->selected
->realname
, NULL
);
2904 #endif /* GTK_CHECK_VERSION(2,0,0) */