ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.14
Committed: Thu Jan 29 00:27:16 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +9 -8 lines
Log Message:
*** empty log message ***

File Contents

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