ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.9
Committed: Wed Dec 24 06:26:52 2003 UTC (20 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +26 -22 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*;
2 * File: defaultfont.C
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003 Marc Lehmann rxvt@plan9.de>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *---------------------------------------------------------------------*/
21
22 #include "../config.h"
23 #include "rxvt.h"
24 #include "defaultfont.h"
25
26 #include <cstdlib>
27
28 #define DISPLAY r->Xdisplay
29 #define DRAWABLE r->TermWin.vt
30 #define GC r->TermWin.gc
31
32 const struct rxvt_fallback_font {
33 codeset cs;
34 const char *name;
35 } fallback_fonts[] = {
36 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
37 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
38 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
39
40 #if ENCODING_EU
41 // cyrillic
42 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
43 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
44
45 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
46 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
47 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
48 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
49 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
50 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
51 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
52 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
53 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
54 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
55 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
56 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
57 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
58 #endif
59
60 // japanese
61 #if ENCODING_JP || ENCODING_JP_EXT
62 # if XFT
63 // prefer xft for complex scripts
64 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
65 # endif
66 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
67 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
68 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
69 #endif
70
71 #if ENCODING_CN || ENCODING_CN_EXT
72 # if XFT
73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
77 # endif
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-1" },
79 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-2" },
80 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-3" },
81 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-4" },
82 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-5" },
83 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-6" },
84 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-7" },
85 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-f" },
86 #endif
87
88 #if XFT
89 { CS_UNICODE, "xft:Andale Mono" },
90 { CS_UNICODE, "xft:Arial Unicode MS" },
91 #endif
92 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "xft:FreeMono" },
94 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
95 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
96 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
97
98 { CS_UNKNOWN, 0 }
99 };
100
101 /////////////////////////////////////////////////////////////////////////////
102
103 static void *enc_buf;
104 static uint32_t enc_len;
105
106 static inline void *
107 get_enc_buf (int len)
108 {
109 if (len > enc_len)
110 {
111 free (enc_buf);
112 enc_buf = malloc (len);
113 }
114
115 return enc_buf;
116 }
117
118 static const char *
119 enc_char (const text_t *text, int len, codeset cs, bool &zero)
120 {
121 uint8_t *buf = (uint8_t *)get_enc_buf (len);
122
123 while (len--)
124 {
125 uint32_t c = FROM_UNICODE (cs, *text++);
126
127 if (c == NOCHAR)
128 {
129 c = 0;
130 zero = true;
131 }
132
133 *buf++ = c;
134 }
135
136 return (const char *)enc_buf;
137 }
138
139 static const XChar2b *
140 enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero)
141 {
142 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
143
144 while (len--)
145 {
146 uint32_t c = FROM_UNICODE (cs, *text++);
147
148 if (c == NOCHAR)
149 {
150 c = 0;
151 zero = true;
152 }
153
154 buf->byte1 = c >> 8;
155 buf->byte2 = c;
156 buf++;
157 }
158
159 return (XChar2b *)enc_buf;
160 }
161
162 /////////////////////////////////////////////////////////////////////////////
163
164 void
165 rxvt_font::clear_rect (int x, int y, int w, int h, int color)
166 {
167 if (color == Color_bg)
168 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE);
169 else if (color >= 0)
170 {
171 XSetForeground (DISPLAY, GC, r->PixColors[color]);
172 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h);
173 }
174 }
175
176 static const char *linedraw_cmds[128] = {
177 "1hH", "2hH", "1vV", "2vV",
178 0, 0, 0, 0,
179 0, 0, 0, 0,
180 "1HV", "2H1V", "1H2V", "2HV",
181
182 // 2510
183 "1hV", "2h1V", "1h2V", "2hV",
184 "1Hv", "2H1v", "1H2v", "2Hv",
185 "1hv", "2h1v", "1h2v", "2hv",
186 "1HvV", "2H1vV", "1HV2v", "1Hv2V",
187
188 // 2520
189 "1H2vV", "2Hv1V", "2HV1v", "2HvV",
190 "1hvV", "2h1vV", "1hV2v", "1hv2V",
191 "1h2vV", "2hv1V", "1v2hV", "2hvV",
192 "1hHV", "2h1HV", "2H1hV", "2hH1V",
193
194 // 2530
195 "1hH2V", "2hV1H", "1h2HV", "2hHV",
196 "1hHv", "1vH2h", "1hv2H", "1v2hH",
197 "1hH2v", "1H2hv", "1h2Hv", "2hHv",
198 "1hHvV", "1vVH2h", "1hvV2H", "1vV2hH",
199
200 // 2540
201 "1hHV2v", "1hHv2V", "1hH2vV", "1HV2hv",
202 "1hV2Hv", "1Hv2hV", "1hv2HV", "1V2hHv",
203 "1v2hHV", "1H2hvV", "1h2HvV", "2hHvV",
204 0, 0, 0, 0,
205
206 // 2550
207 0, 0, 0, 0,
208 0, 0, 0, 0,
209 0, 0, 0, 0,
210 0, 0, 0, 0,
211
212 // 2560
213 0, 0, 0, 0,
214 0, 0, 0, 0,
215 0, 0, 0, 0,
216 0, 0, 0, 0,
217
218 // 2570
219 0, "1a", "1b", "1ab",
220 "1h", "1v", "1H", "1V",
221 "2h", "2v", "2H", "2V",
222 "1h2H", "1v2V", "1H2h", "1V2v"
223
224 // to be done
225 };
226
227 struct rxvt_font_default : rxvt_font {
228 rxvt_fontprop properties ()
229 {
230 rxvt_fontprop p;
231
232 p.height = 1;
233 p.weight = rxvt_fontprop::medium;
234 p.slant = rxvt_fontprop::roman;
235
236 return p;
237 }
238
239 bool load (const rxvt_fontprop &prop)
240 {
241 width = 1; height = 1;
242 ascent = 1; descent = 0;
243
244 return true;
245 }
246
247 bool has_codepoint (uint32_t unicode)
248 {
249 if (unicode <= 0x001f)
250 return true;
251 if (unicode >= 0x0080 && unicode <= 0x009f)
252 return true;
253
254 if (unicode >= 0x2500 && unicode <= 0x257f
255 && linedraw_cmds[unicode - 0x2500])
256 return true;
257
258 switch (unicode)
259 {
260 case ZERO_WIDTH_CHAR:
261 return true;
262 }
263
264 return false;
265 }
266
267 void draw (int x, int y,
268 const text_t *text, int len,
269 int fg, int bg);
270 };
271
272 void
273 rxvt_font_default::draw (int x, int y,
274 const text_t *text, int len,
275 int fg, int bg)
276 {
277 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
278
279 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
280
281 while (len--)
282 {
283 text_t t = *text++;
284
285 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
286 {
287 const char *p = linedraw_cmds[t - 0x2500];
288
289 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ;
290 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight;
291
292 XGCValues gcv;
293
294 while (*p)
295 {
296 switch (*p++)
297 {
298 case '1':
299 gcv.line_width = 0;
300 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
301 break;
302
303 case '2':
304 gcv.line_width = 2;
305 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
306 break;
307
308 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break;
309 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break;
310 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break;
311 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break;
312 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break;
313 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break;
314 }
315 }
316
317 gcv.line_width = 0;
318 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
319 }
320 else
321 switch (*text++)
322 {
323 case NOCHAR:
324 case ZERO_WIDTH_CHAR:
325 break;
326 default:
327 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
328 }
329
330 x += r->TermWin.fwidth;
331 }
332 }
333
334 /////////////////////////////////////////////////////////////////////////////
335
336 struct rxvt_font_x11 : rxvt_font {
337 rxvt_font_x11 () { f = 0; }
338
339 void clear ();
340
341 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
343
344 bool load (const rxvt_fontprop &prop);
345
346 bool has_codepoint (uint32_t unicode);
347
348 void draw (int x, int y,
349 const text_t *text, int len,
350 int fg, int bg);
351
352 XFontStruct *f;
353 codeset cs;
354 bool enc2b, encm;
355
356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
357 };
358
359 const char *
360 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
361 {
362 unsigned long value;
363
364 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
365 return XGetAtomName (DISPLAY, value);
366 else
367 return repl;
368 }
369
370 rxvt_fontprop
371 rxvt_font_x11::properties ()
372 {
373 return properties (f);
374 }
375
376 rxvt_fontprop
377 rxvt_font_x11::properties (XFontStruct *f)
378 {
379 rxvt_fontprop p;
380
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r");
383
384 p.height = height;
385 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
386 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
387
388 return p;
389 }
390
391 bool
392 rxvt_font_x11::load (const rxvt_fontprop &prop)
393 {
394 clear ();
395
396 char **list;
397 int count;
398 XFontStruct *info;
399 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info);
400
401 if (!list)
402 return false;
403
404 int bestdiff = 0x7fffffff;
405 XFontStruct *best = 0;
406 for (int i = 0; i < count; i++)
407 {
408 XFontStruct *f = info + i;
409
410 if (f->ascent + f->descent <= prop.height) // weed out too large fonts
411 {
412 rxvt_fontprop p = properties (f);
413 int diff = (prop.height - f->ascent + f->descent) * 32
414 + abs (prop.weight - p.weight)
415 + abs (prop.slant - p.slant );
416
417 if (!best // compare against best found so far
418 || diff < bestdiff)
419 {
420 best = f;
421 bestdiff = diff;
422 }
423 }
424 }
425
426 if (!best)
427 return false;
428
429 set_name (strdup (list[best - info]));
430
431 XFreeFontInfo (list, info, count);
432
433 f = XLoadQueryFont (DISPLAY, name);
434
435 if (!f)
436 return false;
437
438 unsigned long value;
439
440 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
441 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
442
443 if (registry && encoding)
444 {
445 char charset[64];
446 snprintf (charset, 64, "%s-%s", registry, encoding);
447
448 cs = codeset_from_name (charset);
449 }
450 else
451 {
452 const char *charset = get_property (f, "FONT", 0);
453
454 if (!charset)
455 charset = name;
456
457 int count = 13;
458 while (*charset)
459 if (*charset++ == '-' && !--count)
460 break;
461
462 cs = codeset_from_name (charset);
463 }
464
465 if (cs == CS_UNICODE)
466 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
467
468 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
469 enc2b = encm || f->max_char_or_byte2 > 255;
470
471 ascent = f->ascent;
472 descent = f->descent;
473 height = ascent + descent;
474
475 slow = false;
476
477 if (f->min_bounds.width == f->max_bounds.width)
478 width = f->min_bounds.width;
479 else if (f->per_char == NULL)
480 width = f->max_bounds.width;
481 else
482 {
483 slow = true;
484
485 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
486
487 if (encm)
488 N += (f->max_byte1 - f->min_byte1)
489 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
490
491 while (N)
492 {
493 if (f->per_char[N].width > width)
494 width = f->per_char[N].width;
495
496 --N;
497 }
498 }
499
500 if (cs == CS_UNKNOWN)
501 {
502 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
503
504 clear ();
505
506 return false;
507 }
508
509 return true;
510 }
511
512 void
513 rxvt_font_x11::clear ()
514 {
515 if (f)
516 {
517 XFreeFont (DISPLAY, f);
518 f = 0;
519 }
520 }
521
522 bool
523 rxvt_font_x11::has_codepoint (uint32_t unicode)
524 {
525 uint32_t ch = FROM_UNICODE (cs, unicode);
526
527 if (ch == NOCHAR)
528 return false;
529
530 /* check wether the character exists in _this_ font. horrible. */
531 XCharStruct *xcs;
532
533 if (encm)
534 {
535 int byte1 = ch >> 8;
536 int byte2 = ch & 255;
537
538 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
539 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
540 return false;
541
542 if (!f->per_char)
543 return true;
544
545 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
546 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
547
548 xcs = f->per_char + N;
549 }
550 else
551 {
552 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
553 return false;
554
555 if (!f->per_char)
556 return true;
557
558 xcs = f->per_char + (ch - f->min_char_or_byte2);
559 }
560
561 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
562 && xcs->ascent == 0 && xcs->descent == 0)
563 return false;
564
565 return true;
566 }
567
568 void
569 rxvt_font_x11::draw (int x, int y,
570 const text_t *text, int len,
571 int fg, int bg)
572 {
573 // this looks like a mess /.
574 // and it is a mess /.
575 // yet we are trying to be perfect /.
576 // but the result still isn't perfect /.
577
578 bool slow = this->slow
579 || width != r->TermWin.fwidth
580 || height != r->TermWin.fheight;
581
582 int base = r->TermWin.fbase;
583
584 XGCValues v;
585 v.foreground = r->PixColors[fg];
586 v.background = r->PixColors[bg];
587 v.font = f->fid;
588
589 if (enc2b)
590 {
591 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
592
593 if (bg == Color_bg && !slow)
594 {
595 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
596 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
597 }
598 else
599 {
600 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
601
602 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
603
604 if (slow)
605 {
606 do
607 {
608 if (xc->byte1 || xc->byte2)
609 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
610
611 x += r->TermWin.fwidth;
612 xc++; len--;
613 }
614 while (len);
615 }
616 else
617 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
618 }
619 }
620 else
621 {
622 const char *xc = enc_char (text, len, cs, slow);
623
624 if (bg == Color_bg && !slow)
625 {
626 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
627 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
628 }
629 else
630 {
631 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
632
633 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
634
635 if (slow)
636 {
637 do
638 {
639 if (*xc)
640 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
641
642 x += r->TermWin.fwidth;
643 xc++; len--;
644 }
645 while (len);
646 }
647 else
648 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
649 }
650 }
651 }
652
653 /////////////////////////////////////////////////////////////////////////////
654
655 #if XFT
656 #if 0
657 #define UNIBITS 21
658 //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
659 #define SWATHBITS 8
660 #endif
661
662 struct rxvt_font_xft : rxvt_font {
663 #if 0
664 enum {
665 SWATHCOUNT = 1 << (21 - UNIBITS),
666 SWATHSIZE = 1 << (SWATHBITS - 5)
667 };
668 typedef uint32_t swath[SWATHSIZE];
669
670 swath *cvr[SWATHCOUNT];
671 #endif
672
673 #if 0
674 void gen_coverage_swath (unsigned int page);
675
676 bool has_char (uint32_t ch)
677 {
678 unsigned int page = ch >> SWATHBITS;
679 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
680
681 if (page >= SWATHCOUNT)
682 return false;
683
684 if (!cvr[page]) gen_coverage_swath (page);
685
686 return cvr[page][idx >> 5] & (1 << (idx & 31));
687 }
688 #endif
689 rxvt_font_xft () { f = 0; d = 0; }
690
691 void clear ();
692
693 rxvt_fontprop properties ();
694
695 bool load (const rxvt_fontprop &prop);
696
697 void draw (int x, int y,
698 const text_t *text, int len,
699 int fg, int bg);
700
701 bool has_codepoint (uint32_t unicode);
702
703 protected:
704 XftFont *f;
705 XftDraw *d;
706
707 #if 0
708 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
709 void set_swath (uint32_t ch)
710 {
711 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
712 }
713 #endif
714 };
715
716 void
717 rxvt_font_xft::clear ()
718 {
719 if (f)
720 {
721 XftFontClose (DISPLAY, f);
722 f = 0;
723 }
724
725 if (d)
726 {
727 XftDrawDestroy (d);
728 d = 0;
729 }
730
731 #if 0
732 for (int i = 0; i < SWATHCOUNT; i++)
733 delete cvr[i];
734 #endif
735 }
736
737 rxvt_fontprop
738 rxvt_font_xft::properties ()
739 {
740 rxvt_fontprop p;
741
742 FT_Face face = XftLockFace (f);
743
744 p.height = height;
745 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
746 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
747
748 XftUnlockFace (f);
749
750 return p;
751 }
752
753 bool
754 rxvt_font_xft::load (const rxvt_fontprop &prop)
755 {
756 #if 0
757 for (int i = 0; i < SWATHCOUNT; i++)
758 cvr[i] = 0;
759 #endif
760
761 clear ();
762
763 FcPattern *p = FcNameParse ((FcChar8 *) name);
764
765 if (!p)
766 return false;
767
768 FcValue v;
769
770 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
771 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
772
773 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
774 FcPatternAddInteger (p, FC_SLANT, prop.slant);
775
776 //FcPatternAddBool (p, FC_MINSPACE, 1);
777
778 XftResult result;
779 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
780
781 FcPatternDestroy (p);
782
783 if (!match)
784 return false;
785
786 f = XftFontOpenPattern (DISPLAY, match);
787
788 if (!f)
789 {
790 FcPatternDestroy (match);
791 return false;
792 }
793
794 FT_Face face = XftLockFace (f);
795
796 slow = !FT_IS_FIXED_WIDTH (face);
797
798 int ftheight = 0;
799
800 for (;;)
801 {
802 XGlyphInfo g1, g2;
803 FcChar8 c;
804
805 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
806 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
807
808 if (g1.xOff != g2.xOff) // don't simply trust the font
809 slow = true;
810
811 width = g2.xOff;
812 ascent = (face->size->metrics.ascender + 63) >> 6;
813 descent = (-face->size->metrics.descender + 63) >> 6;
814 height = ascent + descent;
815
816 if (height <= prop.height || !prop.height)
817 break;
818
819 if (ftheight)
820 {
821 // take smaller steps near the end
822 if (height > prop.height + 1) ftheight++;
823 if (height > prop.height + 2) ftheight++;
824 if (height > prop.height + 3) ftheight++;
825
826 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
827 }
828 else
829 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
830 }
831
832 XftUnlockFace (f);
833
834 return true;
835 }
836
837 #if 0
838 void rxvt_font::gen_coverage_swath (unsigned int page)
839 {
840 cvr[page] = new swath;
841
842 for (int i = 0; i < SWATHSIZE; i++)
843 cvr[page][i] = 0;
844
845 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
846 }
847 #endif
848
849 bool
850 rxvt_font_xft::has_codepoint (uint32_t unicode)
851 {
852 return XftCharExists (DISPLAY, f, unicode);
853 }
854
855 void
856 rxvt_font_xft::draw (int x, int y,
857 const text_t *text, int len,
858 int fg, int bg)
859 {
860 if (!d)
861 {
862 dR;
863 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
864 }
865
866 if (bg >= 0 && bg != Color_bg)
867 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
868 else
869 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
870
871 if (!slow && width == r->TermWin.fwidth)
872 {
873 if (sizeof (text_t) == sizeof (FcChar16))
874 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
875 else
876 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
877 }
878 else
879 {
880 while (len)
881 {
882 if (*text != NOCHAR && *text != ' ')
883 {
884 if (sizeof (text_t) == sizeof (FcChar16))
885 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1);
886 else
887 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1);
888 }
889
890 x += r->TermWin.fwidth;
891 text++;
892 len--;
893 }
894 }
895 }
896 #endif
897
898 /////////////////////////////////////////////////////////////////////////////
899
900 rxvt_fontset::rxvt_fontset (rxvt_t r)
901 #ifdef EXPLICIT_CONTEXT
902 : r(r)
903 #endif
904 {
905 clear ();
906 }
907
908 rxvt_fontset::~rxvt_fontset ()
909 {
910 clear ();
911 }
912
913 void
914 rxvt_fontset::clear ()
915 {
916 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
917 FONT_UNREF (*i);
918
919 fonts.clear ();
920 base_id = 0;
921 base_prop.height = 0x7fffffff;
922 base_prop.weight = rxvt_fontprop::medium;
923 base_prop.slant = rxvt_fontprop::roman;
924
925 fallback = fallback_fonts;
926 }
927
928 rxvt_font *
929 rxvt_fontset::new_font (const char *name, codeset cs)
930 {
931 rxvt_font *f;
932
933 if (!name || !*name)
934 {
935 name = "";
936 f = new rxvt_font_default;
937 }
938 #if XFT
939 else if (!strncmp (name, "xft:", 4))
940 {
941 name += 4;
942 f = new rxvt_font_xft;
943 }
944 #endif
945 else if (!strncmp (name, "x:", 2))
946 {
947 name += 2;
948 f = new rxvt_font_x11;
949 }
950 else
951 f = new rxvt_font_x11;
952
953 f->set_term (r);
954 f->set_name (strdup (name));
955
956 f->cs = cs;
957 f->loaded = false;
958
959 return f;
960 }
961
962 /////////////////////////////////////////////////////////////////////////////
963
964 void
965 rxvt_fontset::add_fonts (const char *desc)
966 {
967 if (desc)
968 {
969 char buf[512];
970 const char *end;
971
972 do
973 {
974 while (*desc <= ' ') desc++;
975
976 if (*desc == '[')
977 {
978 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
979
980 const char *extra = desc++;
981
982 desc = strchr (desc, ']');
983
984 if (!desc)
985 {
986 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
987 break;
988 }
989
990 desc++;
991 while (*desc <= ' ') desc++;
992 }
993
994 end = strchr (desc, ',');
995 if (!end)
996 end = desc + strlen (desc);
997
998 if (end - desc < 511)
999 {
1000 strncpy (buf, desc, end - desc);
1001 buf[end - desc] = 0;
1002
1003 fonts.push_back (new_font (buf, CS_UNICODE));
1004 }
1005
1006 desc = end + 1;
1007 }
1008 while (*end);
1009 }
1010 }
1011
1012 bool
1013 rxvt_fontset::realize_font (int i)
1014 {
1015 if (fonts[i]->loaded)
1016 return true;
1017
1018 fonts[i]->loaded = true;
1019
1020 if (!fonts[i]->load (base_prop))
1021 {
1022 fonts[i]->cs = CS_UNKNOWN;
1023 return false;
1024 }
1025
1026 return true;
1027 }
1028
1029 void
1030 rxvt_fontset::populate (const char *desc)
1031 {
1032 clear ();
1033
1034 fonts.push_back (new_font (0, CS_UNICODE));
1035 realize_font (0);
1036
1037 add_fonts (desc);
1038
1039 if (!base_id)
1040 base_id = 1;
1041
1042 // we currently need a base-font, no matter what
1043 if (fonts.size () <= base_id || !realize_font (base_id))
1044 {
1045 add_fonts ("fixed");
1046 base_id = fonts.size () - 1;
1047 }
1048
1049 if (fonts.size () <= base_id || !realize_font (base_id))
1050 {
1051 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1052 exit (1);
1053 }
1054
1055 base_prop = fonts[base_id]->properties ();
1056 }
1057
1058 int
1059 rxvt_fontset::find_font (uint32_t unicode)
1060 {
1061 for (int i = 0; i < fonts.size (); i++)
1062 {
1063 rxvt_font *f = fonts[i];
1064
1065 if (!f->loaded)
1066 {
1067 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1068 goto next_font;
1069
1070 if (!realize_font (i))
1071 goto next_font;
1072 }
1073
1074 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
1075 return i;
1076
1077 next_font:
1078 if (i == fonts.size () - 1 && fallback->name)
1079 {
1080 fonts.push_back (new_font (fallback->name, fallback->cs));
1081 fallback++;
1082 i = 0;
1083 }
1084 }
1085
1086 return 0; /* we must return SOME font */
1087 }
1088
1089
1090