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