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