ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
(Generate patch)

Comparing rxvt-unicode/src/defaultfont.C (file contents):
Revision 1.2 by pcg, Tue Nov 25 15:25:17 2003 UTC vs.
Revision 1.56 by root, Mon Aug 2 04:42:11 2004 UTC

1/*--------------------------------*-C-*---------------------------------*; 1/*--------------------------------*-C-*---------------------------------*
2 * File: defaultfont.C 2 * File: defaultfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003 Marc Lehmann rxvt@plan9.de> 4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 * - original version. 5 * - original version.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 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 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 9 * the Free Software Foundation; either version 2 of the License, or
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib>
27
26#define DISPLAY r->Xdisplay 28#define DISPLAY r->display->display
27#define DRAWABLE r->TermWin.vt
28#define GC r->TermWin.gc 29#define TGC r->TermWin.gc
29 30
30const struct rxvt_fallback_font { 31const struct rxvt_fallback_font {
31 codeset cs; 32 codeset cs;
32 const char *name; 33 const char *name;
33} fallback_fonts[] = { 34} fallback_fonts[] = {
34 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" }, 35 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
35 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" }, 36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" }, 37 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
37 38
38#if ENCODING_EU 39#if ENCODING_EU
39 // cyrillic 40 // cyrillic
40 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" }, 41 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
41 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" }, 42 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
42 43
43 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" }, 44 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
44 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" }, 45 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
45 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" }, 46 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
46 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" }, 47 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
47 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" }, 48 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
48 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" }, 49 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
49 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" }, 50 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
50 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" }, 51 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
51 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, 52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
52 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, 53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
53 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, 54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
54 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, 55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
55 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, 56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
56#endif 57#endif
57 58
58 // japanese 59 // japanese
59#if ENCODING_JP || ENCODING_JP_EXT 60#if ENCODING_JP || ENCODING_JP_EXT
60# if XFT 61# if XFT
61 // prefer xft for complex scripts 62 // prefer xft for complex scripts
62 { CS_UNICODE, "xft:Kochi Gothic" }, 63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
63# endif 64# endif
64 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
65 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
66 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-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" },
67#endif 71#endif
68 72
69#if ENCODING_CN || ENCODING_CN_EXT 73#if ENCODING_CN || ENCODING_CN_EXT
70# if XFT 74# if XFT
71 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
72 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
73 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
74 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
75# endif 79# endif
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" },
76 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
77 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
78 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
79 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
80 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 88 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
81 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
82 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
83 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
84#endif 92#endif
85 93
86#if XFT 94#if XFT
87 { CS_UNICODE, "xft:Andale Mono" }, 95 { CS_UNICODE, "xft:Andale Mono" },
88 { CS_UNICODE, "xft:Arial Unicode MS" }, 96 { CS_UNICODE, "xft:Arial Unicode MS" },
97 { CS_UNICODE, "xft:FreeMono" },
89#endif 98#endif
90 { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 99 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
100 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
91 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 102 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 103
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 104#if UNICODE_3 && XFT
105 { CS_UNICODE, "xft:Code2001" }, // contains many plane-1 characters
106#endif
94 107
95 { CS_UNKNOWN, 0 } 108 { CS_UNKNOWN, 0 }
96}; 109};
97 110
98///////////////////////////////////////////////////////////////////////////// 111/////////////////////////////////////////////////////////////////////////////
99 112
113#if XFT
114rxvt_drawable::~rxvt_drawable ()
115{
116 if (xftdrawable)
117 XftDrawDestroy (xftdrawable);
118}
119
120rxvt_drawable::operator XftDraw *()
121{
122 if (!xftdrawable)
123 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
124
125 return xftdrawable;
126}
127#endif
128
129/////////////////////////////////////////////////////////////////////////////
130
100static void *enc_buf; 131static void *enc_buf;
101static uint32_t enc_len; 132static uint32_t enc_len;
102 133
103static inline void * 134static inline void *
104get_enc_buf (int len) 135get_enc_buf (uint32_t len)
105{ 136{
106 if (len > enc_len) 137 if (len > enc_len)
107 { 138 {
108 free (enc_buf); 139 free (enc_buf);
109 enc_buf = malloc (len); 140 enc_buf = malloc (len);
141 enc_len = len;
110 } 142 }
111 143
112 return enc_buf; 144 return enc_buf;
113} 145}
114 146
115static const char * 147static const char *
116enc_char (const text_t *text, int len, codeset cs, bool &zero) 148enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
117{ 149{
118 uint8_t *buf = (uint8_t *)get_enc_buf (len); 150 uint8_t *buf = (uint8_t *)get_enc_buf (len);
119 151
120 while (len--) 152 while (len--)
121 { 153 {
132 164
133 return (const char *)enc_buf; 165 return (const char *)enc_buf;
134} 166}
135 167
136static const XChar2b * 168static const XChar2b *
137enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 169enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
138{ 170{
139 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 171 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
140 172
141 while (len--) 173 while (len--)
142 { 174 {
157} 189}
158 190
159///////////////////////////////////////////////////////////////////////////// 191/////////////////////////////////////////////////////////////////////////////
160 192
161void 193void
162rxvt_font::clear_rect (int x, int y, int w, int h, int color) 194rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
163{ 195{
164 if (color == Color_bg) 196 if (color == Color_bg)
165 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 197 XClearArea (d.display->display, d, x, y, w, h, FALSE);
166 else if (color >= 0) 198 else if (color >= 0)
167 { 199 {
200#if XFT
201 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
202#else
168 XSetForeground (DISPLAY, GC, r->PixColors[color]); 203 XSetForeground (d.display->display, TGC, r->PixColors[color]);
169 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 204 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
205#endif
170 } 206 }
171} 207}
208
209#include "table/linedraw.h"
172 210
173struct rxvt_font_default : rxvt_font { 211struct rxvt_font_default : rxvt_font {
174 bool load (int maxheight) 212
213 rxvt_fontprop properties ()
214 {
215 rxvt_fontprop p;
216
217 p.width = p.height = 1;
218 p.weight = rxvt_fontprop::medium;
219 p.slant = rxvt_fontprop::roman;
220
221 return p;
222 }
223
224 bool load (const rxvt_fontprop &prop)
175 { 225 {
176 width = 1; height = 1; 226 width = 1; height = 1;
177 ascent = 1; descent = 0; 227 ascent = 1; descent = 0;
178 228
229 set_name (strdup ("built-in pseudofont"));
230
179 return true; 231 return true;
180 } 232 }
181 233
182 bool has_codepoint (uint32_t unicode) 234 bool has_codepoint (unicode_t unicode)
183 { 235 {
184 if (unicode <= 0x001f 236 if (unicode <= 0x001f)
237 return true;
238
239 if (unicode <= 0x007f)
240 return false;
241
242 if (unicode <= 0x009f)
243 return true;
244
185 || (unicode >= 0x80 && unicode <= 0x9f)) 245 if (unicode >= 0x2500 && unicode <= 0x259f)
246 return true;
247
248 if (IS_COMPOSE (unicode))
186 return true; 249 return true;
187 250
188 switch (unicode) 251 switch (unicode)
189 { 252 {
190 case ZERO_WIDTH_CHAR: 253 case ZERO_WIDTH_CHAR:
192 } 255 }
193 256
194 return false; 257 return false;
195 } 258 }
196 259
197 void draw (int x, int y, 260 void draw (rxvt_drawable &d, int x, int y,
198 const text_t *text, int len, 261 const text_t *text, int len,
199 int fg, int bg); 262 int fg, int bg);
200}; 263};
201 264
202void 265void
203rxvt_font_default::draw (int x, int y, 266rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
204 const text_t *text, int len, 267 const text_t *text, int len,
205 int fg, int bg) 268 int fg, int bg)
206{ 269{
207 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 270 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
271
272 XSetForeground (d.display->display, TGC, r->PixColors[fg]);
208 273
209 while (len--) 274 while (len--)
210 { 275 {
276#if ENABLE_COMBINING
277 compose_char *cc;
278#endif
279 text_t t = *text++;
280
281 if (0x2500 <= t && t <= 0x259f)
282 {
283 uint16_t offs = linedraw_offs[t - 0x2500];
284 uint32_t *a = linedraw_command + (offs >> 4);
285 uint32_t *b = a + (offs & 15);
286
287 int W = r->TermWin.fwidth;
288 int H = r->TermWin.fheight;
289
290 int x_[16];
291 int y_[16];
292
293 for (int i = 0; i <= 8; i++)
294 {
295 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
296 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
297 }
298
299 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
300 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
301
302 XGCValues gcv;
303
304 gcv.cap_style = CapButt;
305 gcv.line_width = 0;
306 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv);
307
308 while (a < b)
309 {
310 uint32_t command = *a++;
311
312 int op = (command >> 24) & 255;
313 int a = (command >> 20) & 15;
314 int b = (command >> 16) & 15;
315 int x1 = x_[(command >> 12) & 15];
316 int y1 = y_[(command >> 8) & 15];
317 int x2 = x_[(command >> 4) & 15];
318 int y2 = y_[(command >> 0) & 15];
319
320 switch (op)
321 {
322 case 0: // line
323 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2);
324 break;
325
326 case 1: // rectangle, possibly stippled
327 if (a)
328 {
329 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
330
331 gcv.fill_style = FillStippled;
332 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2);
333 gcv.ts_x_origin = x;
334 gcv.ts_y_origin = y;
335
336 XChangeGC (d.display->display, TGC,
337 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
338 &gcv);
339 }
340
341 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
342
343 if (a)
344 {
345 XFreePixmap (d.display->display, gcv.stipple);
346 gcv.stipple = 0;
347 gcv.fill_style = FillSolid;
348 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv);
349 }
350 break;
351 case 2: // arc
352 XDrawArc (d.display->display, d, TGC,
353 x1 - W/2, y1 - H/2, W-1, H-1,
354 (a - 1) * 90*64, (b - 1) * 90*64);
355 break;
356 }
357 }
358 }
359#if ENABLE_COMBINING
360 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
361 {
362 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
363 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
364 if (cc->c2 != NOCHAR)
365 {
366 // prefer font of first character, for no good reasons
367 rxvt_font *f2 = f1->has_codepoint (cc->c2)
368 ? f1
369 : (*fs)[fs->find_font (cc->c2)];
370
371 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
372 }
373 }
374#endif
375 else
211 switch (*text++) 376 switch (t)
212 { 377 {
213 case NOCHAR:
214 case ZERO_WIDTH_CHAR: 378 case ZERO_WIDTH_CHAR:
215 break; 379 break;
380
216 default: 381 default:
217 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 382 int w = 0;
218 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 383 while (len > 0 && *text == NOCHAR)
384 {
385 ++text;
386 --len;
387 w += r->TermWin.fwidth;
388 }
389
390 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2,
391 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4);
392 x += w;
219 } 393 }
220 394
221 x += r->TermWin.fwidth; 395 x += r->TermWin.fwidth;
222 } 396 }
223} 397}
224 398
227struct rxvt_font_x11 : rxvt_font { 401struct rxvt_font_x11 : rxvt_font {
228 rxvt_font_x11 () { f = 0; } 402 rxvt_font_x11 () { f = 0; }
229 403
230 void clear (); 404 void clear ();
231 405
232 bool load (int maxheight); 406 rxvt_fontprop properties ();
233 407
408 bool load (const rxvt_fontprop &prop);
409
234 bool has_codepoint (uint32_t unicode); 410 bool has_codepoint (unicode_t unicode);
235 411
236 void draw (int x, int y, 412 void draw (rxvt_drawable &d, int x, int y,
237 const text_t *text, int len, 413 const text_t *text, int len,
238 int fg, int bg); 414 int fg, int bg);
239 415
240 XFontStruct *f; 416 XFontStruct *f;
241 codeset cs; 417 codeset cs;
242 bool enc2b, encm; 418 bool enc2b, encm;
243 419
244 const char *get_property (const char *property, const char *repl) const; 420 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
421 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
422 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
423 bool set_properties (rxvt_fontprop &p, const char *name);
245}; 424};
246 425
247const char * 426char *
248rxvt_font_x11::get_property (const char *property, const char *repl) const 427rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
249{ 428{
250 unsigned long value; 429 unsigned long value;
251 430
252 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 431 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
253 return XGetAtomName (DISPLAY, value); 432 return XGetAtomName (DISPLAY, value);
254 else 433 else
434 return rxvt_strdup (repl);
435}
436
437rxvt_fontprop
438rxvt_font_x11::properties ()
439{
440 rxvt_fontprop p;
441 set_properties (p, f);
255 return repl; 442 return p;
256} 443}
257 444
258bool 445bool
259rxvt_font_x11::load (int maxheight) 446rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
447{
448 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
449 p.height = height;
450 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
451 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
452
453 return true;
454}
455
456bool
457rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
458{
459 unsigned long height;
460 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
461 return false;
462
463 unsigned long avgwidth;
464 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
465 avgwidth = 0;
466
467 char *weight = get_property (f, "WEIGHT_NAME", "medium");
468 char *slant = get_property (f, "SLANT", "r");
469
470 set_properties (p, height, weight, slant, avgwidth);
471
472 free (weight);
473 free (slant);
474
475 return true;
476}
477
478bool
479rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
480{
481 int slashes = 0;
482 const char *comp[13];
483
484 for (const char *c = name; *c; c++)
485 if (*c == '-')
486 {
487 comp[slashes++] = c + 1;
488 if (slashes >= 13)
489 break;
490 }
491
492 /* can we short-circuit the costly XLoadQueryFont? */
493 if (slashes >= 13
494 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
495 && (*comp[11] >= '0' && *comp[11] <= '9'))
496 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
497
498 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
499
500 if (f)
501 {
502 // the font should really exist now. if not, we have a problem
503 // (e.g. if the user did xset fp rehash just when we were searching fonts).
504 // in that case, just return garbage.
505 bool ret = set_properties (p, f);
506 XFreeFont (DISPLAY, f);
507 return ret;
508 }
509 else
510 return false;
511}
512
513// fix the size of scalable fonts
514static void
515fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
516{
517 int slashes = 0;
518 const char *size;
519
520 for (const char *c = name; *c; c++)
521 if (*c == '-')
522 {
523 if (slashes == 6)
524 size = c + 1;
525
526 if (++slashes >= 13)
527 break;
528 }
529
530 if (slashes >= 13 && size[0] == '0')
531 {
532 strncpy (buf, name, size - name);
533 buf += size - name;
534 buf += sprintf (buf, "%d", prop.height);
535 strcpy (buf, size + 1);
536 }
537 else
538 strcpy (buf, name);
539}
540
541bool
542rxvt_font_x11::load (const rxvt_fontprop &prop)
260{ 543{
261 clear (); 544 clear ();
545
546 char **list;
547 int count;
548 list = XListFonts (DISPLAY, name, 512, &count);
549 set_name (0);
550
551 if (!list)
552 return false;
553
554 int bestdiff = 0x7fffffff;
555 for (int i = 0; i < count; i++)
556 {
557 rxvt_fontprop p;
558 char fname[1024];
559 fix_scalable (fname, list[i], prop);
560
561 if (!set_properties (p, fname))
562 continue;
563
564 if (p.height > prop.height) // weed out too large fonts
565 continue;
566
567 int diff = (prop.height - p.height) * 32
568 + abs (prop.weight - p.weight)
569 + abs (prop.slant - p.slant );
570
571 if (!name // compare against best found so far
572 || diff < bestdiff)
573 {
574 set_name (strdup (fname));
575 bestdiff = diff;
576 }
577 }
578
579 XFreeFontNames (list);
580
581 if (!name)
582 return false;
262 583
263 f = XLoadQueryFont (DISPLAY, name); 584 f = XLoadQueryFont (DISPLAY, name);
264 585
265 if (!f) 586 if (!f)
266 return false; 587 return false;
267 588
268 unsigned long value;
269
270 const char *registry = get_property ("CHARSET_REGISTRY", 0); 589 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
271 const char *encoding = get_property ("CHARSET_ENCODING", 0); 590 char *encoding = get_property (f, "CHARSET_ENCODING", 0);
272 591
273 if (registry && encoding) 592 if (registry && encoding)
274 { 593 {
275 char charset[64]; 594 char charset[64];
276 snprintf (charset, 64, "%s-%s", registry, encoding); 595 snprintf (charset, 64, "%s-%s", registry, encoding);
277 596
278 cs = codeset_from_name (charset); 597 cs = codeset_from_name (charset);
279 } 598 }
280 else 599 else
281 { 600 {
282 const char *charset = get_property ("FONT", 0); 601 const char *charset = get_property (f, "FONT", 0);
283 602
284 if (!charset) 603 if (!charset)
285 charset = name; 604 charset = name;
286 605
287 int count = 13; 606 int count = 13;
290 break; 609 break;
291 610
292 cs = codeset_from_name (charset); 611 cs = codeset_from_name (charset);
293 } 612 }
294 613
614 free (registry);
615 free (encoding);
616
295 if (cs == CS_UNICODE) 617 if (cs == CS_UNICODE)
296 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 618 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
297 619
298 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 620 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
299 enc2b = encm || f->max_char_or_byte2 > 255; 621 enc2b = encm || f->max_char_or_byte2 > 255;
300 622
301 ascent = f->ascent; 623 ascent = f->ascent;
302 descent = f->descent; 624 descent = f->descent;
303 height = ascent + descent; 625 height = ascent + descent;
304 626
305 prop = false; 627 slow = false;
306 628
307 if (f->min_bounds.width == f->max_bounds.width) 629 if (f->min_bounds.width == f->max_bounds.width)
308 width = f->min_bounds.width; 630 width = f->min_bounds.width;
309 else if (f->per_char == NULL) 631 else if (f->per_char == NULL)
310 width = f->max_bounds.width; 632 width = f->max_bounds.width;
311 else 633 else
312 { 634 {
313 prop = true; 635 slow = true;
314 636
315 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 637 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
316 638
317 if (encm) 639 if (encm)
318 N += (f->max_byte1 - f->min_byte1) 640 N += (f->max_byte1 - f->min_byte1)
348 f = 0; 670 f = 0;
349 } 671 }
350} 672}
351 673
352bool 674bool
353rxvt_font_x11::has_codepoint (uint32_t unicode) 675rxvt_font_x11::has_codepoint (unicode_t unicode)
354{ 676{
355 uint32_t ch = FROM_UNICODE (cs, unicode); 677 uint32_t ch = FROM_UNICODE (cs, unicode);
356 678
357 if (ch == NOCHAR) 679 if (ch == NOCHAR)
358 return false; 680 return false;
360 /* check wether the character exists in _this_ font. horrible. */ 682 /* check wether the character exists in _this_ font. horrible. */
361 XCharStruct *xcs; 683 XCharStruct *xcs;
362 684
363 if (encm) 685 if (encm)
364 { 686 {
365 int byte1 = ch >> 8; 687 unsigned char byte1 = ch >> 8;
366 int byte2 = ch & 255; 688 unsigned char byte2 = ch & 255;
367 689
368 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 690 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
369 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 691 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
370 return false; 692 return false;
371 693
394 716
395 return true; 717 return true;
396} 718}
397 719
398void 720void
399rxvt_font_x11::draw (int x, int y, 721rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
400 const text_t *text, int len, 722 const text_t *text, int len,
401 int fg, int bg) 723 int fg, int bg)
402{ 724{
403 // this looks like a mess /. 725 // this looks like a mess /.
404 // and it is a mess /. 726 // and it is a mess /.
405 // yet we are trying to be perfect /. 727 // yet we are trying to be perfect /.
406 // but the result still isn't perfect /. 728 // but the result still isn't perfect /.
407 729
408 bool slow = prop 730 bool slow = this->slow
409 || width != r->TermWin.fwidth 731 || width != r->TermWin.fwidth
410 || height != r->TermWin.fheight; 732 || height != r->TermWin.fheight;
411 733
412 int base = r->TermWin.fbase; 734 int base = r->TermWin.fbase;
413 735
414 XGCValues v; 736 XGCValues v;
415 v.foreground = r->PixColors[fg]; 737 v.foreground = r->PixColors[fg];
416 v.background = r->PixColors[bg];
417 v.font = f->fid; 738 v.font = f->fid;
418 739
419 if (enc2b) 740 if (enc2b)
420 { 741 {
421 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 742 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
422 743
423 if (bg == Color_bg && !slow) 744 if (bg == Color_bg && !slow)
424 { 745 {
746 v.background = r->PixColors[bg];
425 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 747 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
426 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 748 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
427 } 749 }
428 else 750 else
429 { 751 {
430 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 752 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
431 753
432 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 754 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
433 755
434 if (slow) 756 if (slow)
435 { 757 {
436 do 758 do
437 { 759 {
438 if (xc->byte1 || xc->byte2) 760 if (xc->byte1 || xc->byte2)
439 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 761 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
440 762
441 x += r->TermWin.fwidth; 763 x += r->TermWin.fwidth;
442 xc++; len--; 764 xc++; len--;
443 } 765 }
444 while (len); 766 while (len);
445 } 767 }
446 else 768 else
447 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 769 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
448 } 770 }
449 } 771 }
450 else 772 else
451 { 773 {
452 const char *xc = enc_char (text, len, cs, slow); 774 const char *xc = enc_char (text, len, cs, slow);
453 775
454 if (bg == Color_bg && !slow) 776 if (bg == Color_bg && !slow)
455 { 777 {
778 v.background = r->PixColors[bg];
456 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 779 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
457 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 780 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
458 } 781 }
459 else 782 else
460 { 783 {
461 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 784 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
462 785
463 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 786 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
464 787
465 if (slow) 788 if (slow)
466 { 789 {
467 do 790 do
468 { 791 {
469 if (*xc) 792 if (*xc)
470 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 793 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
471 794
472 x += r->TermWin.fwidth; 795 x += r->TermWin.fwidth;
473 xc++; len--; 796 xc++; len--;
474 } 797 }
475 while (len); 798 while (len);
476 } 799 }
477 else 800 else
478 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 801 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
479 } 802 }
480 } 803 }
481} 804}
482 805
483///////////////////////////////////////////////////////////////////////////// 806/////////////////////////////////////////////////////////////////////////////
488//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 811//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
489#define SWATHBITS 8 812#define SWATHBITS 8
490#endif 813#endif
491 814
492struct rxvt_font_xft : rxvt_font { 815struct rxvt_font_xft : rxvt_font {
493#if 0
494 enum {
495 SWATHCOUNT = 1 << (21 - UNIBITS),
496 SWATHSIZE = 1 << (SWATHBITS - 5)
497 };
498 typedef uint32_t swath[SWATHSIZE];
499
500 swath *cvr[SWATHCOUNT];
501#endif
502
503#if 0
504 void gen_coverage_swath (unsigned int page);
505
506 bool has_char (uint32_t ch)
507 {
508 unsigned int page = ch >> SWATHBITS;
509 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
510
511 if (page >= SWATHCOUNT)
512 return false;
513
514 if (!cvr[page]) gen_coverage_swath (page);
515
516 return cvr[page][idx >> 5] & (1 << (idx & 31));
517 }
518#endif
519 rxvt_font_xft () { f = 0; d = 0; } 816 rxvt_font_xft () { f = 0; }
520 817
521 void clear (); 818 void clear ();
522 819
523 bool load (int maxheight); 820 rxvt_fontprop properties ();
524 821
525 void draw (int x, int y, 822 bool load (const rxvt_fontprop &prop);
823
824 void draw (rxvt_drawable &d, int x, int y,
526 const text_t *text, int len, 825 const text_t *text, int len,
527 int fg, int bg); 826 int fg, int bg);
528 827
529 bool has_codepoint (uint32_t unicode); 828 bool has_codepoint (unicode_t unicode);
530 829
531protected: 830protected:
532 XftFont *f; 831 XftFont *f;
533 XftDraw *d;
534
535#if 0
536 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
537 void set_swath (uint32_t ch)
538 {
539 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
540 }
541#endif
542}; 832};
543 833
544void 834void
545rxvt_font_xft::clear () 835rxvt_font_xft::clear ()
546{ 836{
547 if (f) 837 if (f)
548 { 838 {
549 XftFontClose (DISPLAY, f); 839 XftFontClose (DISPLAY, f);
550 f = 0; 840 f = 0;
551 } 841 }
842}
552 843
553 if (d) 844rxvt_fontprop
554 { 845rxvt_font_xft::properties ()
555 XftDrawDestroy (d); 846{
556 d = 0; 847 rxvt_fontprop p;
557 }
558 848
559#if 0 849 FT_Face face = XftLockFace (f);
560 for (int i = 0; i < SWATHCOUNT; i++) 850
561 delete cvr[i]; 851 p.width = width; p.height = height;
562#endif 852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
853 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
854
855 XftUnlockFace (f);
856
857 return p;
563} 858}
564 859
565bool 860bool
566rxvt_font_xft::load (int maxheight) 861rxvt_font_xft::load (const rxvt_fontprop &prop)
567{ 862{
568#if 0 863#if 0
569 for (int i = 0; i < SWATHCOUNT; i++) 864 for (int i = 0; i < SWATHCOUNT; i++)
570 cvr[i] = 0; 865 cvr[i] = 0;
571#endif 866#endif
572 867
573 clear (); 868 clear ();
574 869
575 f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name); 870 FcPattern *p = FcNameParse ((FcChar8 *) name);
871
872 if (!p)
873 return false;
874
875 FcValue v;
876
877 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
878 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
879
880 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
881 FcPatternAddInteger (p, FC_SLANT, prop.slant);
882
883#if 0 // clipping unfortunately destroys our precious double-width-characters
884 // clip width, we can't do better, or can we?
885 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
886 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
887#endif
888
889 //FcPatternAddBool (p, FC_MINSPACE, 1);
890
891 XftResult result;
892 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
893
894 FcPatternDestroy (p);
895
896 if (!match)
897 return false;
898
899 f = XftFontOpenPattern (DISPLAY, match);
576 900
577 if (!f) 901 if (!f)
902 {
903 FcPatternDestroy (match);
578 return false; 904 return false;
905 }
579 906
580 FT_Face face = XftLockFace (f); 907 FT_Face face = XftLockFace (f);
581 908
582 prop = !FT_IS_FIXED_WIDTH (face); 909 slow = !FT_IS_FIXED_WIDTH (face);
583 910
584 int ftheight = 0; 911 int ftheight = 0;
585 912
586 for (;;) 913 for (;;)
587 { 914 {
589 FcChar8 c; 916 FcChar8 c;
590 917
591 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 918 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
592 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 919 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
593 920
594 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font 921 if (g1.xOff != g2.xOff) // don't simply trust the font
922 slow = true;
595 923
596 width = g2.xOff; 924 width = g2.xOff;
597 ascent = (face->size->metrics.ascender + 63) >> 6; 925 ascent = (face->size->metrics.ascender + 63) >> 6;
598 descent = (-face->size->metrics.descender + 63) >> 6; 926 descent = (-face->size->metrics.descender + 63) >> 6;
599 height = ascent + descent; 927 height = ascent + descent;
600 928
601 if (height <= maxheight || !maxheight) 929 if (height <= prop.height || !prop.height)
602 break; 930 break;
603 931
604 if (ftheight) 932 if (ftheight)
605 { 933 {
606 // take smaller steps near the end 934 // take smaller steps near the end
607 if (height > maxheight + 1) ftheight++; 935 if (height > prop.height + 1) ftheight++;
608 if (height > maxheight + 2) ftheight++; 936 if (height > prop.height + 2) ftheight++;
609 if (height > maxheight + 3) ftheight++; 937 if (height > prop.height + 3) ftheight++;
610 938
611 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 939 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
612 } 940 }
613 else 941 else
614 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 942 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
615 } 943 }
616 944
617 XftUnlockFace (f); 945 XftUnlockFace (f);
618 946
619 return true; 947 return true;
620} 948}
621 949
622#if 0
623void rxvt_font::gen_coverage_swath (unsigned int page)
624{
625 cvr[page] = new swath;
626
627 for (int i = 0; i < SWATHSIZE; i++)
628 cvr[page][i] = 0;
629
630 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
631}
632#endif
633
634bool 950bool
635rxvt_font_xft::has_codepoint (uint32_t unicode) 951rxvt_font_xft::has_codepoint (unicode_t unicode)
636{ 952{
637 return XftCharExists (DISPLAY, f, unicode); 953 return XftCharExists (DISPLAY, f, unicode);
638} 954}
639 955
640void 956void
641rxvt_font_xft::draw (int x, int y, 957rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
642 const text_t *text, int len, 958 const text_t *text, int len,
643 int fg, int bg) 959 int fg, int bg)
644{ 960{
645 if (!d)
646 {
647 dR;
648 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
649 }
650
651 if (bg >= 0 && bg != Color_bg)
652 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
653 else
654 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 961 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
655 962
656 if (!prop && width == r->TermWin.fwidth) 963 if (!slow && width == r->TermWin.fwidth && 0)
657 { 964 {
658 if (sizeof (text_t) == sizeof (FcChar16)) 965 if (sizeof (text_t) == sizeof (FcChar16))
659 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 966 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
660 else 967 else
661 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 968 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
664 { 971 {
665 while (len) 972 while (len)
666 { 973 {
667 if (*text != NOCHAR && *text != ' ') 974 if (*text != NOCHAR && *text != ' ')
668 { 975 {
976 int fwidth = r->TermWin.fwidth;
977 if (len >= 2 && text[1] == NOCHAR)
978 fwidth *= 2;
979
980 XGlyphInfo extents;
669 if (sizeof (text_t) == sizeof (FcChar16)) 981 if (sizeof (text_t) == sizeof (FcChar16))
670 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 982 {
983 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
984 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
985 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
986 }
671 else 987 else
672 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 988 {
989 XGlyphInfo extents;
990 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
991 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
992 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
993 }
673 } 994 }
674 995
675 x += r->TermWin.fwidth; 996 x += r->TermWin.fwidth;
676 text++; 997 text++;
677 len--; 998 len--;
681#endif 1002#endif
682 1003
683///////////////////////////////////////////////////////////////////////////// 1004/////////////////////////////////////////////////////////////////////////////
684 1005
685rxvt_fontset::rxvt_fontset (rxvt_t r) 1006rxvt_fontset::rxvt_fontset (rxvt_t r)
686#ifdef EXPLICIT_CONTEXT 1007: r (r), fontdesc (0)
687: r(r)
688#endif
689{ 1008{
690 clear (); 1009 clear ();
691} 1010}
692 1011
693rxvt_fontset::~rxvt_fontset () 1012rxvt_fontset::~rxvt_fontset ()
696} 1015}
697 1016
698void 1017void
699rxvt_fontset::clear () 1018rxvt_fontset::clear ()
700{ 1019{
701 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 1020 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
702 FONT_UNREF (*i); 1021 FONT_UNREF (*i);
1022
1023 free (fontdesc); fontdesc = 0;
703 1024
704 fonts.clear (); 1025 fonts.clear ();
705 base_id = 0; 1026 base_id = 0;
706 height = 0x7fffffff; 1027 base_prop.height = 0x7fffffff;
1028 base_prop.weight = rxvt_fontprop::medium;
1029 base_prop.slant = rxvt_fontprop::roman;
707 1030
708 fallback = fallback_fonts; 1031 fallback = fallback_fonts;
709} 1032}
710 1033
711rxvt_font * 1034rxvt_font *
731 f = new rxvt_font_x11; 1054 f = new rxvt_font_x11;
732 } 1055 }
733 else 1056 else
734 f = new rxvt_font_x11; 1057 f = new rxvt_font_x11;
735 1058
1059 f->fs = this;
736 f->set_term (r); 1060 f->set_term (r);
737 f->set_name (strdup (name)); 1061 f->set_name (strdup (name));
738 1062
739 f->cs = cs; 1063 f->cs = cs;
740 f->loaded = false; 1064 f->loaded = false;
758 1082
759 if (*desc == '[') 1083 if (*desc == '[')
760 { 1084 {
761 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1085 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
762 1086
763 const char *extra = desc++; 1087 //const char *extra = desc++; // not yet used
764 1088
765 desc = strchr (desc, ']'); 1089 desc = strchr (desc, ']');
766 1090
767 if (!desc) 1091 if (!desc)
768 { 1092 {
796rxvt_fontset::realize_font (int i) 1120rxvt_fontset::realize_font (int i)
797{ 1121{
798 if (fonts[i]->loaded) 1122 if (fonts[i]->loaded)
799 return true; 1123 return true;
800 1124
801 if (fonts[i]->load (height))
802 return fonts[i]->loaded = true; 1125 fonts[i]->loaded = true;
803 1126
804 delete fonts[i]; 1127 if (!fonts[i]->load (base_prop))
805 fonts.erase (fonts.begin () + i); 1128 {
806 1129 fonts[i]->cs = CS_UNKNOWN;
807 return false; 1130 return false;
808} 1131 }
809 1132
810void 1133 return true;
1134}
1135
1136bool
811rxvt_fontset::populate (const char *desc) 1137rxvt_fontset::populate (const char *desc)
812{ 1138{
813 clear (); 1139 clear ();
1140
1141 fontdesc = strdup (desc);
814 1142
815 fonts.push_back (new_font (0, CS_UNICODE)); 1143 fonts.push_back (new_font (0, CS_UNICODE));
816 realize_font (0); 1144 realize_font (0);
817 1145
818 add_fonts (desc); 1146 add_fonts (desc);
819 1147
820 if (!base_id) 1148 if (!base_id)
821 base_id = 1; 1149 base_id = 1;
822 1150
823 // we currently need a base-font, no matter what 1151 // we currently need a base-font, no matter what
824 if (fonts.size () <= base_id) 1152 if ((int)fonts.size () <= base_id || !realize_font (base_id))
825 { 1153 {
1154 puts ("unable to load specified font (s), falling back to 'fixed'\n");
826 add_fonts ("fixed"); 1155 add_fonts ("fixed");
827 base_id = 1; 1156 base_id = fonts.size () - 1;
828 } 1157 }
829 1158
830 if (fonts.size () <= base_id || !realize_font (base_id)) 1159 if ((int)fonts.size () <= base_id || !realize_font (base_id))
831 { 1160 return false;
832 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
833 exit (1);
834 }
835 1161
836 height = fonts[base_id]->height; 1162 base_prop = fonts[base_id]->properties ();
837 1163
838 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/ 1164 return true;
839} 1165}
840 1166
841int 1167int
842rxvt_fontset::find_font (uint32_t unicode) 1168rxvt_fontset::find_font (unicode_t unicode, bool bold)
843{ 1169{
844 for (int i = 0; i < fonts.size (); i++) 1170 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii
1171 i < fonts.size ();
1172 i++)
845 { 1173 {
846 rxvt_font *f = fonts[i]; 1174 rxvt_font *f = fonts[i];
847 1175
848 if (!f->loaded) 1176 if (!f->loaded)
849 { 1177 {
850 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1178 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
851 goto next_font; 1179 goto next_font;
852 1180
853 if (!realize_font (i)) 1181 if (!realize_font (i))
854 {
855 --i;
856 goto next_font; 1182 goto next_font;
857 } 1183 }
858 1184
859 //printf ("added font %s for %04lx\n", f->name, unicode); 1185 if (f->cs == CS_UNKNOWN)
860 } 1186 goto next_font;
1187
1188 if (bold && f->properties ().weight < rxvt_fontprop::bold)
1189 goto next_font;
861 1190
862 if (f->has_codepoint (unicode)) 1191 if (f->has_codepoint (unicode))
863 return i; 1192 return i;
864 1193
865 next_font: 1194 next_font:
866 if (i == fonts.size () - 1 && fallback->name) 1195 if (i == fonts.size () - 1 && fallback->name)
867 { 1196 {
868 fonts.push_back (new_font (fallback->name, fallback->cs)); 1197 fonts.push_back (new_font (fallback->name, fallback->cs));
869 fallback++; 1198 fallback++;
1199 i = 0;
870 } 1200 }
871 } 1201 }
1202
1203 // if no bold font found, use a regular one
1204 if (bold)
1205 return find_font (unicode);
872 1206
873 return 0; /* we must return SOME font */ 1207 return 0; /* we must return SOME font */
874} 1208}
875 1209
876 1210

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines