ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.56
Committed: Fri Dec 10 19:55:55 2004 UTC (19 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_4, rel-4_6, rel-4_7
Changes since 1.52: +28 -36 lines
Log Message:
*** empty log message ***

File Contents

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