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

File Contents

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