ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.91
Committed: Mon Jan 30 04:27:17 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.90: +50 -35 lines
Log Message:
*** empty log message ***

File Contents

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