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.11 by pcg, Wed Dec 24 09:40:30 2003 UTC vs.
Revision 1.12 by pcg, Fri Dec 26 07:42:23 2003 UTC

337 rxvt_font_x11 () { f = 0; } 337 rxvt_font_x11 () { f = 0; }
338 338
339 void clear (); 339 void clear ();
340 340
341 rxvt_fontprop properties (); 341 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
343 342
344 bool load (const rxvt_fontprop &prop); 343 bool load (const rxvt_fontprop &prop);
345 344
346 bool has_codepoint (uint32_t unicode); 345 bool has_codepoint (uint32_t unicode);
347 346
352 XFontStruct *f; 351 XFontStruct *f;
353 codeset cs; 352 codeset cs;
354 bool enc2b, encm; 353 bool enc2b, encm;
355 354
356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const; 355 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
356 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
357 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
358 bool set_properties (rxvt_fontprop &p, const char *name);
357}; 359};
358 360
359const char * 361const char *
360rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 362rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
361{ 363{
368} 370}
369 371
370rxvt_fontprop 372rxvt_fontprop
371rxvt_font_x11::properties () 373rxvt_font_x11::properties ()
372{ 374{
373 return properties (f);
374}
375
376rxvt_fontprop
377rxvt_font_x11::properties (XFontStruct *f)
378{
379 rxvt_fontprop p; 375 rxvt_fontprop p;
376 set_properties (p, f);
377 return p;
378}
380 379
380bool
381rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
382{
383 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
384 p.height = height;
385 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
386 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
387
388 return true;
389}
390
391bool
392rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
393{
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium"); 394 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r"); 395 const char *slant = get_property (f, "SLANT", "r");
383 396
397 unsigned long height;
398 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
399 return false;
400
384 unsigned long avgwidth; 401 unsigned long avgwidth;
385 p.width = XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth) 402 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
386 ? avgwidth / 10 403 avgwidth = 0;
387 : (height + 1) / 2;
388 p.height = height;
389 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
390 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
391 404
392 return p; 405 return set_properties (p, height, weight, slant, avgwidth);
406}
407
408bool
409rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
410{
411 int slashes = 0;
412 const char *comp[12];
413
414 for (const char *c = name; *c; c++)
415 if (*c == '-')
416 {
417 comp[slashes++] = c + 1;
418 if (slashes >= 13)
419 break;
420 }
421
422 /* can we short-circuit the costly XLoadQueryFont? */
423 if (slashes >= 13
424 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
425 && (*comp[11] >= '0' && *comp[11] <= '9'))
426 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
427
428 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
429
430 if (f)
431 {
432 // the font should really exists now. if not, we have a problem
433 // (e.g. if the user did xset fp rehash just when we were searching fonts).
434 // in that case, just return garbage.
435 bool ret = set_properties (p, f);
436 XFreeFont (DISPLAY, f);
437 return ret;
438 }
439 else
440 return false;
441}
442
443// fix the size of scalable fonts
444static void
445fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
446{
447 int slashes = 0;
448 const char *size;
449
450 for (const char *c = name; *c; c++)
451 if (*c == '-')
452 {
453 if (slashes == 6)
454 size = c + 1;
455
456 if (++slashes >= 13)
457 break;
458 }
459
460 if (slashes >= 13 && size[0] == '0')
461 {
462 strncpy (buf, name, size - name);
463 buf += size - name;
464 buf += sprintf (buf, "%d", prop.height);
465 strcpy (buf, size + 1);
466 }
467 else
468 strcpy (buf, name);
393} 469}
394 470
395bool 471bool
396rxvt_font_x11::load (const rxvt_fontprop &prop) 472rxvt_font_x11::load (const rxvt_fontprop &prop)
397{ 473{
398 clear (); 474 clear ();
399 475
400 char **list; 476 char **list;
401 int count; 477 int count;
402 XFontStruct *info;
403 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 478 list = XListFonts (DISPLAY, name, 512, &count);
479 set_name (0);
404 480
405 if (!list) 481 if (!list)
406 return false; 482 return false;
407 483
408 int bestdiff = 0x7fffffff; 484 int bestdiff = 0x7fffffff;
409 XFontStruct *best = 0;
410 for (int i = 0; i < count; i++) 485 for (int i = 0; i < count; i++)
411 { 486 {
412 XFontStruct *f = info + i; 487 rxvt_fontprop p;
488 char fname[1024];
489 fix_scalable (fname, list[i], prop);
413 490
491 if (!set_properties (p, fname))
492 continue;
493
414 if (f->ascent + f->descent <= prop.height) // weed out too large fonts 494 if (p.height > prop.height) // weed out too large fonts
415 { 495 continue;
416 rxvt_fontprop p = properties (f); 496
417 int diff = (prop.height - f->ascent + f->descent) * 32 497 int diff = (prop.height - p.height) * 32
418 + abs (prop.weight - p.weight) 498 + abs (prop.weight - p.weight)
419 + abs (prop.slant - p.slant ); 499 + abs (prop.slant - p.slant );
420 500
421 if (!best // compare against best found so far 501 if (!name // compare against best found so far
422 || diff < bestdiff) 502 || diff < bestdiff)
423 { 503 {
424 best = f; 504 set_name (strdup (fname));
425 bestdiff = diff; 505 bestdiff = diff;
426 } 506 }
427 }
428 } 507 }
429 508
509 XFreeFontNames (list);
510
430 if (!best) 511 if (!name)
431 return false; 512 return false;
432
433 set_name (strdup (list[best - info]));
434
435 XFreeFontInfo (list, info, count);
436 513
437 f = XLoadQueryFont (DISPLAY, name); 514 f = XLoadQueryFont (DISPLAY, name);
438 515
439 if (!f) 516 if (!f)
440 return false; 517 return false;
825 if (bg >= 0 && bg != Color_bg) 902 if (bg >= 0 && bg != Color_bg)
826 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); 903 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
827 else 904 else
828 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 905 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
829 906
830 if (!slow && width == r->TermWin.fwidth) 907 if (!slow && width == r->TermWin.fwidth && 0)
831 { 908 {
832 if (sizeof (text_t) == sizeof (FcChar16)) 909 if (sizeof (text_t) == sizeof (FcChar16))
833 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 910 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
834 else 911 else
835 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 912 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
838 { 915 {
839 while (len) 916 while (len)
840 { 917 {
841 if (*text != NOCHAR && *text != ' ') 918 if (*text != NOCHAR && *text != ' ')
842 { 919 {
920 XGlyphInfo extents;
843 if (sizeof (text_t) == sizeof (FcChar16)) 921 if (sizeof (text_t) == sizeof (FcChar16))
844 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 + (r->TermWin.fwidth - extents.width) / 2,
925 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
926 }
845 else 927 else
846 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 + (r->TermWin.fwidth - extents.width) / 2,
932 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
933 }
847 } 934 }
848 935
849 x += r->TermWin.fwidth; 936 x += r->TermWin.fwidth;
850 text++; 937 text++;
851 len--; 938 len--;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines