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
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.ascent = rxvt_fontprop::unset;
282 p.weight = rxvt_fontprop::medium;
283 p.slant = rxvt_fontprop::roman;
288 bool load (const rxvt_fontprop &prop)
290 width = 1; height = 1;
291 ascent = 1; descent = 0;
293 set_name (strdup ("built-in support font"));
298 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
302 if (unicode <= 0x001f)
305 if (unicode <= 0x007f)
308 if (unicode <= 0x009f)
311 if (unicode >= 0x2500 && unicode <= 0x259f)
314 if (IS_COMPOSE (unicode))
319 case ZERO_WIDTH_CHAR:
327 void draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len,
333 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
334 const text_t *text, int len,
337 Display *disp = d.display->display;
339 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
341 XSetForeground (disp, TGC, r->pix_colors[fg]);
348 const text_t *tp = text;
351 while (++text, --len && *text == NOCHAR)
354 int width = text - tp;
355 int fwidth = r->fwidth * width;
357 if (0x2500 <= t && t <= 0x259f)
359 uint16_t offs = linedraw_offs[t - 0x2500];
360 uint32_t *a = linedraw_command + (offs >> 4);
361 uint32_t *b = a + (offs & 15);
369 for (int i = 0; i <= 8; i++)
371 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
372 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
375 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
376 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
380 gcv.cap_style = CapButt;
382 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
386 uint32_t command = *a++;
388 int op = (command >> 24) & 255;
389 int a = (command >> 20) & 15;
390 int b = (command >> 16) & 15;
391 int x1 = x_[(command >> 12) & 15];
392 int y1 = y_[(command >> 8) & 15];
393 int x2 = x_[(command >> 4) & 15];
394 int y2 = y_[(command >> 0) & 15];
399 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
402 case 1: // rectangle, possibly stippled
405 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
407 gcv.fill_style = FillStippled;
408 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
412 XChangeGC (disp, TGC,
413 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
417 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
421 XFreePixmap (disp, gcv.stipple);
423 gcv.fill_style = FillSolid;
424 XChangeGC (disp, TGC, GCFillStyle, &gcv);
428 XDrawArc (disp, d, TGC,
429 x1 - W/2, y1 - H/2, W-1, H-1,
430 (a - 1) * 90*64, (b - 1) * 90*64);
436 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
439 width = min (2, width);
443 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
444 f1->draw (d, x, y, chrs, width, fg, bg);
446 if (cc->c2 != NOCHAR)
450 // prefer font of first character, for no good reasons
452 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
454 : (*fs)[fs->find_font (cc->c2)];
456 f2->draw (d, x, y, chrs, width, fg, -1);
464 case ZERO_WIDTH_CHAR:
469 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
470 fwidth - 4, r->fheight - 4);
477 /////////////////////////////////////////////////////////////////////////////
479 struct rxvt_font_x11 : rxvt_font {
480 rxvt_font_x11 () { f = 0; }
484 rxvt_fontprop properties ();
486 bool load (const rxvt_fontprop &prop);
488 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
490 void draw (rxvt_drawable &d, int x, int y,
491 const text_t *text, int len,
494 bool slow; // wether this is a proportional font or has other funny characteristics
499 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
500 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
501 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
502 bool set_properties (rxvt_fontprop &p, const char *name);
506 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
510 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
511 return XGetAtomName (DISPLAY, value);
513 return rxvt_strdup (repl);
517 rxvt_font_x11::properties ()
520 set_properties (p, f);
525 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
529 p.ascent = rxvt_fontprop::unset;
530 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
531 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
537 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
539 unsigned long height;
542 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
545 height = f->ascent + f->descent;
548 unsigned long avgwidth;
549 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
552 char *weight = get_property (f, "WEIGHT_NAME", "medium");
553 char *slant = get_property (f, "SLANT", "r");
555 set_properties (p, height, weight, slant, avgwidth);
560 p.ascent = f->ascent;
566 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
569 const char *comp[13];
571 for (const char *c = name; *c; c++)
574 comp[slashes++] = c + 1;
579 /* can we short-circuit the costly XLoadQueryFont? */
581 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
582 && (*comp[11] >= '0' && *comp[11] <= '9'))
583 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
585 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
589 // the font should really exist now. if not, we have a problem
590 // (e.g. if the user did xset fp rehash just when we were searching fonts).
591 // in that case, just return garbage.
592 bool ret = set_properties (p, f);
593 XFreeFont (DISPLAY, f);
600 // fix the size of scalable fonts
602 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
605 const char *field, *end;
607 for (const char *c = name; *c; c++)
610 if (slashes == index)
613 if (slashes == index + 1)
620 if (slashes >= 13 && (!old || *field == old))
622 // TODO: check for overflow in font-name
623 strncpy (buf, name, field - name);
625 strcpy (buf, replace);
639 rxvt_font_x11::load (const rxvt_fontprop &prop)
641 Display *disp = DISPLAY;
645 char field_str[64]; // enough for 128 bits
647 // first morph the font if required
648 if (prop.weight != rxvt_fontprop::unset
649 || prop.slant != rxvt_fontprop::unset)
655 f = XLoadQueryFont (disp, name);
660 char *new_name = get_property (f, "FONT", name);
665 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
671 if (prop.weight != rxvt_fontprop::unset)
673 replace_field (fname, name, 2, 0,
674 prop.weight < rxvt_fontprop::bold
675 ? "medium" : "bold");
676 set_name (strdup (fname));
679 if (prop.slant != rxvt_fontprop::unset)
681 replace_field (fname, name, 3, 0,
682 prop.slant < rxvt_fontprop::italic
683 ? "r" : "i"); // TODO: handle "o"blique, too
684 set_name (strdup (fname));
688 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
701 font_weight () { clear (); }
702 ~font_weight () { free (name); }
707 list = XListFonts (disp, name, 4000, &count);
714 font_weight *fonts = new font_weight[count];
716 for (int i = 0; i < count; i++)
723 if (replace_field (fname, list[i], 6, '0', field_str))
724 diff += 10; // slightly penalize scalable fonts
725 else if (replace_field (fname, list[i], 11, '0', "0"))
726 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
728 if (!set_properties (p, fname))
731 if (prop.height != rxvt_fontprop::unset
732 && p.height > prop.height) // weed out too large fonts
735 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
736 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
737 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
738 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
740 fonts[i].name = strdup (fname);
741 fonts[i].diff = diff;
744 XFreeFontNames (list);
746 // this loop only iterates when the guessed font-size is too small
749 font_weight *best = fonts + count - 1;
751 for (font_weight *w = best; w-- > fonts; )
752 if (w->diff <= best->diff)
756 || !(f = XLoadQueryFont (disp, best->name)))
759 set_name (best->name);
763 descent = f->descent;
764 height = ascent + descent;
766 if (prop.height == rxvt_fontprop::unset
767 || height <= prop.height)
768 break; // font is ready for use
770 // PIXEL_SIZE small enough, but real height too large
779 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
780 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
782 if (registry && encoding)
785 snprintf (charset, 64, "%s-%s", registry, encoding);
787 cs = codeset_from_name (charset);
791 const char *charset = get_property (f, "FONT", 0);
798 if (*charset++ == '-' && !--count)
801 cs = codeset_from_name (charset);
807 if (cs == CS_UNICODE)
808 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
810 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
811 enc2b = encm || f->max_char_or_byte2 > 255;
815 #if 1 // only used for slow detection, TODO optimize
816 if (f->min_bounds.width == f->max_bounds.width)
817 width = f->min_bounds.width;
818 else if (f->per_char == NULL)
819 width = f->max_bounds.width;
824 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
827 N += (f->max_byte1 - f->min_byte1)
828 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
832 if (f->per_char[N].width > width)
833 width = f->per_char[N].width;
842 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
846 && FROM_UNICODE (cs, *t) == NOCHAR)
849 // ignore characters we wouldn't use anyways
851 if (!has_char (*t, &prop, careful))
854 XChar2b ch = { *t >> 8, *t };
857 int dir_ret, asc_ret, des_ret;
858 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
860 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
862 if (width < g.width) width = g.width;
865 if (cs == CS_UNKNOWN)
867 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
873 #if 0 // do it per-character
874 if (prop && width > prop->width)
885 rxvt_font_x11::clear ()
889 XFreeFont (DISPLAY, f);
895 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
897 uint32_t ch = FROM_UNICODE (cs, unicode);
902 /* check wether the character exists in _this_ font. horrible. */
907 unsigned char byte1 = ch >> 8;
908 unsigned char byte2 = ch & 255;
910 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
911 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
916 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
917 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
919 xcs = f->per_char + N;
922 xcs = &f->max_bounds;
926 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
930 xcs = f->per_char + (ch - f->min_char_or_byte2);
932 xcs = &f->max_bounds;
935 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
936 && xcs->ascent == 0 && xcs->descent == 0)
939 if (!prop || prop->width == rxvt_fontprop::unset)
942 // check character against base font bounding box
944 int wcw = wcwidth (unicode);
945 if (wcw > 0) w = (w + wcw - 1) / wcw;
947 careful = w > prop->width;
948 if (careful && w > prop->width * MAX_OVERLAP >> 2)
955 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
956 const text_t *text, int len,
959 // this looks like a mess /.
960 // and it is a mess /.
961 // yet we are trying to be perfect /.
962 // but the result still isn't perfect /.
964 bool slow = this->slow
965 || width != r->fwidth
966 || height != r->fheight;
968 int base = ascent; // sorry, incorrect: r->fbase;
971 v.foreground = r->pix_colors[fg];
976 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
978 if (bg == Color_bg && !slow)
980 v.background = r->pix_colors[bg];
981 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
982 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
986 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
988 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
994 if (xc->byte1 || xc->byte2)
995 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
1003 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
1008 const char *xc = enc_char (text, len, cs, slow);
1010 if (bg == Color_bg && !slow)
1012 v.background = r->pix_colors[bg];
1013 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1014 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1018 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1020 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1027 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1035 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
1040 /////////////////////////////////////////////////////////////////////////////
1044 struct rxvt_font_xft : rxvt_font {
1045 rxvt_font_xft () { f = 0; }
1049 rxvt_fontprop properties ();
1051 bool load (const rxvt_fontprop &prop);
1053 void draw (rxvt_drawable &d, int x, int y,
1054 const text_t *text, int len,
1057 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
1064 rxvt_font_xft::clear ()
1068 XftFontClose (DISPLAY, f);
1074 rxvt_font_xft::properties ()
1078 FT_Face face = XftLockFace (f);
1083 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1084 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1085 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1086 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1094 rxvt_font_xft::load (const rxvt_fontprop &prop)
1096 Display *disp = DISPLAY;
1100 FcPattern *p = FcNameParse ((FcChar8 *) name);
1107 if (prop.height != rxvt_fontprop::unset
1108 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1109 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1110 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1112 if (prop.weight != rxvt_fontprop::unset
1113 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1114 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1116 if (prop.slant != rxvt_fontprop::unset
1117 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1118 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1120 #if 0 // clipping unfortunately destroys our precious double-width-characters
1121 // clip width, we can't do better, or can we?
1122 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1123 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1126 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1127 FcPatternAddBool (p, FC_MINSPACE, 1);
1129 // store generated name so iso14755 view gives better results
1130 set_name ((char *)FcNameUnparse (p));
1133 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1135 FcPatternDestroy (p);
1141 bool success = true;
1145 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1153 FT_Face face = XftLockFace (f);
1155 ascent = (face->size->metrics.ascender + 63) >> 6;
1156 descent = (-face->size->metrics.descender + 63) >> 6;
1157 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1160 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1164 int glheight = height;
1166 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1170 if (cs != CS_UNICODE
1172 && FROM_UNICODE (cs, ch) == NOCHAR)
1175 // ignore characters we wouldn't use anyways
1177 if (!has_char (*t, &prop, careful))
1181 XftTextExtents16 (disp, f, &ch, 1, &g);
1185 int wcw = wcwidth (ch);
1186 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1188 if (width < g.width ) width = g.width;
1189 if (height < g.height ) height = g.height;
1190 if (glheight < g.height - g.y) glheight = g.height - g.y;
1195 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1197 XftFontClose (disp, f);
1204 if (prop.height == rxvt_fontprop::unset
1205 || (height <= prop.height && glheight <= prop.height)
1212 // take smaller steps near the end
1213 if (height > prop.height + 1) ftheight++;
1214 if (height > prop.height + 2) ftheight++;
1215 if (height > prop.height + 3) ftheight++;
1217 ftheight -= height - prop.height;
1220 ftheight = prop.height - 1;
1222 XftFontClose (disp, f);
1223 FcPatternDel (match, FC_PIXEL_SIZE);
1224 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1227 FcPatternDestroy (match);
1229 #if 0 // do it per-character
1230 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1241 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1245 if (!XftCharExists (DISPLAY, f, unicode))
1248 if (!prop || prop->width == rxvt_fontprop::unset)
1251 // check character against base font bounding box
1252 FcChar32 ch = unicode;
1254 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1256 int w = g.width - g.x;
1257 int wcw = wcwidth (unicode);
1258 if (wcw > 0) w = (w + wcw - 1) / wcw;
1260 careful = w > prop->width;
1261 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1268 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1269 const text_t *text, int len,
1272 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1274 int base = ascent; // should be fbase, but that is incorrect
1277 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1283 int cwidth = r->fwidth;
1284 FcChar32 fc = *text++; len--;
1286 while (len && *text == NOCHAR)
1287 text++, len--, cwidth += r->fwidth;
1289 if (fc == ' ' && ep == enc) // skip leading spaces
1293 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1294 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1296 if (extents.xOff != cwidth)
1300 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1301 x, y + base, enc, ep - enc);
1308 if (extents.xOff > cwidth)
1309 extents.xOff = cwidth;
1311 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1312 x + (cwidth - extents.xOff >> 1),
1325 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1326 x, y + base, enc, ep - enc);
1330 /////////////////////////////////////////////////////////////////////////////
1332 rxvt_fontset::rxvt_fontset (rxvt_t r)
1333 : fontdesc (0), r (r)
1338 rxvt_fontset::~rxvt_fontset ()
1344 rxvt_fontset::clear ()
1346 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1347 = rxvt_fontprop::unset;
1349 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1352 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1355 free (fontdesc); fontdesc = 0;
1359 fallback = fallback_fonts;
1363 rxvt_fontset::new_font (const char *name, codeset cs)
1367 if (!name || !*name)
1370 f = new rxvt_font_default (this);
1373 else if (!strncmp (name, "xft:", 4))
1376 f = new rxvt_font_xft ();
1379 else if (!strncmp (name, "x:", 2))
1382 f = new rxvt_font_x11;
1385 f = new rxvt_font_x11;
1388 f->set_name (strdup (name));
1396 /////////////////////////////////////////////////////////////////////////////
1399 rxvt_fontset::add_fonts (const char *desc)
1408 while (*desc && *desc <= ' ')
1411 codeset cs = CS_UNICODE;
1416 const char *extra = ++desc; // not yet used
1418 desc = strchr (desc, ']');
1422 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1426 memcpy (spec, extra, min (desc - extra, 255));
1427 spec[min (desc - extra, 255)] = 0;
1429 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1430 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1432 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1435 while (*desc <= ' ') desc++;
1438 end = strchr (desc, ',');
1440 end = desc + strlen (desc);
1442 if (end - desc < 511)
1444 strncpy (buf, desc, end - desc);
1445 buf[end - desc] = 0;
1447 fonts.push_back (new_font (buf, cs));
1450 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1459 rxvt_fontset::realize_font (int i)
1461 if (i < 0 || i >= fonts.size ())
1464 if (fonts[i]->loaded)
1467 fonts[i]->loaded = true;
1469 if (!fonts[i]->load (prop))
1471 fonts[i]->cs = CS_UNKNOWN;
1479 rxvt_fontset::populate (const char *desc)
1483 fontdesc = strdup (desc);
1485 fonts.push_back (new_font (0, CS_UNICODE));
1494 rxvt_fontset::find_font (const char *name) const
1496 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1497 if ((*f)->name && !strcmp ((*f)->name, name))
1498 return f - fonts.begin ();
1504 rxvt_fontset::find_font (unicode_t unicode)
1506 if (unicode >= 1<<20)
1509 unicode_t hi = unicode >> 8;
1511 if (hi < fmap.size ()
1513 && (*fmap[hi])[unicode & 0xff] != 0xff)
1514 return (*fmap[hi])[unicode & 0xff];
1518 for (i = 0; i < fonts.size (); i++)
1520 rxvt_font *f = fonts[i];
1524 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1527 if (!realize_font (i))
1530 if (prop.ascent != rxvt_fontprop::unset)
1531 max_it (f->ascent, prop.ascent);
1534 if (f->cs == CS_UNKNOWN)
1538 if (f->has_char (unicode, &prop, careful))
1547 if (i == fonts.size () - 1)
1551 // search through the fallback list
1552 fonts.push_back (new_font (fallback->name, fallback->cs));
1557 // try to find a new font.
1558 // only xft currently supported, as there is no
1559 // way to configure this and xft is easier to hack in,
1560 // while x11 has more framework in place already.
1561 // TODO: this is a real resource hog, xft takes ages(?)
1562 #if XFT && USE_SLOW_LOOKUP
1563 // grab the first xft font that seems suitable
1564 FcPattern *p = FcPatternCreate ();
1566 FcCharSet *s = FcCharSetCreate ();
1567 FcCharSetAddChar (s, unicode);
1568 FcPatternAddCharSet (p, FC_CHARSET, s);
1569 // charsets don't help that much, as xft might return
1570 // a non-matching font even if a better font is available :/
1572 x x x x TODO prop might have unset contents
1573 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1574 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1575 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1576 FcPatternAddBool (p, FC_MINSPACE, 1);
1577 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1580 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1582 FcPatternDestroy (p);
1586 FcPatternDel (match, FC_CHARSET);
1587 char *font = (char *)FcNameUnparse (match);
1588 FcPatternDestroy (match);
1590 if (find_font (font) < 0)
1592 char fontname[4096];
1593 sprintf (fontname, "xft:%-.4090s", font);
1595 fonts.push_back (new_font (fontname, CS_UNICODE));
1605 /* we must return SOME font */
1609 // found a font, cache it
1612 while (hi >= fmap.size ())
1617 fmap[hi] = (pagemap *)new pagemap;
1618 memset (fmap[hi], 0xff, sizeof (pagemap));
1621 (*fmap[hi])[unicode & 0xff] = i;