… | |
… | |
143 | |
143 | |
144 | { CS_UNKNOWN, 0 } |
144 | { CS_UNKNOWN, 0 } |
145 | }; |
145 | }; |
146 | |
146 | |
147 | // these characters are used to guess the font height and width |
147 | // these characters are used to guess the font height and width |
148 | // pango uses a similar algorithm and eosn't trust the font either. |
148 | // pango uses a similar algorithm and doesn't trust the font either. |
149 | static uint16_t extent_test_chars[] = { |
149 | static uint16_t extent_test_chars[] = { |
150 | '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', |
150 | '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', |
151 | 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ |
151 | 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ |
152 | 0x304c, 0x672c, // が本 |
152 | 0x304c, 0x672c, // が本 |
153 | }; |
153 | }; |
… | |
… | |
531 | |
531 | |
532 | bool |
532 | bool |
533 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
533 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
534 | { |
534 | { |
535 | unsigned long height; |
535 | unsigned long height; |
|
|
536 | |
|
|
537 | #if 0 |
536 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
538 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
537 | return false; |
539 | return false; |
|
|
540 | #else |
|
|
541 | height = f->ascent + f->descent; |
|
|
542 | #endif |
538 | |
543 | |
539 | unsigned long avgwidth; |
544 | unsigned long avgwidth; |
540 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
545 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
541 | avgwidth = 0; |
546 | avgwidth = 0; |
542 | |
547 | |
… | |
… | |
670 | ? "r" : "i"); // TODO: handle "o"blique, too |
675 | ? "r" : "i"); // TODO: handle "o"blique, too |
671 | set_name (strdup (fname)); |
676 | set_name (strdup (fname)); |
672 | } |
677 | } |
673 | } |
678 | } |
674 | |
679 | |
|
|
680 | sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset |
|
|
681 | ? 0 : prop.height); |
|
|
682 | |
|
|
683 | struct font_weight { |
|
|
684 | char *name; |
|
|
685 | int diff; |
|
|
686 | |
|
|
687 | void clear () |
|
|
688 | { |
|
|
689 | name = 0; |
|
|
690 | diff = 0x7fffffff; |
|
|
691 | } |
|
|
692 | |
|
|
693 | font_weight () { clear (); } |
|
|
694 | ~font_weight () { free (name); } |
|
|
695 | }; |
|
|
696 | |
675 | char **list; |
697 | char **list; |
676 | int count; |
698 | int count; |
677 | list = XListFonts (DISPLAY, name, 1024, &count); |
699 | list = XListFonts (DISPLAY, name, 4000, &count); |
678 | |
700 | |
679 | set_name (0); |
701 | set_name (0); |
680 | |
702 | |
681 | if (!list) |
703 | if (!list) |
682 | return false; |
704 | return false; |
683 | |
705 | |
684 | sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset |
706 | font_weight *fonts = new font_weight[count]; |
685 | ? 0 : prop.height); |
|
|
686 | |
707 | |
687 | int bestdiff = 0x7fffffff; |
|
|
688 | for (int i = 0; i < count; i++) |
708 | for (int i = 0; i < count; i++) |
689 | { |
709 | { |
690 | rxvt_fontprop p; |
710 | rxvt_fontprop p; |
691 | char fname[1024]; |
711 | char fname[1024]; |
692 | |
712 | |
… | |
… | |
705 | if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; |
725 | if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; |
706 | if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); |
726 | if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); |
707 | if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); |
727 | if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); |
708 | //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); |
728 | //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); |
709 | |
729 | |
710 | if (!name // compare against best found so far |
730 | fonts[i].name = strdup (fname); |
711 | || diff < bestdiff) |
731 | fonts[i].diff = diff; |
712 | { |
|
|
713 | set_name (strdup (fname)); |
|
|
714 | bestdiff = diff; |
|
|
715 | } |
|
|
716 | } |
732 | } |
717 | |
733 | |
718 | XFreeFontNames (list); |
734 | XFreeFontNames (list); |
719 | |
735 | |
720 | if (!name) |
736 | // this loop only iterates when the guessed font-size is too small |
721 | return false; |
737 | for (;;) |
|
|
738 | { |
|
|
739 | font_weight *best = fonts + count - 1; |
722 | |
740 | |
|
|
741 | for (font_weight *w = best; w-- > fonts; ) |
|
|
742 | if (w->diff < best->diff) |
|
|
743 | best = w; |
|
|
744 | |
|
|
745 | if (!best->name |
723 | f = XLoadQueryFont (DISPLAY, name); |
746 | || !(f = XLoadQueryFont (DISPLAY, best->name))) |
|
|
747 | break; |
|
|
748 | |
|
|
749 | set_name (best->name); |
|
|
750 | best->clear (); |
|
|
751 | |
|
|
752 | ascent = f->ascent; |
|
|
753 | descent = f->descent; |
|
|
754 | height = ascent + descent; |
|
|
755 | |
|
|
756 | if (prop.height == rxvt_fontprop::unset |
|
|
757 | || height <= prop.height) |
|
|
758 | break; // font is ready for use |
|
|
759 | |
|
|
760 | // PIXEL_SIZE small enough, but real height too large |
|
|
761 | clear (); |
|
|
762 | } |
|
|
763 | |
|
|
764 | delete [] fonts; |
724 | |
765 | |
725 | if (!f) |
766 | if (!f) |
726 | return false; |
767 | return false; |
727 | |
768 | |
728 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
769 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
… | |
… | |
756 | if (cs == CS_UNICODE) |
797 | if (cs == CS_UNICODE) |
757 | cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font |
798 | cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font |
758 | |
799 | |
759 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
800 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
760 | enc2b = encm || f->max_char_or_byte2 > 255; |
801 | enc2b = encm || f->max_char_or_byte2 > 255; |
761 | |
|
|
762 | ascent = f->ascent; |
|
|
763 | descent = f->descent; |
|
|
764 | height = ascent + descent; |
|
|
765 | |
802 | |
766 | slow = false; |
803 | slow = false; |
767 | |
804 | |
768 | #if 1 // only used for slow detection, TODO optimize |
805 | #if 1 // only used for slow detection, TODO optimize |
769 | if (f->min_bounds.width == f->max_bounds.width) |
806 | if (f->min_bounds.width == f->max_bounds.width) |