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