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