ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
Revision: 1.93
Committed: Tue Jan 31 00:25:16 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_5
Changes since 1.92: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

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