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