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