ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.85
Committed: Wed Jan 25 00:42:21 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.84: +7 -21 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*
2 * File: rxvtfont.C
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/
21
22 #include "../config.h"
23 #include "rxvt.h"
24 #include "rxvtlib.h"
25 #include "rxvtutil.h"
26 #include "rxvtfont.h"
27
28 #include <cstdlib>
29 #include <wchar.h>
30 #include <inttypes.h>
31
32 #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
33
34 const struct rxvt_fallback_font {
35 codeset cs;
36 const char *name;
37 } fallback_fonts[] = {
38 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
39 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
40 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
41
42 #if ENCODING_EU
43 // cyrillic
44 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
45 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
46
47 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
48 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
49 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
50 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
51 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
52 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
53 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
54 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
55 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
56 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
57 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
58 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
59 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
60
61 # if XFT
62 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
63
64 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
65 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
66 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
67 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
68 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
69 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
70 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
71 # endif
72 #endif
73
74 // japanese
75 #if ENCODING_JP || ENCODING_JP_EXT
76 # if XFT
77 // prefer xft for complex scripts
78 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
79 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
80 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
81 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
82 # endif
83 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
84 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
85 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
86 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
87 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
88 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
89 #endif
90
91 #if ENCODING_ZH || ENCODING_ZH_EXT
92 # if XFT
93 { CS_GBK_0, "xft:AR PL KaitiM GB" },
94 { CS_GBK_0, "xft:AR PL SungtiL GB" },
95 { CS_GBK_0, "xft::spacing=100:lang=zh" },
96 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
97 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
98 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
99 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
100 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
101 # endif
102 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
103 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
104 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
105 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
106 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
107 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
108 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
109 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
110 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
111 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
112 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
113 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
114 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
115 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
116 #endif
117
118 #if ENCODING_KR
119 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
120 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
121 # if XFT
122 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
123 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
124 # endif
125 #endif
126
127 // generic font fallback
128 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
129 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
130 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
131 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
132 #if XFT
133 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
134 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
135 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
136 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
137
138 // FreeMono is usually uglier than x fonts, so try last only.
139 { CS_UNICODE, "xft:FreeMono:autohint=true" },
140 #endif
141
142 { CS_UNKNOWN, 0 }
143 };
144
145 // these characters are used to guess the font height and width
146 // pango uses a similar algorithm and doesn't trust the font either.
147 static uint16_t extent_test_chars[] = {
148 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
149 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
150 0x304c, 0x672c, // が本
151 };
152
153 #define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
154
155 #define dTermDisplay Display *disp = term->display->display
156 #define dTermGC GC gc = term->gc
157
158 /////////////////////////////////////////////////////////////////////////////
159
160 #if XFT
161 rxvt_drawable::~rxvt_drawable ()
162 {
163 if (xftdrawable)
164 XftDrawDestroy (xftdrawable);
165 }
166
167 rxvt_drawable::operator XftDraw *()
168 {
169 if (!xftdrawable)
170 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
171
172 return xftdrawable;
173 }
174 #endif
175
176 /////////////////////////////////////////////////////////////////////////////
177
178 static const char *
179 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
180 {
181 uint8_t *buf = rxvt_temp_buf<uint8_t> (len);
182 uint8_t *res = buf;
183
184 while (len--)
185 {
186 uint32_t c = FROM_UNICODE (cs, *text++);
187
188 if (c == NOCHAR)
189 {
190 c = 0;
191 zero = true;
192 }
193
194 *buf++ = c;
195 }
196
197 return (const char *)res;
198 }
199
200 static const XChar2b *
201 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
202 {
203 XChar2b *buf = rxvt_temp_buf<XChar2b> (len);
204 XChar2b *res = buf;
205
206 while (len--)
207 {
208 uint32_t c = FROM_UNICODE (cs, *text++);
209
210 if (c == NOCHAR)
211 {
212 c = 0;
213 zero = true;
214 }
215
216 buf->byte1 = c >> 8;
217 buf->byte2 = c;
218 buf++;
219 }
220
221 return res;
222 }
223
224 /////////////////////////////////////////////////////////////////////////////
225
226 void
227 rxvt_font::set_name (char *name)
228 {
229 if (this->name == name)
230 return;
231
232 if (this->name) free (this->name); // let the compiler optimize
233 this->name = name;
234 }
235
236 void
237 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
238 {
239 dTermDisplay;
240 dTermGC;
241
242 if (color == Color_bg)
243 XClearArea (disp, d, x, y, w, h, FALSE);
244 else if (color >= 0)
245 {
246 #if XFT
247 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
248 #else
249 XSetForeground (disp, gc, term->pix_colors[color]);
250 XFillRectangle (disp, d, gc, x, y, w, h);
251 #endif
252 }
253 }
254
255 #include "table/linedraw.h"
256
257 struct rxvt_font_default : rxvt_font {
258 struct rxvt_fontset *fs;
259
260 rxvt_font_default (rxvt_fontset *fs)
261 : rxvt_font ()
262 {
263 this->fs = fs;
264 }
265
266 rxvt_fontprop properties ()
267 {
268 rxvt_fontprop p;
269
270 p.width = p.height = 1;
271 p.ascent = rxvt_fontprop::unset;
272 p.weight = rxvt_fontprop::medium;
273 p.slant = rxvt_fontprop::roman;
274
275 return p;
276 }
277
278 bool load (const rxvt_fontprop &prop)
279 {
280 width = 1; height = 1;
281 ascent = 1; descent = 0;
282
283 set_name (strdup ("built-in support font"));
284
285 return true;
286 }
287
288 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
289 {
290 careful = false;
291
292 if (unicode <= 0x001f)
293 return true;
294
295 if (unicode <= 0x007f)
296 return false;
297
298 if (unicode <= 0x009f)
299 return true;
300
301 if (unicode >= 0x2500 && unicode <= 0x259f &&
302 !term->option (Opt_skipBuiltinGlyphs))
303 return true;
304
305 if (IS_COMPOSE (unicode))
306 return true;
307
308 switch (unicode)
309 {
310 case ZERO_WIDTH_CHAR:
311 case NOCHAR:
312 return true;
313 }
314
315 return false;
316 }
317
318 void draw (rxvt_drawable &d, int x, int y,
319 const text_t *text, int len,
320 int fg, int bg);
321 };
322
323 void
324 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
325 const text_t *text, int len,
326 int fg, int bg)
327 {
328 dTermDisplay;
329 dTermGC;
330
331 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
332
333 XSetForeground (disp, gc, term->pix_colors[fg]);
334
335 while (len)
336 {
337 #if ENABLE_COMBINING
338 compose_char *cc;
339 #endif
340 const text_t *tp = text;
341 text_t t = *tp;
342
343 while (++text, --len && *text == NOCHAR)
344 ;
345
346 int width = text - tp;
347 int fwidth = term->fwidth * width;
348
349 if (0x2500 <= t && t <= 0x259f)
350 {
351 uint16_t offs = linedraw_offs[t - 0x2500];
352 uint32_t *a = linedraw_command + (offs >> 4);
353 uint32_t *b = a + (offs & 15);
354
355 int W = fwidth;
356 int H = term->fheight;
357
358 int x_[16];
359 int y_[16];
360
361 for (int i = 0; i <= 8; i++)
362 {
363 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
364 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
365 }
366
367 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
368 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
369
370 XGCValues gcv;
371
372 gcv.cap_style = CapButt;
373 gcv.line_width = 0;
374 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
375
376 while (a < b)
377 {
378 uint32_t command = *a++;
379
380 int op = (command >> 24) & 255;
381 int a = (command >> 20) & 15;
382 int b = (command >> 16) & 15;
383 int x1 = x_[(command >> 12) & 15];
384 int y1 = y_[(command >> 8) & 15];
385 int x2 = x_[(command >> 4) & 15];
386 int y2 = y_[(command >> 0) & 15];
387
388 switch (op)
389 {
390 case 0: // line
391 XDrawLine (disp, d, gc, x1, y1, x2, y2);
392 break;
393
394 case 1: // rectangle, possibly stippled
395 if (a)
396 {
397 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
398
399 gcv.fill_style = FillStippled;
400 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
401 gcv.ts_x_origin = x;
402 gcv.ts_y_origin = y;
403
404 XChangeGC (disp, gc,
405 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
406 &gcv);
407 }
408
409 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
410
411 if (a)
412 {
413 XFreePixmap (disp, gcv.stipple);
414 gcv.stipple = 0;
415 gcv.fill_style = FillSolid;
416 XChangeGC (disp, gc, GCFillStyle, &gcv);
417 }
418 break;
419 case 2: // arc
420 XDrawArc (disp, d, gc,
421 x1 - W/2, y1 - H/2, W-1, H-1,
422 (a - 1) * 90*64, (b - 1) * 90*64);
423 break;
424 }
425 }
426 }
427 #if ENABLE_COMBINING
428 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
429 {
430 text_t chrs[2];
431 width = min (2, width);
432 chrs [1] = NOCHAR;
433
434 *chrs = cc->c1;
435 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
436 f1->draw (d, x, y, chrs, width, fg, bg);
437
438 if (cc->c2 != NOCHAR)
439 {
440 bool careful;
441
442 // prefer font of first character, for no good reasons
443 *chrs = cc->c2;
444 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
445 ? f1
446 : (*fs)[fs->find_font (cc->c2)];
447
448 f2->draw (d, x, y, chrs, width, fg, -1);
449 }
450 }
451 #endif
452 else
453 switch (t)
454 {
455 case '\t':
456 case ZERO_WIDTH_CHAR:
457 case NOCHAR:
458 break;
459
460 default:
461 XDrawRectangle (disp, d, gc, x + 2, y + 2,
462 fwidth - 4, term->fheight - 4);
463 }
464
465 x += fwidth;
466 }
467 }
468
469 /////////////////////////////////////////////////////////////////////////////
470
471 struct rxvt_font_x11 : rxvt_font {
472 rxvt_font_x11 () { f = 0; }
473
474 void clear ();
475
476 rxvt_fontprop properties ();
477
478 bool load (const rxvt_fontprop &prop);
479
480 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
481
482 void draw (rxvt_drawable &d, int x, int y,
483 const text_t *text, int len,
484 int fg, int bg);
485
486 bool slow; // wether this is a proportional font or has other funny characteristics
487 XFontStruct *f;
488 codeset cs;
489 bool enc2b, encm;
490
491 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
492 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
493 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
494 bool set_properties (rxvt_fontprop &p, const char *name);
495 };
496
497 char *
498 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
499 {
500 unsigned long value;
501
502 if (XGetFontProperty (f, XInternAtom (term->display->display, property, 0), &value))
503 return XGetAtomName (term->display->display, value);
504 else
505 return rxvt_strdup (repl);
506 }
507
508 rxvt_fontprop
509 rxvt_font_x11::properties ()
510 {
511 rxvt_fontprop p;
512 set_properties (p, f);
513 return p;
514 }
515
516 bool
517 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
518 {
519 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
520 p.height = height;
521 p.ascent = rxvt_fontprop::unset;
522 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
523 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
524
525 return true;
526 }
527
528 bool
529 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
530 {
531 unsigned long height;
532
533 #if 0
534 if (!XGetFontProperty (f, XInternAtom (term->display->display, "PIXEL_SIZE", 0), &height))
535 return false;
536 #else
537 height = f->ascent + f->descent;
538 #endif
539
540 unsigned long avgwidth;
541 if (!XGetFontProperty (f, XInternAtom (term->display->display, "AVERAGE_WIDTH", 0), &avgwidth))
542 avgwidth = 0;
543
544 char *weight = get_property (f, "WEIGHT_NAME", "medium");
545 char *slant = get_property (f, "SLANT", "r");
546
547 set_properties (p, height, weight, slant, avgwidth);
548
549 free (weight);
550 free (slant);
551
552 p.ascent = f->ascent;
553
554 return true;
555 }
556
557 bool
558 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
559 {
560 dTermDisplay;
561 int slashes = 0;
562 const char *comp[13];
563
564 for (const char *c = name; *c; c++)
565 if (*c == '-')
566 {
567 comp[slashes++] = c + 1;
568 if (slashes >= 13)
569 break;
570 }
571
572 /* can we short-circuit the costly XLoadQueryFont? */
573 if (slashes >= 13
574 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
575 && (*comp[11] >= '0' && *comp[11] <= '9'))
576 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
577
578 XFontStruct *f = XLoadQueryFont (disp, name);
579
580 if (f)
581 {
582 // the font should really exist now. if not, we have a problem
583 // (e.g. if the user did xset fp rehash just when we were searching fonts).
584 // in that case, just return garbage.
585 bool ret = set_properties (p, f);
586 XFreeFont (disp, f);
587 return ret;
588 }
589 else
590 return false;
591 }
592
593 // fix the size of scalable fonts
594 static bool
595 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
596 {
597 int slashes = 0;
598 const char *field, *end;
599
600 for (const char *c = name; *c; c++)
601 if (*c == '-')
602 {
603 if (slashes == index)
604 field = c + 1;
605
606 if (slashes == index + 1)
607 end = c;
608
609 if (++slashes >= 13)
610 break;
611 }
612
613 if (slashes >= 13 && (!old || *field == old))
614 {
615 // TODO: check for overflow in font-name
616 strncpy (buf, name, field - name);
617 buf += field - name;
618 strcpy (buf, replace);
619 strcat (buf, end);
620
621 return true;
622 }
623 else
624 {
625 strcpy (buf, name);
626
627 return false;
628 }
629 }
630
631 bool
632 rxvt_font_x11::load (const rxvt_fontprop &prop)
633 {
634 dTermDisplay;
635
636 clear ();
637
638 char field_str[64]; // enough for 128 bits
639
640 // first morph the font if required
641 if (prop.weight != rxvt_fontprop::unset
642 || prop.slant != rxvt_fontprop::unset)
643 {
644 char fname[1024];
645
646 if (name[0] != '-')
647 {
648 f = XLoadQueryFont (disp, name);
649
650 if (!f)
651 return false;
652
653 char *new_name = get_property (f, "FONT", name);
654
655 if (new_name)
656 set_name (new_name);
657 else
658 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
659
660 XFreeFont (disp, f);
661 f = 0;
662 }
663
664 if (prop.weight != rxvt_fontprop::unset)
665 {
666 replace_field (fname, name, 2, 0,
667 prop.weight < rxvt_fontprop::bold
668 ? "medium" : "bold");
669 set_name (strdup (fname));
670 }
671
672 if (prop.slant != rxvt_fontprop::unset)
673 {
674 replace_field (fname, name, 3, 0,
675 prop.slant < rxvt_fontprop::italic
676 ? "r" : "i"); // TODO: handle "o"blique, too
677 set_name (strdup (fname));
678 }
679 }
680
681 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
682 ? 0 : prop.height);
683
684 struct font_weight {
685 char *name;
686 int diff;
687
688 void clear ()
689 {
690 name = 0;
691 diff = 0x7fffffff;
692 }
693
694 font_weight () { clear (); }
695 ~font_weight () { free (name); }
696 };
697
698 char **list;
699 int count;
700 list = XListFonts (disp, name, 4000, &count);
701
702 set_name (0);
703
704 if (!list)
705 return false;
706
707 font_weight *fonts = new font_weight[count];
708
709 for (int i = 0; i < count; i++)
710 {
711 rxvt_fontprop p;
712 char fname[1024];
713
714 int diff = 0;
715
716 if (replace_field (fname, list[i], 6, '0', field_str))
717 diff += 10; // slightly penalize scalable fonts
718 else if (replace_field (fname, list[i], 11, '0', "0"))
719 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
720
721 if (!set_properties (p, fname))
722 continue;
723
724 if (prop.height != rxvt_fontprop::unset
725 && p.height > prop.height) // weed out too large fonts
726 continue;
727
728 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
729 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
730 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
731 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
732
733 fonts[i].name = strdup (fname);
734 fonts[i].diff = diff;
735 }
736
737 XFreeFontNames (list);
738
739 // this loop only iterates when the guessed font-size is too small
740 for (;;)
741 {
742 font_weight *best = fonts + count - 1;
743
744 for (font_weight *w = best; w-- > fonts; )
745 if (w->diff <= best->diff)
746 best = w;
747
748 if (!best->name
749 || !(f = XLoadQueryFont (disp, best->name)))
750 break;
751
752 set_name (best->name);
753 best->clear ();
754
755 ascent = f->ascent;
756 descent = f->descent;
757 height = ascent + descent;
758
759 if (prop.height == rxvt_fontprop::unset
760 || height <= prop.height)
761 break; // font is ready for use
762
763 // PIXEL_SIZE small enough, but real height too large
764 clear ();
765 }
766
767 delete [] fonts;
768
769 if (!f)
770 return false;
771
772 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
773 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
774
775 if (registry && encoding)
776 {
777 char charset[64];
778 snprintf (charset, 64, "%s-%s", registry, encoding);
779
780 cs = codeset_from_name (charset);
781 }
782 else
783 {
784 const char *charset = get_property (f, "FONT", 0);
785
786 if (!charset)
787 charset = name;
788
789 int count = 13;
790 while (*charset)
791 if (*charset++ == '-' && !--count)
792 break;
793
794 cs = codeset_from_name (charset);
795 }
796
797 free (registry);
798 free (encoding);
799
800 if (cs == CS_UNICODE)
801 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
802
803 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
804 enc2b = encm || f->max_char_or_byte2 > 255;
805
806 slow = false;
807
808 #if 1 // only used for slow detection, TODO optimize
809 if (f->min_bounds.width == f->max_bounds.width)
810 width = f->min_bounds.width;
811 else if (f->per_char == NULL)
812 width = f->max_bounds.width;
813 else
814 {
815 slow = true;
816
817 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
818
819 if (encm)
820 N += (f->max_byte1 - f->min_byte1)
821 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
822
823 while (N)
824 {
825 if (f->per_char[N].width > width)
826 width = f->per_char[N].width;
827
828 --N;
829 }
830 }
831 #endif
832
833 width = 1;
834
835 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
836 {
837 if (cs != CS_UNICODE
838 && *t > 0x100
839 && FROM_UNICODE (cs, *t) == NOCHAR)
840 continue;
841
842 // ignore characters we wouldn't use anyways
843 bool careful;
844 if (!has_char (*t, &prop, careful))
845 continue;
846
847 XChar2b ch = { *t >> 8, *t };
848
849 XCharStruct g;
850 int dir_ret, asc_ret, des_ret;
851 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
852
853 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
854
855 if (width < g.width) width = g.width;
856 }
857
858 if (cs == CS_UNKNOWN)
859 {
860 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
861
862 clear ();
863 return false;
864 }
865
866 #if 0 // do it per-character
867 if (prop && width > prop->width)
868 {
869 clear ();
870 return false;
871 }
872 #endif
873
874 return true;
875 }
876
877 void
878 rxvt_font_x11::clear ()
879 {
880 if (f)
881 {
882 XFreeFont (term->display->display, f);
883 f = 0;
884 }
885 }
886
887 bool
888 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
889 {
890 uint32_t ch = FROM_UNICODE (cs, unicode);
891
892 if (ch == NOCHAR)
893 return false;
894
895 /* check wether the character exists in _this_ font. horrible. */
896 XCharStruct *xcs;
897
898 if (encm)
899 {
900 unsigned char byte1 = ch >> 8;
901 unsigned char byte2 = ch & 255;
902
903 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
904 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
905 return false;
906
907 if (f->per_char)
908 {
909 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
910 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
911
912 xcs = f->per_char + N;
913 }
914 else
915 xcs = &f->max_bounds;
916 }
917 else
918 {
919 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
920 return false;
921
922 if (f->per_char)
923 xcs = f->per_char + (ch - f->min_char_or_byte2);
924 else
925 xcs = &f->max_bounds;
926 }
927
928 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
929 && xcs->ascent == 0 && xcs->descent == 0)
930 return false;
931
932 if (!prop || prop->width == rxvt_fontprop::unset)
933 return true;
934
935 // check character against base font bounding box
936 int w = xcs->width;
937 int wcw = wcwidth (unicode);
938 if (wcw > 0) w = (w + wcw - 1) / wcw;
939
940 careful = w > prop->width;
941 if (careful && w > prop->width * MAX_OVERLAP >> 2)
942 return false;
943
944 return true;
945 }
946
947 void
948 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
949 const text_t *text, int len,
950 int fg, int bg)
951 {
952 // this looks like a mess /.
953 // and it is a mess /.
954 // yet we are trying to be perfect /.
955 // but the result still isn't perfect /.
956
957 dTermDisplay;
958 dTermGC;
959
960 bool slow = this->slow
961 || width != term->fwidth
962 || height != term->fheight;
963
964 int base = ascent; // sorry, incorrect: term->fbase;
965
966 XGCValues v;
967 v.foreground = term->pix_colors[fg];
968 v.font = f->fid;
969
970 if (enc2b)
971 {
972 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
973
974 if (bg == Color_bg && !slow)
975 {
976 v.background = term->pix_colors[bg];
977 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
978 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
979 }
980 else
981 {
982 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
983
984 XChangeGC (disp, gc, GCForeground | GCFont, &v);
985
986 if (slow)
987 {
988 do
989 {
990 if (xc->byte1 || xc->byte2)
991 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
992
993 x += term->fwidth;
994 xc++; len--;
995 }
996 while (len);
997 }
998 else
999 XDrawString16 (disp, d, gc, x, y + base, xc, len);
1000 }
1001 }
1002 else
1003 {
1004 const char *xc = enc_char (text, len, cs, slow);
1005
1006 if (bg == Color_bg && !slow)
1007 {
1008 v.background = term->pix_colors[bg];
1009 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
1010 XDrawImageString (disp, d, gc, x, y + base, xc, len);
1011 }
1012 else
1013 {
1014 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1015
1016 XChangeGC (disp, gc, GCForeground | GCFont, &v);
1017
1018 if (slow)
1019 {
1020 do
1021 {
1022 if (*xc)
1023 XDrawString (disp, d, gc, x, y + base, xc, 1);
1024
1025 x += term->fwidth;
1026 xc++; len--;
1027 }
1028 while (len);
1029 }
1030 else
1031 XDrawString (disp, d, gc, x, y + base, xc, len);
1032 }
1033 }
1034 }
1035
1036 /////////////////////////////////////////////////////////////////////////////
1037
1038 #if XFT
1039
1040 struct rxvt_font_xft : rxvt_font {
1041 rxvt_font_xft () { f = 0; }
1042
1043 void clear ();
1044
1045 rxvt_fontprop properties ();
1046
1047 bool load (const rxvt_fontprop &prop);
1048
1049 void draw (rxvt_drawable &d, int x, int y,
1050 const text_t *text, int len,
1051 int fg, int bg);
1052
1053 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull) const;
1054
1055 protected:
1056 XftFont *f;
1057 };
1058
1059 void
1060 rxvt_font_xft::clear ()
1061 {
1062 if (f)
1063 {
1064 XftFontClose (term->display->display, f);
1065 f = 0;
1066 }
1067 }
1068
1069 rxvt_fontprop
1070 rxvt_font_xft::properties ()
1071 {
1072 rxvt_fontprop p;
1073
1074 FT_Face face = XftLockFace (f);
1075
1076 p.width = width;
1077 p.height = height;
1078 p.ascent = ascent;
1079 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1080 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1081 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1082 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1083
1084 XftUnlockFace (f);
1085
1086 return p;
1087 }
1088
1089 bool
1090 rxvt_font_xft::load (const rxvt_fontprop &prop)
1091 {
1092 dTermDisplay;
1093
1094 clear ();
1095
1096 FcPattern *p = FcNameParse ((FcChar8 *) name);
1097
1098 if (!p)
1099 return false;
1100
1101 FcValue v;
1102
1103 if (prop.height != rxvt_fontprop::unset
1104 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1105 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1106 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1107
1108 if (prop.weight != rxvt_fontprop::unset
1109 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1110 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1111
1112 if (prop.slant != rxvt_fontprop::unset
1113 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1114 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1115
1116 #if 0 // clipping unfortunately destroys our precious double-width-characters
1117 // clip width, we can't do better, or can we?
1118 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1119 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1120 #endif
1121
1122 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1123 FcPatternAddBool (p, FC_MINSPACE, 1);
1124
1125 // store generated name so iso14755 view gives better results
1126 set_name ((char *)FcNameUnparse (p));
1127
1128 XftResult result;
1129 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
1130
1131 FcPatternDestroy (p);
1132
1133 if (!match)
1134 return false;
1135
1136 int ftheight = 0;
1137 bool success = true;
1138
1139 for (;;)
1140 {
1141 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1142
1143 if (!f)
1144 {
1145 success = false;
1146 break;
1147 }
1148
1149 FT_Face face = XftLockFace (f);
1150
1151 ascent = (face->size->metrics.ascender + 63) >> 6;
1152 descent = (-face->size->metrics.descender + 63) >> 6;
1153 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1154 width = 0;
1155
1156 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1157
1158 XftUnlockFace (f);
1159
1160 int glheight = height;
1161
1162 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1163 {
1164 FcChar16 ch = *t;
1165
1166 if (cs != CS_UNICODE
1167 && ch > 0x100
1168 && FROM_UNICODE (cs, ch) == NOCHAR)
1169 continue;
1170
1171 // ignore characters we wouldn't use anyways
1172 bool careful;
1173 if (!has_char (*t, &prop, careful))
1174 continue;
1175
1176 XGlyphInfo g;
1177 XftTextExtents16 (disp, f, &ch, 1, &g);
1178
1179 g.width -= g.x;
1180
1181 int wcw = wcwidth (ch);
1182 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1183
1184 if (width < g.width ) width = g.width;
1185 if (height < g.height ) height = g.height;
1186 if (glheight < g.height - g.y) glheight = g.height - g.y;
1187 }
1188
1189 if (!width)
1190 {
1191 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1192
1193 XftFontClose (disp, f);
1194 f = 0;
1195
1196 success = false;
1197 break;
1198 }
1199
1200 if (prop.height == rxvt_fontprop::unset
1201 || (height <= prop.height && glheight <= prop.height)
1202 || height <= 2
1203 || !scalable)
1204 break;
1205
1206 if (ftheight)
1207 {
1208 // take smaller steps near the end
1209 if (height > prop.height + 1) ftheight++;
1210 if (height > prop.height + 2) ftheight++;
1211 if (height > prop.height + 3) ftheight++;
1212
1213 ftheight -= height - prop.height;
1214 }
1215 else
1216 ftheight = prop.height - 1;
1217
1218 XftFontClose (disp, f);
1219 FcPatternDel (match, FC_PIXEL_SIZE);
1220 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1221 }
1222
1223 FcPatternDestroy (match);
1224
1225 #if 0 // do it per-character
1226 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1227 {
1228 clear ();
1229 success = false;
1230 }
1231 #endif
1232
1233 return success;
1234 }
1235
1236 bool
1237 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1238 {
1239 careful = false;
1240
1241 if (!XftCharExists (term->display->display, f, unicode))
1242 return false;
1243
1244 if (!prop || prop->width == rxvt_fontprop::unset)
1245 return true;
1246
1247 // check character against base font bounding box
1248 FcChar32 ch = unicode;
1249 XGlyphInfo g;
1250 XftTextExtents32 (term->display->display, f, &ch, 1, &g);
1251
1252 int w = g.width - g.x;
1253 int wcw = wcwidth (unicode);
1254 if (wcw > 0) w = (w + wcw - 1) / wcw;
1255
1256 careful = w > prop->width;
1257 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1258 return false;
1259
1260 return true;
1261 }
1262
1263 void
1264 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1265 const text_t *text, int len,
1266 int fg, int bg)
1267 {
1268 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1269
1270 XGlyphInfo extents;
1271 XftGlyphSpec *enc = (XftGlyphSpec *)rxvt_temp_buf (len * sizeof (XftGlyphSpec));
1272 XftGlyphSpec *ep = enc;
1273
1274 dTermDisplay;
1275 dTermGC;
1276
1277 // cut trailing spaces
1278 while (len && text [len - 1] == ' ')
1279 len--;
1280
1281 while (len)
1282 {
1283 int cwidth = term->fwidth;
1284 FcChar32 fc = *text++; len--;
1285
1286 while (len && *text == NOCHAR)
1287 text++, len--, cwidth += term->fwidth;
1288
1289 if (fc != ' ') // skip spaces
1290 {
1291 FT_UInt glyph = XftCharIndex (disp, f, fc);
1292 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1293
1294 ep->glyph = glyph;
1295 ep->x = x + (cwidth - extents.xOff >> 1);
1296 ep->y = y + ascent;
1297 ep++;
1298 }
1299
1300 x += cwidth;
1301 }
1302
1303 if (ep != enc)
1304 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1305 }
1306 #endif
1307
1308 /////////////////////////////////////////////////////////////////////////////
1309
1310 rxvt_fontset::rxvt_fontset (rxvt_term *term)
1311 : fontdesc (0), term (term)
1312 {
1313 clear ();
1314 }
1315
1316 rxvt_fontset::~rxvt_fontset ()
1317 {
1318 clear ();
1319 }
1320
1321 void
1322 rxvt_fontset::clear ()
1323 {
1324 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1325 = rxvt_fontprop::unset;
1326
1327 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1328 FONT_UNREF (*i);
1329
1330 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1331 delete *p;
1332
1333 free (fontdesc); fontdesc = 0;
1334
1335 fonts.clear ();
1336
1337 fallback = fallback_fonts;
1338 }
1339
1340 rxvt_font *
1341 rxvt_fontset::new_font (const char *name, codeset cs)
1342 {
1343 rxvt_font *f;
1344
1345 if (!name || !*name)
1346 {
1347 name = "";
1348 f = new rxvt_font_default (this);
1349 }
1350 #if XFT
1351 else if (!strncmp (name, "xft:", 4))
1352 {
1353 name += 4;
1354 f = new rxvt_font_xft ();
1355 }
1356 #endif
1357 else if (!strncmp (name, "x:", 2))
1358 {
1359 name += 2;
1360 f = new rxvt_font_x11;
1361 }
1362 else
1363 f = new rxvt_font_x11;
1364
1365 f->set_term (term);
1366 f->set_name (strdup (name));
1367
1368 f->cs = cs;
1369 f->loaded = false;
1370
1371 return f;
1372 }
1373
1374 /////////////////////////////////////////////////////////////////////////////
1375
1376 void
1377 rxvt_fontset::add_fonts (const char *desc)
1378 {
1379 if (desc)
1380 {
1381 char buf[512];
1382 const char *end;
1383
1384 do
1385 {
1386 while (*desc && *desc <= ' ')
1387 desc++;
1388
1389 codeset cs = CS_UNICODE;
1390
1391 if (*desc == '[')
1392 {
1393 char spec[256];
1394 const char *extra = ++desc; // not yet used
1395
1396 desc = strchr (desc, ']');
1397
1398 if (!desc)
1399 {
1400 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1401 break;
1402 }
1403
1404 memcpy (spec, extra, min (desc - extra, 255));
1405 spec[min (desc - extra, 255)] = 0;
1406
1407 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1408 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1409 else
1410 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1411
1412 desc++;
1413 while (*desc <= ' ') desc++;
1414 }
1415
1416 end = strchr (desc, ',');
1417 if (!end)
1418 end = desc + strlen (desc);
1419
1420 if (end - desc < 511)
1421 {
1422 strncpy (buf, desc, end - desc);
1423 buf[end - desc] = 0;
1424
1425 fonts.push_back (new_font (buf, cs));
1426 }
1427 else
1428 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1429
1430 desc = end + 1;
1431 }
1432 while (*end);
1433 }
1434 }
1435
1436 bool
1437 rxvt_fontset::realize_font (int i)
1438 {
1439 if (i < 0 || i >= fonts.size ())
1440 return false;
1441
1442 if (fonts[i]->loaded)
1443 return true;
1444
1445 fonts[i]->loaded = true;
1446
1447 if (!fonts[i]->load (prop))
1448 {
1449 fonts[i]->cs = CS_UNKNOWN;
1450 return false;
1451 }
1452
1453 return true;
1454 }
1455
1456 bool
1457 rxvt_fontset::populate (const char *desc)
1458 {
1459 clear ();
1460
1461 fontdesc = strdup (desc);
1462
1463 fonts.push_back (new_font (0, CS_UNICODE));
1464 realize_font (0);
1465
1466 add_fonts (desc);
1467
1468 return true;
1469 }
1470
1471 int
1472 rxvt_fontset::find_font (const char *name) const
1473 {
1474 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1475 if ((*f)->name && !strcmp ((*f)->name, name))
1476 return f - fonts.begin ();
1477
1478 return -1;
1479 }
1480
1481 int
1482 rxvt_fontset::find_font (unicode_t unicode)
1483 {
1484 if (unicode >= 1<<20)
1485 return 0;
1486
1487 unicode_t hi = unicode >> 8;
1488
1489 if (hi < fmap.size ()
1490 && fmap[hi]
1491 && (*fmap[hi])[unicode & 0xff] != 0xff)
1492 return (*fmap[hi])[unicode & 0xff];
1493
1494 unsigned int i;
1495
1496 for (i = 0; i < fonts.size (); i++)
1497 {
1498 rxvt_font *f = fonts[i];
1499
1500 if (!f->loaded)
1501 {
1502 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1503 goto next_font;
1504
1505 if (!realize_font (i))
1506 goto next_font;
1507
1508 if (prop.ascent != rxvt_fontprop::unset)
1509 max_it (f->ascent, prop.ascent);
1510 }
1511
1512 if (f->cs == CS_UNKNOWN)
1513 goto next_font;
1514
1515 bool careful;
1516 if (f->has_char (unicode, &prop, careful))
1517 {
1518 if (careful)
1519 i |= 128;
1520
1521 goto found;
1522 }
1523
1524 next_font:
1525 if (i == fonts.size () - 1)
1526 {
1527 if (fallback->name)
1528 {
1529 // search through the fallback list
1530 fonts.push_back (new_font (fallback->name, fallback->cs));
1531 fallback++;
1532 }
1533 else
1534 {
1535 // try to find a new font.
1536 // only xft currently supported, as there is no
1537 // way to configure this and xft is easier to hack in,
1538 // while x11 has more framework in place already.
1539 // TODO: this is a real resource hog, xft takes ages(?)
1540 #if XFT && USE_SLOW_LOOKUP
1541 // grab the first xft font that seems suitable
1542 FcPattern *p = FcPatternCreate ();
1543
1544 FcCharSet *s = FcCharSetCreate ();
1545 FcCharSetAddChar (s, unicode);
1546 FcPatternAddCharSet (p, FC_CHARSET, s);
1547 // charsets don't help that much, as xft might return
1548 // a non-matching font even if a better font is available :/
1549
1550 x x x x TODO prop might have unset contents
1551 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1552 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1553 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1554 FcPatternAddBool (p, FC_MINSPACE, 1);
1555 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1556
1557 XftResult result;
1558 FcPattern *match = XftFontMatch (term->display->display, term->display->screen, p, &result);
1559
1560 FcPatternDestroy (p);
1561
1562 if (match)
1563 {
1564 FcPatternDel (match, FC_CHARSET);
1565 char *font = (char *)FcNameUnparse (match);
1566 FcPatternDestroy (match);
1567
1568 if (find_font (font) < 0)
1569 {
1570 char fontname[4096];
1571 sprintf (fontname, "xft:%-.4090s", font);
1572
1573 fonts.push_back (new_font (fontname, CS_UNICODE));
1574 }
1575
1576 free (font);
1577 }
1578 #endif
1579 }
1580 }
1581 }
1582
1583 /* we must return SOME font */
1584 i = 0;
1585
1586 found:
1587 // found a font, cache it
1588 if (i < 255)
1589 {
1590 while (hi >= fmap.size ())
1591 fmap.push_back (0);
1592
1593 if (!fmap[hi])
1594 {
1595 fmap[hi] = (pagemap *)new pagemap;
1596 memset (fmap[hi], 0xff, sizeof (pagemap));
1597 }
1598
1599 (*fmap[hi])[unicode & 0xff] = i;
1600 }
1601
1602 return i;
1603 }
1604
1605
1606