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