ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.42
Committed: Mon Mar 15 06:13:35 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.41: +38 -24 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*;
2     * File: defaultfont.C
3     *----------------------------------------------------------------------*
4 pcg 1.22 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 pcg 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     *---------------------------------------------------------------------*/
21    
22     #include "../config.h"
23     #include "rxvt.h"
24     #include "defaultfont.h"
25    
26 pcg 1.7 #include <cstdlib>
27    
28 pcg 1.17 #define DISPLAY r->display->display
29 pcg 1.2 #define GC r->TermWin.gc
30 pcg 1.1
31     const struct rxvt_fallback_font {
32     codeset cs;
33     const char *name;
34     } fallback_fonts[] = {
35     { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
36     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
37     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
38    
39     #if ENCODING_EU
40     // cyrillic
41     { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
42     { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
43    
44     { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
45     { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
46     { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
47     { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
48     { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
49     { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
50     { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
51     { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
52     { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53     { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54     { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55     { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56     { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
57     #endif
58    
59     // japanese
60     #if ENCODING_JP || ENCODING_JP_EXT
61     # if XFT
62     // prefer xft for complex scripts
63 pcg 1.9 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
64 pcg 1.1 # endif
65 pcg 1.9 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 pcg 1.41 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 pcg 1.9 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 pcg 1.1 #endif
69    
70     #if ENCODING_CN || ENCODING_CN_EXT
71     # if XFT
72 pcg 1.9 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
73     { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
74     { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
75     { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
76 pcg 1.1 # endif
77 pcg 1.28 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
78     { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
79     { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
80 pcg 1.27 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
81     { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
82     { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
83     { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
84     { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
85     { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
86     { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
87     { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
88     { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
89 pcg 1.1 #endif
90    
91     #if XFT
92 pcg 1.9 { CS_UNICODE, "xft:Andale Mono" },
93     { CS_UNICODE, "xft:Arial Unicode MS" },
94 pcg 1.1 #endif
95 pcg 1.9 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
96     { CS_UNICODE, "xft:FreeMono" },
97     { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
98     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
99     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
100 pcg 1.1
101 pcg 1.30 #if UNICODE_3 && XFT
102     { CS_UNICODE, "xft:Code2001" }, // contains many plane-1 characters
103     #endif
104    
105 pcg 1.1 { CS_UNKNOWN, 0 }
106     };
107    
108     /////////////////////////////////////////////////////////////////////////////
109    
110 pcg 1.26 #if XFT
111 pcg 1.24 rxvt_drawable::~rxvt_drawable ()
112     {
113     if (xftdrawable)
114     XftDrawDestroy (xftdrawable);
115     }
116    
117     rxvt_drawable::operator XftDraw *()
118     {
119     if (!xftdrawable)
120     xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
121    
122     return xftdrawable;
123     }
124 pcg 1.26 #endif
125 pcg 1.24
126     /////////////////////////////////////////////////////////////////////////////
127    
128 pcg 1.1 static void *enc_buf;
129     static uint32_t enc_len;
130    
131     static inline void *
132 pcg 1.16 get_enc_buf (uint32_t len)
133 pcg 1.1 {
134     if (len > enc_len)
135     {
136     free (enc_buf);
137     enc_buf = malloc (len);
138     }
139    
140     return enc_buf;
141     }
142    
143     static const char *
144 pcg 1.16 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
145 pcg 1.1 {
146     uint8_t *buf = (uint8_t *)get_enc_buf (len);
147    
148     while (len--)
149     {
150     uint32_t c = FROM_UNICODE (cs, *text++);
151    
152     if (c == NOCHAR)
153     {
154     c = 0;
155     zero = true;
156     }
157    
158     *buf++ = c;
159     }
160    
161     return (const char *)enc_buf;
162     }
163    
164     static const XChar2b *
165 pcg 1.16 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
166 pcg 1.1 {
167     XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
168    
169     while (len--)
170     {
171     uint32_t c = FROM_UNICODE (cs, *text++);
172    
173     if (c == NOCHAR)
174     {
175     c = 0;
176     zero = true;
177     }
178    
179     buf->byte1 = c >> 8;
180     buf->byte2 = c;
181     buf++;
182     }
183    
184     return (XChar2b *)enc_buf;
185     }
186    
187     /////////////////////////////////////////////////////////////////////////////
188    
189     void
190 pcg 1.24 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
191 pcg 1.1 {
192     if (color == Color_bg)
193 pcg 1.24 XClearArea (d.display->display, d, x, y, w, h, FALSE);
194 pcg 1.1 else if (color >= 0)
195     {
196 pcg 1.24 #if XFT
197     XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
198     #else
199     XSetForeground (d.display->display, GC, r->PixColors[color]);
200     XFillRectangle (d.display->display, d, GC, x, y, w, h);
201     #endif
202 pcg 1.1 }
203     }
204    
205 pcg 1.3 static const char *linedraw_cmds[128] = {
206 pcg 1.42 "1-", "2-", "1|", "2|",
207 pcg 1.3 0, 0, 0, 0,
208     0, 0, 0, 0,
209     "1HV", "2H1V", "1H2V", "2HV",
210    
211     // 2510
212     "1hV", "2h1V", "1h2V", "2hV",
213     "1Hv", "2H1v", "1H2v", "2Hv",
214     "1hv", "2h1v", "1h2v", "2hv",
215 pcg 1.42 "1H|", "2H1|", "1HV2v", "1Hv2V",
216 pcg 1.3
217     // 2520
218 pcg 1.42 "1H2|", "2Hv1V", "2HV1v", "2H|",
219     "1h|", "2h1|", "1hV2v", "1hv2V",
220     "1h2|", "2hv1V", "1v2hV", "2h|",
221     "1-V", "2h1HV", "2H1hV", "2-1V",
222 pcg 1.3
223     // 2530
224 pcg 1.42 "1-2V", "2hV1H", "1h2HV", "2-V",
225     "1-v", "1vH2h", "1hv2H", "1v2-",
226     "1-2v", "1H2hv", "1h2Hv", "2-v",
227     "1-|", "1|H2h", "1h|2H", "1|2-",
228 pcg 1.3
229     // 2540
230 pcg 1.42 "1-V2v", "1-v2V", "1-2|", "1HV2hv",
231     "1hV2Hv", "1Hv2hV", "1hv2HV", "1V2-v",
232     "1v2-V", "1H2h|", "1h2H|", "2-|",
233 pcg 1.3 0, 0, 0, 0,
234    
235     // 2550
236     0, 0, 0, 0,
237     0, 0, 0, 0,
238     0, 0, 0, 0,
239     0, 0, 0, 0,
240    
241     // 2560
242     0, 0, 0, 0,
243     0, 0, 0, 0,
244     0, 0, 0, 0,
245 pcg 1.42 0, "A", "B", "C",
246 pcg 1.3
247     // 2570
248 pcg 1.42 "D", "1/", "1\\", "1/\\",
249 pcg 1.3 "1h", "1v", "1H", "1V",
250     "2h", "2v", "2H", "2V",
251     "1h2H", "1v2V", "1H2h", "1V2v"
252    
253     // to be done
254     };
255    
256 pcg 1.1 struct rxvt_font_default : rxvt_font {
257 pcg 1.7 rxvt_fontprop properties ()
258     {
259     rxvt_fontprop p;
260    
261 pcg 1.10 p.width = p.height = 1;
262 pcg 1.7 p.weight = rxvt_fontprop::medium;
263     p.slant = rxvt_fontprop::roman;
264    
265     return p;
266     }
267    
268     bool load (const rxvt_fontprop &prop)
269 pcg 1.1 {
270     width = 1; height = 1;
271     ascent = 1; descent = 0;
272    
273 pcg 1.38 set_name ("built-in pseudofont");
274    
275 pcg 1.1 return true;
276     }
277    
278 pcg 1.40 bool has_codepoint (unicode_t unicode)
279 pcg 1.1 {
280 pcg 1.3 if (unicode <= 0x001f)
281     return true;
282 pcg 1.34
283 pcg 1.3 if (unicode >= 0x0080 && unicode <= 0x009f)
284     return true;
285    
286     if (unicode >= 0x2500 && unicode <= 0x257f
287     && linedraw_cmds[unicode - 0x2500])
288 pcg 1.1 return true;
289    
290 pcg 1.35 if (IS_COMPOSE (unicode))
291 pcg 1.34 return true;
292    
293 pcg 1.1 switch (unicode)
294     {
295     case ZERO_WIDTH_CHAR:
296     return true;
297     }
298    
299     return false;
300     }
301    
302 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
303 pcg 1.1 const text_t *text, int len,
304     int fg, int bg);
305     };
306    
307     void
308 pcg 1.24 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
309 pcg 1.1 const text_t *text, int len,
310     int fg, int bg)
311     {
312 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
313 pcg 1.1
314 pcg 1.24 XSetForeground (d.display->display, GC, r->PixColors[fg]);
315 pcg 1.3
316 pcg 1.1 while (len--)
317     {
318 pcg 1.39 #if ENABLE_COMBINING
319 pcg 1.36 compose_char *cc;
320 pcg 1.39 #endif
321 pcg 1.3 text_t t = *text++;
322    
323 pcg 1.27 // is it in our linedrawing table?
324 pcg 1.3 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
325 pcg 1.1 {
326 pcg 1.3 const char *p = linedraw_cmds[t - 0x2500];
327    
328 pcg 1.42 int W = r->TermWin.fwidth , w = (W - 1) / 2;
329     int H = r->TermWin.fheight, h = (H - 1) / 2;
330     int x0 = x, x1 = x + w, x2 = x + r->TermWin.fwidth ;
331     int y0 = y, y1 = y + h, y2 = y + r->TermWin.fheight;
332 pcg 1.3
333     XGCValues gcv;
334    
335 pcg 1.42 gcv.cap_style = CapNotLast;
336     XChangeGC (d.display->display, GC, GCCapStyle, &gcv);
337    
338 pcg 1.3 while (*p)
339     {
340     switch (*p++)
341     {
342     case '1':
343     gcv.line_width = 0;
344 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
345 pcg 1.3 break;
346    
347     case '2':
348 pcg 1.42 gcv.line_width = 3;
349 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
350 pcg 1.3 break;
351    
352 pcg 1.42 case 'h': XDrawLine (d.display->display, d, GC, x0, y1, x1+1, y1 ); break;
353     case 'H': XDrawLine (d.display->display, d, GC, x2, y1, x1-1, y1 ); break;
354     case '-': XDrawLine (d.display->display, d, GC, x0, y1, x2 , y1 ); break;
355     case 'v': XDrawLine (d.display->display, d, GC, x1, y0, x1 , y1+1); break;
356     case 'V': XDrawLine (d.display->display, d, GC, x1, y2, x1 , y1-1); break;
357     case '|': XDrawLine (d.display->display, d, GC, x1, y0, x1 , y2 ); break;
358    
359     case '/' : XDrawLine (d.display->display, d, GC, x0, y2, x2 , y0 ); break;
360     case '\\': XDrawLine (d.display->display, d, GC, x0, y0, x2 , y2 ); break;
361    
362     case 'A': XDrawArc (d.display->display, d, GC, x1 , y1 , W-1, H-1, 90*64, 90*64); break;
363     case 'B': XDrawArc (d.display->display, d, GC, x1-W+1, y1 , W-1, H-1, 0*64, 90*64); break;
364     case 'C': XDrawArc (d.display->display, d, GC, x1-W+1, y1-H+1, W-1, H-1, 0*64, -90*64); break;
365     case 'D': XDrawArc (d.display->display, d, GC, x1 , y1-H+1, W-1, H-1, -90*64, -90*64); break;
366 pcg 1.3 }
367     }
368    
369     gcv.line_width = 0;
370 pcg 1.24 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
371 pcg 1.1 }
372 pcg 1.42
373 pcg 1.39 #if ENABLE_COMBINING
374 pcg 1.36 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
375 pcg 1.34 {
376 pcg 1.37 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
377     f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
378 pcg 1.36 if (cc->c2 != NOCHAR)
379 pcg 1.37 {
380     // prefer font of first character, for no good reasons
381     rxvt_font *f2 = f1->has_codepoint (cc->c2)
382     ? f1
383     : (*fs)[fs->find_font (cc->c2)];
384    
385     f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
386     }
387 pcg 1.34 }
388 pcg 1.39 #endif
389 pcg 1.3 else
390 pcg 1.27 switch (t)
391 pcg 1.3 {
392     case ZERO_WIDTH_CHAR:
393     break;
394     default:
395 pcg 1.27 int w = 0;
396     while (len > 0 && *text == NOCHAR)
397     {
398     ++text;
399     --len;
400     w += r->TermWin.fwidth;
401     }
402    
403     XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
404     w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
405     x += w;
406 pcg 1.3 }
407 pcg 1.1
408 pcg 1.2 x += r->TermWin.fwidth;
409 pcg 1.1 }
410     }
411    
412     /////////////////////////////////////////////////////////////////////////////
413    
414     struct rxvt_font_x11 : rxvt_font {
415     rxvt_font_x11 () { f = 0; }
416    
417     void clear ();
418    
419 pcg 1.7 rxvt_fontprop properties ();
420    
421     bool load (const rxvt_fontprop &prop);
422 pcg 1.1
423 pcg 1.40 bool has_codepoint (unicode_t unicode);
424 pcg 1.1
425 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
426 pcg 1.1 const text_t *text, int len,
427     int fg, int bg);
428    
429     XFontStruct *f;
430     codeset cs;
431     bool enc2b, encm;
432    
433 pcg 1.7 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
434 pcg 1.12 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
435     bool set_properties (rxvt_fontprop &p, XFontStruct *f);
436     bool set_properties (rxvt_fontprop &p, const char *name);
437 pcg 1.1 };
438    
439     const char *
440 pcg 1.7 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
441 pcg 1.1 {
442     unsigned long value;
443    
444     if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
445     return XGetAtomName (DISPLAY, value);
446     else
447     return repl;
448     }
449    
450 pcg 1.7 rxvt_fontprop
451     rxvt_font_x11::properties ()
452     {
453 pcg 1.12 rxvt_fontprop p;
454     set_properties (p, f);
455     return p;
456 pcg 1.7 }
457    
458 pcg 1.12 bool
459     rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
460 pcg 1.7 {
461 pcg 1.12 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
462     p.height = height;
463     p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
464     p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
465    
466     return true;
467     }
468 pcg 1.7
469 pcg 1.12 bool
470     rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
471     {
472 pcg 1.7 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
473     const char *slant = get_property (f, "SLANT", "r");
474    
475 pcg 1.12 unsigned long height;
476     if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
477     return false;
478    
479 pcg 1.10 unsigned long avgwidth;
480 pcg 1.12 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
481     avgwidth = 0;
482    
483     return set_properties (p, height, weight, slant, avgwidth);
484     }
485    
486     bool
487     rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
488     {
489     int slashes = 0;
490 pcg 1.29 const char *comp[13];
491 pcg 1.12
492     for (const char *c = name; *c; c++)
493     if (*c == '-')
494     {
495     comp[slashes++] = c + 1;
496     if (slashes >= 13)
497     break;
498     }
499    
500     /* can we short-circuit the costly XLoadQueryFont? */
501     if (slashes >= 13
502     && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
503     && (*comp[11] >= '0' && *comp[11] <= '9'))
504     return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
505    
506     XFontStruct *f = XLoadQueryFont (DISPLAY, name);
507    
508     if (f)
509     {
510 pcg 1.19 // the font should really exist now. if not, we have a problem
511 pcg 1.12 // (e.g. if the user did xset fp rehash just when we were searching fonts).
512     // in that case, just return garbage.
513     bool ret = set_properties (p, f);
514     XFreeFont (DISPLAY, f);
515     return ret;
516     }
517     else
518     return false;
519     }
520    
521     // fix the size of scalable fonts
522     static void
523     fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
524     {
525     int slashes = 0;
526     const char *size;
527    
528     for (const char *c = name; *c; c++)
529     if (*c == '-')
530     {
531     if (slashes == 6)
532     size = c + 1;
533    
534     if (++slashes >= 13)
535     break;
536     }
537 pcg 1.7
538 pcg 1.12 if (slashes >= 13 && size[0] == '0')
539     {
540     strncpy (buf, name, size - name);
541     buf += size - name;
542     buf += sprintf (buf, "%d", prop.height);
543     strcpy (buf, size + 1);
544     }
545     else
546     strcpy (buf, name);
547 pcg 1.7 }
548    
549 pcg 1.1 bool
550 pcg 1.7 rxvt_font_x11::load (const rxvt_fontprop &prop)
551 pcg 1.1 {
552     clear ();
553    
554 pcg 1.6 char **list;
555     int count;
556 pcg 1.12 list = XListFonts (DISPLAY, name, 512, &count);
557     set_name (0);
558 pcg 1.6
559     if (!list)
560     return false;
561    
562 pcg 1.7 int bestdiff = 0x7fffffff;
563 pcg 1.6 for (int i = 0; i < count; i++)
564     {
565 pcg 1.12 rxvt_fontprop p;
566     char fname[1024];
567     fix_scalable (fname, list[i], prop);
568    
569     if (!set_properties (p, fname))
570     continue;
571    
572     if (p.height > prop.height) // weed out too large fonts
573     continue;
574    
575     int diff = (prop.height - p.height) * 32
576     + abs (prop.weight - p.weight)
577     + abs (prop.slant - p.slant );
578 pcg 1.7
579 pcg 1.12 if (!name // compare against best found so far
580     || diff < bestdiff)
581 pcg 1.7 {
582 pcg 1.12 set_name (strdup (fname));
583     bestdiff = diff;
584 pcg 1.7 }
585 pcg 1.6 }
586 pcg 1.9
587 pcg 1.12 XFreeFontNames (list);
588    
589     if (!name)
590 pcg 1.9 return false;
591 pcg 1.6
592 pcg 1.1 f = XLoadQueryFont (DISPLAY, name);
593    
594     if (!f)
595     return false;
596    
597 pcg 1.7 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
598     const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
599 pcg 1.1
600     if (registry && encoding)
601     {
602     char charset[64];
603     snprintf (charset, 64, "%s-%s", registry, encoding);
604    
605     cs = codeset_from_name (charset);
606     }
607     else
608     {
609 pcg 1.7 const char *charset = get_property (f, "FONT", 0);
610 pcg 1.1
611     if (!charset)
612     charset = name;
613    
614     int count = 13;
615     while (*charset)
616     if (*charset++ == '-' && !--count)
617     break;
618    
619     cs = codeset_from_name (charset);
620     }
621    
622     if (cs == CS_UNICODE)
623     cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
624    
625     encm = f->min_byte1 != 0 || f->max_byte1 != 0;
626     enc2b = encm || f->max_char_or_byte2 > 255;
627    
628     ascent = f->ascent;
629     descent = f->descent;
630     height = ascent + descent;
631    
632 pcg 1.7 slow = false;
633 pcg 1.1
634     if (f->min_bounds.width == f->max_bounds.width)
635     width = f->min_bounds.width;
636     else if (f->per_char == NULL)
637     width = f->max_bounds.width;
638     else
639     {
640 pcg 1.7 slow = true;
641 pcg 1.1
642     int N = f->max_char_or_byte2 - f->min_char_or_byte2;
643    
644     if (encm)
645     N += (f->max_byte1 - f->min_byte1)
646     * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
647    
648     while (N)
649     {
650     if (f->per_char[N].width > width)
651     width = f->per_char[N].width;
652    
653     --N;
654     }
655     }
656    
657     if (cs == CS_UNKNOWN)
658     {
659     fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
660    
661     clear ();
662    
663     return false;
664     }
665    
666     return true;
667     }
668    
669     void
670     rxvt_font_x11::clear ()
671     {
672     if (f)
673     {
674     XFreeFont (DISPLAY, f);
675     f = 0;
676     }
677     }
678    
679     bool
680 pcg 1.40 rxvt_font_x11::has_codepoint (unicode_t unicode)
681 pcg 1.1 {
682     uint32_t ch = FROM_UNICODE (cs, unicode);
683    
684     if (ch == NOCHAR)
685     return false;
686    
687     /* check wether the character exists in _this_ font. horrible. */
688     XCharStruct *xcs;
689    
690     if (encm)
691     {
692 pcg 1.16 unsigned char byte1 = ch >> 8;
693     unsigned char byte2 = ch & 255;
694 pcg 1.1
695     if (byte1 < f->min_byte1 || byte1 > f->max_byte1
696     || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
697     return false;
698    
699     if (!f->per_char)
700     return true;
701    
702     int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
703     int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
704    
705     xcs = f->per_char + N;
706     }
707     else
708     {
709     if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
710     return false;
711    
712     if (!f->per_char)
713     return true;
714    
715     xcs = f->per_char + (ch - f->min_char_or_byte2);
716     }
717    
718     if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
719     && xcs->ascent == 0 && xcs->descent == 0)
720     return false;
721    
722     return true;
723     }
724    
725     void
726 pcg 1.24 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
727 pcg 1.1 const text_t *text, int len,
728     int fg, int bg)
729     {
730     // this looks like a mess /.
731     // and it is a mess /.
732     // yet we are trying to be perfect /.
733     // but the result still isn't perfect /.
734    
735 pcg 1.7 bool slow = this->slow
736 pcg 1.2 || width != r->TermWin.fwidth
737     || height != r->TermWin.fheight;
738 pcg 1.1
739 pcg 1.2 int base = r->TermWin.fbase;
740 pcg 1.1
741     XGCValues v;
742 pcg 1.2 v.foreground = r->PixColors[fg];
743     v.background = r->PixColors[bg];
744 pcg 1.1 v.font = f->fid;
745    
746     if (enc2b)
747     {
748     const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
749    
750     if (bg == Color_bg && !slow)
751     {
752 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
753     XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
754 pcg 1.1 }
755     else
756     {
757 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
758 pcg 1.1
759 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
760 pcg 1.1
761     if (slow)
762     {
763     do
764     {
765     if (xc->byte1 || xc->byte2)
766 pcg 1.24 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
767 pcg 1.1
768 pcg 1.2 x += r->TermWin.fwidth;
769 pcg 1.1 xc++; len--;
770     }
771     while (len);
772     }
773     else
774 pcg 1.24 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
775 pcg 1.1 }
776     }
777     else
778     {
779     const char *xc = enc_char (text, len, cs, slow);
780    
781     if (bg == Color_bg && !slow)
782     {
783 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
784     XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
785 pcg 1.1 }
786     else
787     {
788 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
789 pcg 1.1
790 pcg 1.24 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
791 pcg 1.1
792     if (slow)
793     {
794     do
795     {
796     if (*xc)
797 pcg 1.24 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
798 pcg 1.1
799 pcg 1.2 x += r->TermWin.fwidth;
800 pcg 1.1 xc++; len--;
801     }
802     while (len);
803     }
804     else
805 pcg 1.24 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
806 pcg 1.1 }
807     }
808     }
809    
810     /////////////////////////////////////////////////////////////////////////////
811    
812     #if XFT
813     #if 0
814     #define UNIBITS 21
815     //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
816     #define SWATHBITS 8
817     #endif
818    
819     struct rxvt_font_xft : rxvt_font {
820 pcg 1.24 rxvt_font_xft () { f = 0; }
821 pcg 1.1
822     void clear ();
823    
824 pcg 1.7 rxvt_fontprop properties ();
825    
826     bool load (const rxvt_fontprop &prop);
827 pcg 1.1
828 pcg 1.24 void draw (rxvt_drawable &d, int x, int y,
829 pcg 1.1 const text_t *text, int len,
830     int fg, int bg);
831    
832 pcg 1.40 bool has_codepoint (unicode_t unicode);
833 pcg 1.1
834     protected:
835     XftFont *f;
836     };
837    
838     void
839     rxvt_font_xft::clear ()
840     {
841     if (f)
842     {
843 pcg 1.2 XftFontClose (DISPLAY, f);
844 pcg 1.1 f = 0;
845     }
846     }
847    
848 pcg 1.7 rxvt_fontprop
849     rxvt_font_xft::properties ()
850     {
851     rxvt_fontprop p;
852    
853     FT_Face face = XftLockFace (f);
854    
855 pcg 1.10 p.width = width; p.height = height;
856 pcg 1.7 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
857     p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
858    
859     XftUnlockFace (f);
860    
861     return p;
862     }
863    
864 pcg 1.1 bool
865 pcg 1.7 rxvt_font_xft::load (const rxvt_fontprop &prop)
866 pcg 1.1 {
867     #if 0
868     for (int i = 0; i < SWATHCOUNT; i++)
869     cvr[i] = 0;
870     #endif
871    
872     clear ();
873    
874 pcg 1.7 FcPattern *p = FcNameParse ((FcChar8 *) name);
875    
876     if (!p)
877     return false;
878    
879     FcValue v;
880    
881     if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
882     FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
883    
884     if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
885     FcPatternAddInteger (p, FC_SLANT, prop.slant);
886    
887 pcg 1.11 #if 0 // clipping unfortunately destroys our precious double-width-characters
888 pcg 1.10 // clip width, we can't do better, or can we?
889     if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
890     FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
891 pcg 1.11 #endif
892 pcg 1.10
893 pcg 1.7 //FcPatternAddBool (p, FC_MINSPACE, 1);
894    
895     XftResult result;
896     FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
897    
898     FcPatternDestroy (p);
899    
900     if (!match)
901     return false;
902    
903     f = XftFontOpenPattern (DISPLAY, match);
904    
905 pcg 1.1 if (!f)
906 pcg 1.8 {
907     FcPatternDestroy (match);
908     return false;
909     }
910 pcg 1.1
911     FT_Face face = XftLockFace (f);
912    
913 pcg 1.7 slow = !FT_IS_FIXED_WIDTH (face);
914 pcg 1.1
915     int ftheight = 0;
916    
917     for (;;)
918     {
919     XGlyphInfo g1, g2;
920     FcChar8 c;
921    
922 pcg 1.2 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
923     c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
924 pcg 1.1
925 pcg 1.7 if (g1.xOff != g2.xOff) // don't simply trust the font
926     slow = true;
927 pcg 1.1
928     width = g2.xOff;
929     ascent = (face->size->metrics.ascender + 63) >> 6;
930     descent = (-face->size->metrics.descender + 63) >> 6;
931     height = ascent + descent;
932    
933 pcg 1.7 if (height <= prop.height || !prop.height)
934 pcg 1.1 break;
935    
936     if (ftheight)
937     {
938     // take smaller steps near the end
939 pcg 1.7 if (height > prop.height + 1) ftheight++;
940     if (height > prop.height + 2) ftheight++;
941     if (height > prop.height + 3) ftheight++;
942 pcg 1.1
943 pcg 1.7 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
944 pcg 1.1 }
945     else
946 pcg 1.7 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
947 pcg 1.1 }
948    
949     XftUnlockFace (f);
950    
951     return true;
952     }
953    
954     bool
955 pcg 1.40 rxvt_font_xft::has_codepoint (unicode_t unicode)
956 pcg 1.1 {
957 pcg 1.2 return XftCharExists (DISPLAY, f, unicode);
958 pcg 1.1 }
959    
960     void
961 pcg 1.24 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
962 pcg 1.1 const text_t *text, int len,
963     int fg, int bg)
964     {
965 pcg 1.24 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
966 pcg 1.1
967 pcg 1.12 if (!slow && width == r->TermWin.fwidth && 0)
968 pcg 1.1 {
969     if (sizeof (text_t) == sizeof (FcChar16))
970 pcg 1.2 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
971 pcg 1.1 else
972 pcg 1.2 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
973 pcg 1.1 }
974     else
975     {
976     while (len)
977     {
978     if (*text != NOCHAR && *text != ' ')
979     {
980 pcg 1.13 int fwidth = r->TermWin.fwidth;
981     if (len >= 2 && text[1] == NOCHAR)
982     fwidth *= 2;
983    
984 pcg 1.12 XGlyphInfo extents;
985 pcg 1.1 if (sizeof (text_t) == sizeof (FcChar16))
986 pcg 1.12 {
987 pcg 1.24 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
988 pcg 1.13 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
989 pcg 1.12 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
990     }
991 pcg 1.1 else
992 pcg 1.12 {
993     XGlyphInfo extents;
994 pcg 1.24 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
995 pcg 1.13 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
996 pcg 1.12 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
997     }
998 pcg 1.1 }
999    
1000 pcg 1.2 x += r->TermWin.fwidth;
1001 pcg 1.1 text++;
1002     len--;
1003     }
1004     }
1005     }
1006     #endif
1007    
1008     /////////////////////////////////////////////////////////////////////////////
1009    
1010 pcg 1.2 rxvt_fontset::rxvt_fontset (rxvt_t r)
1011 pcg 1.18 : r (r)
1012 pcg 1.1 {
1013     clear ();
1014     }
1015    
1016     rxvt_fontset::~rxvt_fontset ()
1017     {
1018     clear ();
1019     }
1020    
1021     void
1022     rxvt_fontset::clear ()
1023     {
1024 pcg 1.18 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1025 pcg 1.1 FONT_UNREF (*i);
1026    
1027     fonts.clear ();
1028     base_id = 0;
1029 pcg 1.7 base_prop.height = 0x7fffffff;
1030     base_prop.weight = rxvt_fontprop::medium;
1031     base_prop.slant = rxvt_fontprop::roman;
1032 pcg 1.1
1033     fallback = fallback_fonts;
1034     }
1035    
1036     rxvt_font *
1037     rxvt_fontset::new_font (const char *name, codeset cs)
1038     {
1039     rxvt_font *f;
1040    
1041     if (!name || !*name)
1042     {
1043     name = "";
1044     f = new rxvt_font_default;
1045     }
1046     #if XFT
1047     else if (!strncmp (name, "xft:", 4))
1048     {
1049     name += 4;
1050     f = new rxvt_font_xft;
1051     }
1052     #endif
1053     else if (!strncmp (name, "x:", 2))
1054     {
1055     name += 2;
1056     f = new rxvt_font_x11;
1057     }
1058     else
1059     f = new rxvt_font_x11;
1060    
1061 pcg 1.36 f->fs = this;
1062 pcg 1.2 f->set_term (r);
1063 pcg 1.1 f->set_name (strdup (name));
1064    
1065     f->cs = cs;
1066     f->loaded = false;
1067    
1068     return f;
1069     }
1070    
1071     /////////////////////////////////////////////////////////////////////////////
1072    
1073     void
1074     rxvt_fontset::add_fonts (const char *desc)
1075     {
1076     if (desc)
1077     {
1078     char buf[512];
1079     const char *end;
1080    
1081     do
1082     {
1083     while (*desc <= ' ') desc++;
1084    
1085     if (*desc == '[')
1086     {
1087     fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
1088    
1089 pcg 1.16 //const char *extra = desc++; // not yet used
1090 pcg 1.1
1091     desc = strchr (desc, ']');
1092    
1093     if (!desc)
1094     {
1095     fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
1096     break;
1097     }
1098    
1099     desc++;
1100     while (*desc <= ' ') desc++;
1101     }
1102    
1103     end = strchr (desc, ',');
1104     if (!end)
1105     end = desc + strlen (desc);
1106    
1107     if (end - desc < 511)
1108     {
1109     strncpy (buf, desc, end - desc);
1110     buf[end - desc] = 0;
1111    
1112     fonts.push_back (new_font (buf, CS_UNICODE));
1113     }
1114    
1115     desc = end + 1;
1116     }
1117     while (*end);
1118     }
1119     }
1120    
1121     bool
1122     rxvt_fontset::realize_font (int i)
1123     {
1124     if (fonts[i]->loaded)
1125     return true;
1126    
1127 pcg 1.5 fonts[i]->loaded = true;
1128 pcg 1.1
1129 pcg 1.7 if (!fonts[i]->load (base_prop))
1130 pcg 1.5 {
1131     fonts[i]->cs = CS_UNKNOWN;
1132     return false;
1133     }
1134 pcg 1.1
1135 pcg 1.5 return true;
1136 pcg 1.1 }
1137    
1138 pcg 1.23 bool
1139 pcg 1.1 rxvt_fontset::populate (const char *desc)
1140     {
1141     clear ();
1142    
1143     fonts.push_back (new_font (0, CS_UNICODE));
1144     realize_font (0);
1145    
1146     add_fonts (desc);
1147    
1148     if (!base_id)
1149     base_id = 1;
1150    
1151     // we currently need a base-font, no matter what
1152 pcg 1.16 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1153 pcg 1.1 {
1154 pcg 1.18 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1155 pcg 1.1 add_fonts ("fixed");
1156 pcg 1.7 base_id = fonts.size () - 1;
1157 pcg 1.1 }
1158    
1159 pcg 1.16 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1160 pcg 1.23 return false;
1161 pcg 1.1
1162 pcg 1.7 base_prop = fonts[base_id]->properties ();
1163 pcg 1.23
1164     return true;
1165 pcg 1.1 }
1166    
1167     int
1168 pcg 1.40 rxvt_fontset::find_font (unicode_t unicode)
1169 pcg 1.1 {
1170 pcg 1.16 for (unsigned int i = 0; i < fonts.size (); i++)
1171 pcg 1.1 {
1172     rxvt_font *f = fonts[i];
1173    
1174     if (!f->loaded)
1175     {
1176     if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1177     goto next_font;
1178    
1179     if (!realize_font (i))
1180 pcg 1.5 goto next_font;
1181 pcg 1.1 }
1182    
1183 pcg 1.5 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
1184 pcg 1.1 return i;
1185    
1186     next_font:
1187     if (i == fonts.size () - 1 && fallback->name)
1188     {
1189     fonts.push_back (new_font (fallback->name, fallback->cs));
1190     fallback++;
1191 pcg 1.5 i = 0;
1192 pcg 1.1 }
1193     }
1194    
1195     return 0; /* we must return SOME font */
1196     }
1197    
1198    
1199