ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.40
Committed: Mon Mar 15 00:08:11 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.39: +6 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*;
2     * File: defaultfont.C
3     *----------------------------------------------------------------------*
4 pcg 1.22 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 pcg 1.1 * - original version.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     *---------------------------------------------------------------------*/
21    
22     #include "../config.h"
23     #include "rxvt.h"
24     #include "defaultfont.h"
25    
26 pcg 1.7 #include <cstdlib>
27    
28 pcg 1.17 #define DISPLAY r->display->display
29 pcg 1.2 #define GC r->TermWin.gc
30 pcg 1.1
31     const struct rxvt_fallback_font {
32     codeset cs;
33     const char *name;
34     } fallback_fonts[] = {
35     { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
36     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
37     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
38    
39     #if ENCODING_EU
40     // cyrillic
41     { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
42     { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
43    
44     { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
45     { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
46     { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
47     { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
48     { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
49     { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
50     { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
51     { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
52     { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53     { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54     { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55     { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56     { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
57     #endif
58    
59     // japanese
60     #if ENCODING_JP || ENCODING_JP_EXT
61     # if XFT
62     // prefer xft for complex scripts
63 pcg 1.9 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
64 pcg 1.1 # endif
65 pcg 1.9 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66     { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67     { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 pcg 1.1 #endif
69    
70     #if ENCODING_CN || ENCODING_CN_EXT
71     # if XFT
72 pcg 1.9 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
73     { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
74     { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
75     { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
76 pcg 1.1 # endif
77 pcg 1.28 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
78     { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
79     { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
80 pcg 1.27 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
81     { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
82     { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
83     { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
84     { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
85     { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
86     { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
87     { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
88     { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
89 pcg 1.1 #endif
90    
91     #if XFT
92 pcg 1.9 { CS_UNICODE, "xft:Andale Mono" },
93     { CS_UNICODE, "xft:Arial Unicode MS" },
94 pcg 1.1 #endif
95 pcg 1.9 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
96     { CS_UNICODE, "xft:FreeMono" },
97     { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
98     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
99     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
100 pcg 1.1
101 pcg 1.30 #if UNICODE_3 && XFT
102     { CS_UNICODE, "xft:Code2001" }, // contains many plane-1 characters
103     #endif
104    
105 pcg 1.1 { CS_UNKNOWN, 0 }
106     };
107    
108     /////////////////////////////////////////////////////////////////////////////
109    
110 pcg 1.26 #if XFT
111 pcg 1.24 rxvt_drawable::~rxvt_drawable ()
112     {
113     if (xftdrawable)
114     XftDrawDestroy (xftdrawable);
115     }
116    
117     rxvt_drawable::operator XftDraw *()
118     {
119     if (!xftdrawable)
120     xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
121    
122     return xftdrawable;
123     }
124 pcg 1.26 #endif
125 pcg 1.24
126     /////////////////////////////////////////////////////////////////////////////
127    
128 pcg 1.1 static void *enc_buf;
129     static uint32_t enc_len;
130    
131     static inline void *
132 pcg 1.16 get_enc_buf (uint32_t len)
133 pcg 1.1 {
134     if (len > enc_len)
135     {
136     free (enc_buf);
137     enc_buf = malloc (len);
138     }
139    
140     return enc_buf;
141     }
142    
143     static const char *
144 pcg 1.16 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
145 pcg 1.1 {
146     uint8_t *buf = (uint8_t *)get_enc_buf (len);
147    
148     while (len--)
149     {
150     uint32_t c = FROM_UNICODE (cs, *text++);
151    
152     if (c == NOCHAR)
153     {
154     c = 0;
155     zero = true;
156     }
157    
158     *buf++ = c;
159     }
160    
161     return (const char *)enc_buf;
162     }
163    
164     static const XChar2b *
165 pcg 1.16 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
166 pcg 1.1 {
167     XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
168    
169     while (len--)
170     {
171     uint32_t c = FROM_UNICODE (cs, *text++);
172    
173     if (c == NOCHAR)
174     {
175     c = 0;
176     zero = true;
177     }
178    
179     buf->byte1 = c >> 8;
180     buf->byte2 = c;
181     buf++;
182     }
183    
184     return (XChar2b *)enc_buf;
185     }
186    
187     /////////////////////////////////////////////////////////////////////////////
188    
189     void
190 pcg 1.24 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
191 pcg 1.1 {
192     if (color == Color_bg)
193 pcg 1.24 XClearArea (d.display->display, d, x, y, w, h, FALSE);
194 pcg 1.1 else if (color >= 0)
195     {
196 pcg 1.24 #if XFT
197     XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
198     #else
199     XSetForeground (d.display->display, GC, r->PixColors[color]);
200     XFillRectangle (d.display->display, d, GC, x, y, w, h);
201     #endif
202 pcg 1.1 }
203     }
204    
205 pcg 1.3 static const char *linedraw_cmds[128] = {
206     "1hH", "2hH", "1vV", "2vV",
207     0, 0, 0, 0,
208     0, 0, 0, 0,
209     "1HV", "2H1V", "1H2V", "2HV",
210    
211     // 2510
212     "1hV", "2h1V", "1h2V", "2hV",
213     "1Hv", "2H1v", "1H2v", "2Hv",
214     "1hv", "2h1v", "1h2v", "2hv",
215     "1HvV", "2H1vV", "1HV2v", "1Hv2V",
216    
217     // 2520
218     "1H2vV", "2Hv1V", "2HV1v", "2HvV",
219     "1hvV", "2h1vV", "1hV2v", "1hv2V",
220     "1h2vV", "2hv1V", "1v2hV", "2hvV",
221     "1hHV", "2h1HV", "2H1hV", "2hH1V",
222    
223     // 2530
224     "1hH2V", "2hV1H", "1h2HV", "2hHV",
225     "1hHv", "1vH2h", "1hv2H", "1v2hH",
226     "1hH2v", "1H2hv", "1h2Hv", "2hHv",
227     "1hHvV", "1vVH2h", "1hvV2H", "1vV2hH",
228    
229     // 2540
230     "1hHV2v", "1hHv2V", "1hH2vV", "1HV2hv",
231     "1hV2Hv", "1Hv2hV", "1hv2HV", "1V2hHv",
232     "1v2hHV", "1H2hvV", "1h2HvV", "2hHvV",
233     0, 0, 0, 0,
234    
235     // 2550
236     0, 0, 0, 0,
237     0, 0, 0, 0,
238     0, 0, 0, 0,
239     0, 0, 0, 0,
240    
241     // 2560
242     0, 0, 0, 0,
243     0, 0, 0, 0,
244     0, 0, 0, 0,
245     0, 0, 0, 0,
246    
247     // 2570
248     0, "1a", "1b", "1ab",
249     "1h", "1v", "1H", "1V",
250     "2h", "2v", "2H", "2V",
251     "1h2H", "1v2V", "1H2h", "1V2v"
252    
253     // to be done
254     };
255    
256 pcg 1.1 struct rxvt_font_default : rxvt_font {
257 pcg 1.7 rxvt_fontprop properties ()
258     {
259     rxvt_fontprop p;
260    
261 pcg 1.10 p.width = p.height = 1;
262 pcg 1.7 p.weight = rxvt_fontprop::medium;
263     p.slant = rxvt_fontprop::roman;
264    
265     return p;
266     }
267    
268     bool load (const rxvt_fontprop &prop)
269 pcg 1.1 {
270     width = 1; height = 1;
271     ascent = 1; descent = 0;
272    
273 pcg 1.38 set_name ("built-in pseudofont");
274    
275 pcg 1.1 return true;
276     }
277    
278 pcg 1.40 bool has_codepoint (unicode_t unicode)
279 pcg 1.1 {
280 pcg 1.3 if (unicode <= 0x001f)
281     return true;
282 pcg 1.34
283 pcg 1.3 if (unicode >= 0x0080 && unicode <= 0x009f)
284     return true;
285    
286     if (unicode >= 0x2500 && unicode <= 0x257f
287     && linedraw_cmds[unicode - 0x2500])
288 pcg 1.1 return true;
289    
290 pcg 1.35 if (IS_COMPOSE (unicode))
291 pcg 1.34 return true;
292    
293 pcg 1.1 switch (unicode)
294     {
295     case ZERO_WIDTH_CHAR:
296     return true;
297     }
298    
299     return false;
300     }
301    
302 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
303 pcg 1.1 const text_t *text, int len,
304     int fg, int bg);
305     };
306    
307     void
308 pcg 1.24 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
309 pcg 1.1 const text_t *text, int len,
310     int fg, int bg)
311     {
312 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
313 pcg 1.1
314 pcg 1.24 XSetForeground (d.display->display, GC, r->PixColors[fg]);
315 pcg 1.3
316 pcg 1.1 while (len--)
317     {
318 pcg 1.39 #if ENABLE_COMBINING
319 pcg 1.36 compose_char *cc;
320 pcg 1.39 #endif
321 pcg 1.3 text_t t = *text++;
322    
323 pcg 1.27 // is it in our linedrawing table?
324 pcg 1.3 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
325 pcg 1.1 {
326 pcg 1.3 const char *p = linedraw_cmds[t - 0x2500];
327    
328 pcg 1.31 int x0 = x, x1 = x + (r->TermWin.fwidth - 1) / 2, x2 = x + r->TermWin.fwidth - 1;
329     int y0 = y, y1 = y + (r->TermWin.fheight - 1) / 2, y2 = y + r->TermWin.fheight - 1;
330 pcg 1.3
331     XGCValues gcv;
332    
333     while (*p)
334     {
335     switch (*p++)
336     {
337     case '1':
338     gcv.line_width = 0;
339 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
340 pcg 1.3 break;
341    
342     case '2':
343     gcv.line_width = 2;
344 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
345 pcg 1.3 break;
346    
347 pcg 1.24 case 'h': XDrawLine (d.display->display, d, GC, x0, y1, x1, y1); break;
348     case 'H': XDrawLine (d.display->display, d, GC, x1, y1, x2, y1); break;
349     case 'v': XDrawLine (d.display->display, d, GC, x1, y0, x1, y1); break;
350     case 'V': XDrawLine (d.display->display, d, GC, x1, y1, x1, y2); break;
351     case 'a': XDrawLine (d.display->display, d, GC, x0, y2, x2, y0); break;
352     case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
353 pcg 1.3 }
354     }
355    
356     gcv.line_width = 0;
357 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
358 pcg 1.1 }
359 pcg 1.39 #if ENABLE_COMBINING
360 pcg 1.36 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
361 pcg 1.34 {
362 pcg 1.37 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
363     f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
364 pcg 1.36 if (cc->c2 != NOCHAR)
365 pcg 1.37 {
366     // prefer font of first character, for no good reasons
367     rxvt_font *f2 = f1->has_codepoint (cc->c2)
368     ? f1
369     : (*fs)[fs->find_font (cc->c2)];
370    
371     f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
372     }
373 pcg 1.34 }
374 pcg 1.39 #endif
375 pcg 1.3 else
376 pcg 1.27 switch (t)
377 pcg 1.3 {
378     case ZERO_WIDTH_CHAR:
379     break;
380     default:
381 pcg 1.27 int w = 0;
382     while (len > 0 && *text == NOCHAR)
383     {
384     ++text;
385     --len;
386     w += r->TermWin.fwidth;
387     }
388    
389     XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
390     w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
391     x += w;
392 pcg 1.3 }
393 pcg 1.1
394 pcg 1.2 x += r->TermWin.fwidth;
395 pcg 1.1 }
396     }
397    
398     /////////////////////////////////////////////////////////////////////////////
399    
400     struct rxvt_font_x11 : rxvt_font {
401     rxvt_font_x11 () { f = 0; }
402    
403     void clear ();
404    
405 pcg 1.7 rxvt_fontprop properties ();
406    
407     bool load (const rxvt_fontprop &prop);
408 pcg 1.1
409 pcg 1.40 bool has_codepoint (unicode_t unicode);
410 pcg 1.1
411 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
412 pcg 1.1 const text_t *text, int len,
413     int fg, int bg);
414    
415     XFontStruct *f;
416     codeset cs;
417     bool enc2b, encm;
418    
419 pcg 1.7 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
420 pcg 1.12 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
421     bool set_properties (rxvt_fontprop &p, XFontStruct *f);
422     bool set_properties (rxvt_fontprop &p, const char *name);
423 pcg 1.1 };
424    
425     const char *
426 pcg 1.7 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
427 pcg 1.1 {
428     unsigned long value;
429    
430     if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
431     return XGetAtomName (DISPLAY, value);
432     else
433     return repl;
434     }
435    
436 pcg 1.7 rxvt_fontprop
437     rxvt_font_x11::properties ()
438     {
439 pcg 1.12 rxvt_fontprop p;
440     set_properties (p, f);
441     return p;
442 pcg 1.7 }
443    
444 pcg 1.12 bool
445     rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
446 pcg 1.7 {
447 pcg 1.12 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
448     p.height = height;
449     p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
450     p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
451    
452     return true;
453     }
454 pcg 1.7
455 pcg 1.12 bool
456     rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
457     {
458 pcg 1.7 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
459     const char *slant = get_property (f, "SLANT", "r");
460    
461 pcg 1.12 unsigned long height;
462     if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
463     return false;
464    
465 pcg 1.10 unsigned long avgwidth;
466 pcg 1.12 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
467     avgwidth = 0;
468    
469     return set_properties (p, height, weight, slant, avgwidth);
470     }
471    
472     bool
473     rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
474     {
475     int slashes = 0;
476 pcg 1.29 const char *comp[13];
477 pcg 1.12
478     for (const char *c = name; *c; c++)
479     if (*c == '-')
480     {
481     comp[slashes++] = c + 1;
482     if (slashes >= 13)
483     break;
484     }
485    
486     /* can we short-circuit the costly XLoadQueryFont? */
487     if (slashes >= 13
488     && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
489     && (*comp[11] >= '0' && *comp[11] <= '9'))
490     return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
491    
492     XFontStruct *f = XLoadQueryFont (DISPLAY, name);
493    
494     if (f)
495     {
496 pcg 1.19 // the font should really exist now. if not, we have a problem
497 pcg 1.12 // (e.g. if the user did xset fp rehash just when we were searching fonts).
498     // in that case, just return garbage.
499     bool ret = set_properties (p, f);
500     XFreeFont (DISPLAY, f);
501     return ret;
502     }
503     else
504     return false;
505     }
506    
507     // fix the size of scalable fonts
508     static void
509     fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
510     {
511     int slashes = 0;
512     const char *size;
513    
514     for (const char *c = name; *c; c++)
515     if (*c == '-')
516     {
517     if (slashes == 6)
518     size = c + 1;
519    
520     if (++slashes >= 13)
521     break;
522     }
523 pcg 1.7
524 pcg 1.12 if (slashes >= 13 && size[0] == '0')
525     {
526     strncpy (buf, name, size - name);
527     buf += size - name;
528     buf += sprintf (buf, "%d", prop.height);
529     strcpy (buf, size + 1);
530     }
531     else
532     strcpy (buf, name);
533 pcg 1.7 }
534    
535 pcg 1.1 bool
536 pcg 1.7 rxvt_font_x11::load (const rxvt_fontprop &prop)
537 pcg 1.1 {
538     clear ();
539    
540 pcg 1.6 char **list;
541     int count;
542 pcg 1.12 list = XListFonts (DISPLAY, name, 512, &count);
543     set_name (0);
544 pcg 1.6
545     if (!list)
546     return false;
547    
548 pcg 1.7 int bestdiff = 0x7fffffff;
549 pcg 1.6 for (int i = 0; i < count; i++)
550     {
551 pcg 1.12 rxvt_fontprop p;
552     char fname[1024];
553     fix_scalable (fname, list[i], prop);
554    
555     if (!set_properties (p, fname))
556     continue;
557    
558     if (p.height > prop.height) // weed out too large fonts
559     continue;
560    
561     int diff = (prop.height - p.height) * 32
562     + abs (prop.weight - p.weight)
563     + abs (prop.slant - p.slant );
564 pcg 1.7
565 pcg 1.12 if (!name // compare against best found so far
566     || diff < bestdiff)
567 pcg 1.7 {
568 pcg 1.12 set_name (strdup (fname));
569     bestdiff = diff;
570 pcg 1.7 }
571 pcg 1.6 }
572 pcg 1.9
573 pcg 1.12 XFreeFontNames (list);
574    
575     if (!name)
576 pcg 1.9 return false;
577 pcg 1.6
578 pcg 1.1 f = XLoadQueryFont (DISPLAY, name);
579    
580     if (!f)
581     return false;
582    
583 pcg 1.7 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
584     const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
585 pcg 1.1
586     if (registry && encoding)
587     {
588     char charset[64];
589     snprintf (charset, 64, "%s-%s", registry, encoding);
590    
591     cs = codeset_from_name (charset);
592     }
593     else
594     {
595 pcg 1.7 const char *charset = get_property (f, "FONT", 0);
596 pcg 1.1
597     if (!charset)
598     charset = name;
599    
600     int count = 13;
601     while (*charset)
602     if (*charset++ == '-' && !--count)
603     break;
604    
605     cs = codeset_from_name (charset);
606     }
607    
608     if (cs == CS_UNICODE)
609     cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
610    
611     encm = f->min_byte1 != 0 || f->max_byte1 != 0;
612     enc2b = encm || f->max_char_or_byte2 > 255;
613    
614     ascent = f->ascent;
615     descent = f->descent;
616     height = ascent + descent;
617    
618 pcg 1.7 slow = false;
619 pcg 1.1
620     if (f->min_bounds.width == f->max_bounds.width)
621     width = f->min_bounds.width;
622     else if (f->per_char == NULL)
623     width = f->max_bounds.width;
624     else
625     {
626 pcg 1.7 slow = true;
627 pcg 1.1
628     int N = f->max_char_or_byte2 - f->min_char_or_byte2;
629    
630     if (encm)
631     N += (f->max_byte1 - f->min_byte1)
632     * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
633    
634     while (N)
635     {
636     if (f->per_char[N].width > width)
637     width = f->per_char[N].width;
638    
639     --N;
640     }
641     }
642    
643     if (cs == CS_UNKNOWN)
644     {
645     fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
646    
647     clear ();
648    
649     return false;
650     }
651    
652     return true;
653     }
654    
655     void
656     rxvt_font_x11::clear ()
657     {
658     if (f)
659     {
660     XFreeFont (DISPLAY, f);
661     f = 0;
662     }
663     }
664    
665     bool
666 pcg 1.40 rxvt_font_x11::has_codepoint (unicode_t unicode)
667 pcg 1.1 {
668     uint32_t ch = FROM_UNICODE (cs, unicode);
669    
670     if (ch == NOCHAR)
671     return false;
672    
673     /* check wether the character exists in _this_ font. horrible. */
674     XCharStruct *xcs;
675    
676     if (encm)
677     {
678 pcg 1.16 unsigned char byte1 = ch >> 8;
679     unsigned char byte2 = ch & 255;
680 pcg 1.1
681     if (byte1 < f->min_byte1 || byte1 > f->max_byte1
682     || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
683     return false;
684    
685     if (!f->per_char)
686     return true;
687    
688     int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
689     int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
690    
691     xcs = f->per_char + N;
692     }
693     else
694     {
695     if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
696     return false;
697    
698     if (!f->per_char)
699     return true;
700    
701     xcs = f->per_char + (ch - f->min_char_or_byte2);
702     }
703    
704     if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
705     && xcs->ascent == 0 && xcs->descent == 0)
706     return false;
707    
708     return true;
709     }
710    
711     void
712 pcg 1.24 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
713 pcg 1.1 const text_t *text, int len,
714     int fg, int bg)
715     {
716     // this looks like a mess /.
717     // and it is a mess /.
718     // yet we are trying to be perfect /.
719     // but the result still isn't perfect /.
720    
721 pcg 1.7 bool slow = this->slow
722 pcg 1.2 || width != r->TermWin.fwidth
723     || height != r->TermWin.fheight;
724 pcg 1.1
725 pcg 1.2 int base = r->TermWin.fbase;
726 pcg 1.1
727     XGCValues v;
728 pcg 1.2 v.foreground = r->PixColors[fg];
729     v.background = r->PixColors[bg];
730 pcg 1.1 v.font = f->fid;
731    
732     if (enc2b)
733     {
734     const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
735    
736     if (bg == Color_bg && !slow)
737     {
738 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
739     XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
740 pcg 1.1 }
741     else
742     {
743 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
744 pcg 1.1
745 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
746 pcg 1.1
747     if (slow)
748     {
749     do
750     {
751     if (xc->byte1 || xc->byte2)
752 pcg 1.24 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
753 pcg 1.1
754 pcg 1.2 x += r->TermWin.fwidth;
755 pcg 1.1 xc++; len--;
756     }
757     while (len);
758     }
759     else
760 pcg 1.24 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
761 pcg 1.1 }
762     }
763     else
764     {
765     const char *xc = enc_char (text, len, cs, slow);
766    
767     if (bg == Color_bg && !slow)
768     {
769 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
770     XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
771 pcg 1.1 }
772     else
773     {
774 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
775 pcg 1.1
776 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
777 pcg 1.1
778     if (slow)
779     {
780     do
781     {
782     if (*xc)
783 pcg 1.24 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
784 pcg 1.1
785 pcg 1.2 x += r->TermWin.fwidth;
786 pcg 1.1 xc++; len--;
787     }
788     while (len);
789     }
790     else
791 pcg 1.24 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
792 pcg 1.1 }
793     }
794     }
795    
796     /////////////////////////////////////////////////////////////////////////////
797    
798     #if XFT
799     #if 0
800     #define UNIBITS 21
801     //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
802     #define SWATHBITS 8
803     #endif
804    
805     struct rxvt_font_xft : rxvt_font {
806 pcg 1.24 rxvt_font_xft () { f = 0; }
807 pcg 1.1
808     void clear ();
809    
810 pcg 1.7 rxvt_fontprop properties ();
811    
812     bool load (const rxvt_fontprop &prop);
813 pcg 1.1
814 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
815 pcg 1.1 const text_t *text, int len,
816     int fg, int bg);
817    
818 pcg 1.40 bool has_codepoint (unicode_t unicode);
819 pcg 1.1
820     protected:
821     XftFont *f;
822     };
823    
824     void
825     rxvt_font_xft::clear ()
826     {
827     if (f)
828     {
829 pcg 1.2 XftFontClose (DISPLAY, f);
830 pcg 1.1 f = 0;
831     }
832     }
833    
834 pcg 1.7 rxvt_fontprop
835     rxvt_font_xft::properties ()
836     {
837     rxvt_fontprop p;
838    
839     FT_Face face = XftLockFace (f);
840    
841 pcg 1.10 p.width = width; p.height = height;
842 pcg 1.7 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
843     p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
844    
845     XftUnlockFace (f);
846    
847     return p;
848     }
849    
850 pcg 1.1 bool
851 pcg 1.7 rxvt_font_xft::load (const rxvt_fontprop &prop)
852 pcg 1.1 {
853     #if 0
854     for (int i = 0; i < SWATHCOUNT; i++)
855     cvr[i] = 0;
856     #endif
857    
858     clear ();
859    
860 pcg 1.7 FcPattern *p = FcNameParse ((FcChar8 *) name);
861    
862     if (!p)
863     return false;
864    
865     FcValue v;
866    
867     if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
868     FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
869    
870     if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
871     FcPatternAddInteger (p, FC_SLANT, prop.slant);
872    
873 pcg 1.11 #if 0 // clipping unfortunately destroys our precious double-width-characters
874 pcg 1.10 // clip width, we can't do better, or can we?
875     if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
876     FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
877 pcg 1.11 #endif
878 pcg 1.10
879 pcg 1.7 //FcPatternAddBool (p, FC_MINSPACE, 1);
880    
881     XftResult result;
882     FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
883    
884     FcPatternDestroy (p);
885    
886     if (!match)
887     return false;
888    
889     f = XftFontOpenPattern (DISPLAY, match);
890    
891 pcg 1.1 if (!f)
892 pcg 1.8 {
893     FcPatternDestroy (match);
894     return false;
895     }
896 pcg 1.1
897     FT_Face face = XftLockFace (f);
898    
899 pcg 1.7 slow = !FT_IS_FIXED_WIDTH (face);
900 pcg 1.1
901     int ftheight = 0;
902    
903     for (;;)
904     {
905     XGlyphInfo g1, g2;
906     FcChar8 c;
907    
908 pcg 1.2 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
909     c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
910 pcg 1.1
911 pcg 1.7 if (g1.xOff != g2.xOff) // don't simply trust the font
912     slow = true;
913 pcg 1.1
914     width = g2.xOff;
915     ascent = (face->size->metrics.ascender + 63) >> 6;
916     descent = (-face->size->metrics.descender + 63) >> 6;
917     height = ascent + descent;
918    
919 pcg 1.7 if (height <= prop.height || !prop.height)
920 pcg 1.1 break;
921    
922     if (ftheight)
923     {
924     // take smaller steps near the end
925 pcg 1.7 if (height > prop.height + 1) ftheight++;
926     if (height > prop.height + 2) ftheight++;
927     if (height > prop.height + 3) ftheight++;
928 pcg 1.1
929 pcg 1.7 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
930 pcg 1.1 }
931     else
932 pcg 1.7 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
933 pcg 1.1 }
934    
935     XftUnlockFace (f);
936    
937     return true;
938     }
939    
940     bool
941 pcg 1.40 rxvt_font_xft::has_codepoint (unicode_t unicode)
942 pcg 1.1 {
943 pcg 1.2 return XftCharExists (DISPLAY, f, unicode);
944 pcg 1.1 }
945    
946     void
947 pcg 1.24 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
948 pcg 1.1 const text_t *text, int len,
949     int fg, int bg)
950     {
951 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
952 pcg 1.1
953 pcg 1.12 if (!slow && width == r->TermWin.fwidth && 0)
954 pcg 1.1 {
955     if (sizeof (text_t) == sizeof (FcChar16))
956 pcg 1.2 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
957 pcg 1.1 else
958 pcg 1.2 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
959 pcg 1.1 }
960     else
961     {
962     while (len)
963     {
964     if (*text != NOCHAR && *text != ' ')
965     {
966 pcg 1.13 int fwidth = r->TermWin.fwidth;
967     if (len >= 2 && text[1] == NOCHAR)
968     fwidth *= 2;
969    
970 pcg 1.12 XGlyphInfo extents;
971 pcg 1.1 if (sizeof (text_t) == sizeof (FcChar16))
972 pcg 1.12 {
973 pcg 1.24 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
974 pcg 1.13 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
975 pcg 1.12 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
976     }
977 pcg 1.1 else
978 pcg 1.12 {
979     XGlyphInfo extents;
980 pcg 1.24 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
981 pcg 1.13 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
982 pcg 1.12 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
983     }
984 pcg 1.1 }
985    
986 pcg 1.2 x += r->TermWin.fwidth;
987 pcg 1.1 text++;
988     len--;
989     }
990     }
991     }
992     #endif
993    
994     /////////////////////////////////////////////////////////////////////////////
995    
996 pcg 1.2 rxvt_fontset::rxvt_fontset (rxvt_t r)
997 pcg 1.18 : r (r)
998 pcg 1.1 {
999     clear ();
1000     }
1001    
1002     rxvt_fontset::~rxvt_fontset ()
1003     {
1004     clear ();
1005     }
1006    
1007     void
1008     rxvt_fontset::clear ()
1009     {
1010 pcg 1.18 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1011 pcg 1.1 FONT_UNREF (*i);
1012    
1013     fonts.clear ();
1014     base_id = 0;
1015 pcg 1.7 base_prop.height = 0x7fffffff;
1016     base_prop.weight = rxvt_fontprop::medium;
1017     base_prop.slant = rxvt_fontprop::roman;
1018 pcg 1.1
1019     fallback = fallback_fonts;
1020     }
1021    
1022     rxvt_font *
1023     rxvt_fontset::new_font (const char *name, codeset cs)
1024     {
1025     rxvt_font *f;
1026    
1027     if (!name || !*name)
1028     {
1029     name = "";
1030     f = new rxvt_font_default;
1031     }
1032     #if XFT
1033     else if (!strncmp (name, "xft:", 4))
1034     {
1035     name += 4;
1036     f = new rxvt_font_xft;
1037     }
1038     #endif
1039     else if (!strncmp (name, "x:", 2))
1040     {
1041     name += 2;
1042     f = new rxvt_font_x11;
1043     }
1044     else
1045     f = new rxvt_font_x11;
1046    
1047 pcg 1.36 f->fs = this;
1048 pcg 1.2 f->set_term (r);
1049 pcg 1.1 f->set_name (strdup (name));
1050    
1051     f->cs = cs;
1052     f->loaded = false;
1053    
1054     return f;
1055     }
1056    
1057     /////////////////////////////////////////////////////////////////////////////
1058    
1059     void
1060     rxvt_fontset::add_fonts (const char *desc)
1061     {
1062     if (desc)
1063     {
1064     char buf[512];
1065     const char *end;
1066    
1067     do
1068     {
1069     while (*desc <= ' ') desc++;
1070    
1071     if (*desc == '[')
1072     {
1073     fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
1074    
1075 pcg 1.16 //const char *extra = desc++; // not yet used
1076 pcg 1.1
1077     desc = strchr (desc, ']');
1078    
1079     if (!desc)
1080     {
1081     fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
1082     break;
1083     }
1084    
1085     desc++;
1086     while (*desc <= ' ') desc++;
1087     }
1088    
1089     end = strchr (desc, ',');
1090     if (!end)
1091     end = desc + strlen (desc);
1092    
1093     if (end - desc < 511)
1094     {
1095     strncpy (buf, desc, end - desc);
1096     buf[end - desc] = 0;
1097    
1098     fonts.push_back (new_font (buf, CS_UNICODE));
1099     }
1100    
1101     desc = end + 1;
1102     }
1103     while (*end);
1104     }
1105     }
1106    
1107     bool
1108     rxvt_fontset::realize_font (int i)
1109     {
1110     if (fonts[i]->loaded)
1111     return true;
1112    
1113 pcg 1.5 fonts[i]->loaded = true;
1114 pcg 1.1
1115 pcg 1.7 if (!fonts[i]->load (base_prop))
1116 pcg 1.5 {
1117     fonts[i]->cs = CS_UNKNOWN;
1118     return false;
1119     }
1120 pcg 1.1
1121 pcg 1.5 return true;
1122 pcg 1.1 }
1123    
1124 pcg 1.23 bool
1125 pcg 1.1 rxvt_fontset::populate (const char *desc)
1126     {
1127     clear ();
1128    
1129     fonts.push_back (new_font (0, CS_UNICODE));
1130     realize_font (0);
1131    
1132     add_fonts (desc);
1133    
1134     if (!base_id)
1135     base_id = 1;
1136    
1137     // we currently need a base-font, no matter what
1138 pcg 1.16 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1139 pcg 1.1 {
1140 pcg 1.18 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1141 pcg 1.1 add_fonts ("fixed");
1142 pcg 1.7 base_id = fonts.size () - 1;
1143 pcg 1.1 }
1144    
1145 pcg 1.16 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1146 pcg 1.23 return false;
1147 pcg 1.1
1148 pcg 1.7 base_prop = fonts[base_id]->properties ();
1149 pcg 1.23
1150     return true;
1151 pcg 1.1 }
1152    
1153     int
1154 pcg 1.40 rxvt_fontset::find_font (unicode_t unicode)
1155 pcg 1.1 {
1156 pcg 1.16 for (unsigned int i = 0; i < fonts.size (); i++)
1157 pcg 1.1 {
1158     rxvt_font *f = fonts[i];
1159    
1160     if (!f->loaded)
1161     {
1162     if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1163     goto next_font;
1164    
1165     if (!realize_font (i))
1166 pcg 1.5 goto next_font;
1167 pcg 1.1 }
1168    
1169 pcg 1.5 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
1170 pcg 1.1 return i;
1171    
1172     next_font:
1173     if (i == fonts.size () - 1 && fallback->name)
1174     {
1175     fonts.push_back (new_font (fallback->name, fallback->cs));
1176     fallback++;
1177 pcg 1.5 i = 0;
1178 pcg 1.1 }
1179     }
1180    
1181     return 0; /* we must return SOME font */
1182     }
1183    
1184    
1185