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