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