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