ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.134
Committed: Wed Jan 23 09:42:02 2008 UTC (16 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.133: +35 -41 lines
Log Message:
improve transparency without render a bit again

File Contents

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