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