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