ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.76
Committed: Tue Jan 3 23:40:58 2006 UTC (18 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.75: +9 -1 lines
Log Message:
*** empty log message ***

File Contents

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