f96e9a61 |
1 | /* |
2 | * Unified font management for GTK. |
3 | * |
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 |
9 | * types of font. |
10 | */ |
11 | |
12 | #include <assert.h> |
13 | #include <stdlib.h> |
14 | #include <string.h> |
15 | #include <gtk/gtk.h> |
16 | #include <gdk/gdkkeysyms.h> |
17 | #include <gdk/gdkx.h> |
18 | #include <X11/Xlib.h> |
19 | #include <X11/Xutil.h> |
20 | #include <X11/Xatom.h> |
21 | |
22 | #include "putty.h" |
23 | #include "gtkfont.h" |
24 | #include "tree234.h" |
25 | |
26 | /* |
27 | * Future work: |
28 | * |
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. |
32 | * |
f96e9a61 |
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. |
42 | */ |
43 | |
3bd9f270 |
44 | #if !GLIB_CHECK_VERSION(1,3,7) |
45 | #define g_ascii_strcasecmp g_strcasecmp |
46 | #define g_ascii_strncasecmp g_strncasecmp |
47 | #endif |
48 | |
f96e9a61 |
49 | /* |
50 | * Ad-hoc vtable mechanism to allow font structures to be |
51 | * polymorphic. |
52 | * |
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. |
58 | */ |
59 | |
60 | #define FONTFLAG_CLIENTSIDE 0x0001 |
61 | #define FONTFLAG_SERVERSIDE 0x0002 |
62 | #define FONTFLAG_SERVERALIAS 0x0004 |
63 | #define FONTFLAG_NONMONOSPACED 0x0008 |
64 | |
65 | #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */ |
66 | |
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, |
70 | int size, int flags, |
71 | const struct unifont_vtable *fontclass); |
72 | |
73 | struct unifont_vtable { |
74 | /* |
75 | * `Methods' of the `class'. |
76 | */ |
77 | unifont *(*create)(GtkWidget *widget, const char *name, int wide, int bold, |
78 | int shadowoffset, int shadowalways); |
0affbab4 |
79 | unifont *(*create_fallback)(GtkWidget *widget, int height, int wide, |
80 | int bold, int shadowoffset, int shadowalways); |
f96e9a61 |
81 | void (*destroy)(unifont *font); |
0affbab4 |
82 | int (*has_glyph)(unifont *font, wchar_t glyph); |
f96e9a61 |
83 | void (*draw_text)(GdkDrawable *target, GdkGC *gc, unifont *font, |
572820e1 |
84 | int x, int y, const wchar_t *string, int len, int wide, |
f96e9a61 |
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); |
91 | |
92 | /* |
93 | * `Static data members' of the `class'. |
94 | */ |
95 | const char *prefix; |
96 | }; |
97 | |
98 | /* ---------------------------------------------------------------------- |
97f01ee0 |
99 | * X11 font implementation, directly using Xlib calls. |
f96e9a61 |
100 | */ |
101 | |
0affbab4 |
102 | static int x11font_has_glyph(unifont *font, wchar_t glyph); |
f96e9a61 |
103 | static void x11font_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
572820e1 |
104 | int x, int y, const wchar_t *string, int len, |
f96e9a61 |
105 | int wide, int bold, int cellwidth); |
106 | static unifont *x11font_create(GtkWidget *widget, const char *name, |
107 | int wide, int bold, |
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, |
116 | int size); |
117 | |
118 | struct x11font { |
119 | struct unifont u; |
120 | /* |
121 | * Actual font objects. We store a number of these, for |
122 | * automatically guessed bold and wide variants. |
123 | * |
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 |
128 | * subsequently). |
129 | */ |
97f01ee0 |
130 | XFontStruct *fonts[4]; |
f96e9a61 |
131 | int allocated[4]; |
132 | /* |
133 | * `sixteen_bit' is true iff the font object is indexed by |
134 | * values larger than a byte. That is, this flag tells us |
97f01ee0 |
135 | * whether we use XDrawString or XDrawString16, etc. |
f96e9a61 |
136 | */ |
137 | int sixteen_bit; |
138 | /* |
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. |
142 | */ |
143 | int variable; |
144 | /* |
572820e1 |
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. |
150 | */ |
151 | int real_charset; |
152 | /* |
f96e9a61 |
153 | * Data passed in to unifont_create(). |
154 | */ |
155 | int wide, bold, shadowoffset, shadowalways; |
156 | }; |
157 | |
158 | static const struct unifont_vtable x11font_vtable = { |
159 | x11font_create, |
0affbab4 |
160 | NULL, /* no fallback fonts in X11 */ |
f96e9a61 |
161 | x11font_destroy, |
0affbab4 |
162 | x11font_has_glyph, |
f96e9a61 |
163 | x11font_draw_text, |
164 | x11font_enum_fonts, |
165 | x11font_canonify_fontname, |
166 | x11font_scale_fontname, |
167 | "server", |
168 | }; |
169 | |
97f01ee0 |
170 | static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide) |
f96e9a61 |
171 | { |
97f01ee0 |
172 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
173 | Atom fontprop = XInternAtom(disp, "FONT", False); |
174 | unsigned long ret; |
175 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
176 | char *name = XGetAtomName(disp, (Atom)ret); |
177 | if (name && name[0] == '-') { |
178 | char *strings[13]; |
179 | char *dupname, *extrafree = NULL, *ret; |
180 | char *p, *q; |
181 | int nstr; |
182 | |
183 | p = q = dupname = dupstr(name); /* skip initial minus */ |
184 | nstr = 0; |
185 | |
186 | while (*p && nstr < lenof(strings)) { |
187 | if (*p == '-') { |
188 | *p = '\0'; |
189 | strings[nstr++] = p+1; |
190 | } |
191 | p++; |
192 | } |
193 | |
194 | if (nstr < lenof(strings)) |
195 | return NULL; /* XLFD was malformed */ |
196 | |
197 | if (bold) |
198 | strings[2] = "bold"; |
199 | |
200 | if (wide) { |
201 | /* 4 is `wideness', which obviously may have changed. */ |
202 | /* 5 is additional style, which may be e.g. `ja' or `ko'. */ |
203 | strings[4] = strings[5] = "*"; |
204 | strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11])); |
205 | } |
206 | |
207 | ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2], |
208 | "-", strings[ 3], "-", strings[ 4], "-", strings[ 5], |
209 | "-", strings[ 6], "-", strings[ 7], "-", strings[ 8], |
210 | "-", strings[ 9], "-", strings[10], "-", strings[11], |
211 | "-", strings[12], NULL); |
212 | sfree(extrafree); |
213 | sfree(dupname); |
214 | |
215 | return ret; |
216 | } |
217 | } |
218 | return NULL; |
219 | } |
220 | |
97f01ee0 |
221 | static int x11_font_width(XFontStruct *xfs, int sixteen_bit) |
f96e9a61 |
222 | { |
223 | if (sixteen_bit) { |
224 | XChar2b space; |
225 | space.byte1 = 0; |
226 | space.byte2 = '0'; |
97f01ee0 |
227 | return XTextWidth16(xfs, &space, 1); |
f96e9a61 |
228 | } else { |
97f01ee0 |
229 | return XTextWidth(xfs, "0", 1); |
f96e9a61 |
230 | } |
231 | } |
232 | |
0affbab4 |
233 | static int x11_font_has_glyph(XFontStruct *xfs, int byte1, int byte2) |
234 | { |
235 | int index; |
236 | |
237 | /* |
238 | * Not to be confused with x11font_has_glyph, which is a method of |
239 | * the x11font 'class' and hence takes a unifont as argument. This |
240 | * is the low-level function which grubs about in an actual |
241 | * XFontStruct to see if a given glyph exists. |
242 | * |
243 | * We must do this ourselves rather than letting Xlib's |
244 | * XTextExtents16 do the job, because XTextExtents will helpfully |
245 | * substitute the font's default_char for any missing glyph and |
246 | * not tell us it did so, which precisely won't help us find out |
247 | * which glyphs _are_ missing. |
248 | * |
249 | * The man page for XQueryFont is rather confusing about how the |
250 | * per_char array in the XFontStruct is laid out, because it gives |
251 | * formulae for determining the two-byte X character code _from_ |
252 | * an index into the per_char array. Going the other way, it's |
253 | * rather simpler: |
254 | * |
255 | * The valid character codes have byte1 between min_byte1 and |
256 | * max_byte1 inclusive, and byte2 between min_char_or_byte2 and |
257 | * max_char_or_byte2 inclusive. This gives a rectangle of size |
258 | * (max_byte2-min_byte1+1) by |
259 | * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the |
260 | * rectangle encoded in the per_char array. Hence, given a |
261 | * character code which is valid in the sense that it falls |
262 | * somewhere in that rectangle, its index in per_char is given by |
263 | * setting |
264 | * |
265 | * x = byte2 - min_char_or_byte2 |
266 | * y = byte1 - min_byte1 |
267 | * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x |
268 | * |
269 | * If min_byte1 and min_byte2 are both zero, that's a special case |
270 | * which can be treated as if min_byte2 was 1 instead, i.e. the |
271 | * per_char array just runs from min_char_or_byte2 to |
272 | * max_char_or_byte2 inclusive, and byte1 should always be zero. |
273 | */ |
274 | |
275 | if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2) |
276 | return FALSE; |
277 | |
278 | if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) { |
279 | index = byte2 - xfs->min_char_or_byte2; |
280 | } else { |
281 | if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1) |
282 | return FALSE; |
283 | index = ((byte2 - xfs->min_char_or_byte2) + |
284 | ((byte1 - xfs->min_byte1) * |
285 | (xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1))); |
286 | } |
287 | |
6a922834 |
288 | if (!xfs->per_char) /* per_char NULL => everything in range exists */ |
289 | return TRUE; |
290 | |
0affbab4 |
291 | return (xfs->per_char[index].ascent + xfs->per_char[index].descent > 0 || |
292 | xfs->per_char[index].width > 0); |
293 | } |
294 | |
f96e9a61 |
295 | static unifont *x11font_create(GtkWidget *widget, const char *name, |
296 | int wide, int bold, |
297 | int shadowoffset, int shadowalways) |
298 | { |
299 | struct x11font *xfont; |
f96e9a61 |
300 | XFontStruct *xfs; |
97f01ee0 |
301 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
302 | Atom charset_registry, charset_encoding, spacing; |
303 | unsigned long registry_ret, encoding_ret, spacing_ret; |
304 | int pubcs, realcs, sixteen_bit, variable; |
305 | int i; |
306 | |
97f01ee0 |
307 | xfs = XLoadQueryFont(disp, name); |
308 | if (!xfs) |
f96e9a61 |
309 | return NULL; |
310 | |
f96e9a61 |
311 | charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False); |
312 | charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False); |
313 | |
314 | pubcs = realcs = CS_NONE; |
315 | sixteen_bit = FALSE; |
316 | variable = TRUE; |
317 | |
318 | if (XGetFontProperty(xfs, charset_registry, ®istry_ret) && |
319 | XGetFontProperty(xfs, charset_encoding, &encoding_ret)) { |
320 | char *reg, *enc; |
321 | reg = XGetAtomName(disp, (Atom)registry_ret); |
322 | enc = XGetAtomName(disp, (Atom)encoding_ret); |
323 | if (reg && enc) { |
324 | char *encoding = dupcat(reg, "-", enc, NULL); |
325 | pubcs = realcs = charset_from_xenc(encoding); |
326 | |
327 | /* |
328 | * iso10646-1 is the only wide font encoding we |
329 | * support. In this case, we expect clients to give us |
330 | * UTF-8, which this module must internally convert |
331 | * into 16-bit Unicode. |
332 | */ |
333 | if (!strcasecmp(encoding, "iso10646-1")) { |
334 | sixteen_bit = TRUE; |
335 | pubcs = realcs = CS_UTF8; |
336 | } |
337 | |
338 | /* |
0affbab4 |
339 | * Hack for X line-drawing characters: if the primary font |
340 | * is encoded as ISO-8859-1, and has valid glyphs in the |
341 | * low character positions, it is assumed that those |
342 | * glyphs are the VT100 line-drawing character set. |
f96e9a61 |
343 | */ |
344 | if (pubcs == CS_ISO8859_1) { |
0affbab4 |
345 | int ch; |
346 | for (ch = 1; ch < 32; ch++) |
347 | if (!x11_font_has_glyph(xfs, 0, ch)) |
348 | break; |
349 | if (ch == 32) |
350 | realcs = CS_ISO8859_1_X11; |
351 | } |
f96e9a61 |
352 | |
353 | sfree(encoding); |
354 | } |
355 | } |
356 | |
357 | spacing = XInternAtom(disp, "SPACING", False); |
358 | if (XGetFontProperty(xfs, spacing, &spacing_ret)) { |
359 | char *spc; |
360 | spc = XGetAtomName(disp, (Atom)spacing_ret); |
361 | |
362 | if (spc && strchr("CcMm", spc[0])) |
363 | variable = FALSE; |
364 | } |
365 | |
366 | xfont = snew(struct x11font); |
367 | xfont->u.vt = &x11font_vtable; |
97f01ee0 |
368 | xfont->u.width = x11_font_width(xfs, sixteen_bit); |
369 | xfont->u.ascent = xfs->ascent; |
370 | xfont->u.descent = xfs->descent; |
f96e9a61 |
371 | xfont->u.height = xfont->u.ascent + xfont->u.descent; |
372 | xfont->u.public_charset = pubcs; |
0affbab4 |
373 | xfont->u.want_fallback = TRUE; |
572820e1 |
374 | xfont->real_charset = realcs; |
97f01ee0 |
375 | xfont->fonts[0] = xfs; |
f96e9a61 |
376 | xfont->allocated[0] = TRUE; |
377 | xfont->sixteen_bit = sixteen_bit; |
378 | xfont->variable = variable; |
379 | xfont->wide = wide; |
380 | xfont->bold = bold; |
381 | xfont->shadowoffset = shadowoffset; |
382 | xfont->shadowalways = shadowalways; |
383 | |
384 | for (i = 1; i < lenof(xfont->fonts); i++) { |
385 | xfont->fonts[i] = NULL; |
386 | xfont->allocated[i] = FALSE; |
387 | } |
388 | |
389 | return (unifont *)xfont; |
390 | } |
391 | |
392 | static void x11font_destroy(unifont *font) |
393 | { |
97f01ee0 |
394 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
395 | struct x11font *xfont = (struct x11font *)font; |
396 | int i; |
397 | |
398 | for (i = 0; i < lenof(xfont->fonts); i++) |
399 | if (xfont->fonts[i]) |
97f01ee0 |
400 | XFreeFont(disp, xfont->fonts[i]); |
f96e9a61 |
401 | sfree(font); |
402 | } |
403 | |
404 | static void x11_alloc_subfont(struct x11font *xfont, int sfid) |
405 | { |
97f01ee0 |
406 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
407 | char *derived_name = x11_guess_derived_font_name |
408 | (xfont->fonts[0], sfid & 1, !!(sfid & 2)); |
97f01ee0 |
409 | xfont->fonts[sfid] = XLoadQueryFont(disp, derived_name); |
f96e9a61 |
410 | xfont->allocated[sfid] = TRUE; |
411 | sfree(derived_name); |
97f01ee0 |
412 | /* Note that xfont->fonts[sfid] may still be NULL, if XLQF failed. */ |
f96e9a61 |
413 | } |
414 | |
0affbab4 |
415 | static int x11font_has_glyph(unifont *font, wchar_t glyph) |
416 | { |
417 | struct x11font *xfont = (struct x11font *)font; |
418 | |
419 | if (xfont->sixteen_bit) { |
420 | /* |
421 | * This X font has 16-bit character indices, which means |
422 | * we can directly use our Unicode input value. |
423 | */ |
424 | return x11_font_has_glyph(xfont->fonts[0], glyph >> 8, glyph & 0xFF); |
425 | } else { |
426 | /* |
427 | * This X font has 8-bit indices, so we must convert to the |
428 | * appropriate character set. |
429 | */ |
430 | char sbstring[2]; |
431 | int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1, |
432 | sbstring, 2, "", NULL, NULL); |
8a0c2552 |
433 | if (sblen == 0 || !sbstring[0]) |
0affbab4 |
434 | return FALSE; /* not even in the charset */ |
435 | |
31393e34 |
436 | return x11_font_has_glyph(xfont->fonts[0], 0, |
437 | (unsigned char)sbstring[0]); |
0affbab4 |
438 | } |
439 | } |
440 | |
97f01ee0 |
441 | #if !GTK_CHECK_VERSION(2,0,0) |
442 | #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */ |
443 | #endif |
444 | |
445 | static void x11font_really_draw_text_16(GdkDrawable *target, XFontStruct *xfs, |
446 | GC gc, int x, int y, |
447 | const XChar2b *string, int nchars, |
448 | int shadowoffset, |
449 | int fontvariable, int cellwidth) |
f96e9a61 |
450 | { |
97f01ee0 |
451 | Display *disp = GDK_DISPLAY(); |
452 | int step, nsteps, centre; |
f96e9a61 |
453 | |
454 | if (fontvariable) { |
455 | /* |
456 | * In a variable-pitch font, we draw one character at a |
457 | * time, and centre it in the character cell. |
458 | */ |
97f01ee0 |
459 | step = 1; |
f96e9a61 |
460 | nsteps = nchars; |
461 | centre = TRUE; |
97f01ee0 |
462 | } else { |
463 | /* |
464 | * In a fixed-pitch font, we can draw the whole lot in one go. |
465 | */ |
466 | step = nchars; |
467 | nsteps = 1; |
468 | centre = FALSE; |
f96e9a61 |
469 | } |
470 | |
471 | while (nsteps-- > 0) { |
472 | int X = x; |
473 | if (centre) |
97f01ee0 |
474 | X += (cellwidth - XTextWidth16(xfs, string, step)) / 2; |
f96e9a61 |
475 | |
97f01ee0 |
476 | XDrawString16(disp, GDK_DRAWABLE_XID(target), gc, |
477 | X, y, string, step); |
478 | if (shadowoffset) |
479 | XDrawString16(disp, GDK_DRAWABLE_XID(target), gc, |
480 | X + shadowoffset, y, string, step); |
f96e9a61 |
481 | |
482 | x += cellwidth; |
483 | string += step; |
484 | } |
485 | } |
486 | |
97f01ee0 |
487 | static void x11font_really_draw_text(GdkDrawable *target, XFontStruct *xfs, |
488 | GC gc, int x, int y, |
489 | const char *string, int nchars, |
490 | int shadowoffset, |
491 | int fontvariable, int cellwidth) |
492 | { |
493 | Display *disp = GDK_DISPLAY(); |
494 | int step, nsteps, centre; |
495 | |
496 | if (fontvariable) { |
497 | /* |
498 | * In a variable-pitch font, we draw one character at a |
499 | * time, and centre it in the character cell. |
500 | */ |
501 | step = 1; |
502 | nsteps = nchars; |
503 | centre = TRUE; |
504 | } else { |
505 | /* |
506 | * In a fixed-pitch font, we can draw the whole lot in one go. |
507 | */ |
508 | step = nchars; |
509 | nsteps = 1; |
510 | centre = FALSE; |
511 | } |
512 | |
513 | while (nsteps-- > 0) { |
514 | int X = x; |
515 | if (centre) |
516 | X += (cellwidth - XTextWidth(xfs, string, step)) / 2; |
517 | |
518 | XDrawString(disp, GDK_DRAWABLE_XID(target), gc, |
519 | X, y, string, step); |
520 | if (shadowoffset) |
521 | XDrawString(disp, GDK_DRAWABLE_XID(target), gc, |
522 | X + shadowoffset, y, string, step); |
523 | |
524 | x += cellwidth; |
525 | string += step; |
526 | } |
527 | } |
528 | |
529 | static void x11font_draw_text(GdkDrawable *target, GdkGC *gdkgc, unifont *font, |
572820e1 |
530 | int x, int y, const wchar_t *string, int len, |
f96e9a61 |
531 | int wide, int bold, int cellwidth) |
532 | { |
97f01ee0 |
533 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
534 | struct x11font *xfont = (struct x11font *)font; |
97f01ee0 |
535 | GC gc = GDK_GC_XGC(gdkgc); |
f96e9a61 |
536 | int sfid; |
97f01ee0 |
537 | int shadowoffset = 0; |
f96e9a61 |
538 | int mult = (wide ? 2 : 1); |
539 | |
540 | wide -= xfont->wide; |
541 | bold -= xfont->bold; |
542 | |
543 | /* |
544 | * Decide which subfont we're using, and whether we have to |
545 | * use shadow bold. |
546 | */ |
547 | if (xfont->shadowalways && bold) { |
97f01ee0 |
548 | shadowoffset = xfont->shadowoffset; |
f96e9a61 |
549 | bold = 0; |
550 | } |
551 | sfid = 2 * wide + bold; |
552 | if (!xfont->allocated[sfid]) |
553 | x11_alloc_subfont(xfont, sfid); |
554 | if (bold && !xfont->fonts[sfid]) { |
555 | bold = 0; |
97f01ee0 |
556 | shadowoffset = xfont->shadowoffset; |
f96e9a61 |
557 | sfid = 2 * wide + bold; |
558 | if (!xfont->allocated[sfid]) |
559 | x11_alloc_subfont(xfont, sfid); |
560 | } |
561 | |
562 | if (!xfont->fonts[sfid]) |
563 | return; /* we've tried our best, but no luck */ |
564 | |
97f01ee0 |
565 | XSetFont(disp, gc, xfont->fonts[sfid]->fid); |
566 | |
f96e9a61 |
567 | if (xfont->sixteen_bit) { |
568 | /* |
569 | * This X font has 16-bit character indices, which means |
572820e1 |
570 | * we can directly use our Unicode input string. |
f96e9a61 |
571 | */ |
572 | XChar2b *xcs; |
572820e1 |
573 | int i; |
f96e9a61 |
574 | |
572820e1 |
575 | xcs = snewn(len, XChar2b); |
576 | for (i = 0; i < len; i++) { |
577 | xcs[i].byte1 = string[i] >> 8; |
578 | xcs[i].byte2 = string[i]; |
f96e9a61 |
579 | } |
580 | |
97f01ee0 |
581 | x11font_really_draw_text_16(target, xfont->fonts[sfid], gc, x, y, |
572820e1 |
582 | xcs, len, shadowoffset, |
97f01ee0 |
583 | xfont->variable, cellwidth * mult); |
f96e9a61 |
584 | sfree(xcs); |
f96e9a61 |
585 | } else { |
572820e1 |
586 | /* |
587 | * This X font has 8-bit indices, so we must convert to the |
588 | * appropriate character set. |
589 | */ |
590 | char *sbstring = snewn(len+1, char); |
591 | int sblen = wc_to_mb(xfont->real_charset, 0, string, len, |
592 | sbstring, len+1, ".", NULL, NULL); |
f96e9a61 |
593 | x11font_really_draw_text(target, xfont->fonts[sfid], gc, x, y, |
572820e1 |
594 | sbstring, sblen, shadowoffset, |
f96e9a61 |
595 | xfont->variable, cellwidth * mult); |
572820e1 |
596 | sfree(sbstring); |
f96e9a61 |
597 | } |
598 | } |
599 | |
600 | static void x11font_enum_fonts(GtkWidget *widget, |
601 | fontsel_add_entry callback, void *callback_ctx) |
602 | { |
603 | char **fontnames; |
604 | char *tmp = NULL; |
605 | int nnames, i, max, tmpsize; |
606 | |
607 | max = 32768; |
608 | while (1) { |
609 | fontnames = XListFonts(GDK_DISPLAY(), "*", max, &nnames); |
610 | if (nnames >= max) { |
611 | XFreeFontNames(fontnames); |
612 | max *= 2; |
613 | } else |
614 | break; |
615 | } |
616 | |
617 | tmpsize = 0; |
618 | |
619 | for (i = 0; i < nnames; i++) { |
620 | if (fontnames[i][0] == '-') { |
621 | /* |
622 | * Dismember an XLFD and convert it into the format |
623 | * we'll be using in the font selector. |
624 | */ |
625 | char *components[14]; |
626 | char *p, *font, *style, *stylekey, *charset; |
627 | int j, weightkey, slantkey, setwidthkey; |
628 | int thistmpsize, fontsize, flags; |
629 | |
630 | thistmpsize = 4 * strlen(fontnames[i]) + 256; |
631 | if (tmpsize < thistmpsize) { |
632 | tmpsize = thistmpsize; |
633 | tmp = sresize(tmp, tmpsize, char); |
634 | } |
635 | strcpy(tmp, fontnames[i]); |
636 | |
637 | p = tmp; |
638 | for (j = 0; j < 14; j++) { |
639 | if (*p) |
640 | *p++ = '\0'; |
641 | components[j] = p; |
642 | while (*p && *p != '-') |
643 | p++; |
644 | } |
645 | *p++ = '\0'; |
646 | |
647 | /* |
648 | * Font name is made up of fields 0 and 1, in reverse |
649 | * order with parentheses. (This is what the GTK 1.2 X |
650 | * font selector does, and it seems to come out |
651 | * looking reasonably sensible.) |
652 | */ |
653 | font = p; |
654 | p += 1 + sprintf(p, "%s (%s)", components[1], components[0]); |
655 | |
656 | /* |
657 | * Charset is made up of fields 12 and 13. |
658 | */ |
659 | charset = p; |
660 | p += 1 + sprintf(p, "%s-%s", components[12], components[13]); |
661 | |
662 | /* |
663 | * Style is a mixture of quite a lot of the fields, |
664 | * with some strange formatting. |
665 | */ |
666 | style = p; |
667 | p += sprintf(p, "%s", components[2][0] ? components[2] : |
668 | "regular"); |
3bd9f270 |
669 | if (!g_ascii_strcasecmp(components[3], "i")) |
f96e9a61 |
670 | p += sprintf(p, " italic"); |
3bd9f270 |
671 | else if (!g_ascii_strcasecmp(components[3], "o")) |
f96e9a61 |
672 | p += sprintf(p, " oblique"); |
3bd9f270 |
673 | else if (!g_ascii_strcasecmp(components[3], "ri")) |
f96e9a61 |
674 | p += sprintf(p, " reverse italic"); |
3bd9f270 |
675 | else if (!g_ascii_strcasecmp(components[3], "ro")) |
f96e9a61 |
676 | p += sprintf(p, " reverse oblique"); |
3bd9f270 |
677 | else if (!g_ascii_strcasecmp(components[3], "ot")) |
f96e9a61 |
678 | p += sprintf(p, " other-slant"); |
3bd9f270 |
679 | if (components[4][0] && g_ascii_strcasecmp(components[4], "normal")) |
f96e9a61 |
680 | p += sprintf(p, " %s", components[4]); |
3bd9f270 |
681 | if (!g_ascii_strcasecmp(components[10], "m")) |
f96e9a61 |
682 | p += sprintf(p, " [M]"); |
3bd9f270 |
683 | if (!g_ascii_strcasecmp(components[10], "c")) |
f96e9a61 |
684 | p += sprintf(p, " [C]"); |
685 | if (components[5][0]) |
686 | p += sprintf(p, " %s", components[5]); |
687 | |
688 | /* |
689 | * Style key is the same stuff as above, but with a |
690 | * couple of transformations done on it to make it |
691 | * sort more sensibly. |
692 | */ |
693 | p++; |
694 | stylekey = p; |
3bd9f270 |
695 | if (!g_ascii_strcasecmp(components[2], "medium") || |
696 | !g_ascii_strcasecmp(components[2], "regular") || |
697 | !g_ascii_strcasecmp(components[2], "normal") || |
698 | !g_ascii_strcasecmp(components[2], "book")) |
f96e9a61 |
699 | weightkey = 0; |
3bd9f270 |
700 | else if (!g_ascii_strncasecmp(components[2], "demi", 4) || |
701 | !g_ascii_strncasecmp(components[2], "semi", 4)) |
f96e9a61 |
702 | weightkey = 1; |
703 | else |
704 | weightkey = 2; |
3bd9f270 |
705 | if (!g_ascii_strcasecmp(components[3], "r")) |
f96e9a61 |
706 | slantkey = 0; |
3bd9f270 |
707 | else if (!g_ascii_strncasecmp(components[3], "r", 1)) |
f96e9a61 |
708 | slantkey = 2; |
709 | else |
710 | slantkey = 1; |
3bd9f270 |
711 | if (!g_ascii_strcasecmp(components[4], "normal")) |
f96e9a61 |
712 | setwidthkey = 0; |
713 | else |
714 | setwidthkey = 1; |
715 | |
716 | p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s", |
717 | weightkey, |
718 | (int)strlen(components[2]), components[2], |
719 | slantkey, |
720 | (int)strlen(components[3]), components[3], |
721 | setwidthkey, |
722 | (int)strlen(components[4]), components[4], |
723 | (int)strlen(components[10]), components[10], |
724 | (int)strlen(components[5]), components[5]); |
725 | |
726 | assert(p - tmp < thistmpsize); |
727 | |
728 | /* |
729 | * Size is in pixels, for our application, so we |
730 | * derive it directly from the pixel size field, |
731 | * number 6. |
732 | */ |
733 | fontsize = atoi(components[6]); |
734 | |
735 | /* |
736 | * Flags: we need to know whether this is a monospaced |
737 | * font, which we do by examining the spacing field |
738 | * again. |
739 | */ |
740 | flags = FONTFLAG_SERVERSIDE; |
741 | if (!strchr("CcMm", components[10][0])) |
742 | flags |= FONTFLAG_NONMONOSPACED; |
743 | |
744 | /* |
745 | * Not sure why, but sometimes the X server will |
746 | * deliver dummy font types in which fontsize comes |
747 | * out as zero. Filter those out. |
748 | */ |
749 | if (fontsize) |
750 | callback(callback_ctx, fontnames[i], font, charset, |
751 | style, stylekey, fontsize, flags, &x11font_vtable); |
752 | } else { |
753 | /* |
754 | * This isn't an XLFD, so it must be an alias. |
755 | * Transmit it with mostly null data. |
756 | * |
757 | * It would be nice to work out if it's monospaced |
758 | * here, but at the moment I can't see that being |
759 | * anything but computationally hideous. Ah well. |
760 | */ |
761 | callback(callback_ctx, fontnames[i], fontnames[i], NULL, |
762 | NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable); |
763 | } |
764 | } |
765 | XFreeFontNames(fontnames); |
766 | } |
767 | |
768 | static char *x11font_canonify_fontname(GtkWidget *widget, const char *name, |
769 | int *size, int *flags, |
770 | int resolve_aliases) |
771 | { |
772 | /* |
773 | * When given an X11 font name to try to make sense of for a |
774 | * font selector, we must attempt to load it (to see if it |
775 | * exists), and then canonify it by extracting its FONT |
776 | * property, which should give its full XLFD even if what we |
777 | * originally had was a wildcard. |
778 | * |
779 | * However, we must carefully avoid canonifying font |
780 | * _aliases_, unless specifically asked to, because the font |
781 | * selector treats them as worthwhile in their own right. |
782 | */ |
f96e9a61 |
783 | XFontStruct *xfs; |
97f01ee0 |
784 | Display *disp = GDK_DISPLAY(); |
f96e9a61 |
785 | Atom fontprop, fontprop2; |
786 | unsigned long ret; |
787 | |
97f01ee0 |
788 | xfs = XLoadQueryFont(disp, name); |
f96e9a61 |
789 | |
97f01ee0 |
790 | if (!xfs) |
791 | return NULL; /* didn't make sense to us, sorry */ |
f96e9a61 |
792 | |
f96e9a61 |
793 | fontprop = XInternAtom(disp, "FONT", False); |
794 | |
795 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
796 | char *newname = XGetAtomName(disp, (Atom)ret); |
797 | if (newname) { |
798 | unsigned long fsize = 12; |
799 | |
800 | fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False); |
801 | if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) { |
802 | *size = fsize; |
97f01ee0 |
803 | XFreeFont(disp, xfs); |
f96e9a61 |
804 | if (flags) { |
805 | if (name[0] == '-' || resolve_aliases) |
806 | *flags = FONTFLAG_SERVERSIDE; |
807 | else |
808 | *flags = FONTFLAG_SERVERALIAS; |
809 | } |
810 | return dupstr(name[0] == '-' || resolve_aliases ? |
811 | newname : name); |
812 | } |
813 | } |
814 | } |
815 | |
97f01ee0 |
816 | XFreeFont(disp, xfs); |
817 | |
f96e9a61 |
818 | return NULL; /* something went wrong */ |
819 | } |
820 | |
821 | static char *x11font_scale_fontname(GtkWidget *widget, const char *name, |
822 | int size) |
823 | { |
824 | return NULL; /* shan't */ |
825 | } |
826 | |
827 | #if GTK_CHECK_VERSION(2,0,0) |
828 | |
829 | /* ---------------------------------------------------------------------- |
830 | * Pango font implementation (for GTK 2 only). |
831 | */ |
832 | |
833 | #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6 |
834 | #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */ |
835 | #endif |
836 | |
0affbab4 |
837 | static int pangofont_has_glyph(unifont *font, wchar_t glyph); |
f96e9a61 |
838 | static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
572820e1 |
839 | int x, int y, const wchar_t *string, int len, |
f96e9a61 |
840 | int wide, int bold, int cellwidth); |
841 | static unifont *pangofont_create(GtkWidget *widget, const char *name, |
842 | int wide, int bold, |
843 | int shadowoffset, int shadowalways); |
0affbab4 |
844 | static unifont *pangofont_create_fallback(GtkWidget *widget, int height, |
845 | int wide, int bold, |
846 | int shadowoffset, int shadowalways); |
f96e9a61 |
847 | static void pangofont_destroy(unifont *font); |
848 | static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback, |
849 | void *callback_ctx); |
850 | static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name, |
851 | int *size, int *flags, |
852 | int resolve_aliases); |
853 | static char *pangofont_scale_fontname(GtkWidget *widget, const char *name, |
854 | int size); |
855 | |
856 | struct pangofont { |
857 | struct unifont u; |
858 | /* |
859 | * Pango objects. |
860 | */ |
861 | PangoFontDescription *desc; |
862 | PangoFontset *fset; |
863 | /* |
864 | * The containing widget. |
865 | */ |
866 | GtkWidget *widget; |
867 | /* |
868 | * Data passed in to unifont_create(). |
869 | */ |
870 | int bold, shadowoffset, shadowalways; |
871 | }; |
872 | |
873 | static const struct unifont_vtable pangofont_vtable = { |
874 | pangofont_create, |
0affbab4 |
875 | pangofont_create_fallback, |
f96e9a61 |
876 | pangofont_destroy, |
0affbab4 |
877 | pangofont_has_glyph, |
f96e9a61 |
878 | pangofont_draw_text, |
879 | pangofont_enum_fonts, |
880 | pangofont_canonify_fontname, |
881 | pangofont_scale_fontname, |
882 | "client", |
883 | }; |
884 | |
885 | /* |
886 | * This function is used to rigorously validate a |
887 | * PangoFontDescription. Later versions of Pango have a nasty |
888 | * habit of accepting _any_ old string as input to |
889 | * pango_font_description_from_string and returning a font |
890 | * description which can actually be used to display text, even if |
891 | * they have to do it by falling back to their most default font. |
892 | * This is doubtless helpful in some situations, but not here, |
893 | * because we need to know if a Pango font string actually _makes |
894 | * sense_ in order to fall back to treating it as an X font name |
895 | * if it doesn't. So we check that the font family is actually one |
896 | * supported by Pango. |
897 | */ |
898 | static int pangofont_check_desc_makes_sense(PangoContext *ctx, |
899 | PangoFontDescription *desc) |
900 | { |
901 | #ifndef PANGO_PRE_1POINT6 |
902 | PangoFontMap *map; |
903 | #endif |
904 | PangoFontFamily **families; |
905 | int i, nfamilies, matched; |
906 | |
907 | /* |
908 | * Ask Pango for a list of font families, and iterate through |
909 | * them to see if one of them matches the family in the |
910 | * PangoFontDescription. |
911 | */ |
912 | #ifndef PANGO_PRE_1POINT6 |
913 | map = pango_context_get_font_map(ctx); |
914 | if (!map) |
915 | return FALSE; |
916 | pango_font_map_list_families(map, &families, &nfamilies); |
917 | #else |
918 | pango_context_list_families(ctx, &families, &nfamilies); |
919 | #endif |
920 | |
921 | matched = FALSE; |
922 | for (i = 0; i < nfamilies; i++) { |
3bd9f270 |
923 | if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]), |
924 | pango_font_description_get_family(desc))) { |
f96e9a61 |
925 | matched = TRUE; |
926 | break; |
927 | } |
928 | } |
929 | g_free(families); |
930 | |
931 | return matched; |
932 | } |
933 | |
0affbab4 |
934 | static unifont *pangofont_create_internal(GtkWidget *widget, |
935 | PangoContext *ctx, |
936 | PangoFontDescription *desc, |
937 | int wide, int bold, |
938 | int shadowoffset, int shadowalways) |
f96e9a61 |
939 | { |
940 | struct pangofont *pfont; |
f96e9a61 |
941 | #ifndef PANGO_PRE_1POINT6 |
942 | PangoFontMap *map; |
943 | #endif |
f96e9a61 |
944 | PangoFontset *fset; |
945 | PangoFontMetrics *metrics; |
946 | |
f96e9a61 |
947 | #ifndef PANGO_PRE_1POINT6 |
948 | map = pango_context_get_font_map(ctx); |
949 | if (!map) { |
950 | pango_font_description_free(desc); |
951 | return NULL; |
952 | } |
953 | fset = pango_font_map_load_fontset(map, ctx, desc, |
954 | pango_context_get_language(ctx)); |
955 | #else |
956 | fset = pango_context_load_fontset(ctx, desc, |
957 | pango_context_get_language(ctx)); |
958 | #endif |
959 | if (!fset) { |
960 | pango_font_description_free(desc); |
961 | return NULL; |
962 | } |
963 | metrics = pango_fontset_get_metrics(fset); |
964 | if (!metrics || |
965 | pango_font_metrics_get_approximate_digit_width(metrics) == 0) { |
966 | pango_font_description_free(desc); |
967 | g_object_unref(fset); |
968 | return NULL; |
969 | } |
970 | |
971 | pfont = snew(struct pangofont); |
972 | pfont->u.vt = &pangofont_vtable; |
973 | pfont->u.width = |
974 | PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics)); |
975 | pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics)); |
976 | pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics)); |
977 | pfont->u.height = pfont->u.ascent + pfont->u.descent; |
0affbab4 |
978 | pfont->u.want_fallback = FALSE; |
f96e9a61 |
979 | /* The Pango API is hardwired to UTF-8 */ |
980 | pfont->u.public_charset = CS_UTF8; |
f96e9a61 |
981 | pfont->desc = desc; |
982 | pfont->fset = fset; |
983 | pfont->widget = widget; |
984 | pfont->bold = bold; |
985 | pfont->shadowoffset = shadowoffset; |
986 | pfont->shadowalways = shadowalways; |
987 | |
988 | pango_font_metrics_unref(metrics); |
989 | |
990 | return (unifont *)pfont; |
991 | } |
992 | |
0affbab4 |
993 | static unifont *pangofont_create(GtkWidget *widget, const char *name, |
994 | int wide, int bold, |
995 | int shadowoffset, int shadowalways) |
996 | { |
997 | PangoContext *ctx; |
998 | PangoFontDescription *desc; |
999 | |
1000 | desc = pango_font_description_from_string(name); |
1001 | if (!desc) |
1002 | return NULL; |
1003 | ctx = gtk_widget_get_pango_context(widget); |
1004 | if (!ctx) { |
1005 | pango_font_description_free(desc); |
1006 | return NULL; |
1007 | } |
1008 | if (!pangofont_check_desc_makes_sense(ctx, desc)) { |
1009 | pango_font_description_free(desc); |
1010 | return NULL; |
1011 | } |
1012 | return pangofont_create_internal(widget, ctx, desc, wide, bold, |
1013 | shadowoffset, shadowalways); |
1014 | } |
1015 | |
1016 | static unifont *pangofont_create_fallback(GtkWidget *widget, int height, |
1017 | int wide, int bold, |
1018 | int shadowoffset, int shadowalways) |
1019 | { |
1020 | PangoContext *ctx; |
1021 | PangoFontDescription *desc; |
1022 | |
1023 | desc = pango_font_description_from_string("Monospace"); |
1024 | if (!desc) |
1025 | return NULL; |
1026 | ctx = gtk_widget_get_pango_context(widget); |
1027 | if (!ctx) { |
1028 | pango_font_description_free(desc); |
1029 | return NULL; |
1030 | } |
1031 | pango_font_description_set_absolute_size(desc, height * PANGO_SCALE); |
1032 | return pangofont_create_internal(widget, ctx, desc, wide, bold, |
1033 | shadowoffset, shadowalways); |
1034 | } |
1035 | |
f96e9a61 |
1036 | static void pangofont_destroy(unifont *font) |
1037 | { |
1038 | struct pangofont *pfont = (struct pangofont *)font; |
1039 | pango_font_description_free(pfont->desc); |
1040 | g_object_unref(pfont->fset); |
1041 | sfree(font); |
1042 | } |
1043 | |
0affbab4 |
1044 | static int pangofont_has_glyph(unifont *font, wchar_t glyph) |
1045 | { |
1046 | /* Pango implements font fallback, so assume it has everything */ |
1047 | return TRUE; |
1048 | } |
1049 | |
f96e9a61 |
1050 | static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
572820e1 |
1051 | int x, int y, const wchar_t *string, int len, |
f96e9a61 |
1052 | int wide, int bold, int cellwidth) |
1053 | { |
1054 | struct pangofont *pfont = (struct pangofont *)font; |
1055 | PangoLayout *layout; |
1056 | PangoRectangle rect; |
572820e1 |
1057 | char *utfstring, *utfptr; |
1058 | int utflen; |
f96e9a61 |
1059 | int shadowbold = FALSE; |
1060 | |
1061 | if (wide) |
1062 | cellwidth *= 2; |
1063 | |
1064 | y -= pfont->u.ascent; |
1065 | |
1066 | layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget)); |
1067 | pango_layout_set_font_description(layout, pfont->desc); |
1068 | if (bold > pfont->bold) { |
1069 | if (pfont->shadowalways) |
1070 | shadowbold = TRUE; |
1071 | else { |
1072 | PangoFontDescription *desc2 = |
1073 | pango_font_description_copy_static(pfont->desc); |
1074 | pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD); |
1075 | pango_layout_set_font_description(layout, desc2); |
1076 | } |
1077 | } |
1078 | |
572820e1 |
1079 | /* |
1080 | * Pango always expects UTF-8, so convert the input wide character |
1081 | * string to UTF-8. |
1082 | */ |
1083 | utfstring = snewn(len*6+1, char); /* UTF-8 has max 6 bytes/char */ |
1084 | utflen = wc_to_mb(CS_UTF8, 0, string, len, |
1085 | utfstring, len*6+1, ".", NULL, NULL); |
1086 | |
1087 | utfptr = utfstring; |
1088 | while (utflen > 0) { |
8412ec80 |
1089 | int clen, n; |
f96e9a61 |
1090 | |
1091 | /* |
8412ec80 |
1092 | * We want to display every character from this string in |
1093 | * the centre of its own character cell. In the worst case, |
1094 | * this requires a separate text-drawing call for each |
1095 | * character; but in the common case where the font is |
1096 | * properly fixed-width, we can draw many characters in one |
1097 | * go which is much faster. |
1098 | * |
1099 | * This still isn't really ideal. If you look at what |
1100 | * happens in the X protocol as a result of all of this, you |
1101 | * find - naturally enough - that each call to |
1102 | * gdk_draw_layout() generates a separate set of X RENDER |
1103 | * operations involving creating a picture, setting a clip |
1104 | * rectangle, doing some drawing and undoing the whole lot. |
1105 | * In an ideal world, we should _always_ be able to turn the |
1106 | * contents of this loop into a single RenderCompositeGlyphs |
1107 | * operation which internally specifies inter-character |
1108 | * deltas to get the spacing right, which would give us full |
1109 | * speed _even_ in the worst case of a non-fixed-width font. |
1110 | * However, Pango's architecture and documentation are so |
1111 | * unhelpful that I have no idea how if at all to persuade |
1112 | * them to do that. |
1113 | */ |
1114 | |
1115 | /* |
1116 | * Start by extracting a single UTF-8 character from the |
1117 | * string. |
f96e9a61 |
1118 | */ |
1119 | clen = 1; |
572820e1 |
1120 | while (clen < utflen && |
1121 | (unsigned char)utfptr[clen] >= 0x80 && |
1122 | (unsigned char)utfptr[clen] < 0xC0) |
f96e9a61 |
1123 | clen++; |
8412ec80 |
1124 | n = 1; |
f96e9a61 |
1125 | |
8ef0be9b |
1126 | /* |
1127 | * If it's a right-to-left character, we must display it on |
1128 | * its own, to stop Pango helpfully re-reversing our already |
1129 | * reversed text. |
1130 | */ |
1131 | if (!is_rtl(string[0])) { |
1132 | |
1133 | /* |
1134 | * See if that character has the width we expect. |
1135 | */ |
1136 | pango_layout_set_text(layout, utfptr, clen); |
1137 | pango_layout_get_pixel_extents(layout, NULL, &rect); |
1138 | |
1139 | if (rect.width == cellwidth) { |
1140 | /* |
1141 | * Try extracting more characters, for as long as they |
1142 | * stay well-behaved. |
1143 | */ |
1144 | while (clen < utflen) { |
1145 | int oldclen = clen; |
1146 | clen++; /* skip UTF-8 introducer byte */ |
1147 | while (clen < utflen && |
1148 | (unsigned char)utfptr[clen] >= 0x80 && |
1149 | (unsigned char)utfptr[clen] < 0xC0) |
1150 | clen++; |
1151 | n++; |
1152 | pango_layout_set_text(layout, utfptr, clen); |
1153 | pango_layout_get_pixel_extents(layout, NULL, &rect); |
1154 | if (rect.width != n * cellwidth) { |
1155 | clen = oldclen; |
1156 | n--; |
1157 | break; |
1158 | } |
1159 | } |
1160 | } |
1161 | } |
8412ec80 |
1162 | |
572820e1 |
1163 | pango_layout_set_text(layout, utfptr, clen); |
8412ec80 |
1164 | pango_layout_get_pixel_extents(layout, NULL, &rect); |
1165 | gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2, |
f96e9a61 |
1166 | y + (pfont->u.height - rect.height)/2, layout); |
1167 | if (shadowbold) |
8412ec80 |
1168 | gdk_draw_layout(target, gc, x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset, |
f96e9a61 |
1169 | y + (pfont->u.height - rect.height)/2, layout); |
1170 | |
572820e1 |
1171 | utflen -= clen; |
1172 | utfptr += clen; |
8ef0be9b |
1173 | string += n; |
8412ec80 |
1174 | x += n * cellwidth; |
f96e9a61 |
1175 | } |
1176 | |
572820e1 |
1177 | sfree(utfstring); |
1178 | |
f96e9a61 |
1179 | g_object_unref(layout); |
1180 | } |
1181 | |
1182 | /* |
1183 | * Dummy size value to be used when converting a |
1184 | * PangoFontDescription of a scalable font to a string for |
1185 | * internal use. |
1186 | */ |
1187 | #define PANGO_DUMMY_SIZE 12 |
1188 | |
1189 | static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback, |
1190 | void *callback_ctx) |
1191 | { |
1192 | PangoContext *ctx; |
1193 | #ifndef PANGO_PRE_1POINT6 |
1194 | PangoFontMap *map; |
1195 | #endif |
1196 | PangoFontFamily **families; |
1197 | int i, nfamilies; |
1198 | |
1199 | ctx = gtk_widget_get_pango_context(widget); |
1200 | if (!ctx) |
1201 | return; |
1202 | |
1203 | /* |
1204 | * Ask Pango for a list of font families, and iterate through |
1205 | * them. |
1206 | */ |
1207 | #ifndef PANGO_PRE_1POINT6 |
1208 | map = pango_context_get_font_map(ctx); |
1209 | if (!map) |
1210 | return; |
1211 | pango_font_map_list_families(map, &families, &nfamilies); |
1212 | #else |
1213 | pango_context_list_families(ctx, &families, &nfamilies); |
1214 | #endif |
1215 | for (i = 0; i < nfamilies; i++) { |
1216 | PangoFontFamily *family = families[i]; |
1217 | const char *familyname; |
1218 | int flags; |
1219 | PangoFontFace **faces; |
1220 | int j, nfaces; |
1221 | |
1222 | /* |
1223 | * Set up our flags for this font family, and get the name |
1224 | * string. |
1225 | */ |
1226 | flags = FONTFLAG_CLIENTSIDE; |
1227 | #ifndef PANGO_PRE_1POINT4 |
1228 | /* |
1229 | * In very early versions of Pango, we can't tell |
1230 | * monospaced fonts from non-monospaced. |
1231 | */ |
1232 | if (!pango_font_family_is_monospace(family)) |
1233 | flags |= FONTFLAG_NONMONOSPACED; |
1234 | #endif |
1235 | familyname = pango_font_family_get_name(family); |
1236 | |
1237 | /* |
1238 | * Go through the available font faces in this family. |
1239 | */ |
1240 | pango_font_family_list_faces(family, &faces, &nfaces); |
1241 | for (j = 0; j < nfaces; j++) { |
1242 | PangoFontFace *face = faces[j]; |
1243 | PangoFontDescription *desc; |
1244 | const char *facename; |
1245 | int *sizes; |
1246 | int k, nsizes, dummysize; |
1247 | |
1248 | /* |
1249 | * Get the face name string. |
1250 | */ |
1251 | facename = pango_font_face_get_face_name(face); |
1252 | |
1253 | /* |
1254 | * Set up a font description with what we've got so |
1255 | * far. We'll fill in the size field manually and then |
1256 | * call pango_font_description_to_string() to give the |
1257 | * full real name of the specific font. |
1258 | */ |
1259 | desc = pango_font_face_describe(face); |
1260 | |
1261 | /* |
1262 | * See if this font has a list of specific sizes. |
1263 | */ |
1264 | #ifndef PANGO_PRE_1POINT4 |
1265 | pango_font_face_list_sizes(face, &sizes, &nsizes); |
1266 | #else |
1267 | /* |
1268 | * In early versions of Pango, that call wasn't |
1269 | * supported; we just have to assume everything is |
1270 | * scalable. |
1271 | */ |
1272 | sizes = NULL; |
1273 | #endif |
1274 | if (!sizes) { |
1275 | /* |
1276 | * Write a single entry with a dummy size. |
1277 | */ |
1278 | dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE; |
1279 | sizes = &dummysize; |
1280 | nsizes = 1; |
1281 | } |
1282 | |
1283 | /* |
1284 | * If so, go through them one by one. |
1285 | */ |
1286 | for (k = 0; k < nsizes; k++) { |
1287 | char *fullname; |
1288 | char stylekey[128]; |
1289 | |
1290 | pango_font_description_set_size(desc, sizes[k]); |
1291 | |
1292 | fullname = pango_font_description_to_string(desc); |
1293 | |
1294 | /* |
1295 | * Construct the sorting key for font styles. |
1296 | */ |
1297 | { |
1298 | char *p = stylekey; |
1299 | int n; |
1300 | |
1301 | n = pango_font_description_get_weight(desc); |
1302 | /* Weight: normal, then lighter, then bolder */ |
1303 | if (n <= PANGO_WEIGHT_NORMAL) |
1304 | n = PANGO_WEIGHT_NORMAL - n; |
1305 | p += sprintf(p, "%4d", n); |
1306 | |
1307 | n = pango_font_description_get_style(desc); |
1308 | p += sprintf(p, " %2d", n); |
1309 | |
1310 | n = pango_font_description_get_stretch(desc); |
1311 | /* Stretch: closer to normal sorts earlier */ |
1312 | n = 2 * abs(PANGO_STRETCH_NORMAL - n) + |
1313 | (n < PANGO_STRETCH_NORMAL); |
1314 | p += sprintf(p, " %2d", n); |
1315 | |
1316 | n = pango_font_description_get_variant(desc); |
1317 | p += sprintf(p, " %2d", n); |
1318 | |
1319 | } |
1320 | |
1321 | /* |
1322 | * Got everything. Hand off to the callback. |
1323 | * (The charset string is NULL, because only |
1324 | * server-side X fonts use it.) |
1325 | */ |
1326 | callback(callback_ctx, fullname, familyname, NULL, facename, |
1327 | stylekey, |
1328 | (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])), |
1329 | flags, &pangofont_vtable); |
1330 | |
1331 | g_free(fullname); |
1332 | } |
1333 | if (sizes != &dummysize) |
1334 | g_free(sizes); |
1335 | |
1336 | pango_font_description_free(desc); |
1337 | } |
1338 | g_free(faces); |
1339 | } |
1340 | g_free(families); |
1341 | } |
1342 | |
1343 | static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name, |
1344 | int *size, int *flags, |
1345 | int resolve_aliases) |
1346 | { |
1347 | /* |
1348 | * When given a Pango font name to try to make sense of for a |
1349 | * font selector, we must normalise it to PANGO_DUMMY_SIZE and |
1350 | * extract its original size (in pixels) into the `size' field. |
1351 | */ |
1352 | PangoContext *ctx; |
1353 | #ifndef PANGO_PRE_1POINT6 |
1354 | PangoFontMap *map; |
1355 | #endif |
1356 | PangoFontDescription *desc; |
1357 | PangoFontset *fset; |
1358 | PangoFontMetrics *metrics; |
1359 | char *newname, *retname; |
1360 | |
1361 | desc = pango_font_description_from_string(name); |
1362 | if (!desc) |
1363 | return NULL; |
1364 | ctx = gtk_widget_get_pango_context(widget); |
1365 | if (!ctx) { |
1366 | pango_font_description_free(desc); |
1367 | return NULL; |
1368 | } |
1369 | if (!pangofont_check_desc_makes_sense(ctx, desc)) { |
1370 | pango_font_description_free(desc); |
1371 | return NULL; |
1372 | } |
1373 | #ifndef PANGO_PRE_1POINT6 |
1374 | map = pango_context_get_font_map(ctx); |
1375 | if (!map) { |
1376 | pango_font_description_free(desc); |
1377 | return NULL; |
1378 | } |
1379 | fset = pango_font_map_load_fontset(map, ctx, desc, |
1380 | pango_context_get_language(ctx)); |
1381 | #else |
1382 | fset = pango_context_load_fontset(ctx, desc, |
1383 | pango_context_get_language(ctx)); |
1384 | #endif |
1385 | if (!fset) { |
1386 | pango_font_description_free(desc); |
1387 | return NULL; |
1388 | } |
1389 | metrics = pango_fontset_get_metrics(fset); |
1390 | if (!metrics || |
1391 | pango_font_metrics_get_approximate_digit_width(metrics) == 0) { |
1392 | pango_font_description_free(desc); |
1393 | g_object_unref(fset); |
1394 | return NULL; |
1395 | } |
1396 | |
1397 | *size = PANGO_PIXELS(pango_font_description_get_size(desc)); |
1398 | *flags = FONTFLAG_CLIENTSIDE; |
1399 | pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE); |
1400 | newname = pango_font_description_to_string(desc); |
1401 | retname = dupstr(newname); |
1402 | g_free(newname); |
1403 | |
1404 | pango_font_metrics_unref(metrics); |
1405 | pango_font_description_free(desc); |
1406 | g_object_unref(fset); |
1407 | |
1408 | return retname; |
1409 | } |
1410 | |
1411 | static char *pangofont_scale_fontname(GtkWidget *widget, const char *name, |
1412 | int size) |
1413 | { |
1414 | PangoFontDescription *desc; |
1415 | char *newname, *retname; |
1416 | |
1417 | desc = pango_font_description_from_string(name); |
1418 | if (!desc) |
1419 | return NULL; |
1420 | pango_font_description_set_size(desc, size * PANGO_SCALE); |
1421 | newname = pango_font_description_to_string(desc); |
1422 | retname = dupstr(newname); |
1423 | g_free(newname); |
1424 | pango_font_description_free(desc); |
1425 | |
1426 | return retname; |
1427 | } |
1428 | |
1429 | #endif /* GTK_CHECK_VERSION(2,0,0) */ |
1430 | |
1431 | /* ---------------------------------------------------------------------- |
1432 | * Outermost functions which do the vtable dispatch. |
1433 | */ |
1434 | |
1435 | /* |
1436 | * Complete list of font-type subclasses. Listed in preference |
1437 | * order for unifont_create(). (That is, in the extremely unlikely |
1438 | * event that the same font name is valid as both a Pango and an |
1439 | * X11 font, it will be interpreted as the former in the absence |
1440 | * of an explicit type-disambiguating prefix.) |
0affbab4 |
1441 | * |
1442 | * The 'multifont' subclass is omitted here, as discussed above. |
f96e9a61 |
1443 | */ |
1444 | static const struct unifont_vtable *unifont_types[] = { |
1445 | #if GTK_CHECK_VERSION(2,0,0) |
1446 | &pangofont_vtable, |
1447 | #endif |
1448 | &x11font_vtable, |
1449 | }; |
1450 | |
1451 | /* |
1452 | * Function which takes a font name and processes the optional |
1453 | * scheme prefix. Returns the tail of the font name suitable for |
1454 | * passing to individual font scheme functions, and also provides |
1455 | * a subrange of the unifont_types[] array above. |
1456 | * |
1457 | * The return values `start' and `end' denote a half-open interval |
1458 | * in unifont_types[]; that is, the correct way to iterate over |
1459 | * them is |
1460 | * |
1461 | * for (i = start; i < end; i++) {...} |
1462 | */ |
1463 | static const char *unifont_do_prefix(const char *name, int *start, int *end) |
1464 | { |
1465 | int colonpos = strcspn(name, ":"); |
1466 | int i; |
1467 | |
1468 | if (name[colonpos]) { |
1469 | /* |
1470 | * There's a colon prefix on the font name. Use it to work |
1471 | * out which subclass to use. |
1472 | */ |
1473 | for (i = 0; i < lenof(unifont_types); i++) { |
1474 | if (strlen(unifont_types[i]->prefix) == colonpos && |
1475 | !strncmp(unifont_types[i]->prefix, name, colonpos)) { |
1476 | *start = i; |
1477 | *end = i+1; |
1478 | return name + colonpos + 1; |
1479 | } |
1480 | } |
1481 | /* |
1482 | * None matched, so return an empty scheme list to prevent |
1483 | * any scheme from being called at all. |
1484 | */ |
1485 | *start = *end = 0; |
1486 | return name + colonpos + 1; |
1487 | } else { |
1488 | /* |
1489 | * No colon prefix, so just use all the subclasses. |
1490 | */ |
1491 | *start = 0; |
1492 | *end = lenof(unifont_types); |
1493 | return name; |
1494 | } |
1495 | } |
1496 | |
1497 | unifont *unifont_create(GtkWidget *widget, const char *name, int wide, |
1498 | int bold, int shadowoffset, int shadowalways) |
1499 | { |
1500 | int i, start, end; |
1501 | |
1502 | name = unifont_do_prefix(name, &start, &end); |
1503 | |
1504 | for (i = start; i < end; i++) { |
1505 | unifont *ret = unifont_types[i]->create(widget, name, wide, bold, |
1506 | shadowoffset, shadowalways); |
1507 | if (ret) |
1508 | return ret; |
1509 | } |
1510 | return NULL; /* font not found in any scheme */ |
1511 | } |
1512 | |
1513 | void unifont_destroy(unifont *font) |
1514 | { |
1515 | font->vt->destroy(font); |
1516 | } |
1517 | |
1518 | void unifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
572820e1 |
1519 | int x, int y, const wchar_t *string, int len, |
f96e9a61 |
1520 | int wide, int bold, int cellwidth) |
1521 | { |
1522 | font->vt->draw_text(target, gc, font, x, y, string, len, |
1523 | wide, bold, cellwidth); |
1524 | } |
1525 | |
0affbab4 |
1526 | /* ---------------------------------------------------------------------- |
1527 | * Multiple-font wrapper. This is a type of unifont which encapsulates |
1528 | * up to two other unifonts, permitting missing glyphs in the main |
1529 | * font to be filled in by a fallback font. |
1530 | * |
1531 | * This is a type of unifont just like the previous two, but it has a |
1532 | * separate constructor which is manually called by the client, so it |
1533 | * doesn't appear in the list of available font types enumerated by |
1534 | * unifont_create. This means it's not used by unifontsel either, so |
1535 | * it doesn't need to support any methods except draw_text and |
1536 | * destroy. |
1537 | */ |
1538 | |
1539 | static void multifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
1540 | int x, int y, const wchar_t *string, int len, |
1541 | int wide, int bold, int cellwidth); |
1542 | static void multifont_destroy(unifont *font); |
1543 | |
1544 | struct multifont { |
1545 | struct unifont u; |
1546 | unifont *main; |
1547 | unifont *fallback; |
1548 | }; |
1549 | |
1550 | static const struct unifont_vtable multifont_vtable = { |
1551 | NULL, /* creation is done specially */ |
1552 | NULL, |
1553 | multifont_destroy, |
1554 | NULL, |
1555 | multifont_draw_text, |
1556 | NULL, |
1557 | NULL, |
1558 | NULL, |
1559 | "client", |
1560 | }; |
1561 | |
1562 | unifont *multifont_create(GtkWidget *widget, const char *name, |
1563 | int wide, int bold, |
1564 | int shadowoffset, int shadowalways) |
1565 | { |
1566 | int i; |
1567 | unifont *font, *fallback; |
1568 | struct multifont *mfont; |
1569 | |
1570 | font = unifont_create(widget, name, wide, bold, |
1571 | shadowoffset, shadowalways); |
1572 | if (!font) |
1573 | return NULL; |
1574 | |
b473b52b |
1575 | fallback = NULL; |
0affbab4 |
1576 | if (font->want_fallback) { |
1577 | for (i = 0; i < lenof(unifont_types); i++) { |
1578 | if (unifont_types[i]->create_fallback) { |
1579 | fallback = unifont_types[i]->create_fallback |
1580 | (widget, font->height, wide, bold, |
1581 | shadowoffset, shadowalways); |
1582 | if (fallback) |
1583 | break; |
1584 | } |
1585 | } |
1586 | } |
1587 | |
1588 | /* |
1589 | * Construct our multifont. Public members are all copied from the |
1590 | * primary font we're wrapping. |
1591 | */ |
1592 | mfont = snew(struct multifont); |
1593 | mfont->u.vt = &multifont_vtable; |
1594 | mfont->u.width = font->width; |
1595 | mfont->u.ascent = font->ascent; |
1596 | mfont->u.descent = font->descent; |
1597 | mfont->u.height = font->height; |
1598 | mfont->u.public_charset = font->public_charset; |
1599 | mfont->u.want_fallback = FALSE; /* shouldn't be needed, but just in case */ |
1600 | mfont->main = font; |
1601 | mfont->fallback = fallback; |
1602 | |
1603 | return (unifont *)mfont; |
1604 | } |
1605 | |
1606 | static void multifont_destroy(unifont *font) |
1607 | { |
1608 | struct multifont *mfont = (struct multifont *)font; |
1609 | unifont_destroy(mfont->main); |
1610 | if (mfont->fallback) |
1611 | unifont_destroy(mfont->fallback); |
1612 | sfree(font); |
1613 | } |
1614 | |
1615 | static void multifont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font, |
1616 | int x, int y, const wchar_t *string, int len, |
1617 | int wide, int bold, int cellwidth) |
1618 | { |
1619 | struct multifont *mfont = (struct multifont *)font; |
1620 | int ok, i; |
1621 | |
1622 | while (len > 0) { |
1623 | /* |
1624 | * Find a maximal sequence of characters which are, or are |
1625 | * not, supported by our main font. |
1626 | */ |
1627 | ok = mfont->main->vt->has_glyph(mfont->main, string[0]); |
1628 | for (i = 1; |
1629 | i < len && |
1630 | !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok; |
1631 | i++); |
1632 | |
1633 | /* |
1634 | * Now display it. |
1635 | */ |
1636 | unifont_draw_text(target, gc, ok ? mfont->main : mfont->fallback, |
1637 | x, y, string, i, wide, bold, cellwidth); |
1638 | string += i; |
1639 | len -= i; |
1640 | x += i * cellwidth; |
1641 | } |
1642 | } |
1643 | |
f96e9a61 |
1644 | #if GTK_CHECK_VERSION(2,0,0) |
1645 | |
1646 | /* ---------------------------------------------------------------------- |
1647 | * Implementation of a unified font selector. Used on GTK 2 only; |
1648 | * for GTK 1 we still use the standard font selector. |
1649 | */ |
1650 | |
1651 | typedef struct fontinfo fontinfo; |
1652 | |
1653 | typedef struct unifontsel_internal { |
1654 | /* This must be the structure's first element, for cross-casting */ |
1655 | unifontsel u; |
1656 | GtkListStore *family_model, *style_model, *size_model; |
1657 | GtkWidget *family_list, *style_list, *size_entry, *size_list; |
1658 | GtkWidget *filter_buttons[4]; |
1659 | GtkWidget *preview_area; |
1660 | GdkPixmap *preview_pixmap; |
1661 | int preview_width, preview_height; |
1662 | GdkColor preview_fg, preview_bg; |
1663 | int filter_flags; |
1664 | tree234 *fonts_by_realname, *fonts_by_selorder; |
1665 | fontinfo *selected; |
1666 | int selsize, intendedsize; |
1667 | int inhibit_response; /* inhibit callbacks when we change GUI controls */ |
1668 | } unifontsel_internal; |
1669 | |
1670 | /* |
1671 | * The structure held in the tree234s. All the string members are |
1672 | * part of the same allocated area, so don't need freeing |
1673 | * separately. |
1674 | */ |
1675 | struct fontinfo { |
1676 | char *realname; |
1677 | char *family, *charset, *style, *stylekey; |
1678 | int size, flags; |
1679 | /* |
1680 | * Fallback sorting key, to permit multiple identical entries |
1681 | * to exist in the selorder tree. |
1682 | */ |
1683 | int index; |
1684 | /* |
1685 | * Indices mapping fontinfo structures to indices in the list |
1686 | * boxes. sizeindex is irrelevant if the font is scalable |
1687 | * (size==0). |
1688 | */ |
1689 | int familyindex, styleindex, sizeindex; |
1690 | /* |
1691 | * The class of font. |
1692 | */ |
1693 | const struct unifont_vtable *fontclass; |
1694 | }; |
1695 | |
1696 | struct fontinfo_realname_find { |
1697 | const char *realname; |
1698 | int flags; |
1699 | }; |
1700 | |
1701 | static int strnullcasecmp(const char *a, const char *b) |
1702 | { |
1703 | int i; |
1704 | |
1705 | /* |
1706 | * If exactly one of the inputs is NULL, it compares before |
1707 | * the other one. |
1708 | */ |
1709 | if ((i = (!b) - (!a)) != 0) |
1710 | return i; |
1711 | |
1712 | /* |
1713 | * NULL compares equal. |
1714 | */ |
1715 | if (!a) |
1716 | return 0; |
1717 | |
1718 | /* |
1719 | * Otherwise, ordinary strcasecmp. |
1720 | */ |
3bd9f270 |
1721 | return g_ascii_strcasecmp(a, b); |
f96e9a61 |
1722 | } |
1723 | |
1724 | static int fontinfo_realname_compare(void *av, void *bv) |
1725 | { |
1726 | fontinfo *a = (fontinfo *)av; |
1727 | fontinfo *b = (fontinfo *)bv; |
1728 | int i; |
1729 | |
1730 | if ((i = strnullcasecmp(a->realname, b->realname)) != 0) |
1731 | return i; |
1732 | if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK)) |
1733 | return ((a->flags & FONTFLAG_SORT_MASK) < |
1734 | (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1); |
1735 | return 0; |
1736 | } |
1737 | |
1738 | static int fontinfo_realname_find(void *av, void *bv) |
1739 | { |
1740 | struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av; |
1741 | fontinfo *b = (fontinfo *)bv; |
1742 | int i; |
1743 | |
1744 | if ((i = strnullcasecmp(a->realname, b->realname)) != 0) |
1745 | return i; |
1746 | if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK)) |
1747 | return ((a->flags & FONTFLAG_SORT_MASK) < |
1748 | (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1); |
1749 | return 0; |
1750 | } |
1751 | |
1752 | static int fontinfo_selorder_compare(void *av, void *bv) |
1753 | { |
1754 | fontinfo *a = (fontinfo *)av; |
1755 | fontinfo *b = (fontinfo *)bv; |
1756 | int i; |
1757 | if ((i = strnullcasecmp(a->family, b->family)) != 0) |
1758 | return i; |
1759 | /* |
1760 | * Font class comes immediately after family, so that fonts |
1761 | * from different classes with the same family |
1762 | */ |
1763 | if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK)) |
1764 | return ((a->flags & FONTFLAG_SORT_MASK) < |
1765 | (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1); |
1766 | if ((i = strnullcasecmp(a->charset, b->charset)) != 0) |
1767 | return i; |
1768 | if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0) |
1769 | return i; |
1770 | if ((i = strnullcasecmp(a->style, b->style)) != 0) |
1771 | return i; |
1772 | if (a->size != b->size) |
1773 | return (a->size < b->size ? -1 : +1); |
1774 | if (a->index != b->index) |
1775 | return (a->index < b->index ? -1 : +1); |
1776 | return 0; |
1777 | } |
1778 | |
1779 | static void unifontsel_deselect(unifontsel_internal *fs) |
1780 | { |
1781 | fs->selected = NULL; |
1782 | gtk_list_store_clear(fs->style_model); |
1783 | gtk_list_store_clear(fs->size_model); |
1784 | gtk_widget_set_sensitive(fs->u.ok_button, FALSE); |
1785 | gtk_widget_set_sensitive(fs->size_entry, FALSE); |
1786 | } |
1787 | |
1788 | static void unifontsel_setup_familylist(unifontsel_internal *fs) |
1789 | { |
1790 | GtkTreeIter iter; |
1791 | int i, listindex, minpos = -1, maxpos = -1; |
1792 | char *currfamily = NULL; |
1793 | int currflags = -1; |
1794 | fontinfo *info; |
1795 | |
1796 | gtk_list_store_clear(fs->family_model); |
1797 | listindex = 0; |
1798 | |
1799 | /* |
1800 | * Search through the font tree for anything matching our |
1801 | * current filter criteria. When we find one, add its font |
1802 | * name to the list box. |
1803 | */ |
1804 | for (i = 0 ;; i++) { |
1805 | info = (fontinfo *)index234(fs->fonts_by_selorder, i); |
1806 | /* |
1807 | * info may be NULL if we've just run off the end of the |
1808 | * tree. We must still do a processing pass in that |
1809 | * situation, in case we had an unfinished font record in |
1810 | * progress. |
1811 | */ |
1812 | if (info && (info->flags &~ fs->filter_flags)) { |
1813 | info->familyindex = -1; |
1814 | continue; /* we're filtering out this font */ |
1815 | } |
1816 | if (!info || strnullcasecmp(currfamily, info->family) || |
1817 | currflags != (info->flags & FONTFLAG_SORT_MASK)) { |
1818 | /* |
1819 | * We've either finished a family, or started a new |
1820 | * one, or both. |
1821 | */ |
1822 | if (currfamily) { |
1823 | gtk_list_store_append(fs->family_model, &iter); |
1824 | gtk_list_store_set(fs->family_model, &iter, |
1825 | 0, currfamily, 1, minpos, 2, maxpos+1, -1); |
1826 | listindex++; |
1827 | } |
1828 | if (info) { |
1829 | minpos = i; |
1830 | currfamily = info->family; |
1831 | currflags = info->flags & FONTFLAG_SORT_MASK; |
1832 | } |
1833 | } |
1834 | if (!info) |
1835 | break; /* now we're done */ |
1836 | info->familyindex = listindex; |
1837 | maxpos = i; |
1838 | } |
1839 | |
1840 | /* |
1841 | * If we've just filtered out the previously selected font, |
1842 | * deselect it thoroughly. |
1843 | */ |
1844 | if (fs->selected && fs->selected->familyindex < 0) |
1845 | unifontsel_deselect(fs); |
1846 | } |
1847 | |
1848 | static void unifontsel_setup_stylelist(unifontsel_internal *fs, |
1849 | int start, int end) |
1850 | { |
1851 | GtkTreeIter iter; |
1852 | int i, listindex, minpos = -1, maxpos = -1, started = FALSE; |
1853 | char *currcs = NULL, *currstyle = NULL; |
1854 | fontinfo *info; |
1855 | |
1856 | gtk_list_store_clear(fs->style_model); |
1857 | listindex = 0; |
1858 | started = FALSE; |
1859 | |
1860 | /* |
1861 | * Search through the font tree for anything matching our |
1862 | * current filter criteria. When we find one, add its charset |
1863 | * and/or style name to the list box. |
1864 | */ |
1865 | for (i = start; i <= end; i++) { |
1866 | if (i == end) |
1867 | info = NULL; |
1868 | else |
1869 | info = (fontinfo *)index234(fs->fonts_by_selorder, i); |
1870 | /* |
1871 | * info may be NULL if we've just run off the end of the |
1872 | * relevant data. We must still do a processing pass in |
1873 | * that situation, in case we had an unfinished font |
1874 | * record in progress. |
1875 | */ |
1876 | if (info && (info->flags &~ fs->filter_flags)) { |
1877 | info->styleindex = -1; |
1878 | continue; /* we're filtering out this font */ |
1879 | } |
1880 | if (!info || !started || strnullcasecmp(currcs, info->charset) || |
1881 | strnullcasecmp(currstyle, info->style)) { |
1882 | /* |
1883 | * We've either finished a style/charset, or started a |
1884 | * new one, or both. |
1885 | */ |
1886 | started = TRUE; |
1887 | if (currstyle) { |
1888 | gtk_list_store_append(fs->style_model, &iter); |
1889 | gtk_list_store_set(fs->style_model, &iter, |
1890 | 0, currstyle, 1, minpos, 2, maxpos+1, |
1891 | 3, TRUE, -1); |
1892 | listindex++; |
1893 | } |
1894 | if (info) { |
1895 | minpos = i; |
1896 | if (info->charset && strnullcasecmp(currcs, info->charset)) { |
1897 | gtk_list_store_append(fs->style_model, &iter); |
1898 | gtk_list_store_set(fs->style_model, &iter, |
1899 | 0, info->charset, 1, -1, 2, -1, |
1900 | 3, FALSE, -1); |
1901 | listindex++; |
1902 | } |
1903 | currcs = info->charset; |
1904 | currstyle = info->style; |
1905 | } |
1906 | } |
1907 | if (!info) |
1908 | break; /* now we're done */ |
1909 | info->styleindex = listindex; |
1910 | maxpos = i; |
1911 | } |
1912 | } |
1913 | |
1914 | static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 }; |
1915 | |
1916 | static void unifontsel_setup_sizelist(unifontsel_internal *fs, |
1917 | int start, int end) |
1918 | { |
1919 | GtkTreeIter iter; |
1920 | int i, listindex; |
1921 | char sizetext[40]; |
1922 | fontinfo *info; |
1923 | |
1924 | gtk_list_store_clear(fs->size_model); |
1925 | listindex = 0; |
1926 | |
1927 | /* |
1928 | * Search through the font tree for anything matching our |
1929 | * current filter criteria. When we find one, add its font |
1930 | * name to the list box. |
1931 | */ |
1932 | for (i = start; i < end; i++) { |
1933 | info = (fontinfo *)index234(fs->fonts_by_selorder, i); |
1934 | if (info->flags &~ fs->filter_flags) { |
1935 | info->sizeindex = -1; |
1936 | continue; /* we're filtering out this font */ |
1937 | } |
1938 | if (info->size) { |
1939 | sprintf(sizetext, "%d", info->size); |
1940 | info->sizeindex = listindex; |
1941 | gtk_list_store_append(fs->size_model, &iter); |
1942 | gtk_list_store_set(fs->size_model, &iter, |
1943 | 0, sizetext, 1, i, 2, info->size, -1); |
1944 | listindex++; |
1945 | } else { |
1946 | int j; |
1947 | |
1948 | assert(i == start); |
1949 | assert(i+1 == end); |
1950 | |
1951 | for (j = 0; j < lenof(unifontsel_default_sizes); j++) { |
1952 | sprintf(sizetext, "%d", unifontsel_default_sizes[j]); |
1953 | gtk_list_store_append(fs->size_model, &iter); |
1954 | gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i, |
1955 | 2, unifontsel_default_sizes[j], -1); |
1956 | listindex++; |
1957 | } |
1958 | } |
1959 | } |
1960 | } |
1961 | |
1962 | static void unifontsel_set_filter_buttons(unifontsel_internal *fs) |
1963 | { |
1964 | int i; |
1965 | |
1966 | for (i = 0; i < lenof(fs->filter_buttons); i++) { |
1967 | int flagbit = GPOINTER_TO_INT(gtk_object_get_data |
1968 | (GTK_OBJECT(fs->filter_buttons[i]), |
1969 | "user-data")); |
1970 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]), |
1971 | !!(fs->filter_flags & flagbit)); |
1972 | } |
1973 | } |
1974 | |
1975 | static void unifontsel_draw_preview_text(unifontsel_internal *fs) |
1976 | { |
1977 | unifont *font; |
1978 | char *sizename = NULL; |
1979 | fontinfo *info = fs->selected; |
1980 | |
1981 | if (info) { |
1982 | sizename = info->fontclass->scale_fontname |
1983 | (GTK_WIDGET(fs->u.window), info->realname, fs->selsize); |
1984 | font = info->fontclass->create(GTK_WIDGET(fs->u.window), |
1985 | sizename ? sizename : info->realname, |
1986 | FALSE, FALSE, 0, 0); |
1987 | } else |
1988 | font = NULL; |
1989 | |
1990 | if (fs->preview_pixmap) { |
1991 | GdkGC *gc = gdk_gc_new(fs->preview_pixmap); |
1992 | gdk_gc_set_foreground(gc, &fs->preview_bg); |
1993 | gdk_draw_rectangle(fs->preview_pixmap, gc, 1, 0, 0, |
1994 | fs->preview_width, fs->preview_height); |
1995 | gdk_gc_set_foreground(gc, &fs->preview_fg); |
1996 | if (font) { |
1997 | /* |
1998 | * The pangram used here is rather carefully |
1999 | * constructed: it contains a sequence of very narrow |
2000 | * letters (`jil') and a pair of adjacent very wide |
2001 | * letters (`wm'). |
2002 | * |
2003 | * If the user selects a proportional font, it will be |
2004 | * coerced into fixed-width character cells when used |
2005 | * in the actual terminal window. We therefore display |
2006 | * it the same way in the preview pane, so as to show |
2007 | * it the way it will actually be displayed - and we |
2008 | * deliberately pick a pangram which will show the |
2009 | * resulting miskerning at its worst. |
2010 | * |
2011 | * We aren't trying to sell people these fonts; we're |
2012 | * trying to let them make an informed choice. Better |
2013 | * that they find out the problems with using |
2014 | * proportional fonts in terminal windows here than |
2015 | * that they go to the effort of selecting their font |
2016 | * and _then_ realise it was a mistake. |
2017 | */ |
2018 | info->fontclass->draw_text(fs->preview_pixmap, gc, font, |
2019 | 0, font->ascent, |
572820e1 |
2020 | L"bankrupt jilted showmen quiz convex fogey", |
f96e9a61 |
2021 | 41, FALSE, FALSE, font->width); |
2022 | info->fontclass->draw_text(fs->preview_pixmap, gc, font, |
2023 | 0, font->ascent + font->height, |
572820e1 |
2024 | L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY", |
f96e9a61 |
2025 | 41, FALSE, FALSE, font->width); |
2026 | /* |
2027 | * The ordering of punctuation here is also selected |
2028 | * with some specific aims in mind. I put ` and ' |
2029 | * together because some software (and people) still |
2030 | * use them as matched quotes no matter what Unicode |
2031 | * might say on the matter, so people can quickly |
2032 | * check whether they look silly in a candidate font. |
2033 | * The sequence #_@ is there to let people judge the |
2034 | * suitability of the underscore as an effectively |
2035 | * alphabetic character (since that's how it's often |
2036 | * used in practice, at least by programmers). |
2037 | */ |
2038 | info->fontclass->draw_text(fs->preview_pixmap, gc, font, |
2039 | 0, font->ascent + font->height * 2, |
572820e1 |
2040 | L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$", |
f96e9a61 |
2041 | 42, FALSE, FALSE, font->width); |
2042 | } |
2043 | gdk_gc_unref(gc); |
2044 | gdk_window_invalidate_rect(fs->preview_area->window, NULL, FALSE); |
2045 | } |
2046 | if (font) |
2047 | info->fontclass->destroy(font); |
2048 | |
2049 | sfree(sizename); |
2050 | } |
2051 | |
2052 | static void unifontsel_select_font(unifontsel_internal *fs, |
2053 | fontinfo *info, int size, int leftlist, |
2054 | int size_is_explicit) |
2055 | { |
2056 | int index; |
2057 | int minval, maxval; |
2058 | GtkTreePath *treepath; |
2059 | GtkTreeIter iter; |
2060 | |
2061 | fs->inhibit_response = TRUE; |
2062 | |
2063 | fs->selected = info; |
2064 | fs->selsize = size; |
2065 | if (size_is_explicit) |
2066 | fs->intendedsize = size; |
2067 | |
2068 | gtk_widget_set_sensitive(fs->u.ok_button, TRUE); |
2069 | |
2070 | /* |
2071 | * Find the index of this fontinfo in the selorder list. |
2072 | */ |
2073 | index = -1; |
2074 | findpos234(fs->fonts_by_selorder, info, NULL, &index); |
2075 | assert(index >= 0); |
2076 | |
2077 | /* |
2078 | * Adjust the font selector flags and redo the font family |
2079 | * list box, if necessary. |
2080 | */ |
2081 | if (leftlist <= 0 && |
2082 | (fs->filter_flags | info->flags) != fs->filter_flags) { |
2083 | fs->filter_flags |= info->flags; |
2084 | unifontsel_set_filter_buttons(fs); |
2085 | unifontsel_setup_familylist(fs); |
2086 | } |
2087 | |
2088 | /* |
2089 | * Find the appropriate family name and select it in the list. |
2090 | */ |
2091 | assert(info->familyindex >= 0); |
2092 | treepath = gtk_tree_path_new_from_indices(info->familyindex, -1); |
2093 | gtk_tree_selection_select_path |
2094 | (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)), |
2095 | treepath); |
2096 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list), |
2097 | treepath, NULL, FALSE, 0.0, 0.0); |
2098 | gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, treepath); |
2099 | gtk_tree_path_free(treepath); |
2100 | |
2101 | /* |
2102 | * Now set up the font style list. |
2103 | */ |
2104 | gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, |
2105 | 1, &minval, 2, &maxval, -1); |
2106 | if (leftlist <= 1) |
2107 | unifontsel_setup_stylelist(fs, minval, maxval); |
2108 | |
2109 | /* |
2110 | * Find the appropriate style name and select it in the list. |
2111 | */ |
2112 | if (info->style) { |
2113 | assert(info->styleindex >= 0); |
2114 | treepath = gtk_tree_path_new_from_indices(info->styleindex, -1); |
2115 | gtk_tree_selection_select_path |
2116 | (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)), |
2117 | treepath); |
2118 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list), |
2119 | treepath, NULL, FALSE, 0.0, 0.0); |
2120 | gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model), |
2121 | &iter, treepath); |
2122 | gtk_tree_path_free(treepath); |
2123 | |
2124 | /* |
2125 | * And set up the size list. |
2126 | */ |
2127 | gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter, |
2128 | 1, &minval, 2, &maxval, -1); |
2129 | if (leftlist <= 2) |
2130 | unifontsel_setup_sizelist(fs, minval, maxval); |
2131 | |
2132 | /* |
2133 | * Find the appropriate size, and select it in the list. |
2134 | */ |
2135 | if (info->size) { |
2136 | assert(info->sizeindex >= 0); |
2137 | treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1); |
2138 | gtk_tree_selection_select_path |
2139 | (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)), |
2140 | treepath); |
2141 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list), |
2142 | treepath, NULL, FALSE, 0.0, 0.0); |
2143 | gtk_tree_path_free(treepath); |
2144 | size = info->size; |
2145 | } else { |
2146 | int j; |
2147 | for (j = 0; j < lenof(unifontsel_default_sizes); j++) |
2148 | if (unifontsel_default_sizes[j] == size) { |
2149 | treepath = gtk_tree_path_new_from_indices(j, -1); |
2150 | gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list), |
2151 | treepath, NULL, FALSE); |
2152 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list), |
2153 | treepath, NULL, FALSE, 0.0, |
2154 | 0.0); |
2155 | gtk_tree_path_free(treepath); |
2156 | } |
2157 | } |
2158 | |
2159 | /* |
2160 | * And set up the font size text entry box. |
2161 | */ |
2162 | { |
2163 | char sizetext[40]; |
2164 | sprintf(sizetext, "%d", size); |
2165 | gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext); |
2166 | } |
2167 | } else { |
2168 | if (leftlist <= 2) |
2169 | unifontsel_setup_sizelist(fs, 0, 0); |
2170 | gtk_entry_set_text(GTK_ENTRY(fs->size_entry), ""); |
2171 | } |
2172 | |
2173 | /* |
2174 | * Grey out the font size edit box if we're not using a |
2175 | * scalable font. |
2176 | */ |
2177 | gtk_entry_set_editable(GTK_ENTRY(fs->size_entry), fs->selected->size == 0); |
2178 | gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0); |
2179 | |
2180 | unifontsel_draw_preview_text(fs); |
2181 | |
2182 | fs->inhibit_response = FALSE; |
2183 | } |
2184 | |
2185 | static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data) |
2186 | { |
2187 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2188 | int newstate = gtk_toggle_button_get_active(tb); |
2189 | int newflags; |
2190 | int flagbit = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(tb), |
2191 | "user-data")); |
2192 | |
2193 | if (newstate) |
2194 | newflags = fs->filter_flags | flagbit; |
2195 | else |
2196 | newflags = fs->filter_flags & ~flagbit; |
2197 | |
2198 | if (fs->filter_flags != newflags) { |
2199 | fs->filter_flags = newflags; |
2200 | unifontsel_setup_familylist(fs); |
2201 | } |
2202 | } |
2203 | |
2204 | static void unifontsel_add_entry(void *ctx, const char *realfontname, |
2205 | const char *family, const char *charset, |
2206 | const char *style, const char *stylekey, |
2207 | int size, int flags, |
2208 | const struct unifont_vtable *fontclass) |
2209 | { |
2210 | unifontsel_internal *fs = (unifontsel_internal *)ctx; |
2211 | fontinfo *info; |
2212 | int totalsize; |
2213 | char *p; |
2214 | |
2215 | totalsize = sizeof(fontinfo) + strlen(realfontname) + |
2216 | (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) + |
2217 | (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10; |
2218 | info = (fontinfo *)smalloc(totalsize); |
2219 | info->fontclass = fontclass; |
2220 | p = (char *)info + sizeof(fontinfo); |
2221 | info->realname = p; |
2222 | strcpy(p, realfontname); |
2223 | p += 1+strlen(p); |
2224 | if (family) { |
2225 | info->family = p; |
2226 | strcpy(p, family); |
2227 | p += 1+strlen(p); |
2228 | } else |
2229 | info->family = NULL; |
2230 | if (charset) { |
2231 | info->charset = p; |
2232 | strcpy(p, charset); |
2233 | p += 1+strlen(p); |
2234 | } else |
2235 | info->charset = NULL; |
2236 | if (style) { |
2237 | info->style = p; |
2238 | strcpy(p, style); |
2239 | p += 1+strlen(p); |
2240 | } else |
2241 | info->style = NULL; |
2242 | if (stylekey) { |
2243 | info->stylekey = p; |
2244 | strcpy(p, stylekey); |
2245 | p += 1+strlen(p); |
2246 | } else |
2247 | info->stylekey = NULL; |
2248 | assert(p - (char *)info <= totalsize); |
2249 | info->size = size; |
2250 | info->flags = flags; |
2251 | info->index = count234(fs->fonts_by_selorder); |
2252 | |
2253 | /* |
2254 | * It's just conceivable that a misbehaving font enumerator |
2255 | * might tell us about the same font real name more than once, |
2256 | * in which case we should silently drop the new one. |
2257 | */ |
2258 | if (add234(fs->fonts_by_realname, info) != info) { |
2259 | sfree(info); |
2260 | return; |
2261 | } |
2262 | /* |
2263 | * However, we should never get a duplicate key in the |
2264 | * selorder tree, because the index field carefully |
2265 | * disambiguates otherwise identical records. |
2266 | */ |
2267 | add234(fs->fonts_by_selorder, info); |
2268 | } |
2269 | |
2270 | static fontinfo *update_for_intended_size(unifontsel_internal *fs, |
2271 | fontinfo *info) |
2272 | { |
2273 | fontinfo info2, *below, *above; |
2274 | int pos; |
2275 | |
2276 | /* |
2277 | * Copy the info structure. This doesn't copy its dynamic |
2278 | * string fields, but that's unimportant because all we're |
2279 | * going to do is to adjust the size field and use it in one |
2280 | * tree search. |
2281 | */ |
2282 | info2 = *info; |
2283 | info2.size = fs->intendedsize; |
2284 | |
2285 | /* |
2286 | * Search in the tree to find the fontinfo structure which |
2287 | * best approximates the size the user last requested. |
2288 | */ |
2289 | below = findrelpos234(fs->fonts_by_selorder, &info2, NULL, |
2290 | REL234_LE, &pos); |
2291 | above = index234(fs->fonts_by_selorder, pos+1); |
2292 | |
2293 | /* |
2294 | * See if we've found it exactly, which is an easy special |
2295 | * case. If we have, it'll be in `below' and not `above', |
2296 | * because we did a REL234_LE rather than REL234_LT search. |
2297 | */ |
2298 | if (!fontinfo_selorder_compare(&info2, below)) |
2299 | return below; |
2300 | |
2301 | /* |
2302 | * Now we've either found two suitable fonts, one smaller and |
2303 | * one larger, or we're at one or other extreme end of the |
2304 | * scale. Find out which, by NULLing out either of below and |
2305 | * above if it differs from this one in any respect but size |
2306 | * (and the disambiguating index field). Bear in mind, also, |
2307 | * that either one might _already_ be NULL if we're at the |
2308 | * extreme ends of the font list. |
2309 | */ |
2310 | if (below) { |
2311 | info2.size = below->size; |
2312 | info2.index = below->index; |
2313 | if (fontinfo_selorder_compare(&info2, below)) |
2314 | below = NULL; |
2315 | } |
2316 | if (above) { |
2317 | info2.size = above->size; |
2318 | info2.index = above->index; |
2319 | if (fontinfo_selorder_compare(&info2, above)) |
2320 | above = NULL; |
2321 | } |
2322 | |
2323 | /* |
2324 | * Now return whichever of above and below is non-NULL, if |
2325 | * that's unambiguous. |
2326 | */ |
2327 | if (!above) |
2328 | return below; |
2329 | if (!below) |
2330 | return above; |
2331 | |
2332 | /* |
2333 | * And now we really do have to make a choice about whether to |
2334 | * round up or down. We'll do it by rounding to nearest, |
2335 | * breaking ties by rounding up. |
2336 | */ |
2337 | if (above->size - fs->intendedsize <= fs->intendedsize - below->size) |
2338 | return above; |
2339 | else |
2340 | return below; |
2341 | } |
2342 | |
2343 | static void family_changed(GtkTreeSelection *treeselection, gpointer data) |
2344 | { |
2345 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2346 | GtkTreeModel *treemodel; |
2347 | GtkTreeIter treeiter; |
2348 | int minval; |
2349 | fontinfo *info; |
2350 | |
2351 | if (fs->inhibit_response) /* we made this change ourselves */ |
2352 | return; |
2353 | |
2354 | if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter)) |
2355 | return; |
2356 | |
2357 | gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1); |
2358 | info = (fontinfo *)index234(fs->fonts_by_selorder, minval); |
2359 | info = update_for_intended_size(fs, info); |
2360 | if (!info) |
2361 | return; /* _shouldn't_ happen unless font list is completely funted */ |
2362 | if (!info->size) |
2363 | fs->selsize = fs->intendedsize; /* font is scalable */ |
2364 | unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize, |
2365 | 1, FALSE); |
2366 | } |
2367 | |
2368 | static void style_changed(GtkTreeSelection *treeselection, gpointer data) |
2369 | { |
2370 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2371 | GtkTreeModel *treemodel; |
2372 | GtkTreeIter treeiter; |
2373 | int minval; |
2374 | fontinfo *info; |
2375 | |
2376 | if (fs->inhibit_response) /* we made this change ourselves */ |
2377 | return; |
2378 | |
2379 | if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter)) |
2380 | return; |
2381 | |
2382 | gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1); |
2383 | if (minval < 0) |
2384 | return; /* somehow a charset heading got clicked */ |
2385 | info = (fontinfo *)index234(fs->fonts_by_selorder, minval); |
2386 | info = update_for_intended_size(fs, info); |
2387 | if (!info) |
2388 | return; /* _shouldn't_ happen unless font list is completely funted */ |
2389 | if (!info->size) |
2390 | fs->selsize = fs->intendedsize; /* font is scalable */ |
2391 | unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize, |
2392 | 2, FALSE); |
2393 | } |
2394 | |
2395 | static void size_changed(GtkTreeSelection *treeselection, gpointer data) |
2396 | { |
2397 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2398 | GtkTreeModel *treemodel; |
2399 | GtkTreeIter treeiter; |
2400 | int minval, size; |
2401 | fontinfo *info; |
2402 | |
2403 | if (fs->inhibit_response) /* we made this change ourselves */ |
2404 | return; |
2405 | |
2406 | if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter)) |
2407 | return; |
2408 | |
2409 | gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1); |
2410 | info = (fontinfo *)index234(fs->fonts_by_selorder, minval); |
2411 | unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE); |
2412 | } |
2413 | |
2414 | static void size_entry_changed(GtkEditable *ed, gpointer data) |
2415 | { |
2416 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2417 | const char *text; |
2418 | int size; |
2419 | |
2420 | if (fs->inhibit_response) /* we made this change ourselves */ |
2421 | return; |
2422 | |
2423 | text = gtk_entry_get_text(GTK_ENTRY(ed)); |
2424 | size = atoi(text); |
2425 | |
2426 | if (size > 0) { |
2427 | assert(fs->selected->size == 0); |
2428 | unifontsel_select_font(fs, fs->selected, size, 3, TRUE); |
2429 | } |
2430 | } |
2431 | |
2432 | static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path, |
2433 | GtkTreeViewColumn *column, gpointer data) |
2434 | { |
2435 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2436 | GtkTreeIter iter; |
2437 | int minval, newsize; |
2438 | fontinfo *info, *newinfo; |
2439 | char *newname; |
2440 | |
2441 | if (fs->inhibit_response) /* we made this change ourselves */ |
2442 | return; |
2443 | |
2444 | gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path); |
2445 | gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1); |
2446 | info = (fontinfo *)index234(fs->fonts_by_selorder, minval); |
2447 | if (info) { |
2448 | int flags; |
2449 | struct fontinfo_realname_find f; |
2450 | |
2451 | newname = info->fontclass->canonify_fontname |
2452 | (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE); |
2453 | |
2454 | f.realname = newname; |
2455 | f.flags = flags; |
2456 | newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find); |
2457 | |
2458 | sfree(newname); |
2459 | if (!newinfo) |
2460 | return; /* font name not in our index */ |
2461 | if (newinfo == info) |
2462 | return; /* didn't change under canonification => not an alias */ |
2463 | unifontsel_select_font(fs, newinfo, |
2464 | newinfo->size ? newinfo->size : newsize, |
2465 | 1, TRUE); |
2466 | } |
2467 | } |
2468 | |
2469 | static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event, |
2470 | gpointer data) |
2471 | { |
2472 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2473 | |
2474 | if (fs->preview_pixmap) { |
2475 | gdk_draw_pixmap(widget->window, |
2476 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
2477 | fs->preview_pixmap, |
2478 | event->area.x, event->area.y, |
2479 | event->area.x, event->area.y, |
2480 | event->area.width, event->area.height); |
2481 | } |
2482 | return TRUE; |
2483 | } |
2484 | |
2485 | static gint unifontsel_configure_area(GtkWidget *widget, |
2486 | GdkEventConfigure *event, gpointer data) |
2487 | { |
2488 | unifontsel_internal *fs = (unifontsel_internal *)data; |
2489 | int ox, oy, nx, ny, x, y; |
2490 | |
2491 | /* |
2492 | * Enlarge the pixmap, but never shrink it. |
2493 | */ |
2494 | ox = fs->preview_width; |
2495 | oy = fs->preview_height; |
2496 | x = event->width; |
2497 | y = event->height; |
2498 | if (x > ox || y > oy) { |
2499 | if (fs->preview_pixmap) |
2500 | gdk_pixmap_unref(fs->preview_pixmap); |
2501 | |
2502 | nx = (x > ox ? x : ox); |
2503 | ny = (y > oy ? y : oy); |
2504 | fs->preview_pixmap = gdk_pixmap_new(widget->window, nx, ny, -1); |
2505 | fs->preview_width = nx; |
2506 | fs->preview_height = ny; |
2507 | |
2508 | unifontsel_draw_preview_text(fs); |
2509 | } |
2510 | |
2511 | gdk_window_invalidate_rect(widget->window, NULL, FALSE); |
2512 | |
2513 | return TRUE; |
2514 | } |
2515 | |
2516 | unifontsel *unifontsel_new(const char *wintitle) |
2517 | { |
2518 | unifontsel_internal *fs = snew(unifontsel_internal); |
2519 | GtkWidget *table, *label, *w, *ww, *scroll; |
2520 | GtkListStore *model; |
2521 | GtkTreeViewColumn *column; |
2522 | int lists_height, preview_height, font_width, style_width, size_width; |
2523 | int i; |
2524 | |
2525 | fs->inhibit_response = FALSE; |
2526 | fs->selected = NULL; |
2527 | |
2528 | { |
2529 | /* |
2530 | * Invent some magic size constants. |
2531 | */ |
2532 | GtkRequisition req; |
2533 | label = gtk_label_new("Quite Long Font Name (Foundry)"); |
2534 | gtk_widget_size_request(label, &req); |
2535 | font_width = req.width; |
2536 | lists_height = 14 * req.height; |
2537 | preview_height = 5 * req.height; |
2538 | gtk_label_set_text(GTK_LABEL(label), "Italic Extra Condensed"); |
2539 | gtk_widget_size_request(label, &req); |
2540 | style_width = req.width; |
2541 | gtk_label_set_text(GTK_LABEL(label), "48000"); |
2542 | gtk_widget_size_request(label, &req); |
2543 | size_width = req.width; |
2544 | #if GTK_CHECK_VERSION(2,10,0) |
2545 | g_object_ref_sink(label); |
2546 | g_object_unref(label); |
2547 | #else |
2548 | gtk_object_sink(GTK_OBJECT(label)); |
2549 | #endif |
2550 | } |
2551 | |
2552 | /* |
2553 | * Create the dialog box and initialise the user-visible |
2554 | * fields in the returned structure. |
2555 | */ |
2556 | fs->u.user_data = NULL; |
2557 | fs->u.window = GTK_WINDOW(gtk_dialog_new()); |
2558 | gtk_window_set_title(fs->u.window, wintitle); |
2559 | fs->u.cancel_button = gtk_dialog_add_button |
2560 | (GTK_DIALOG(fs->u.window), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL); |
2561 | fs->u.ok_button = gtk_dialog_add_button |
2562 | (GTK_DIALOG(fs->u.window), GTK_STOCK_OK, GTK_RESPONSE_OK); |
2563 | gtk_widget_grab_default(fs->u.ok_button); |
2564 | |
2565 | /* |
2566 | * Now set up the internal fields, including in particular all |
2567 | * the controls that actually allow the user to select fonts. |
2568 | */ |
2569 | table = gtk_table_new(8, 3, FALSE); |
2570 | gtk_widget_show(table); |
2571 | gtk_table_set_col_spacings(GTK_TABLE(table), 8); |
2572 | #if GTK_CHECK_VERSION(2,4,0) |
2573 | /* GtkAlignment seems to be the simplest way to put padding round things */ |
2574 | w = gtk_alignment_new(0, 0, 1, 1); |
2575 | gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8); |
2576 | gtk_container_add(GTK_CONTAINER(w), table); |
2577 | gtk_widget_show(w); |
2578 | #else |
2579 | w = table; |
2580 | #endif |
2581 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fs->u.window)->vbox), |
2582 | w, TRUE, TRUE, 0); |
2583 | |
2584 | label = gtk_label_new_with_mnemonic("_Font:"); |
2585 | gtk_widget_show(label); |
2586 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0); |
2587 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0); |
2588 | |
2589 | /* |
2590 | * The Font list box displays only a string, but additionally |
2591 | * stores two integers which give the limits within the |
2592 | * tree234 of the font entries covered by this list entry. |
2593 | */ |
2594 | model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT); |
2595 | w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
2596 | gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE); |
2597 | gtk_label_set_mnemonic_widget(GTK_LABEL(label), w); |
2598 | gtk_widget_show(w); |
2599 | column = gtk_tree_view_column_new_with_attributes |
2600 | ("Font", gtk_cell_renderer_text_new(), |
2601 | "text", 0, (char *)NULL); |
2602 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); |
2603 | gtk_tree_view_append_column(GTK_TREE_VIEW(w), column); |
2604 | g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))), |
2605 | "changed", G_CALLBACK(family_changed), fs); |
2606 | g_signal_connect(G_OBJECT(w), "row-activated", |
2607 | G_CALLBACK(alias_resolve), fs); |
2608 | |
2609 | scroll = gtk_scrolled_window_new(NULL, NULL); |
2610 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), |
2611 | GTK_SHADOW_IN); |
2612 | gtk_container_add(GTK_CONTAINER(scroll), w); |
2613 | gtk_widget_show(scroll); |
2614 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
2615 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); |
2616 | gtk_widget_set_size_request(scroll, font_width, lists_height); |
2617 | gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL, |
2618 | GTK_EXPAND | GTK_FILL, 0, 0); |
2619 | fs->family_model = model; |
2620 | fs->family_list = w; |
2621 | |
2622 | label = gtk_label_new_with_mnemonic("_Style:"); |
2623 | gtk_widget_show(label); |
2624 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0); |
2625 | gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0); |
2626 | |
2627 | /* |
2628 | * The Style list box can contain insensitive elements |
2629 | * (character set headings for server-side fonts), so we add |
2630 | * an extra column to the list store to hold that information. |
2631 | */ |
2632 | model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, |
2633 | G_TYPE_BOOLEAN); |
2634 | w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
2635 | gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE); |
2636 | gtk_label_set_mnemonic_widget(GTK_LABEL(label), w); |
2637 | gtk_widget_show(w); |
2638 | column = gtk_tree_view_column_new_with_attributes |
2639 | ("Style", gtk_cell_renderer_text_new(), |
2640 | "text", 0, "sensitive", 3, (char *)NULL); |
2641 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); |
2642 | gtk_tree_view_append_column(GTK_TREE_VIEW(w), column); |
2643 | g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))), |
2644 | "changed", G_CALLBACK(style_changed), fs); |
2645 | |
2646 | scroll = gtk_scrolled_window_new(NULL, NULL); |
2647 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), |
2648 | GTK_SHADOW_IN); |
2649 | gtk_container_add(GTK_CONTAINER(scroll), w); |
2650 | gtk_widget_show(scroll); |
2651 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
2652 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); |
2653 | gtk_widget_set_size_request(scroll, style_width, lists_height); |
2654 | gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL, |
2655 | GTK_EXPAND | GTK_FILL, 0, 0); |
2656 | fs->style_model = model; |
2657 | fs->style_list = w; |
2658 | |
2659 | label = gtk_label_new_with_mnemonic("Si_ze:"); |
2660 | gtk_widget_show(label); |
2661 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0); |
2662 | gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0); |
2663 | |
2664 | /* |
2665 | * The Size label attaches primarily to a text input box so |
2666 | * that the user can select a size of their choice. The list |
2667 | * of available sizes is secondary. |
2668 | */ |
2669 | fs->size_entry = w = gtk_entry_new(); |
2670 | gtk_label_set_mnemonic_widget(GTK_LABEL(label), w); |
2671 | gtk_widget_set_size_request(w, size_width, -1); |
2672 | gtk_widget_show(w); |
2673 | gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0); |
2674 | g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed), |
2675 | fs); |
2676 | |
2677 | model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT); |
2678 | w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
2679 | gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE); |
2680 | gtk_widget_show(w); |
2681 | column = gtk_tree_view_column_new_with_attributes |
2682 | ("Size", gtk_cell_renderer_text_new(), |
2683 | "text", 0, (char *)NULL); |
2684 | gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); |
2685 | gtk_tree_view_append_column(GTK_TREE_VIEW(w), column); |
2686 | g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))), |
2687 | "changed", G_CALLBACK(size_changed), fs); |
2688 | |
2689 | scroll = gtk_scrolled_window_new(NULL, NULL); |
2690 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), |
2691 | GTK_SHADOW_IN); |
2692 | gtk_container_add(GTK_CONTAINER(scroll), w); |
2693 | gtk_widget_show(scroll); |
2694 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
2695 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); |
2696 | gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL, |
2697 | GTK_EXPAND | GTK_FILL, 0, 0); |
2698 | fs->size_model = model; |
2699 | fs->size_list = w; |
2700 | |
2701 | /* |
2702 | * Preview widget. |
2703 | */ |
2704 | fs->preview_area = gtk_drawing_area_new(); |
2705 | fs->preview_pixmap = NULL; |
2706 | fs->preview_width = 0; |
2707 | fs->preview_height = 0; |
2708 | fs->preview_fg.pixel = fs->preview_bg.pixel = 0; |
2709 | fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000; |
2710 | fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF; |
2711 | gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg, |
2712 | FALSE, FALSE); |
2713 | gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg, |
2714 | FALSE, FALSE); |
2715 | gtk_signal_connect(GTK_OBJECT(fs->preview_area), "expose_event", |
2716 | GTK_SIGNAL_FUNC(unifontsel_expose_area), fs); |
2717 | gtk_signal_connect(GTK_OBJECT(fs->preview_area), "configure_event", |
2718 | GTK_SIGNAL_FUNC(unifontsel_configure_area), fs); |
2719 | gtk_widget_set_size_request(fs->preview_area, 1, preview_height); |
2720 | gtk_widget_show(fs->preview_area); |
2721 | ww = fs->preview_area; |
2722 | w = gtk_frame_new(NULL); |
2723 | gtk_container_add(GTK_CONTAINER(w), ww); |
2724 | gtk_widget_show(w); |
2725 | #if GTK_CHECK_VERSION(2,4,0) |
2726 | ww = w; |
2727 | /* GtkAlignment seems to be the simplest way to put padding round things */ |
2728 | w = gtk_alignment_new(0, 0, 1, 1); |
2729 | gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8); |
2730 | gtk_container_add(GTK_CONTAINER(w), ww); |
2731 | gtk_widget_show(w); |
2732 | #endif |
2733 | ww = w; |
2734 | w = gtk_frame_new("Preview of font"); |
2735 | gtk_container_add(GTK_CONTAINER(w), ww); |
2736 | gtk_widget_show(w); |
2737 | gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4, |
2738 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8); |
2739 | |
2740 | i = 0; |
2741 | w = gtk_check_button_new_with_label("Show client-side fonts"); |
2742 | gtk_object_set_data(GTK_OBJECT(w), "user-data", |
2743 | GINT_TO_POINTER(FONTFLAG_CLIENTSIDE)); |
2744 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
2745 | GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs); |
2746 | gtk_widget_show(w); |
2747 | fs->filter_buttons[i++] = w; |
2748 | gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0); |
2749 | w = gtk_check_button_new_with_label("Show server-side fonts"); |
2750 | gtk_object_set_data(GTK_OBJECT(w), "user-data", |
2751 | GINT_TO_POINTER(FONTFLAG_SERVERSIDE)); |
2752 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
2753 | GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs); |
2754 | gtk_widget_show(w); |
2755 | fs->filter_buttons[i++] = w; |
2756 | gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0); |
2757 | w = gtk_check_button_new_with_label("Show server-side font aliases"); |
2758 | gtk_object_set_data(GTK_OBJECT(w), "user-data", |
2759 | GINT_TO_POINTER(FONTFLAG_SERVERALIAS)); |
2760 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
2761 | GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs); |
2762 | gtk_widget_show(w); |
2763 | fs->filter_buttons[i++] = w; |
2764 | gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0); |
2765 | w = gtk_check_button_new_with_label("Show non-monospaced fonts"); |
2766 | gtk_object_set_data(GTK_OBJECT(w), "user-data", |
2767 | GINT_TO_POINTER(FONTFLAG_NONMONOSPACED)); |
2768 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
2769 | GTK_SIGNAL_FUNC(unifontsel_button_toggled), fs); |
2770 | gtk_widget_show(w); |
2771 | fs->filter_buttons[i++] = w; |
2772 | gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0); |
2773 | |
2774 | assert(i == lenof(fs->filter_buttons)); |
2775 | fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE | |
2776 | FONTFLAG_SERVERALIAS; |
2777 | unifontsel_set_filter_buttons(fs); |
2778 | |
2779 | /* |
2780 | * Go and find all the font names, and set up our master font |
2781 | * list. |
2782 | */ |
2783 | fs->fonts_by_realname = newtree234(fontinfo_realname_compare); |
2784 | fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare); |
2785 | for (i = 0; i < lenof(unifont_types); i++) |
2786 | unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window), |
2787 | unifontsel_add_entry, fs); |
2788 | |
2789 | /* |
2790 | * And set up the initial font names list. |
2791 | */ |
2792 | unifontsel_setup_familylist(fs); |
2793 | |
2794 | fs->selsize = fs->intendedsize = 13; /* random default */ |
2795 | gtk_widget_set_sensitive(fs->u.ok_button, FALSE); |
2796 | |
2797 | return (unifontsel *)fs; |
2798 | } |
2799 | |
2800 | void unifontsel_destroy(unifontsel *fontsel) |
2801 | { |
2802 | unifontsel_internal *fs = (unifontsel_internal *)fontsel; |
2803 | fontinfo *info; |
2804 | |
2805 | if (fs->preview_pixmap) |
2806 | gdk_pixmap_unref(fs->preview_pixmap); |
2807 | |
2808 | freetree234(fs->fonts_by_selorder); |
2809 | while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL) |
2810 | sfree(info); |
2811 | freetree234(fs->fonts_by_realname); |
2812 | |
2813 | gtk_widget_destroy(GTK_WIDGET(fs->u.window)); |
2814 | sfree(fs); |
2815 | } |
2816 | |
2817 | void unifontsel_set_name(unifontsel *fontsel, const char *fontname) |
2818 | { |
2819 | unifontsel_internal *fs = (unifontsel_internal *)fontsel; |
2820 | int i, start, end, size, flags; |
2821 | const char *fontname2 = NULL; |
2822 | fontinfo *info; |
2823 | |
2824 | /* |
2825 | * Provide a default if given an empty or null font name. |
2826 | */ |
2827 | if (!fontname || !*fontname) |
2828 | fontname = "server:fixed"; |
2829 | |
2830 | /* |
2831 | * Call the canonify_fontname function. |
2832 | */ |
2833 | fontname = unifont_do_prefix(fontname, &start, &end); |
2834 | for (i = start; i < end; i++) { |
2835 | fontname2 = unifont_types[i]->canonify_fontname |
2836 | (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE); |
2837 | if (fontname2) |
2838 | break; |
2839 | } |
2840 | if (i == end) |
2841 | return; /* font name not recognised */ |
2842 | |
2843 | /* |
2844 | * Now look up the canonified font name in our index. |
2845 | */ |
2846 | { |
2847 | struct fontinfo_realname_find f; |
2848 | f.realname = fontname2; |
2849 | f.flags = flags; |
2850 | info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find); |
2851 | } |
2852 | |
2853 | /* |
2854 | * If we've found the font, and its size field is either |
2855 | * correct or zero (the latter indicating a scalable font), |
2856 | * then we're done. Otherwise, try looking up the original |
2857 | * font name instead. |
2858 | */ |
2859 | if (!info || (info->size != size && info->size != 0)) { |
2860 | struct fontinfo_realname_find f; |
2861 | f.realname = fontname; |
2862 | f.flags = flags; |
2863 | |
2864 | info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find); |
2865 | if (!info || info->size != size) |
2866 | return; /* font name not in our index */ |
2867 | } |
2868 | |
2869 | /* |
2870 | * Now we've got a fontinfo structure and a font size, so we |
2871 | * know everything we need to fill in all the fields in the |
2872 | * dialog. |
2873 | */ |
2874 | unifontsel_select_font(fs, info, size, 0, TRUE); |
2875 | } |
2876 | |
2877 | char *unifontsel_get_name(unifontsel *fontsel) |
2878 | { |
2879 | unifontsel_internal *fs = (unifontsel_internal *)fontsel; |
2880 | char *name; |
2881 | |
2882 | if (!fs->selected) |
2883 | return NULL; |
2884 | |
2885 | if (fs->selected->size == 0) { |
2886 | name = fs->selected->fontclass->scale_fontname |
2887 | (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize); |
2888 | if (name) { |
2889 | char *ret = dupcat(fs->selected->fontclass->prefix, ":", |
2890 | name, NULL); |
2891 | sfree(name); |
2892 | return ret; |
2893 | } |
2894 | } |
2895 | |
2896 | return dupcat(fs->selected->fontclass->prefix, ":", |
2897 | fs->selected->realname, NULL); |
2898 | } |
2899 | |
2900 | #endif /* GTK_CHECK_VERSION(2,0,0) */ |