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

Comparing rxvt-unicode/src/rxvtfont.C (file contents):
Revision 1.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.81 by root, Mon Jan 9 07:19:18 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: defaultfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2006 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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtlib.h"
25#include "rxvtutil.h"
24#include "rxvtfont.h" 26#include "rxvtfont.h"
25 27
26#include <cstdlib> 28#include <cstdlib>
29#include <wchar.h>
30#include <inttypes.h>
27 31
28#define DISPLAY r->display->display 32#define DISPLAY r->display->display
29#define TGC r->TermWin.gc 33#define TGC r->gc
34
35#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
30 36
31const struct rxvt_fallback_font { 37const struct rxvt_fallback_font {
32 codeset cs; 38 codeset cs;
33 const char *name; 39 const char *name;
34} fallback_fonts[] = { 40} fallback_fonts[] = {
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, 58 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, 59 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, 60 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, 61 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, 62 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
63
64# if XFT
65 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
66
67 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
68 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
69 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
70 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
71 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
72 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
73 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
74# endif
57#endif 75#endif
58 76
59 // japanese 77 // japanese
60#if ENCODING_JP || ENCODING_JP_EXT 78#if ENCODING_JP || ENCODING_JP_EXT
61# if XFT 79# if XFT
62 // prefer xft for complex scripts 80 // prefer xft for complex scripts
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, 81 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
82 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
83 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
84 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
64# endif 85# endif
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 86 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 87 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 88 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 89 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
69 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 90 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
70 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 91 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
71#endif 92#endif
72 93
73#if ENCODING_CN || ENCODING_CN_EXT 94#if ENCODING_ZH || ENCODING_ZH_EXT
74# if XFT 95# if XFT
96 { CS_GBK_0, "xft:AR PL KaitiM GB" },
97 { CS_GBK_0, "xft:AR PL SungtiL GB" },
98 { CS_GBK_0, "xft::spacing=100:lang=zh" },
75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 99 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 100 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 101 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 102 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
103 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
79# endif 104# endif
105 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
80 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 106 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
81 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 107 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
82 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 108 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
109 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
83 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 111 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 112 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 113 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 114 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, 116 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, 117 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, 118 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
92#endif 119#endif
93 120
121#if ENCODING_KR
122 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
123 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
94#if XFT 124# if XFT
95 { CS_UNICODE, "xft:Andale Mono" }, 125 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
96 { CS_UNICODE, "xft:Arial Unicode MS" }, 126 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
127# endif
97#endif 128#endif
129
130 // generic font fallback
98 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
99 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
100 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 134 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
102#if XFT 135#if XFT
136 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
138 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
139 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
140
103 // FreeMono is usually uglier than x fonts, so try last only. 141 // FreeMono is usually uglier than x fonts, so try last only.
104 //{ CS_UNICODE, "xft:FreeMono" }, 142 { CS_UNICODE, "xft:FreeMono:autohint=true" },
105#endif 143#endif
106 144
107 { CS_UNKNOWN, 0 } 145 { CS_UNKNOWN, 0 }
108}; 146};
147
148// these characters are used to guess the font height and width
149// pango uses a similar algorithm and doesn't trust the font either.
150static uint16_t extent_test_chars[] = {
151 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
152 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
153 0x304c, 0x672c, // が本
154};
155
156#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
109 157
110///////////////////////////////////////////////////////////////////////////// 158/////////////////////////////////////////////////////////////////////////////
111 159
112#if XFT 160#if XFT
113rxvt_drawable::~rxvt_drawable () 161rxvt_drawable::~rxvt_drawable ()
188} 236}
189 237
190///////////////////////////////////////////////////////////////////////////// 238/////////////////////////////////////////////////////////////////////////////
191 239
192void 240void
241rxvt_font::set_name (char *name)
242{
243 if (this->name == name)
244 return;
245
246 if (this->name) free (this->name); // let the compiler optimize
247 this->name = name;
248}
249
250void
193rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 251rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
194{ 252{
195 if (color == Color_bg) 253 if (color == Color_bg)
196 XClearArea (d.display->display, d, x, y, w, h, FALSE); 254 XClearArea (d.display->display, d, x, y, w, h, FALSE);
197 else if (color >= 0) 255 else if (color >= 0)
198 { 256 {
199#if XFT 257#if XFT
200 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h); 258 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h);
201#else 259#else
202 XSetForeground (d.display->display, TGC, r->PixColors[color]); 260 XSetForeground (d.display->display, TGC, r->pix_colors[color]);
203 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 261 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
204#endif 262#endif
205 } 263 }
206} 264}
207 265
208#include "table/linedraw.h" 266#include "table/linedraw.h"
209 267
210struct rxvt_font_default : rxvt_font { 268struct rxvt_font_default : rxvt_font {
269 struct rxvt_fontset *fs;
270
271 rxvt_font_default (rxvt_fontset *fs)
272 : rxvt_font ()
273 {
274 this->fs = fs;
275 }
211 276
212 rxvt_fontprop properties () 277 rxvt_fontprop properties ()
213 { 278 {
214 rxvt_fontprop p; 279 rxvt_fontprop p;
215 280
216 p.width = p.height = 1; 281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
217 p.weight = rxvt_fontprop::medium; 283 p.weight = rxvt_fontprop::medium;
218 p.slant = rxvt_fontprop::roman; 284 p.slant = rxvt_fontprop::roman;
219 285
220 return p; 286 return p;
221 } 287 }
223 bool load (const rxvt_fontprop &prop) 289 bool load (const rxvt_fontprop &prop)
224 { 290 {
225 width = 1; height = 1; 291 width = 1; height = 1;
226 ascent = 1; descent = 0; 292 ascent = 1; descent = 0;
227 293
228 set_name (strdup ("built-in pseudofont")); 294 set_name (strdup ("built-in support font"));
229 295
230 return true; 296 return true;
231 } 297 }
232 298
233 bool has_codepoint (unicode_t unicode) 299 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
234 { 300 {
301 careful = false;
302
235 if (unicode <= 0x001f) 303 if (unicode <= 0x001f)
236 return true; 304 return true;
237 305
238 if (unicode <= 0x007f) 306 if (unicode <= 0x007f)
239 return false; 307 return false;
240 308
241 if (unicode <= 0x009f) 309 if (unicode <= 0x009f)
242 return true; 310 return true;
243 311
244 if (unicode >= 0x2500 && unicode <= 0x259f) 312 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 !r->option (Opt_skipBuiltinGlyphs))
245 return true; 314 return true;
246 315
247 if (IS_COMPOSE (unicode)) 316 if (IS_COMPOSE (unicode))
248 return true; 317 return true;
249 318
250 switch (unicode) 319 switch (unicode)
251 { 320 {
252 case ZERO_WIDTH_CHAR: 321 case ZERO_WIDTH_CHAR:
322 case NOCHAR:
253 return true; 323 return true;
254 } 324 }
255 325
256 return false; 326 return false;
257 } 327 }
264void 334void
265rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 335rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
266 const text_t *text, int len, 336 const text_t *text, int len,
267 int fg, int bg) 337 int fg, int bg)
268{ 338{
339 Display *disp = d.display->display;
340
269 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 341 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
270 342
271 XSetForeground (d.display->display, TGC, r->PixColors[fg]); 343 XSetForeground (disp, TGC, r->pix_colors[fg]);
272 344
273 while (len--) 345 while (len)
274 { 346 {
275#if ENABLE_COMBINING 347#if ENABLE_COMBINING
276 compose_char *cc; 348 compose_char *cc;
277#endif 349#endif
350 const text_t *tp = text;
278 text_t t = *text++; 351 text_t t = *tp;
352
353 while (++text, --len && *text == NOCHAR)
354 ;
355
356 int width = text - tp;
357 int fwidth = r->fwidth * width;
279 358
280 if (0x2500 <= t && t <= 0x259f) 359 if (0x2500 <= t && t <= 0x259f)
281 { 360 {
282 uint16_t offs = linedraw_offs[t - 0x2500]; 361 uint16_t offs = linedraw_offs[t - 0x2500];
283 uint32_t *a = linedraw_command + (offs >> 4); 362 uint32_t *a = linedraw_command + (offs >> 4);
284 uint32_t *b = a + (offs & 15); 363 uint32_t *b = a + (offs & 15);
285 364
286 int W = r->TermWin.fwidth; 365 int W = fwidth;
287 int H = r->TermWin.fheight; 366 int H = r->fheight;
288 367
289 int x_[16]; 368 int x_[16];
290 int y_[16]; 369 int y_[16];
291 370
292 for (int i = 0; i <= 8; i++) 371 for (int i = 0; i <= 8; i++)
300 379
301 XGCValues gcv; 380 XGCValues gcv;
302 381
303 gcv.cap_style = CapButt; 382 gcv.cap_style = CapButt;
304 gcv.line_width = 0; 383 gcv.line_width = 0;
305 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 384 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
306 385
307 while (a < b) 386 while (a < b)
308 { 387 {
309 uint32_t command = *a++; 388 uint32_t command = *a++;
310 389
317 int y2 = y_[(command >> 0) & 15]; 396 int y2 = y_[(command >> 0) & 15];
318 397
319 switch (op) 398 switch (op)
320 { 399 {
321 case 0: // line 400 case 0: // line
322 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 401 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
323 break; 402 break;
324 403
325 case 1: // rectangle, possibly stippled 404 case 1: // rectangle, possibly stippled
326 if (a) 405 if (a)
327 { 406 {
328 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 407 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
329 408
330 gcv.fill_style = FillStippled; 409 gcv.fill_style = FillStippled;
331 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 410 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
332 gcv.ts_x_origin = x; 411 gcv.ts_x_origin = x;
333 gcv.ts_y_origin = y; 412 gcv.ts_y_origin = y;
334 413
335 XChangeGC (d.display->display, TGC, 414 XChangeGC (disp, TGC,
336 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 415 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
337 &gcv); 416 &gcv);
338 } 417 }
339 418
340 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 419 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
341 420
342 if (a) 421 if (a)
343 { 422 {
344 XFreePixmap (d.display->display, gcv.stipple); 423 XFreePixmap (disp, gcv.stipple);
345 gcv.stipple = 0; 424 gcv.stipple = 0;
346 gcv.fill_style = FillSolid; 425 gcv.fill_style = FillSolid;
347 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 426 XChangeGC (disp, TGC, GCFillStyle, &gcv);
348 } 427 }
349 break; 428 break;
350 case 2: // arc 429 case 2: // arc
351 XDrawArc (d.display->display, d, TGC, 430 XDrawArc (disp, d, TGC,
352 x1 - W/2, y1 - H/2, W-1, H-1, 431 x1 - W/2, y1 - H/2, W-1, H-1,
353 (a - 1) * 90*64, (b - 1) * 90*64); 432 (a - 1) * 90*64, (b - 1) * 90*64);
354 break; 433 break;
355 } 434 }
356 } 435 }
357 } 436 }
358#if ENABLE_COMBINING 437#if ENABLE_COMBINING
359 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 438 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
360 { 439 {
440 text_t chrs[2];
441 width = min (2, width);
442 chrs [1] = NOCHAR;
443
444 *chrs = cc->c1;
361 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 445 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
362 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 446 f1->draw (d, x, y, chrs, width, fg, bg);
447
363 if (cc->c2 != NOCHAR) 448 if (cc->c2 != NOCHAR)
364 { 449 {
450 bool careful;
451
365 // prefer font of first character, for no good reasons 452 // prefer font of first character, for no good reasons
366 rxvt_font *f2 = f1->has_codepoint (cc->c2) 453 *chrs = cc->c2;
454 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
367 ? f1 455 ? f1
368 : (*fs)[fs->find_font (cc->c2)]; 456 : (*fs)[fs->find_font (cc->c2)];
369 457
370 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 458 f2->draw (d, x, y, chrs, width, fg, -1);
371 } 459 }
372 } 460 }
373#endif 461#endif
374 else 462 else
375 switch (t) 463 switch (t)
376 { 464 {
465 case '\t':
377 case ZERO_WIDTH_CHAR: 466 case ZERO_WIDTH_CHAR:
467 case NOCHAR:
378 break; 468 break;
379 469
380 default: 470 default:
381 int w = 0;
382 while (len > 0 && *text == NOCHAR)
383 {
384 ++text;
385 --len;
386 w += r->TermWin.fwidth;
387 }
388
389 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 471 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
390 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 472 fwidth - 4, r->fheight - 4);
391 x += w;
392 } 473 }
393 474
394 x += r->TermWin.fwidth; 475 x += fwidth;
395 } 476 }
396} 477}
397 478
398///////////////////////////////////////////////////////////////////////////// 479/////////////////////////////////////////////////////////////////////////////
399 480
404 485
405 rxvt_fontprop properties (); 486 rxvt_fontprop properties ();
406 487
407 bool load (const rxvt_fontprop &prop); 488 bool load (const rxvt_fontprop &prop);
408 489
409 bool has_codepoint (unicode_t unicode); 490 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
410 491
411 void draw (rxvt_drawable &d, int x, int y, 492 void draw (rxvt_drawable &d, int x, int y,
412 const text_t *text, int len, 493 const text_t *text, int len,
413 int fg, int bg); 494 int fg, int bg);
414 495
496 bool slow; // wether this is a proportional font or has other funny characteristics
415 XFontStruct *f; 497 XFontStruct *f;
416 codeset cs; 498 codeset cs;
417 bool enc2b, encm; 499 bool enc2b, encm;
418 500
419 char *get_property (XFontStruct *f, const char *property, const char *repl) const; 501 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
442} 524}
443 525
444bool 526bool
445rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 527rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
446{ 528{
447 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 529 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
448 p.height = height; 530 p.height = height;
531 p.ascent = rxvt_fontprop::unset;
449 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 532 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; 533 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
451 534
452 return true; 535 return true;
453} 536}
454 537
455bool 538bool
456rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 539rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
457{ 540{
458 unsigned long height; 541 unsigned long height;
542
543#if 0
459 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
460 return false; 545 return false;
546#else
547 height = f->ascent + f->descent;
548#endif
461 549
462 unsigned long avgwidth; 550 unsigned long avgwidth;
463 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 551 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
464 avgwidth = 0; 552 avgwidth = 0;
465 553
468 556
469 set_properties (p, height, weight, slant, avgwidth); 557 set_properties (p, height, weight, slant, avgwidth);
470 558
471 free (weight); 559 free (weight);
472 free (slant); 560 free (slant);
561
562 p.ascent = f->ascent;
473 563
474 return true; 564 return true;
475} 565}
476 566
477bool 567bool
508 else 598 else
509 return false; 599 return false;
510} 600}
511 601
512// fix the size of scalable fonts 602// fix the size of scalable fonts
513static void 603static bool
514fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop) 604replace_field (char *buf, const char *name, int index, const char old, const char *replace)
515{ 605{
516 int slashes = 0; 606 int slashes = 0;
517 const char *size; 607 const char *field, *end;
518 608
519 for (const char *c = name; *c; c++) 609 for (const char *c = name; *c; c++)
520 if (*c == '-') 610 if (*c == '-')
521 { 611 {
522 if (slashes == 6) 612 if (slashes == index)
523 size = c + 1; 613 field = c + 1;
614
615 if (slashes == index + 1)
616 end = c;
524 617
525 if (++slashes >= 13) 618 if (++slashes >= 13)
526 break; 619 break;
527 } 620 }
528 621
529 if (slashes >= 13 && size[0] == '0') 622 if (slashes >= 13 && (!old || *field == old))
530 { 623 {
624 // TODO: check for overflow in font-name
531 strncpy (buf, name, size - name); 625 strncpy (buf, name, field - name);
532 buf += size - name; 626 buf += field - name;
533 buf += sprintf (buf, "%d", prop.height);
534 strcpy (buf, size + 1); 627 strcpy (buf, replace);
628 strcat (buf, end);
629
630 return true;
535 } 631 }
536 else 632 else
633 {
537 strcpy (buf, name); 634 strcpy (buf, name);
635
636 return false;
637 }
538} 638}
539 639
540bool 640bool
541rxvt_font_x11::load (const rxvt_fontprop &prop) 641rxvt_font_x11::load (const rxvt_fontprop &prop)
542{ 642{
643 Display *disp = DISPLAY;
644
543 clear (); 645 clear ();
646
647 char field_str[64]; // enough for 128 bits
648
649 // first morph the font if required
650 if (prop.weight != rxvt_fontprop::unset
651 || prop.slant != rxvt_fontprop::unset)
652 {
653 char fname[1024];
654
655 if (name[0] != '-')
656 {
657 f = XLoadQueryFont (disp, name);
658
659 if (!f)
660 return false;
661
662 char *new_name = get_property (f, "FONT", name);
663
664 if (new_name)
665 set_name (new_name);
666 else
667 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
668
669 XFreeFont (disp, f);
670 f = 0;
671 }
672
673 if (prop.weight != rxvt_fontprop::unset)
674 {
675 replace_field (fname, name, 2, 0,
676 prop.weight < rxvt_fontprop::bold
677 ? "medium" : "bold");
678 set_name (strdup (fname));
679 }
680
681 if (prop.slant != rxvt_fontprop::unset)
682 {
683 replace_field (fname, name, 3, 0,
684 prop.slant < rxvt_fontprop::italic
685 ? "r" : "i"); // TODO: handle "o"blique, too
686 set_name (strdup (fname));
687 }
688 }
689
690 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
691 ? 0 : prop.height);
692
693 struct font_weight {
694 char *name;
695 int diff;
696
697 void clear ()
698 {
699 name = 0;
700 diff = 0x7fffffff;
701 }
702
703 font_weight () { clear (); }
704 ~font_weight () { free (name); }
705 };
544 706
545 char **list; 707 char **list;
546 int count; 708 int count;
547 list = XListFonts (DISPLAY, name, 1024, &count); 709 list = XListFonts (disp, name, 4000, &count);
710
548 set_name (0); 711 set_name (0);
549 712
550 if (!list) 713 if (!list)
551 return false; 714 return false;
552 715
553 int bestdiff = 0x7fffffff; 716 font_weight *fonts = new font_weight[count];
717
554 for (int i = 0; i < count; i++) 718 for (int i = 0; i < count; i++)
555 { 719 {
556 rxvt_fontprop p; 720 rxvt_fontprop p;
557 char fname[1024]; 721 char fname[1024];
558 fix_scalable (fname, list[i], prop); 722
723 int diff = 0;
724
725 if (replace_field (fname, list[i], 6, '0', field_str))
726 diff += 10; // slightly penalize scalable fonts
727 else if (replace_field (fname, list[i], 11, '0', "0"))
728 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
559 729
560 if (!set_properties (p, fname)) 730 if (!set_properties (p, fname))
561 continue; 731 continue;
562 732
733 if (prop.height != rxvt_fontprop::unset
563 if (p.height > prop.height) // weed out too large fonts 734 && p.height > prop.height) // weed out too large fonts
564 continue; 735 continue;
565 736
566 int diff = (prop.height - p.height) * 32 737 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
567 + abs (prop.weight - p.weight) 738 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
568 + abs (prop.slant - p.slant ); 739 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
740 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
569 741
570 if (!name // compare against best found so far 742 fonts[i].name = strdup (fname);
571 || diff < bestdiff) 743 fonts[i].diff = diff;
572 {
573 set_name (strdup (fname));
574 bestdiff = diff;
575 }
576 } 744 }
577 745
578 XFreeFontNames (list); 746 XFreeFontNames (list);
579 747
580 if (!name) 748 // this loop only iterates when the guessed font-size is too small
581 return false; 749 for (;;)
750 {
751 font_weight *best = fonts + count - 1;
582 752
583 f = XLoadQueryFont (DISPLAY, name); 753 for (font_weight *w = best; w-- > fonts; )
754 if (w->diff <= best->diff)
755 best = w;
756
757 if (!best->name
758 || !(f = XLoadQueryFont (disp, best->name)))
759 break;
760
761 set_name (best->name);
762 best->clear ();
763
764 ascent = f->ascent;
765 descent = f->descent;
766 height = ascent + descent;
767
768 if (prop.height == rxvt_fontprop::unset
769 || height <= prop.height)
770 break; // font is ready for use
771
772 // PIXEL_SIZE small enough, but real height too large
773 clear ();
774 }
775
776 delete [] fonts;
584 777
585 if (!f) 778 if (!f)
586 return false; 779 return false;
587 780
588 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 781 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
617 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 810 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
618 811
619 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 812 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
620 enc2b = encm || f->max_char_or_byte2 > 255; 813 enc2b = encm || f->max_char_or_byte2 > 255;
621 814
622 ascent = f->ascent;
623 descent = f->descent;
624 height = ascent + descent;
625
626 slow = false; 815 slow = false;
627 816
817#if 1 // only used for slow detection, TODO optimize
628 if (f->min_bounds.width == f->max_bounds.width) 818 if (f->min_bounds.width == f->max_bounds.width)
629 width = f->min_bounds.width; 819 width = f->min_bounds.width;
630 else if (f->per_char == NULL) 820 else if (f->per_char == NULL)
631 width = f->max_bounds.width; 821 width = f->max_bounds.width;
632 else 822 else
645 width = f->per_char[N].width; 835 width = f->per_char[N].width;
646 836
647 --N; 837 --N;
648 } 838 }
649 } 839 }
840#endif
841
842 width = 1;
843
844 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
845 {
846 if (cs != CS_UNICODE
847 && *t > 0x100
848 && FROM_UNICODE (cs, *t) == NOCHAR)
849 continue;
850
851 // ignore characters we wouldn't use anyways
852 bool careful;
853 if (!has_char (*t, &prop, careful))
854 continue;
855
856 XChar2b ch = { *t >> 8, *t };
857
858 XCharStruct g;
859 int dir_ret, asc_ret, des_ret;
860 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
861
862 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
863
864 if (width < g.width) width = g.width;
865 }
650 866
651 if (cs == CS_UNKNOWN) 867 if (cs == CS_UNKNOWN)
652 { 868 {
653 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name); 869 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
654 870
655 clear (); 871 clear ();
656
657 return false; 872 return false;
658 } 873 }
874
875#if 0 // do it per-character
876 if (prop && width > prop->width)
877 {
878 clear ();
879 return false;
880 }
881#endif
659 882
660 return true; 883 return true;
661} 884}
662 885
663void 886void
669 f = 0; 892 f = 0;
670 } 893 }
671} 894}
672 895
673bool 896bool
674rxvt_font_x11::has_codepoint (unicode_t unicode) 897rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
675{ 898{
676 uint32_t ch = FROM_UNICODE (cs, unicode); 899 uint32_t ch = FROM_UNICODE (cs, unicode);
677 900
678 if (ch == NOCHAR) 901 if (ch == NOCHAR)
679 return false; 902 return false;
688 911
689 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 912 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
690 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 913 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
691 return false; 914 return false;
692 915
693 if (!f->per_char) 916 if (f->per_char)
694 return true; 917 {
695
696 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1; 918 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; 919 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
698 920
699 xcs = f->per_char + N; 921 xcs = f->per_char + N;
922 }
923 else
924 xcs = &f->max_bounds;
700 } 925 }
701 else 926 else
702 { 927 {
703 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2) 928 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
704 return false; 929 return false;
705 930
706 if (!f->per_char) 931 if (f->per_char)
707 return true;
708
709 xcs = f->per_char + (ch - f->min_char_or_byte2); 932 xcs = f->per_char + (ch - f->min_char_or_byte2);
933 else
934 xcs = &f->max_bounds;
710 } 935 }
711 936
712 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0 937 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
713 && xcs->ascent == 0 && xcs->descent == 0) 938 && xcs->ascent == 0 && xcs->descent == 0)
939 return false;
940
941 if (!prop || prop->width == rxvt_fontprop::unset)
942 return true;
943
944 // check character against base font bounding box
945 int w = xcs->width;
946 int wcw = wcwidth (unicode);
947 if (wcw > 0) w = (w + wcw - 1) / wcw;
948
949 careful = w > prop->width;
950 if (careful && w > prop->width * MAX_OVERLAP >> 2)
714 return false; 951 return false;
715 952
716 return true; 953 return true;
717} 954}
718 955
725 // and it is a mess /. 962 // and it is a mess /.
726 // yet we are trying to be perfect /. 963 // yet we are trying to be perfect /.
727 // but the result still isn't perfect /. 964 // but the result still isn't perfect /.
728 965
729 bool slow = this->slow 966 bool slow = this->slow
730 || width != r->TermWin.fwidth 967 || width != r->fwidth
731 || height != r->TermWin.fheight; 968 || height != r->fheight;
732 969
733 int base = r->TermWin.fbase; 970 int base = ascent; // sorry, incorrect: r->fbase;
734 971
735 XGCValues v; 972 XGCValues v;
736 v.foreground = r->PixColors[fg]; 973 v.foreground = r->pix_colors[fg];
737 v.font = f->fid; 974 v.font = f->fid;
738 975
739 if (enc2b) 976 if (enc2b)
740 { 977 {
741 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 978 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
742 979
743 if (bg == Color_bg && !slow) 980 if (bg == Color_bg && !slow)
744 { 981 {
745 v.background = r->PixColors[bg]; 982 v.background = r->pix_colors[bg];
746 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 983 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
747 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 984 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
748 } 985 }
749 else 986 else
750 { 987 {
751 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 988 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
752 989
753 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 990 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
754 991
755 if (slow) 992 if (slow)
756 { 993 {
757 do 994 do
758 { 995 {
759 if (xc->byte1 || xc->byte2) 996 if (xc->byte1 || xc->byte2)
760 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 997 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
761 998
762 x += r->TermWin.fwidth; 999 x += r->fwidth;
763 xc++; len--; 1000 xc++; len--;
764 } 1001 }
765 while (len); 1002 while (len);
766 } 1003 }
767 else 1004 else
772 { 1009 {
773 const char *xc = enc_char (text, len, cs, slow); 1010 const char *xc = enc_char (text, len, cs, slow);
774 1011
775 if (bg == Color_bg && !slow) 1012 if (bg == Color_bg && !slow)
776 { 1013 {
777 v.background = r->PixColors[bg]; 1014 v.background = r->pix_colors[bg];
778 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1015 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
779 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1016 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
780 } 1017 }
781 else 1018 else
782 { 1019 {
783 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1020 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
784 1021
785 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1022 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
786 1023
787 if (slow) 1024 if (slow)
788 { 1025 {
789 do 1026 do
790 { 1027 {
791 if (*xc) 1028 if (*xc)
792 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1029 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
793 1030
794 x += r->TermWin.fwidth; 1031 x += r->fwidth;
795 xc++; len--; 1032 xc++; len--;
796 } 1033 }
797 while (len); 1034 while (len);
798 } 1035 }
799 else 1036 else
803} 1040}
804 1041
805///////////////////////////////////////////////////////////////////////////// 1042/////////////////////////////////////////////////////////////////////////////
806 1043
807#if XFT 1044#if XFT
808#if 0
809#define UNIBITS 21
810//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
811#define SWATHBITS 8
812#endif
813 1045
814struct rxvt_font_xft : rxvt_font { 1046struct rxvt_font_xft : rxvt_font {
815 rxvt_font_xft () { f = 0; } 1047 rxvt_font_xft () { f = 0; }
816 1048
817 void clear (); 1049 void clear ();
822 1054
823 void draw (rxvt_drawable &d, int x, int y, 1055 void draw (rxvt_drawable &d, int x, int y,
824 const text_t *text, int len, 1056 const text_t *text, int len,
825 int fg, int bg); 1057 int fg, int bg);
826 1058
827 bool has_codepoint (unicode_t unicode); 1059 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
828 1060
829protected: 1061protected:
830 XftFont *f; 1062 XftFont *f;
831}; 1063};
832 1064
847 1079
848 FT_Face face = XftLockFace (f); 1080 FT_Face face = XftLockFace (f);
849 1081
850 p.width = width; 1082 p.width = width;
851 p.height = height; 1083 p.height = height;
1084 p.ascent = ascent;
852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1085 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
853 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1086 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
854 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1087 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
855 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1088 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
856 1089
860} 1093}
861 1094
862bool 1095bool
863rxvt_font_xft::load (const rxvt_fontprop &prop) 1096rxvt_font_xft::load (const rxvt_fontprop &prop)
864{ 1097{
865#if 0 1098 Display *disp = DISPLAY;
866 for (int i = 0; i < SWATHCOUNT; i++)
867 cvr[i] = 0;
868#endif
869 1099
870 clear (); 1100 clear ();
871 1101
872 FcPattern *p = FcNameParse ((FcChar8 *) name); 1102 FcPattern *p = FcNameParse ((FcChar8 *) name);
873 1103
874 if (!p) 1104 if (!p)
875 return false; 1105 return false;
876 1106
877 FcValue v; 1107 FcValue v;
878 1108
1109 if (prop.height != rxvt_fontprop::unset
879 if (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1110 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1111 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
880 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1112 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
881 1113
1114 if (prop.weight != rxvt_fontprop::unset
882 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1115 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
883 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1116 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
884 1117
1118 if (prop.slant != rxvt_fontprop::unset
885 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 1119 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
886 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1120 FcPatternAddInteger (p, FC_SLANT, prop.slant);
887
888 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
889 FcPatternAddBool (p, FC_MINSPACE, 1);
890 1121
891#if 0 // clipping unfortunately destroys our precious double-width-characters 1122#if 0 // clipping unfortunately destroys our precious double-width-characters
892 // clip width, we can't do better, or can we? 1123 // clip width, we can't do better, or can we?
893 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 1124 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
894 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 1125 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
895#endif 1126#endif
896 1127
1128 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1129 FcPatternAddBool (p, FC_MINSPACE, 1);
1130
1131 // store generated name so iso14755 view gives better results
1132 set_name ((char *)FcNameUnparse (p));
1133
897 XftResult result; 1134 XftResult result;
898 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1135 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
899 1136
900 FcPatternDestroy (p); 1137 FcPatternDestroy (p);
901 1138
902 if (!match) 1139 if (!match)
903 return false; 1140 return false;
904 1141
905 f = XftFontOpenPattern (DISPLAY, match);
906
907 if (!f)
908 {
909 FcPatternDestroy (match);
910 return false;
911 }
912
913 FT_Face face = XftLockFace (f);
914
915 slow = !FT_IS_FIXED_WIDTH (face);
916
917 int ftheight = 0; 1142 int ftheight = 0;
1143 bool success = true;
918 1144
919 for (;;) 1145 for (;;)
920 { 1146 {
921 XGlyphInfo g1, g2; 1147 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
922 FcChar8 c;
923 1148
924 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 1149 if (!f)
925 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 1150 {
1151 success = false;
1152 break;
1153 }
926 1154
927 if (g1.xOff != g2.xOff) // don't simply trust the font 1155 FT_Face face = XftLockFace (f);
928 slow = true;
929 1156
930 width = g2.xOff;
931 ascent = (face->size->metrics.ascender + 63) >> 6; 1157 ascent = (face->size->metrics.ascender + 63) >> 6;
932 descent = (-face->size->metrics.descender + 63) >> 6; 1158 descent = (-face->size->metrics.descender + 63) >> 6;
933 height = ascent + descent; 1159 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1160 width = 0;
934 1161
1162 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1163
1164 XftUnlockFace (f);
1165
1166 int glheight = height;
1167
1168 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1169 {
1170 FcChar16 ch = *t;
1171
1172 if (cs != CS_UNICODE
1173 && ch > 0x100
1174 && FROM_UNICODE (cs, ch) == NOCHAR)
1175 continue;
1176
1177 // ignore characters we wouldn't use anyways
1178 bool careful;
1179 if (!has_char (*t, &prop, careful))
1180 continue;
1181
1182 XGlyphInfo g;
1183 XftTextExtents16 (disp, f, &ch, 1, &g);
1184
1185 g.width -= g.x;
1186
1187 int wcw = wcwidth (ch);
1188 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1189
1190 if (width < g.width ) width = g.width;
1191 if (height < g.height ) height = g.height;
1192 if (glheight < g.height - g.y) glheight = g.height - g.y;
1193 }
1194
1195 if (!width)
1196 {
1197 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1198
1199 XftFontClose (disp, f);
1200 f = 0;
1201
1202 success = false;
1203 break;
1204 }
1205
1206 if (prop.height == rxvt_fontprop::unset
935 if (height <= prop.height || !prop.height) 1207 || (height <= prop.height && glheight <= prop.height)
1208 || height <= 2
1209 || !scalable)
936 break; 1210 break;
937 1211
938 if (ftheight) 1212 if (ftheight)
939 { 1213 {
940 // take smaller steps near the end 1214 // take smaller steps near the end
941 if (height > prop.height + 1) ftheight++; 1215 if (height > prop.height + 1) ftheight++;
942 if (height > prop.height + 2) ftheight++; 1216 if (height > prop.height + 2) ftheight++;
943 if (height > prop.height + 3) ftheight++; 1217 if (height > prop.height + 3) ftheight++;
944 1218
945 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); 1219 ftheight -= height - prop.height;
946 } 1220 }
947 else 1221 else
948 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); 1222 ftheight = prop.height - 1;
1223
1224 XftFontClose (disp, f);
1225 FcPatternDel (match, FC_PIXEL_SIZE);
1226 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1227 }
1228
1229 FcPatternDestroy (match);
1230
1231#if 0 // do it per-character
1232 if (prop.width != rxvt_fontprop::unset && width > prop.width)
949 } 1233 {
1234 clear ();
1235 success = false;
1236 }
1237#endif
950 1238
951 XftUnlockFace (f); 1239 return success;
1240}
1241
1242bool
1243rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1244{
1245 careful = false;
1246
1247 if (!XftCharExists (DISPLAY, f, unicode))
1248 return false;
1249
1250 if (!prop || prop->width == rxvt_fontprop::unset)
1251 return true;
1252
1253 // check character against base font bounding box
1254 FcChar32 ch = unicode;
1255 XGlyphInfo g;
1256 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1257
1258 int w = g.width - g.x;
1259 int wcw = wcwidth (unicode);
1260 if (wcw > 0) w = (w + wcw - 1) / wcw;
1261
1262 careful = w > prop->width;
1263 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1264 return false;
952 1265
953 return true; 1266 return true;
954}
955
956bool
957rxvt_font_xft::has_codepoint (unicode_t unicode)
958{
959 return XftCharExists (DISPLAY, f, unicode);
960} 1267}
961 1268
962void 1269void
963rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1270rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len, 1271 const text_t *text, int len,
965 int fg, int bg) 1272 int fg, int bg)
966{ 1273{
967 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1274 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
968 1275
969 if (!slow && width == r->TermWin.fwidth && 0) 1276 XGlyphInfo extents;
970 { 1277 XftGlyphSpec *enc = (XftGlyphSpec *) get_enc_buf (len * sizeof (XftGlyphSpec));
971 if (sizeof (text_t) == sizeof (FcChar16)) 1278 XftGlyphSpec *ep = enc;
972 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 1279
973 else 1280 // cut trailing spaces
974 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 1281 while (len && text [len - 1] == ' ')
975 } 1282 len--;
976 else 1283
977 {
978 while (len) 1284 while (len)
1285 {
1286 int cwidth = r->fwidth;
1287 FcChar32 fc = *text++; len--;
1288
1289 while (len && *text == NOCHAR)
1290 text++, len--, cwidth += r->fwidth;
979 { 1291
980 if (*text != NOCHAR && *text != ' ') 1292 if (fc != ' ') // skip spaces
981 {
982 int fwidth = r->TermWin.fwidth;
983 if (len >= 2 && text[1] == NOCHAR)
984 fwidth *= 2;
985
986 XGlyphInfo extents;
987 if (sizeof (text_t) == sizeof (FcChar16))
988 {
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);
992 }
993 else
994 {
995 XGlyphInfo extents;
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);
999 }
1000 }
1001
1002 x += r->TermWin.fwidth;
1003 text++;
1004 len--;
1005 } 1293 {
1294 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1295 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1296
1297 ep->glyph = gl;
1298 ep->x = x + (cwidth - extents.xOff >> 1);
1299 ep->y = y + ascent;
1300 ep++;
1301 }
1302
1303 x += cwidth;
1006 } 1304 }
1305
1306 if (ep != enc)
1307 XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc);
1007} 1308}
1008#endif 1309#endif
1009 1310
1010///////////////////////////////////////////////////////////////////////////// 1311/////////////////////////////////////////////////////////////////////////////
1011 1312
1012rxvt_fontset::rxvt_fontset (rxvt_t r) 1313rxvt_fontset::rxvt_fontset (rxvt_t r)
1013: r (r), fontdesc (0) 1314: fontdesc (0), r (r)
1014{ 1315{
1015 clear (); 1316 clear ();
1016} 1317}
1017 1318
1018rxvt_fontset::~rxvt_fontset () 1319rxvt_fontset::~rxvt_fontset ()
1021} 1322}
1022 1323
1023void 1324void
1024rxvt_fontset::clear () 1325rxvt_fontset::clear ()
1025{ 1326{
1327 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1328 = rxvt_fontprop::unset;
1329
1026 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1330 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1027 FONT_UNREF (*i); 1331 FONT_UNREF (*i);
1028 1332
1333 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1334 delete *p;
1335
1029 free (fontdesc); fontdesc = 0; 1336 free (fontdesc); fontdesc = 0;
1030 1337
1031 fonts.clear (); 1338 fonts.clear ();
1032 base_id = 0;
1033 base_prop.height = 0x7fffffff;
1034 base_prop.weight = rxvt_fontprop::medium;
1035 base_prop.slant = rxvt_fontprop::roman;
1036 1339
1037 fallback = fallback_fonts; 1340 fallback = fallback_fonts;
1038} 1341}
1039 1342
1040rxvt_font * 1343rxvt_font *
1043 rxvt_font *f; 1346 rxvt_font *f;
1044 1347
1045 if (!name || !*name) 1348 if (!name || !*name)
1046 { 1349 {
1047 name = ""; 1350 name = "";
1048 f = new rxvt_font_default; 1351 f = new rxvt_font_default (this);
1049 } 1352 }
1050#if XFT 1353#if XFT
1051 else if (!strncmp (name, "xft:", 4)) 1354 else if (!strncmp (name, "xft:", 4))
1052 { 1355 {
1053 name += 4; 1356 name += 4;
1054 f = new rxvt_font_xft; 1357 f = new rxvt_font_xft ();
1055 } 1358 }
1056#endif 1359#endif
1057 else if (!strncmp (name, "x:", 2)) 1360 else if (!strncmp (name, "x:", 2))
1058 { 1361 {
1059 name += 2; 1362 name += 2;
1060 f = new rxvt_font_x11; 1363 f = new rxvt_font_x11;
1061 } 1364 }
1062 else 1365 else
1063 f = new rxvt_font_x11; 1366 f = new rxvt_font_x11;
1064 1367
1065 f->fs = this;
1066 f->set_term (r); 1368 f->set_term (r);
1067 f->set_name (strdup (name)); 1369 f->set_name (strdup (name));
1068 1370
1069 f->cs = cs; 1371 f->cs = cs;
1070 f->loaded = false; 1372 f->loaded = false;
1082 char buf[512]; 1384 char buf[512];
1083 const char *end; 1385 const char *end;
1084 1386
1085 do 1387 do
1086 { 1388 {
1087 while (*desc <= ' ') desc++; 1389 while (*desc && *desc <= ' ')
1390 desc++;
1391
1392 codeset cs = CS_UNICODE;
1088 1393
1089 if (*desc == '[') 1394 if (*desc == '[')
1090 { 1395 {
1091 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1396 char spec[256];
1092
1093 //const char *extra = desc++; // not yet used 1397 const char *extra = ++desc; // not yet used
1094 1398
1095 desc = strchr (desc, ']'); 1399 desc = strchr (desc, ']');
1096 1400
1097 if (!desc) 1401 if (!desc)
1098 { 1402 {
1099 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n"); 1403 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1100 break; 1404 break;
1101 } 1405 }
1406
1407 memcpy (spec, extra, min (desc - extra, 255));
1408 spec[min (desc - extra, 255)] = 0;
1409
1410 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1411 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1412 else
1413 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1102 1414
1103 desc++; 1415 desc++;
1104 while (*desc <= ' ') desc++; 1416 while (*desc <= ' ') desc++;
1105 } 1417 }
1106 1418
1111 if (end - desc < 511) 1423 if (end - desc < 511)
1112 { 1424 {
1113 strncpy (buf, desc, end - desc); 1425 strncpy (buf, desc, end - desc);
1114 buf[end - desc] = 0; 1426 buf[end - desc] = 0;
1115 1427
1116 fonts.push_back (new_font (buf, CS_UNICODE)); 1428 fonts.push_back (new_font (buf, cs));
1117 } 1429 }
1430 else
1431 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1118 1432
1119 desc = end + 1; 1433 desc = end + 1;
1120 } 1434 }
1121 while (*end); 1435 while (*end);
1122 } 1436 }
1123} 1437}
1124 1438
1125bool 1439bool
1126rxvt_fontset::realize_font (int i) 1440rxvt_fontset::realize_font (int i)
1127{ 1441{
1442 if (i < 0 || i >= fonts.size ())
1443 return false;
1444
1128 if (fonts[i]->loaded) 1445 if (fonts[i]->loaded)
1129 return true; 1446 return true;
1130 1447
1131 fonts[i]->loaded = true; 1448 fonts[i]->loaded = true;
1132 1449
1133 if (!fonts[i]->load (base_prop)) 1450 if (!fonts[i]->load (prop))
1134 { 1451 {
1135 fonts[i]->cs = CS_UNKNOWN; 1452 fonts[i]->cs = CS_UNKNOWN;
1136 return false; 1453 return false;
1137 } 1454 }
1138 1455
1148 1465
1149 fonts.push_back (new_font (0, CS_UNICODE)); 1466 fonts.push_back (new_font (0, CS_UNICODE));
1150 realize_font (0); 1467 realize_font (0);
1151 1468
1152 add_fonts (desc); 1469 add_fonts (desc);
1153
1154 if (!base_id)
1155 base_id = 1;
1156
1157 // we currently need a base-font, no matter what
1158 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1159 {
1160 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1161 add_fonts ("fixed");
1162 base_id = fonts.size () - 1;
1163 }
1164
1165 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1166 return false;
1167
1168 base_prop = fonts[base_id]->properties ();
1169 1470
1170 return true; 1471 return true;
1171} 1472}
1172 1473
1173int 1474int
1179 1480
1180 return -1; 1481 return -1;
1181} 1482}
1182 1483
1183int 1484int
1184rxvt_fontset::find_font (unicode_t unicode, bool bold) 1485rxvt_fontset::find_font (unicode_t unicode)
1185{ 1486{
1487 if (unicode >= 1<<20)
1488 return 0;
1186 1489
1187 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii 1490 unicode_t hi = unicode >> 8;
1188 i < fonts.size (); 1491
1189 i++) 1492 if (hi < fmap.size ()
1493 && fmap[hi]
1494 && (*fmap[hi])[unicode & 0xff] != 0xff)
1495 return (*fmap[hi])[unicode & 0xff];
1496
1497 unsigned int i;
1498
1499 for (i = 0; i < fonts.size (); i++)
1190 { 1500 {
1191 rxvt_font *f = fonts[i]; 1501 rxvt_font *f = fonts[i];
1192 1502
1193 if (!f->loaded) 1503 if (!f->loaded)
1194 { 1504 {
1195 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1505 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1196 goto next_font; 1506 goto next_font;
1197 1507
1198 if (!realize_font (i)) 1508 if (!realize_font (i))
1199 goto next_font; 1509 goto next_font;
1510
1511 if (prop.ascent != rxvt_fontprop::unset)
1512 max_it (f->ascent, prop.ascent);
1200 } 1513 }
1201 1514
1202 if (f->cs == CS_UNKNOWN) 1515 if (f->cs == CS_UNKNOWN)
1203 goto next_font; 1516 goto next_font;
1204 1517
1205 if (bold && f->properties ().weight < rxvt_fontprop::bold) 1518 bool careful;
1519 if (f->has_char (unicode, &prop, careful))
1520 {
1521 if (careful)
1522 i |= 128;
1523
1206 goto next_font; 1524 goto found;
1207 1525 }
1208 if (f->has_codepoint (unicode))
1209 return i;
1210 1526
1211 next_font: 1527 next_font:
1212 if (i == fonts.size () - 1) 1528 if (i == fonts.size () - 1)
1213 { 1529 {
1214 if (fallback->name) 1530 if (fallback->name)
1221 { 1537 {
1222 // try to find a new font. 1538 // try to find a new font.
1223 // only xft currently supported, as there is no 1539 // only xft currently supported, as there is no
1224 // way to configure this and xft is easier to hack in, 1540 // way to configure this and xft is easier to hack in,
1225 // while x11 has more framework in place already. 1541 // while x11 has more framework in place already.
1226#if XFT 1542 // TODO: this is a real resource hog, xft takes ages(?)
1543#if XFT && USE_SLOW_LOOKUP
1227 // grab the first xft font that seems suitable 1544 // grab the first xft font that seems suitable
1228 FcPattern *p = FcPatternCreate (); 1545 FcPattern *p = FcPatternCreate ();
1229 1546
1230 FcCharSet *s = FcCharSetCreate (); 1547 FcCharSet *s = FcCharSetCreate ();
1231 FcCharSetAddChar (s, unicode); 1548 FcCharSetAddChar (s, unicode);
1232 FcPatternAddCharSet (p, FC_CHARSET, s); 1549 FcPatternAddCharSet (p, FC_CHARSET, s);
1233 // charsets don't help that much, as xft might return 1550 // charsets don't help that much, as xft might return
1234 // a non-matching font even if a better font is available :/ 1551 // a non-matching font even if a better font is available :/
1235 1552
1553 x x x x TODO prop might have unset contents
1236 FcPatternAddInteger (p, FC_PIXEL_SIZE, base_prop.height); 1554 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1237 FcPatternAddInteger (p, FC_WEIGHT, base_prop.weight); 1555 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1238 FcPatternAddInteger (p, FC_SLANT, base_prop.slant); 1556 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1239 FcPatternAddBool (p, FC_MINSPACE, 1); 1557 FcPatternAddBool (p, FC_MINSPACE, 1);
1240 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1558 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1241 1559
1242 XftResult result; 1560 XftResult result;
1243 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1561 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1244 1562
1245 FcPatternDestroy (p); 1563 FcPatternDestroy (p);
1246 1564
1247 if (match) 1565 if (match)
1248 { 1566 {
1263#endif 1581#endif
1264 } 1582 }
1265 } 1583 }
1266 } 1584 }
1267 1585
1268 // if no bold font found, use a regular one 1586 /* we must return SOME font */
1269 if (bold) 1587 i = 0;
1270 return find_font (unicode);
1271 1588
1272 return 0; /* we must return SOME font */ 1589found:
1273} 1590 // found a font, cache it
1591 if (i < 255)
1592 {
1593 while (hi >= fmap.size ())
1594 fmap.push_back (0);
1274 1595
1596 if (!fmap[hi])
1597 {
1598 fmap[hi] = (pagemap *)new pagemap;
1599 memset (fmap[hi], 0xff, sizeof (pagemap));
1600 }
1275 1601
1602 (*fmap[hi])[unicode & 0xff] = i;
1603 }
1276 1604
1605 return i;
1606}
1607
1608
1609

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines