ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.98
Committed: Mon Feb 20 22:42:00 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_7
Changes since 1.97: +1 -1 lines
Log Message:
*** empty log message ***

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