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