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