1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: defaultfont.C |
2 | * File: rxvtfont.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
5 | * - original version. |
5 | * - original version. |
6 | * |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
7 | * This program is free software; you can redistribute it and/or modify |
… | |
… | |
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 | }; |
… | |
… | |
331 | void |
331 | void |
332 | rxvt_font_default::draw (rxvt_drawable &d, int x, int y, |
332 | rxvt_font_default::draw (rxvt_drawable &d, int x, int y, |
333 | const text_t *text, int len, |
333 | const text_t *text, int len, |
334 | int fg, int bg) |
334 | int fg, int bg) |
335 | { |
335 | { |
|
|
336 | Display *disp = d.display->display; |
|
|
337 | |
336 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
338 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
337 | |
339 | |
338 | XSetForeground (d.display->display, TGC, r->pix_colors[fg]); |
340 | XSetForeground (disp, TGC, r->pix_colors[fg]); |
339 | |
341 | |
340 | while (len) |
342 | while (len) |
341 | { |
343 | { |
342 | #if ENABLE_COMBINING |
344 | #if ENABLE_COMBINING |
343 | compose_char *cc; |
345 | compose_char *cc; |
… | |
… | |
374 | |
376 | |
375 | XGCValues gcv; |
377 | XGCValues gcv; |
376 | |
378 | |
377 | gcv.cap_style = CapButt; |
379 | gcv.cap_style = CapButt; |
378 | gcv.line_width = 0; |
380 | gcv.line_width = 0; |
379 | XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); |
381 | XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv); |
380 | |
382 | |
381 | while (a < b) |
383 | while (a < b) |
382 | { |
384 | { |
383 | uint32_t command = *a++; |
385 | uint32_t command = *a++; |
384 | |
386 | |
… | |
… | |
391 | int y2 = y_[(command >> 0) & 15]; |
393 | int y2 = y_[(command >> 0) & 15]; |
392 | |
394 | |
393 | switch (op) |
395 | switch (op) |
394 | { |
396 | { |
395 | case 0: // line |
397 | case 0: // line |
396 | XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); |
398 | XDrawLine (disp, d, TGC, x1, y1, x2, y2); |
397 | break; |
399 | break; |
398 | |
400 | |
399 | case 1: // rectangle, possibly stippled |
401 | case 1: // rectangle, possibly stippled |
400 | if (a) |
402 | if (a) |
401 | { |
403 | { |
402 | static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; |
404 | static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; |
403 | |
405 | |
404 | gcv.fill_style = FillStippled; |
406 | gcv.fill_style = FillStippled; |
405 | gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); |
407 | gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2); |
406 | gcv.ts_x_origin = x; |
408 | gcv.ts_x_origin = x; |
407 | gcv.ts_y_origin = y; |
409 | gcv.ts_y_origin = y; |
408 | |
410 | |
409 | XChangeGC (d.display->display, TGC, |
411 | XChangeGC (disp, TGC, |
410 | GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, |
412 | GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, |
411 | &gcv); |
413 | &gcv); |
412 | } |
414 | } |
413 | |
415 | |
414 | XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
416 | XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
415 | |
417 | |
416 | if (a) |
418 | if (a) |
417 | { |
419 | { |
418 | XFreePixmap (d.display->display, gcv.stipple); |
420 | XFreePixmap (disp, gcv.stipple); |
419 | gcv.stipple = 0; |
421 | gcv.stipple = 0; |
420 | gcv.fill_style = FillSolid; |
422 | gcv.fill_style = FillSolid; |
421 | XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); |
423 | XChangeGC (disp, TGC, GCFillStyle, &gcv); |
422 | } |
424 | } |
423 | break; |
425 | break; |
424 | case 2: // arc |
426 | case 2: // arc |
425 | XDrawArc (d.display->display, d, TGC, |
427 | XDrawArc (disp, d, TGC, |
426 | x1 - W/2, y1 - H/2, W-1, H-1, |
428 | x1 - W/2, y1 - H/2, W-1, H-1, |
427 | (a - 1) * 90*64, (b - 1) * 90*64); |
429 | (a - 1) * 90*64, (b - 1) * 90*64); |
428 | break; |
430 | break; |
429 | } |
431 | } |
430 | } |
432 | } |
… | |
… | |
461 | case ZERO_WIDTH_CHAR: |
463 | case ZERO_WIDTH_CHAR: |
462 | case NOCHAR: |
464 | case NOCHAR: |
463 | break; |
465 | break; |
464 | |
466 | |
465 | default: |
467 | default: |
466 | XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, |
468 | XDrawRectangle (disp, d, TGC, x + 2, y + 2, |
467 | fwidth - 4, r->TermWin.fheight - 4); |
469 | fwidth - 4, r->TermWin.fheight - 4); |
468 | } |
470 | } |
469 | |
471 | |
470 | x += fwidth; |
472 | x += fwidth; |
471 | } |
473 | } |
… | |
… | |
531 | |
533 | |
532 | bool |
534 | bool |
533 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
535 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
534 | { |
536 | { |
535 | unsigned long height; |
537 | unsigned long height; |
|
|
538 | |
|
|
539 | #if 0 |
536 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
540 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
537 | return false; |
541 | return false; |
|
|
542 | #else |
|
|
543 | height = f->ascent + f->descent; |
|
|
544 | #endif |
538 | |
545 | |
539 | unsigned long avgwidth; |
546 | unsigned long avgwidth; |
540 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
547 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
541 | avgwidth = 0; |
548 | avgwidth = 0; |
542 | |
549 | |
… | |
… | |
625 | } |
632 | } |
626 | |
633 | |
627 | bool |
634 | bool |
628 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
635 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
629 | { |
636 | { |
|
|
637 | Display *disp = DISPLAY; |
|
|
638 | |
630 | clear (); |
639 | clear (); |
631 | |
640 | |
632 | char field_str[64]; // enough for 128 bits |
641 | char field_str[64]; // enough for 128 bits |
633 | |
642 | |
634 | // first morph the font if required |
643 | // first morph the font if required |
… | |
… | |
637 | { |
646 | { |
638 | char fname[1024]; |
647 | char fname[1024]; |
639 | |
648 | |
640 | if (name[0] != '-') |
649 | if (name[0] != '-') |
641 | { |
650 | { |
642 | f = XLoadQueryFont (DISPLAY, name); |
651 | f = XLoadQueryFont (disp, name); |
643 | |
652 | |
644 | if (!f) |
653 | if (!f) |
645 | return false; |
654 | return false; |
646 | |
655 | |
647 | char *new_name = get_property (f, "FONT", name); |
656 | char *new_name = get_property (f, "FONT", name); |
… | |
… | |
649 | if (new_name) |
658 | if (new_name) |
650 | set_name (new_name); |
659 | set_name (new_name); |
651 | else |
660 | else |
652 | rxvt_warn ("font '%s' has no FONT property, continuing without.", name); |
661 | rxvt_warn ("font '%s' has no FONT property, continuing without.", name); |
653 | |
662 | |
654 | XFreeFont (DISPLAY, f); |
663 | XFreeFont (disp, f); |
655 | f = 0; |
664 | f = 0; |
656 | } |
665 | } |
657 | |
666 | |
658 | if (prop.weight != rxvt_fontprop::unset) |
667 | if (prop.weight != rxvt_fontprop::unset) |
659 | { |
668 | { |
… | |
… | |
670 | ? "r" : "i"); // TODO: handle "o"blique, too |
679 | ? "r" : "i"); // TODO: handle "o"blique, too |
671 | set_name (strdup (fname)); |
680 | set_name (strdup (fname)); |
672 | } |
681 | } |
673 | } |
682 | } |
674 | |
683 | |
|
|
684 | sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset |
|
|
685 | ? 0 : prop.height); |
|
|
686 | |
|
|
687 | struct font_weight { |
|
|
688 | char *name; |
|
|
689 | int diff; |
|
|
690 | |
|
|
691 | void clear () |
|
|
692 | { |
|
|
693 | name = 0; |
|
|
694 | diff = 0x7fffffff; |
|
|
695 | } |
|
|
696 | |
|
|
697 | font_weight () { clear (); } |
|
|
698 | ~font_weight () { free (name); } |
|
|
699 | }; |
|
|
700 | |
675 | char **list; |
701 | char **list; |
676 | int count; |
702 | int count; |
677 | list = XListFonts (DISPLAY, name, 1024, &count); |
703 | list = XListFonts (disp, name, 4000, &count); |
678 | |
704 | |
679 | set_name (0); |
705 | set_name (0); |
680 | |
706 | |
681 | if (!list) |
707 | if (!list) |
682 | return false; |
708 | return false; |
683 | |
709 | |
684 | sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset |
710 | font_weight *fonts = new font_weight[count]; |
685 | ? 0 : prop.height); |
|
|
686 | |
711 | |
687 | int bestdiff = 0x7fffffff; |
|
|
688 | for (int i = 0; i < count; i++) |
712 | for (int i = 0; i < count; i++) |
689 | { |
713 | { |
690 | rxvt_fontprop p; |
714 | rxvt_fontprop p; |
691 | char fname[1024]; |
715 | char fname[1024]; |
692 | |
716 | |
693 | int diff = 0; |
717 | int diff = 0; |
694 | |
718 | |
695 | if (replace_field (fname, list[i], 6, '0', field_str)) |
719 | if (replace_field (fname, list[i], 6, '0', field_str)) |
696 | diff += 10; // slightly penalize scalable fonts |
720 | diff += 10; // slightly penalize scalable fonts |
|
|
721 | else if (replace_field (fname, list[i], 11, '0', "0")) |
|
|
722 | diff += 300; // more heavily penalize what looks like scaled bitmap fotns |
697 | |
723 | |
698 | if (!set_properties (p, fname)) |
724 | if (!set_properties (p, fname)) |
699 | continue; |
725 | continue; |
700 | |
726 | |
701 | if (prop.height != rxvt_fontprop::unset |
727 | if (prop.height != rxvt_fontprop::unset |
… | |
… | |
705 | if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; |
731 | 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); |
732 | 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); |
733 | 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); |
734 | //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); |
709 | |
735 | |
710 | if (!name // compare against best found so far |
736 | fonts[i].name = strdup (fname); |
711 | || diff < bestdiff) |
737 | fonts[i].diff = diff; |
712 | { |
|
|
713 | set_name (strdup (fname)); |
|
|
714 | bestdiff = diff; |
|
|
715 | } |
|
|
716 | } |
738 | } |
717 | |
739 | |
718 | XFreeFontNames (list); |
740 | XFreeFontNames (list); |
719 | |
741 | |
720 | if (!name) |
742 | // this loop only iterates when the guessed font-size is too small |
721 | return false; |
743 | for (;;) |
|
|
744 | { |
|
|
745 | font_weight *best = fonts + count - 1; |
722 | |
746 | |
723 | f = XLoadQueryFont (DISPLAY, name); |
747 | for (font_weight *w = best; w-- > fonts; ) |
|
|
748 | if (w->diff <= best->diff) |
|
|
749 | best = w; |
|
|
750 | |
|
|
751 | if (!best->name |
|
|
752 | || !(f = XLoadQueryFont (disp, best->name))) |
|
|
753 | break; |
|
|
754 | |
|
|
755 | set_name (best->name); |
|
|
756 | best->clear (); |
|
|
757 | |
|
|
758 | ascent = f->ascent; |
|
|
759 | descent = f->descent; |
|
|
760 | height = ascent + descent; |
|
|
761 | |
|
|
762 | if (prop.height == rxvt_fontprop::unset |
|
|
763 | || height <= prop.height) |
|
|
764 | break; // font is ready for use |
|
|
765 | |
|
|
766 | // PIXEL_SIZE small enough, but real height too large |
|
|
767 | clear (); |
|
|
768 | } |
|
|
769 | |
|
|
770 | delete [] fonts; |
724 | |
771 | |
725 | if (!f) |
772 | if (!f) |
726 | return false; |
773 | return false; |
727 | |
774 | |
728 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
775 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
… | |
… | |
756 | if (cs == CS_UNICODE) |
803 | if (cs == CS_UNICODE) |
757 | cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font |
804 | cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font |
758 | |
805 | |
759 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
806 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
760 | enc2b = encm || f->max_char_or_byte2 > 255; |
807 | enc2b = encm || f->max_char_or_byte2 > 255; |
761 | |
|
|
762 | ascent = f->ascent; |
|
|
763 | descent = f->descent; |
|
|
764 | height = ascent + descent; |
|
|
765 | |
808 | |
766 | slow = false; |
809 | slow = false; |
767 | |
810 | |
768 | #if 1 // only used for slow detection, TODO optimize |
811 | #if 1 // only used for slow detection, TODO optimize |
769 | if (f->min_bounds.width == f->max_bounds.width) |
812 | if (f->min_bounds.width == f->max_bounds.width) |
… | |
… | |
1043 | } |
1086 | } |
1044 | |
1087 | |
1045 | bool |
1088 | bool |
1046 | rxvt_font_xft::load (const rxvt_fontprop &prop) |
1089 | rxvt_font_xft::load (const rxvt_fontprop &prop) |
1047 | { |
1090 | { |
|
|
1091 | Display *disp = DISPLAY; |
|
|
1092 | |
1048 | clear (); |
1093 | clear (); |
1049 | |
1094 | |
1050 | FcPattern *p = FcNameParse ((FcChar8 *) name); |
1095 | FcPattern *p = FcNameParse ((FcChar8 *) name); |
1051 | |
1096 | |
1052 | if (!p) |
1097 | if (!p) |
1053 | return false; |
1098 | return false; |
1054 | |
1099 | |
1055 | FcValue v; |
1100 | FcValue v; |
1056 | |
1101 | |
1057 | if (prop.height != rxvt_fontprop::unset |
1102 | if (prop.height != rxvt_fontprop::unset |
1058 | || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch |
1103 | && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch |
1059 | && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) |
1104 | && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) |
1060 | FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); |
1105 | FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); |
1061 | |
1106 | |
1062 | if (prop.weight != rxvt_fontprop::unset |
1107 | if (prop.weight != rxvt_fontprop::unset |
1063 | && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
1108 | && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
… | |
… | |
1078 | |
1123 | |
1079 | // store generated name so iso14755 view gives better results |
1124 | // store generated name so iso14755 view gives better results |
1080 | set_name ((char *)FcNameUnparse (p)); |
1125 | set_name ((char *)FcNameUnparse (p)); |
1081 | |
1126 | |
1082 | XftResult result; |
1127 | XftResult result; |
1083 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
1128 | FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result); |
1084 | |
1129 | |
1085 | FcPatternDestroy (p); |
1130 | FcPatternDestroy (p); |
1086 | |
1131 | |
1087 | if (!match) |
1132 | if (!match) |
1088 | return false; |
1133 | return false; |
… | |
… | |
1090 | int ftheight = 0; |
1135 | int ftheight = 0; |
1091 | bool success = true; |
1136 | bool success = true; |
1092 | |
1137 | |
1093 | for (;;) |
1138 | for (;;) |
1094 | { |
1139 | { |
1095 | f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); |
1140 | f = XftFontOpenPattern (disp, FcPatternDuplicate (match)); |
1096 | |
1141 | |
1097 | if (!f) |
1142 | if (!f) |
1098 | { |
1143 | { |
1099 | success = false; |
1144 | success = false; |
1100 | break; |
1145 | break; |
… | |
… | |
1124 | bool careful; |
1169 | bool careful; |
1125 | if (!has_char (*t, &prop, careful)) |
1170 | if (!has_char (*t, &prop, careful)) |
1126 | continue; |
1171 | continue; |
1127 | |
1172 | |
1128 | XGlyphInfo g; |
1173 | XGlyphInfo g; |
1129 | XftTextExtents16 (DISPLAY, f, &ch, 1, &g); |
1174 | XftTextExtents16 (disp, f, &ch, 1, &g); |
1130 | |
1175 | |
1131 | int wcw = wcwidth (ch); |
1176 | int wcw = wcwidth (ch); |
1132 | if (wcw > 0) g.width = g.width / wcw; |
1177 | if (wcw > 0) g.width = g.width / wcw; |
1133 | |
1178 | |
1134 | if (width < g.width) width = g.width; |
1179 | if (width < g.width) width = g.width; |
… | |
… | |
1151 | ftheight -= height - prop.height; |
1196 | ftheight -= height - prop.height; |
1152 | } |
1197 | } |
1153 | else |
1198 | else |
1154 | ftheight = prop.height - 1; |
1199 | ftheight = prop.height - 1; |
1155 | |
1200 | |
1156 | XftFontClose (DISPLAY, f); |
1201 | XftFontClose (disp, f); |
1157 | FcPatternDel (match, FC_PIXEL_SIZE); |
1202 | FcPatternDel (match, FC_PIXEL_SIZE); |
1158 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1203 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1159 | } |
1204 | } |
1160 | |
1205 | |
1161 | FcPatternDestroy (match); |
1206 | FcPatternDestroy (match); |
… | |
… | |
1338 | char buf[512]; |
1383 | char buf[512]; |
1339 | const char *end; |
1384 | const char *end; |
1340 | |
1385 | |
1341 | do |
1386 | do |
1342 | { |
1387 | { |
1343 | while (*desc <= ' ') desc++; |
1388 | while (*desc && *desc <= ' ') |
|
|
1389 | desc++; |
1344 | |
1390 | |
1345 | codeset cs = CS_UNICODE; |
1391 | codeset cs = CS_UNICODE; |
1346 | |
1392 | |
1347 | if (*desc == '[') |
1393 | if (*desc == '[') |
1348 | { |
1394 | { |
… | |
… | |
1506 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
1552 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
1507 | FcPatternAddBool (p, FC_MINSPACE, 1); |
1553 | FcPatternAddBool (p, FC_MINSPACE, 1); |
1508 | //FcPatternAddBool (p, FC_ANTIALIAS, 1); |
1554 | //FcPatternAddBool (p, FC_ANTIALIAS, 1); |
1509 | |
1555 | |
1510 | XftResult result; |
1556 | XftResult result; |
1511 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
1557 | FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result); |
1512 | |
1558 | |
1513 | FcPatternDestroy (p); |
1559 | FcPatternDestroy (p); |
1514 | |
1560 | |
1515 | if (match) |
1561 | if (match) |
1516 | { |
1562 | { |