ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.167
Committed: Mon Nov 29 12:33:22 2010 UTC (13 years, 6 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_10
Changes since 1.166: +4 -0 lines
Log Message:
Explicitly depend on fontconfig and xrender when xft support is enabled,
since in recent Xft releases these libs are private dependencies only.

File Contents

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