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 ! OPTION_R (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 &&
360 ! OPTION_R (Opt_skipBuiltinGlyphs))
362 uint16_t offs = linedraw_offs[t - 0x2500];
363 uint32_t *a = linedraw_command + (offs >> 4);
364 uint32_t *b = a + (offs & 15);
372 for (int i = 0; i <= 8; i++)
374 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
375 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
378 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
379 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
383 gcv.cap_style = CapButt;
385 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
389 uint32_t command = *a++;
391 int op = (command >> 24) & 255;
392 int a = (command >> 20) & 15;
393 int b = (command >> 16) & 15;
394 int x1 = x_[(command >> 12) & 15];
395 int y1 = y_[(command >> 8) & 15];
396 int x2 = x_[(command >> 4) & 15];
397 int y2 = y_[(command >> 0) & 15];
402 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
405 case 1: // rectangle, possibly stippled
408 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
410 gcv.fill_style = FillStippled;
411 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
415 XChangeGC (disp, TGC,
416 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
420 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
424 XFreePixmap (disp, gcv.stipple);
426 gcv.fill_style = FillSolid;
427 XChangeGC (disp, TGC, GCFillStyle, &gcv);
431 XDrawArc (disp, d, TGC,
432 x1 - W/2, y1 - H/2, W-1, H-1,
433 (a - 1) * 90*64, (b - 1) * 90*64);
439 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
442 width = min (2, width);
446 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
447 f1->draw (d, x, y, chrs, width, fg, bg);
449 if (cc->c2 != NOCHAR)
453 // prefer font of first character, for no good reasons
455 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
457 : (*fs)[fs->find_font (cc->c2)];
459 f2->draw (d, x, y, chrs, width, fg, -1);
467 case ZERO_WIDTH_CHAR:
472 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
473 fwidth - 4, r->fheight - 4);
480 /////////////////////////////////////////////////////////////////////////////
482 struct rxvt_font_x11 : rxvt_font {
483 rxvt_font_x11 () { f = 0; }
487 rxvt_fontprop properties ();
489 bool load (const rxvt_fontprop &prop);
491 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
493 void draw (rxvt_drawable &d, int x, int y,
494 const text_t *text, int len,
497 bool slow; // wether this is a proportional font or has other funny characteristics
502 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
503 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
504 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
505 bool set_properties (rxvt_fontprop &p, const char *name);
509 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
513 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
514 return XGetAtomName (DISPLAY, value);
516 return rxvt_strdup (repl);
520 rxvt_font_x11::properties ()
523 set_properties (p, f);
528 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
530 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
532 p.ascent = rxvt_fontprop::unset;
533 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
534 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
540 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
542 unsigned long height;
545 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
548 height = f->ascent + f->descent;
551 unsigned long avgwidth;
552 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
555 char *weight = get_property (f, "WEIGHT_NAME", "medium");
556 char *slant = get_property (f, "SLANT", "r");
558 set_properties (p, height, weight, slant, avgwidth);
563 p.ascent = f->ascent;
569 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
572 const char *comp[13];
574 for (const char *c = name; *c; c++)
577 comp[slashes++] = c + 1;
582 /* can we short-circuit the costly XLoadQueryFont? */
584 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
585 && (*comp[11] >= '0' && *comp[11] <= '9'))
586 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
588 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
592 // the font should really exist now. if not, we have a problem
593 // (e.g. if the user did xset fp rehash just when we were searching fonts).
594 // in that case, just return garbage.
595 bool ret = set_properties (p, f);
596 XFreeFont (DISPLAY, f);
603 // fix the size of scalable fonts
605 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
608 const char *field, *end;
610 for (const char *c = name; *c; c++)
613 if (slashes == index)
616 if (slashes == index + 1)
623 if (slashes >= 13 && (!old || *field == old))
625 // TODO: check for overflow in font-name
626 strncpy (buf, name, field - name);
628 strcpy (buf, replace);
642 rxvt_font_x11::load (const rxvt_fontprop &prop)
644 Display *disp = DISPLAY;
648 char field_str[64]; // enough for 128 bits
650 // first morph the font if required
651 if (prop.weight != rxvt_fontprop::unset
652 || prop.slant != rxvt_fontprop::unset)
658 f = XLoadQueryFont (disp, name);
663 char *new_name = get_property (f, "FONT", name);
668 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
674 if (prop.weight != rxvt_fontprop::unset)
676 replace_field (fname, name, 2, 0,
677 prop.weight < rxvt_fontprop::bold
678 ? "medium" : "bold");
679 set_name (strdup (fname));
682 if (prop.slant != rxvt_fontprop::unset)
684 replace_field (fname, name, 3, 0,
685 prop.slant < rxvt_fontprop::italic
686 ? "r" : "i"); // TODO: handle "o"blique, too
687 set_name (strdup (fname));
691 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
704 font_weight () { clear (); }
705 ~font_weight () { free (name); }
710 list = XListFonts (disp, name, 4000, &count);
717 font_weight *fonts = new font_weight[count];
719 for (int i = 0; i < count; i++)
726 if (replace_field (fname, list[i], 6, '0', field_str))
727 diff += 10; // slightly penalize scalable fonts
728 else if (replace_field (fname, list[i], 11, '0', "0"))
729 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
731 if (!set_properties (p, fname))
734 if (prop.height != rxvt_fontprop::unset
735 && p.height > prop.height) // weed out too large fonts
738 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
739 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
740 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
741 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
743 fonts[i].name = strdup (fname);
744 fonts[i].diff = diff;
747 XFreeFontNames (list);
749 // this loop only iterates when the guessed font-size is too small
752 font_weight *best = fonts + count - 1;
754 for (font_weight *w = best; w-- > fonts; )
755 if (w->diff <= best->diff)
759 || !(f = XLoadQueryFont (disp, best->name)))
762 set_name (best->name);
766 descent = f->descent;
767 height = ascent + descent;
769 if (prop.height == rxvt_fontprop::unset
770 || height <= prop.height)
771 break; // font is ready for use
773 // PIXEL_SIZE small enough, but real height too large
782 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
783 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
785 if (registry && encoding)
788 snprintf (charset, 64, "%s-%s", registry, encoding);
790 cs = codeset_from_name (charset);
794 const char *charset = get_property (f, "FONT", 0);
801 if (*charset++ == '-' && !--count)
804 cs = codeset_from_name (charset);
810 if (cs == CS_UNICODE)
811 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
813 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
814 enc2b = encm || f->max_char_or_byte2 > 255;
818 #if 1 // only used for slow detection, TODO optimize
819 if (f->min_bounds.width == f->max_bounds.width)
820 width = f->min_bounds.width;
821 else if (f->per_char == NULL)
822 width = f->max_bounds.width;
827 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
830 N += (f->max_byte1 - f->min_byte1)
831 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
835 if (f->per_char[N].width > width)
836 width = f->per_char[N].width;
845 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
849 && FROM_UNICODE (cs, *t) == NOCHAR)
852 // ignore characters we wouldn't use anyways
854 if (!has_char (*t, &prop, careful))
857 XChar2b ch = { *t >> 8, *t };
860 int dir_ret, asc_ret, des_ret;
861 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
863 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
865 if (width < g.width) width = g.width;
868 if (cs == CS_UNKNOWN)
870 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
876 #if 0 // do it per-character
877 if (prop && width > prop->width)
888 rxvt_font_x11::clear ()
892 XFreeFont (DISPLAY, f);
898 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
900 uint32_t ch = FROM_UNICODE (cs, unicode);
905 /* check wether the character exists in _this_ font. horrible. */
910 unsigned char byte1 = ch >> 8;
911 unsigned char byte2 = ch & 255;
913 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
914 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
919 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
920 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
922 xcs = f->per_char + N;
925 xcs = &f->max_bounds;
929 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
933 xcs = f->per_char + (ch - f->min_char_or_byte2);
935 xcs = &f->max_bounds;
938 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
939 && xcs->ascent == 0 && xcs->descent == 0)
942 if (!prop || prop->width == rxvt_fontprop::unset)
945 // check character against base font bounding box
947 int wcw = wcwidth (unicode);
948 if (wcw > 0) w = (w + wcw - 1) / wcw;
950 careful = w > prop->width;
951 if (careful && w > prop->width * MAX_OVERLAP >> 2)
958 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
959 const text_t *text, int len,
962 // this looks like a mess /.
963 // and it is a mess /.
964 // yet we are trying to be perfect /.
965 // but the result still isn't perfect /.
967 bool slow = this->slow
968 || width != r->fwidth
969 || height != r->fheight;
971 int base = ascent; // sorry, incorrect: r->fbase;
974 v.foreground = r->pix_colors[fg];
979 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
981 if (bg == Color_bg && !slow)
983 v.background = r->pix_colors[bg];
984 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
985 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
989 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
991 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
997 if (xc->byte1 || xc->byte2)
998 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
1006 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
1011 const char *xc = enc_char (text, len, cs, slow);
1013 if (bg == Color_bg && !slow)
1015 v.background = r->pix_colors[bg];
1016 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1017 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1021 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1023 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1030 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1038 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
1043 /////////////////////////////////////////////////////////////////////////////
1047 struct rxvt_font_xft : rxvt_font {
1048 rxvt_font_xft () { f = 0; }
1052 rxvt_fontprop properties ();
1054 bool load (const rxvt_fontprop &prop);
1056 void draw (rxvt_drawable &d, int x, int y,
1057 const text_t *text, int len,
1060 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
1067 rxvt_font_xft::clear ()
1071 XftFontClose (DISPLAY, f);
1077 rxvt_font_xft::properties ()
1081 FT_Face face = XftLockFace (f);
1086 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1087 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1088 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1089 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1097 rxvt_font_xft::load (const rxvt_fontprop &prop)
1099 Display *disp = DISPLAY;
1103 FcPattern *p = FcNameParse ((FcChar8 *) name);
1110 if (prop.height != rxvt_fontprop::unset
1111 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1112 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1113 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1115 if (prop.weight != rxvt_fontprop::unset
1116 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1117 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1119 if (prop.slant != rxvt_fontprop::unset
1120 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1121 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1123 #if 0 // clipping unfortunately destroys our precious double-width-characters
1124 // clip width, we can't do better, or can we?
1125 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1126 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1129 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1130 FcPatternAddBool (p, FC_MINSPACE, 1);
1132 // store generated name so iso14755 view gives better results
1133 set_name ((char *)FcNameUnparse (p));
1136 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1138 FcPatternDestroy (p);
1144 bool success = true;
1148 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1156 FT_Face face = XftLockFace (f);
1158 ascent = (face->size->metrics.ascender + 63) >> 6;
1159 descent = (-face->size->metrics.descender + 63) >> 6;
1160 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1163 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1167 int glheight = height;
1169 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1173 if (cs != CS_UNICODE
1175 && FROM_UNICODE (cs, ch) == NOCHAR)
1178 // ignore characters we wouldn't use anyways
1180 if (!has_char (*t, &prop, careful))
1184 XftTextExtents16 (disp, f, &ch, 1, &g);
1188 int wcw = wcwidth (ch);
1189 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1191 if (width < g.width ) width = g.width;
1192 if (height < g.height ) height = g.height;
1193 if (glheight < g.height - g.y) glheight = g.height - g.y;
1198 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1200 XftFontClose (disp, f);
1207 if (prop.height == rxvt_fontprop::unset
1208 || (height <= prop.height && glheight <= prop.height)
1215 // take smaller steps near the end
1216 if (height > prop.height + 1) ftheight++;
1217 if (height > prop.height + 2) ftheight++;
1218 if (height > prop.height + 3) ftheight++;
1220 ftheight -= height - prop.height;
1223 ftheight = prop.height - 1;
1225 XftFontClose (disp, f);
1226 FcPatternDel (match, FC_PIXEL_SIZE);
1227 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1230 FcPatternDestroy (match);
1232 #if 0 // do it per-character
1233 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1244 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1248 if (!XftCharExists (DISPLAY, f, unicode))
1251 if (!prop || prop->width == rxvt_fontprop::unset)
1254 // check character against base font bounding box
1255 FcChar32 ch = unicode;
1257 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1259 int w = g.width - g.x;
1260 int wcw = wcwidth (unicode);
1261 if (wcw > 0) w = (w + wcw - 1) / wcw;
1263 careful = w > prop->width;
1264 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1271 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1272 const text_t *text, int len,
1275 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1277 int base = ascent; // should be fbase, but that is incorrect
1280 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1286 int cwidth = r->fwidth;
1287 FcChar32 fc = *text++; len--;
1289 while (len && *text == NOCHAR)
1290 text++, len--, cwidth += r->fwidth;
1292 if (fc == ' ' && ep == enc) // skip leading spaces
1296 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1297 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1299 if (extents.xOff != cwidth)
1303 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1304 x, y + base, enc, ep - enc);
1311 if (extents.xOff > cwidth)
1312 extents.xOff = cwidth;
1314 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1315 x + (cwidth - extents.xOff >> 1),
1328 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1329 x, y + base, enc, ep - enc);
1333 /////////////////////////////////////////////////////////////////////////////
1335 rxvt_fontset::rxvt_fontset (rxvt_t r)
1336 : fontdesc (0), r (r)
1341 rxvt_fontset::~rxvt_fontset ()
1347 rxvt_fontset::clear ()
1349 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1350 = rxvt_fontprop::unset;
1352 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1355 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1358 free (fontdesc); fontdesc = 0;
1362 fallback = fallback_fonts;
1366 rxvt_fontset::new_font (const char *name, codeset cs)
1370 if (!name || !*name)
1373 f = new rxvt_font_default (this);
1376 else if (!strncmp (name, "xft:", 4))
1379 f = new rxvt_font_xft ();
1382 else if (!strncmp (name, "x:", 2))
1385 f = new rxvt_font_x11;
1388 f = new rxvt_font_x11;
1391 f->set_name (strdup (name));
1399 /////////////////////////////////////////////////////////////////////////////
1402 rxvt_fontset::add_fonts (const char *desc)
1411 while (*desc && *desc <= ' ')
1414 codeset cs = CS_UNICODE;
1419 const char *extra = ++desc; // not yet used
1421 desc = strchr (desc, ']');
1425 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1429 memcpy (spec, extra, min (desc - extra, 255));
1430 spec[min (desc - extra, 255)] = 0;
1432 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1433 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1435 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1438 while (*desc <= ' ') desc++;
1441 end = strchr (desc, ',');
1443 end = desc + strlen (desc);
1445 if (end - desc < 511)
1447 strncpy (buf, desc, end - desc);
1448 buf[end - desc] = 0;
1450 fonts.push_back (new_font (buf, cs));
1453 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1462 rxvt_fontset::realize_font (int i)
1464 if (i < 0 || i >= fonts.size ())
1467 if (fonts[i]->loaded)
1470 fonts[i]->loaded = true;
1472 if (!fonts[i]->load (prop))
1474 fonts[i]->cs = CS_UNKNOWN;
1482 rxvt_fontset::populate (const char *desc)
1486 fontdesc = strdup (desc);
1488 fonts.push_back (new_font (0, CS_UNICODE));
1497 rxvt_fontset::find_font (const char *name) const
1499 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1500 if ((*f)->name && !strcmp ((*f)->name, name))
1501 return f - fonts.begin ();
1507 rxvt_fontset::find_font (unicode_t unicode)
1509 if (unicode >= 1<<20)
1512 unicode_t hi = unicode >> 8;
1514 if (hi < fmap.size ()
1516 && (*fmap[hi])[unicode & 0xff] != 0xff)
1517 return (*fmap[hi])[unicode & 0xff];
1521 for (i = 0; i < fonts.size (); i++)
1523 rxvt_font *f = fonts[i];
1527 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1530 if (!realize_font (i))
1533 if (prop.ascent != rxvt_fontprop::unset)
1534 max_it (f->ascent, prop.ascent);
1537 if (f->cs == CS_UNKNOWN)
1541 if (f->has_char (unicode, &prop, careful))
1550 if (i == fonts.size () - 1)
1554 // search through the fallback list
1555 fonts.push_back (new_font (fallback->name, fallback->cs));
1560 // try to find a new font.
1561 // only xft currently supported, as there is no
1562 // way to configure this and xft is easier to hack in,
1563 // while x11 has more framework in place already.
1564 // TODO: this is a real resource hog, xft takes ages(?)
1565 #if XFT && USE_SLOW_LOOKUP
1566 // grab the first xft font that seems suitable
1567 FcPattern *p = FcPatternCreate ();
1569 FcCharSet *s = FcCharSetCreate ();
1570 FcCharSetAddChar (s, unicode);
1571 FcPatternAddCharSet (p, FC_CHARSET, s);
1572 // charsets don't help that much, as xft might return
1573 // a non-matching font even if a better font is available :/
1575 x x x x TODO prop might have unset contents
1576 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1577 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1578 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1579 FcPatternAddBool (p, FC_MINSPACE, 1);
1580 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1583 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1585 FcPatternDestroy (p);
1589 FcPatternDel (match, FC_CHARSET);
1590 char *font = (char *)FcNameUnparse (match);
1591 FcPatternDestroy (match);
1593 if (find_font (font) < 0)
1595 char fontname[4096];
1596 sprintf (fontname, "xft:%-.4090s", font);
1598 fonts.push_back (new_font (fontname, CS_UNICODE));
1608 /* we must return SOME font */
1612 // found a font, cache it
1615 while (hi >= fmap.size ())
1620 fmap[hi] = (pagemap *)new pagemap;
1621 memset (fmap[hi], 0xff, sizeof (pagemap));
1624 (*fmap[hi])[unicode & 0xff] = i;