1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 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"
31 #define DISPLAY r->display->display
32 #define TGC r->TermWin.gc
34 #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
36 const struct rxvt_fallback_font {
39 } fallback_fonts[] = {
40 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
41 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
42 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
46 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
47 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
49 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
50 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
51 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
52 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
53 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
54 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
55 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
56 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
57 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
58 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
59 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
60 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
61 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
64 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
66 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
67 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
68 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
69 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
70 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
71 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
72 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
77 #if ENCODING_JP || ENCODING_JP_EXT
79 // prefer xft for complex scripts
80 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
81 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
82 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
83 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
85 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
86 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
87 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
88 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
89 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
93 #if ENCODING_ZH || ENCODING_ZH_EXT
95 { CS_GBK_0, "xft:AR PL KaitiM GB" },
96 { CS_GBK_0, "xft:AR PL SungtiL GB" },
97 { CS_GBK_0, "xft::spacing=100:lang=zh" },
98 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
99 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
100 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
101 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
102 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
104 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
105 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
106 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
107 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
108 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
109 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
111 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
112 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
113 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
114 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
115 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
116 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
117 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
121 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
122 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
124 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
125 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
129 // generic font fallback
130 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
131 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
132 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
135 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
136 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
138 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
140 // FreeMono is usually uglier than x fonts, so try last only.
141 { CS_UNICODE, "xft:FreeMono:autohint=true" },
147 // these characters are used to guess the font height and width
148 // pango uses a similar algorithm and doesn't trust the font either.
149 static uint16_t extent_test_chars[] = {
150 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
151 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
152 0x304c, 0x672c, // が本
155 #define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
157 /////////////////////////////////////////////////////////////////////////////
160 rxvt_drawable::~rxvt_drawable ()
163 XftDrawDestroy (xftdrawable);
166 rxvt_drawable::operator XftDraw *()
169 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
175 /////////////////////////////////////////////////////////////////////////////
177 static void *enc_buf;
178 static uint32_t enc_len;
181 get_enc_buf (uint32_t len)
186 enc_buf = malloc (len);
194 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
196 uint8_t *buf = (uint8_t *)get_enc_buf (len);
200 uint32_t c = FROM_UNICODE (cs, *text++);
211 return (const char *)enc_buf;
214 static const XChar2b *
215 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
217 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
221 uint32_t c = FROM_UNICODE (cs, *text++);
234 return (XChar2b *)enc_buf;
237 /////////////////////////////////////////////////////////////////////////////
240 rxvt_font::set_name (char *name)
242 if (this->name == name)
245 if (this->name) free (this->name); // let the compiler optimize
250 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
252 if (color == Color_bg)
253 XClearArea (d.display->display, d, x, y, w, h, FALSE);
257 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h);
259 XSetForeground (d.display->display, TGC, r->pix_colors[color]);
260 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
265 #include "table/linedraw.h"
267 struct rxvt_font_default : rxvt_font {
268 struct rxvt_fontset *fs;
270 rxvt_font_default (rxvt_fontset *fs)
276 rxvt_fontprop properties ()
280 p.width = p.height = 1;
281 p.weight = rxvt_fontprop::medium;
282 p.slant = rxvt_fontprop::roman;
287 bool load (const rxvt_fontprop &prop)
289 width = 1; height = 1;
290 ascent = 1; descent = 0;
292 set_name (strdup ("built-in support font"));
297 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
301 if (unicode <= 0x001f)
304 if (unicode <= 0x007f)
307 if (unicode <= 0x009f)
310 if (unicode >= 0x2500 && unicode <= 0x259f)
313 if (IS_COMPOSE (unicode))
318 case ZERO_WIDTH_CHAR:
326 void draw (rxvt_drawable &d, int x, int y,
327 const text_t *text, int len,
332 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
333 const text_t *text, int len,
336 Display *disp = d.display->display;
338 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
340 XSetForeground (disp, TGC, r->pix_colors[fg]);
347 const text_t *tp = text;
350 while (++text, --len && *text == NOCHAR)
353 int width = text - tp;
354 int fwidth = r->TermWin.fwidth * width;
356 if (0x2500 <= t && t <= 0x259f)
358 uint16_t offs = linedraw_offs[t - 0x2500];
359 uint32_t *a = linedraw_command + (offs >> 4);
360 uint32_t *b = a + (offs & 15);
363 int H = r->TermWin.fheight;
368 for (int i = 0; i <= 8; i++)
370 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
371 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
374 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
375 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
379 gcv.cap_style = CapButt;
381 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
385 uint32_t command = *a++;
387 int op = (command >> 24) & 255;
388 int a = (command >> 20) & 15;
389 int b = (command >> 16) & 15;
390 int x1 = x_[(command >> 12) & 15];
391 int y1 = y_[(command >> 8) & 15];
392 int x2 = x_[(command >> 4) & 15];
393 int y2 = y_[(command >> 0) & 15];
398 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
401 case 1: // rectangle, possibly stippled
404 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
406 gcv.fill_style = FillStippled;
407 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
411 XChangeGC (disp, TGC,
412 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
416 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
420 XFreePixmap (disp, gcv.stipple);
422 gcv.fill_style = FillSolid;
423 XChangeGC (disp, TGC, GCFillStyle, &gcv);
427 XDrawArc (disp, d, TGC,
428 x1 - W/2, y1 - H/2, W-1, H-1,
429 (a - 1) * 90*64, (b - 1) * 90*64);
435 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
438 width = min (2, width);
442 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
443 f1->draw (d, x, y, chrs, width, fg, bg);
445 if (cc->c2 != NOCHAR)
449 // prefer font of first character, for no good reasons
451 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
453 : (*fs)[fs->find_font (cc->c2)];
455 f2->draw (d, x, y, chrs, width, fg, -1);
463 case ZERO_WIDTH_CHAR:
468 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
469 fwidth - 4, r->TermWin.fheight - 4);
476 /////////////////////////////////////////////////////////////////////////////
478 struct rxvt_font_x11 : rxvt_font {
479 rxvt_font_x11 () { f = 0; }
483 rxvt_fontprop properties ();
485 bool load (const rxvt_fontprop &prop);
487 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
489 void draw (rxvt_drawable &d, int x, int y,
490 const text_t *text, int len,
493 bool slow; // wether this is a proportional font or has other funny characteristics
498 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
499 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
500 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
501 bool set_properties (rxvt_fontprop &p, const char *name);
505 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
509 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
510 return XGetAtomName (DISPLAY, value);
512 return rxvt_strdup (repl);
516 rxvt_font_x11::properties ()
519 set_properties (p, f);
524 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
526 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
528 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
529 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
535 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
537 unsigned long height;
540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
543 height = f->ascent + f->descent;
546 unsigned long avgwidth;
547 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
550 char *weight = get_property (f, "WEIGHT_NAME", "medium");
551 char *slant = get_property (f, "SLANT", "r");
553 set_properties (p, height, weight, slant, avgwidth);
562 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
565 const char *comp[13];
567 for (const char *c = name; *c; c++)
570 comp[slashes++] = c + 1;
575 /* can we short-circuit the costly XLoadQueryFont? */
577 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
578 && (*comp[11] >= '0' && *comp[11] <= '9'))
579 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
581 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
585 // the font should really exist now. if not, we have a problem
586 // (e.g. if the user did xset fp rehash just when we were searching fonts).
587 // in that case, just return garbage.
588 bool ret = set_properties (p, f);
589 XFreeFont (DISPLAY, f);
596 // fix the size of scalable fonts
598 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
601 const char *field, *end;
603 for (const char *c = name; *c; c++)
606 if (slashes == index)
609 if (slashes == index + 1)
616 if (slashes >= 13 && (!old || *field == old))
618 // TODO: check for overflow in font-name
619 strncpy (buf, name, field - name);
621 strcpy (buf, replace);
635 rxvt_font_x11::load (const rxvt_fontprop &prop)
637 Display *disp = DISPLAY;
641 char field_str[64]; // enough for 128 bits
643 // first morph the font if required
644 if (prop.weight != rxvt_fontprop::unset
645 || prop.slant != rxvt_fontprop::unset)
651 f = XLoadQueryFont (disp, name);
656 char *new_name = get_property (f, "FONT", name);
661 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
667 if (prop.weight != rxvt_fontprop::unset)
669 replace_field (fname, name, 2, 0,
670 prop.weight < rxvt_fontprop::bold
671 ? "medium" : "bold");
672 set_name (strdup (fname));
675 if (prop.slant != rxvt_fontprop::unset)
677 replace_field (fname, name, 3, 0,
678 prop.slant < rxvt_fontprop::italic
679 ? "r" : "i"); // TODO: handle "o"blique, too
680 set_name (strdup (fname));
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
697 font_weight () { clear (); }
698 ~font_weight () { free (name); }
703 list = XListFonts (disp, name, 4000, &count);
710 font_weight *fonts = new font_weight[count];
712 for (int i = 0; i < count; i++)
719 if (replace_field (fname, list[i], 6, '0', field_str))
720 diff += 10; // slightly penalize scalable fonts
721 else if (replace_field (fname, list[i], 11, '0', "0"))
722 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
724 if (!set_properties (p, fname))
727 if (prop.height != rxvt_fontprop::unset
728 && p.height > prop.height) // weed out too large fonts
731 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
732 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
733 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
734 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
736 fonts[i].name = strdup (fname);
737 fonts[i].diff = diff;
740 XFreeFontNames (list);
742 // this loop only iterates when the guessed font-size is too small
745 font_weight *best = fonts + count - 1;
747 for (font_weight *w = best; w-- > fonts; )
748 if (w->diff <= best->diff)
752 || !(f = XLoadQueryFont (disp, best->name)))
755 set_name (best->name);
759 descent = f->descent;
760 height = ascent + descent;
762 if (prop.height == rxvt_fontprop::unset
763 || height <= prop.height)
764 break; // font is ready for use
766 // PIXEL_SIZE small enough, but real height too large
775 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
776 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
778 if (registry && encoding)
781 snprintf (charset, 64, "%s-%s", registry, encoding);
783 cs = codeset_from_name (charset);
787 const char *charset = get_property (f, "FONT", 0);
794 if (*charset++ == '-' && !--count)
797 cs = codeset_from_name (charset);
803 if (cs == CS_UNICODE)
804 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
806 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
807 enc2b = encm || f->max_char_or_byte2 > 255;
811 #if 1 // only used for slow detection, TODO optimize
812 if (f->min_bounds.width == f->max_bounds.width)
813 width = f->min_bounds.width;
814 else if (f->per_char == NULL)
815 width = f->max_bounds.width;
820 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
823 N += (f->max_byte1 - f->min_byte1)
824 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
828 if (f->per_char[N].width > width)
829 width = f->per_char[N].width;
838 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
842 && FROM_UNICODE (cs, *t) == NOCHAR)
845 // ignore characters we wouldn't use anyways
847 if (!has_char (*t, &prop, careful))
850 XChar2b ch = { *t >> 8, *t };
853 int dir_ret, asc_ret, des_ret;
854 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
856 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
858 if (width < g.width) width = g.width;
861 if (cs == CS_UNKNOWN)
863 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
869 #if 0 // do it per-character
870 if (prop && width > prop->width)
881 rxvt_font_x11::clear ()
885 XFreeFont (DISPLAY, f);
891 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
893 uint32_t ch = FROM_UNICODE (cs, unicode);
898 /* check wether the character exists in _this_ font. horrible. */
903 unsigned char byte1 = ch >> 8;
904 unsigned char byte2 = ch & 255;
906 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
907 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
912 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
913 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
915 xcs = f->per_char + N;
918 xcs = &f->max_bounds;
922 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
926 xcs = f->per_char + (ch - f->min_char_or_byte2);
928 xcs = &f->max_bounds;
931 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
932 && xcs->ascent == 0 && xcs->descent == 0)
935 if (!prop || prop->width == rxvt_fontprop::unset)
938 // check character against base font bounding box
940 int wcw = wcwidth (unicode);
941 if (wcw > 0) w = (w + wcw - 1) / wcw;
943 careful = w > prop->width;
944 if (careful && w > prop->width * MAX_OVERLAP >> 2)
951 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
952 const text_t *text, int len,
955 // this looks like a mess /.
956 // and it is a mess /.
957 // yet we are trying to be perfect /.
958 // but the result still isn't perfect /.
960 bool slow = this->slow
961 || width != r->TermWin.fwidth
962 || height != r->TermWin.fheight;
964 int base = ascent; // sorry, incorrect: r->TermWin.fbase;
967 v.foreground = r->pix_colors[fg];
972 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
974 if (bg == Color_bg && !slow)
976 v.background = r->pix_colors[bg];
977 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
978 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
982 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
984 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
990 if (xc->byte1 || xc->byte2)
991 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
993 x += r->TermWin.fwidth;
999 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
1004 const char *xc = enc_char (text, len, cs, slow);
1006 if (bg == Color_bg && !slow)
1008 v.background = r->pix_colors[bg];
1009 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1010 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1014 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
1016 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1023 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1025 x += r->TermWin.fwidth;
1031 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
1036 /////////////////////////////////////////////////////////////////////////////
1040 struct rxvt_font_xft : rxvt_font {
1041 rxvt_font_xft () { f = 0; }
1045 rxvt_fontprop properties ();
1047 bool load (const rxvt_fontprop &prop);
1049 void draw (rxvt_drawable &d, int x, int y,
1050 const text_t *text, int len,
1053 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
1060 rxvt_font_xft::clear ()
1064 XftFontClose (DISPLAY, f);
1070 rxvt_font_xft::properties ()
1074 FT_Face face = XftLockFace (f);
1078 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1079 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1080 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1081 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1089 rxvt_font_xft::load (const rxvt_fontprop &prop)
1091 Display *disp = DISPLAY;
1095 FcPattern *p = FcNameParse ((FcChar8 *) name);
1102 if (prop.height != rxvt_fontprop::unset
1103 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1104 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1105 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1107 if (prop.weight != rxvt_fontprop::unset
1108 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1109 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1111 if (prop.slant != rxvt_fontprop::unset
1112 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1113 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1115 #if 0 // clipping unfortunately destroys our precious double-width-characters
1116 // clip width, we can't do better, or can we?
1117 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1118 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1121 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1122 FcPatternAddBool (p, FC_MINSPACE, 1);
1124 // store generated name so iso14755 view gives better results
1125 set_name ((char *)FcNameUnparse (p));
1128 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1130 FcPatternDestroy (p);
1136 bool success = true;
1140 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1148 FT_Face face = XftLockFace (f);
1150 ascent = (face->size->metrics.ascender + 63) >> 6;
1151 descent = (-face->size->metrics.descender + 63) >> 6;
1152 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1155 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1159 int glheight = height;
1161 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1165 if (cs != CS_UNICODE
1167 && FROM_UNICODE (cs, ch) == NOCHAR)
1170 // ignore characters we wouldn't use anyways
1172 if (!has_char (*t, &prop, careful))
1176 XftTextExtents16 (disp, f, &ch, 1, &g);
1180 int wcw = wcwidth (ch);
1181 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1183 if (width < g.width ) width = g.width;
1184 if (height < g.height ) height = g.height;
1185 if (glheight < g.height - g.y) glheight = g.height - g.y;
1188 if (prop.height == rxvt_fontprop::unset
1189 || (height <= prop.height && glheight <= prop.height)
1196 // take smaller steps near the end
1197 if (height > prop.height + 1) ftheight++;
1198 if (height > prop.height + 2) ftheight++;
1199 if (height > prop.height + 3) ftheight++;
1201 ftheight -= height - prop.height;
1204 ftheight = prop.height - 1;
1206 XftFontClose (disp, f);
1207 FcPatternDel (match, FC_PIXEL_SIZE);
1208 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1211 FcPatternDestroy (match);
1213 #if 0 // do it per-character
1214 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1225 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1229 if (!XftCharExists (DISPLAY, f, unicode))
1232 if (!prop || prop->width == rxvt_fontprop::unset)
1235 // check character against base font bounding box
1236 FcChar32 ch = unicode;
1238 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1240 int w = g.width - g.x;
1241 int wcw = wcwidth (unicode);
1242 if (wcw > 0) w = (w + wcw - 1) / wcw;
1244 careful = w > prop->width;
1245 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1252 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1253 const text_t *text, int len,
1256 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
1258 int base = ascent; // should be fbase, but that is incorrect
1261 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1267 int cwidth = r->TermWin.fwidth;
1268 FcChar32 fc = *text++; len--;
1270 while (len && *text == NOCHAR)
1271 text++, len--, cwidth += r->TermWin.fwidth;
1273 if (fc == ' ' && ep == enc) // skip leading spaces
1277 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1278 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1280 if (extents.xOff != cwidth)
1284 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1285 x, y + base, enc, ep - enc);
1292 if (extents.xOff > cwidth)
1293 extents.xOff = cwidth;
1295 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1296 x + (cwidth - extents.xOff >> 1),
1309 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1310 x, y + base, enc, ep - enc);
1314 /////////////////////////////////////////////////////////////////////////////
1316 rxvt_fontset::rxvt_fontset (rxvt_t r)
1317 : fontdesc (0), r (r)
1322 rxvt_fontset::~rxvt_fontset ()
1328 rxvt_fontset::clear ()
1330 prop.width = prop.height = prop.weight = prop.slant
1331 = rxvt_fontprop::unset;
1333 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1336 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1339 free (fontdesc); fontdesc = 0;
1343 fallback = fallback_fonts;
1347 rxvt_fontset::new_font (const char *name, codeset cs)
1351 if (!name || !*name)
1354 f = new rxvt_font_default (this);
1357 else if (!strncmp (name, "xft:", 4))
1360 f = new rxvt_font_xft ();
1363 else if (!strncmp (name, "x:", 2))
1366 f = new rxvt_font_x11;
1369 f = new rxvt_font_x11;
1372 f->set_name (strdup (name));
1380 /////////////////////////////////////////////////////////////////////////////
1383 rxvt_fontset::add_fonts (const char *desc)
1392 while (*desc && *desc <= ' ')
1395 codeset cs = CS_UNICODE;
1400 const char *extra = ++desc; // not yet used
1402 desc = strchr (desc, ']');
1406 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1410 memcpy (spec, extra, min (desc - extra, 255));
1411 spec[min (desc - extra, 255)] = 0;
1413 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1414 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1416 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1419 while (*desc <= ' ') desc++;
1422 end = strchr (desc, ',');
1424 end = desc + strlen (desc);
1426 if (end - desc < 511)
1428 strncpy (buf, desc, end - desc);
1429 buf[end - desc] = 0;
1431 fonts.push_back (new_font (buf, cs));
1434 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1443 rxvt_fontset::realize_font (int i)
1445 if (i < 0 || i >= fonts.size ())
1448 if (fonts[i]->loaded)
1451 fonts[i]->loaded = true;
1453 if (!fonts[i]->load (prop))
1455 fonts[i]->cs = CS_UNKNOWN;
1463 rxvt_fontset::populate (const char *desc)
1467 fontdesc = strdup (desc);
1469 fonts.push_back (new_font (0, CS_UNICODE));
1478 rxvt_fontset::find_font (const char *name) const
1480 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1481 if ((*f)->name && !strcmp ((*f)->name, name))
1482 return f - fonts.begin ();
1488 rxvt_fontset::find_font (unicode_t unicode)
1490 if (unicode >= 1<<20)
1493 unicode_t hi = unicode >> 8;
1495 if (hi < fmap.size ()
1497 && (*fmap[hi])[unicode & 0xff] != 0xff)
1498 return (*fmap[hi])[unicode & 0xff];
1502 for (i = 0; i < fonts.size (); i++)
1504 rxvt_font *f = fonts[i];
1508 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1511 if (!realize_font (i))
1515 if (f->cs == CS_UNKNOWN)
1519 if (f->has_char (unicode, &prop, careful))
1528 if (i == fonts.size () - 1)
1532 // search through the fallback list
1533 fonts.push_back (new_font (fallback->name, fallback->cs));
1538 // try to find a new font.
1539 // only xft currently supported, as there is no
1540 // way to configure this and xft is easier to hack in,
1541 // while x11 has more framework in place already.
1542 // TODO: this is a real resource hog, xft takes ages(?)
1543 #if XFT && USE_SLOW_LOOKUP
1544 // grab the first xft font that seems suitable
1545 FcPattern *p = FcPatternCreate ();
1547 FcCharSet *s = FcCharSetCreate ();
1548 FcCharSetAddChar (s, unicode);
1549 FcPatternAddCharSet (p, FC_CHARSET, s);
1550 // charsets don't help that much, as xft might return
1551 // a non-matching font even if a better font is available :/
1553 x x x x TODO prop might have unset contents
1554 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1555 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1556 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1557 FcPatternAddBool (p, FC_MINSPACE, 1);
1558 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1561 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1563 FcPatternDestroy (p);
1567 FcPatternDel (match, FC_CHARSET);
1568 char *font = (char *)FcNameUnparse (match);
1569 FcPatternDestroy (match);
1571 if (find_font (font) < 0)
1573 char fontname[4096];
1574 sprintf (fontname, "xft:%-.4090s", font);
1576 fonts.push_back (new_font (fontname, CS_UNICODE));
1586 /* we must return SOME font */
1590 // found a font, cache it
1593 while (hi >= fmap.size ())
1598 fmap[hi] = (pagemap *)new pagemap;
1599 memset (fmap[hi], 0xff, sizeof (pagemap));
1602 (*fmap[hi])[unicode & 0xff] = i;