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.2 by pcg, Tue Nov 25 15:25:17 2003 UTC vs.
Revision 1.16 by pcg, Sat Jan 31 00:20:21 2004 UTC

21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib>
27
26#define DISPLAY r->Xdisplay 28#define DISPLAY r->Xdisplay
27#define DRAWABLE r->TermWin.vt 29#define DRAWABLE r->TermWin.vt
28#define GC r->TermWin.gc 30#define GC r->TermWin.gc
29 31
30const struct rxvt_fallback_font { 32const struct rxvt_fallback_font {
57 59
58 // japanese 60 // japanese
59#if ENCODING_JP || ENCODING_JP_EXT 61#if ENCODING_JP || ENCODING_JP_EXT
60# if XFT 62# if XFT
61 // prefer xft for complex scripts 63 // prefer xft for complex scripts
62 { CS_UNICODE, "xft:Kochi Gothic" }, 64 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
63# endif 65# endif
64 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 66 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
65 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 67 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
66 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 68 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
67#endif 69#endif
68 70
69#if ENCODING_CN || ENCODING_CN_EXT 71#if ENCODING_CN || ENCODING_CN_EXT
70# if XFT 72# if XFT
71 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
72 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
73 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
74 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
75# endif 77# endif
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
76 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 79 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
77 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 80 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
78 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 81 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
79 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 82 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
80 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 83 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
81 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 84 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
82 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 85 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
83 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 86 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
84#endif 87#endif
85 88
86#if XFT 89#if XFT
87 { CS_UNICODE, "xft:Andale Mono" }, 90 { CS_UNICODE, "xft:Andale Mono" },
88 { CS_UNICODE, "xft:Arial Unicode MS" }, 91 { CS_UNICODE, "xft:Arial Unicode MS" },
89#endif 92#endif
90 { 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" },
91 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 97 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
94 98
95 { CS_UNKNOWN, 0 } 99 { CS_UNKNOWN, 0 }
96}; 100};
97 101
98///////////////////////////////////////////////////////////////////////////// 102/////////////////////////////////////////////////////////////////////////////
99 103
100static void *enc_buf; 104static void *enc_buf;
101static uint32_t enc_len; 105static uint32_t enc_len;
102 106
103static inline void * 107static inline void *
104get_enc_buf (int len) 108get_enc_buf (uint32_t len)
105{ 109{
106 if (len > enc_len) 110 if (len > enc_len)
107 { 111 {
108 free (enc_buf); 112 free (enc_buf);
109 enc_buf = malloc (len); 113 enc_buf = malloc (len);
111 115
112 return enc_buf; 116 return enc_buf;
113} 117}
114 118
115static const char * 119static const char *
116enc_char (const text_t *text, int len, codeset cs, bool &zero) 120enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
117{ 121{
118 uint8_t *buf = (uint8_t *)get_enc_buf (len); 122 uint8_t *buf = (uint8_t *)get_enc_buf (len);
119 123
120 while (len--) 124 while (len--)
121 { 125 {
132 136
133 return (const char *)enc_buf; 137 return (const char *)enc_buf;
134} 138}
135 139
136static const XChar2b * 140static const XChar2b *
137enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 141enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
138{ 142{
139 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 143 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
140 144
141 while (len--) 145 while (len--)
142 { 146 {
168 XSetForeground (DISPLAY, GC, r->PixColors[color]); 172 XSetForeground (DISPLAY, GC, r->PixColors[color]);
169 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 173 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h);
170 } 174 }
171} 175}
172 176
177static const char *linedraw_cmds[128] = {
178 "1hH", "2hH", "1vV", "2vV",
179 0, 0, 0, 0,
180 0, 0, 0, 0,
181 "1HV", "2H1V", "1H2V", "2HV",
182
183 // 2510
184 "1hV", "2h1V", "1h2V", "2hV",
185 "1Hv", "2H1v", "1H2v", "2Hv",
186 "1hv", "2h1v", "1h2v", "2hv",
187 "1HvV", "2H1vV", "1HV2v", "1Hv2V",
188
189 // 2520
190 "1H2vV", "2Hv1V", "2HV1v", "2HvV",
191 "1hvV", "2h1vV", "1hV2v", "1hv2V",
192 "1h2vV", "2hv1V", "1v2hV", "2hvV",
193 "1hHV", "2h1HV", "2H1hV", "2hH1V",
194
195 // 2530
196 "1hH2V", "2hV1H", "1h2HV", "2hHV",
197 "1hHv", "1vH2h", "1hv2H", "1v2hH",
198 "1hH2v", "1H2hv", "1h2Hv", "2hHv",
199 "1hHvV", "1vVH2h", "1hvV2H", "1vV2hH",
200
201 // 2540
202 "1hHV2v", "1hHv2V", "1hH2vV", "1HV2hv",
203 "1hV2Hv", "1Hv2hV", "1hv2HV", "1V2hHv",
204 "1v2hHV", "1H2hvV", "1h2HvV", "2hHvV",
205 0, 0, 0, 0,
206
207 // 2550
208 0, 0, 0, 0,
209 0, 0, 0, 0,
210 0, 0, 0, 0,
211 0, 0, 0, 0,
212
213 // 2560
214 0, 0, 0, 0,
215 0, 0, 0, 0,
216 0, 0, 0, 0,
217 0, 0, 0, 0,
218
219 // 2570
220 0, "1a", "1b", "1ab",
221 "1h", "1v", "1H", "1V",
222 "2h", "2v", "2H", "2V",
223 "1h2H", "1v2V", "1H2h", "1V2v"
224
225 // to be done
226};
227
173struct rxvt_font_default : rxvt_font { 228struct rxvt_font_default : rxvt_font {
174 bool load (int maxheight) 229 rxvt_fontprop properties ()
230 {
231 rxvt_fontprop p;
232
233 p.width = p.height = 1;
234 p.weight = rxvt_fontprop::medium;
235 p.slant = rxvt_fontprop::roman;
236
237 return p;
238 }
239
240 bool load (const rxvt_fontprop &prop)
175 { 241 {
176 width = 1; height = 1; 242 width = 1; height = 1;
177 ascent = 1; descent = 0; 243 ascent = 1; descent = 0;
178 244
179 return true; 245 return true;
180 } 246 }
181 247
182 bool has_codepoint (uint32_t unicode) 248 bool has_codepoint (uint32_t unicode)
183 { 249 {
184 if (unicode <= 0x001f 250 if (unicode <= 0x001f)
251 return true;
185 || (unicode >= 0x80 && unicode <= 0x9f)) 252 if (unicode >= 0x0080 && unicode <= 0x009f)
253 return true;
254
255 if (unicode >= 0x2500 && unicode <= 0x257f
256 && linedraw_cmds[unicode - 0x2500])
186 return true; 257 return true;
187 258
188 switch (unicode) 259 switch (unicode)
189 { 260 {
190 case ZERO_WIDTH_CHAR: 261 case ZERO_WIDTH_CHAR:
204 const text_t *text, int len, 275 const text_t *text, int len,
205 int fg, int bg) 276 int fg, int bg)
206{ 277{
207 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 278 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
208 279
280 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
281
209 while (len--) 282 while (len--)
210 { 283 {
284 text_t t = *text++;
285
286 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
287 {
288 const char *p = linedraw_cmds[t - 0x2500];
289
290 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ;
291 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight;
292
293 XGCValues gcv;
294
295 while (*p)
296 {
297 switch (*p++)
298 {
299 case '1':
300 gcv.line_width = 0;
301 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
302 break;
303
304 case '2':
305 gcv.line_width = 2;
306 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
307 break;
308
309 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break;
310 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break;
311 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break;
312 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break;
313 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break;
314 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break;
315 }
316 }
317
318 gcv.line_width = 0;
319 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
320 }
321 else
211 switch (*text++) 322 switch (*text++)
212 { 323 {
213 case NOCHAR: 324 case NOCHAR:
214 case ZERO_WIDTH_CHAR: 325 case ZERO_WIDTH_CHAR:
215 break; 326 break;
216 default: 327 default:
217 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
218 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 328 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
219 } 329 }
220 330
221 x += r->TermWin.fwidth; 331 x += r->TermWin.fwidth;
222 } 332 }
223} 333}
224 334
227struct rxvt_font_x11 : rxvt_font { 337struct rxvt_font_x11 : rxvt_font {
228 rxvt_font_x11 () { f = 0; } 338 rxvt_font_x11 () { f = 0; }
229 339
230 void clear (); 340 void clear ();
231 341
232 bool load (int maxheight); 342 rxvt_fontprop properties ();
343
344 bool load (const rxvt_fontprop &prop);
233 345
234 bool has_codepoint (uint32_t unicode); 346 bool has_codepoint (uint32_t unicode);
235 347
236 void draw (int x, int y, 348 void draw (int x, int y,
237 const text_t *text, int len, 349 const text_t *text, int len,
239 351
240 XFontStruct *f; 352 XFontStruct *f;
241 codeset cs; 353 codeset cs;
242 bool enc2b, encm; 354 bool enc2b, encm;
243 355
244 const char *get_property (const char *property, const char *repl) const; 356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
357 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
358 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
359 bool set_properties (rxvt_fontprop &p, const char *name);
245}; 360};
246 361
247const char * 362const char *
248rxvt_font_x11::get_property (const char *property, const char *repl) const 363rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
249{ 364{
250 unsigned long value; 365 unsigned long value;
251 366
252 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 367 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
253 return XGetAtomName (DISPLAY, value); 368 return XGetAtomName (DISPLAY, value);
254 else 369 else
255 return repl; 370 return repl;
256} 371}
257 372
373rxvt_fontprop
374rxvt_font_x11::properties ()
375{
376 rxvt_fontprop p;
377 set_properties (p, f);
378 return p;
379}
380
258bool 381bool
259rxvt_font_x11::load (int maxheight) 382rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
383{
384 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
385 p.height = height;
386 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
387 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
388
389 return true;
390}
391
392bool
393rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
394{
395 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
396 const char *slant = get_property (f, "SLANT", "r");
397
398 unsigned long height;
399 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
400 return false;
401
402 unsigned long avgwidth;
403 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
404 avgwidth = 0;
405
406 return set_properties (p, height, weight, slant, avgwidth);
407}
408
409bool
410rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
411{
412 int slashes = 0;
413 const char *comp[12];
414
415 for (const char *c = name; *c; c++)
416 if (*c == '-')
417 {
418 comp[slashes++] = c + 1;
419 if (slashes >= 13)
420 break;
421 }
422
423 /* can we short-circuit the costly XLoadQueryFont? */
424 if (slashes >= 13
425 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
426 && (*comp[11] >= '0' && *comp[11] <= '9'))
427 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
428
429 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
430
431 if (f)
432 {
433 // the font should really exists now. if not, we have a problem
434 // (e.g. if the user did xset fp rehash just when we were searching fonts).
435 // in that case, just return garbage.
436 bool ret = set_properties (p, f);
437 XFreeFont (DISPLAY, f);
438 return ret;
439 }
440 else
441 return false;
442}
443
444// fix the size of scalable fonts
445static void
446fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
447{
448 int slashes = 0;
449 const char *size;
450
451 for (const char *c = name; *c; c++)
452 if (*c == '-')
453 {
454 if (slashes == 6)
455 size = c + 1;
456
457 if (++slashes >= 13)
458 break;
459 }
460
461 if (slashes >= 13 && size[0] == '0')
462 {
463 strncpy (buf, name, size - name);
464 buf += size - name;
465 buf += sprintf (buf, "%d", prop.height);
466 strcpy (buf, size + 1);
467 }
468 else
469 strcpy (buf, name);
470}
471
472bool
473rxvt_font_x11::load (const rxvt_fontprop &prop)
260{ 474{
261 clear (); 475 clear ();
476
477 char **list;
478 int count;
479 list = XListFonts (DISPLAY, name, 512, &count);
480 set_name (0);
481
482 if (!list)
483 return false;
484
485 int bestdiff = 0x7fffffff;
486 for (int i = 0; i < count; i++)
487 {
488 rxvt_fontprop p;
489 char fname[1024];
490 fix_scalable (fname, list[i], prop);
491
492 if (!set_properties (p, fname))
493 continue;
494
495 if (p.height > prop.height) // weed out too large fonts
496 continue;
497
498 int diff = (prop.height - p.height) * 32
499 + abs (prop.weight - p.weight)
500 + abs (prop.slant - p.slant );
501
502 if (!name // compare against best found so far
503 || diff < bestdiff)
504 {
505 set_name (strdup (fname));
506 bestdiff = diff;
507 }
508 }
509
510 XFreeFontNames (list);
511
512 if (!name)
513 return false;
262 514
263 f = XLoadQueryFont (DISPLAY, name); 515 f = XLoadQueryFont (DISPLAY, name);
264 516
265 if (!f) 517 if (!f)
266 return false; 518 return false;
267 519
268 unsigned long value;
269
270 const char *registry = get_property ("CHARSET_REGISTRY", 0); 520 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
271 const char *encoding = get_property ("CHARSET_ENCODING", 0); 521 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
272 522
273 if (registry && encoding) 523 if (registry && encoding)
274 { 524 {
275 char charset[64]; 525 char charset[64];
276 snprintf (charset, 64, "%s-%s", registry, encoding); 526 snprintf (charset, 64, "%s-%s", registry, encoding);
277 527
278 cs = codeset_from_name (charset); 528 cs = codeset_from_name (charset);
279 } 529 }
280 else 530 else
281 { 531 {
282 const char *charset = get_property ("FONT", 0); 532 const char *charset = get_property (f, "FONT", 0);
283 533
284 if (!charset) 534 if (!charset)
285 charset = name; 535 charset = name;
286 536
287 int count = 13; 537 int count = 13;
300 550
301 ascent = f->ascent; 551 ascent = f->ascent;
302 descent = f->descent; 552 descent = f->descent;
303 height = ascent + descent; 553 height = ascent + descent;
304 554
305 prop = false; 555 slow = false;
306 556
307 if (f->min_bounds.width == f->max_bounds.width) 557 if (f->min_bounds.width == f->max_bounds.width)
308 width = f->min_bounds.width; 558 width = f->min_bounds.width;
309 else if (f->per_char == NULL) 559 else if (f->per_char == NULL)
310 width = f->max_bounds.width; 560 width = f->max_bounds.width;
311 else 561 else
312 { 562 {
313 prop = true; 563 slow = true;
314 564
315 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 565 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
316 566
317 if (encm) 567 if (encm)
318 N += (f->max_byte1 - f->min_byte1) 568 N += (f->max_byte1 - f->min_byte1)
360 /* check wether the character exists in _this_ font. horrible. */ 610 /* check wether the character exists in _this_ font. horrible. */
361 XCharStruct *xcs; 611 XCharStruct *xcs;
362 612
363 if (encm) 613 if (encm)
364 { 614 {
365 int byte1 = ch >> 8; 615 unsigned char byte1 = ch >> 8;
366 int byte2 = ch & 255; 616 unsigned char byte2 = ch & 255;
367 617
368 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 618 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
369 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 619 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
370 return false; 620 return false;
371 621
403 // this looks like a mess /. 653 // this looks like a mess /.
404 // and it is a mess /. 654 // and it is a mess /.
405 // yet we are trying to be perfect /. 655 // yet we are trying to be perfect /.
406 // but the result still isn't perfect /. 656 // but the result still isn't perfect /.
407 657
408 bool slow = prop 658 bool slow = this->slow
409 || width != r->TermWin.fwidth 659 || width != r->TermWin.fwidth
410 || height != r->TermWin.fheight; 660 || height != r->TermWin.fheight;
411 661
412 int base = r->TermWin.fbase; 662 int base = r->TermWin.fbase;
413 663
488//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 738//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
489#define SWATHBITS 8 739#define SWATHBITS 8
490#endif 740#endif
491 741
492struct rxvt_font_xft : rxvt_font { 742struct rxvt_font_xft : rxvt_font {
493#if 0
494 enum {
495 SWATHCOUNT = 1 << (21 - UNIBITS),
496 SWATHSIZE = 1 << (SWATHBITS - 5)
497 };
498 typedef uint32_t swath[SWATHSIZE];
499
500 swath *cvr[SWATHCOUNT];
501#endif
502
503#if 0
504 void gen_coverage_swath (unsigned int page);
505
506 bool has_char (uint32_t ch)
507 {
508 unsigned int page = ch >> SWATHBITS;
509 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
510
511 if (page >= SWATHCOUNT)
512 return false;
513
514 if (!cvr[page]) gen_coverage_swath (page);
515
516 return cvr[page][idx >> 5] & (1 << (idx & 31));
517 }
518#endif
519 rxvt_font_xft () { f = 0; d = 0; } 743 rxvt_font_xft () { f = 0; d = 0; }
520 744
521 void clear (); 745 void clear ();
522 746
523 bool load (int maxheight); 747 rxvt_fontprop properties ();
748
749 bool load (const rxvt_fontprop &prop);
524 750
525 void draw (int x, int y, 751 void draw (int x, int y,
526 const text_t *text, int len, 752 const text_t *text, int len,
527 int fg, int bg); 753 int fg, int bg);
528 754
529 bool has_codepoint (uint32_t unicode); 755 bool has_codepoint (uint32_t unicode);
530 756
531protected: 757protected:
532 XftFont *f; 758 XftFont *f;
533 XftDraw *d; 759 XftDraw *d;
534
535#if 0
536 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
537 void set_swath (uint32_t ch)
538 {
539 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
540 }
541#endif
542}; 760};
543 761
544void 762void
545rxvt_font_xft::clear () 763rxvt_font_xft::clear ()
546{ 764{
553 if (d) 771 if (d)
554 { 772 {
555 XftDrawDestroy (d); 773 XftDrawDestroy (d);
556 d = 0; 774 d = 0;
557 } 775 }
776}
558 777
559#if 0 778rxvt_fontprop
560 for (int i = 0; i < SWATHCOUNT; i++) 779rxvt_font_xft::properties ()
561 delete cvr[i]; 780{
562#endif 781 rxvt_fontprop p;
782
783 FT_Face face = XftLockFace (f);
784
785 p.width = width; p.height = height;
786 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
787 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
788
789 XftUnlockFace (f);
790
791 return p;
563} 792}
564 793
565bool 794bool
566rxvt_font_xft::load (int maxheight) 795rxvt_font_xft::load (const rxvt_fontprop &prop)
567{ 796{
568#if 0 797#if 0
569 for (int i = 0; i < SWATHCOUNT; i++) 798 for (int i = 0; i < SWATHCOUNT; i++)
570 cvr[i] = 0; 799 cvr[i] = 0;
571#endif 800#endif
572 801
573 clear (); 802 clear ();
574 803
575 f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name); 804 FcPattern *p = FcNameParse ((FcChar8 *) name);
805
806 if (!p)
807 return false;
808
809 FcValue v;
810
811 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
812 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
813
814 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
815 FcPatternAddInteger (p, FC_SLANT, prop.slant);
816
817#if 0 // clipping unfortunately destroys our precious double-width-characters
818 // clip width, we can't do better, or can we?
819 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
820 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
821#endif
822
823 //FcPatternAddBool (p, FC_MINSPACE, 1);
824
825 XftResult result;
826 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
827
828 FcPatternDestroy (p);
829
830 if (!match)
831 return false;
832
833 f = XftFontOpenPattern (DISPLAY, match);
576 834
577 if (!f) 835 if (!f)
836 {
837 FcPatternDestroy (match);
578 return false; 838 return false;
839 }
579 840
580 FT_Face face = XftLockFace (f); 841 FT_Face face = XftLockFace (f);
581 842
582 prop = !FT_IS_FIXED_WIDTH (face); 843 slow = !FT_IS_FIXED_WIDTH (face);
583 844
584 int ftheight = 0; 845 int ftheight = 0;
585 846
586 for (;;) 847 for (;;)
587 { 848 {
589 FcChar8 c; 850 FcChar8 c;
590 851
591 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 852 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
592 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 853 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
593 854
594 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font 855 if (g1.xOff != g2.xOff) // don't simply trust the font
856 slow = true;
595 857
596 width = g2.xOff; 858 width = g2.xOff;
597 ascent = (face->size->metrics.ascender + 63) >> 6; 859 ascent = (face->size->metrics.ascender + 63) >> 6;
598 descent = (-face->size->metrics.descender + 63) >> 6; 860 descent = (-face->size->metrics.descender + 63) >> 6;
599 height = ascent + descent; 861 height = ascent + descent;
600 862
601 if (height <= maxheight || !maxheight) 863 if (height <= prop.height || !prop.height)
602 break; 864 break;
603 865
604 if (ftheight) 866 if (ftheight)
605 { 867 {
606 // take smaller steps near the end 868 // take smaller steps near the end
607 if (height > maxheight + 1) ftheight++; 869 if (height > prop.height + 1) ftheight++;
608 if (height > maxheight + 2) ftheight++; 870 if (height > prop.height + 2) ftheight++;
609 if (height > maxheight + 3) ftheight++; 871 if (height > prop.height + 3) ftheight++;
610 872
611 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 873 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
612 } 874 }
613 else 875 else
614 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 876 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
615 } 877 }
616 878
617 XftUnlockFace (f); 879 XftUnlockFace (f);
618 880
619 return true; 881 return true;
620} 882}
621
622#if 0
623void rxvt_font::gen_coverage_swath (unsigned int page)
624{
625 cvr[page] = new swath;
626
627 for (int i = 0; i < SWATHSIZE; i++)
628 cvr[page][i] = 0;
629
630 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
631}
632#endif
633 883
634bool 884bool
635rxvt_font_xft::has_codepoint (uint32_t unicode) 885rxvt_font_xft::has_codepoint (uint32_t unicode)
636{ 886{
637 return XftCharExists (DISPLAY, f, unicode); 887 return XftCharExists (DISPLAY, f, unicode);
641rxvt_font_xft::draw (int x, int y, 891rxvt_font_xft::draw (int x, int y,
642 const text_t *text, int len, 892 const text_t *text, int len,
643 int fg, int bg) 893 int fg, int bg)
644{ 894{
645 if (!d) 895 if (!d)
646 {
647 dR;
648 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP); 896 d = XftDrawCreate (DISPLAY, DRAWABLE, r->Xvisual, r->Xcmap);
649 }
650 897
651 if (bg >= 0 && bg != Color_bg) 898 if (bg >= 0 && bg != Color_bg)
652 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); 899 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
653 else 900 else
654 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 901 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
655 902
656 if (!prop && width == r->TermWin.fwidth) 903 if (!slow && width == r->TermWin.fwidth && 0)
657 { 904 {
658 if (sizeof (text_t) == sizeof (FcChar16)) 905 if (sizeof (text_t) == sizeof (FcChar16))
659 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 906 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
660 else 907 else
661 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 908 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
664 { 911 {
665 while (len) 912 while (len)
666 { 913 {
667 if (*text != NOCHAR && *text != ' ') 914 if (*text != NOCHAR && *text != ' ')
668 { 915 {
916 int fwidth = r->TermWin.fwidth;
917 if (len >= 2 && text[1] == NOCHAR)
918 fwidth *= 2;
919
920 XGlyphInfo extents;
669 if (sizeof (text_t) == sizeof (FcChar16)) 921 if (sizeof (text_t) == sizeof (FcChar16))
670 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 922 {
923 XftTextExtents16 (DISPLAY, f, (const FcChar16 *)text, 1, &extents);
924 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
925 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
926 }
671 else 927 else
672 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 928 {
929 XGlyphInfo extents;
930 XftTextExtents32 (DISPLAY, f, (const FcChar32 *)text, 1, &extents);
931 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
932 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
933 }
673 } 934 }
674 935
675 x += r->TermWin.fwidth; 936 x += r->TermWin.fwidth;
676 text++; 937 text++;
677 len--; 938 len--;
681#endif 942#endif
682 943
683///////////////////////////////////////////////////////////////////////////// 944/////////////////////////////////////////////////////////////////////////////
684 945
685rxvt_fontset::rxvt_fontset (rxvt_t r) 946rxvt_fontset::rxvt_fontset (rxvt_t r)
686#ifdef EXPLICIT_CONTEXT
687: r(r) 947: r(r)
688#endif
689{ 948{
690 clear (); 949 clear ();
691} 950}
692 951
693rxvt_fontset::~rxvt_fontset () 952rxvt_fontset::~rxvt_fontset ()
701 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 960 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
702 FONT_UNREF (*i); 961 FONT_UNREF (*i);
703 962
704 fonts.clear (); 963 fonts.clear ();
705 base_id = 0; 964 base_id = 0;
706 height = 0x7fffffff; 965 base_prop.height = 0x7fffffff;
966 base_prop.weight = rxvt_fontprop::medium;
967 base_prop.slant = rxvt_fontprop::roman;
707 968
708 fallback = fallback_fonts; 969 fallback = fallback_fonts;
709} 970}
710 971
711rxvt_font * 972rxvt_font *
758 1019
759 if (*desc == '[') 1020 if (*desc == '[')
760 { 1021 {
761 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1022 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
762 1023
763 const char *extra = desc++; 1024 //const char *extra = desc++; // not yet used
764 1025
765 desc = strchr (desc, ']'); 1026 desc = strchr (desc, ']');
766 1027
767 if (!desc) 1028 if (!desc)
768 { 1029 {
796rxvt_fontset::realize_font (int i) 1057rxvt_fontset::realize_font (int i)
797{ 1058{
798 if (fonts[i]->loaded) 1059 if (fonts[i]->loaded)
799 return true; 1060 return true;
800 1061
801 if (fonts[i]->load (height))
802 return fonts[i]->loaded = true; 1062 fonts[i]->loaded = true;
803 1063
804 delete fonts[i]; 1064 if (!fonts[i]->load (base_prop))
805 fonts.erase (fonts.begin () + i); 1065 {
806 1066 fonts[i]->cs = CS_UNKNOWN;
807 return false; 1067 return false;
1068 }
1069
1070 return true;
808} 1071}
809 1072
810void 1073void
811rxvt_fontset::populate (const char *desc) 1074rxvt_fontset::populate (const char *desc)
812{ 1075{
819 1082
820 if (!base_id) 1083 if (!base_id)
821 base_id = 1; 1084 base_id = 1;
822 1085
823 // we currently need a base-font, no matter what 1086 // we currently need a base-font, no matter what
824 if (fonts.size () <= base_id) 1087 if ((int)fonts.size () <= base_id || !realize_font (base_id))
825 { 1088 {
1089 puts ("unable to load specified font(s), falling back to 'fixed'\n");
826 add_fonts ("fixed"); 1090 add_fonts ("fixed");
827 base_id = 1; 1091 base_id = fonts.size () - 1;
828 } 1092 }
829 1093
830 if (fonts.size () <= base_id || !realize_font (base_id)) 1094 if ((int)fonts.size () <= base_id || !realize_font (base_id))
831 { 1095 {
832 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); 1096 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
833 exit (1); 1097 exit (1);
834 } 1098 }
835 1099
836 height = fonts[base_id]->height; 1100 base_prop = fonts[base_id]->properties ();
837
838 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
839} 1101}
840 1102
841int 1103int
842rxvt_fontset::find_font (uint32_t unicode) 1104rxvt_fontset::find_font (uint32_t unicode)
843{ 1105{
844 for (int i = 0; i < fonts.size (); i++) 1106 for (unsigned int i = 0; i < fonts.size (); i++)
845 { 1107 {
846 rxvt_font *f = fonts[i]; 1108 rxvt_font *f = fonts[i];
847 1109
848 if (!f->loaded) 1110 if (!f->loaded)
849 { 1111 {
850 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1112 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
851 goto next_font; 1113 goto next_font;
852 1114
853 if (!realize_font (i)) 1115 if (!realize_font (i))
854 {
855 --i;
856 goto next_font; 1116 goto next_font;
857 } 1117 }
858 1118
859 //printf ("added font %s for %04lx\n", f->name, unicode);
860 }
861
862 if (f->has_codepoint (unicode)) 1119 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
863 return i; 1120 return i;
864 1121
865 next_font: 1122 next_font:
866 if (i == fonts.size () - 1 && fallback->name) 1123 if (i == fonts.size () - 1 && fallback->name)
867 { 1124 {
868 fonts.push_back (new_font (fallback->name, fallback->cs)); 1125 fonts.push_back (new_font (fallback->name, fallback->cs));
869 fallback++; 1126 fallback++;
1127 i = 0;
870 } 1128 }
871 } 1129 }
872 1130
873 return 0; /* we must return SOME font */ 1131 return 0; /* we must return SOME font */
874} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines