1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/
22 #include "../config.h"
32 #define DISPLAY r->display->display
35 #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
37 const struct rxvt_fallback_font {
40 } fallback_fonts[] = {
41 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
42 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
43 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
47 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
48 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
50 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
51 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
52 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
53 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
54 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
55 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
56 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
57 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
58 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
59 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
60 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
61 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
62 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
65 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
67 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
68 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
69 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
70 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
71 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
72 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
73 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
78 #if ENCODING_JP || ENCODING_JP_EXT
80 // prefer xft for complex scripts
81 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
82 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
83 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
84 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
86 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
87 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
88 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
89 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
90 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
91 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
94 #if ENCODING_ZH || ENCODING_ZH_EXT
96 { CS_GBK_0, "xft:AR PL KaitiM GB" },
97 { CS_GBK_0, "xft:AR PL SungtiL GB" },
98 { CS_GBK_0, "xft::spacing=100:lang=zh" },
99 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
100 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
101 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
102 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
103 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
105 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
106 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
107 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
108 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
109 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
111 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
112 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
113 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
114 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
115 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
116 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
117 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
118 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
122 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
123 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
125 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
126 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
130 // generic font fallback
131 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
132 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
134 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
136 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
138 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
139 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
141 // FreeMono is usually uglier than x fonts, so try last only.
142 { CS_UNICODE, "xft:FreeMono:autohint=true" },
148 // these characters are used to guess the font height and width
149 // pango uses a similar algorithm and doesn't trust the font either.
150 static uint16_t extent_test_chars[] = {
151 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
152 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
153 0x304c, 0x672c, // が本
156 #define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
158 /////////////////////////////////////////////////////////////////////////////
161 rxvt_drawable::~rxvt_drawable ()
164 XftDrawDestroy (xftdrawable);
167 rxvt_drawable::operator XftDraw *()
170 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
176 /////////////////////////////////////////////////////////////////////////////
178 static void *enc_buf;
179 static uint32_t enc_len;
182 get_enc_buf (uint32_t len)
187 enc_buf = malloc (len);
195 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
197 uint8_t *buf = (uint8_t *)get_enc_buf (len);
201 uint32_t c = FROM_UNICODE (cs, *text++);
212 return (const char *)enc_buf;
215 static const XChar2b *
216 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
218 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
222 uint32_t c = FROM_UNICODE (cs, *text++);
235 return (XChar2b *)enc_buf;
238 /////////////////////////////////////////////////////////////////////////////
241 rxvt_font::set_name (char *name)
243 if (this->name == name)
246 if (this->name) free (this->name); // let the compiler optimize
251 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
253 if (color == Color_bg)
254 XClearArea (d.display->display, d, x, y, w, h, FALSE);
258 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h);
260 XSetForeground (d.display->display, TGC, r->pix_colors[color]);
261 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
266 #include "table/linedraw.h"
268 struct rxvt_font_default : rxvt_font {
269 struct rxvt_fontset *fs;
271 rxvt_font_default (rxvt_fontset *fs)
277 rxvt_fontprop properties ()
281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
283 p.weight = rxvt_fontprop::medium;
284 p.slant = rxvt_fontprop::roman;
289 bool load (const rxvt_fontprop &prop)
291 width = 1; height = 1;
292 ascent = 1; descent = 0;
294 set_name (strdup ("built-in support font"));
299 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
303 if (unicode <= 0x001f)
306 if (unicode <= 0x007f)
309 if (unicode <= 0x009f)
312 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 !r->option (Opt_skipBuiltinGlyphs))
316 if (IS_COMPOSE (unicode))
321 case ZERO_WIDTH_CHAR:
329 void draw (rxvt_drawable &d, int x, int y,
330 const text_t *text, int len,
335 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
336 const text_t *text, int len,
339 Display *disp = d.display->display;
341 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
343 XSetForeground (disp, TGC, r->pix_colors[fg]);
350 const text_t *tp = text;
353 while (++text, --len && *text == NOCHAR)
356 int width = text - tp;
357 int fwidth = r->fwidth * width;
359 if (0x2500 <= t && t <= 0x259f)
361 uint16_t offs = linedraw_offs[t - 0x2500];
362 uint32_t *a = linedraw_command + (offs >> 4);
363 uint32_t *b = a + (offs & 15);
371 for (int i = 0; i <= 8; i++)
373 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
374 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
377 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
378 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
382 gcv.cap_style = CapButt;
384 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
388 uint32_t command = *a++;
390 int op = (command >> 24) & 255;
391 int a = (command >> 20) & 15;
392 int b = (command >> 16) & 15;
393 int x1 = x_[(command >> 12) & 15];
394 int y1 = y_[(command >> 8) & 15];
395 int x2 = x_[(command >> 4) & 15];
396 int y2 = y_[(command >> 0) & 15];
401 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
404 case 1: // rectangle, possibly stippled
407 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
409 gcv.fill_style = FillStippled;
410 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
414 XChangeGC (disp, TGC,
415 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
419 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
423 XFreePixmap (disp, gcv.stipple);
425 gcv.fill_style = FillSolid;
426 XChangeGC (disp, TGC, GCFillStyle, &gcv);
430 XDrawArc (disp, d, TGC,
431 x1 - W/2, y1 - H/2, W-1, H-1,
432 (a - 1) * 90*64, (b - 1) * 90*64);
438 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
441 width = min (2, width);
445 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
446 f1->draw (d, x, y, chrs, width, fg, bg);
448 if (cc->c2 != NOCHAR)
452 // prefer font of first character, for no good reasons
454 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
456 : (*fs)[fs->find_font (cc->c2)];
458 f2->draw (d, x, y, chrs, width, fg, -1);
466 case ZERO_WIDTH_CHAR:
471 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
472 fwidth - 4, r->fheight - 4);
479 /////////////////////////////////////////////////////////////////////////////
481 struct rxvt_font_x11 : rxvt_font {
482 rxvt_font_x11 () { f = 0; }
486 rxvt_fontprop properties ();
488 bool load (const rxvt_fontprop &prop);
490 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
492 void draw (rxvt_drawable &d, int x, int y,
493 const text_t *text, int len,
496 bool slow; // wether this is a proportional font or has other funny characteristics
501 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
502 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
503 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
504 bool set_properties (rxvt_fontprop &p, const char *name);
508 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
512 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
513 return XGetAtomName (DISPLAY, value);
515 return rxvt_strdup (repl);
519 rxvt_font_x11::properties ()
522 set_properties (p, f);
527 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
529 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
531 p.ascent = rxvt_fontprop::unset;
532 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
533 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
539 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
541 unsigned long height;
544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
547 height = f->ascent + f->descent;
550 unsigned long avgwidth;
551 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
554 char *weight = get_property (f, "WEIGHT_NAME", "medium");
555 char *slant = get_property (f, "SLANT", "r");
557 set_properties (p, height, weight, slant, avgwidth);
562 p.ascent = f->ascent;
568 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
571 const char *comp[13];
573 for (const char *c = name; *c; c++)
576 comp[slashes++] = c + 1;
581 /* can we short-circuit the costly XLoadQueryFont? */
583 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
584 && (*comp[11] >= '0' && *comp[11] <= '9'))
585 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
587 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
591 // the font should really exist now. if not, we have a problem
592 // (e.g. if the user did xset fp rehash just when we were searching fonts).
593 // in that case, just return garbage.
594 bool ret = set_properties (p, f);
595 XFreeFont (DISPLAY, f);
602 // fix the size of scalable fonts
604 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
607 const char *field, *end;
609 for (const char *c = name; *c; c++)
612 if (slashes == index)
615 if (slashes == index + 1)
622 if (slashes >= 13 && (!old || *field == old))
624 // TODO: check for overflow in font-name
625 strncpy (buf, name, field - name);
627 strcpy (buf, replace);
641 rxvt_font_x11::load (const rxvt_fontprop &prop)
643 Display *disp = DISPLAY;
647 char field_str[64]; // enough for 128 bits
649 // first morph the font if required
650 if (prop.weight != rxvt_fontprop::unset
651 || prop.slant != rxvt_fontprop::unset)
657 f = XLoadQueryFont (disp, name);
662 char *new_name = get_property (f, "FONT", name);
667 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
673 if (prop.weight != rxvt_fontprop::unset)
675 replace_field (fname, name, 2, 0,
676 prop.weight < rxvt_fontprop::bold
677 ? "medium" : "bold");
678 set_name (strdup (fname));
681 if (prop.slant != rxvt_fontprop::unset)
683 replace_field (fname, name, 3, 0,
684 prop.slant < rxvt_fontprop::italic
685 ? "r" : "i"); // TODO: handle "o"blique, too
686 set_name (strdup (fname));
690 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
703 font_weight () { clear (); }
704 ~font_weight () { free (name); }
709 list = XListFonts (disp, name, 4000, &count);
716 font_weight *fonts = new font_weight[count];
718 for (int i = 0; i < count; i++)
725 if (replace_field (fname, list[i], 6, '0', field_str))
726 diff += 10; // slightly penalize scalable fonts
727 else if (replace_field (fname, list[i], 11, '0', "0"))
728 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
730 if (!set_properties (p, fname))
733 if (prop.height != rxvt_fontprop::unset
734 && p.height > prop.height) // weed out too large fonts
737 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
738 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
739 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
740 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
742 fonts[i].name = strdup (fname);
743 fonts[i].diff = diff;
746 XFreeFontNames (list);
748 // this loop only iterates when the guessed font-size is too small
751 font_weight *best = fonts + count - 1;
753 for (font_weight *w = best; w-- > fonts; )
754 if (w->diff <= best->diff)
758 || !(f = XLoadQueryFont (disp, best->name)))
761 set_name (best->name);
765 descent = f->descent;
766 height = ascent + descent;
768 if (prop.height == rxvt_fontprop::unset
769 || height <= prop.height)
770 break; // font is ready for use
772 // PIXEL_SIZE small enough, but real height too large
781 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
782 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
784 if (registry && encoding)
787 snprintf (charset, 64, "%s-%s", registry, encoding);
789 cs = codeset_from_name (charset);
793 const char *charset = get_property (f, "FONT", 0);
800 if (*charset++ == '-' && !--count)
803 cs = codeset_from_name (charset);
809 if (cs == CS_UNICODE)
810 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
812 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
813 enc2b = encm || f->max_char_or_byte2 > 255;
817 #if 1 // only used for slow detection, TODO optimize
818 if (f->min_bounds.width == f->max_bounds.width)
819 width = f->min_bounds.width;
820 else if (f->per_char == NULL)
821 width = f->max_bounds.width;
826 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
829 N += (f->max_byte1 - f->min_byte1)
830 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
834 if (f->per_char[N].width > width)
835 width = f->per_char[N].width;
844 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
848 && FROM_UNICODE (cs, *t) == NOCHAR)
851 // ignore characters we wouldn't use anyways
853 if (!has_char (*t, &prop, careful))
856 XChar2b ch = { *t >> 8, *t };
859 int dir_ret, asc_ret, des_ret;
860 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
862 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
864 if (width < g.width) width = g.width;
867 if (cs == CS_UNKNOWN)
869 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
875 #if 0 // do it per-character
876 if (prop && width > prop->width)
887 rxvt_font_x11::clear ()
891 XFreeFont (DISPLAY, f);
897 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
899 uint32_t ch = FROM_UNICODE (cs, unicode);
904 /* check wether the character exists in _this_ font. horrible. */
909 unsigned char byte1 = ch >> 8;
910 unsigned char byte2 = ch & 255;
912 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
913 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
918 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
919 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
921 xcs = f->per_char + N;
924 xcs = &f->max_bounds;
928 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
932 xcs = f->per_char + (ch - f->min_char_or_byte2);
934 xcs = &f->max_bounds;
937 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
938 && xcs->ascent == 0 && xcs->descent == 0)
941 if (!prop || prop->width == rxvt_fontprop::unset)
944 // check character against base font bounding box
946 int wcw = wcwidth (unicode);
947 if (wcw > 0) w = (w + wcw - 1) / wcw;
949 careful = w > prop->width;
950 if (careful && w > prop->width * MAX_OVERLAP >> 2)
957 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
958 const text_t *text, int len,
961 // this looks like a mess /.
962 // and it is a mess /.
963 // yet we are trying to be perfect /.
964 // but the result still isn't perfect /.
966 bool slow = this->slow
967 || width != r->fwidth
968 || height != r->fheight;
970 int base = ascent; // sorry, incorrect: r->fbase;
973 v.foreground = r->pix_colors[fg];
978 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
980 if (bg == Color_bg && !slow)
982 v.background = r->pix_colors[bg];
983 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
984 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
988 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
990 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
996 if (xc->byte1 || xc->byte2)
997 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
1005 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
1010 const char *xc = enc_char (text, len, cs, slow);
1012 if (bg == Color_bg && !slow)
1014 v.background = r->pix_colors[bg];
1015 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1016 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1020 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1022 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1029 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1037 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
1042 /////////////////////////////////////////////////////////////////////////////
1046 struct rxvt_font_xft : rxvt_font {
1047 rxvt_font_xft () { f = 0; }
1051 rxvt_fontprop properties ();
1053 bool load (const rxvt_fontprop &prop);
1055 void draw (rxvt_drawable &d, int x, int y,
1056 const text_t *text, int len,
1059 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
1066 rxvt_font_xft::clear ()
1070 XftFontClose (DISPLAY, f);
1076 rxvt_font_xft::properties ()
1080 FT_Face face = XftLockFace (f);
1085 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1086 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1087 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1088 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1096 rxvt_font_xft::load (const rxvt_fontprop &prop)
1098 Display *disp = DISPLAY;
1102 FcPattern *p = FcNameParse ((FcChar8 *) name);
1109 if (prop.height != rxvt_fontprop::unset
1110 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1111 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1112 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1114 if (prop.weight != rxvt_fontprop::unset
1115 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1116 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1118 if (prop.slant != rxvt_fontprop::unset
1119 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1120 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1122 #if 0 // clipping unfortunately destroys our precious double-width-characters
1123 // clip width, we can't do better, or can we?
1124 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1125 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1128 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1129 FcPatternAddBool (p, FC_MINSPACE, 1);
1131 // store generated name so iso14755 view gives better results
1132 set_name ((char *)FcNameUnparse (p));
1135 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1137 FcPatternDestroy (p);
1143 bool success = true;
1147 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1155 FT_Face face = XftLockFace (f);
1157 ascent = (face->size->metrics.ascender + 63) >> 6;
1158 descent = (-face->size->metrics.descender + 63) >> 6;
1159 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1162 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1166 int glheight = height;
1168 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1172 if (cs != CS_UNICODE
1174 && FROM_UNICODE (cs, ch) == NOCHAR)
1177 // ignore characters we wouldn't use anyways
1179 if (!has_char (*t, &prop, careful))
1183 XftTextExtents16 (disp, f, &ch, 1, &g);
1187 int wcw = wcwidth (ch);
1188 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1190 if (width < g.width ) width = g.width;
1191 if (height < g.height ) height = g.height;
1192 if (glheight < g.height - g.y) glheight = g.height - g.y;
1197 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1199 XftFontClose (disp, f);
1206 if (prop.height == rxvt_fontprop::unset
1207 || (height <= prop.height && glheight <= prop.height)
1214 // take smaller steps near the end
1215 if (height > prop.height + 1) ftheight++;
1216 if (height > prop.height + 2) ftheight++;
1217 if (height > prop.height + 3) ftheight++;
1219 ftheight -= height - prop.height;
1222 ftheight = prop.height - 1;
1224 XftFontClose (disp, f);
1225 FcPatternDel (match, FC_PIXEL_SIZE);
1226 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1229 FcPatternDestroy (match);
1231 #if 0 // do it per-character
1232 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1243 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1247 if (!XftCharExists (DISPLAY, f, unicode))
1250 if (!prop || prop->width == rxvt_fontprop::unset)
1253 // check character against base font bounding box
1254 FcChar32 ch = unicode;
1256 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1258 int w = g.width - g.x;
1259 int wcw = wcwidth (unicode);
1260 if (wcw > 0) w = (w + wcw - 1) / wcw;
1262 careful = w > prop->width;
1263 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1270 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1271 const text_t *text, int len,
1274 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1276 int base = ascent; // should be fbase, but that is incorrect
1279 XftGlyphSpec *enc = (XftGlyphSpec *) get_enc_buf (len * sizeof (XftGlyphSpec));
1280 XftGlyphSpec *ep = enc;
1282 // skip trailing spaces
1283 while (len && text [len - 1] == ' ')
1288 int cwidth = r->fwidth;
1289 FcChar32 fc = *text++; len--;
1291 while (len && *text == NOCHAR)
1292 text++, len--, cwidth += r->fwidth;
1294 if (fc != ' ') // skip spaces
1296 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1297 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1300 ep->x = x + (cwidth - extents.xOff >> 1);
1309 XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc);
1313 /////////////////////////////////////////////////////////////////////////////
1315 rxvt_fontset::rxvt_fontset (rxvt_t r)
1316 : fontdesc (0), r (r)
1321 rxvt_fontset::~rxvt_fontset ()
1327 rxvt_fontset::clear ()
1329 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1330 = rxvt_fontprop::unset;
1332 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1335 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1338 free (fontdesc); fontdesc = 0;
1342 fallback = fallback_fonts;
1346 rxvt_fontset::new_font (const char *name, codeset cs)
1350 if (!name || !*name)
1353 f = new rxvt_font_default (this);
1356 else if (!strncmp (name, "xft:", 4))
1359 f = new rxvt_font_xft ();
1362 else if (!strncmp (name, "x:", 2))
1365 f = new rxvt_font_x11;
1368 f = new rxvt_font_x11;
1371 f->set_name (strdup (name));
1379 /////////////////////////////////////////////////////////////////////////////
1382 rxvt_fontset::add_fonts (const char *desc)
1391 while (*desc && *desc <= ' ')
1394 codeset cs = CS_UNICODE;
1399 const char *extra = ++desc; // not yet used
1401 desc = strchr (desc, ']');
1405 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1409 memcpy (spec, extra, min (desc - extra, 255));
1410 spec[min (desc - extra, 255)] = 0;
1412 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1413 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1415 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1418 while (*desc <= ' ') desc++;
1421 end = strchr (desc, ',');
1423 end = desc + strlen (desc);
1425 if (end - desc < 511)
1427 strncpy (buf, desc, end - desc);
1428 buf[end - desc] = 0;
1430 fonts.push_back (new_font (buf, cs));
1433 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1442 rxvt_fontset::realize_font (int i)
1444 if (i < 0 || i >= fonts.size ())
1447 if (fonts[i]->loaded)
1450 fonts[i]->loaded = true;
1452 if (!fonts[i]->load (prop))
1454 fonts[i]->cs = CS_UNKNOWN;
1462 rxvt_fontset::populate (const char *desc)
1466 fontdesc = strdup (desc);
1468 fonts.push_back (new_font (0, CS_UNICODE));
1477 rxvt_fontset::find_font (const char *name) const
1479 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1480 if ((*f)->name && !strcmp ((*f)->name, name))
1481 return f - fonts.begin ();
1487 rxvt_fontset::find_font (unicode_t unicode)
1489 if (unicode >= 1<<20)
1492 unicode_t hi = unicode >> 8;
1494 if (hi < fmap.size ()
1496 && (*fmap[hi])[unicode & 0xff] != 0xff)
1497 return (*fmap[hi])[unicode & 0xff];
1501 for (i = 0; i < fonts.size (); i++)
1503 rxvt_font *f = fonts[i];
1507 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1510 if (!realize_font (i))
1513 if (prop.ascent != rxvt_fontprop::unset)
1514 max_it (f->ascent, prop.ascent);
1517 if (f->cs == CS_UNKNOWN)
1521 if (f->has_char (unicode, &prop, careful))
1530 if (i == fonts.size () - 1)
1534 // search through the fallback list
1535 fonts.push_back (new_font (fallback->name, fallback->cs));
1540 // try to find a new font.
1541 // only xft currently supported, as there is no
1542 // way to configure this and xft is easier to hack in,
1543 // while x11 has more framework in place already.
1544 // TODO: this is a real resource hog, xft takes ages(?)
1545 #if XFT && USE_SLOW_LOOKUP
1546 // grab the first xft font that seems suitable
1547 FcPattern *p = FcPatternCreate ();
1549 FcCharSet *s = FcCharSetCreate ();
1550 FcCharSetAddChar (s, unicode);
1551 FcPatternAddCharSet (p, FC_CHARSET, s);
1552 // charsets don't help that much, as xft might return
1553 // a non-matching font even if a better font is available :/
1555 x x x x TODO prop might have unset contents
1556 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1557 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1558 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1559 FcPatternAddBool (p, FC_MINSPACE, 1);
1560 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1563 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1565 FcPatternDestroy (p);
1569 FcPatternDel (match, FC_CHARSET);
1570 char *font = (char *)FcNameUnparse (match);
1571 FcPatternDestroy (match);
1573 if (find_font (font) < 0)
1575 char fontname[4096];
1576 sprintf (fontname, "xft:%-.4090s", font);
1578 fonts.push_back (new_font (fontname, CS_UNICODE));
1588 /* we must return SOME font */
1592 // found a font, cache it
1595 while (hi >= fmap.size ())
1600 fmap[hi] = (pagemap *)new pagemap;
1601 memset (fmap[hi], 0xff, sizeof (pagemap));
1604 (*fmap[hi])[unicode & 0xff] = i;