ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.25
Committed: Tue Feb 24 23:58:12 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1-9, rel-1_9
Changes since 1.24: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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