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.41 by root, Wed Aug 25 02:33:09 2004 UTC vs.
Revision 1.90 by root, Mon Jan 30 02:21:20 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"
24#include "rxvtutil.h" 25#include "rxvtutil.h"
25#include "rxvtfont.h" 26#include "rxvtfont.h"
26 27
27#include <cstdlib> 28#include <cstdlib>
28#include <wchar.h> 29#include <wchar.h>
29#include <inttypes.h> 30#include <inttypes.h>
30 31
31#define DISPLAY r->display->display
32#define TGC r->TermWin.gc
33
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 32#define MAX_OVERLAP_ROMAN (8 + 2) // max. character width in 8ths of the base width
33#define MAX_OVERLAP_ITALIC (8 + 3) // max. overlap for italic fonts
34
35#define OVERLAP_OK(w,prop) (w) > ( \
36 prop->slant >= rxvt_fontprop::italic \
37 ? (prop->width * MAX_OVERLAP_ITALIC + 7) >> 3 \
38 : (prop->width * MAX_OVERLAP_ROMAN + 7) >> 3 \
39 )
35 40
36const struct rxvt_fallback_font { 41const struct rxvt_fallback_font {
37 codeset cs; 42 codeset cs;
38 const char *name; 43 const char *name;
39} fallback_fonts[] = { 44} fallback_fonts[] = {
88 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 93 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
89 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 94 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 95 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91#endif 96#endif
92 97
93#if ENCODING_CN || ENCODING_CN_EXT 98#if ENCODING_ZH || ENCODING_ZH_EXT
94# if XFT 99# if XFT
100 { CS_GBK_0, "xft:AR PL KaitiM GB" },
101 { CS_GBK_0, "xft:AR PL SungtiL GB" },
102 { CS_GBK_0, "xft::spacing=100:lang=zh" },
95 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 103 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
96 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 104 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
97 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 105 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
98 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 106 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
99 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, 107 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
100# endif 108# endif
109 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
101 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 110 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
102 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 111 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
103 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 112 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
113 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
104 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 114 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
105 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 115 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
106 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 116 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
107 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 117 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
108 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 118 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
125 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 135 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
126 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 136 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
127 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 137 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
128 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 138 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
129#if XFT 139#if XFT
130 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false"}, 140 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
131 { CS_UNICODE, "xft:Andale Mono:antialias=false" },
132 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" },
133 { CS_UNICODE, "xft:Courier New:antialias=false" }, 141 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
142 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
143 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
134 144
135 // FreeMono is usually uglier than x fonts, so try last only. 145 // FreeMono is usually uglier than x fonts, so try last only.
136 { CS_UNICODE, "xft:FreeMono" }, 146 { CS_UNICODE, "xft:FreeMono:autohint=true" },
137#endif 147#endif
138 148
139 { CS_UNKNOWN, 0 } 149 { CS_UNKNOWN, 0 }
140}; 150};
141 151
142// these characters are used to guess the font height and width 152// these characters are used to guess the font height and width
143// pango uses a similar algorithm and eosn't trust the font either. 153// pango uses a similar algorithm and doesn't trust the font either.
144static uint16_t extent_test_chars[] = { 154static uint16_t extent_test_chars[] = {
145 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 155 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
146 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 156 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
147 0x304c, 0x672c, // が本 157 0x304c, 0x672c, // が本
148}; 158};
149 159
150#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0])) 160#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
151 161
162#define dTermDisplay Display *disp = term->xdisp
163#define dTermGC GC gc = term->gc
164
152///////////////////////////////////////////////////////////////////////////// 165/////////////////////////////////////////////////////////////////////////////
153 166
154#if XFT 167#if XFT
155rxvt_drawable::~rxvt_drawable () 168rxvt_drawable::~rxvt_drawable ()
156{ 169{
159} 172}
160 173
161rxvt_drawable::operator XftDraw *() 174rxvt_drawable::operator XftDraw *()
162{ 175{
163 if (!xftdrawable) 176 if (!xftdrawable)
164 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap); 177 xftdrawable = XftDrawCreate (screen->xdisp, drawable, screen->visual, screen->cmap);
165 178
166 return xftdrawable; 179 return xftdrawable;
167} 180}
168#endif 181#endif
169 182
170///////////////////////////////////////////////////////////////////////////// 183/////////////////////////////////////////////////////////////////////////////
171
172static void *enc_buf;
173static uint32_t enc_len;
174
175static inline void *
176get_enc_buf (uint32_t len)
177{
178 if (len > enc_len)
179 {
180 free (enc_buf);
181 enc_buf = malloc (len);
182 enc_len = len;
183 }
184
185 return enc_buf;
186}
187 184
188static const char * 185static const char *
189enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero) 186enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
190{ 187{
191 uint8_t *buf = (uint8_t *)get_enc_buf (len); 188 uint8_t *buf = rxvt_temp_buf<uint8_t> (len);
189 uint8_t *res = buf;
192 190
193 while (len--) 191 while (len--)
194 { 192 {
195 uint32_t c = FROM_UNICODE (cs, *text++); 193 uint32_t c = FROM_UNICODE (cs, *text++);
196 194
201 } 199 }
202 200
203 *buf++ = c; 201 *buf++ = c;
204 } 202 }
205 203
206 return (const char *)enc_buf; 204 return (const char *)res;
207} 205}
208 206
209static const XChar2b * 207static const XChar2b *
210enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero) 208enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
211{ 209{
212 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 210 XChar2b *buf = rxvt_temp_buf<XChar2b> (len);
211 XChar2b *res = buf;
213 212
214 while (len--) 213 while (len--)
215 { 214 {
216 uint32_t c = FROM_UNICODE (cs, *text++); 215 uint32_t c = FROM_UNICODE (cs, *text++);
217 216
224 buf->byte1 = c >> 8; 223 buf->byte1 = c >> 8;
225 buf->byte2 = c; 224 buf->byte2 = c;
226 buf++; 225 buf++;
227 } 226 }
228 227
229 return (XChar2b *)enc_buf; 228 return res;
230} 229}
231 230
232///////////////////////////////////////////////////////////////////////////// 231/////////////////////////////////////////////////////////////////////////////
233 232
234void 233void
240 if (this->name) free (this->name); // let the compiler optimize 239 if (this->name) free (this->name); // let the compiler optimize
241 this->name = name; 240 this->name = name;
242} 241}
243 242
244void 243void
245rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 244rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
246{ 245{
246 dTermDisplay;
247 dTermGC;
248
247 if (color == Color_bg) 249 if (color == Color_bg)
248 XClearArea (d.display->display, d, x, y, w, h, FALSE); 250 XClearArea (disp, d, x, y, w, h, false);
249 else if (color >= 0) 251 else if (color >= 0)
250 { 252 {
251#if XFT 253#if XFT
252 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h); 254 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
253#else 255#else
254 XSetForeground (d.display->display, TGC, r->pix_colors[color]); 256 XSetForeground (disp, gc, term->pix_colors[color]);
255 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 257 XFillRectangle (disp, d, gc, x, y, w, h);
256#endif 258#endif
257 } 259 }
258} 260}
259 261
260#include "table/linedraw.h" 262#include "table/linedraw.h"
271 rxvt_fontprop properties () 273 rxvt_fontprop properties ()
272 { 274 {
273 rxvt_fontprop p; 275 rxvt_fontprop p;
274 276
275 p.width = p.height = 1; 277 p.width = p.height = 1;
278 p.ascent = rxvt_fontprop::unset;
276 p.weight = rxvt_fontprop::medium; 279 p.weight = rxvt_fontprop::medium;
277 p.slant = rxvt_fontprop::roman; 280 p.slant = rxvt_fontprop::roman;
278 281
279 return p; 282 return p;
280 } 283 }
287 set_name (strdup ("built-in support font")); 290 set_name (strdup ("built-in support font"));
288 291
289 return true; 292 return true;
290 } 293 }
291 294
292 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 295 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
293 { 296 {
294 careful = false; 297 careful = false;
295 298
296 if (unicode <= 0x001f) 299 if (unicode <= 0x001f)
297 return true; 300 return true;
300 return false; 303 return false;
301 304
302 if (unicode <= 0x009f) 305 if (unicode <= 0x009f)
303 return true; 306 return true;
304 307
305 if (unicode >= 0x2500 && unicode <= 0x259f) 308 if (unicode >= 0x2500 && unicode <= 0x259f &&
309 !term->option (Opt_skipBuiltinGlyphs))
306 return true; 310 return true;
307 311
308 if (IS_COMPOSE (unicode)) 312 if (IS_COMPOSE (unicode))
309 return true; 313 return true;
310 314
326void 330void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 331rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 332 const text_t *text, int len,
329 int fg, int bg) 333 int fg, int bg)
330{ 334{
335 dTermDisplay;
336 dTermGC;
337
331 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 338 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
332 339
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 340 XSetForeground (disp, gc, term->pix_colors[fg]);
334 341
335 while (len--) 342 while (len)
336 { 343 {
337#if ENABLE_COMBINING 344#if ENABLE_COMBINING
338 compose_char *cc; 345 compose_char *cc;
339#endif 346#endif
347 const text_t *tp = text;
340 text_t t = *text++; 348 text_t t = *tp;
349
350 while (++text, --len && *text == NOCHAR)
351 ;
352
353 int width = text - tp;
354 int fwidth = term->fwidth * width;
341 355
342 if (0x2500 <= t && t <= 0x259f) 356 if (0x2500 <= t && t <= 0x259f)
343 { 357 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 358 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 359 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 360 uint32_t *b = a + (offs & 15);
347 361
348 int W = r->TermWin.fwidth; 362 int W = fwidth;
349 int H = r->TermWin.fheight; 363 int H = term->fheight;
350 364
351 int x_[16]; 365 int x_[16];
352 int y_[16]; 366 int y_[16];
353 367
354 for (int i = 0; i <= 8; i++) 368 for (int i = 0; i <= 8; i++)
362 376
363 XGCValues gcv; 377 XGCValues gcv;
364 378
365 gcv.cap_style = CapButt; 379 gcv.cap_style = CapButt;
366 gcv.line_width = 0; 380 gcv.line_width = 0;
367 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 381 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
368 382
369 while (a < b) 383 while (a < b)
370 { 384 {
371 uint32_t command = *a++; 385 uint32_t command = *a++;
372 386
379 int y2 = y_[(command >> 0) & 15]; 393 int y2 = y_[(command >> 0) & 15];
380 394
381 switch (op) 395 switch (op)
382 { 396 {
383 case 0: // line 397 case 0: // line
384 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 398 XDrawLine (disp, d, gc, x1, y1, x2, y2);
385 break; 399 break;
386 400
387 case 1: // rectangle, possibly stippled 401 case 1: // rectangle, possibly stippled
388 if (a) 402 if (a)
389 { 403 {
390 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 404 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
391 405
392 gcv.fill_style = FillStippled; 406 gcv.fill_style = FillStippled;
393 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 407 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
394 gcv.ts_x_origin = x; 408 gcv.ts_x_origin = x;
395 gcv.ts_y_origin = y; 409 gcv.ts_y_origin = y;
396 410
397 XChangeGC (d.display->display, TGC, 411 XChangeGC (disp, gc,
398 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 412 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
399 &gcv); 413 &gcv);
400 } 414 }
401 415
402 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 416 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
403 417
404 if (a) 418 if (a)
405 { 419 {
406 XFreePixmap (d.display->display, gcv.stipple); 420 XFreePixmap (disp, gcv.stipple);
407 gcv.stipple = 0; 421 gcv.stipple = 0;
408 gcv.fill_style = FillSolid; 422 gcv.fill_style = FillSolid;
409 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 423 XChangeGC (disp, gc, GCFillStyle, &gcv);
410 } 424 }
411 break; 425 break;
412 case 2: // arc 426 case 2: // arc
413 XDrawArc (d.display->display, d, TGC, 427 XDrawArc (disp, d, gc,
414 x1 - W/2, y1 - H/2, W-1, H-1, 428 x1 - W/2, y1 - H/2, W-1, H-1,
415 (a - 1) * 90*64, (b - 1) * 90*64); 429 (a - 1) * 90*64, (b - 1) * 90*64);
416 break; 430 break;
417 } 431 }
418 } 432 }
419 } 433 }
420#if ENABLE_COMBINING 434#if ENABLE_COMBINING
421 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 435 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
422 { 436 {
437 text_t chrs[2];
438 width = min (2, width);
439 chrs [1] = NOCHAR;
440
441 *chrs = cc->c1;
423 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 442 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
424 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 443 f1->draw (d, x, y, chrs, width, fg, bg);
444
425 if (cc->c2 != NOCHAR) 445 if (cc->c2 != NOCHAR)
426 { 446 {
447 bool careful;
448
427 // prefer font of first character, for no good reasons 449 // prefer font of first character, for no good reasons
428 bool careful; 450 *chrs = cc->c2;
429 rxvt_font *f2 = f1->has_char (cc->c2, 0, careful) 451 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
430 ? f1 452 ? f1
431 : (*fs)[fs->find_font (cc->c2)]; 453 : (*fs)[fs->find_font (cc->c2)];
432 454
433 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 455 f2->draw (d, x, y, chrs, width, fg, -1);
434 } 456 }
435 } 457 }
436#endif 458#endif
437 else 459 else
438 switch (t) 460 switch (t)
441 case ZERO_WIDTH_CHAR: 463 case ZERO_WIDTH_CHAR:
442 case NOCHAR: 464 case NOCHAR:
443 break; 465 break;
444 466
445 default: 467 default:
446 int w = 0;
447 while (len > 0 && *text == NOCHAR)
448 {
449 ++text;
450 --len;
451 w += r->TermWin.fwidth;
452 }
453
454 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 468 XDrawRectangle (disp, d, gc, x + 2, y + 2,
455 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 469 fwidth - 4, term->fheight - 4);
456 x += w;
457 } 470 }
458 471
459 x += r->TermWin.fwidth; 472 x += fwidth;
460 } 473 }
461} 474}
462 475
463///////////////////////////////////////////////////////////////////////////// 476/////////////////////////////////////////////////////////////////////////////
464 477
469 482
470 rxvt_fontprop properties (); 483 rxvt_fontprop properties ();
471 484
472 bool load (const rxvt_fontprop &prop); 485 bool load (const rxvt_fontprop &prop);
473 486
474 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful); 487 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
475 488
476 void draw (rxvt_drawable &d, int x, int y, 489 void draw (rxvt_drawable &d, int x, int y,
477 const text_t *text, int len, 490 const text_t *text, int len,
478 int fg, int bg); 491 int fg, int bg);
479 492
480 bool slow; // wether this is a proportional font or has other funny characteristics 493 bool slow; // wether this is a proportional font or has other funny characteristics
481 XFontStruct *f; 494 XFontStruct *f;
482 codeset cs; 495 codeset cs;
483 bool enc2b, encm; 496 bool enc2b, encm;
484 497
485 char *get_property (XFontStruct *f, const char *property, const char *repl) const; 498 char *get_property (XFontStruct *f, Atom property, const char *repl) const;
486 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth); 499 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
487 bool set_properties (rxvt_fontprop &p, XFontStruct *f); 500 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
488 bool set_properties (rxvt_fontprop &p, const char *name); 501 bool set_properties (rxvt_fontprop &p, const char *name);
489}; 502};
490 503
491char * 504char *
492rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 505rxvt_font_x11::get_property (XFontStruct *f, Atom property, const char *repl) const
493{ 506{
494 unsigned long value; 507 unsigned long value;
495 508
496 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 509 if (XGetFontProperty (f, property, &value))
497 return XGetAtomName (DISPLAY, value); 510 return XGetAtomName (term->xdisp, value);
498 else 511 else
499 return rxvt_strdup (repl); 512 return rxvt_strdup (repl);
500} 513}
501 514
502rxvt_fontprop 515rxvt_fontprop
510bool 523bool
511rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 524rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
512{ 525{
513 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 526 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
514 p.height = height; 527 p.height = height;
528 p.ascent = rxvt_fontprop::unset;
515 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 529 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
516 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 530 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
517 531
518 return true; 532 return true;
519} 533}
520 534
521bool 535bool
522rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 536rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
523{ 537{
524 unsigned long height; 538 unsigned long height;
539
540#if 0
525 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 541 if (!XGetFontProperty (f, XInternAtom (term->xdisp, "PIXEL_SIZE", 0), &height))
526 return false; 542 return false;
543#else
544 height = f->ascent + f->descent;
545#endif
527 546
528 unsigned long avgwidth; 547 unsigned long avgwidth;
529 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 548 if (!XGetFontProperty (f, term->xa [XA_AVERAGE_WIDTH], &avgwidth))
530 avgwidth = 0; 549 avgwidth = 0;
531 550
532 char *weight = get_property (f, "WEIGHT_NAME", "medium"); 551 char *weight = get_property (f, term->xa [XA_WEIGHT_NAME], "medium");
533 char *slant = get_property (f, "SLANT", "r"); 552 char *slant = get_property (f, term->xa [XA_SLANT], "r");
534 553
535 set_properties (p, height, weight, slant, avgwidth); 554 set_properties (p, height, weight, slant, avgwidth);
536 555
537 free (weight); 556 free (weight);
538 free (slant); 557 free (slant);
539 558
559 p.ascent = f->ascent;
560
540 return true; 561 return true;
541} 562}
542 563
543bool 564bool
544rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) 565rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
545{ 566{
567 dTermDisplay;
546 int slashes = 0; 568 int slashes = 0;
547 const char *comp[13]; 569 const char *comp[13];
548 570
549 for (const char *c = name; *c; c++) 571 for (const char *c = name; *c; c++)
550 if (*c == '-') 572 if (*c == '-')
558 if (slashes >= 13 580 if (slashes >= 13
559 && (*comp[ 6] >= '1' && *comp[ 6] <= '9') 581 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
560 && (*comp[11] >= '0' && *comp[11] <= '9')) 582 && (*comp[11] >= '0' && *comp[11] <= '9'))
561 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11])); 583 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
562 584
563 XFontStruct *f = XLoadQueryFont (DISPLAY, name); 585 XFontStruct *f = XLoadQueryFont (disp, name);
564 586
565 if (f) 587 if (f)
566 { 588 {
567 // the font should really exist now. if not, we have a problem 589 // the font should really exist now. if not, we have a problem
568 // (e.g. if the user did xset fp rehash just when we were searching fonts). 590 // (e.g. if the user did xset fp rehash just when we were searching fonts).
569 // in that case, just return garbage. 591 // in that case, just return garbage.
570 bool ret = set_properties (p, f); 592 bool ret = set_properties (p, f);
571 XFreeFont (DISPLAY, f); 593 XFreeFont (disp, f);
572 return ret; 594 return ret;
573 } 595 }
574 else 596 else
575 return false; 597 return false;
576} 598}
614} 636}
615 637
616bool 638bool
617rxvt_font_x11::load (const rxvt_fontprop &prop) 639rxvt_font_x11::load (const rxvt_fontprop &prop)
618{ 640{
641 dTermDisplay;
642
619 clear (); 643 clear ();
620 644
621 char field_str[64]; // enough for 128 bits 645 char field_str[64]; // enough for 128 bits
622 646
623 // first morph the font if required 647 // first morph the font if required
626 { 650 {
627 char fname[1024]; 651 char fname[1024];
628 652
629 if (name[0] != '-') 653 if (name[0] != '-')
630 { 654 {
631 f = XLoadQueryFont (DISPLAY, name); 655 f = XLoadQueryFont (disp, name);
632 656
633 if (!f) 657 if (!f)
634 return false; 658 return false;
635 659
636 char *new_name = get_property (f, "FONT", name); 660 char *new_name = get_property (f, XA_FONT, name);
637 661
638 if (new_name) 662 if (new_name)
639 set_name (new_name); 663 set_name (new_name);
640 else 664 else
641 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 665 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
642 666
643 XFreeFont (DISPLAY, f); 667 XFreeFont (disp, f);
644 f = 0; 668 f = 0;
645 } 669 }
646 670
647 if (prop.weight != rxvt_fontprop::unset) 671 if (prop.weight != rxvt_fontprop::unset)
648 { 672 {
659 ? "r" : "i"); // TODO: handle "o"blique, too 683 ? "r" : "i"); // TODO: handle "o"blique, too
660 set_name (strdup (fname)); 684 set_name (strdup (fname));
661 } 685 }
662 } 686 }
663 687
688 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
689 ? 0 : prop.height);
690
691 struct font_weight {
692 char *name;
693 int diff;
694
695 void clear ()
696 {
697 name = 0;
698 diff = 0x7fffffff;
699 }
700
701 font_weight () { clear (); }
702 ~font_weight () { free (name); }
703 };
704
664 char **list; 705 char **list;
665 int count; 706 int count;
666 list = XListFonts (DISPLAY, name, 1024, &count); 707 list = XListFonts (disp, name, 4000, &count);
667 708
668 set_name (0); 709 set_name (0);
669 710
670 if (!list) 711 if (!list)
671 return false; 712 return false;
672 713
673 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 714 font_weight *fonts = new font_weight[count];
674 ? 0 : prop.height);
675 715
676 int bestdiff = 0x7fffffff;
677 for (int i = 0; i < count; i++) 716 for (int i = 0; i < count; i++)
678 { 717 {
679 rxvt_fontprop p; 718 rxvt_fontprop p;
680 char fname[1024]; 719 char fname[1024];
681 720
682 int diff = 0; 721 int diff = 0;
683 722
684 if (replace_field (fname, list[i], 6, '0', field_str)) 723 if (replace_field (fname, list[i], 6, '0', field_str))
685 diff += 10; // slightly penalize scalable fonts 724 diff += 10; // slightly penalize scalable fonts
725 else if (replace_field (fname, list[i], 11, '0', "0"))
726 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
686 727
687 if (!set_properties (p, fname)) 728 if (!set_properties (p, fname))
688 continue; 729 continue;
689 730
690 if (prop.height != rxvt_fontprop::unset 731 if (prop.height != rxvt_fontprop::unset
694 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 735 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
695 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); 736 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
696 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); 737 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
697 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); 738 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
698 739
699 if (!name // compare against best found so far 740 fonts[i].name = strdup (fname);
700 || diff < bestdiff) 741 fonts[i].diff = diff;
701 {
702 set_name (strdup (fname));
703 bestdiff = diff;
704 }
705 } 742 }
706 743
707 XFreeFontNames (list); 744 XFreeFontNames (list);
708 745
709 if (!name) 746 // this loop only iterates when the guessed font-size is too small
710 return false; 747 for (;;)
748 {
749 font_weight *best = fonts + count - 1;
711 750
712 f = XLoadQueryFont (DISPLAY, name); 751 for (font_weight *w = best; w-- > fonts; )
752 if (w->diff <= best->diff)
753 best = w;
754
755 if (!best->name
756 || !(f = XLoadQueryFont (disp, best->name)))
757 break;
758
759 set_name (best->name);
760 best->clear ();
761
762 ascent = f->ascent;
763 descent = f->descent;
764 height = ascent + descent;
765
766 if (prop.height == rxvt_fontprop::unset
767 || height <= prop.height)
768 break; // font is ready for use
769
770 // PIXEL_SIZE small enough, but real height too large
771 clear ();
772 }
773
774 delete [] fonts;
713 775
714 if (!f) 776 if (!f)
715 return false; 777 return false;
716 778
717 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 779 char *registry = get_property (f, term->xa [XA_CHARSET_REGISTRY], 0);
718 char *encoding = get_property (f, "CHARSET_ENCODING", 0); 780 char *encoding = get_property (f, term->xa [XA_CHARSET_ENCODING], 0);
719 781
720 if (registry && encoding) 782 if (registry && encoding)
721 { 783 {
722 char charset[64]; 784 char charset[64];
723 snprintf (charset, 64, "%s-%s", registry, encoding); 785 snprintf (charset, 64, "%s-%s", registry, encoding);
724 786
725 cs = codeset_from_name (charset); 787 cs = codeset_from_name (charset);
726 } 788 }
727 else 789 else
728 { 790 {
729 const char *charset = get_property (f, "FONT", 0); 791 const char *charset = get_property (f, XA_FONT, 0);
730 792
731 if (!charset) 793 if (!charset)
732 charset = name; 794 charset = name;
733 795
734 int count = 13; 796 int count = 13;
745 if (cs == CS_UNICODE) 807 if (cs == CS_UNICODE)
746 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 808 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
747 809
748 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 810 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
749 enc2b = encm || f->max_char_or_byte2 > 255; 811 enc2b = encm || f->max_char_or_byte2 > 255;
750
751 ascent = f->ascent;
752 descent = f->descent;
753 height = ascent + descent;
754 812
755 slow = false; 813 slow = false;
756 814
757#if 1 // only used for slow detection, TODO optimize 815#if 1 // only used for slow detection, TODO optimize
758 if (f->min_bounds.width == f->max_bounds.width) 816 if (f->min_bounds.width == f->max_bounds.width)
797 855
798 XCharStruct g; 856 XCharStruct g;
799 int dir_ret, asc_ret, des_ret; 857 int dir_ret, asc_ret, des_ret;
800 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 858 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
801 859
802 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 860 int wcw = WCWIDTH (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
803 861
804 if (width < g.width) width = g.width; 862 if (width < g.width) width = g.width;
805 } 863 }
806 864
807 if (cs == CS_UNKNOWN) 865 if (cs == CS_UNKNOWN)
826void 884void
827rxvt_font_x11::clear () 885rxvt_font_x11::clear ()
828{ 886{
829 if (f) 887 if (f)
830 { 888 {
831 XFreeFont (DISPLAY, f); 889 XFreeFont (term->xdisp, f);
832 f = 0; 890 f = 0;
833 } 891 }
834} 892}
835 893
836bool 894bool
837rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 895rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
838{ 896{
839 uint32_t ch = FROM_UNICODE (cs, unicode); 897 uint32_t ch = FROM_UNICODE (cs, unicode);
840 898
841 if (ch == NOCHAR) 899 if (ch == NOCHAR)
842 return false; 900 return false;
880 938
881 if (!prop || prop->width == rxvt_fontprop::unset) 939 if (!prop || prop->width == rxvt_fontprop::unset)
882 return true; 940 return true;
883 941
884 // check character against base font bounding box 942 // check character against base font bounding box
885 int w = xcs->width; 943 int w = xcs->rbearing - xcs->lbearing;
886 int wcw = wcwidth (unicode); 944 int wcw = WCWIDTH (unicode);
887 if (wcw > 0) w /= wcw; 945 if (wcw > 0) w = (w + wcw - 1) / wcw;
888 946
889 careful = w > prop->width; 947 careful = w > prop->width;
890 if (careful && w > prop->width * MAX_OVERLAP >> 2) 948 if (careful && OVERLAP_OK (w, prop))
891 return false; 949 return false;
892 950
893 return true; 951 return true;
894} 952}
895 953
901 // this looks like a mess /. 959 // this looks like a mess /.
902 // and it is a mess /. 960 // and it is a mess /.
903 // yet we are trying to be perfect /. 961 // yet we are trying to be perfect /.
904 // but the result still isn't perfect /. 962 // but the result still isn't perfect /.
905 963
964 dTermDisplay;
965 dTermGC;
966
906 bool slow = this->slow 967 bool slow = this->slow
907 || width != r->TermWin.fwidth 968 || width != term->fwidth
908 || height != r->TermWin.fheight; 969 || height != term->fheight;
909 970
910 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 971 int base = ascent; // sorry, incorrect: term->fbase;
911 972
912 XGCValues v; 973 XGCValues v;
913 v.foreground = r->pix_colors[fg]; 974 v.foreground = term->pix_colors[fg];
914 v.font = f->fid; 975 v.font = f->fid;
915 976
916 if (enc2b) 977 if (enc2b)
917 { 978 {
918 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 979 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
919 980
920 if (bg == Color_bg && !slow) 981 if (bg == Color_bg && !slow)
921 { 982 {
922 v.background = r->pix_colors[bg]; 983 v.background = term->pix_colors[bg];
923 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 984 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
924 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 985 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
925 } 986 }
926 else 987 else
927 { 988 {
928 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 989 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
929 990
930 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 991 XChangeGC (disp, gc, GCForeground | GCFont, &v);
931 992
932 if (slow) 993 if (slow)
933 { 994 {
934 do 995 do
935 { 996 {
936 if (xc->byte1 || xc->byte2) 997 if (xc->byte1 || xc->byte2)
937 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 998 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
938 999
939 x += r->TermWin.fwidth; 1000 x += term->fwidth;
940 xc++; len--; 1001 xc++; len--;
941 } 1002 }
942 while (len); 1003 while (len);
943 } 1004 }
944 else 1005 else
945 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len); 1006 XDrawString16 (disp, d, gc, x, y + base, xc, len);
946 } 1007 }
947 } 1008 }
948 else 1009 else
949 { 1010 {
950 const char *xc = enc_char (text, len, cs, slow); 1011 const char *xc = enc_char (text, len, cs, slow);
951 1012
952 if (bg == Color_bg && !slow) 1013 if (bg == Color_bg && !slow)
953 { 1014 {
954 v.background = r->pix_colors[bg]; 1015 v.background = term->pix_colors[bg];
955 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1016 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
956 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1017 XDrawImageString (disp, d, gc, x, y + base, xc, len);
957 } 1018 }
958 else 1019 else
959 { 1020 {
960 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1021 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
961 1022
962 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1023 XChangeGC (disp, gc, GCForeground | GCFont, &v);
963 1024
964 if (slow) 1025 if (slow)
965 { 1026 {
966 do 1027 do
967 { 1028 {
968 if (*xc) 1029 if (*xc)
969 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1030 XDrawString (disp, d, gc, x, y + base, xc, 1);
970 1031
971 x += r->TermWin.fwidth; 1032 x += term->fwidth;
972 xc++; len--; 1033 xc++; len--;
973 } 1034 }
974 while (len); 1035 while (len);
975 } 1036 }
976 else 1037 else
977 XDrawString (d.display->display, d, TGC, x, y + base, xc, len); 1038 XDrawString (disp, d, gc, x, y + base, xc, len);
978 } 1039 }
979 } 1040 }
980} 1041}
981 1042
982///////////////////////////////////////////////////////////////////////////// 1043/////////////////////////////////////////////////////////////////////////////
994 1055
995 void draw (rxvt_drawable &d, int x, int y, 1056 void draw (rxvt_drawable &d, int x, int y,
996 const text_t *text, int len, 1057 const text_t *text, int len,
997 int fg, int bg); 1058 int fg, int bg);
998 1059
999 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull); 1060 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull) const;
1000 1061
1001protected: 1062protected:
1002 XftFont *f; 1063 XftFont *f;
1003}; 1064};
1004 1065
1005void 1066void
1006rxvt_font_xft::clear () 1067rxvt_font_xft::clear ()
1007{ 1068{
1008 if (f) 1069 if (f)
1009 { 1070 {
1010 XftFontClose (DISPLAY, f); 1071 XftFontClose (term->xdisp, f);
1011 f = 0; 1072 f = 0;
1012 } 1073 }
1013} 1074}
1014 1075
1015rxvt_fontprop 1076rxvt_fontprop
1019 1080
1020 FT_Face face = XftLockFace (f); 1081 FT_Face face = XftLockFace (f);
1021 1082
1022 p.width = width; 1083 p.width = width;
1023 p.height = height; 1084 p.height = height;
1085 p.ascent = ascent;
1024 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1086 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1025 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1087 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1026 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1088 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1027 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1089 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1028 1090
1032} 1094}
1033 1095
1034bool 1096bool
1035rxvt_font_xft::load (const rxvt_fontprop &prop) 1097rxvt_font_xft::load (const rxvt_fontprop &prop)
1036{ 1098{
1099 dTermDisplay;
1100
1037 clear (); 1101 clear ();
1038 1102
1039 FcPattern *p = FcNameParse ((FcChar8 *) name); 1103 FcPattern *p = FcNameParse ((FcChar8 *) name);
1040 1104
1041 if (!p) 1105 if (!p)
1042 return false; 1106 return false;
1043 1107
1044 FcValue v; 1108 FcValue v;
1045 1109
1046 if (prop.height != rxvt_fontprop::unset 1110 if (prop.height != rxvt_fontprop::unset
1047 && FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1111 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1112 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1048 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1113 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1049 1114
1050 if (prop.weight != rxvt_fontprop::unset 1115 if (prop.weight != rxvt_fontprop::unset
1051 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1116 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1052 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1117 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1066 1131
1067 // store generated name so iso14755 view gives better results 1132 // store generated name so iso14755 view gives better results
1068 set_name ((char *)FcNameUnparse (p)); 1133 set_name ((char *)FcNameUnparse (p));
1069 1134
1070 XftResult result; 1135 XftResult result;
1071 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1136 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
1072 1137
1073 FcPatternDestroy (p); 1138 FcPatternDestroy (p);
1074 1139
1075 if (!match) 1140 if (!match)
1076 return false; 1141 return false;
1078 int ftheight = 0; 1143 int ftheight = 0;
1079 bool success = true; 1144 bool success = true;
1080 1145
1081 for (;;) 1146 for (;;)
1082 { 1147 {
1083 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1148 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1084 1149
1085 if (!f) 1150 if (!f)
1086 { 1151 {
1087 success = false; 1152 success = false;
1088 break; 1153 break;
1097 1162
1098 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1163 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1099 1164
1100 XftUnlockFace (f); 1165 XftUnlockFace (f);
1101 1166
1167 int glheight = height;
1168
1102 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1169 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1103 { 1170 {
1104 FcChar16 ch = *t; 1171 FcChar16 ch = *t;
1105 1172
1106 if (cs != CS_UNICODE 1173 if (cs != CS_UNICODE
1112 bool careful; 1179 bool careful;
1113 if (!has_char (*t, &prop, careful)) 1180 if (!has_char (*t, &prop, careful))
1114 continue; 1181 continue;
1115 1182
1116 XGlyphInfo g; 1183 XGlyphInfo g;
1117 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1184 XftTextExtents16 (disp, f, &ch, 1, &g);
1118 1185
1186 g.width -= g.x;
1187
1119 int wcw = wcwidth (ch); 1188 int wcw = WCWIDTH (ch);
1120 if (wcw > 0) g.width = g.width / wcw; 1189 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1121 1190
1122 if (width < g.width) width = g.width; 1191 if (width < g.width ) width = g.width;
1123 if (height < g.height) height = g.height; 1192 if (height < g.height ) height = g.height;
1193 if (glheight < g.height - g.y) glheight = g.height - g.y;
1194 }
1195
1196 if (!width)
1197 {
1198 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1199
1200 XftFontClose (disp, f);
1201 f = 0;
1202
1203 success = false;
1204 break;
1124 } 1205 }
1125 1206
1126 if (prop.height == rxvt_fontprop::unset 1207 if (prop.height == rxvt_fontprop::unset
1127 || height <= prop.height 1208 || (height <= prop.height && glheight <= prop.height)
1128 || height <= 2 1209 || height <= 2
1129 || !scalable) 1210 || !scalable)
1130 break; 1211 break;
1131 1212
1132 if (ftheight) 1213 if (ftheight)
1139 ftheight -= height - prop.height; 1220 ftheight -= height - prop.height;
1140 } 1221 }
1141 else 1222 else
1142 ftheight = prop.height - 1; 1223 ftheight = prop.height - 1;
1143 1224
1144 XftFontClose (DISPLAY, f); 1225 XftFontClose (disp, f);
1145 FcPatternDel (match, FC_PIXEL_SIZE); 1226 FcPatternDel (match, FC_PIXEL_SIZE);
1146 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1227 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1147 } 1228 }
1148 1229
1149 FcPatternDestroy (match); 1230 FcPatternDestroy (match);
1150 1231
1151#if 0 // do it per-character 1232#if 0 // do it per-character
1158 1239
1159 return success; 1240 return success;
1160} 1241}
1161 1242
1162bool 1243bool
1163rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 1244rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1164{ 1245{
1165 careful = false; 1246 careful = false;
1166 1247
1167 if (!XftCharExists (DISPLAY, f, unicode)) 1248 if (!XftCharExists (term->xdisp, f, unicode))
1168 return false; 1249 return false;
1169 1250
1170 if (!prop || prop->width == rxvt_fontprop::unset) 1251 if (!prop || prop->width == rxvt_fontprop::unset)
1171 return true; 1252 return true;
1172 1253
1173 // check character against base font bounding box 1254 // check character against base font bounding box
1174 FcChar32 ch = unicode; 1255 FcChar32 ch = unicode;
1175 XGlyphInfo g; 1256 XGlyphInfo g;
1176 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1257 XftTextExtents32 (term->xdisp, f, &ch, 1, &g);
1177 1258
1178 int w = g.width; 1259 int w = g.width - g.x;
1179 int wcw = wcwidth (unicode); 1260 int wcw = WCWIDTH (unicode);
1180 if (wcw > 0) w /= wcw; 1261 if (wcw > 0) w = (w + wcw - 1) / wcw;
1181 1262
1182 careful = w > prop->width; 1263 careful = w > prop->width;
1183 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1264 if (careful && OVERLAP_OK (w, prop))
1184 return false; 1265 return false;
1185 1266
1186 return true; 1267 return true;
1187} 1268}
1188 1269
1189void 1270void
1190rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1271rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1191 const text_t *text, int len, 1272 const text_t *text, int len,
1192 int fg, int bg) 1273 int fg, int bg)
1193{ 1274{
1194 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1275 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1195
1196 int base = ascent; // should be fbase, but that is incorrect
1197 1276
1198 XGlyphInfo extents; 1277 XGlyphInfo extents;
1199 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1278 XftGlyphSpec *enc = (XftGlyphSpec *)rxvt_temp_buf (len * sizeof (XftGlyphSpec));
1200 FcChar32 *ep = enc; 1279 XftGlyphSpec *ep = enc;
1201 int ewidth = 0; 1280
1202 int xoff = 0; 1281 dTermDisplay;
1282 dTermGC;
1283
1284 // cut trailing spaces
1285 while (len && text [len - 1] == ' ')
1286 len--;
1203 1287
1204 while (len) 1288 while (len)
1205 { 1289 {
1206 int cwidth = r->TermWin.fwidth; 1290 int cwidth = term->fwidth;
1207 FcChar32 fc = *text++; len--; 1291 FcChar32 fc = *text++; len--;
1208 FT_UInt gl;
1209 1292
1210 while (len && *text == NOCHAR) 1293 while (len && *text == NOCHAR)
1211 text++, len--, cwidth += r->TermWin.fwidth; 1294 text++, len--, cwidth += term->fwidth;
1212 1295
1213 gl = XftCharIndex (d.display->display, f, fc); 1296 if (fc != ' ') // skip spaces
1214 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1215
1216 if (extents.xOff != cwidth && ep != enc)
1217 {
1218 if (xoff > ewidth) xoff = ewidth;
1219 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1220 x + (ewidth - xoff >> 1),
1221 y + base, enc, ep - enc);
1222 x += ewidth;
1223
1224 ep = enc;
1225 ewidth = 0;
1226 xoff = 0;
1227 } 1297 {
1298 FT_UInt glyph = XftCharIndex (disp, f, fc);
1299 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1228 1300
1229 if (fc == ' ' && ep == enc) // skip leading spaces 1301 ep->glyph = glyph;
1302 ep->x = x + (cwidth - extents.xOff >> 1);
1303 ep->y = y + ascent;
1304 ep++;
1230 { 1305 }
1306
1231 x += cwidth; 1307 x += cwidth;
1232 continue;
1233 }
1234
1235 else
1236 {
1237 *ep++ = gl;
1238 ewidth += cwidth;
1239 xoff += extents.xOff;
1240 }
1241 } 1308 }
1242 1309
1243 if (ep != enc) 1310 if (ep != enc)
1244 {
1245 if (xoff > ewidth) xoff = ewidth;
1246 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1311 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1247 x + (ewidth - xoff >> 1),
1248 y + base, enc, ep - enc);
1249 }
1250} 1312}
1251#endif 1313#endif
1252 1314
1253///////////////////////////////////////////////////////////////////////////// 1315/////////////////////////////////////////////////////////////////////////////
1254 1316
1255rxvt_fontset::rxvt_fontset (rxvt_t r) 1317rxvt_fontset::rxvt_fontset (rxvt_term *term)
1256: r (r), fontdesc (0) 1318: fontdesc (0), term (term)
1257{ 1319{
1258 clear (); 1320 clear ();
1259} 1321}
1260 1322
1261rxvt_fontset::~rxvt_fontset () 1323rxvt_fontset::~rxvt_fontset ()
1264} 1326}
1265 1327
1266void 1328void
1267rxvt_fontset::clear () 1329rxvt_fontset::clear ()
1268{ 1330{
1331 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1332 = rxvt_fontprop::unset;
1333
1269 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1334 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1270 FONT_UNREF (*i); 1335 FONT_UNREF (*i);
1271 1336
1272 for (pagemap **p = fmap.begin (); p != fmap.end (); p++) 1337 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1273 delete *p; 1338 delete *p;
1302 f = new rxvt_font_x11; 1367 f = new rxvt_font_x11;
1303 } 1368 }
1304 else 1369 else
1305 f = new rxvt_font_x11; 1370 f = new rxvt_font_x11;
1306 1371
1307 f->set_term (r); 1372 f->set_term (term);
1308 f->set_name (strdup (name)); 1373 f->set_name (strdup (name));
1309 1374
1310 f->cs = cs; 1375 f->cs = cs;
1311 f->loaded = false; 1376 f->loaded = false;
1312 1377
1323 char buf[512]; 1388 char buf[512];
1324 const char *end; 1389 const char *end;
1325 1390
1326 do 1391 do
1327 { 1392 {
1328 while (*desc <= ' ') desc++; 1393 while (*desc && *desc <= ' ')
1394 desc++;
1329 1395
1330 codeset cs = CS_UNICODE; 1396 codeset cs = CS_UNICODE;
1331 1397
1332 if (*desc == '[') 1398 if (*desc == '[')
1333 { 1399 {
1393 1459
1394 return true; 1460 return true;
1395} 1461}
1396 1462
1397bool 1463bool
1398rxvt_fontset::populate (const char *desc, const rxvt_fontprop &prop) 1464rxvt_fontset::populate (const char *desc)
1399{ 1465{
1400 clear (); 1466 clear ();
1401 1467
1402 fontdesc = strdup (desc); 1468 fontdesc = strdup (desc);
1403 1469
1404 fonts.push_back (new_font (0, CS_UNICODE)); 1470 fonts.push_back (new_font (0, CS_UNICODE));
1405 realize_font (0); 1471 realize_font (0);
1406
1407 this->prop = prop;
1408 1472
1409 add_fonts (desc); 1473 add_fonts (desc);
1410 1474
1411 return true; 1475 return true;
1412} 1476}
1445 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1509 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1446 goto next_font; 1510 goto next_font;
1447 1511
1448 if (!realize_font (i)) 1512 if (!realize_font (i))
1449 goto next_font; 1513 goto next_font;
1514
1515 if (prop.ascent != rxvt_fontprop::unset)
1516 max_it (f->ascent, prop.ascent);
1450 } 1517 }
1451 1518
1452 if (f->cs == CS_UNKNOWN) 1519 if (f->cs == CS_UNKNOWN)
1453 goto next_font; 1520 goto next_font;
1454 1521
1493 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1560 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1494 FcPatternAddBool (p, FC_MINSPACE, 1); 1561 FcPatternAddBool (p, FC_MINSPACE, 1);
1495 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1562 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1496 1563
1497 XftResult result; 1564 XftResult result;
1498 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1565 FcPattern *match = XftFontMatch (term->xdisp, term->display->screen, p, &result);
1499 1566
1500 FcPatternDestroy (p); 1567 FcPatternDestroy (p);
1501 1568
1502 if (match) 1569 if (match)
1503 { 1570 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines