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 MAX_OVERLAP_ROMAN (8 + 2) // max. character width in 8ths of the base width
33 #define MAX_OVERLAP_ITALIC (8 + 3) // max. overlap for italic fonts
35 #define OVERLAP_OK(w,wcw,prop) ((w) <= ( \
36 (prop)->slant >= rxvt_fontprop::italic \
37 ? ((prop)->width * (wcw) * MAX_OVERLAP_ITALIC + 7) >> 3 \
38 : ((prop)->width * (wcw) * MAX_OVERLAP_ROMAN + 7) >> 3 \
41 const struct rxvt_fallback_font {
44 } fallback_fonts[] = {
45 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
46 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
47 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
51 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
52 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
54 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
55 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
56 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
57 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
58 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
59 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
60 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
61 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
62 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
63 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
64 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
65 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
66 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
69 { CS_KOI8_U, "xft::lang=ru" },
71 { CS_ISO8859_5, "xft::lang=ru" },
72 { CS_ISO8859_6, "xft::lang=ar" },
73 { CS_ISO8859_7, "xft::lang=el" },
74 { CS_ISO8859_8, "xft::lang=he" },
75 { CS_ISO8859_9, "xft::lang=tr" },
76 { CS_ISO8859_10, "xft::lang=se" },
77 { CS_ISO8859_11, "xft::lang=th" },
82 #if ENCODING_JP || ENCODING_JP_EXT
84 // prefer xft for complex scripts
85 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
86 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
87 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
88 { CS_JIS0208_1990_0, "xft::lang=ja:antialias=false" },
90 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
91 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
92 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
93 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
94 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
95 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
98 #if ENCODING_ZH || ENCODING_ZH_EXT
100 { CS_GBK_0, "xft:AR PL KaitiM GB" },
101 { CS_GBK_0, "xft:AR PL SungtiL GB" },
102 { CS_GBK_0, "xft::lang=zh" },
103 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
104 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
105 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
106 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
107 { CS_GB2312_1980_0, "xft::lang=zh" },
109 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
110 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
111 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
112 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
113 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
114 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
115 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
116 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
117 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
118 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
119 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
120 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
121 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
122 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
126 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
127 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
129 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
130 { CS_KSC5601_1987_0, "xft::lang=ko:antialias=false" },
134 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
135 //{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, // this gem of a font has actual dotted circles within the combining character glyphs.
137 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
138 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
139 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
140 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
142 // FreeMono is usually uglier than x fonts, so try after the others
143 { CS_UNICODE, "xft:FreeMono:autohint=true" },
146 // generic font fallback, put this last, as many iso10646 fonts have extents
147 // specified for all glyphs in the range they cover, but most are simply empty
148 //{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
149 //{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
153 // these characters are used to guess the font height and width
154 // pango uses a similar algorithm and doesn't trust the font either.
155 static uint16_t extent_test_chars[] = {
156 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
157 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
158 0x304c, 0x672c, // が本
161 #define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
163 #define dTermDisplay Display *disp = term->xdisp
164 #define dTermGC GC gc = term->gc
166 /////////////////////////////////////////////////////////////////////////////
169 rxvt_drawable::~rxvt_drawable ()
172 XftDrawDestroy (xftdrawable);
175 rxvt_drawable::operator XftDraw *()
178 xftdrawable = XftDrawCreate (screen->xdisp, drawable, screen->visual, screen->cmap);
184 /////////////////////////////////////////////////////////////////////////////
187 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
189 uint8_t *buf = rxvt_temp_buf<uint8_t> (len);
194 uint32_t c = FROM_UNICODE (cs, *text++);
205 return (const char *)res;
208 static const XChar2b *
209 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
211 XChar2b *buf = rxvt_temp_buf<XChar2b> (len);
216 uint32_t c = FROM_UNICODE (cs, *text++);
232 /////////////////////////////////////////////////////////////////////////////
234 rxvt_font::rxvt_font ()
235 : name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset)
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) const
255 if (color == Color_bg)
256 XClearArea (disp, d, x, y, w, h, false);
260 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
262 XSetForeground (disp, gc, term->pix_colors[color]);
263 XFillRectangle (disp, d, gc, x, y, w, h);
268 /////////////////////////////////////////////////////////////////////////////
270 #include "table/linedraw.h"
272 struct rxvt_font_default : rxvt_font {
273 struct rxvt_fontset *fs;
275 rxvt_font_default (rxvt_fontset *fs)
281 rxvt_fontprop properties ()
285 p.width = p.height = 1;
286 p.ascent = rxvt_fontprop::unset;
287 p.weight = rxvt_fontprop::medium;
288 p.slant = rxvt_fontprop::roman;
293 bool load (const rxvt_fontprop &prop)
295 width = 1; height = 1;
296 ascent = 1; descent = 0;
298 set_name (strdup ("built-in support font"));
303 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
307 if (unicode <= 0x001f)
310 if (unicode <= 0x007f)
313 if (unicode <= 0x009f)
316 if (unicode >= 0x2500 && unicode <= 0x259f &&
317 !term->option (Opt_skipBuiltinGlyphs))
320 if (IS_COMPOSE (unicode))
325 case ZERO_WIDTH_CHAR:
333 void draw (rxvt_drawable &d, int x, int y,
334 const text_t *text, int len,
339 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
340 const text_t *text, int len,
346 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
348 XSetForeground (disp, gc, term->pix_colors[fg]);
355 const text_t *tp = text;
358 while (++text, --len && *text == NOCHAR)
361 int width = text - tp;
362 int fwidth = term->fwidth * width;
364 if (0x2500 <= t && t <= 0x259f)
366 uint16_t offs = linedraw_offs[t - 0x2500];
367 uint32_t *a = linedraw_command + (offs >> 4);
368 uint32_t *b = a + (offs & 15);
371 int H = term->fheight;
376 for (int i = 0; i <= 8; i++)
378 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
379 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
382 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
383 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
387 gcv.cap_style = CapButt;
389 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
393 uint32_t command = *a++;
395 int op = (command >> 24) & 255;
396 int a = (command >> 20) & 15;
397 int b = (command >> 16) & 15;
398 int x1 = x_[(command >> 12) & 15];
399 int y1 = y_[(command >> 8) & 15];
400 int x2 = x_[(command >> 4) & 15];
401 int y2 = y_[(command >> 0) & 15];
406 XDrawLine (disp, d, gc, x1, y1, x2, y2);
409 case 1: // rectangle, possibly stippled
412 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
414 gcv.fill_style = FillStippled;
415 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
420 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
424 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
428 XFreePixmap (disp, gcv.stipple);
430 gcv.fill_style = FillSolid;
431 XChangeGC (disp, gc, GCFillStyle, &gcv);
435 XDrawArc (disp, d, gc,
436 x1 - W/2, y1 - H/2, W-1, H-1,
437 (a - 1) * 90*64, (b - 1) * 90*64);
443 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
445 min_it (width, 2); // we only support wcwidth up to 2
451 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
452 f1->draw (d, x, y, chrs, width, fg, bg);
454 if (cc->c2 != NOCHAR)
458 // prefer font of first character, for no good reasons
460 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
462 : (*fs)[fs->find_font (cc->c2)];
464 f2->draw (d, x, y, chrs, width, fg, -1);
472 case ZERO_WIDTH_CHAR:
477 XDrawRectangle (disp, d, gc, x + 2, y + 2,
478 fwidth - 4, term->fheight - 4);
485 /////////////////////////////////////////////////////////////////////////////
487 struct rxvt_font_x11 : rxvt_font {
488 rxvt_font_x11 () { f = 0; }
492 rxvt_fontprop properties ();
494 bool load (const rxvt_fontprop &prop);
496 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
498 void draw (rxvt_drawable &d, int x, int y,
499 const text_t *text, int len,
502 bool slow; // wether this is a proportional font or has other funny characteristics
507 char *get_property (XFontStruct *f, Atom property, const char *repl) const;
508 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
509 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
510 bool set_properties (rxvt_fontprop &p, const char *name);
514 rxvt_font_x11::get_property (XFontStruct *f, Atom property, const char *repl) const
518 if (XGetFontProperty (f, property, &value))
519 return XGetAtomName (term->xdisp, value);
521 return rxvt_strdup (repl);
525 rxvt_font_x11::properties ()
528 set_properties (p, f);
533 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
535 p.width = width != rxvt_fontprop::unset ? width
536 : avgwidth ? (avgwidth + 1) / 10
539 p.ascent = rxvt_fontprop::unset;
540 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
541 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
547 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
549 unsigned long height;
552 if (!XGetFontProperty (f, XInternAtom (term->xdisp, "PIXEL_SIZE", 0), &height))
555 height = f->ascent + f->descent;
558 unsigned long avgwidth;
559 if (!XGetFontProperty (f, term->xa [XA_AVERAGE_WIDTH], &avgwidth))
562 char *weight = get_property (f, term->xa [XA_WEIGHT_NAME], "medium");
563 char *slant = get_property (f, term->xa [XA_SLANT], "r");
565 set_properties (p, height, weight, slant, avgwidth);
570 p.ascent = f->ascent;
576 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
580 const char *comp[13];
582 for (const char *c = name; *c; c++)
585 comp[slashes++] = c + 1;
590 /* can we short-circuit the costly XLoadQueryFont? */
592 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
593 && (*comp[11] >= '0' && *comp[11] <= '9'))
594 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
596 XFontStruct *f = XLoadQueryFont (disp, name);
600 // the font should really exist now. if not, we have a problem
601 // (e.g. if the user did xset fp rehash just when we were searching fonts).
602 // in that case, just return garbage.
603 bool ret = set_properties (p, f);
611 // fix the size of scalable fonts
613 replace_field (char *buf, const char *name, int index, const char old, const char *replace)
616 const char *field, *end;
618 for (const char *c = name; *c; c++)
621 if (slashes == index)
624 if (slashes == index + 1)
631 if (slashes >= 13 && (!old || *field == old))
633 // TODO: check for overflow in font-name
634 strncpy (buf, name, field - name);
636 strcpy (buf, replace);
650 rxvt_font_x11::load (const rxvt_fontprop &prop)
656 char field_str[64]; // enough for 128 bits
658 // first morph the font if required
659 if (prop.weight != rxvt_fontprop::unset
660 || prop.slant != rxvt_fontprop::unset)
666 f = XLoadQueryFont (disp, name);
671 char *new_name = get_property (f, XA_FONT, name);
676 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
682 if (prop.weight != rxvt_fontprop::unset)
684 replace_field (fname, name, 2, 0,
685 prop.weight < rxvt_fontprop::bold
686 ? "medium" : "bold");
687 set_name (strdup (fname));
690 if (prop.slant != rxvt_fontprop::unset)
692 replace_field (fname, name, 3, 0,
693 prop.slant < rxvt_fontprop::italic
694 ? "r" : "i"); // TODO: handle "o"blique, too
695 set_name (strdup (fname));
699 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
712 font_weight () { clear (); }
713 ~font_weight () { free (name); }
718 list = XListFonts (disp, name, 4000, &count);
725 font_weight *fonts = new font_weight[count];
727 for (int i = 0; i < count; i++)
734 if (replace_field (fname, list[i], 6, '0', field_str))
735 diff += 10; // slightly penalize scalable fonts
736 else if (replace_field (fname, list[i], 11, '0', "0"))
737 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
739 if (!set_properties (p, fname))
742 if (prop.height != rxvt_fontprop::unset
743 && p.height > prop.height) // weed out too large fonts
746 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
747 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
748 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
749 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
751 fonts[i].name = strdup (fname);
752 fonts[i].diff = diff;
755 XFreeFontNames (list);
757 // this loop only iterates when the guessed font-size is too small
760 font_weight *best = fonts + count - 1;
762 for (font_weight *w = best; w-- > fonts; )
763 if (w->diff <= best->diff)
767 || !(f = XLoadQueryFont (disp, best->name)))
770 set_name (best->name);
774 descent = f->descent;
775 height = ascent + descent;
777 if (prop.height == rxvt_fontprop::unset
778 || height <= prop.height)
779 break; // font is ready for use
781 // PIXEL_SIZE small enough, but real height too large
790 char *registry = get_property (f, term->xa [XA_CHARSET_REGISTRY], 0);
791 char *encoding = get_property (f, term->xa [XA_CHARSET_ENCODING], 0);
793 if (registry && encoding)
796 snprintf (charset, 64, "%s-%s", registry, encoding);
798 cs = codeset_from_name (charset);
802 const char *charset = get_property (f, XA_FONT, 0);
809 if (*charset++ == '-' && !--count)
812 cs = codeset_from_name (charset);
818 if (cs == CS_UNICODE)
819 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
821 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
822 enc2b = encm || f->max_char_or_byte2 > 255;
826 #if 1 // only used for slow detection, TODO optimize
827 if (f->min_bounds.width == f->max_bounds.width || !f->per_char)
828 width = f->max_bounds.width;
833 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
836 N += (f->max_byte1 - f->min_byte1)
837 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
841 if (f->per_char[N].width > width)
842 width = f->per_char[N].width;
851 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
853 if (FROM_UNICODE (cs, *t) == NOCHAR)
856 // ignore characters we wouldn't use anyways
858 if (!has_char (*t, &prop, careful))
861 XChar2b ch = { *t >> 8, *t };
864 int dir_ret, asc_ret, des_ret;
865 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
867 int wcw = WCWIDTH (*t);
868 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
870 if (width < g.width) width = g.width;
873 if (cs == CS_UNKNOWN)
875 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
881 #if 0 // do it per-character
882 if (prop && width > prop->width)
893 rxvt_font_x11::clear ()
897 XFreeFont (term->xdisp, f);
903 rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
905 uint32_t ch = FROM_UNICODE (cs, unicode);
910 /* check wether the character exists in _this_ font. horrible. */
915 unsigned char byte1 = ch >> 8;
916 unsigned char byte2 = ch & 255;
918 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
919 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
924 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
925 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
927 xcs = f->per_char + N;
930 xcs = &f->max_bounds;
934 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
938 xcs = f->per_char + (ch - f->min_char_or_byte2);
940 xcs = &f->max_bounds;
943 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
944 && xcs->ascent == 0 && xcs->descent == 0)
947 if (!prop || prop->width == rxvt_fontprop::unset)
950 // check wether character overlaps previous/next character
951 int w = xcs->rbearing - xcs->lbearing;
952 int wcw = max (WCWIDTH (unicode), 1);
954 careful = xcs->lbearing < 0 || xcs->rbearing > prop->width * wcw;
956 if (careful && !OVERLAP_OK (w, wcw, prop))
963 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len,
967 // this looks like a mess /.
968 // and it is a mess /.
969 // yet we are trying to be perfect /.
970 // but the result still isn't perfect /.
975 bool slow = this->slow
976 || width != term->fwidth
977 || height != term->fheight;
979 int base = ascent; // sorry, incorrect: term->fbase;
982 v.foreground = term->pix_colors[fg];
987 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
989 if (bg == Color_bg && !slow)
991 v.background = term->pix_colors[bg];
992 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
993 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
997 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
999 XChangeGC (disp, gc, GCForeground | GCFont, &v);
1005 if (xc->byte1 || xc->byte2)
1006 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
1014 XDrawString16 (disp, d, gc, x, y + base, xc, len);
1019 const char *xc = enc_char (text, len, cs, slow);
1021 if (bg == Color_bg && !slow)
1023 v.background = term->pix_colors[bg];
1024 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
1025 XDrawImageString (disp, d, gc, x, y + base, xc, len);
1029 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1031 XChangeGC (disp, gc, GCForeground | GCFont, &v);
1038 XDrawString (disp, d, gc, x, y + base, xc, 1);
1046 XDrawString (disp, d, gc, x, y + base, xc, len);
1051 /////////////////////////////////////////////////////////////////////////////
1055 struct rxvt_font_xft : rxvt_font {
1056 rxvt_font_xft () { f = 0; }
1060 rxvt_fontprop properties ();
1062 bool load (const rxvt_fontprop &prop);
1064 void draw (rxvt_drawable &d, int x, int y,
1065 const text_t *text, int len,
1068 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull) const;
1075 rxvt_font_xft::clear ()
1079 XftFontClose (term->xdisp, f);
1085 rxvt_font_xft::properties ()
1089 FT_Face face = XftLockFace (f);
1094 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1095 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1096 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1097 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1105 rxvt_font_xft::load (const rxvt_fontprop &prop)
1111 FcPattern *p = FcNameParse ((FcChar8 *) name);
1118 if (prop.height != rxvt_fontprop::unset
1119 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1120 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1121 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1123 if (prop.weight != rxvt_fontprop::unset
1124 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1125 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1127 if (prop.slant != rxvt_fontprop::unset
1128 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
1129 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1131 #if 0 // clipping unfortunately destroys our precious double-width-characters
1132 // clip width, we can't do better, or can we?
1133 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1134 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
1137 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1138 FcPatternAddBool (p, FC_MINSPACE, 1);
1140 // store generated name so iso14755 view gives better results
1141 set_name ((char *)FcNameUnparse (p));
1144 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
1146 FcPatternDestroy (p);
1152 bool success = true;
1156 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1164 FT_Face face = XftLockFace (f);
1166 ascent = (face->size->metrics.ascender + 63) >> 6;
1167 descent = (-face->size->metrics.descender + 63) >> 6;
1168 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1171 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1175 int glheight = height;
1177 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1181 if (cs != CS_UNICODE
1183 && FROM_UNICODE (cs, ch) == NOCHAR)
1186 // ignore characters we wouldn't use anyways
1188 if (!has_char (*t, &prop, careful))
1192 XftTextExtents16 (disp, f, &ch, 1, &g);
1196 int wcw = WCWIDTH (ch);
1197 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1199 if (width < g.width ) width = g.width;
1200 if (height < g.height ) height = g.height;
1201 if (glheight < g.height - g.y) glheight = g.height - g.y;
1206 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1208 XftFontClose (disp, f);
1215 if (prop.height == rxvt_fontprop::unset
1216 || (height <= prop.height && glheight <= prop.height)
1223 // take smaller steps near the end
1224 if (height > prop.height + 1) ftheight++;
1225 if (height > prop.height + 2) ftheight++;
1226 if (height > prop.height + 3) ftheight++;
1228 ftheight -= height - prop.height;
1231 ftheight = prop.height - 1;
1233 XftFontClose (disp, f);
1234 FcPatternDel (match, FC_PIXEL_SIZE);
1235 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1238 FcPatternDestroy (match);
1240 #if 0 // do it per-character
1241 if (prop.width != rxvt_fontprop::unset && width > prop.width)
1252 rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1256 if (!XftCharExists (term->xdisp, f, unicode))
1259 if (!prop || prop->width == rxvt_fontprop::unset)
1262 // check character against base font bounding box
1263 FcChar32 ch = unicode;
1265 XftTextExtents32 (term->xdisp, f, &ch, 1, &g);
1267 int w = g.width - g.x;
1268 int wcw = max (WCWIDTH (unicode), 1);
1270 careful = g.x > 0 || w > prop->width * wcw;
1272 if (careful && !OVERLAP_OK (w, wcw, prop))
1275 // this weeds out _totally_ broken fonts, or glyphs
1276 if (!OVERLAP_OK (g.xOff, wcw, prop))
1283 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1284 const text_t *text, int len,
1287 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1290 XftGlyphSpec *enc = (XftGlyphSpec *)rxvt_temp_buf (len * sizeof (XftGlyphSpec));
1291 XftGlyphSpec *ep = enc;
1296 // cut trailing spaces
1297 while (len && text [len - 1] == ' ')
1302 int cwidth = term->fwidth;
1303 FcChar32 fc = *text++; len--;
1305 while (len && *text == NOCHAR)
1306 text++, len--, cwidth += term->fwidth;
1308 if (fc != ' ') // skip spaces
1310 FT_UInt glyph = XftCharIndex (disp, f, fc);
1311 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1314 ep->x = x + (cwidth - extents.xOff >> 1);
1317 if (extents.xOff == 0)
1327 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1331 /////////////////////////////////////////////////////////////////////////////
1333 rxvt_fontset::rxvt_fontset (rxvt_term *term)
1334 : fontdesc (0), term (term)
1339 rxvt_fontset::~rxvt_fontset ()
1345 rxvt_fontset::clear ()
1347 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1348 = rxvt_fontprop::unset;
1350 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1353 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1356 free (fontdesc); fontdesc = 0;
1360 fallback = fallback_fonts;
1364 rxvt_fontset::new_font (const char *name, codeset cs)
1368 if (!name || !*name)
1371 f = new rxvt_font_default (this);
1374 else if (!strncmp (name, "xft:", 4))
1377 f = new rxvt_font_xft ();
1380 else if (!strncmp (name, "x:", 2))
1383 f = new rxvt_font_x11;
1386 f = new rxvt_font_x11;
1389 f->set_name (strdup (name));
1397 /////////////////////////////////////////////////////////////////////////////
1400 rxvt_fontset::add_fonts (const char *desc)
1409 while (*desc && *desc <= ' ')
1412 codeset cs = CS_UNICODE;
1417 const char *extra = ++desc; // not yet used
1419 desc = strchr (desc, ']');
1423 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1427 memcpy (spec, extra, min (desc - extra, 255));
1428 spec[min (desc - extra, 255)] = 0;
1430 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1431 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1433 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1436 while (*desc <= ' ') desc++;
1439 end = strchr (desc, ',');
1441 end = desc + strlen (desc);
1443 if (end - desc < 511)
1445 strncpy (buf, desc, end - desc);
1446 buf[end - desc] = 0;
1448 fonts.push_back (new_font (buf, cs));
1451 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1460 rxvt_fontset::realize_font (int i)
1462 if (i < 0 || i >= fonts.size ())
1465 if (fonts[i]->loaded)
1468 fonts[i]->loaded = true;
1470 if (!fonts[i]->load (prop))
1472 fonts[i]->cs = CS_UNKNOWN;
1480 rxvt_fontset::populate (const char *desc)
1484 fontdesc = strdup (desc);
1486 fonts.push_back (new_font (0, CS_UNICODE));
1495 rxvt_fontset::find_font (const char *name) const
1497 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1498 if ((*f)->name && !strcmp ((*f)->name, name))
1499 return f - fonts.begin ();
1505 rxvt_fontset::find_font (unicode_t unicode)
1507 if (unicode >= 1<<20)
1510 unicode_t hi = unicode >> 8;
1512 if (hi < fmap.size ()
1514 && (*fmap[hi])[unicode & 0xff] != 0xff)
1515 return (*fmap[hi])[unicode & 0xff];
1519 for (i = 0; i < fonts.size (); i++)
1521 rxvt_font *f = fonts[i];
1525 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1528 if (!realize_font (i))
1531 if (prop.ascent != rxvt_fontprop::unset)
1532 max_it (f->ascent, prop.ascent);
1535 if (f->cs == CS_UNKNOWN)
1539 if (f->has_char (unicode, &prop, careful))
1548 if (i == fonts.size () - 1)
1552 // search through the fallback list
1553 fonts.push_back (new_font (fallback->name, fallback->cs));
1558 // try to find a new font.
1559 // only xft currently supported, as there is no
1560 // way to configure this and xft is easier to hack in,
1561 // while x11 has more framework in place already.
1562 // TODO: this is a real resource hog, xft takes ages(?)
1563 #if XFT && USE_SLOW_LOOKUP
1564 // grab the first xft font that seems suitable
1565 FcPattern *p = FcPatternCreate ();
1567 FcCharSet *s = FcCharSetCreate ();
1568 FcCharSetAddChar (s, unicode);
1569 FcPatternAddCharSet (p, FC_CHARSET, s);
1570 // charsets don't help that much, as xft might return
1571 // a non-matching font even if a better font is available :/
1573 x x x x TODO prop might have unset contents
1574 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1575 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1576 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1577 FcPatternAddBool (p, FC_MINSPACE, 1);
1578 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1581 FcPattern *match = XftFontMatch (term->xdisp, term->display->screen, p, &result);
1583 FcPatternDestroy (p);
1587 FcPatternDel (match, FC_CHARSET);
1588 char *font = (char *)FcNameUnparse (match);
1589 FcPatternDestroy (match);
1591 if (find_font (font) < 0)
1593 char fontname[4096];
1594 sprintf (fontname, "xft:%-.4090s", font);
1596 fonts.push_back (new_font (fontname, CS_UNICODE));
1606 /* we must return SOME font */
1610 // found a font, cache it
1613 while (hi >= fmap.size ())
1618 fmap[hi] = (pagemap *)new pagemap;
1619 memset (fmap[hi], 0xff, sizeof (pagemap));
1622 (*fmap[hi])[unicode & 0xff] = i;