ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.43
Committed: Mon Mar 15 07:10:47 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.42: +110 -42 lines
Log Message:
*** empty log message ***

File Contents

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