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