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