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

Comparing rxvt-unicode/src/defaultfont.C (file contents):
Revision 1.8 by pcg, Thu Dec 18 04:27:36 2003 UTC vs.
Revision 1.27 by pcg, Wed Mar 3 00:20:33 2004 UTC

1/*--------------------------------*-C-*---------------------------------*; 1/*--------------------------------*-C-*---------------------------------*;
2 * File: defaultfont.C 2 * File: defaultfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003 Marc Lehmann rxvt@plan9.de> 4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 * - original version. 5 * - original version.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
23#include "rxvt.h" 23#include "rxvt.h"
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib> 26#include <cstdlib>
27 27
28#define DISPLAY r->Xdisplay 28#define DISPLAY r->display->display
29#define DRAWABLE r->TermWin.vt
30#define GC r->TermWin.gc 29#define GC r->TermWin.gc
31 30
32const struct rxvt_fallback_font { 31const struct rxvt_fallback_font {
33 codeset cs; 32 codeset cs;
34 const char *name; 33 const char *name;
59 58
60 // japanese 59 // japanese
61#if ENCODING_JP || ENCODING_JP_EXT 60#if ENCODING_JP || ENCODING_JP_EXT
62# if XFT 61# if XFT
63 // prefer xft for complex scripts 62 // prefer xft for complex scripts
64 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, 63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
65# endif 64# endif
66 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
67 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 66 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
68 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
69#endif 68#endif
70 69
71#if ENCODING_CN || ENCODING_CN_EXT 70#if ENCODING_CN || ENCODING_CN_EXT
72# if XFT 71# if XFT
73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 72 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 73 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 74 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 75 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
77# endif 76# endif
77 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5*-0" },
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
78 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 79 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
79 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 80 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
80 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 81 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
81 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 82 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
82 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 83 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
83 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 84 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
84 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 85 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
85 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 86 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
86#endif 87#endif
87 88
88#if XFT 89#if XFT
89 { CS_UNICODE, "xft:Andale Mono" }, 90 { CS_UNICODE, "xft:Andale Mono" },
90 { CS_UNICODE, "xft:Arial Unicode MS" }, 91 { CS_UNICODE, "xft:Arial Unicode MS" },
91#endif 92#endif
92 { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 93 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
94 { CS_UNICODE, "xft:FreeMono" },
95 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
96 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 97 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
94 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
95 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
96 98
97 { CS_UNKNOWN, 0 } 99 { CS_UNKNOWN, 0 }
98}; 100};
99 101
100///////////////////////////////////////////////////////////////////////////// 102/////////////////////////////////////////////////////////////////////////////
101 103
104#if XFT
105rxvt_drawable::~rxvt_drawable ()
106{
107 if (xftdrawable)
108 XftDrawDestroy (xftdrawable);
109}
110
111rxvt_drawable::operator XftDraw *()
112{
113 if (!xftdrawable)
114 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
115
116 return xftdrawable;
117}
118#endif
119
120/////////////////////////////////////////////////////////////////////////////
121
102static void *enc_buf; 122static void *enc_buf;
103static uint32_t enc_len; 123static uint32_t enc_len;
104 124
105static inline void * 125static inline void *
106get_enc_buf (int len) 126get_enc_buf (uint32_t len)
107{ 127{
108 if (len > enc_len) 128 if (len > enc_len)
109 { 129 {
110 free (enc_buf); 130 free (enc_buf);
111 enc_buf = malloc (len); 131 enc_buf = malloc (len);
113 133
114 return enc_buf; 134 return enc_buf;
115} 135}
116 136
117static const char * 137static const char *
118enc_char (const text_t *text, int len, codeset cs, bool &zero) 138enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
119{ 139{
120 uint8_t *buf = (uint8_t *)get_enc_buf (len); 140 uint8_t *buf = (uint8_t *)get_enc_buf (len);
121 141
122 while (len--) 142 while (len--)
123 { 143 {
134 154
135 return (const char *)enc_buf; 155 return (const char *)enc_buf;
136} 156}
137 157
138static const XChar2b * 158static const XChar2b *
139enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 159enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
140{ 160{
141 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 161 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
142 162
143 while (len--) 163 while (len--)
144 { 164 {
159} 179}
160 180
161///////////////////////////////////////////////////////////////////////////// 181/////////////////////////////////////////////////////////////////////////////
162 182
163void 183void
164rxvt_font::clear_rect (int x, int y, int w, int h, int color) 184rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
165{ 185{
166 if (color == Color_bg) 186 if (color == Color_bg)
167 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 187 XClearArea (d.display->display, d, x, y, w, h, FALSE);
168 else if (color >= 0) 188 else if (color >= 0)
169 { 189 {
190#if XFT
191 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
192#else
170 XSetForeground (DISPLAY, GC, r->PixColors[color]); 193 XSetForeground (d.display->display, GC, r->PixColors[color]);
171 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 194 XFillRectangle (d.display->display, d, GC, x, y, w, h);
195#endif
172 } 196 }
173} 197}
174 198
175static const char *linedraw_cmds[128] = { 199static const char *linedraw_cmds[128] = {
176 "1hH", "2hH", "1vV", "2vV", 200 "1hH", "2hH", "1vV", "2vV",
226struct rxvt_font_default : rxvt_font { 250struct rxvt_font_default : rxvt_font {
227 rxvt_fontprop properties () 251 rxvt_fontprop properties ()
228 { 252 {
229 rxvt_fontprop p; 253 rxvt_fontprop p;
230 254
231 p.height = 1; 255 p.width = p.height = 1;
232 p.weight = rxvt_fontprop::medium; 256 p.weight = rxvt_fontprop::medium;
233 p.slant = rxvt_fontprop::roman; 257 p.slant = rxvt_fontprop::roman;
234 258
235 return p; 259 return p;
236 } 260 }
261 } 285 }
262 286
263 return false; 287 return false;
264 } 288 }
265 289
266 void draw (int x, int y, 290 void draw (rxvt_drawable &d, int x, int y,
267 const text_t *text, int len, 291 const text_t *text, int len,
268 int fg, int bg); 292 int fg, int bg);
269}; 293};
270 294
271void 295void
272rxvt_font_default::draw (int x, int y, 296rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
273 const text_t *text, int len, 297 const text_t *text, int len,
274 int fg, int bg) 298 int fg, int bg)
275{ 299{
276 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 300 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
277 301
278 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 302 XSetForeground (d.display->display, GC, r->PixColors[fg]);
279 303
280 while (len--) 304 while (len--)
281 { 305 {
282 text_t t = *text++; 306 text_t t = *text++;
283 307
308 // is it in our linedrawing table?
284 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 309 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
285 { 310 {
286 const char *p = linedraw_cmds[t - 0x2500]; 311 const char *p = linedraw_cmds[t - 0x2500];
287 312
288 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; 313 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth - 1;
289 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight; 314 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight - 1;
290 315
291 XGCValues gcv; 316 XGCValues gcv;
292 317
293 while (*p) 318 while (*p)
294 { 319 {
295 switch (*p++) 320 switch (*p++)
296 { 321 {
297 case '1': 322 case '1':
298 gcv.line_width = 0; 323 gcv.line_width = 0;
299 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 324 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
300 break; 325 break;
301 326
302 case '2': 327 case '2':
303 gcv.line_width = 2; 328 gcv.line_width = 2;
304 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 329 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
305 break; 330 break;
306 331
307 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; 332 case 'h': XDrawLine (d.display->display, d, GC, x0, y1, x1, y1); break;
308 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break; 333 case 'H': XDrawLine (d.display->display, d, GC, x1, y1, x2, y1); break;
309 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break; 334 case 'v': XDrawLine (d.display->display, d, GC, x1, y0, x1, y1); break;
310 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break; 335 case 'V': XDrawLine (d.display->display, d, GC, x1, y1, x1, y2); break;
311 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break; 336 case 'a': XDrawLine (d.display->display, d, GC, x0, y2, x2, y0); break;
312 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break; 337 case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
313 } 338 }
314 } 339 }
315 340
316 gcv.line_width = 0; 341 gcv.line_width = 0;
317 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 342 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
318 } 343 }
319 else 344 else
320 switch (*text++) 345 switch (t)
321 { 346 {
322 case NOCHAR:
323 case ZERO_WIDTH_CHAR: 347 case ZERO_WIDTH_CHAR:
324 break; 348 break;
325 default: 349 default:
326 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 350 int w = 0;
351 while (len > 0 && *text == NOCHAR)
352 {
353 ++text;
354 --len;
355 w += r->TermWin.fwidth;
356 }
357
358 XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
359 w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
360 x += w;
327 } 361 }
328 362
329 x += r->TermWin.fwidth; 363 x += r->TermWin.fwidth;
330 } 364 }
331} 365}
336 rxvt_font_x11 () { f = 0; } 370 rxvt_font_x11 () { f = 0; }
337 371
338 void clear (); 372 void clear ();
339 373
340 rxvt_fontprop properties (); 374 rxvt_fontprop properties ();
341 rxvt_fontprop properties (XFontStruct *f);
342 375
343 bool load (const rxvt_fontprop &prop); 376 bool load (const rxvt_fontprop &prop);
344 377
345 bool has_codepoint (uint32_t unicode); 378 bool has_codepoint (uint32_t unicode);
346 379
347 void draw (int x, int y, 380 void draw (rxvt_drawable &d, int x, int y,
348 const text_t *text, int len, 381 const text_t *text, int len,
349 int fg, int bg); 382 int fg, int bg);
350 383
351 XFontStruct *f; 384 XFontStruct *f;
352 codeset cs; 385 codeset cs;
353 bool enc2b, encm; 386 bool enc2b, encm;
354 387
355 const char *get_property (XFontStruct *f, const char *property, const char *repl) const; 388 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
389 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
390 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
391 bool set_properties (rxvt_fontprop &p, const char *name);
356}; 392};
357 393
358const char * 394const char *
359rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 395rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
360{ 396{
367} 403}
368 404
369rxvt_fontprop 405rxvt_fontprop
370rxvt_font_x11::properties () 406rxvt_font_x11::properties ()
371{ 407{
372 return properties (f);
373}
374
375rxvt_fontprop
376rxvt_font_x11::properties (XFontStruct *f)
377{
378 rxvt_fontprop p; 408 rxvt_fontprop p;
409 set_properties (p, f);
410 return p;
411}
379 412
413bool
414rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
415{
416 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
417 p.height = height;
418 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
419 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
420
421 return true;
422}
423
424bool
425rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
426{
380 const char *weight = get_property (f, "WEIGHT_NAME", "medium"); 427 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
381 const char *slant = get_property (f, "SLANT", "r"); 428 const char *slant = get_property (f, "SLANT", "r");
382 429
383 p.height = height; 430 unsigned long height;
384 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 431 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
385 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 432 return false;
386 433
387 return p; 434 unsigned long avgwidth;
435 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
436 avgwidth = 0;
437
438 return set_properties (p, height, weight, slant, avgwidth);
439}
440
441bool
442rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
443{
444 int slashes = 0;
445 const char *comp[12];
446
447 for (const char *c = name; *c; c++)
448 if (*c == '-')
449 {
450 comp[slashes++] = c + 1;
451 if (slashes >= 13)
452 break;
453 }
454
455 /* can we short-circuit the costly XLoadQueryFont? */
456 if (slashes >= 13
457 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
458 && (*comp[11] >= '0' && *comp[11] <= '9'))
459 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
460
461 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
462
463 if (f)
464 {
465 // the font should really exist now. if not, we have a problem
466 // (e.g. if the user did xset fp rehash just when we were searching fonts).
467 // in that case, just return garbage.
468 bool ret = set_properties (p, f);
469 XFreeFont (DISPLAY, f);
470 return ret;
471 }
472 else
473 return false;
474}
475
476// fix the size of scalable fonts
477static void
478fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
479{
480 int slashes = 0;
481 const char *size;
482
483 for (const char *c = name; *c; c++)
484 if (*c == '-')
485 {
486 if (slashes == 6)
487 size = c + 1;
488
489 if (++slashes >= 13)
490 break;
491 }
492
493 if (slashes >= 13 && size[0] == '0')
494 {
495 strncpy (buf, name, size - name);
496 buf += size - name;
497 buf += sprintf (buf, "%d", prop.height);
498 strcpy (buf, size + 1);
499 }
500 else
501 strcpy (buf, name);
388} 502}
389 503
390bool 504bool
391rxvt_font_x11::load (const rxvt_fontprop &prop) 505rxvt_font_x11::load (const rxvt_fontprop &prop)
392{ 506{
393 clear (); 507 clear ();
394 508
395 char **list; 509 char **list;
396 int count; 510 int count;
397 XFontStruct *info;
398 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 511 list = XListFonts (DISPLAY, name, 512, &count);
512 set_name (0);
399 513
400 if (!list) 514 if (!list)
401 return false; 515 return false;
402 516
403 int bestdiff = 0x7fffffff; 517 int bestdiff = 0x7fffffff;
404 XFontStruct *best = 0;
405 for (int i = 0; i < count; i++) 518 for (int i = 0; i < count; i++)
406 { 519 {
407 XFontStruct *f = info + i; 520 rxvt_fontprop p;
521 char fname[1024];
522 fix_scalable (fname, list[i], prop);
408 523
524 if (!set_properties (p, fname))
525 continue;
526
409 if (f->ascent + f->descent <= prop.height) // weed out too large fonts 527 if (p.height > prop.height) // weed out too large fonts
410 { 528 continue;
411 rxvt_fontprop p = properties (f); 529
412 int diff = (prop.height - f->ascent + f->descent) * 32 530 int diff = (prop.height - p.height) * 32
413 + abs (prop.weight - p.weight) 531 + abs (prop.weight - p.weight)
414 + abs (prop.slant - p.slant ); 532 + abs (prop.slant - p.slant );
415 533
416 if (!best // compare against best found so far 534 if (!name // compare against best found so far
417 || diff < bestdiff) 535 || diff < bestdiff)
418 { 536 {
419 best = f; 537 set_name (strdup (fname));
420 bestdiff = diff; 538 bestdiff = diff;
421 } 539 }
422 }
423 } 540 }
424 541
425 set_name (strdup (list[best - info])); 542 XFreeFontNames (list);
426 543
427 XFreeFontInfo (list, info, count); 544 if (!name)
545 return false;
428 546
429 f = XLoadQueryFont (DISPLAY, name); 547 f = XLoadQueryFont (DISPLAY, name);
430 548
431 if (!f) 549 if (!f)
432 return false; 550 return false;
433
434 unsigned long value;
435 551
436 const char *registry = get_property (f, "CHARSET_REGISTRY", 0); 552 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
437 const char *encoding = get_property (f, "CHARSET_ENCODING", 0); 553 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
438 554
439 if (registry && encoding) 555 if (registry && encoding)
526 /* check wether the character exists in _this_ font. horrible. */ 642 /* check wether the character exists in _this_ font. horrible. */
527 XCharStruct *xcs; 643 XCharStruct *xcs;
528 644
529 if (encm) 645 if (encm)
530 { 646 {
531 int byte1 = ch >> 8; 647 unsigned char byte1 = ch >> 8;
532 int byte2 = ch & 255; 648 unsigned char byte2 = ch & 255;
533 649
534 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 650 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
535 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 651 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
536 return false; 652 return false;
537 653
560 676
561 return true; 677 return true;
562} 678}
563 679
564void 680void
565rxvt_font_x11::draw (int x, int y, 681rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
566 const text_t *text, int len, 682 const text_t *text, int len,
567 int fg, int bg) 683 int fg, int bg)
568{ 684{
569 // this looks like a mess /. 685 // this looks like a mess /.
570 // and it is a mess /. 686 // and it is a mess /.
586 { 702 {
587 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 703 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
588 704
589 if (bg == Color_bg && !slow) 705 if (bg == Color_bg && !slow)
590 { 706 {
591 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 707 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
592 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 708 XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
593 } 709 }
594 else 710 else
595 { 711 {
596 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 712 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
597 713
598 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 714 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
599 715
600 if (slow) 716 if (slow)
601 { 717 {
602 do 718 do
603 { 719 {
604 if (xc->byte1 || xc->byte2) 720 if (xc->byte1 || xc->byte2)
605 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 721 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
606 722
607 x += r->TermWin.fwidth; 723 x += r->TermWin.fwidth;
608 xc++; len--; 724 xc++; len--;
609 } 725 }
610 while (len); 726 while (len);
611 } 727 }
612 else 728 else
613 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 729 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
614 } 730 }
615 } 731 }
616 else 732 else
617 { 733 {
618 const char *xc = enc_char (text, len, cs, slow); 734 const char *xc = enc_char (text, len, cs, slow);
619 735
620 if (bg == Color_bg && !slow) 736 if (bg == Color_bg && !slow)
621 { 737 {
622 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 738 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
623 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 739 XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
624 } 740 }
625 else 741 else
626 { 742 {
627 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 743 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
628 744
629 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 745 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
630 746
631 if (slow) 747 if (slow)
632 { 748 {
633 do 749 do
634 { 750 {
635 if (*xc) 751 if (*xc)
636 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 752 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
637 753
638 x += r->TermWin.fwidth; 754 x += r->TermWin.fwidth;
639 xc++; len--; 755 xc++; len--;
640 } 756 }
641 while (len); 757 while (len);
642 } 758 }
643 else 759 else
644 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 760 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
645 } 761 }
646 } 762 }
647} 763}
648 764
649///////////////////////////////////////////////////////////////////////////// 765/////////////////////////////////////////////////////////////////////////////
654//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 770//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
655#define SWATHBITS 8 771#define SWATHBITS 8
656#endif 772#endif
657 773
658struct rxvt_font_xft : rxvt_font { 774struct rxvt_font_xft : rxvt_font {
659#if 0
660 enum {
661 SWATHCOUNT = 1 << (21 - UNIBITS),
662 SWATHSIZE = 1 << (SWATHBITS - 5)
663 };
664 typedef uint32_t swath[SWATHSIZE];
665
666 swath *cvr[SWATHCOUNT];
667#endif
668
669#if 0
670 void gen_coverage_swath (unsigned int page);
671
672 bool has_char (uint32_t ch)
673 {
674 unsigned int page = ch >> SWATHBITS;
675 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
676
677 if (page >= SWATHCOUNT)
678 return false;
679
680 if (!cvr[page]) gen_coverage_swath (page);
681
682 return cvr[page][idx >> 5] & (1 << (idx & 31));
683 }
684#endif
685 rxvt_font_xft () { f = 0; d = 0; } 775 rxvt_font_xft () { f = 0; }
686 776
687 void clear (); 777 void clear ();
688 778
689 rxvt_fontprop properties (); 779 rxvt_fontprop properties ();
690 780
691 bool load (const rxvt_fontprop &prop); 781 bool load (const rxvt_fontprop &prop);
692 782
693 void draw (int x, int y, 783 void draw (rxvt_drawable &d, int x, int y,
694 const text_t *text, int len, 784 const text_t *text, int len,
695 int fg, int bg); 785 int fg, int bg);
696 786
697 bool has_codepoint (uint32_t unicode); 787 bool has_codepoint (uint32_t unicode);
698 788
699protected: 789protected:
700 XftFont *f; 790 XftFont *f;
701 XftDraw *d;
702
703#if 0
704 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
705 void set_swath (uint32_t ch)
706 {
707 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
708 }
709#endif
710}; 791};
711 792
712void 793void
713rxvt_font_xft::clear () 794rxvt_font_xft::clear ()
714{ 795{
715 if (f) 796 if (f)
716 { 797 {
717 XftFontClose (DISPLAY, f); 798 XftFontClose (DISPLAY, f);
718 f = 0; 799 f = 0;
719 } 800 }
720
721 if (d)
722 {
723 XftDrawDestroy (d);
724 d = 0;
725 }
726
727#if 0
728 for (int i = 0; i < SWATHCOUNT; i++)
729 delete cvr[i];
730#endif
731} 801}
732 802
733rxvt_fontprop 803rxvt_fontprop
734rxvt_font_xft::properties () 804rxvt_font_xft::properties ()
735{ 805{
736 rxvt_fontprop p; 806 rxvt_fontprop p;
737 807
738 FT_Face face = XftLockFace (f); 808 FT_Face face = XftLockFace (f);
739 809
740 p.height = height; 810 p.width = width; p.height = height;
741 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; 811 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
742 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman; 812 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
743 813
744 XftUnlockFace (f); 814 XftUnlockFace (f);
745 815
767 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 837 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
768 838
769 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 839 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
770 FcPatternAddInteger (p, FC_SLANT, prop.slant); 840 FcPatternAddInteger (p, FC_SLANT, prop.slant);
771 841
842#if 0 // clipping unfortunately destroys our precious double-width-characters
843 // clip width, we can't do better, or can we?
844 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
845 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
846#endif
847
772 //FcPatternAddBool (p, FC_MINSPACE, 1); 848 //FcPatternAddBool (p, FC_MINSPACE, 1);
773 849
774 XftResult result; 850 XftResult result;
775 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 851 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
776 852
828 XftUnlockFace (f); 904 XftUnlockFace (f);
829 905
830 return true; 906 return true;
831} 907}
832 908
833#if 0
834void rxvt_font::gen_coverage_swath (unsigned int page)
835{
836 cvr[page] = new swath;
837
838 for (int i = 0; i < SWATHSIZE; i++)
839 cvr[page][i] = 0;
840
841 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
842}
843#endif
844
845bool 909bool
846rxvt_font_xft::has_codepoint (uint32_t unicode) 910rxvt_font_xft::has_codepoint (uint32_t unicode)
847{ 911{
848 return XftCharExists (DISPLAY, f, unicode); 912 return XftCharExists (DISPLAY, f, unicode);
849} 913}
850 914
851void 915void
852rxvt_font_xft::draw (int x, int y, 916rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
853 const text_t *text, int len, 917 const text_t *text, int len,
854 int fg, int bg) 918 int fg, int bg)
855{ 919{
856 if (!d)
857 {
858 dR;
859 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
860 }
861
862 if (bg >= 0 && bg != Color_bg)
863 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
864 else
865 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 920 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
866 921
867 if (!slow && width == r->TermWin.fwidth) 922 if (!slow && width == r->TermWin.fwidth && 0)
868 { 923 {
869 if (sizeof (text_t) == sizeof (FcChar16)) 924 if (sizeof (text_t) == sizeof (FcChar16))
870 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 925 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
871 else 926 else
872 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 927 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
875 { 930 {
876 while (len) 931 while (len)
877 { 932 {
878 if (*text != NOCHAR && *text != ' ') 933 if (*text != NOCHAR && *text != ' ')
879 { 934 {
935 int fwidth = r->TermWin.fwidth;
936 if (len >= 2 && text[1] == NOCHAR)
937 fwidth *= 2;
938
939 XGlyphInfo extents;
880 if (sizeof (text_t) == sizeof (FcChar16)) 940 if (sizeof (text_t) == sizeof (FcChar16))
881 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 941 {
942 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
943 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
944 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
945 }
882 else 946 else
883 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 947 {
948 XGlyphInfo extents;
949 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
950 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
951 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
952 }
884 } 953 }
885 954
886 x += r->TermWin.fwidth; 955 x += r->TermWin.fwidth;
887 text++; 956 text++;
888 len--; 957 len--;
892#endif 961#endif
893 962
894///////////////////////////////////////////////////////////////////////////// 963/////////////////////////////////////////////////////////////////////////////
895 964
896rxvt_fontset::rxvt_fontset (rxvt_t r) 965rxvt_fontset::rxvt_fontset (rxvt_t r)
897#ifdef EXPLICIT_CONTEXT
898: r(r) 966: r (r)
899#endif
900{ 967{
901 clear (); 968 clear ();
902} 969}
903 970
904rxvt_fontset::~rxvt_fontset () 971rxvt_fontset::~rxvt_fontset ()
907} 974}
908 975
909void 976void
910rxvt_fontset::clear () 977rxvt_fontset::clear ()
911{ 978{
912 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 979 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
913 FONT_UNREF (*i); 980 FONT_UNREF (*i);
914 981
915 fonts.clear (); 982 fonts.clear ();
916 base_id = 0; 983 base_id = 0;
917 base_prop.height = 0x7fffffff; 984 base_prop.height = 0x7fffffff;
971 1038
972 if (*desc == '[') 1039 if (*desc == '[')
973 { 1040 {
974 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1041 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
975 1042
976 const char *extra = desc++; 1043 //const char *extra = desc++; // not yet used
977 1044
978 desc = strchr (desc, ']'); 1045 desc = strchr (desc, ']');
979 1046
980 if (!desc) 1047 if (!desc)
981 { 1048 {
1020 } 1087 }
1021 1088
1022 return true; 1089 return true;
1023} 1090}
1024 1091
1025void 1092bool
1026rxvt_fontset::populate (const char *desc) 1093rxvt_fontset::populate (const char *desc)
1027{ 1094{
1028 clear (); 1095 clear ();
1029 1096
1030 fonts.push_back (new_font (0, CS_UNICODE)); 1097 fonts.push_back (new_font (0, CS_UNICODE));
1034 1101
1035 if (!base_id) 1102 if (!base_id)
1036 base_id = 1; 1103 base_id = 1;
1037 1104
1038 // we currently need a base-font, no matter what 1105 // we currently need a base-font, no matter what
1039 if (fonts.size () <= base_id || !realize_font (base_id)) 1106 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1040 { 1107 {
1108 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1041 add_fonts ("fixed"); 1109 add_fonts ("fixed");
1042 base_id = fonts.size () - 1; 1110 base_id = fonts.size () - 1;
1043 } 1111 }
1044 1112
1045 if (fonts.size () <= base_id || !realize_font (base_id)) 1113 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1046 { 1114 return false;
1047 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1048 exit (1);
1049 }
1050 1115
1051 base_prop = fonts[base_id]->properties (); 1116 base_prop = fonts[base_id]->properties ();
1117
1118 return true;
1052} 1119}
1053 1120
1054int 1121int
1055rxvt_fontset::find_font (uint32_t unicode) 1122rxvt_fontset::find_font (uint32_t unicode)
1056{ 1123{
1057 for (int i = 0; i < fonts.size (); i++) 1124 for (unsigned int i = 0; i < fonts.size (); i++)
1058 { 1125 {
1059 rxvt_font *f = fonts[i]; 1126 rxvt_font *f = fonts[i];
1060 1127
1061 if (!f->loaded) 1128 if (!f->loaded)
1062 { 1129 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines