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.10 by pcg, Wed Dec 24 09:07:01 2003 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 {
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
76 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-1" },
77 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 79 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-2" },
78 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 80 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-3" },
79 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 81 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-4" },
80 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 82 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-5" },
81 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 83 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-6" },
82 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 84 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-7" },
83 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 85 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-f" },
84#endif 86#endif
85 87
86#if XFT 88#if XFT
87 { CS_UNICODE, "xft:Andale Mono" }, 89 { CS_UNICODE, "xft:Andale Mono" },
88 { CS_UNICODE, "xft:Arial Unicode MS" }, 90 { CS_UNICODE, "xft:Arial Unicode MS" },
89#endif 91#endif
90 { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 92 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "xft:FreeMono" },
94 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
95 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
91 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 96 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
94 97
95 { CS_UNKNOWN, 0 } 98 { CS_UNKNOWN, 0 }
96}; 99};
97 100
98///////////////////////////////////////////////////////////////////////////// 101/////////////////////////////////////////////////////////////////////////////
163{ 166{
164 if (color == Color_bg) 167 if (color == Color_bg)
165 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 168 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE);
166 else if (color >= 0) 169 else if (color >= 0)
167 { 170 {
168 XSetForeground (DISPLAY, GC, R->PixColors[color]); 171 XSetForeground (DISPLAY, GC, r->PixColors[color]);
169 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 172 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h);
170 } 173 }
171} 174}
172 175
176static const char *linedraw_cmds[128] = {
177 "1hH", "2hH", "1vV", "2vV",
178 0, 0, 0, 0,
179 0, 0, 0, 0,
180 "1HV", "2H1V", "1H2V", "2HV",
181
182 // 2510
183 "1hV", "2h1V", "1h2V", "2hV",
184 "1Hv", "2H1v", "1H2v", "2Hv",
185 "1hv", "2h1v", "1h2v", "2hv",
186 "1HvV", "2H1vV", "1HV2v", "1Hv2V",
187
188 // 2520
189 "1H2vV", "2Hv1V", "2HV1v", "2HvV",
190 "1hvV", "2h1vV", "1hV2v", "1hv2V",
191 "1h2vV", "2hv1V", "1v2hV", "2hvV",
192 "1hHV", "2h1HV", "2H1hV", "2hH1V",
193
194 // 2530
195 "1hH2V", "2hV1H", "1h2HV", "2hHV",
196 "1hHv", "1vH2h", "1hv2H", "1v2hH",
197 "1hH2v", "1H2hv", "1h2Hv", "2hHv",
198 "1hHvV", "1vVH2h", "1hvV2H", "1vV2hH",
199
200 // 2540
201 "1hHV2v", "1hHv2V", "1hH2vV", "1HV2hv",
202 "1hV2Hv", "1Hv2hV", "1hv2HV", "1V2hHv",
203 "1v2hHV", "1H2hvV", "1h2HvV", "2hHvV",
204 0, 0, 0, 0,
205
206 // 2550
207 0, 0, 0, 0,
208 0, 0, 0, 0,
209 0, 0, 0, 0,
210 0, 0, 0, 0,
211
212 // 2560
213 0, 0, 0, 0,
214 0, 0, 0, 0,
215 0, 0, 0, 0,
216 0, 0, 0, 0,
217
218 // 2570
219 0, "1a", "1b", "1ab",
220 "1h", "1v", "1H", "1V",
221 "2h", "2v", "2H", "2V",
222 "1h2H", "1v2V", "1H2h", "1V2v"
223
224 // to be done
225};
226
173struct rxvt_font_default : rxvt_font { 227struct rxvt_font_default : rxvt_font {
174 bool load (int maxheight) 228 rxvt_fontprop properties ()
229 {
230 rxvt_fontprop p;
231
232 p.width = p.height = 1;
233 p.weight = rxvt_fontprop::medium;
234 p.slant = rxvt_fontprop::roman;
235
236 return p;
237 }
238
239 bool load (const rxvt_fontprop &prop)
175 { 240 {
176 width = 1; height = 1; 241 width = 1; height = 1;
177 ascent = 1; descent = 0; 242 ascent = 1; descent = 0;
178 243
179 return true; 244 return true;
180 } 245 }
181 246
182 bool has_codepoint (uint32_t unicode) 247 bool has_codepoint (uint32_t unicode)
183 { 248 {
184 if (unicode <= 0x001f 249 if (unicode <= 0x001f)
250 return true;
185 || (unicode >= 0x80 && unicode <= 0x9f)) 251 if (unicode >= 0x0080 && unicode <= 0x009f)
252 return true;
253
254 if (unicode >= 0x2500 && unicode <= 0x257f
255 && linedraw_cmds[unicode - 0x2500])
186 return true; 256 return true;
187 257
188 switch (unicode) 258 switch (unicode)
189 { 259 {
190 case ZERO_WIDTH_CHAR: 260 case ZERO_WIDTH_CHAR:
202void 272void
203rxvt_font_default::draw (int x, int y, 273rxvt_font_default::draw (int x, int y,
204 const text_t *text, int len, 274 const text_t *text, int len,
205 int fg, int bg) 275 int fg, int bg)
206{ 276{
207 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 277 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
278
279 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
208 280
209 while (len--) 281 while (len--)
210 { 282 {
283 text_t t = *text++;
284
285 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
286 {
287 const char *p = linedraw_cmds[t - 0x2500];
288
289 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ;
290 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight;
291
292 XGCValues gcv;
293
294 while (*p)
295 {
296 switch (*p++)
297 {
298 case '1':
299 gcv.line_width = 0;
300 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
301 break;
302
303 case '2':
304 gcv.line_width = 2;
305 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
306 break;
307
308 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break;
309 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break;
310 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break;
311 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break;
312 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break;
313 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break;
314 }
315 }
316
317 gcv.line_width = 0;
318 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv);
319 }
320 else
211 switch (*text++) 321 switch (*text++)
212 { 322 {
213 case NOCHAR: 323 case NOCHAR:
214 case ZERO_WIDTH_CHAR: 324 case ZERO_WIDTH_CHAR:
215 break; 325 break;
216 default: 326 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); 327 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
219 } 328 }
220 329
221 x += R->TermWin.fwidth; 330 x += r->TermWin.fwidth;
222 } 331 }
223} 332}
224 333
225///////////////////////////////////////////////////////////////////////////// 334/////////////////////////////////////////////////////////////////////////////
226 335
227struct rxvt_font_x11 : rxvt_font { 336struct rxvt_font_x11 : rxvt_font {
228 rxvt_font_x11 () { f = 0; } 337 rxvt_font_x11 () { f = 0; }
229 338
230 void clear (); 339 void clear ();
231 340
232 bool load (int maxheight); 341 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
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;
245}; 357};
246 358
247const char * 359const char *
248rxvt_font_x11::get_property (const char *property, const char *repl) const 360rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
249{ 361{
250 unsigned long value; 362 unsigned long value;
251 363
252 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 364 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
253 return XGetAtomName (DISPLAY, value); 365 return XGetAtomName (DISPLAY, value);
254 else 366 else
255 return repl; 367 return repl;
256} 368}
257 369
370rxvt_fontprop
371rxvt_font_x11::properties ()
372{
373 return properties (f);
374}
375
376rxvt_fontprop
377rxvt_font_x11::properties (XFontStruct *f)
378{
379 rxvt_fontprop p;
380
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r");
383
384 unsigned long avgwidth;
385 p.width = XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)
386 ? avgwidth / 10
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
392 return p;
393}
394
258bool 395bool
259rxvt_font_x11::load (int maxheight) 396rxvt_font_x11::load (const rxvt_fontprop &prop)
260{ 397{
261 clear (); 398 clear ();
399
400 char **list;
401 int count;
402 XFontStruct *info;
403 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info);
404
405 if (!list)
406 return false;
407
408 int bestdiff = 0x7fffffff;
409 XFontStruct *best = 0;
410 for (int i = 0; i < count; i++)
411 {
412 XFontStruct *f = info + i;
413
414 if (f->ascent + f->descent <= prop.height) // weed out too large fonts
415 {
416 rxvt_fontprop p = properties (f);
417 int diff = (prop.height - f->ascent + f->descent) * 32
418 + abs (prop.weight - p.weight)
419 + abs (prop.slant - p.slant );
420
421 if (!best // compare against best found so far
422 || diff < bestdiff)
423 {
424 best = f;
425 bestdiff = diff;
426 }
427 }
428 }
429
430 if (!best)
431 return false;
432
433 set_name (strdup (list[best - info]));
434
435 XFreeFontInfo (list, info, count);
262 436
263 f = XLoadQueryFont (DISPLAY, name); 437 f = XLoadQueryFont (DISPLAY, name);
264 438
265 if (!f) 439 if (!f)
266 return false; 440 return false;
267 441
268 unsigned long value; 442 unsigned long value;
269 443
270 const char *registry = get_property ("CHARSET_REGISTRY", 0); 444 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
271 const char *encoding = get_property ("CHARSET_ENCODING", 0); 445 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
272 446
273 if (registry && encoding) 447 if (registry && encoding)
274 { 448 {
275 char charset[64]; 449 char charset[64];
276 snprintf (charset, 64, "%s-%s", registry, encoding); 450 snprintf (charset, 64, "%s-%s", registry, encoding);
277 451
278 cs = codeset_from_name (charset); 452 cs = codeset_from_name (charset);
279 } 453 }
280 else 454 else
281 { 455 {
282 const char *charset = get_property ("FONT", 0); 456 const char *charset = get_property (f, "FONT", 0);
283 457
284 if (!charset) 458 if (!charset)
285 charset = name; 459 charset = name;
286 460
287 int count = 13; 461 int count = 13;
300 474
301 ascent = f->ascent; 475 ascent = f->ascent;
302 descent = f->descent; 476 descent = f->descent;
303 height = ascent + descent; 477 height = ascent + descent;
304 478
305 prop = false; 479 slow = false;
306 480
307 if (f->min_bounds.width == f->max_bounds.width) 481 if (f->min_bounds.width == f->max_bounds.width)
308 width = f->min_bounds.width; 482 width = f->min_bounds.width;
309 else if (f->per_char == NULL) 483 else if (f->per_char == NULL)
310 width = f->max_bounds.width; 484 width = f->max_bounds.width;
311 else 485 else
312 { 486 {
313 prop = true; 487 slow = true;
314 488
315 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 489 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
316 490
317 if (encm) 491 if (encm)
318 N += (f->max_byte1 - f->min_byte1) 492 N += (f->max_byte1 - f->min_byte1)
403 // this looks like a mess /. 577 // this looks like a mess /.
404 // and it is a mess /. 578 // and it is a mess /.
405 // yet we are trying to be perfect /. 579 // yet we are trying to be perfect /.
406 // but the result still isn't perfect /. 580 // but the result still isn't perfect /.
407 581
408 bool slow = prop 582 bool slow = this->slow
409 || width != R->TermWin.fwidth 583 || width != r->TermWin.fwidth
410 || height != R->TermWin.fheight; 584 || height != r->TermWin.fheight;
411 585
412 int base = R->TermWin.fbase; 586 int base = r->TermWin.fbase;
413 587
414 XGCValues v; 588 XGCValues v;
415 v.foreground = R->PixColors[fg]; 589 v.foreground = r->PixColors[fg];
416 v.background = R->PixColors[bg]; 590 v.background = r->PixColors[bg];
417 v.font = f->fid; 591 v.font = f->fid;
418 592
419 if (enc2b) 593 if (enc2b)
420 { 594 {
421 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 595 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
425 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 599 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
426 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 600 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
427 } 601 }
428 else 602 else
429 { 603 {
430 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 604 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
431 605
432 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 606 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
433 607
434 if (slow) 608 if (slow)
435 { 609 {
436 do 610 do
437 { 611 {
438 if (xc->byte1 || xc->byte2) 612 if (xc->byte1 || xc->byte2)
439 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 613 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
440 614
441 x += R->TermWin.fwidth; 615 x += r->TermWin.fwidth;
442 xc++; len--; 616 xc++; len--;
443 } 617 }
444 while (len); 618 while (len);
445 } 619 }
446 else 620 else
456 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 630 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
457 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 631 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
458 } 632 }
459 else 633 else
460 { 634 {
461 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 635 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
462 636
463 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 637 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
464 638
465 if (slow) 639 if (slow)
466 { 640 {
467 do 641 do
468 { 642 {
469 if (*xc) 643 if (*xc)
470 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 644 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
471 645
472 x += R->TermWin.fwidth; 646 x += r->TermWin.fwidth;
473 xc++; len--; 647 xc++; len--;
474 } 648 }
475 while (len); 649 while (len);
476 } 650 }
477 else 651 else
488//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 662//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
489#define SWATHBITS 8 663#define SWATHBITS 8
490#endif 664#endif
491 665
492struct rxvt_font_xft : rxvt_font { 666struct 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; } 667 rxvt_font_xft () { f = 0; d = 0; }
520 668
521 void clear (); 669 void clear ();
522 670
523 bool load (int maxheight); 671 rxvt_fontprop properties ();
672
673 bool load (const rxvt_fontprop &prop);
524 674
525 void draw (int x, int y, 675 void draw (int x, int y,
526 const text_t *text, int len, 676 const text_t *text, int len,
527 int fg, int bg); 677 int fg, int bg);
528 678
529 bool has_codepoint (uint32_t unicode); 679 bool has_codepoint (uint32_t unicode);
530 680
531protected: 681protected:
532 XftFont *f; 682 XftFont *f;
533 XftDraw *d; 683 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}; 684};
543 685
544void 686void
545rxvt_font_xft::clear () 687rxvt_font_xft::clear ()
546{ 688{
547 if (f) 689 if (f)
548 { 690 {
549 XftFontClose (R->Xdisplay, f); 691 XftFontClose (DISPLAY, f);
550 f = 0; 692 f = 0;
551 } 693 }
552 694
553 if (d) 695 if (d)
554 { 696 {
555 XftDrawDestroy (d); 697 XftDrawDestroy (d);
556 d = 0; 698 d = 0;
557 } 699 }
700}
558 701
559#if 0 702rxvt_fontprop
560 for (int i = 0; i < SWATHCOUNT; i++) 703rxvt_font_xft::properties ()
561 delete cvr[i]; 704{
562#endif 705 rxvt_fontprop p;
706
707 FT_Face face = XftLockFace (f);
708
709 p.width = width; p.height = height;
710 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
711 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
712
713 XftUnlockFace (f);
714
715 return p;
563} 716}
564 717
565bool 718bool
566rxvt_font_xft::load (int maxheight) 719rxvt_font_xft::load (const rxvt_fontprop &prop)
567{ 720{
568#if 0 721#if 0
569 for (int i = 0; i < SWATHCOUNT; i++) 722 for (int i = 0; i < SWATHCOUNT; i++)
570 cvr[i] = 0; 723 cvr[i] = 0;
571#endif 724#endif
572 725
573 clear (); 726 clear ();
574 727
575 f = XftFontOpenName (R->Xdisplay, DefaultScreen (R->Xdisplay), name); 728 FcPattern *p = FcNameParse ((FcChar8 *) name);
729
730 if (!p)
731 return false;
732
733 FcValue v;
734
735 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
736 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
737
738 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
739 FcPatternAddInteger (p, FC_SLANT, prop.slant);
740
741 // clip width, we can't do better, or can we?
742 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
743 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
744
745 //FcPatternAddBool (p, FC_MINSPACE, 1);
746
747 XftResult result;
748 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
749
750 FcPatternDestroy (p);
751
752 if (!match)
753 return false;
754
755 f = XftFontOpenPattern (DISPLAY, match);
576 756
577 if (!f) 757 if (!f)
758 {
759 FcPatternDestroy (match);
578 return false; 760 return false;
761 }
579 762
580 FT_Face face = XftLockFace (f); 763 FT_Face face = XftLockFace (f);
581 764
582 prop = !FT_IS_FIXED_WIDTH (face); 765 slow = !FT_IS_FIXED_WIDTH (face);
583 766
584 int ftheight = 0; 767 int ftheight = 0;
585 768
586 for (;;) 769 for (;;)
587 { 770 {
588 XGlyphInfo g1, g2; 771 XGlyphInfo g1, g2;
589 FcChar8 c; 772 FcChar8 c;
590 773
591 c = 'i'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g1); 774 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
592 c = 'W'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g2); 775 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
593 776
594 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font 777 if (g1.xOff != g2.xOff) // don't simply trust the font
778 slow = true;
595 779
596 width = g2.xOff; 780 width = g2.xOff;
597 ascent = (face->size->metrics.ascender + 63) >> 6; 781 ascent = (face->size->metrics.ascender + 63) >> 6;
598 descent = (-face->size->metrics.descender + 63) >> 6; 782 descent = (-face->size->metrics.descender + 63) >> 6;
599 height = ascent + descent; 783 height = ascent + descent;
600 784
601 if (height <= maxheight || !maxheight) 785 if (height <= prop.height || !prop.height)
602 break; 786 break;
603 787
604 if (ftheight) 788 if (ftheight)
605 { 789 {
606 // take smaller steps near the end 790 // take smaller steps near the end
607 if (height > maxheight + 1) ftheight++; 791 if (height > prop.height + 1) ftheight++;
608 if (height > maxheight + 2) ftheight++; 792 if (height > prop.height + 2) ftheight++;
609 if (height > maxheight + 3) ftheight++; 793 if (height > prop.height + 3) ftheight++;
610 794
611 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 795 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
612 } 796 }
613 else 797 else
614 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 798 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
615 } 799 }
616 800
617 XftUnlockFace (f); 801 XftUnlockFace (f);
618 802
619 return true; 803 return true;
620} 804}
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 805
634bool 806bool
635rxvt_font_xft::has_codepoint (uint32_t unicode) 807rxvt_font_xft::has_codepoint (uint32_t unicode)
636{ 808{
637 return XftCharExists (R->Xdisplay, f, unicode); 809 return XftCharExists (DISPLAY, f, unicode);
638} 810}
639 811
640void 812void
641rxvt_font_xft::draw (int x, int y, 813rxvt_font_xft::draw (int x, int y,
642 const text_t *text, int len, 814 const text_t *text, int len,
643 int fg, int bg) 815 int fg, int bg)
644{ 816{
645 if (!d) 817 if (!d)
818 {
819 dR;
646 d = XftDrawCreate (R->Xdisplay, DRAWABLE, XVISUAL, XCMAP); 820 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
821 }
647 822
648 if (bg >= 0 && bg != Color_bg) 823 if (bg >= 0 && bg != Color_bg)
649 XftDrawRect (d, &R->PixColors[bg].c, x, y, R->TermWin.fwidth * len, R->TermWin.fheight); 824 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
650 else 825 else
651 clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg); 826 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
652 827
653 if (!prop && width == R->TermWin.fwidth) 828 if (!slow && width == r->TermWin.fwidth)
654 { 829 {
655 if (sizeof (text_t) == sizeof (FcChar16)) 830 if (sizeof (text_t) == sizeof (FcChar16))
656 XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, len); 831 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
657 else 832 else
658 XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, len); 833 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
659 } 834 }
660 else 835 else
661 { 836 {
662 while (len) 837 while (len)
663 { 838 {
664 if (*text != NOCHAR && *text != ' ') 839 if (*text != NOCHAR && *text != ' ')
665 { 840 {
666 if (sizeof (text_t) == sizeof (FcChar16)) 841 if (sizeof (text_t) == sizeof (FcChar16))
667 XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, 1); 842 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1);
668 else 843 else
669 XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, 1); 844 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1);
670 } 845 }
671 846
672 x += R->TermWin.fwidth; 847 x += r->TermWin.fwidth;
673 text++; 848 text++;
674 len--; 849 len--;
675 } 850 }
676 } 851 }
677} 852}
678#endif 853#endif
679 854
680///////////////////////////////////////////////////////////////////////////// 855/////////////////////////////////////////////////////////////////////////////
681 856
682rxvt_fontset::rxvt_fontset (pR) 857rxvt_fontset::rxvt_fontset (rxvt_t r)
683#ifdef EXPLICIT_CONTEXT 858#ifdef EXPLICIT_CONTEXT
684: rxvt_term(R) 859: r(r)
685#endif 860#endif
686{ 861{
687 clear (); 862 clear ();
688} 863}
689 864
698 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 873 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
699 FONT_UNREF (*i); 874 FONT_UNREF (*i);
700 875
701 fonts.clear (); 876 fonts.clear ();
702 base_id = 0; 877 base_id = 0;
703 height = 0x7fffffff; 878 base_prop.height = 0x7fffffff;
879 base_prop.weight = rxvt_fontprop::medium;
880 base_prop.slant = rxvt_fontprop::roman;
704 881
705 fallback = fallback_fonts; 882 fallback = fallback_fonts;
706} 883}
707 884
708rxvt_font * 885rxvt_font *
728 f = new rxvt_font_x11; 905 f = new rxvt_font_x11;
729 } 906 }
730 else 907 else
731 f = new rxvt_font_x11; 908 f = new rxvt_font_x11;
732 909
733 f->set_term (aR); 910 f->set_term (r);
734 f->set_name (strdup (name)); 911 f->set_name (strdup (name));
735 912
736 f->cs = cs; 913 f->cs = cs;
737 f->loaded = false; 914 f->loaded = false;
738 915
793rxvt_fontset::realize_font (int i) 970rxvt_fontset::realize_font (int i)
794{ 971{
795 if (fonts[i]->loaded) 972 if (fonts[i]->loaded)
796 return true; 973 return true;
797 974
798 if (fonts[i]->load (height))
799 return fonts[i]->loaded = true; 975 fonts[i]->loaded = true;
800 976
801 delete fonts[i]; 977 if (!fonts[i]->load (base_prop))
802 fonts.erase (fonts.begin () + i); 978 {
803 979 fonts[i]->cs = CS_UNKNOWN;
804 return false; 980 return false;
981 }
982
983 return true;
805} 984}
806 985
807void 986void
808rxvt_fontset::populate (const char *desc) 987rxvt_fontset::populate (const char *desc)
809{ 988{
816 995
817 if (!base_id) 996 if (!base_id)
818 base_id = 1; 997 base_id = 1;
819 998
820 // we currently need a base-font, no matter what 999 // we currently need a base-font, no matter what
821 if (fonts.size () <= base_id) 1000 if (fonts.size () <= base_id || !realize_font (base_id))
822 { 1001 {
823 add_fonts ("fixed"); 1002 add_fonts ("fixed");
824 base_id = 1; 1003 base_id = fonts.size () - 1;
825 } 1004 }
826 1005
827 if (fonts.size () <= base_id || !realize_font (base_id)) 1006 if (fonts.size () <= base_id || !realize_font (base_id))
828 { 1007 {
829 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); 1008 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
830 exit (1); 1009 exit (1);
831 } 1010 }
832 1011
833 height = fonts[base_id]->height; 1012 base_prop = fonts[base_id]->properties ();
834
835 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
836} 1013}
837 1014
838int 1015int
839rxvt_fontset::find_font (uint32_t unicode) 1016rxvt_fontset::find_font (uint32_t unicode)
840{ 1017{
846 { 1023 {
847 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1024 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
848 goto next_font; 1025 goto next_font;
849 1026
850 if (!realize_font (i)) 1027 if (!realize_font (i))
851 {
852 --i;
853 goto next_font; 1028 goto next_font;
854 } 1029 }
855 1030
856 //printf ("added font %s for %04lx\n", f->name, unicode);
857 }
858
859 if (f->has_codepoint (unicode)) 1031 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
860 return i; 1032 return i;
861 1033
862 next_font: 1034 next_font:
863 if (i == fonts.size () - 1 && fallback->name) 1035 if (i == fonts.size () - 1 && fallback->name)
864 { 1036 {
865 fonts.push_back (new_font (fallback->name, fallback->cs)); 1037 fonts.push_back (new_font (fallback->name, fallback->cs));
866 fallback++; 1038 fallback++;
1039 i = 0;
867 } 1040 }
868 } 1041 }
869 1042
870 return 0; /* we must return SOME font */ 1043 return 0; /* we must return SOME font */
871} 1044}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines