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.29 by pcg, Wed Mar 3 20:15:18 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" },
98 { CS_UNKNOWN, 0 } 101 { CS_UNKNOWN, 0 }
99}; 102};
100 103
101///////////////////////////////////////////////////////////////////////////// 104/////////////////////////////////////////////////////////////////////////////
102 105
106#if XFT
107rxvt_drawable::~rxvt_drawable ()
108{
109 if (xftdrawable)
110 XftDrawDestroy (xftdrawable);
111}
112
113rxvt_drawable::operator XftDraw *()
114{
115 if (!xftdrawable)
116 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
117
118 return xftdrawable;
119}
120#endif
121
122/////////////////////////////////////////////////////////////////////////////
123
103static void *enc_buf; 124static void *enc_buf;
104static uint32_t enc_len; 125static uint32_t enc_len;
105 126
106static inline void * 127static inline void *
107get_enc_buf (int len) 128get_enc_buf (uint32_t len)
108{ 129{
109 if (len > enc_len) 130 if (len > enc_len)
110 { 131 {
111 free (enc_buf); 132 free (enc_buf);
112 enc_buf = malloc (len); 133 enc_buf = malloc (len);
114 135
115 return enc_buf; 136 return enc_buf;
116} 137}
117 138
118static const char * 139static const char *
119enc_char (const text_t *text, int len, codeset cs, bool &zero) 140enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
120{ 141{
121 uint8_t *buf = (uint8_t *)get_enc_buf (len); 142 uint8_t *buf = (uint8_t *)get_enc_buf (len);
122 143
123 while (len--) 144 while (len--)
124 { 145 {
135 156
136 return (const char *)enc_buf; 157 return (const char *)enc_buf;
137} 158}
138 159
139static const XChar2b * 160static const XChar2b *
140enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 161enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
141{ 162{
142 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 163 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
143 164
144 while (len--) 165 while (len--)
145 { 166 {
160} 181}
161 182
162///////////////////////////////////////////////////////////////////////////// 183/////////////////////////////////////////////////////////////////////////////
163 184
164void 185void
165rxvt_font::clear_rect (int x, int y, int w, int h, int color) 186rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
166{ 187{
167 if (color == Color_bg) 188 if (color == Color_bg)
168 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 189 XClearArea (d.display->display, d, x, y, w, h, FALSE);
169 else if (color >= 0) 190 else if (color >= 0)
170 { 191 {
192#if XFT
193 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
194#else
171 XSetForeground (DISPLAY, GC, r->PixColors[color]); 195 XSetForeground (d.display->display, GC, r->PixColors[color]);
172 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 196 XFillRectangle (d.display->display, d, GC, x, y, w, h);
197#endif
173 } 198 }
174} 199}
175 200
176static const char *linedraw_cmds[128] = { 201static const char *linedraw_cmds[128] = {
177 "1hH", "2hH", "1vV", "2vV", 202 "1hH", "2hH", "1vV", "2vV",
262 } 287 }
263 288
264 return false; 289 return false;
265 } 290 }
266 291
267 void draw (int x, int y, 292 void draw (rxvt_drawable &d, int x, int y,
268 const text_t *text, int len, 293 const text_t *text, int len,
269 int fg, int bg); 294 int fg, int bg);
270}; 295};
271 296
272void 297void
273rxvt_font_default::draw (int x, int y, 298rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
274 const text_t *text, int len, 299 const text_t *text, int len,
275 int fg, int bg) 300 int fg, int bg)
276{ 301{
277 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 302 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
278 303
279 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 304 XSetForeground (d.display->display, GC, r->PixColors[fg]);
280 305
281 while (len--) 306 while (len--)
282 { 307 {
283 text_t t = *text++; 308 text_t t = *text++;
284 309
310 // is it in our linedrawing table?
285 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 311 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
286 { 312 {
287 const char *p = linedraw_cmds[t - 0x2500]; 313 const char *p = linedraw_cmds[t - 0x2500];
288 314
289 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; 315 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth - 1;
290 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight; 316 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight - 1;
291 317
292 XGCValues gcv; 318 XGCValues gcv;
293 319
294 while (*p) 320 while (*p)
295 { 321 {
296 switch (*p++) 322 switch (*p++)
297 { 323 {
298 case '1': 324 case '1':
299 gcv.line_width = 0; 325 gcv.line_width = 0;
300 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 326 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
301 break; 327 break;
302 328
303 case '2': 329 case '2':
304 gcv.line_width = 2; 330 gcv.line_width = 2;
305 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 331 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
306 break; 332 break;
307 333
308 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; 334 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; 335 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; 336 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; 337 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; 338 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; 339 case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
314 } 340 }
315 } 341 }
316 342
317 gcv.line_width = 0; 343 gcv.line_width = 0;
318 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 344 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
319 } 345 }
320 else 346 else
321 switch (*text++) 347 switch (t)
322 { 348 {
323 case NOCHAR:
324 case ZERO_WIDTH_CHAR: 349 case ZERO_WIDTH_CHAR:
325 break; 350 break;
326 default: 351 default:
327 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 352 int w = 0;
353 while (len > 0 && *text == NOCHAR)
354 {
355 ++text;
356 --len;
357 w += r->TermWin.fwidth;
358 }
359
360 XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
361 w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
362 x += w;
328 } 363 }
329 364
330 x += r->TermWin.fwidth; 365 x += r->TermWin.fwidth;
331 } 366 }
332} 367}
337 rxvt_font_x11 () { f = 0; } 372 rxvt_font_x11 () { f = 0; }
338 373
339 void clear (); 374 void clear ();
340 375
341 rxvt_fontprop properties (); 376 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
343 377
344 bool load (const rxvt_fontprop &prop); 378 bool load (const rxvt_fontprop &prop);
345 379
346 bool has_codepoint (uint32_t unicode); 380 bool has_codepoint (uint32_t unicode);
347 381
348 void draw (int x, int y, 382 void draw (rxvt_drawable &d, int x, int y,
349 const text_t *text, int len, 383 const text_t *text, int len,
350 int fg, int bg); 384 int fg, int bg);
351 385
352 XFontStruct *f; 386 XFontStruct *f;
353 codeset cs; 387 codeset cs;
354 bool enc2b, encm; 388 bool enc2b, encm;
355 389
356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const; 390 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
391 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
392 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
393 bool set_properties (rxvt_fontprop &p, const char *name);
357}; 394};
358 395
359const char * 396const char *
360rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 397rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
361{ 398{
368} 405}
369 406
370rxvt_fontprop 407rxvt_fontprop
371rxvt_font_x11::properties () 408rxvt_font_x11::properties ()
372{ 409{
373 return properties (f);
374}
375
376rxvt_fontprop
377rxvt_font_x11::properties (XFontStruct *f)
378{
379 rxvt_fontprop p; 410 rxvt_fontprop p;
411 set_properties (p, f);
412 return p;
413}
380 414
415bool
416rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
417{
418 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
419 p.height = height;
420 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
421 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
422
423 return true;
424}
425
426bool
427rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
428{
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium"); 429 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r"); 430 const char *slant = get_property (f, "SLANT", "r");
383 431
432 unsigned long height;
433 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
434 return false;
435
384 unsigned long avgwidth; 436 unsigned long avgwidth;
385 p.width = XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth) 437 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
386 ? avgwidth / 10 438 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 439
392 return p; 440 return set_properties (p, height, weight, slant, avgwidth);
441}
442
443bool
444rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
445{
446 int slashes = 0;
447 const char *comp[13];
448
449 for (const char *c = name; *c; c++)
450 if (*c == '-')
451 {
452 comp[slashes++] = c + 1;
453 if (slashes >= 13)
454 break;
455 }
456
457 /* can we short-circuit the costly XLoadQueryFont? */
458 if (slashes >= 13
459 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
460 && (*comp[11] >= '0' && *comp[11] <= '9'))
461 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
462
463 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
464
465 if (f)
466 {
467 // the font should really exist now. if not, we have a problem
468 // (e.g. if the user did xset fp rehash just when we were searching fonts).
469 // in that case, just return garbage.
470 bool ret = set_properties (p, f);
471 XFreeFont (DISPLAY, f);
472 return ret;
473 }
474 else
475 return false;
476}
477
478// fix the size of scalable fonts
479static void
480fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
481{
482 int slashes = 0;
483 const char *size;
484
485 for (const char *c = name; *c; c++)
486 if (*c == '-')
487 {
488 if (slashes == 6)
489 size = c + 1;
490
491 if (++slashes >= 13)
492 break;
493 }
494
495 if (slashes >= 13 && size[0] == '0')
496 {
497 strncpy (buf, name, size - name);
498 buf += size - name;
499 buf += sprintf (buf, "%d", prop.height);
500 strcpy (buf, size + 1);
501 }
502 else
503 strcpy (buf, name);
393} 504}
394 505
395bool 506bool
396rxvt_font_x11::load (const rxvt_fontprop &prop) 507rxvt_font_x11::load (const rxvt_fontprop &prop)
397{ 508{
398 clear (); 509 clear ();
399 510
400 char **list; 511 char **list;
401 int count; 512 int count;
402 XFontStruct *info;
403 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 513 list = XListFonts (DISPLAY, name, 512, &count);
514 set_name (0);
404 515
405 if (!list) 516 if (!list)
406 return false; 517 return false;
407 518
408 int bestdiff = 0x7fffffff; 519 int bestdiff = 0x7fffffff;
409 XFontStruct *best = 0;
410 for (int i = 0; i < count; i++) 520 for (int i = 0; i < count; i++)
411 { 521 {
412 XFontStruct *f = info + i; 522 rxvt_fontprop p;
523 char fname[1024];
524 fix_scalable (fname, list[i], prop);
413 525
526 if (!set_properties (p, fname))
527 continue;
528
414 if (f->ascent + f->descent <= prop.height) // weed out too large fonts 529 if (p.height > prop.height) // weed out too large fonts
415 { 530 continue;
416 rxvt_fontprop p = properties (f); 531
417 int diff = (prop.height - f->ascent + f->descent) * 32 532 int diff = (prop.height - p.height) * 32
418 + abs (prop.weight - p.weight) 533 + abs (prop.weight - p.weight)
419 + abs (prop.slant - p.slant ); 534 + abs (prop.slant - p.slant );
420 535
421 if (!best // compare against best found so far 536 if (!name // compare against best found so far
422 || diff < bestdiff) 537 || diff < bestdiff)
423 { 538 {
424 best = f; 539 set_name (strdup (fname));
425 bestdiff = diff; 540 bestdiff = diff;
426 } 541 }
427 }
428 } 542 }
429 543
544 XFreeFontNames (list);
545
430 if (!best) 546 if (!name)
431 return false; 547 return false;
432
433 set_name (strdup (list[best - info]));
434
435 XFreeFontInfo (list, info, count);
436 548
437 f = XLoadQueryFont (DISPLAY, name); 549 f = XLoadQueryFont (DISPLAY, name);
438 550
439 if (!f) 551 if (!f)
440 return false; 552 return false;
441
442 unsigned long value;
443 553
444 const char *registry = get_property (f, "CHARSET_REGISTRY", 0); 554 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
445 const char *encoding = get_property (f, "CHARSET_ENCODING", 0); 555 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
446 556
447 if (registry && encoding) 557 if (registry && encoding)
534 /* check wether the character exists in _this_ font. horrible. */ 644 /* check wether the character exists in _this_ font. horrible. */
535 XCharStruct *xcs; 645 XCharStruct *xcs;
536 646
537 if (encm) 647 if (encm)
538 { 648 {
539 int byte1 = ch >> 8; 649 unsigned char byte1 = ch >> 8;
540 int byte2 = ch & 255; 650 unsigned char byte2 = ch & 255;
541 651
542 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 652 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
543 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 653 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
544 return false; 654 return false;
545 655
568 678
569 return true; 679 return true;
570} 680}
571 681
572void 682void
573rxvt_font_x11::draw (int x, int y, 683rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
574 const text_t *text, int len, 684 const text_t *text, int len,
575 int fg, int bg) 685 int fg, int bg)
576{ 686{
577 // this looks like a mess /. 687 // this looks like a mess /.
578 // and it is a mess /. 688 // and it is a mess /.
594 { 704 {
595 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 705 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
596 706
597 if (bg == Color_bg && !slow) 707 if (bg == Color_bg && !slow)
598 { 708 {
599 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 709 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
600 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 710 XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
601 } 711 }
602 else 712 else
603 { 713 {
604 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 714 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
605 715
606 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 716 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
607 717
608 if (slow) 718 if (slow)
609 { 719 {
610 do 720 do
611 { 721 {
612 if (xc->byte1 || xc->byte2) 722 if (xc->byte1 || xc->byte2)
613 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 723 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
614 724
615 x += r->TermWin.fwidth; 725 x += r->TermWin.fwidth;
616 xc++; len--; 726 xc++; len--;
617 } 727 }
618 while (len); 728 while (len);
619 } 729 }
620 else 730 else
621 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 731 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
622 } 732 }
623 } 733 }
624 else 734 else
625 { 735 {
626 const char *xc = enc_char (text, len, cs, slow); 736 const char *xc = enc_char (text, len, cs, slow);
627 737
628 if (bg == Color_bg && !slow) 738 if (bg == Color_bg && !slow)
629 { 739 {
630 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 740 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
631 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 741 XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
632 } 742 }
633 else 743 else
634 { 744 {
635 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 745 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
636 746
637 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 747 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
638 748
639 if (slow) 749 if (slow)
640 { 750 {
641 do 751 do
642 { 752 {
643 if (*xc) 753 if (*xc)
644 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 754 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
645 755
646 x += r->TermWin.fwidth; 756 x += r->TermWin.fwidth;
647 xc++; len--; 757 xc++; len--;
648 } 758 }
649 while (len); 759 while (len);
650 } 760 }
651 else 761 else
652 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 762 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
653 } 763 }
654 } 764 }
655} 765}
656 766
657///////////////////////////////////////////////////////////////////////////// 767/////////////////////////////////////////////////////////////////////////////
662//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 772//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
663#define SWATHBITS 8 773#define SWATHBITS 8
664#endif 774#endif
665 775
666struct rxvt_font_xft : rxvt_font { 776struct rxvt_font_xft : rxvt_font {
667 rxvt_font_xft () { f = 0; d = 0; } 777 rxvt_font_xft () { f = 0; }
668 778
669 void clear (); 779 void clear ();
670 780
671 rxvt_fontprop properties (); 781 rxvt_fontprop properties ();
672 782
673 bool load (const rxvt_fontprop &prop); 783 bool load (const rxvt_fontprop &prop);
674 784
675 void draw (int x, int y, 785 void draw (rxvt_drawable &d, int x, int y,
676 const text_t *text, int len, 786 const text_t *text, int len,
677 int fg, int bg); 787 int fg, int bg);
678 788
679 bool has_codepoint (uint32_t unicode); 789 bool has_codepoint (uint32_t unicode);
680 790
681protected: 791protected:
682 XftFont *f; 792 XftFont *f;
683 XftDraw *d;
684}; 793};
685 794
686void 795void
687rxvt_font_xft::clear () 796rxvt_font_xft::clear ()
688{ 797{
689 if (f) 798 if (f)
690 { 799 {
691 XftFontClose (DISPLAY, f); 800 XftFontClose (DISPLAY, f);
692 f = 0; 801 f = 0;
693 }
694
695 if (d)
696 {
697 XftDrawDestroy (d);
698 d = 0;
699 } 802 }
700} 803}
701 804
702rxvt_fontprop 805rxvt_fontprop
703rxvt_font_xft::properties () 806rxvt_font_xft::properties ()
736 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 839 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
737 840
738 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 841 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
739 FcPatternAddInteger (p, FC_SLANT, prop.slant); 842 FcPatternAddInteger (p, FC_SLANT, prop.slant);
740 843
844#if 0 // clipping unfortunately destroys our precious double-width-characters
741 // clip width, we can't do better, or can we? 845 // clip width, we can't do better, or can we?
742 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 846 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
743 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 847 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
848#endif
744 849
745 //FcPatternAddBool (p, FC_MINSPACE, 1); 850 //FcPatternAddBool (p, FC_MINSPACE, 1);
746 851
747 XftResult result; 852 XftResult result;
748 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 853 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
808{ 913{
809 return XftCharExists (DISPLAY, f, unicode); 914 return XftCharExists (DISPLAY, f, unicode);
810} 915}
811 916
812void 917void
813rxvt_font_xft::draw (int x, int y, 918rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
814 const text_t *text, int len, 919 const text_t *text, int len,
815 int fg, int bg) 920 int fg, int bg)
816{ 921{
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); 922 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
827 923
828 if (!slow && width == r->TermWin.fwidth) 924 if (!slow && width == r->TermWin.fwidth && 0)
829 { 925 {
830 if (sizeof (text_t) == sizeof (FcChar16)) 926 if (sizeof (text_t) == sizeof (FcChar16))
831 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 927 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
832 else 928 else
833 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 929 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
836 { 932 {
837 while (len) 933 while (len)
838 { 934 {
839 if (*text != NOCHAR && *text != ' ') 935 if (*text != NOCHAR && *text != ' ')
840 { 936 {
937 int fwidth = r->TermWin.fwidth;
938 if (len >= 2 && text[1] == NOCHAR)
939 fwidth *= 2;
940
941 XGlyphInfo extents;
841 if (sizeof (text_t) == sizeof (FcChar16)) 942 if (sizeof (text_t) == sizeof (FcChar16))
842 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 943 {
944 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
945 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
946 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
947 }
843 else 948 else
844 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 949 {
950 XGlyphInfo extents;
951 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
952 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
953 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
954 }
845 } 955 }
846 956
847 x += r->TermWin.fwidth; 957 x += r->TermWin.fwidth;
848 text++; 958 text++;
849 len--; 959 len--;
853#endif 963#endif
854 964
855///////////////////////////////////////////////////////////////////////////// 965/////////////////////////////////////////////////////////////////////////////
856 966
857rxvt_fontset::rxvt_fontset (rxvt_t r) 967rxvt_fontset::rxvt_fontset (rxvt_t r)
858#ifdef EXPLICIT_CONTEXT
859: r(r) 968: r (r)
860#endif
861{ 969{
862 clear (); 970 clear ();
863} 971}
864 972
865rxvt_fontset::~rxvt_fontset () 973rxvt_fontset::~rxvt_fontset ()
868} 976}
869 977
870void 978void
871rxvt_fontset::clear () 979rxvt_fontset::clear ()
872{ 980{
873 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 981 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
874 FONT_UNREF (*i); 982 FONT_UNREF (*i);
875 983
876 fonts.clear (); 984 fonts.clear ();
877 base_id = 0; 985 base_id = 0;
878 base_prop.height = 0x7fffffff; 986 base_prop.height = 0x7fffffff;
932 1040
933 if (*desc == '[') 1041 if (*desc == '[')
934 { 1042 {
935 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1043 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
936 1044
937 const char *extra = desc++; 1045 //const char *extra = desc++; // not yet used
938 1046
939 desc = strchr (desc, ']'); 1047 desc = strchr (desc, ']');
940 1048
941 if (!desc) 1049 if (!desc)
942 { 1050 {
981 } 1089 }
982 1090
983 return true; 1091 return true;
984} 1092}
985 1093
986void 1094bool
987rxvt_fontset::populate (const char *desc) 1095rxvt_fontset::populate (const char *desc)
988{ 1096{
989 clear (); 1097 clear ();
990 1098
991 fonts.push_back (new_font (0, CS_UNICODE)); 1099 fonts.push_back (new_font (0, CS_UNICODE));
995 1103
996 if (!base_id) 1104 if (!base_id)
997 base_id = 1; 1105 base_id = 1;
998 1106
999 // we currently need a base-font, no matter what 1107 // we currently need a base-font, no matter what
1000 if (fonts.size () <= base_id || !realize_font (base_id)) 1108 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1001 { 1109 {
1110 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1002 add_fonts ("fixed"); 1111 add_fonts ("fixed");
1003 base_id = fonts.size () - 1; 1112 base_id = fonts.size () - 1;
1004 } 1113 }
1005 1114
1006 if (fonts.size () <= base_id || !realize_font (base_id)) 1115 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1007 { 1116 return false;
1008 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1009 exit (1);
1010 }
1011 1117
1012 base_prop = fonts[base_id]->properties (); 1118 base_prop = fonts[base_id]->properties ();
1119
1120 return true;
1013} 1121}
1014 1122
1015int 1123int
1016rxvt_fontset::find_font (uint32_t unicode) 1124rxvt_fontset::find_font (uint32_t unicode)
1017{ 1125{
1018 for (int i = 0; i < fonts.size (); i++) 1126 for (unsigned int i = 0; i < fonts.size (); i++)
1019 { 1127 {
1020 rxvt_font *f = fonts[i]; 1128 rxvt_font *f = fonts[i];
1021 1129
1022 if (!f->loaded) 1130 if (!f->loaded)
1023 { 1131 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines