ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.103
Committed: Sun Aug 27 10:10:25 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.102: +10 -10 lines
Log Message:
*** empty log message ***

File Contents

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