1 /*--------------------------------*-C-*---------------------------------*;
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003 Marc Lehmann rxvt@plan9.de>
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"
24 #include "defaultfont.h"
26 #define DISPLAY R->Xdisplay
27 #define DRAWABLE R->TermWin.vt
28 #define GC R->TermWin.gc
30 const struct rxvt_fallback_font {
33 } fallback_fonts[] = {
34 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
35 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
40 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
41 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
43 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
44 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
45 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
46 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
47 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
48 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
49 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
50 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
51 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
52 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
53 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
54 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
55 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
59 #if ENCODING_JP || ENCODING_JP_EXT
61 // prefer xft for complex scripts
62 { CS_UNICODE, "xft:Kochi Gothic" },
64 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
65 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
66 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
69 #if ENCODING_CN || ENCODING_CN_EXT
71 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
72 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
73 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
74 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
76 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" },
77 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" },
78 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" },
79 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" },
80 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" },
81 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" },
82 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" },
83 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" },
87 { CS_UNICODE, "xft:Andale Mono" },
88 { CS_UNICODE, "xft:Arial Unicode MS" },
90 { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
91 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
98 /////////////////////////////////////////////////////////////////////////////
100 static void *enc_buf;
101 static uint32_t enc_len;
104 get_enc_buf (int len)
109 enc_buf = malloc (len);
116 enc_char (const text_t *text, int len, codeset cs, bool &zero)
118 uint8_t *buf = (uint8_t *)get_enc_buf (len);
122 uint32_t c = FROM_UNICODE (cs, *text++);
133 return (const char *)enc_buf;
136 static const XChar2b *
137 enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero)
139 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
143 uint32_t c = FROM_UNICODE (cs, *text++);
156 return (XChar2b *)enc_buf;
159 /////////////////////////////////////////////////////////////////////////////
162 rxvt_font::clear_rect (int x, int y, int w, int h, int color)
164 if (color == Color_bg)
165 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE);
168 XSetForeground (DISPLAY, GC, R->PixColors[color]);
169 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h);
173 struct rxvt_font_default : rxvt_font {
174 bool load (int maxheight)
176 width = 1; height = 1;
177 ascent = 1; descent = 0;
182 bool has_codepoint (uint32_t unicode)
184 if (unicode <= 0x001f
185 || (unicode >= 0x80 && unicode <= 0x9f))
190 case ZERO_WIDTH_CHAR:
197 void draw (int x, int y,
198 const text_t *text, int len,
203 rxvt_font_default::draw (int x, int y,
204 const text_t *text, int len,
207 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
214 case ZERO_WIDTH_CHAR:
217 XSetForeground (DISPLAY, GC, R->PixColors[fg]);
218 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, R->TermWin.fwidth - 5, R->TermWin.fheight - 5);
221 x += R->TermWin.fwidth;
225 /////////////////////////////////////////////////////////////////////////////
227 struct rxvt_font_x11 : rxvt_font {
228 rxvt_font_x11 () { f = 0; }
232 bool load (int maxheight);
234 bool has_codepoint (uint32_t unicode);
236 void draw (int x, int y,
237 const text_t *text, int len,
244 const char *get_property (const char *property, const char *repl) const;
248 rxvt_font_x11::get_property (const char *property, const char *repl) const
252 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
253 return XGetAtomName (DISPLAY, value);
259 rxvt_font_x11::load (int maxheight)
263 f = XLoadQueryFont (DISPLAY, name);
270 const char *registry = get_property ("CHARSET_REGISTRY", 0);
271 const char *encoding = get_property ("CHARSET_ENCODING", 0);
273 if (registry && encoding)
276 snprintf (charset, 64, "%s-%s", registry, encoding);
278 cs = codeset_from_name (charset);
282 const char *charset = get_property ("FONT", 0);
289 if (*charset++ == '-' && !--count)
292 cs = codeset_from_name (charset);
295 if (cs == CS_UNICODE)
296 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
298 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
299 enc2b = encm || f->max_char_or_byte2 > 255;
302 descent = f->descent;
303 height = ascent + descent;
307 if (f->min_bounds.width == f->max_bounds.width)
308 width = f->min_bounds.width;
309 else if (f->per_char == NULL)
310 width = f->max_bounds.width;
315 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
318 N += (f->max_byte1 - f->min_byte1)
319 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
323 if (f->per_char[N].width > width)
324 width = f->per_char[N].width;
330 if (cs == CS_UNKNOWN)
332 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
343 rxvt_font_x11::clear ()
347 XFreeFont (DISPLAY, f);
353 rxvt_font_x11::has_codepoint (uint32_t unicode)
355 uint32_t ch = FROM_UNICODE (cs, unicode);
360 /* check wether the character exists in _this_ font. horrible. */
366 int byte2 = ch & 255;
368 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
369 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
375 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
376 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
378 xcs = f->per_char + N;
382 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
388 xcs = f->per_char + (ch - f->min_char_or_byte2);
391 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
392 && xcs->ascent == 0 && xcs->descent == 0)
399 rxvt_font_x11::draw (int x, int y,
400 const text_t *text, int len,
403 // this looks like a mess /.
404 // and it is a mess /.
405 // yet we are trying to be perfect /.
406 // but the result still isn't perfect /.
409 || width != R->TermWin.fwidth
410 || height != R->TermWin.fheight;
412 int base = R->TermWin.fbase;
415 v.foreground = R->PixColors[fg];
416 v.background = R->PixColors[bg];
421 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
423 if (bg == Color_bg && !slow)
425 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
426 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
430 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
432 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
438 if (xc->byte1 || xc->byte2)
439 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
441 x += R->TermWin.fwidth;
447 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
452 const char *xc = enc_char (text, len, cs, slow);
454 if (bg == Color_bg && !slow)
456 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
457 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
461 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
463 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
470 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
472 x += R->TermWin.fwidth;
478 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
483 /////////////////////////////////////////////////////////////////////////////
488 //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
492 struct rxvt_font_xft : rxvt_font {
495 SWATHCOUNT = 1 << (21 - UNIBITS),
496 SWATHSIZE = 1 << (SWATHBITS - 5)
498 typedef uint32_t swath[SWATHSIZE];
500 swath *cvr[SWATHCOUNT];
504 void gen_coverage_swath (unsigned int page);
506 bool has_char (uint32_t ch)
508 unsigned int page = ch >> SWATHBITS;
509 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
511 if (page >= SWATHCOUNT)
514 if (!cvr[page]) gen_coverage_swath (page);
516 return cvr[page][idx >> 5] & (1 << (idx & 31));
519 rxvt_font_xft () { f = 0; d = 0; }
523 bool load (int maxheight);
525 void draw (int x, int y,
526 const text_t *text, int len,
529 bool has_codepoint (uint32_t unicode);
536 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
537 void set_swath (uint32_t ch)
539 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
545 rxvt_font_xft::clear ()
549 XftFontClose (R->Xdisplay, f);
560 for (int i = 0; i < SWATHCOUNT; i++)
566 rxvt_font_xft::load (int maxheight)
569 for (int i = 0; i < SWATHCOUNT; i++)
575 f = XftFontOpenName (R->Xdisplay, DefaultScreen (R->Xdisplay), name);
580 FT_Face face = XftLockFace (f);
582 prop = !FT_IS_FIXED_WIDTH (face);
591 c = 'i'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g1);
592 c = 'W'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g2);
594 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font
597 ascent = (face->size->metrics.ascender + 63) >> 6;
598 descent = (-face->size->metrics.descender + 63) >> 6;
599 height = ascent + descent;
601 if (height <= maxheight || !maxheight)
606 // take smaller steps near the end
607 if (height > maxheight + 1) ftheight++;
608 if (height > maxheight + 2) ftheight++;
609 if (height > maxheight + 3) ftheight++;
611 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight);
614 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight);
623 void rxvt_font::gen_coverage_swath (unsigned int page)
625 cvr[page] = new swath;
627 for (int i = 0; i < SWATHSIZE; i++)
630 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
635 rxvt_font_xft::has_codepoint (uint32_t unicode)
637 return XftCharExists (R->Xdisplay, f, unicode);
641 rxvt_font_xft::draw (int x, int y,
642 const text_t *text, int len,
646 d = XftDrawCreate (R->Xdisplay, DRAWABLE, XVISUAL, XCMAP);
648 if (bg >= 0 && bg != Color_bg)
649 XftDrawRect (d, &R->PixColors[bg].c, x, y, R->TermWin.fwidth * len, R->TermWin.fheight);
651 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
653 if (!prop && width == R->TermWin.fwidth)
655 if (sizeof (text_t) == sizeof (FcChar16))
656 XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, len);
658 XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, len);
664 if (*text != NOCHAR && *text != ' ')
666 if (sizeof (text_t) == sizeof (FcChar16))
667 XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, 1);
669 XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, 1);
672 x += R->TermWin.fwidth;
680 /////////////////////////////////////////////////////////////////////////////
682 rxvt_fontset::rxvt_fontset (pR)
683 #ifdef EXPLICIT_CONTEXT
690 rxvt_fontset::~rxvt_fontset ()
696 rxvt_fontset::clear ()
698 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
705 fallback = fallback_fonts;
709 rxvt_fontset::new_font (const char *name, codeset cs)
716 f = new rxvt_font_default;
719 else if (!strncmp (name, "xft:", 4))
722 f = new rxvt_font_xft;
725 else if (!strncmp (name, "x:", 2))
728 f = new rxvt_font_x11;
731 f = new rxvt_font_x11;
734 f->set_name (strdup (name));
742 /////////////////////////////////////////////////////////////////////////////
745 rxvt_fontset::add_fonts (const char *desc)
754 while (*desc <= ' ') desc++;
758 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
760 const char *extra = desc++;
762 desc = strchr (desc, ']');
766 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
771 while (*desc <= ' ') desc++;
774 end = strchr (desc, ',');
776 end = desc + strlen (desc);
778 if (end - desc < 511)
780 strncpy (buf, desc, end - desc);
783 fonts.push_back (new_font (buf, CS_UNICODE));
793 rxvt_fontset::realize_font (int i)
795 if (fonts[i]->loaded)
798 if (fonts[i]->load (height))
799 return fonts[i]->loaded = true;
802 fonts.erase (fonts.begin () + i);
808 rxvt_fontset::populate (const char *desc)
812 fonts.push_back (new_font (0, CS_UNICODE));
820 // we currently need a base-font, no matter what
821 if (fonts.size () <= base_id)
827 if (fonts.size () <= base_id || !realize_font (base_id))
829 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
833 height = fonts[base_id]->height;
835 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
839 rxvt_fontset::find_font (uint32_t unicode)
841 for (int i = 0; i < fonts.size (); i++)
843 rxvt_font *f = fonts[i];
847 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
850 if (!realize_font (i))
856 //printf ("added font %s for %04lx\n", f->name, unicode);
859 if (f->has_codepoint (unicode))
863 if (i == fonts.size () - 1 && fallback->name)
865 fonts.push_back (new_font (fallback->name, fallback->cs));
870 return 0; /* we must return SOME font */