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