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.10 by pcg, Wed Dec 24 09:07:01 2003 UTC vs.
Revision 1.38 by pcg, Fri Mar 5 21:26:01 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;
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, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
78 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
79 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
80 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 81 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
79 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 82 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
80 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 83 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
81 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 84 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
82 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 85 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
83 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 86 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
84 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 87 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
85 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 88 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
86#endif 89#endif
87 90
88#if XFT 91#if XFT
89 { CS_UNICODE, "xft:Andale Mono" }, 92 { CS_UNICODE, "xft:Andale Mono" },
90 { CS_UNICODE, "xft:Arial Unicode MS" }, 93 { CS_UNICODE, "xft:Arial Unicode MS" },
93 { CS_UNICODE, "xft:FreeMono" }, 96 { CS_UNICODE, "xft:FreeMono" },
94 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 97 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
95 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 98 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
96 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 99 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
97 100
101#if UNICODE_3 && XFT
102 { CS_UNICODE, "xft:Code2001" }, // contains many plane-1 characters
103#endif
104
98 { CS_UNKNOWN, 0 } 105 { CS_UNKNOWN, 0 }
99}; 106};
100 107
101///////////////////////////////////////////////////////////////////////////// 108/////////////////////////////////////////////////////////////////////////////
102 109
110#if XFT
111rxvt_drawable::~rxvt_drawable ()
112{
113 if (xftdrawable)
114 XftDrawDestroy (xftdrawable);
115}
116
117rxvt_drawable::operator XftDraw *()
118{
119 if (!xftdrawable)
120 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
121
122 return xftdrawable;
123}
124#endif
125
126/////////////////////////////////////////////////////////////////////////////
127
103static void *enc_buf; 128static void *enc_buf;
104static uint32_t enc_len; 129static uint32_t enc_len;
105 130
106static inline void * 131static inline void *
107get_enc_buf (int len) 132get_enc_buf (uint32_t len)
108{ 133{
109 if (len > enc_len) 134 if (len > enc_len)
110 { 135 {
111 free (enc_buf); 136 free (enc_buf);
112 enc_buf = malloc (len); 137 enc_buf = malloc (len);
114 139
115 return enc_buf; 140 return enc_buf;
116} 141}
117 142
118static const char * 143static const char *
119enc_char (const text_t *text, int len, codeset cs, bool &zero) 144enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
120{ 145{
121 uint8_t *buf = (uint8_t *)get_enc_buf (len); 146 uint8_t *buf = (uint8_t *)get_enc_buf (len);
122 147
123 while (len--) 148 while (len--)
124 { 149 {
135 160
136 return (const char *)enc_buf; 161 return (const char *)enc_buf;
137} 162}
138 163
139static const XChar2b * 164static const XChar2b *
140enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 165enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
141{ 166{
142 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 167 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
143 168
144 while (len--) 169 while (len--)
145 { 170 {
160} 185}
161 186
162///////////////////////////////////////////////////////////////////////////// 187/////////////////////////////////////////////////////////////////////////////
163 188
164void 189void
165rxvt_font::clear_rect (int x, int y, int w, int h, int color) 190rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
166{ 191{
167 if (color == Color_bg) 192 if (color == Color_bg)
168 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 193 XClearArea (d.display->display, d, x, y, w, h, FALSE);
169 else if (color >= 0) 194 else if (color >= 0)
170 { 195 {
196#if XFT
197 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
198#else
171 XSetForeground (DISPLAY, GC, r->PixColors[color]); 199 XSetForeground (d.display->display, GC, r->PixColors[color]);
172 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 200 XFillRectangle (d.display->display, d, GC, x, y, w, h);
201#endif
173 } 202 }
174} 203}
175 204
176static const char *linedraw_cmds[128] = { 205static const char *linedraw_cmds[128] = {
177 "1hH", "2hH", "1vV", "2vV", 206 "1hH", "2hH", "1vV", "2vV",
239 bool load (const rxvt_fontprop &prop) 268 bool load (const rxvt_fontprop &prop)
240 { 269 {
241 width = 1; height = 1; 270 width = 1; height = 1;
242 ascent = 1; descent = 0; 271 ascent = 1; descent = 0;
243 272
273 set_name ("built-in pseudofont");
274
244 return true; 275 return true;
245 } 276 }
246 277
247 bool has_codepoint (uint32_t unicode) 278 bool has_codepoint (uint32_t unicode)
248 { 279 {
249 if (unicode <= 0x001f) 280 if (unicode <= 0x001f)
250 return true; 281 return true;
282
251 if (unicode >= 0x0080 && unicode <= 0x009f) 283 if (unicode >= 0x0080 && unicode <= 0x009f)
252 return true; 284 return true;
253 285
254 if (unicode >= 0x2500 && unicode <= 0x257f 286 if (unicode >= 0x2500 && unicode <= 0x257f
255 && linedraw_cmds[unicode - 0x2500]) 287 && linedraw_cmds[unicode - 0x2500])
288 return true;
289
290 if (IS_COMPOSE (unicode))
256 return true; 291 return true;
257 292
258 switch (unicode) 293 switch (unicode)
259 { 294 {
260 case ZERO_WIDTH_CHAR: 295 case ZERO_WIDTH_CHAR:
262 } 297 }
263 298
264 return false; 299 return false;
265 } 300 }
266 301
267 void draw (int x, int y, 302 void draw (rxvt_drawable &d, int x, int y,
268 const text_t *text, int len, 303 const text_t *text, int len,
269 int fg, int bg); 304 int fg, int bg);
270}; 305};
271 306
272void 307void
273rxvt_font_default::draw (int x, int y, 308rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
274 const text_t *text, int len, 309 const text_t *text, int len,
275 int fg, int bg) 310 int fg, int bg)
276{ 311{
277 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 312 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
278 313
279 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 314 XSetForeground (d.display->display, GC, r->PixColors[fg]);
280 315
281 while (len--) 316 while (len--)
282 { 317 {
318 compose_char *cc;
283 text_t t = *text++; 319 text_t t = *text++;
284 320
321 // is it in our linedrawing table?
285 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 322 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
286 { 323 {
287 const char *p = linedraw_cmds[t - 0x2500]; 324 const char *p = linedraw_cmds[t - 0x2500];
288 325
289 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; 326 int x0 = x, x1 = x + (r->TermWin.fwidth - 1) / 2, x2 = x + r->TermWin.fwidth - 1;
290 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight; 327 int y0 = y, y1 = y + (r->TermWin.fheight - 1) / 2, y2 = y + r->TermWin.fheight - 1;
291 328
292 XGCValues gcv; 329 XGCValues gcv;
293 330
294 while (*p) 331 while (*p)
295 { 332 {
296 switch (*p++) 333 switch (*p++)
297 { 334 {
298 case '1': 335 case '1':
299 gcv.line_width = 0; 336 gcv.line_width = 0;
300 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 337 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
301 break; 338 break;
302 339
303 case '2': 340 case '2':
304 gcv.line_width = 2; 341 gcv.line_width = 2;
305 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 342 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
306 break; 343 break;
307 344
308 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; 345 case 'h': XDrawLine (d.display->display, d, GC, x0, y1, x1, y1); break;
309 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break; 346 case 'H': XDrawLine (d.display->display, d, GC, x1, y1, x2, y1); break;
310 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break; 347 case 'v': XDrawLine (d.display->display, d, GC, x1, y0, x1, y1); break;
311 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break; 348 case 'V': XDrawLine (d.display->display, d, GC, x1, y1, x1, y2); break;
312 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break; 349 case 'a': XDrawLine (d.display->display, d, GC, x0, y2, x2, y0); break;
313 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break; 350 case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
314 } 351 }
315 } 352 }
316 353
317 gcv.line_width = 0; 354 gcv.line_width = 0;
318 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 355 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
356 }
357 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
358 {
359 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
360 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
361 if (cc->c2 != NOCHAR)
362 {
363 // prefer font of first character, for no good reasons
364 rxvt_font *f2 = f1->has_codepoint (cc->c2)
365 ? f1
366 : (*fs)[fs->find_font (cc->c2)];
367
368 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
369 }
319 } 370 }
320 else 371 else
321 switch (*text++) 372 switch (t)
322 { 373 {
323 case NOCHAR:
324 case ZERO_WIDTH_CHAR: 374 case ZERO_WIDTH_CHAR:
325 break; 375 break;
326 default: 376 default:
327 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 377 int w = 0;
378 while (len > 0 && *text == NOCHAR)
379 {
380 ++text;
381 --len;
382 w += r->TermWin.fwidth;
383 }
384
385 XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
386 w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
387 x += w;
328 } 388 }
329 389
330 x += r->TermWin.fwidth; 390 x += r->TermWin.fwidth;
331 } 391 }
332} 392}
337 rxvt_font_x11 () { f = 0; } 397 rxvt_font_x11 () { f = 0; }
338 398
339 void clear (); 399 void clear ();
340 400
341 rxvt_fontprop properties (); 401 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
343 402
344 bool load (const rxvt_fontprop &prop); 403 bool load (const rxvt_fontprop &prop);
345 404
346 bool has_codepoint (uint32_t unicode); 405 bool has_codepoint (uint32_t unicode);
347 406
348 void draw (int x, int y, 407 void draw (rxvt_drawable &d, int x, int y,
349 const text_t *text, int len, 408 const text_t *text, int len,
350 int fg, int bg); 409 int fg, int bg);
351 410
352 XFontStruct *f; 411 XFontStruct *f;
353 codeset cs; 412 codeset cs;
354 bool enc2b, encm; 413 bool enc2b, encm;
355 414
356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const; 415 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
416 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
417 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
418 bool set_properties (rxvt_fontprop &p, const char *name);
357}; 419};
358 420
359const char * 421const char *
360rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 422rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
361{ 423{
368} 430}
369 431
370rxvt_fontprop 432rxvt_fontprop
371rxvt_font_x11::properties () 433rxvt_font_x11::properties ()
372{ 434{
373 return properties (f);
374}
375
376rxvt_fontprop
377rxvt_font_x11::properties (XFontStruct *f)
378{
379 rxvt_fontprop p; 435 rxvt_fontprop p;
436 set_properties (p, f);
437 return p;
438}
380 439
440bool
441rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
442{
443 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
444 p.height = height;
445 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
446 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
447
448 return true;
449}
450
451bool
452rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
453{
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium"); 454 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r"); 455 const char *slant = get_property (f, "SLANT", "r");
383 456
457 unsigned long height;
458 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
459 return false;
460
384 unsigned long avgwidth; 461 unsigned long avgwidth;
385 p.width = XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth) 462 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
386 ? avgwidth / 10 463 avgwidth = 0;
387 : (height + 1) / 2;
388 p.height = height;
389 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
390 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
391 464
392 return p; 465 return set_properties (p, height, weight, slant, avgwidth);
466}
467
468bool
469rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
470{
471 int slashes = 0;
472 const char *comp[13];
473
474 for (const char *c = name; *c; c++)
475 if (*c == '-')
476 {
477 comp[slashes++] = c + 1;
478 if (slashes >= 13)
479 break;
480 }
481
482 /* can we short-circuit the costly XLoadQueryFont? */
483 if (slashes >= 13
484 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
485 && (*comp[11] >= '0' && *comp[11] <= '9'))
486 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
487
488 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
489
490 if (f)
491 {
492 // the font should really exist now. if not, we have a problem
493 // (e.g. if the user did xset fp rehash just when we were searching fonts).
494 // in that case, just return garbage.
495 bool ret = set_properties (p, f);
496 XFreeFont (DISPLAY, f);
497 return ret;
498 }
499 else
500 return false;
501}
502
503// fix the size of scalable fonts
504static void
505fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
506{
507 int slashes = 0;
508 const char *size;
509
510 for (const char *c = name; *c; c++)
511 if (*c == '-')
512 {
513 if (slashes == 6)
514 size = c + 1;
515
516 if (++slashes >= 13)
517 break;
518 }
519
520 if (slashes >= 13 && size[0] == '0')
521 {
522 strncpy (buf, name, size - name);
523 buf += size - name;
524 buf += sprintf (buf, "%d", prop.height);
525 strcpy (buf, size + 1);
526 }
527 else
528 strcpy (buf, name);
393} 529}
394 530
395bool 531bool
396rxvt_font_x11::load (const rxvt_fontprop &prop) 532rxvt_font_x11::load (const rxvt_fontprop &prop)
397{ 533{
398 clear (); 534 clear ();
399 535
400 char **list; 536 char **list;
401 int count; 537 int count;
402 XFontStruct *info;
403 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 538 list = XListFonts (DISPLAY, name, 512, &count);
539 set_name (0);
404 540
405 if (!list) 541 if (!list)
406 return false; 542 return false;
407 543
408 int bestdiff = 0x7fffffff; 544 int bestdiff = 0x7fffffff;
409 XFontStruct *best = 0;
410 for (int i = 0; i < count; i++) 545 for (int i = 0; i < count; i++)
411 { 546 {
412 XFontStruct *f = info + i; 547 rxvt_fontprop p;
548 char fname[1024];
549 fix_scalable (fname, list[i], prop);
413 550
551 if (!set_properties (p, fname))
552 continue;
553
414 if (f->ascent + f->descent <= prop.height) // weed out too large fonts 554 if (p.height > prop.height) // weed out too large fonts
415 { 555 continue;
416 rxvt_fontprop p = properties (f); 556
417 int diff = (prop.height - f->ascent + f->descent) * 32 557 int diff = (prop.height - p.height) * 32
418 + abs (prop.weight - p.weight) 558 + abs (prop.weight - p.weight)
419 + abs (prop.slant - p.slant ); 559 + abs (prop.slant - p.slant );
420 560
421 if (!best // compare against best found so far 561 if (!name // compare against best found so far
422 || diff < bestdiff) 562 || diff < bestdiff)
423 { 563 {
424 best = f; 564 set_name (strdup (fname));
425 bestdiff = diff; 565 bestdiff = diff;
426 } 566 }
427 }
428 } 567 }
429 568
569 XFreeFontNames (list);
570
430 if (!best) 571 if (!name)
431 return false; 572 return false;
432
433 set_name (strdup (list[best - info]));
434
435 XFreeFontInfo (list, info, count);
436 573
437 f = XLoadQueryFont (DISPLAY, name); 574 f = XLoadQueryFont (DISPLAY, name);
438 575
439 if (!f) 576 if (!f)
440 return false; 577 return false;
441
442 unsigned long value;
443 578
444 const char *registry = get_property (f, "CHARSET_REGISTRY", 0); 579 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
445 const char *encoding = get_property (f, "CHARSET_ENCODING", 0); 580 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
446 581
447 if (registry && encoding) 582 if (registry && encoding)
534 /* check wether the character exists in _this_ font. horrible. */ 669 /* check wether the character exists in _this_ font. horrible. */
535 XCharStruct *xcs; 670 XCharStruct *xcs;
536 671
537 if (encm) 672 if (encm)
538 { 673 {
539 int byte1 = ch >> 8; 674 unsigned char byte1 = ch >> 8;
540 int byte2 = ch & 255; 675 unsigned char byte2 = ch & 255;
541 676
542 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 677 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
543 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 678 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
544 return false; 679 return false;
545 680
568 703
569 return true; 704 return true;
570} 705}
571 706
572void 707void
573rxvt_font_x11::draw (int x, int y, 708rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
574 const text_t *text, int len, 709 const text_t *text, int len,
575 int fg, int bg) 710 int fg, int bg)
576{ 711{
577 // this looks like a mess /. 712 // this looks like a mess /.
578 // and it is a mess /. 713 // and it is a mess /.
594 { 729 {
595 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 730 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
596 731
597 if (bg == Color_bg && !slow) 732 if (bg == Color_bg && !slow)
598 { 733 {
599 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 734 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
600 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 735 XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
601 } 736 }
602 else 737 else
603 { 738 {
604 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 739 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
605 740
606 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 741 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
607 742
608 if (slow) 743 if (slow)
609 { 744 {
610 do 745 do
611 { 746 {
612 if (xc->byte1 || xc->byte2) 747 if (xc->byte1 || xc->byte2)
613 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 748 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
614 749
615 x += r->TermWin.fwidth; 750 x += r->TermWin.fwidth;
616 xc++; len--; 751 xc++; len--;
617 } 752 }
618 while (len); 753 while (len);
619 } 754 }
620 else 755 else
621 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 756 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
622 } 757 }
623 } 758 }
624 else 759 else
625 { 760 {
626 const char *xc = enc_char (text, len, cs, slow); 761 const char *xc = enc_char (text, len, cs, slow);
627 762
628 if (bg == Color_bg && !slow) 763 if (bg == Color_bg && !slow)
629 { 764 {
630 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 765 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
631 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 766 XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
632 } 767 }
633 else 768 else
634 { 769 {
635 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 770 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
636 771
637 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 772 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
638 773
639 if (slow) 774 if (slow)
640 { 775 {
641 do 776 do
642 { 777 {
643 if (*xc) 778 if (*xc)
644 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 779 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
645 780
646 x += r->TermWin.fwidth; 781 x += r->TermWin.fwidth;
647 xc++; len--; 782 xc++; len--;
648 } 783 }
649 while (len); 784 while (len);
650 } 785 }
651 else 786 else
652 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 787 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
653 } 788 }
654 } 789 }
655} 790}
656 791
657///////////////////////////////////////////////////////////////////////////// 792/////////////////////////////////////////////////////////////////////////////
662//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 797//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
663#define SWATHBITS 8 798#define SWATHBITS 8
664#endif 799#endif
665 800
666struct rxvt_font_xft : rxvt_font { 801struct rxvt_font_xft : rxvt_font {
667 rxvt_font_xft () { f = 0; d = 0; } 802 rxvt_font_xft () { f = 0; }
668 803
669 void clear (); 804 void clear ();
670 805
671 rxvt_fontprop properties (); 806 rxvt_fontprop properties ();
672 807
673 bool load (const rxvt_fontprop &prop); 808 bool load (const rxvt_fontprop &prop);
674 809
675 void draw (int x, int y, 810 void draw (rxvt_drawable &d, int x, int y,
676 const text_t *text, int len, 811 const text_t *text, int len,
677 int fg, int bg); 812 int fg, int bg);
678 813
679 bool has_codepoint (uint32_t unicode); 814 bool has_codepoint (uint32_t unicode);
680 815
681protected: 816protected:
682 XftFont *f; 817 XftFont *f;
683 XftDraw *d;
684}; 818};
685 819
686void 820void
687rxvt_font_xft::clear () 821rxvt_font_xft::clear ()
688{ 822{
689 if (f) 823 if (f)
690 { 824 {
691 XftFontClose (DISPLAY, f); 825 XftFontClose (DISPLAY, f);
692 f = 0; 826 f = 0;
693 }
694
695 if (d)
696 {
697 XftDrawDestroy (d);
698 d = 0;
699 } 827 }
700} 828}
701 829
702rxvt_fontprop 830rxvt_fontprop
703rxvt_font_xft::properties () 831rxvt_font_xft::properties ()
736 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 864 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
737 865
738 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 866 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
739 FcPatternAddInteger (p, FC_SLANT, prop.slant); 867 FcPatternAddInteger (p, FC_SLANT, prop.slant);
740 868
869#if 0 // clipping unfortunately destroys our precious double-width-characters
741 // clip width, we can't do better, or can we? 870 // clip width, we can't do better, or can we?
742 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 871 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
743 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 872 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
873#endif
744 874
745 //FcPatternAddBool (p, FC_MINSPACE, 1); 875 //FcPatternAddBool (p, FC_MINSPACE, 1);
746 876
747 XftResult result; 877 XftResult result;
748 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 878 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
808{ 938{
809 return XftCharExists (DISPLAY, f, unicode); 939 return XftCharExists (DISPLAY, f, unicode);
810} 940}
811 941
812void 942void
813rxvt_font_xft::draw (int x, int y, 943rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
814 const text_t *text, int len, 944 const text_t *text, int len,
815 int fg, int bg) 945 int fg, int bg)
816{ 946{
817 if (!d)
818 {
819 dR;
820 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
821 }
822
823 if (bg >= 0 && bg != Color_bg)
824 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
825 else
826 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 947 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
827 948
828 if (!slow && width == r->TermWin.fwidth) 949 if (!slow && width == r->TermWin.fwidth && 0)
829 { 950 {
830 if (sizeof (text_t) == sizeof (FcChar16)) 951 if (sizeof (text_t) == sizeof (FcChar16))
831 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 952 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
832 else 953 else
833 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 954 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
836 { 957 {
837 while (len) 958 while (len)
838 { 959 {
839 if (*text != NOCHAR && *text != ' ') 960 if (*text != NOCHAR && *text != ' ')
840 { 961 {
962 int fwidth = r->TermWin.fwidth;
963 if (len >= 2 && text[1] == NOCHAR)
964 fwidth *= 2;
965
966 XGlyphInfo extents;
841 if (sizeof (text_t) == sizeof (FcChar16)) 967 if (sizeof (text_t) == sizeof (FcChar16))
842 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 968 {
969 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
970 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
971 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
972 }
843 else 973 else
844 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 974 {
975 XGlyphInfo extents;
976 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
977 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
978 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
979 }
845 } 980 }
846 981
847 x += r->TermWin.fwidth; 982 x += r->TermWin.fwidth;
848 text++; 983 text++;
849 len--; 984 len--;
853#endif 988#endif
854 989
855///////////////////////////////////////////////////////////////////////////// 990/////////////////////////////////////////////////////////////////////////////
856 991
857rxvt_fontset::rxvt_fontset (rxvt_t r) 992rxvt_fontset::rxvt_fontset (rxvt_t r)
858#ifdef EXPLICIT_CONTEXT
859: r(r) 993: r (r)
860#endif
861{ 994{
862 clear (); 995 clear ();
863} 996}
864 997
865rxvt_fontset::~rxvt_fontset () 998rxvt_fontset::~rxvt_fontset ()
868} 1001}
869 1002
870void 1003void
871rxvt_fontset::clear () 1004rxvt_fontset::clear ()
872{ 1005{
873 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 1006 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
874 FONT_UNREF (*i); 1007 FONT_UNREF (*i);
875 1008
876 fonts.clear (); 1009 fonts.clear ();
877 base_id = 0; 1010 base_id = 0;
878 base_prop.height = 0x7fffffff; 1011 base_prop.height = 0x7fffffff;
905 f = new rxvt_font_x11; 1038 f = new rxvt_font_x11;
906 } 1039 }
907 else 1040 else
908 f = new rxvt_font_x11; 1041 f = new rxvt_font_x11;
909 1042
1043 f->fs = this;
910 f->set_term (r); 1044 f->set_term (r);
911 f->set_name (strdup (name)); 1045 f->set_name (strdup (name));
912 1046
913 f->cs = cs; 1047 f->cs = cs;
914 f->loaded = false; 1048 f->loaded = false;
932 1066
933 if (*desc == '[') 1067 if (*desc == '[')
934 { 1068 {
935 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1069 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
936 1070
937 const char *extra = desc++; 1071 //const char *extra = desc++; // not yet used
938 1072
939 desc = strchr (desc, ']'); 1073 desc = strchr (desc, ']');
940 1074
941 if (!desc) 1075 if (!desc)
942 { 1076 {
981 } 1115 }
982 1116
983 return true; 1117 return true;
984} 1118}
985 1119
986void 1120bool
987rxvt_fontset::populate (const char *desc) 1121rxvt_fontset::populate (const char *desc)
988{ 1122{
989 clear (); 1123 clear ();
990 1124
991 fonts.push_back (new_font (0, CS_UNICODE)); 1125 fonts.push_back (new_font (0, CS_UNICODE));
995 1129
996 if (!base_id) 1130 if (!base_id)
997 base_id = 1; 1131 base_id = 1;
998 1132
999 // we currently need a base-font, no matter what 1133 // we currently need a base-font, no matter what
1000 if (fonts.size () <= base_id || !realize_font (base_id)) 1134 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1001 { 1135 {
1136 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1002 add_fonts ("fixed"); 1137 add_fonts ("fixed");
1003 base_id = fonts.size () - 1; 1138 base_id = fonts.size () - 1;
1004 } 1139 }
1005 1140
1006 if (fonts.size () <= base_id || !realize_font (base_id)) 1141 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1007 { 1142 return false;
1008 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1009 exit (1);
1010 }
1011 1143
1012 base_prop = fonts[base_id]->properties (); 1144 base_prop = fonts[base_id]->properties ();
1145
1146 return true;
1013} 1147}
1014 1148
1015int 1149int
1016rxvt_fontset::find_font (uint32_t unicode) 1150rxvt_fontset::find_font (uint32_t unicode)
1017{ 1151{
1018 for (int i = 0; i < fonts.size (); i++) 1152 for (unsigned int i = 0; i < fonts.size (); i++)
1019 { 1153 {
1020 rxvt_font *f = fonts[i]; 1154 rxvt_font *f = fonts[i];
1021 1155
1022 if (!f->loaded) 1156 if (!f->loaded)
1023 { 1157 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines