… | |
… | |
227 | struct rxvt_font_default : rxvt_font { |
227 | struct rxvt_font_default : rxvt_font { |
228 | rxvt_fontprop properties () |
228 | rxvt_fontprop properties () |
229 | { |
229 | { |
230 | rxvt_fontprop p; |
230 | rxvt_fontprop p; |
231 | |
231 | |
232 | p.height = 1; |
232 | p.width = p.height = 1; |
233 | p.weight = rxvt_fontprop::medium; |
233 | p.weight = rxvt_fontprop::medium; |
234 | p.slant = rxvt_fontprop::roman; |
234 | p.slant = rxvt_fontprop::roman; |
235 | |
235 | |
236 | return p; |
236 | return p; |
237 | } |
237 | } |
… | |
… | |
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 | |
359 | const char * |
361 | const char * |
360 | rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const |
362 | rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const |
361 | { |
363 | { |
… | |
… | |
368 | } |
370 | } |
369 | |
371 | |
370 | rxvt_fontprop |
372 | rxvt_fontprop |
371 | rxvt_font_x11::properties () |
373 | rxvt_font_x11::properties () |
372 | { |
374 | { |
373 | return properties (f); |
|
|
374 | } |
|
|
375 | |
|
|
376 | rxvt_fontprop |
|
|
377 | rxvt_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 | |
|
|
380 | bool |
|
|
381 | rxvt_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 | |
|
|
391 | bool |
|
|
392 | rxvt_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 | |
384 | p.height = height; |
397 | unsigned long height; |
385 | p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
398 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
386 | p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; |
399 | return false; |
387 | |
400 | |
388 | return p; |
401 | unsigned long avgwidth; |
|
|
402 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
|
|
403 | avgwidth = 0; |
|
|
404 | |
|
|
405 | return set_properties (p, height, weight, slant, avgwidth); |
|
|
406 | } |
|
|
407 | |
|
|
408 | bool |
|
|
409 | rxvt_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 |
|
|
444 | static void |
|
|
445 | fix_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); |
389 | } |
469 | } |
390 | |
470 | |
391 | bool |
471 | bool |
392 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
472 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
393 | { |
473 | { |
394 | clear (); |
474 | clear (); |
395 | |
475 | |
396 | char **list; |
476 | char **list; |
397 | int count; |
477 | int count; |
398 | XFontStruct *info; |
|
|
399 | list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); |
478 | list = XListFonts (DISPLAY, name, 512, &count); |
|
|
479 | set_name (0); |
400 | |
480 | |
401 | if (!list) |
481 | if (!list) |
402 | return false; |
482 | return false; |
403 | |
483 | |
404 | int bestdiff = 0x7fffffff; |
484 | int bestdiff = 0x7fffffff; |
405 | XFontStruct *best = 0; |
|
|
406 | for (int i = 0; i < count; i++) |
485 | for (int i = 0; i < count; i++) |
407 | { |
486 | { |
408 | XFontStruct *f = info + i; |
487 | rxvt_fontprop p; |
|
|
488 | char fname[1024]; |
|
|
489 | fix_scalable (fname, list[i], prop); |
409 | |
490 | |
|
|
491 | if (!set_properties (p, fname)) |
|
|
492 | continue; |
|
|
493 | |
410 | if (f->ascent + f->descent <= prop.height) // weed out too large fonts |
494 | if (p.height > prop.height) // weed out too large fonts |
411 | { |
495 | continue; |
412 | rxvt_fontprop p = properties (f); |
496 | |
413 | int diff = (prop.height - f->ascent + f->descent) * 32 |
497 | int diff = (prop.height - p.height) * 32 |
414 | + abs (prop.weight - p.weight) |
498 | + abs (prop.weight - p.weight) |
415 | + abs (prop.slant - p.slant ); |
499 | + abs (prop.slant - p.slant ); |
416 | |
500 | |
417 | if (!best // compare against best found so far |
501 | if (!name // compare against best found so far |
418 | || diff < bestdiff) |
502 | || diff < bestdiff) |
419 | { |
503 | { |
420 | best = f; |
504 | set_name (strdup (fname)); |
421 | bestdiff = diff; |
505 | bestdiff = diff; |
422 | } |
506 | } |
423 | } |
|
|
424 | } |
507 | } |
425 | |
508 | |
|
|
509 | XFreeFontNames (list); |
|
|
510 | |
426 | if (!best) |
511 | if (!name) |
427 | return false; |
512 | return false; |
428 | |
|
|
429 | set_name (strdup (list[best - info])); |
|
|
430 | |
|
|
431 | XFreeFontInfo (list, info, count); |
|
|
432 | |
513 | |
433 | f = XLoadQueryFont (DISPLAY, name); |
514 | f = XLoadQueryFont (DISPLAY, name); |
434 | |
515 | |
435 | if (!f) |
516 | if (!f) |
436 | return false; |
517 | return false; |
… | |
… | |
658 | //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables |
739 | //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables |
659 | #define SWATHBITS 8 |
740 | #define SWATHBITS 8 |
660 | #endif |
741 | #endif |
661 | |
742 | |
662 | struct rxvt_font_xft : rxvt_font { |
743 | struct rxvt_font_xft : rxvt_font { |
663 | #if 0 |
|
|
664 | enum { |
|
|
665 | SWATHCOUNT = 1 << (21 - UNIBITS), |
|
|
666 | SWATHSIZE = 1 << (SWATHBITS - 5) |
|
|
667 | }; |
|
|
668 | typedef uint32_t swath[SWATHSIZE]; |
|
|
669 | |
|
|
670 | swath *cvr[SWATHCOUNT]; |
|
|
671 | #endif |
|
|
672 | |
|
|
673 | #if 0 |
|
|
674 | void gen_coverage_swath (unsigned int page); |
|
|
675 | |
|
|
676 | bool has_char (uint32_t ch) |
|
|
677 | { |
|
|
678 | unsigned int page = ch >> SWATHBITS; |
|
|
679 | unsigned int idx = ch & ((1 << SWATHBITS) - 1); |
|
|
680 | |
|
|
681 | if (page >= SWATHCOUNT) |
|
|
682 | return false; |
|
|
683 | |
|
|
684 | if (!cvr[page]) gen_coverage_swath (page); |
|
|
685 | |
|
|
686 | return cvr[page][idx >> 5] & (1 << (idx & 31)); |
|
|
687 | } |
|
|
688 | #endif |
|
|
689 | rxvt_font_xft () { f = 0; d = 0; } |
744 | rxvt_font_xft () { f = 0; d = 0; } |
690 | |
745 | |
691 | void clear (); |
746 | void clear (); |
692 | |
747 | |
693 | rxvt_fontprop properties (); |
748 | rxvt_fontprop properties (); |
… | |
… | |
701 | bool has_codepoint (uint32_t unicode); |
756 | bool has_codepoint (uint32_t unicode); |
702 | |
757 | |
703 | protected: |
758 | protected: |
704 | XftFont *f; |
759 | XftFont *f; |
705 | XftDraw *d; |
760 | XftDraw *d; |
706 | |
|
|
707 | #if 0 |
|
|
708 | virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0; |
|
|
709 | void set_swath (uint32_t ch) |
|
|
710 | { |
|
|
711 | cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1)); |
|
|
712 | } |
|
|
713 | #endif |
|
|
714 | }; |
761 | }; |
715 | |
762 | |
716 | void |
763 | void |
717 | rxvt_font_xft::clear () |
764 | rxvt_font_xft::clear () |
718 | { |
765 | { |
… | |
… | |
725 | if (d) |
772 | if (d) |
726 | { |
773 | { |
727 | XftDrawDestroy (d); |
774 | XftDrawDestroy (d); |
728 | d = 0; |
775 | d = 0; |
729 | } |
776 | } |
730 | |
|
|
731 | #if 0 |
|
|
732 | for (int i = 0; i < SWATHCOUNT; i++) |
|
|
733 | delete cvr[i]; |
|
|
734 | #endif |
|
|
735 | } |
777 | } |
736 | |
778 | |
737 | rxvt_fontprop |
779 | rxvt_fontprop |
738 | rxvt_font_xft::properties () |
780 | rxvt_font_xft::properties () |
739 | { |
781 | { |
740 | rxvt_fontprop p; |
782 | rxvt_fontprop p; |
741 | |
783 | |
742 | FT_Face face = XftLockFace (f); |
784 | FT_Face face = XftLockFace (f); |
743 | |
785 | |
744 | p.height = height; |
786 | p.width = width; p.height = height; |
745 | p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
787 | p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
746 | p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
788 | p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
747 | |
789 | |
748 | XftUnlockFace (f); |
790 | XftUnlockFace (f); |
749 | |
791 | |
… | |
… | |
771 | FcPatternAddInteger (p, FC_WEIGHT, prop.weight); |
813 | FcPatternAddInteger (p, FC_WEIGHT, prop.weight); |
772 | |
814 | |
773 | if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) |
815 | if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) |
774 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
816 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
775 | |
817 | |
|
|
818 | #if 0 // clipping unfortunately destroys our precious double-width-characters |
|
|
819 | // clip width, we can't do better, or can we? |
|
|
820 | if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) |
|
|
821 | FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); |
|
|
822 | #endif |
|
|
823 | |
776 | //FcPatternAddBool (p, FC_MINSPACE, 1); |
824 | //FcPatternAddBool (p, FC_MINSPACE, 1); |
777 | |
825 | |
778 | XftResult result; |
826 | XftResult result; |
779 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
827 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
780 | |
828 | |
… | |
… | |
832 | XftUnlockFace (f); |
880 | XftUnlockFace (f); |
833 | |
881 | |
834 | return true; |
882 | return true; |
835 | } |
883 | } |
836 | |
884 | |
837 | #if 0 |
|
|
838 | void rxvt_font::gen_coverage_swath (unsigned int page) |
|
|
839 | { |
|
|
840 | cvr[page] = new swath; |
|
|
841 | |
|
|
842 | for (int i = 0; i < SWATHSIZE; i++) |
|
|
843 | cvr[page][i] = 0; |
|
|
844 | |
|
|
845 | populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1); |
|
|
846 | } |
|
|
847 | #endif |
|
|
848 | |
|
|
849 | bool |
885 | bool |
850 | rxvt_font_xft::has_codepoint (uint32_t unicode) |
886 | rxvt_font_xft::has_codepoint (uint32_t unicode) |
851 | { |
887 | { |
852 | return XftCharExists (DISPLAY, f, unicode); |
888 | return XftCharExists (DISPLAY, f, unicode); |
853 | } |
889 | } |
… | |
… | |
866 | if (bg >= 0 && bg != Color_bg) |
902 | if (bg >= 0 && bg != Color_bg) |
867 | 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); |
868 | else |
904 | else |
869 | 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); |
870 | |
906 | |
871 | if (!slow && width == r->TermWin.fwidth) |
907 | if (!slow && width == r->TermWin.fwidth && 0) |
872 | { |
908 | { |
873 | if (sizeof (text_t) == sizeof (FcChar16)) |
909 | if (sizeof (text_t) == sizeof (FcChar16)) |
874 | 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); |
875 | else |
911 | else |
876 | 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); |
… | |
… | |
879 | { |
915 | { |
880 | while (len) |
916 | while (len) |
881 | { |
917 | { |
882 | if (*text != NOCHAR && *text != ' ') |
918 | if (*text != NOCHAR && *text != ' ') |
883 | { |
919 | { |
|
|
920 | XGlyphInfo extents; |
884 | if (sizeof (text_t) == sizeof (FcChar16)) |
921 | if (sizeof (text_t) == sizeof (FcChar16)) |
885 | 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 | } |
886 | else |
927 | else |
887 | 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 | } |
888 | } |
934 | } |
889 | |
935 | |
890 | x += r->TermWin.fwidth; |
936 | x += r->TermWin.fwidth; |
891 | text++; |
937 | text++; |
892 | len--; |
938 | len--; |