ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.134
Committed: Wed Jan 23 09:42:02 2008 UTC (16 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.133: +35 -41 lines
Log Message:
improve transparency without render a bit again

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