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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *---------------------------------------------------------------------*/
22 #include "../config.h"
28 #define DISPLAY r->display->display
29 #define TGC r->TermWin.gc
31 const struct rxvt_fallback_font {
34 } fallback_fonts[] = {
35 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
37 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
41 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
42 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
44 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
45 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
46 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
47 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
48 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
49 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
50 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
51 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
60 #if ENCODING_JP || ENCODING_JP_EXT
62 // prefer xft for complex scripts
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
69 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
70 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
73 #if ENCODING_CN || ENCODING_CN_EXT
75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
80 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
81 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
82 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
83 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
88 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
95 { CS_UNICODE, "xft:Andale Mono" },
96 { CS_UNICODE, "xft:Arial Unicode MS" },
98 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
99 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
100 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
103 // FreeMono is usually uglier than x fonts, so try last only.
104 //{ CS_UNICODE, "xft:FreeMono" },
110 /////////////////////////////////////////////////////////////////////////////
113 rxvt_drawable::~rxvt_drawable ()
116 XftDrawDestroy (xftdrawable);
119 rxvt_drawable::operator XftDraw *()
122 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
128 /////////////////////////////////////////////////////////////////////////////
130 static void *enc_buf;
131 static uint32_t enc_len;
134 get_enc_buf (uint32_t len)
139 enc_buf = malloc (len);
147 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
149 uint8_t *buf = (uint8_t *)get_enc_buf (len);
153 uint32_t c = FROM_UNICODE (cs, *text++);
164 return (const char *)enc_buf;
167 static const XChar2b *
168 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
170 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
174 uint32_t c = FROM_UNICODE (cs, *text++);
187 return (XChar2b *)enc_buf;
190 /////////////////////////////////////////////////////////////////////////////
193 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
195 if (color == Color_bg)
196 XClearArea (d.display->display, d, x, y, w, h, FALSE);
200 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
202 XSetForeground (d.display->display, TGC, r->PixColors[color]);
203 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
208 #include "table/linedraw.h"
210 struct rxvt_font_default : rxvt_font {
212 rxvt_fontprop properties ()
216 p.width = p.height = 1;
217 p.weight = rxvt_fontprop::medium;
218 p.slant = rxvt_fontprop::roman;
223 bool load (const rxvt_fontprop &prop)
225 width = 1; height = 1;
226 ascent = 1; descent = 0;
228 set_name (strdup ("built-in pseudofont"));
233 bool has_codepoint (unicode_t unicode)
235 if (unicode <= 0x001f)
238 if (unicode <= 0x007f)
241 if (unicode <= 0x009f)
244 if (unicode >= 0x2500 && unicode <= 0x259f)
247 if (IS_COMPOSE (unicode))
252 case ZERO_WIDTH_CHAR:
259 void draw (rxvt_drawable &d, int x, int y,
260 const text_t *text, int len,
265 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
266 const text_t *text, int len,
269 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
271 XSetForeground (d.display->display, TGC, r->PixColors[fg]);
280 if (0x2500 <= t && t <= 0x259f)
282 uint16_t offs = linedraw_offs[t - 0x2500];
283 uint32_t *a = linedraw_command + (offs >> 4);
284 uint32_t *b = a + (offs & 15);
286 int W = r->TermWin.fwidth;
287 int H = r->TermWin.fheight;
292 for (int i = 0; i <= 8; i++)
294 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
295 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
298 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
299 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
303 gcv.cap_style = CapButt;
305 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv);
309 uint32_t command = *a++;
311 int op = (command >> 24) & 255;
312 int a = (command >> 20) & 15;
313 int b = (command >> 16) & 15;
314 int x1 = x_[(command >> 12) & 15];
315 int y1 = y_[(command >> 8) & 15];
316 int x2 = x_[(command >> 4) & 15];
317 int y2 = y_[(command >> 0) & 15];
322 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2);
325 case 1: // rectangle, possibly stippled
328 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
330 gcv.fill_style = FillStippled;
331 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2);
335 XChangeGC (d.display->display, TGC,
336 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
340 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
344 XFreePixmap (d.display->display, gcv.stipple);
346 gcv.fill_style = FillSolid;
347 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv);
351 XDrawArc (d.display->display, d, TGC,
352 x1 - W/2, y1 - H/2, W-1, H-1,
353 (a - 1) * 90*64, (b - 1) * 90*64);
359 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
361 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
362 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
363 if (cc->c2 != NOCHAR)
365 // prefer font of first character, for no good reasons
366 rxvt_font *f2 = f1->has_codepoint (cc->c2)
368 : (*fs)[fs->find_font (cc->c2)];
370 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
377 case ZERO_WIDTH_CHAR:
382 while (len > 0 && *text == NOCHAR)
386 w += r->TermWin.fwidth;
389 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2,
390 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4);
394 x += r->TermWin.fwidth;
398 /////////////////////////////////////////////////////////////////////////////
400 struct rxvt_font_x11 : rxvt_font {
401 rxvt_font_x11 () { f = 0; }
405 rxvt_fontprop properties ();
407 bool load (const rxvt_fontprop &prop);
409 bool has_codepoint (unicode_t unicode);
411 void draw (rxvt_drawable &d, int x, int y,
412 const text_t *text, int len,
419 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
420 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
421 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
422 bool set_properties (rxvt_fontprop &p, const char *name);
426 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
430 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
431 return XGetAtomName (DISPLAY, value);
433 return rxvt_strdup (repl);
437 rxvt_font_x11::properties ()
440 set_properties (p, f);
445 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
447 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
449 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
450 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
456 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
458 unsigned long height;
459 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
462 unsigned long avgwidth;
463 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
466 char *weight = get_property (f, "WEIGHT_NAME", "medium");
467 char *slant = get_property (f, "SLANT", "r");
469 set_properties (p, height, weight, slant, avgwidth);
478 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
481 const char *comp[13];
483 for (const char *c = name; *c; c++)
486 comp[slashes++] = c + 1;
491 /* can we short-circuit the costly XLoadQueryFont? */
493 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
494 && (*comp[11] >= '0' && *comp[11] <= '9'))
495 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
497 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
501 // the font should really exist now. if not, we have a problem
502 // (e.g. if the user did xset fp rehash just when we were searching fonts).
503 // in that case, just return garbage.
504 bool ret = set_properties (p, f);
505 XFreeFont (DISPLAY, f);
512 // fix the size of scalable fonts
514 fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
519 for (const char *c = name; *c; c++)
529 if (slashes >= 13 && size[0] == '0')
531 strncpy (buf, name, size - name);
533 buf += sprintf (buf, "%d", prop.height);
534 strcpy (buf, size + 1);
541 rxvt_font_x11::load (const rxvt_fontprop &prop)
547 list = XListFonts (DISPLAY, name, 1024, &count);
553 int bestdiff = 0x7fffffff;
554 for (int i = 0; i < count; i++)
558 fix_scalable (fname, list[i], prop);
560 if (!set_properties (p, fname))
563 if (p.height > prop.height) // weed out too large fonts
566 int diff = (prop.height - p.height) * 32
567 + abs (prop.weight - p.weight)
568 + abs (prop.slant - p.slant );
570 if (!name // compare against best found so far
573 set_name (strdup (fname));
578 XFreeFontNames (list);
583 f = XLoadQueryFont (DISPLAY, name);
588 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
589 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
591 if (registry && encoding)
594 snprintf (charset, 64, "%s-%s", registry, encoding);
596 cs = codeset_from_name (charset);
600 const char *charset = get_property (f, "FONT", 0);
607 if (*charset++ == '-' && !--count)
610 cs = codeset_from_name (charset);
616 if (cs == CS_UNICODE)
617 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
619 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
620 enc2b = encm || f->max_char_or_byte2 > 255;
623 descent = f->descent;
624 height = ascent + descent;
628 if (f->min_bounds.width == f->max_bounds.width)
629 width = f->min_bounds.width;
630 else if (f->per_char == NULL)
631 width = f->max_bounds.width;
636 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
639 N += (f->max_byte1 - f->min_byte1)
640 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
644 if (f->per_char[N].width > width)
645 width = f->per_char[N].width;
651 if (cs == CS_UNKNOWN)
653 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
664 rxvt_font_x11::clear ()
668 XFreeFont (DISPLAY, f);
674 rxvt_font_x11::has_codepoint (unicode_t unicode)
676 uint32_t ch = FROM_UNICODE (cs, unicode);
681 /* check wether the character exists in _this_ font. horrible. */
686 unsigned char byte1 = ch >> 8;
687 unsigned char byte2 = ch & 255;
689 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
690 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
696 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
697 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
699 xcs = f->per_char + N;
703 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
709 xcs = f->per_char + (ch - f->min_char_or_byte2);
712 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
713 && xcs->ascent == 0 && xcs->descent == 0)
720 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
721 const text_t *text, int len,
724 // this looks like a mess /.
725 // and it is a mess /.
726 // yet we are trying to be perfect /.
727 // but the result still isn't perfect /.
729 bool slow = this->slow
730 || width != r->TermWin.fwidth
731 || height != r->TermWin.fheight;
733 int base = r->TermWin.fbase;
736 v.foreground = r->PixColors[fg];
741 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
743 if (bg == Color_bg && !slow)
745 v.background = r->PixColors[bg];
746 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
747 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
751 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
753 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
759 if (xc->byte1 || xc->byte2)
760 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
762 x += r->TermWin.fwidth;
768 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
773 const char *xc = enc_char (text, len, cs, slow);
775 if (bg == Color_bg && !slow)
777 v.background = r->PixColors[bg];
778 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
779 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
783 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
785 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
792 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
794 x += r->TermWin.fwidth;
800 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
805 /////////////////////////////////////////////////////////////////////////////
810 //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
814 struct rxvt_font_xft : rxvt_font {
815 rxvt_font_xft () { f = 0; }
819 rxvt_fontprop properties ();
821 bool load (const rxvt_fontprop &prop);
823 void draw (rxvt_drawable &d, int x, int y,
824 const text_t *text, int len,
827 bool has_codepoint (unicode_t unicode);
834 rxvt_font_xft::clear ()
838 XftFontClose (DISPLAY, f);
844 rxvt_font_xft::properties ()
848 FT_Face face = XftLockFace (f);
852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
853 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
854 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
855 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
863 rxvt_font_xft::load (const rxvt_fontprop &prop)
866 for (int i = 0; i < SWATHCOUNT; i++)
872 FcPattern *p = FcNameParse ((FcChar8 *) name);
879 if (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch)
880 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
882 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
883 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
885 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
886 FcPatternAddInteger (p, FC_SLANT, prop.slant);
888 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
889 FcPatternAddBool (p, FC_MINSPACE, 1);
891 #if 0 // clipping unfortunately destroys our precious double-width-characters
892 // clip width, we can't do better, or can we?
893 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
894 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
898 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
900 FcPatternDestroy (p);
905 f = XftFontOpenPattern (DISPLAY, match);
909 FcPatternDestroy (match);
913 FT_Face face = XftLockFace (f);
915 slow = !FT_IS_FIXED_WIDTH (face);
924 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
925 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
927 if (g1.xOff != g2.xOff) // don't simply trust the font
931 ascent = (face->size->metrics.ascender + 63) >> 6;
932 descent = (-face->size->metrics.descender + 63) >> 6;
933 height = ascent + descent;
935 if (height <= prop.height || !prop.height)
940 // take smaller steps near the end
941 if (height > prop.height + 1) ftheight++;
942 if (height > prop.height + 2) ftheight++;
943 if (height > prop.height + 3) ftheight++;
945 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
948 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
957 rxvt_font_xft::has_codepoint (unicode_t unicode)
959 return XftCharExists (DISPLAY, f, unicode);
963 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len,
967 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
969 if (!slow && width == r->TermWin.fwidth && 0)
971 if (sizeof (text_t) == sizeof (FcChar16))
972 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
974 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
980 if (*text != NOCHAR && *text != ' ')
982 int fwidth = r->TermWin.fwidth;
983 if (len >= 2 && text[1] == NOCHAR)
987 if (sizeof (text_t) == sizeof (FcChar16))
989 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
990 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
991 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
996 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
997 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
998 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
1002 x += r->TermWin.fwidth;
1010 /////////////////////////////////////////////////////////////////////////////
1012 rxvt_fontset::rxvt_fontset (rxvt_t r)
1013 : r (r), fontdesc (0)
1018 rxvt_fontset::~rxvt_fontset ()
1024 rxvt_fontset::clear ()
1026 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1029 free (fontdesc); fontdesc = 0;
1033 base_prop.height = 0x7fffffff;
1034 base_prop.weight = rxvt_fontprop::medium;
1035 base_prop.slant = rxvt_fontprop::roman;
1037 fallback = fallback_fonts;
1041 rxvt_fontset::new_font (const char *name, codeset cs)
1045 if (!name || !*name)
1048 f = new rxvt_font_default;
1051 else if (!strncmp (name, "xft:", 4))
1054 f = new rxvt_font_xft;
1057 else if (!strncmp (name, "x:", 2))
1060 f = new rxvt_font_x11;
1063 f = new rxvt_font_x11;
1067 f->set_name (strdup (name));
1075 /////////////////////////////////////////////////////////////////////////////
1078 rxvt_fontset::add_fonts (const char *desc)
1087 while (*desc <= ' ') desc++;
1091 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
1093 //const char *extra = desc++; // not yet used
1095 desc = strchr (desc, ']');
1099 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
1104 while (*desc <= ' ') desc++;
1107 end = strchr (desc, ',');
1109 end = desc + strlen (desc);
1111 if (end - desc < 511)
1113 strncpy (buf, desc, end - desc);
1114 buf[end - desc] = 0;
1116 fonts.push_back (new_font (buf, CS_UNICODE));
1126 rxvt_fontset::realize_font (int i)
1128 if (fonts[i]->loaded)
1131 fonts[i]->loaded = true;
1133 if (!fonts[i]->load (base_prop))
1135 fonts[i]->cs = CS_UNKNOWN;
1143 rxvt_fontset::populate (const char *desc)
1147 fontdesc = strdup (desc);
1149 fonts.push_back (new_font (0, CS_UNICODE));
1157 // we currently need a base-font, no matter what
1158 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1160 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1161 add_fonts ("fixed");
1162 base_id = fonts.size () - 1;
1165 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1168 base_prop = fonts[base_id]->properties ();
1174 rxvt_fontset::find_font (const char *name) const
1176 for (rxvt_font *const *f = fonts.begin (); f < fonts.end (); f++)
1177 if ((*f)->name && !strcmp ((*f)->name, name))
1178 return f - fonts.begin ();
1184 rxvt_fontset::find_font (unicode_t unicode, bool bold)
1187 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii
1191 rxvt_font *f = fonts[i];
1195 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1198 if (!realize_font (i))
1202 if (f->cs == CS_UNKNOWN)
1205 if (bold && f->properties ().weight < rxvt_fontprop::bold)
1208 if (f->has_codepoint (unicode))
1212 if (i == fonts.size () - 1)
1216 // search through the fallback list
1217 fonts.push_back (new_font (fallback->name, fallback->cs));
1222 // try to find a new font.
1223 // only xft currently supported, as there is no
1224 // way to configure this and xft is easier to hack in,
1225 // while x11 has more framework in place already.
1227 // grab the first xft font that seems suitable
1228 FcPattern *p = FcPatternCreate ();
1230 FcCharSet *s = FcCharSetCreate ();
1231 FcCharSetAddChar (s, unicode);
1232 FcPatternAddCharSet (p, FC_CHARSET, s);
1233 // charsets don't help that much, as xft might return
1234 // a non-matching font even if a better font is available :/
1236 FcPatternAddInteger (p, FC_PIXEL_SIZE, base_prop.height);
1237 FcPatternAddInteger (p, FC_WEIGHT, base_prop.weight);
1238 FcPatternAddInteger (p, FC_SLANT, base_prop.slant);
1239 FcPatternAddBool (p, FC_MINSPACE, 1);
1240 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1243 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
1245 FcPatternDestroy (p);
1249 FcPatternDel (match, FC_CHARSET);
1250 char *font = (char *)FcNameUnparse (match);
1251 FcPatternDestroy (match);
1253 if (find_font (font) < 0)
1255 char fontname[4096];
1256 sprintf (fontname, "xft:%-.4090s", font);
1258 fonts.push_back (new_font (fontname, CS_UNICODE));
1268 // if no bold font found, use a regular one
1270 return find_font (unicode);
1272 return 0; /* we must return SOME font */