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