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