ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.56
Committed: Mon Aug 2 04:42:11 2004 UTC (19 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_5, rel-3_4
Changes since 1.55: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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