ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.167
Committed: Mon Nov 29 12:33:22 2010 UTC (13 years, 5 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_10
Changes since 1.166: +4 -0 lines
Log Message:
Explicitly depend on fontconfig and xrender when xft support is enabled,
since in recent Xft releases these libs are private dependencies only.

File Contents

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