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.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.19 by pcg, Fri Feb 13 13:28:17 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->display->display
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 {
31 codeset cs; 33 codeset cs;
32 const char *name; 34 const char *name;
33} fallback_fonts[] = { 35} fallback_fonts[] = {
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 {
163{ 167{
164 if (color == Color_bg) 168 if (color == Color_bg)
165 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 169 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE);
166 else if (color >= 0) 170 else if (color >= 0)
167 { 171 {
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:
202void 273void
203rxvt_font_default::draw (int x, int y, 274rxvt_font_default::draw (int x, int y,
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);
279
280 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
208 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
225///////////////////////////////////////////////////////////////////////////// 335/////////////////////////////////////////////////////////////////////////////
226 336
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 exist 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
414 XGCValues v; 664 XGCValues v;
415 v.foreground = R->PixColors[fg]; 665 v.foreground = r->PixColors[fg];
416 v.background = R->PixColors[bg]; 666 v.background = r->PixColors[bg];
417 v.font = f->fid; 667 v.font = f->fid;
418 668
419 if (enc2b) 669 if (enc2b)
420 { 670 {
421 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 671 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
425 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 675 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
426 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 676 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
427 } 677 }
428 else 678 else
429 { 679 {
430 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 680 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
431 681
432 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 682 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
433 683
434 if (slow) 684 if (slow)
435 { 685 {
436 do 686 do
437 { 687 {
438 if (xc->byte1 || xc->byte2) 688 if (xc->byte1 || xc->byte2)
439 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 689 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
440 690
441 x += R->TermWin.fwidth; 691 x += r->TermWin.fwidth;
442 xc++; len--; 692 xc++; len--;
443 } 693 }
444 while (len); 694 while (len);
445 } 695 }
446 else 696 else
456 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 706 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
457 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 707 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
458 } 708 }
459 else 709 else
460 { 710 {
461 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 711 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
462 712
463 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 713 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
464 714
465 if (slow) 715 if (slow)
466 { 716 {
467 do 717 do
468 { 718 {
469 if (*xc) 719 if (*xc)
470 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 720 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
471 721
472 x += R->TermWin.fwidth; 722 x += r->TermWin.fwidth;
473 xc++; len--; 723 xc++; len--;
474 } 724 }
475 while (len); 725 while (len);
476 } 726 }
477 else 727 else
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{
547 if (f) 765 if (f)
548 { 766 {
549 XftFontClose (R->Xdisplay, f); 767 XftFontClose (DISPLAY, f);
550 f = 0; 768 f = 0;
551 } 769 }
552 770
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 (R->Xdisplay, DefaultScreen (R->Xdisplay), 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 {
588 XGlyphInfo g1, g2; 849 XGlyphInfo g1, g2;
589 FcChar8 c; 850 FcChar8 c;
590 851
591 c = 'i'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g1); 852 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
592 c = 'W'; XftTextExtents8 (R->Xdisplay, 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 (R->Xdisplay, f, unicode); 887 return XftCharExists (DISPLAY, f, unicode);
638} 888}
639 889
640void 890void
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 d = XftDrawCreate (R->Xdisplay, DRAWABLE, XVISUAL, XCMAP); 896 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
647 897
648 if (bg >= 0 && bg != Color_bg) 898 if (bg >= 0 && bg != Color_bg)
649 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);
650 else 900 else
651 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);
652 902
653 if (!prop && width == R->TermWin.fwidth) 903 if (!slow && width == r->TermWin.fwidth && 0)
654 { 904 {
655 if (sizeof (text_t) == sizeof (FcChar16)) 905 if (sizeof (text_t) == sizeof (FcChar16))
656 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);
657 else 907 else
658 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);
659 } 909 }
660 else 910 else
661 { 911 {
662 while (len) 912 while (len)
663 { 913 {
664 if (*text != NOCHAR && *text != ' ') 914 if (*text != NOCHAR && *text != ' ')
665 { 915 {
916 int fwidth = r->TermWin.fwidth;
917 if (len >= 2 && text[1] == NOCHAR)
918 fwidth *= 2;
919
920 XGlyphInfo extents;
666 if (sizeof (text_t) == sizeof (FcChar16)) 921 if (sizeof (text_t) == sizeof (FcChar16))
667 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 }
668 else 927 else
669 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 }
670 } 934 }
671 935
672 x += R->TermWin.fwidth; 936 x += r->TermWin.fwidth;
673 text++; 937 text++;
674 len--; 938 len--;
675 } 939 }
676 } 940 }
677} 941}
678#endif 942#endif
679 943
680///////////////////////////////////////////////////////////////////////////// 944/////////////////////////////////////////////////////////////////////////////
681 945
682rxvt_fontset::rxvt_fontset (pR) 946rxvt_fontset::rxvt_fontset (rxvt_t r)
683#ifdef EXPLICIT_CONTEXT 947: r (r)
684: rxvt_term(R)
685#endif
686{ 948{
687 clear (); 949 clear ();
688} 950}
689 951
690rxvt_fontset::~rxvt_fontset () 952rxvt_fontset::~rxvt_fontset ()
693} 955}
694 956
695void 957void
696rxvt_fontset::clear () 958rxvt_fontset::clear ()
697{ 959{
698 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 960 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
699 FONT_UNREF (*i); 961 FONT_UNREF (*i);
700 962
701 fonts.clear (); 963 fonts.clear ();
702 base_id = 0; 964 base_id = 0;
703 height = 0x7fffffff; 965 base_prop.height = 0x7fffffff;
966 base_prop.weight = rxvt_fontprop::medium;
967 base_prop.slant = rxvt_fontprop::roman;
704 968
705 fallback = fallback_fonts; 969 fallback = fallback_fonts;
706} 970}
707 971
708rxvt_font * 972rxvt_font *
728 f = new rxvt_font_x11; 992 f = new rxvt_font_x11;
729 } 993 }
730 else 994 else
731 f = new rxvt_font_x11; 995 f = new rxvt_font_x11;
732 996
733 f->set_term (aR); 997 f->set_term (r);
734 f->set_name (strdup (name)); 998 f->set_name (strdup (name));
735 999
736 f->cs = cs; 1000 f->cs = cs;
737 f->loaded = false; 1001 f->loaded = false;
738 1002
755 1019
756 if (*desc == '[') 1020 if (*desc == '[')
757 { 1021 {
758 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1022 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
759 1023
760 const char *extra = desc++; 1024 //const char *extra = desc++; // not yet used
761 1025
762 desc = strchr (desc, ']'); 1026 desc = strchr (desc, ']');
763 1027
764 if (!desc) 1028 if (!desc)
765 { 1029 {
793rxvt_fontset::realize_font (int i) 1057rxvt_fontset::realize_font (int i)
794{ 1058{
795 if (fonts[i]->loaded) 1059 if (fonts[i]->loaded)
796 return true; 1060 return true;
797 1061
798 if (fonts[i]->load (height))
799 return fonts[i]->loaded = true; 1062 fonts[i]->loaded = true;
800 1063
801 delete fonts[i]; 1064 if (!fonts[i]->load (base_prop))
802 fonts.erase (fonts.begin () + i); 1065 {
803 1066 fonts[i]->cs = CS_UNKNOWN;
804 return false; 1067 return false;
1068 }
1069
1070 return true;
805} 1071}
806 1072
807void 1073void
808rxvt_fontset::populate (const char *desc) 1074rxvt_fontset::populate (const char *desc)
809{ 1075{
816 1082
817 if (!base_id) 1083 if (!base_id)
818 base_id = 1; 1084 base_id = 1;
819 1085
820 // we currently need a base-font, no matter what 1086 // we currently need a base-font, no matter what
821 if (fonts.size () <= base_id) 1087 if ((int)fonts.size () <= base_id || !realize_font (base_id))
822 { 1088 {
1089 puts ("unable to load specified font (s), falling back to 'fixed'\n");
823 add_fonts ("fixed"); 1090 add_fonts ("fixed");
824 base_id = 1; 1091 base_id = fonts.size () - 1;
825 } 1092 }
826 1093
827 if (fonts.size () <= base_id || !realize_font (base_id)) 1094 if ((int)fonts.size () <= base_id || !realize_font (base_id))
828 { 1095 {
829 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");
830 exit (1); 1097 exit (1);
831 } 1098 }
832 1099
833 height = fonts[base_id]->height; 1100 base_prop = fonts[base_id]->properties ();
834
835 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
836} 1101}
837 1102
838int 1103int
839rxvt_fontset::find_font (uint32_t unicode) 1104rxvt_fontset::find_font (uint32_t unicode)
840{ 1105{
841 for (int i = 0; i < fonts.size (); i++) 1106 for (unsigned int i = 0; i < fonts.size (); i++)
842 { 1107 {
843 rxvt_font *f = fonts[i]; 1108 rxvt_font *f = fonts[i];
844 1109
845 if (!f->loaded) 1110 if (!f->loaded)
846 { 1111 {
847 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1112 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
848 goto next_font; 1113 goto next_font;
849 1114
850 if (!realize_font (i)) 1115 if (!realize_font (i))
851 {
852 --i;
853 goto next_font; 1116 goto next_font;
854 } 1117 }
855 1118
856 //printf ("added font %s for %04lx\n", f->name, unicode);
857 }
858
859 if (f->has_codepoint (unicode)) 1119 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
860 return i; 1120 return i;
861 1121
862 next_font: 1122 next_font:
863 if (i == fonts.size () - 1 && fallback->name) 1123 if (i == fonts.size () - 1 && fallback->name)
864 { 1124 {
865 fonts.push_back (new_font (fallback->name, fallback->cs)); 1125 fonts.push_back (new_font (fallback->name, fallback->cs));
866 fallback++; 1126 fallback++;
1127 i = 0;
867 } 1128 }
868 } 1129 }
869 1130
870 return 0; /* we must return SOME font */ 1131 return 0; /* we must return SOME font */
871} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines