ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.67
Committed: Sat Feb 12 18:55:04 2005 UTC (19 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.66: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*--------------------------------*-C-*---------------------------------*
2 root 1.66 * File: rxvtfont.C
3 root 1.1 *----------------------------------------------------------------------*
4     * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5     * - 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 root 1.41 #include "rxvtutil.h"
25 root 1.1 #include "rxvtfont.h"
26    
27     #include <cstdlib>
28 root 1.41 #include <wchar.h>
29     #include <inttypes.h>
30 root 1.1
31     #define DISPLAY r->display->display
32     #define TGC r->TermWin.gc
33    
34 root 1.41 #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35    
36 root 1.1 const struct rxvt_fallback_font {
37     codeset cs;
38     const char *name;
39     } fallback_fonts[] = {
40     { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
41     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
42     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
43    
44     #if ENCODING_EU
45     // cyrillic
46     { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
47     { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
48    
49     { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
50     { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
51     { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
52     { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
53     { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
54     { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
55     { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
56     { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
57     { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
58     { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
59     { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
60     { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
61     { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
62 root 1.41
63     # if XFT
64     { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
65    
66     { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
67     { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
68     { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
69     { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
70     { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
71     { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
72     { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
73     # endif
74 root 1.1 #endif
75    
76     // japanese
77     #if ENCODING_JP || ENCODING_JP_EXT
78     # if XFT
79     // prefer xft for complex scripts
80 root 1.41 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
81     { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
82     { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
83     { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
84 root 1.1 # endif
85     { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
86     { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
87     { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
88     { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
89     { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
90     { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91     #endif
92    
93 root 1.46 #if ENCODING_ZH || ENCODING_ZH_EXT
94 root 1.1 # if XFT
95 root 1.56 { CS_GBK_0, "xft:AR PL KaitiM GB" },
96     { CS_GBK_0, "xft:AR PL SungtiL GB" },
97     { CS_GBK_0, "xft::spacing=100:lang=zh" },
98 root 1.1 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
99     { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
100     { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
101     { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
102 root 1.41 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
103 root 1.1 # endif
104 root 1.56 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
105 root 1.1 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
106     { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
107     { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
108 root 1.56 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
109 root 1.1 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
110     { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
111     { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
112     { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
113     { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
114     { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
115     { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
116     { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
117     { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
118     #endif
119    
120 root 1.41 #if ENCODING_KR
121     { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
122     { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
123     # if XFT
124     { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
125     { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
126     # endif
127 root 1.1 #endif
128 root 1.41
129     // generic font fallback
130 root 1.1 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
131     { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
132     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
133     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
134     #if XFT
135 root 1.46 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"},
136     { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
137 root 1.41 { CS_UNICODE, "xft:Andale Mono:antialias=false" },
138     { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" },
139    
140 root 1.1 // FreeMono is usually uglier than x fonts, so try last only.
141 root 1.46 { CS_UNICODE, "xft:FreeMono:autohint=true" },
142 root 1.1 #endif
143    
144     { CS_UNKNOWN, 0 }
145     };
146    
147 root 1.41 // these characters are used to guess the font height and width
148 root 1.64 // pango uses a similar algorithm and doesn't trust the font either.
149 root 1.41 static uint16_t extent_test_chars[] = {
150     '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
151     0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
152     0x304c, 0x672c, // が本
153     };
154    
155     #define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
156    
157 root 1.1 /////////////////////////////////////////////////////////////////////////////
158    
159     #if XFT
160     rxvt_drawable::~rxvt_drawable ()
161     {
162     if (xftdrawable)
163     XftDrawDestroy (xftdrawable);
164     }
165    
166     rxvt_drawable::operator XftDraw *()
167     {
168     if (!xftdrawable)
169     xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
170    
171     return xftdrawable;
172     }
173     #endif
174    
175     /////////////////////////////////////////////////////////////////////////////
176    
177     static void *enc_buf;
178     static uint32_t enc_len;
179    
180     static inline void *
181     get_enc_buf (uint32_t len)
182     {
183     if (len > enc_len)
184     {
185     free (enc_buf);
186     enc_buf = malloc (len);
187     enc_len = len;
188     }
189    
190     return enc_buf;
191     }
192    
193     static const char *
194     enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
195     {
196     uint8_t *buf = (uint8_t *)get_enc_buf (len);
197    
198     while (len--)
199     {
200     uint32_t c = FROM_UNICODE (cs, *text++);
201    
202     if (c == NOCHAR)
203     {
204     c = 0;
205     zero = true;
206     }
207    
208     *buf++ = c;
209     }
210    
211     return (const char *)enc_buf;
212     }
213    
214     static const XChar2b *
215     enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
216     {
217     XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
218    
219     while (len--)
220     {
221     uint32_t c = FROM_UNICODE (cs, *text++);
222    
223     if (c == NOCHAR)
224     {
225     c = 0;
226     zero = true;
227     }
228    
229     buf->byte1 = c >> 8;
230     buf->byte2 = c;
231     buf++;
232     }
233    
234     return (XChar2b *)enc_buf;
235     }
236    
237     /////////////////////////////////////////////////////////////////////////////
238    
239     void
240 root 1.41 rxvt_font::set_name (char *name)
241     {
242     if (this->name == name)
243     return;
244    
245     if (this->name) free (this->name); // let the compiler optimize
246     this->name = name;
247     }
248    
249     void
250 root 1.1 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
251     {
252     if (color == Color_bg)
253     XClearArea (d.display->display, d, x, y, w, h, FALSE);
254     else if (color >= 0)
255     {
256     #if XFT
257 root 1.3 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h);
258 root 1.1 #else
259 root 1.3 XSetForeground (d.display->display, TGC, r->pix_colors[color]);
260 root 1.1 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
261     #endif
262     }
263     }
264    
265     #include "table/linedraw.h"
266    
267     struct rxvt_font_default : rxvt_font {
268 root 1.41 struct rxvt_fontset *fs;
269    
270     rxvt_font_default (rxvt_fontset *fs)
271     : rxvt_font ()
272     {
273     this->fs = fs;
274     }
275 root 1.1
276     rxvt_fontprop properties ()
277     {
278     rxvt_fontprop p;
279    
280     p.width = p.height = 1;
281     p.weight = rxvt_fontprop::medium;
282     p.slant = rxvt_fontprop::roman;
283    
284     return p;
285     }
286    
287     bool load (const rxvt_fontprop &prop)
288     {
289     width = 1; height = 1;
290     ascent = 1; descent = 0;
291    
292 root 1.41 set_name (strdup ("built-in support font"));
293 root 1.1
294     return true;
295     }
296    
297 root 1.41 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
298 root 1.1 {
299 root 1.41 careful = false;
300    
301 root 1.1 if (unicode <= 0x001f)
302     return true;
303    
304     if (unicode <= 0x007f)
305     return false;
306    
307     if (unicode <= 0x009f)
308     return true;
309    
310     if (unicode >= 0x2500 && unicode <= 0x259f)
311     return true;
312    
313     if (IS_COMPOSE (unicode))
314     return true;
315    
316     switch (unicode)
317     {
318     case ZERO_WIDTH_CHAR:
319 root 1.41 case NOCHAR:
320 root 1.1 return true;
321     }
322    
323     return false;
324     }
325    
326     void draw (rxvt_drawable &d, int x, int y,
327     const text_t *text, int len,
328     int fg, int bg);
329     };
330    
331     void
332     rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
333     const text_t *text, int len,
334     int fg, int bg)
335     {
336 root 1.66 Display *disp = d.display->display;
337    
338 root 1.1 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
339    
340 root 1.66 XSetForeground (disp, TGC, r->pix_colors[fg]);
341 root 1.1
342 root 1.56 while (len)
343 root 1.1 {
344     #if ENABLE_COMBINING
345     compose_char *cc;
346     #endif
347 root 1.56 const text_t *tp = text;
348     text_t t = *tp;
349    
350     while (++text, --len && *text == NOCHAR)
351     ;
352    
353     int width = text - tp;
354     int fwidth = r->TermWin.fwidth * width;
355 root 1.1
356     if (0x2500 <= t && t <= 0x259f)
357     {
358     uint16_t offs = linedraw_offs[t - 0x2500];
359     uint32_t *a = linedraw_command + (offs >> 4);
360     uint32_t *b = a + (offs & 15);
361    
362 root 1.56 int W = fwidth;
363 root 1.1 int H = r->TermWin.fheight;
364    
365     int x_[16];
366     int y_[16];
367    
368     for (int i = 0; i <= 8; i++)
369     {
370     x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
371     y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
372     }
373    
374     x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
375     y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
376    
377     XGCValues gcv;
378    
379     gcv.cap_style = CapButt;
380     gcv.line_width = 0;
381 root 1.66 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
382 root 1.1
383     while (a < b)
384     {
385     uint32_t command = *a++;
386    
387     int op = (command >> 24) & 255;
388     int a = (command >> 20) & 15;
389     int b = (command >> 16) & 15;
390     int x1 = x_[(command >> 12) & 15];
391     int y1 = y_[(command >> 8) & 15];
392     int x2 = x_[(command >> 4) & 15];
393     int y2 = y_[(command >> 0) & 15];
394    
395     switch (op)
396     {
397     case 0: // line
398 root 1.66 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
399 root 1.1 break;
400    
401     case 1: // rectangle, possibly stippled
402     if (a)
403     {
404     static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
405    
406     gcv.fill_style = FillStippled;
407 root 1.66 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
408 root 1.1 gcv.ts_x_origin = x;
409     gcv.ts_y_origin = y;
410    
411 root 1.66 XChangeGC (disp, TGC,
412 root 1.1 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
413     &gcv);
414     }
415    
416 root 1.66 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
417 root 1.1
418     if (a)
419     {
420 root 1.66 XFreePixmap (disp, gcv.stipple);
421 root 1.1 gcv.stipple = 0;
422     gcv.fill_style = FillSolid;
423 root 1.66 XChangeGC (disp, TGC, GCFillStyle, &gcv);
424 root 1.1 }
425     break;
426     case 2: // arc
427 root 1.66 XDrawArc (disp, d, TGC,
428 root 1.1 x1 - W/2, y1 - H/2, W-1, H-1,
429     (a - 1) * 90*64, (b - 1) * 90*64);
430     break;
431     }
432     }
433     }
434     #if ENABLE_COMBINING
435     else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
436     {
437 root 1.56 text_t chrs[2];
438     width = min (2, width);
439     chrs [1] = NOCHAR;
440    
441     *chrs = cc->c1;
442     rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
443     f1->draw (d, x, y, chrs, width, fg, bg);
444 root 1.46
445 root 1.1 if (cc->c2 != NOCHAR)
446     {
447 root 1.46 bool careful;
448    
449 root 1.1 // prefer font of first character, for no good reasons
450 root 1.56 *chrs = cc->c2;
451     rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
452 root 1.2 ? f1
453 root 1.56 : (*fs)[fs->find_font (cc->c2)];
454 root 1.1
455 root 1.56 f2->draw (d, x, y, chrs, width, fg, -1);
456 root 1.1 }
457     }
458     #endif
459     else
460     switch (t)
461     {
462 root 1.41 case '\t':
463 root 1.1 case ZERO_WIDTH_CHAR:
464 root 1.41 case NOCHAR:
465 root 1.1 break;
466    
467     default:
468 root 1.66 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
469 root 1.56 fwidth - 4, r->TermWin.fheight - 4);
470 root 1.1 }
471    
472 root 1.56 x += fwidth;
473 root 1.1 }
474     }
475    
476     /////////////////////////////////////////////////////////////////////////////
477    
478     struct rxvt_font_x11 : rxvt_font {
479     rxvt_font_x11 () { f = 0; }
480    
481     void clear ();
482    
483     rxvt_fontprop properties ();
484    
485     bool load (const rxvt_fontprop &prop);
486    
487 root 1.41 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
488 root 1.1
489     void draw (rxvt_drawable &d, int x, int y,
490     const text_t *text, int len,
491     int fg, int bg);
492    
493 root 1.41 bool slow; // wether this is a proportional font or has other funny characteristics
494 root 1.1 XFontStruct *f;
495     codeset cs;
496     bool enc2b, encm;
497    
498     char *get_property (XFontStruct *f, const char *property, const char *repl) const;
499     bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
500     bool set_properties (rxvt_fontprop &p, XFontStruct *f);
501     bool set_properties (rxvt_fontprop &p, const char *name);
502     };
503    
504     char *
505     rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
506     {
507     unsigned long value;
508    
509     if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
510     return XGetAtomName (DISPLAY, value);
511     else
512     return rxvt_strdup (repl);
513     }
514    
515     rxvt_fontprop
516     rxvt_font_x11::properties ()
517     {
518     rxvt_fontprop p;
519     set_properties (p, f);
520     return p;
521     }
522    
523     bool
524     rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
525     {
526 root 1.41 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
527 root 1.1 p.height = height;
528     p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
529     p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
530    
531     return true;
532     }
533    
534     bool
535     rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
536     {
537     unsigned long height;
538 root 1.64
539     #if 0
540 root 1.1 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
541     return false;
542 root 1.64 #else
543     height = f->ascent + f->descent;
544     #endif
545 root 1.1
546     unsigned long avgwidth;
547     if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
548     avgwidth = 0;
549    
550     char *weight = get_property (f, "WEIGHT_NAME", "medium");
551     char *slant = get_property (f, "SLANT", "r");
552    
553     set_properties (p, height, weight, slant, avgwidth);
554    
555     free (weight);
556     free (slant);
557    
558     return true;
559     }
560    
561     bool
562     rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
563     {
564     int slashes = 0;
565     const char *comp[13];
566    
567     for (const char *c = name; *c; c++)
568     if (*c == '-')
569     {
570     comp[slashes++] = c + 1;
571     if (slashes >= 13)
572     break;
573     }
574    
575     /* can we short-circuit the costly XLoadQueryFont? */
576     if (slashes >= 13
577     && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
578     && (*comp[11] >= '0' && *comp[11] <= '9'))
579     return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
580    
581     XFontStruct *f = XLoadQueryFont (DISPLAY, name);
582    
583     if (f)
584     {
585     // the font should really exist now. if not, we have a problem
586     // (e.g. if the user did xset fp rehash just when we were searching fonts).
587     // in that case, just return garbage.
588     bool ret = set_properties (p, f);
589     XFreeFont (DISPLAY, f);
590     return ret;
591     }
592     else
593     return false;
594     }
595    
596     // fix the size of scalable fonts
597 root 1.41 static bool
598     replace_field (char *buf, const char *name, int index, const char old, const char *replace)
599 root 1.1 {
600     int slashes = 0;
601 root 1.41 const char *field, *end;
602 root 1.1
603     for (const char *c = name; *c; c++)
604     if (*c == '-')
605     {
606 root 1.41 if (slashes == index)
607     field = c + 1;
608    
609     if (slashes == index + 1)
610     end = c;
611 root 1.1
612     if (++slashes >= 13)
613     break;
614     }
615    
616 root 1.41 if (slashes >= 13 && (!old || *field == old))
617 root 1.1 {
618 root 1.41 // TODO: check for overflow in font-name
619     strncpy (buf, name, field - name);
620     buf += field - name;
621     strcpy (buf, replace);
622     strcat (buf, end);
623    
624     return true;
625 root 1.1 }
626     else
627 root 1.41 {
628     strcpy (buf, name);
629    
630     return false;
631     }
632 root 1.1 }
633    
634     bool
635     rxvt_font_x11::load (const rxvt_fontprop &prop)
636     {
637 root 1.66 Display *disp = DISPLAY;
638    
639 root 1.1 clear ();
640    
641 root 1.41 char field_str[64]; // enough for 128 bits
642    
643     // first morph the font if required
644     if (prop.weight != rxvt_fontprop::unset
645     || prop.slant != rxvt_fontprop::unset)
646     {
647     char fname[1024];
648    
649     if (name[0] != '-')
650     {
651 root 1.66 f = XLoadQueryFont (disp, name);
652 root 1.41
653     if (!f)
654     return false;
655    
656     char *new_name = get_property (f, "FONT", name);
657    
658     if (new_name)
659     set_name (new_name);
660     else
661     rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
662    
663 root 1.66 XFreeFont (disp, f);
664 root 1.41 f = 0;
665     }
666    
667     if (prop.weight != rxvt_fontprop::unset)
668     {
669     replace_field (fname, name, 2, 0,
670     prop.weight < rxvt_fontprop::bold
671     ? "medium" : "bold");
672     set_name (strdup (fname));
673     }
674    
675     if (prop.slant != rxvt_fontprop::unset)
676     {
677     replace_field (fname, name, 3, 0,
678     prop.slant < rxvt_fontprop::italic
679     ? "r" : "i"); // TODO: handle "o"blique, too
680     set_name (strdup (fname));
681     }
682     }
683    
684 root 1.64 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
685     ? 0 : prop.height);
686    
687     struct font_weight {
688     char *name;
689     int diff;
690    
691     void clear ()
692     {
693     name = 0;
694     diff = 0x7fffffff;
695     }
696    
697     font_weight () { clear (); }
698     ~font_weight () { free (name); }
699     };
700    
701 root 1.1 char **list;
702     int count;
703 root 1.66 list = XListFonts (disp, name, 4000, &count);
704 root 1.41
705 root 1.1 set_name (0);
706    
707     if (!list)
708     return false;
709    
710 root 1.64 font_weight *fonts = new font_weight[count];
711 root 1.41
712 root 1.1 for (int i = 0; i < count; i++)
713     {
714     rxvt_fontprop p;
715     char fname[1024];
716 root 1.41
717     int diff = 0;
718 root 1.48
719 root 1.41 if (replace_field (fname, list[i], 6, '0', field_str))
720     diff += 10; // slightly penalize scalable fonts
721 root 1.1
722     if (!set_properties (p, fname))
723     continue;
724    
725 root 1.41 if (prop.height != rxvt_fontprop::unset
726     && p.height > prop.height) // weed out too large fonts
727 root 1.1 continue;
728    
729 root 1.41 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
730     if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
731     if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
732     //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
733 root 1.1
734 root 1.64 fonts[i].name = strdup (fname);
735     fonts[i].diff = diff;
736 root 1.1 }
737    
738     XFreeFontNames (list);
739    
740 root 1.64 // this loop only iterates when the guessed font-size is too small
741     for (;;)
742     {
743     font_weight *best = fonts + count - 1;
744    
745     for (font_weight *w = best; w-- > fonts; )
746     if (w->diff < best->diff)
747     best = w;
748    
749     if (!best->name
750 root 1.66 || !(f = XLoadQueryFont (disp, best->name)))
751 root 1.64 break;
752    
753     set_name (best->name);
754     best->clear ();
755    
756     ascent = f->ascent;
757     descent = f->descent;
758     height = ascent + descent;
759    
760     if (prop.height == rxvt_fontprop::unset
761     || height <= prop.height)
762     break; // font is ready for use
763    
764     // PIXEL_SIZE small enough, but real height too large
765     clear ();
766     }
767 root 1.1
768 root 1.64 delete [] fonts;
769 root 1.1
770     if (!f)
771     return false;
772    
773     char *registry = get_property (f, "CHARSET_REGISTRY", 0);
774     char *encoding = get_property (f, "CHARSET_ENCODING", 0);
775    
776     if (registry && encoding)
777     {
778     char charset[64];
779     snprintf (charset, 64, "%s-%s", registry, encoding);
780    
781     cs = codeset_from_name (charset);
782     }
783     else
784     {
785     const char *charset = get_property (f, "FONT", 0);
786    
787     if (!charset)
788     charset = name;
789    
790     int count = 13;
791     while (*charset)
792     if (*charset++ == '-' && !--count)
793     break;
794    
795     cs = codeset_from_name (charset);
796     }
797    
798     free (registry);
799     free (encoding);
800    
801     if (cs == CS_UNICODE)
802     cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
803    
804     encm = f->min_byte1 != 0 || f->max_byte1 != 0;
805     enc2b = encm || f->max_char_or_byte2 > 255;
806    
807     slow = false;
808    
809 root 1.41 #if 1 // only used for slow detection, TODO optimize
810 root 1.1 if (f->min_bounds.width == f->max_bounds.width)
811     width = f->min_bounds.width;
812     else if (f->per_char == NULL)
813     width = f->max_bounds.width;
814     else
815     {
816     slow = true;
817    
818     int N = f->max_char_or_byte2 - f->min_char_or_byte2;
819    
820     if (encm)
821     N += (f->max_byte1 - f->min_byte1)
822     * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
823    
824     while (N)
825     {
826     if (f->per_char[N].width > width)
827     width = f->per_char[N].width;
828    
829     --N;
830     }
831     }
832 root 1.41 #endif
833    
834     width = 1;
835    
836     for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
837     {
838     if (cs != CS_UNICODE
839     && *t > 0x100
840     && FROM_UNICODE (cs, *t) == NOCHAR)
841     continue;
842    
843     // ignore characters we wouldn't use anyways
844     bool careful;
845     if (!has_char (*t, &prop, careful))
846     continue;
847    
848     XChar2b ch = { *t >> 8, *t };
849    
850     XCharStruct g;
851     int dir_ret, asc_ret, des_ret;
852     XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
853    
854     int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw;
855    
856     if (width < g.width) width = g.width;
857     }
858 root 1.1
859     if (cs == CS_UNKNOWN)
860     {
861     fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
862    
863     clear ();
864 root 1.41 return false;
865     }
866 root 1.1
867 root 1.41 #if 0 // do it per-character
868     if (prop && width > prop->width)
869     {
870     clear ();
871 root 1.1 return false;
872     }
873 root 1.41 #endif
874 root 1.1
875     return true;
876     }
877    
878     void
879     rxvt_font_x11::clear ()
880     {
881     if (f)
882     {
883     XFreeFont (DISPLAY, f);
884     f = 0;
885     }
886     }
887    
888     bool
889 root 1.41 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
890 root 1.1 {
891     uint32_t ch = FROM_UNICODE (cs, unicode);
892    
893     if (ch == NOCHAR)
894     return false;
895    
896     /* check wether the character exists in _this_ font. horrible. */
897     XCharStruct *xcs;
898    
899     if (encm)
900     {
901     unsigned char byte1 = ch >> 8;
902     unsigned char byte2 = ch & 255;
903    
904     if (byte1 < f->min_byte1 || byte1 > f->max_byte1
905     || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
906     return false;
907    
908 root 1.41 if (f->per_char)
909     {
910     int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
911     int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
912 root 1.1
913 root 1.41 xcs = f->per_char + N;
914     }
915     else
916     xcs = &f->max_bounds;
917 root 1.1 }
918     else
919     {
920     if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
921     return false;
922    
923 root 1.41 if (f->per_char)
924     xcs = f->per_char + (ch - f->min_char_or_byte2);
925     else
926     xcs = &f->max_bounds;
927 root 1.1 }
928    
929     if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
930     && xcs->ascent == 0 && xcs->descent == 0)
931     return false;
932    
933 root 1.41 if (!prop || prop->width == rxvt_fontprop::unset)
934     return true;
935    
936     // check character against base font bounding box
937     int w = xcs->width;
938     int wcw = wcwidth (unicode);
939     if (wcw > 0) w /= wcw;
940    
941     careful = w > prop->width;
942     if (careful && w > prop->width * MAX_OVERLAP >> 2)
943     return false;
944    
945 root 1.1 return true;
946     }
947    
948     void
949     rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
950     const text_t *text, int len,
951     int fg, int bg)
952     {
953     // this looks like a mess /.
954     // and it is a mess /.
955     // yet we are trying to be perfect /.
956     // but the result still isn't perfect /.
957    
958     bool slow = this->slow
959     || width != r->TermWin.fwidth
960     || height != r->TermWin.fheight;
961    
962 root 1.41 int base = ascent; // sorry, incorrect: r->TermWin.fbase;
963 root 1.1
964     XGCValues v;
965 root 1.3 v.foreground = r->pix_colors[fg];
966 root 1.1 v.font = f->fid;
967    
968     if (enc2b)
969     {
970     const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
971    
972     if (bg == Color_bg && !slow)
973     {
974 root 1.3 v.background = r->pix_colors[bg];
975 root 1.1 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
976     XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
977     }
978     else
979     {
980     clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
981    
982     XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
983    
984     if (slow)
985     {
986     do
987     {
988     if (xc->byte1 || xc->byte2)
989     XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
990    
991     x += r->TermWin.fwidth;
992     xc++; len--;
993     }
994     while (len);
995     }
996     else
997     XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
998     }
999     }
1000     else
1001     {
1002     const char *xc = enc_char (text, len, cs, slow);
1003    
1004     if (bg == Color_bg && !slow)
1005     {
1006 root 1.3 v.background = r->pix_colors[bg];
1007 root 1.1 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1008     XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1009     }
1010     else
1011     {
1012     clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
1013    
1014     XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1015    
1016     if (slow)
1017     {
1018     do
1019     {
1020     if (*xc)
1021     XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1022    
1023     x += r->TermWin.fwidth;
1024     xc++; len--;
1025     }
1026     while (len);
1027     }
1028     else
1029     XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
1030     }
1031     }
1032     }
1033    
1034     /////////////////////////////////////////////////////////////////////////////
1035    
1036     #if XFT
1037    
1038     struct rxvt_font_xft : rxvt_font {
1039     rxvt_font_xft () { f = 0; }
1040    
1041     void clear ();
1042    
1043     rxvt_fontprop properties ();
1044    
1045     bool load (const rxvt_fontprop &prop);
1046    
1047     void draw (rxvt_drawable &d, int x, int y,
1048     const text_t *text, int len,
1049     int fg, int bg);
1050    
1051 root 1.41 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
1052 root 1.1
1053     protected:
1054     XftFont *f;
1055     };
1056    
1057     void
1058     rxvt_font_xft::clear ()
1059     {
1060     if (f)
1061     {
1062     XftFontClose (DISPLAY, f);
1063     f = 0;
1064     }
1065     }
1066    
1067     rxvt_fontprop
1068     rxvt_font_xft::properties ()
1069     {
1070     rxvt_fontprop p;
1071    
1072     FT_Face face = XftLockFace (f);
1073    
1074     p.width = width;
1075     p.height = height;
1076     p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1077     ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1078     p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1079     ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1080    
1081     XftUnlockFace (f);
1082    
1083     return p;
1084     }
1085    
1086     bool
1087     rxvt_font_xft::load (const rxvt_fontprop &prop)
1088     {
1089 root 1.66 Display *disp = DISPLAY;
1090    
1091 root 1.1 clear ();
1092    
1093     FcPattern *p = FcNameParse ((FcChar8 *) name);
1094    
1095     if (!p)
1096     return false;
1097    
1098     FcValue v;
1099    
1100 root 1.41 if (prop.height != rxvt_fontprop::unset
1101 root 1.50 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1102     && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1103 root 1.1 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1104    
1105 root 1.41 if (prop.weight != rxvt_fontprop::unset
1106     && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1107 root 1.1 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1108    
1109 root 1.41 if (prop.slant != rxvt_fontprop::unset
1110     && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1111 root 1.1 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1112    
1113     #if 0 // clipping unfortunately destroys our precious double-width-characters
1114     // clip width, we can't do better, or can we?
1115     if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1116 root 1.41 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1117 root 1.1 #endif
1118    
1119 root 1.41 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1120     FcPatternAddBool (p, FC_MINSPACE, 1);
1121    
1122     // store generated name so iso14755 view gives better results
1123     set_name ((char *)FcNameUnparse (p));
1124    
1125 root 1.1 XftResult result;
1126 root 1.66 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1127 root 1.1
1128     FcPatternDestroy (p);
1129    
1130     if (!match)
1131     return false;
1132    
1133 root 1.41 int ftheight = 0;
1134     bool success = true;
1135 root 1.1
1136 root 1.41 for (;;)
1137 root 1.1 {
1138 root 1.66 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1139 root 1.41
1140     if (!f)
1141     {
1142     success = false;
1143     break;
1144     }
1145    
1146     FT_Face face = XftLockFace (f);
1147    
1148     ascent = (face->size->metrics.ascender + 63) >> 6;
1149     descent = (-face->size->metrics.descender + 63) >> 6;
1150     height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1151     width = 0;
1152 root 1.1
1153 root 1.41 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1154 root 1.1
1155 root 1.41 XftUnlockFace (f);
1156 root 1.1
1157 root 1.41 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1158     {
1159     FcChar16 ch = *t;
1160 root 1.1
1161 root 1.41 if (cs != CS_UNICODE
1162     && ch > 0x100
1163     && FROM_UNICODE (cs, ch) == NOCHAR)
1164     continue;
1165    
1166     // ignore characters we wouldn't use anyways
1167     bool careful;
1168     if (!has_char (*t, &prop, careful))
1169     continue;
1170 root 1.1
1171 root 1.41 XGlyphInfo g;
1172 root 1.66 XftTextExtents16 (disp, f, &ch, 1, &g);
1173 root 1.1
1174 root 1.41 int wcw = wcwidth (ch);
1175     if (wcw > 0) g.width = g.width / wcw;
1176 root 1.1
1177 root 1.41 if (width < g.width) width = g.width;
1178     if (height < g.height) height = g.height;
1179     }
1180 root 1.1
1181 root 1.41 if (prop.height == rxvt_fontprop::unset
1182     || height <= prop.height
1183     || height <= 2
1184     || !scalable)
1185 root 1.1 break;
1186    
1187     if (ftheight)
1188     {
1189     // take smaller steps near the end
1190     if (height > prop.height + 1) ftheight++;
1191     if (height > prop.height + 2) ftheight++;
1192     if (height > prop.height + 3) ftheight++;
1193    
1194 root 1.41 ftheight -= height - prop.height;
1195 root 1.1 }
1196     else
1197 root 1.41 ftheight = prop.height - 1;
1198    
1199 root 1.66 XftFontClose (disp, f);
1200 root 1.41 FcPatternDel (match, FC_PIXEL_SIZE);
1201     FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1202 root 1.1 }
1203    
1204 root 1.41 FcPatternDestroy (match);
1205    
1206     #if 0 // do it per-character
1207     if (prop.width != rxvt_fontprop::unset && width > prop.width)
1208     {
1209     clear ();
1210     success = false;
1211     }
1212     #endif
1213 root 1.1
1214 root 1.41 return success;
1215 root 1.1 }
1216    
1217     bool
1218 root 1.41 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1219 root 1.1 {
1220 root 1.41 careful = false;
1221    
1222     if (!XftCharExists (DISPLAY, f, unicode))
1223     return false;
1224    
1225     if (!prop || prop->width == rxvt_fontprop::unset)
1226     return true;
1227    
1228     // check character against base font bounding box
1229     FcChar32 ch = unicode;
1230     XGlyphInfo g;
1231     XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1232    
1233     int w = g.width;
1234     int wcw = wcwidth (unicode);
1235     if (wcw > 0) w /= wcw;
1236    
1237     careful = w > prop->width;
1238     if (careful && w > prop->width * MAX_OVERLAP >> 2)
1239     return false;
1240    
1241     return true;
1242 root 1.1 }
1243    
1244     void
1245     rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1246     const text_t *text, int len,
1247     int fg, int bg)
1248     {
1249     clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
1250    
1251 root 1.41 int base = ascent; // should be fbase, but that is incorrect
1252    
1253     XGlyphInfo extents;
1254     FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1255     FcChar32 *ep = enc;
1256     int ewidth = 0;
1257     int xoff = 0;
1258    
1259     while (len)
1260     {
1261     int cwidth = r->TermWin.fwidth;
1262     FcChar32 fc = *text++; len--;
1263     FT_UInt gl;
1264    
1265     while (len && *text == NOCHAR)
1266     text++, len--, cwidth += r->TermWin.fwidth;
1267    
1268     gl = XftCharIndex (d.display->display, f, fc);
1269     XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1270    
1271     if (extents.xOff != cwidth && ep != enc)
1272 root 1.1 {
1273 root 1.41 if (xoff > ewidth) xoff = ewidth;
1274     XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1275     x + (ewidth - xoff >> 1),
1276     y + base, enc, ep - enc);
1277     x += ewidth;
1278    
1279     ep = enc;
1280     ewidth = 0;
1281     xoff = 0;
1282     }
1283 root 1.1
1284 root 1.41 if (fc == ' ' && ep == enc) // skip leading spaces
1285     {
1286     x += cwidth;
1287     continue;
1288     }
1289 root 1.1
1290 root 1.41 else
1291     {
1292     *ep++ = gl;
1293     ewidth += cwidth;
1294     xoff += extents.xOff;
1295 root 1.1 }
1296     }
1297 root 1.41
1298     if (ep != enc)
1299     {
1300     if (xoff > ewidth) xoff = ewidth;
1301     XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1302     x + (ewidth - xoff >> 1),
1303     y + base, enc, ep - enc);
1304     }
1305 root 1.1 }
1306     #endif
1307    
1308     /////////////////////////////////////////////////////////////////////////////
1309    
1310     rxvt_fontset::rxvt_fontset (rxvt_t r)
1311     : r (r), fontdesc (0)
1312     {
1313     clear ();
1314     }
1315    
1316     rxvt_fontset::~rxvt_fontset ()
1317     {
1318     clear ();
1319     }
1320    
1321     void
1322     rxvt_fontset::clear ()
1323     {
1324 root 1.46 prop.width = prop.height = prop.weight = prop.slant
1325     = rxvt_fontprop::unset;
1326    
1327 root 1.1 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1328     FONT_UNREF (*i);
1329    
1330 root 1.41 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1331     delete *p;
1332    
1333 root 1.1 free (fontdesc); fontdesc = 0;
1334    
1335     fonts.clear ();
1336    
1337     fallback = fallback_fonts;
1338     }
1339    
1340     rxvt_font *
1341     rxvt_fontset::new_font (const char *name, codeset cs)
1342     {
1343     rxvt_font *f;
1344    
1345     if (!name || !*name)
1346     {
1347     name = "";
1348 root 1.41 f = new rxvt_font_default (this);
1349 root 1.1 }
1350     #if XFT
1351     else if (!strncmp (name, "xft:", 4))
1352     {
1353     name += 4;
1354 root 1.41 f = new rxvt_font_xft ();
1355 root 1.1 }
1356     #endif
1357     else if (!strncmp (name, "x:", 2))
1358     {
1359     name += 2;
1360     f = new rxvt_font_x11;
1361     }
1362     else
1363     f = new rxvt_font_x11;
1364    
1365     f->set_term (r);
1366     f->set_name (strdup (name));
1367    
1368     f->cs = cs;
1369     f->loaded = false;
1370    
1371     return f;
1372     }
1373    
1374     /////////////////////////////////////////////////////////////////////////////
1375    
1376     void
1377     rxvt_fontset::add_fonts (const char *desc)
1378     {
1379     if (desc)
1380     {
1381     char buf[512];
1382     const char *end;
1383    
1384     do
1385     {
1386 root 1.67 while (*desc && *desc <= ' ')
1387     desc++;
1388 root 1.1
1389 root 1.41 codeset cs = CS_UNICODE;
1390    
1391 root 1.1 if (*desc == '[')
1392     {
1393 root 1.41 char spec[256];
1394     const char *extra = ++desc; // not yet used
1395 root 1.1
1396     desc = strchr (desc, ']');
1397    
1398     if (!desc)
1399     {
1400 root 1.41 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1401 root 1.1 break;
1402     }
1403    
1404 root 1.41 memcpy (spec, extra, min (desc - extra, 255));
1405     spec[min (desc - extra, 255)] = 0;
1406    
1407     if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1408     cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1409     else
1410     rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1411    
1412 root 1.1 desc++;
1413     while (*desc <= ' ') desc++;
1414     }
1415    
1416     end = strchr (desc, ',');
1417     if (!end)
1418     end = desc + strlen (desc);
1419    
1420     if (end - desc < 511)
1421     {
1422     strncpy (buf, desc, end - desc);
1423     buf[end - desc] = 0;
1424    
1425 root 1.41 fonts.push_back (new_font (buf, cs));
1426 root 1.1 }
1427 root 1.41 else
1428     rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1429 root 1.1
1430     desc = end + 1;
1431     }
1432     while (*end);
1433     }
1434     }
1435    
1436     bool
1437     rxvt_fontset::realize_font (int i)
1438     {
1439 root 1.41 if (i < 0 || i >= fonts.size ())
1440     return false;
1441    
1442 root 1.1 if (fonts[i]->loaded)
1443     return true;
1444    
1445     fonts[i]->loaded = true;
1446    
1447 root 1.41 if (!fonts[i]->load (prop))
1448 root 1.1 {
1449     fonts[i]->cs = CS_UNKNOWN;
1450     return false;
1451     }
1452    
1453     return true;
1454     }
1455    
1456     bool
1457 root 1.46 rxvt_fontset::populate (const char *desc)
1458 root 1.1 {
1459     clear ();
1460    
1461     fontdesc = strdup (desc);
1462    
1463     fonts.push_back (new_font (0, CS_UNICODE));
1464     realize_font (0);
1465    
1466     add_fonts (desc);
1467    
1468     return true;
1469     }
1470    
1471     int
1472     rxvt_fontset::find_font (const char *name) const
1473     {
1474     for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1475     if ((*f)->name && !strcmp ((*f)->name, name))
1476     return f - fonts.begin ();
1477    
1478     return -1;
1479     }
1480    
1481     int
1482 root 1.41 rxvt_fontset::find_font (unicode_t unicode)
1483 root 1.1 {
1484 root 1.41 if (unicode >= 1<<20)
1485     return 0;
1486    
1487     unicode_t hi = unicode >> 8;
1488    
1489     if (hi < fmap.size ()
1490     && fmap[hi]
1491     && (*fmap[hi])[unicode & 0xff] != 0xff)
1492     return (*fmap[hi])[unicode & 0xff];
1493 root 1.1
1494 root 1.41 unsigned int i;
1495    
1496     for (i = 0; i < fonts.size (); i++)
1497 root 1.1 {
1498     rxvt_font *f = fonts[i];
1499    
1500     if (!f->loaded)
1501     {
1502     if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1503     goto next_font;
1504    
1505     if (!realize_font (i))
1506     goto next_font;
1507     }
1508    
1509     if (f->cs == CS_UNKNOWN)
1510     goto next_font;
1511    
1512 root 1.41 bool careful;
1513     if (f->has_char (unicode, &prop, careful))
1514     {
1515     if (careful)
1516     i |= 128;
1517 root 1.1
1518 root 1.41 goto found;
1519     }
1520 root 1.1
1521     next_font:
1522     if (i == fonts.size () - 1)
1523     {
1524     if (fallback->name)
1525     {
1526     // search through the fallback list
1527     fonts.push_back (new_font (fallback->name, fallback->cs));
1528     fallback++;
1529     }
1530 root 1.41 else
1531 root 1.1 {
1532     // try to find a new font.
1533     // only xft currently supported, as there is no
1534     // way to configure this and xft is easier to hack in,
1535     // while x11 has more framework in place already.
1536 root 1.41 // TODO: this is a real resource hog, xft takes ages(?)
1537     #if XFT && USE_SLOW_LOOKUP
1538 root 1.1 // grab the first xft font that seems suitable
1539     FcPattern *p = FcPatternCreate ();
1540    
1541     FcCharSet *s = FcCharSetCreate ();
1542     FcCharSetAddChar (s, unicode);
1543     FcPatternAddCharSet (p, FC_CHARSET, s);
1544     // charsets don't help that much, as xft might return
1545     // a non-matching font even if a better font is available :/
1546    
1547 root 1.41 x x x x TODO prop might have unset contents
1548     FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1549     FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1550     FcPatternAddInteger (p, FC_SLANT, prop.slant);
1551 root 1.1 FcPatternAddBool (p, FC_MINSPACE, 1);
1552     //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1553    
1554     XftResult result;
1555 root 1.66 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1556 root 1.1
1557     FcPatternDestroy (p);
1558    
1559     if (match)
1560     {
1561     FcPatternDel (match, FC_CHARSET);
1562     char *font = (char *)FcNameUnparse (match);
1563     FcPatternDestroy (match);
1564    
1565     if (find_font (font) < 0)
1566     {
1567     char fontname[4096];
1568     sprintf (fontname, "xft:%-.4090s", font);
1569    
1570     fonts.push_back (new_font (fontname, CS_UNICODE));
1571     }
1572    
1573     free (font);
1574     }
1575     #endif
1576     }
1577     }
1578     }
1579    
1580 root 1.41 /* we must return SOME font */
1581     i = 0;
1582    
1583     found:
1584     // found a font, cache it
1585     if (i < 255)
1586     {
1587     while (hi >= fmap.size ())
1588     fmap.push_back (0);
1589    
1590     if (!fmap[hi])
1591     {
1592     fmap[hi] = (pagemap *)new pagemap;
1593     memset (fmap[hi], 0xff, sizeof (pagemap));
1594     }
1595    
1596     (*fmap[hi])[unicode & 0xff] = i;
1597     }
1598 root 1.1
1599 root 1.41 return i;
1600 root 1.1 }
1601    
1602    
1603