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 |
… | |
… | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
16 | * |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | *---------------------------------------------------------------------*/ |
20 | *---------------------------------------------------------------------*/ |
21 | |
21 | |
22 | #include "../config.h" |
22 | #include "../config.h" |
23 | #include "rxvt.h" |
23 | #include "rxvt.h" |
24 | #include "rxvtutil.h" |
24 | #include "rxvtutil.h" |
… | |
… | |
90 | { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, |
90 | { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, |
91 | #endif |
91 | #endif |
92 | |
92 | |
93 | #if ENCODING_ZH || ENCODING_ZH_EXT |
93 | #if ENCODING_ZH || ENCODING_ZH_EXT |
94 | # if XFT |
94 | # if XFT |
|
|
95 | { CS_GBK_0, "xft:AR PL KaitiM GB" }, |
|
|
96 | { CS_GBK_0, "xft:AR PL SungtiL GB" }, |
|
|
97 | { CS_GBK_0, "xft::spacing=100:lang=zh" }, |
95 | { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, |
98 | { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, |
96 | { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, |
99 | { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, |
97 | { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, |
100 | { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, |
98 | { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, |
101 | { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, |
99 | { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, |
102 | { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, |
100 | # endif |
103 | # endif |
|
|
104 | { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" }, |
101 | { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, |
105 | { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, |
102 | { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, |
106 | { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, |
103 | { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, |
107 | { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, |
|
|
108 | { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, |
104 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, |
109 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, |
105 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, |
110 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, |
106 | { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, |
111 | { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, |
107 | { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, |
112 | { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, |
108 | { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, |
113 | { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, |
… | |
… | |
125 | { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, |
130 | { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, |
126 | { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, |
131 | { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, |
127 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, |
132 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, |
128 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
133 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
129 | #if XFT |
134 | #if XFT |
130 | { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, |
135 | { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" }, |
131 | { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, |
136 | { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, |
132 | { CS_UNICODE, "xft:Andale Mono:antialias=false" }, |
137 | { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" }, |
133 | { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, |
138 | { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" }, |
134 | |
139 | |
135 | // FreeMono is usually uglier than x fonts, so try last only. |
140 | // FreeMono is usually uglier than x fonts, so try last only. |
136 | { CS_UNICODE, "xft:FreeMono:autohint=true" }, |
141 | { CS_UNICODE, "xft:FreeMono:autohint=true" }, |
137 | #endif |
142 | #endif |
138 | |
143 | |
139 | { CS_UNKNOWN, 0 } |
144 | { CS_UNKNOWN, 0 } |
140 | }; |
145 | }; |
141 | |
146 | |
142 | // these characters are used to guess the font height and width |
147 | // these characters are used to guess the font height and width |
143 | // 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. |
144 | static uint16_t extent_test_chars[] = { |
149 | static uint16_t extent_test_chars[] = { |
145 | '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', |
150 | '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', |
146 | 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ |
151 | 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ |
147 | 0x304c, 0x672c, // が本 |
152 | 0x304c, 0x672c, // が本 |
148 | }; |
153 | }; |
… | |
… | |
326 | void |
331 | void |
327 | rxvt_font_default::draw (rxvt_drawable &d, int x, int y, |
332 | rxvt_font_default::draw (rxvt_drawable &d, int x, int y, |
328 | const text_t *text, int len, |
333 | const text_t *text, int len, |
329 | int fg, int bg) |
334 | int fg, int bg) |
330 | { |
335 | { |
|
|
336 | Display *disp = d.display->display; |
|
|
337 | |
331 | 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); |
332 | |
339 | |
333 | XSetForeground (d.display->display, TGC, r->pix_colors[fg]); |
340 | XSetForeground (disp, TGC, r->pix_colors[fg]); |
334 | |
341 | |
335 | while (len--) |
342 | while (len) |
336 | { |
343 | { |
337 | #if ENABLE_COMBINING |
344 | #if ENABLE_COMBINING |
338 | compose_char *cc; |
345 | compose_char *cc; |
339 | #endif |
346 | #endif |
|
|
347 | const text_t *tp = text; |
340 | text_t t = *text++; |
348 | text_t t = *tp; |
|
|
349 | |
|
|
350 | while (++text, --len && *text == NOCHAR) |
|
|
351 | ; |
|
|
352 | |
|
|
353 | int width = text - tp; |
|
|
354 | int fwidth = r->TermWin.fwidth * width; |
341 | |
355 | |
342 | if (0x2500 <= t && t <= 0x259f) |
356 | if (0x2500 <= t && t <= 0x259f) |
343 | { |
357 | { |
344 | uint16_t offs = linedraw_offs[t - 0x2500]; |
358 | uint16_t offs = linedraw_offs[t - 0x2500]; |
345 | uint32_t *a = linedraw_command + (offs >> 4); |
359 | uint32_t *a = linedraw_command + (offs >> 4); |
346 | uint32_t *b = a + (offs & 15); |
360 | uint32_t *b = a + (offs & 15); |
347 | |
361 | |
348 | int W = r->TermWin.fwidth; |
362 | int W = fwidth; |
349 | int H = r->TermWin.fheight; |
363 | int H = r->TermWin.fheight; |
350 | |
|
|
351 | // support double-width (but not more) graphics chars |
|
|
352 | // TODO: do this outside the if/elsif.. statement for all cases |
|
|
353 | if (len && *text == NOCHAR) |
|
|
354 | W += r->TermWin.fwidth; |
|
|
355 | |
364 | |
356 | int x_[16]; |
365 | int x_[16]; |
357 | int y_[16]; |
366 | int y_[16]; |
358 | |
367 | |
359 | for (int i = 0; i <= 8; i++) |
368 | for (int i = 0; i <= 8; i++) |
… | |
… | |
367 | |
376 | |
368 | XGCValues gcv; |
377 | XGCValues gcv; |
369 | |
378 | |
370 | gcv.cap_style = CapButt; |
379 | gcv.cap_style = CapButt; |
371 | gcv.line_width = 0; |
380 | gcv.line_width = 0; |
372 | XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); |
381 | XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv); |
373 | |
382 | |
374 | while (a < b) |
383 | while (a < b) |
375 | { |
384 | { |
376 | uint32_t command = *a++; |
385 | uint32_t command = *a++; |
377 | |
386 | |
… | |
… | |
384 | int y2 = y_[(command >> 0) & 15]; |
393 | int y2 = y_[(command >> 0) & 15]; |
385 | |
394 | |
386 | switch (op) |
395 | switch (op) |
387 | { |
396 | { |
388 | case 0: // line |
397 | case 0: // line |
389 | XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); |
398 | XDrawLine (disp, d, TGC, x1, y1, x2, y2); |
390 | break; |
399 | break; |
391 | |
400 | |
392 | case 1: // rectangle, possibly stippled |
401 | case 1: // rectangle, possibly stippled |
393 | if (a) |
402 | if (a) |
394 | { |
403 | { |
395 | static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; |
404 | static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; |
396 | |
405 | |
397 | gcv.fill_style = FillStippled; |
406 | gcv.fill_style = FillStippled; |
398 | gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); |
407 | gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2); |
399 | gcv.ts_x_origin = x; |
408 | gcv.ts_x_origin = x; |
400 | gcv.ts_y_origin = y; |
409 | gcv.ts_y_origin = y; |
401 | |
410 | |
402 | XChangeGC (d.display->display, TGC, |
411 | XChangeGC (disp, TGC, |
403 | GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, |
412 | GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, |
404 | &gcv); |
413 | &gcv); |
405 | } |
414 | } |
406 | |
415 | |
407 | 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); |
408 | |
417 | |
409 | if (a) |
418 | if (a) |
410 | { |
419 | { |
411 | XFreePixmap (d.display->display, gcv.stipple); |
420 | XFreePixmap (disp, gcv.stipple); |
412 | gcv.stipple = 0; |
421 | gcv.stipple = 0; |
413 | gcv.fill_style = FillSolid; |
422 | gcv.fill_style = FillSolid; |
414 | XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); |
423 | XChangeGC (disp, TGC, GCFillStyle, &gcv); |
415 | } |
424 | } |
416 | break; |
425 | break; |
417 | case 2: // arc |
426 | case 2: // arc |
418 | XDrawArc (d.display->display, d, TGC, |
427 | XDrawArc (disp, d, TGC, |
419 | x1 - W/2, y1 - H/2, W-1, H-1, |
428 | x1 - W/2, y1 - H/2, W-1, H-1, |
420 | (a - 1) * 90*64, (b - 1) * 90*64); |
429 | (a - 1) * 90*64, (b - 1) * 90*64); |
421 | break; |
430 | break; |
422 | } |
431 | } |
423 | } |
432 | } |
424 | } |
433 | } |
425 | #if ENABLE_COMBINING |
434 | #if ENABLE_COMBINING |
426 | else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) |
435 | else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) |
427 | { |
436 | { |
428 | text_t c2[2]; |
437 | text_t chrs[2]; |
429 | int len2 = 1; |
438 | width = min (2, width); |
430 | |
439 | chrs [1] = NOCHAR; |
431 | // support double-width (but not more) combining characters |
|
|
432 | if (len && *text == NOCHAR) |
|
|
433 | { |
|
|
434 | c2[1] = *text; // not text++ to get correct x-width |
|
|
435 | len2++; |
|
|
436 | } |
|
|
437 | |
440 | |
438 | c2[0] = cc->c1; |
441 | *chrs = cc->c1; |
439 | rxvt_font *f1 = (*fs)[fs->find_font (c2[0])]; |
442 | rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; |
440 | f1->draw (d, x, y, c2, len2, fg, bg); |
443 | f1->draw (d, x, y, chrs, width, fg, bg); |
441 | |
444 | |
442 | if (cc->c2 != NOCHAR) |
445 | if (cc->c2 != NOCHAR) |
443 | { |
446 | { |
444 | bool careful; |
447 | bool careful; |
445 | |
448 | |
446 | // prefer font of first character, for no good reasons |
449 | // prefer font of first character, for no good reasons |
447 | c2[0] = cc->c2; |
450 | *chrs = cc->c2; |
448 | rxvt_font *f2 = (f1->has_char (c2[0], 0, careful) && !careful) |
451 | rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful) |
449 | ? f1 |
452 | ? f1 |
450 | : (*fs)[fs->find_font (c2[0])]; |
453 | : (*fs)[fs->find_font (cc->c2)]; |
451 | |
454 | |
452 | f2->draw (d, x, y, c2, len2, fg, -1); |
455 | f2->draw (d, x, y, chrs, width, fg, -1); |
453 | } |
456 | } |
454 | } |
457 | } |
455 | #endif |
458 | #endif |
456 | else |
459 | else |
457 | switch (t) |
460 | switch (t) |
… | |
… | |
460 | case ZERO_WIDTH_CHAR: |
463 | case ZERO_WIDTH_CHAR: |
461 | case NOCHAR: |
464 | case NOCHAR: |
462 | break; |
465 | break; |
463 | |
466 | |
464 | default: |
467 | default: |
465 | int w = 0; |
|
|
466 | while (len > 0 && *text == NOCHAR) |
|
|
467 | { |
|
|
468 | ++text; |
|
|
469 | --len; |
|
|
470 | w += r->TermWin.fwidth; |
|
|
471 | } |
|
|
472 | |
|
|
473 | XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, |
468 | XDrawRectangle (disp, d, TGC, x + 2, y + 2, |
474 | w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); |
469 | fwidth - 4, r->TermWin.fheight - 4); |
475 | x += w; |
|
|
476 | } |
470 | } |
477 | |
471 | |
478 | x += r->TermWin.fwidth; |
472 | x += fwidth; |
479 | } |
473 | } |
480 | } |
474 | } |
481 | |
475 | |
482 | ///////////////////////////////////////////////////////////////////////////// |
476 | ///////////////////////////////////////////////////////////////////////////// |
483 | |
477 | |
… | |
… | |
539 | |
533 | |
540 | bool |
534 | bool |
541 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
535 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
542 | { |
536 | { |
543 | unsigned long height; |
537 | unsigned long height; |
|
|
538 | |
|
|
539 | #if 0 |
544 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
540 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
545 | return false; |
541 | return false; |
|
|
542 | #else |
|
|
543 | height = f->ascent + f->descent; |
|
|
544 | #endif |
546 | |
545 | |
547 | unsigned long avgwidth; |
546 | unsigned long avgwidth; |
548 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
547 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
549 | avgwidth = 0; |
548 | avgwidth = 0; |
550 | |
549 | |
… | |
… | |
633 | } |
632 | } |
634 | |
633 | |
635 | bool |
634 | bool |
636 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
635 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
637 | { |
636 | { |
|
|
637 | Display *disp = DISPLAY; |
|
|
638 | |
638 | clear (); |
639 | clear (); |
639 | |
640 | |
640 | char field_str[64]; // enough for 128 bits |
641 | char field_str[64]; // enough for 128 bits |
641 | |
642 | |
642 | // first morph the font if required |
643 | // first morph the font if required |
… | |
… | |
645 | { |
646 | { |
646 | char fname[1024]; |
647 | char fname[1024]; |
647 | |
648 | |
648 | if (name[0] != '-') |
649 | if (name[0] != '-') |
649 | { |
650 | { |
650 | f = XLoadQueryFont (DISPLAY, name); |
651 | f = XLoadQueryFont (disp, name); |
651 | |
652 | |
652 | if (!f) |
653 | if (!f) |
653 | return false; |
654 | return false; |
654 | |
655 | |
655 | char *new_name = get_property (f, "FONT", name); |
656 | char *new_name = get_property (f, "FONT", name); |
… | |
… | |
657 | if (new_name) |
658 | if (new_name) |
658 | set_name (new_name); |
659 | set_name (new_name); |
659 | else |
660 | else |
660 | rxvt_warn ("font '%s' has no FONT property, continuing without.", name); |
661 | rxvt_warn ("font '%s' has no FONT property, continuing without.", name); |
661 | |
662 | |
662 | XFreeFont (DISPLAY, f); |
663 | XFreeFont (disp, f); |
663 | f = 0; |
664 | f = 0; |
664 | } |
665 | } |
665 | |
666 | |
666 | if (prop.weight != rxvt_fontprop::unset) |
667 | if (prop.weight != rxvt_fontprop::unset) |
667 | { |
668 | { |
… | |
… | |
678 | ? "r" : "i"); // TODO: handle "o"blique, too |
679 | ? "r" : "i"); // TODO: handle "o"blique, too |
679 | set_name (strdup (fname)); |
680 | set_name (strdup (fname)); |
680 | } |
681 | } |
681 | } |
682 | } |
682 | |
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 | |
683 | char **list; |
701 | char **list; |
684 | int count; |
702 | int count; |
685 | list = XListFonts (DISPLAY, name, 1024, &count); |
703 | list = XListFonts (disp, name, 4000, &count); |
686 | |
704 | |
687 | set_name (0); |
705 | set_name (0); |
688 | |
706 | |
689 | if (!list) |
707 | if (!list) |
690 | return false; |
708 | return false; |
691 | |
709 | |
692 | sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset |
710 | font_weight *fonts = new font_weight[count]; |
693 | ? 0 : prop.height); |
|
|
694 | |
711 | |
695 | int bestdiff = 0x7fffffff; |
|
|
696 | for (int i = 0; i < count; i++) |
712 | for (int i = 0; i < count; i++) |
697 | { |
713 | { |
698 | rxvt_fontprop p; |
714 | rxvt_fontprop p; |
699 | char fname[1024]; |
715 | char fname[1024]; |
700 | |
716 | |
701 | int diff = 0; |
717 | int diff = 0; |
702 | |
718 | |
703 | if (replace_field (fname, list[i], 6, '0', field_str)) |
719 | if (replace_field (fname, list[i], 6, '0', field_str)) |
704 | 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 |
705 | |
723 | |
706 | if (!set_properties (p, fname)) |
724 | if (!set_properties (p, fname)) |
707 | continue; |
725 | continue; |
708 | |
726 | |
709 | if (prop.height != rxvt_fontprop::unset |
727 | if (prop.height != rxvt_fontprop::unset |
… | |
… | |
713 | 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; |
714 | 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); |
715 | 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); |
716 | //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); |
717 | |
735 | |
718 | if (!name // compare against best found so far |
736 | fonts[i].name = strdup (fname); |
719 | || diff < bestdiff) |
737 | fonts[i].diff = diff; |
720 | { |
|
|
721 | set_name (strdup (fname)); |
|
|
722 | bestdiff = diff; |
|
|
723 | } |
|
|
724 | } |
738 | } |
725 | |
739 | |
726 | XFreeFontNames (list); |
740 | XFreeFontNames (list); |
727 | |
741 | |
728 | if (!name) |
742 | // this loop only iterates when the guessed font-size is too small |
729 | return false; |
743 | for (;;) |
|
|
744 | { |
|
|
745 | font_weight *best = fonts + count - 1; |
730 | |
746 | |
731 | 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; |
732 | |
771 | |
733 | if (!f) |
772 | if (!f) |
734 | return false; |
773 | return false; |
735 | |
774 | |
736 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
775 | char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
… | |
… | |
764 | if (cs == CS_UNICODE) |
803 | if (cs == CS_UNICODE) |
765 | 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 |
766 | |
805 | |
767 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
806 | encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
768 | enc2b = encm || f->max_char_or_byte2 > 255; |
807 | enc2b = encm || f->max_char_or_byte2 > 255; |
769 | |
|
|
770 | ascent = f->ascent; |
|
|
771 | descent = f->descent; |
|
|
772 | height = ascent + descent; |
|
|
773 | |
808 | |
774 | slow = false; |
809 | slow = false; |
775 | |
810 | |
776 | #if 1 // only used for slow detection, TODO optimize |
811 | #if 1 // only used for slow detection, TODO optimize |
777 | if (f->min_bounds.width == f->max_bounds.width) |
812 | if (f->min_bounds.width == f->max_bounds.width) |
… | |
… | |
816 | |
851 | |
817 | XCharStruct g; |
852 | XCharStruct g; |
818 | int dir_ret, asc_ret, des_ret; |
853 | int dir_ret, asc_ret, des_ret; |
819 | XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); |
854 | XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); |
820 | |
855 | |
821 | int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; |
856 | int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; |
822 | |
857 | |
823 | if (width < g.width) width = g.width; |
858 | if (width < g.width) width = g.width; |
824 | } |
859 | } |
825 | |
860 | |
826 | if (cs == CS_UNKNOWN) |
861 | if (cs == CS_UNKNOWN) |
… | |
… | |
901 | return true; |
936 | return true; |
902 | |
937 | |
903 | // check character against base font bounding box |
938 | // check character against base font bounding box |
904 | int w = xcs->width; |
939 | int w = xcs->width; |
905 | int wcw = wcwidth (unicode); |
940 | int wcw = wcwidth (unicode); |
906 | if (wcw > 0) w /= wcw; |
941 | if (wcw > 0) w = (w + wcw - 1) / wcw; |
907 | |
942 | |
908 | careful = w > prop->width; |
943 | careful = w > prop->width; |
909 | if (careful && w > prop->width * MAX_OVERLAP >> 2) |
944 | if (careful && w > prop->width * MAX_OVERLAP >> 2) |
910 | return false; |
945 | return false; |
911 | |
946 | |
… | |
… | |
1051 | } |
1086 | } |
1052 | |
1087 | |
1053 | bool |
1088 | bool |
1054 | rxvt_font_xft::load (const rxvt_fontprop &prop) |
1089 | rxvt_font_xft::load (const rxvt_fontprop &prop) |
1055 | { |
1090 | { |
|
|
1091 | Display *disp = DISPLAY; |
|
|
1092 | |
1056 | clear (); |
1093 | clear (); |
1057 | |
1094 | |
1058 | FcPattern *p = FcNameParse ((FcChar8 *) name); |
1095 | FcPattern *p = FcNameParse ((FcChar8 *) name); |
1059 | |
1096 | |
1060 | if (!p) |
1097 | if (!p) |
1061 | return false; |
1098 | return false; |
1062 | |
1099 | |
1063 | FcValue v; |
1100 | FcValue v; |
1064 | |
1101 | |
1065 | if (prop.height != rxvt_fontprop::unset |
1102 | if (prop.height != rxvt_fontprop::unset |
1066 | || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch |
1103 | && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch |
1067 | && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) |
1104 | && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) |
1068 | FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); |
1105 | FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); |
1069 | |
1106 | |
1070 | if (prop.weight != rxvt_fontprop::unset |
1107 | if (prop.weight != rxvt_fontprop::unset |
1071 | && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
1108 | && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
… | |
… | |
1086 | |
1123 | |
1087 | // store generated name so iso14755 view gives better results |
1124 | // store generated name so iso14755 view gives better results |
1088 | set_name ((char *)FcNameUnparse (p)); |
1125 | set_name ((char *)FcNameUnparse (p)); |
1089 | |
1126 | |
1090 | XftResult result; |
1127 | XftResult result; |
1091 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
1128 | FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result); |
1092 | |
1129 | |
1093 | FcPatternDestroy (p); |
1130 | FcPatternDestroy (p); |
1094 | |
1131 | |
1095 | if (!match) |
1132 | if (!match) |
1096 | return false; |
1133 | return false; |
… | |
… | |
1098 | int ftheight = 0; |
1135 | int ftheight = 0; |
1099 | bool success = true; |
1136 | bool success = true; |
1100 | |
1137 | |
1101 | for (;;) |
1138 | for (;;) |
1102 | { |
1139 | { |
1103 | f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); |
1140 | f = XftFontOpenPattern (disp, FcPatternDuplicate (match)); |
1104 | |
1141 | |
1105 | if (!f) |
1142 | if (!f) |
1106 | { |
1143 | { |
1107 | success = false; |
1144 | success = false; |
1108 | break; |
1145 | break; |
… | |
… | |
1117 | |
1154 | |
1118 | bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; |
1155 | bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; |
1119 | |
1156 | |
1120 | XftUnlockFace (f); |
1157 | XftUnlockFace (f); |
1121 | |
1158 | |
|
|
1159 | int glheight = height; |
|
|
1160 | |
1122 | for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) |
1161 | for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) |
1123 | { |
1162 | { |
1124 | FcChar16 ch = *t; |
1163 | FcChar16 ch = *t; |
1125 | |
1164 | |
1126 | if (cs != CS_UNICODE |
1165 | if (cs != CS_UNICODE |
… | |
… | |
1132 | bool careful; |
1171 | bool careful; |
1133 | if (!has_char (*t, &prop, careful)) |
1172 | if (!has_char (*t, &prop, careful)) |
1134 | continue; |
1173 | continue; |
1135 | |
1174 | |
1136 | XGlyphInfo g; |
1175 | XGlyphInfo g; |
1137 | XftTextExtents16 (DISPLAY, f, &ch, 1, &g); |
1176 | XftTextExtents16 (disp, f, &ch, 1, &g); |
|
|
1177 | |
|
|
1178 | g.width -= g.x; |
1138 | |
1179 | |
1139 | int wcw = wcwidth (ch); |
1180 | int wcw = wcwidth (ch); |
1140 | if (wcw > 0) g.width = g.width / wcw; |
1181 | if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; |
1141 | |
1182 | |
1142 | if (width < g.width) width = g.width; |
1183 | if (width < g.width ) width = g.width; |
1143 | if (height < g.height) height = g.height; |
1184 | if (height < g.height ) height = g.height; |
|
|
1185 | if (glheight < g.height - g.y) glheight = g.height - g.y; |
|
|
1186 | } |
|
|
1187 | |
|
|
1188 | if (!width) |
|
|
1189 | { |
|
|
1190 | rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name); |
|
|
1191 | |
|
|
1192 | XftFontClose (disp, f); |
|
|
1193 | f = 0; |
|
|
1194 | |
|
|
1195 | success = false; |
|
|
1196 | break; |
1144 | } |
1197 | } |
1145 | |
1198 | |
1146 | if (prop.height == rxvt_fontprop::unset |
1199 | if (prop.height == rxvt_fontprop::unset |
1147 | || height <= prop.height |
1200 | || (height <= prop.height && glheight <= prop.height) |
1148 | || height <= 2 |
1201 | || height <= 2 |
1149 | || !scalable) |
1202 | || !scalable) |
1150 | break; |
1203 | break; |
1151 | |
1204 | |
1152 | if (ftheight) |
1205 | if (ftheight) |
… | |
… | |
1159 | ftheight -= height - prop.height; |
1212 | ftheight -= height - prop.height; |
1160 | } |
1213 | } |
1161 | else |
1214 | else |
1162 | ftheight = prop.height - 1; |
1215 | ftheight = prop.height - 1; |
1163 | |
1216 | |
1164 | XftFontClose (DISPLAY, f); |
1217 | XftFontClose (disp, f); |
1165 | FcPatternDel (match, FC_PIXEL_SIZE); |
1218 | FcPatternDel (match, FC_PIXEL_SIZE); |
1166 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1219 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1167 | } |
1220 | } |
1168 | |
1221 | |
1169 | FcPatternDestroy (match); |
1222 | FcPatternDestroy (match); |
1170 | |
1223 | |
1171 | #if 0 // do it per-character |
1224 | #if 0 // do it per-character |
… | |
… | |
1193 | // check character against base font bounding box |
1246 | // check character against base font bounding box |
1194 | FcChar32 ch = unicode; |
1247 | FcChar32 ch = unicode; |
1195 | XGlyphInfo g; |
1248 | XGlyphInfo g; |
1196 | XftTextExtents32 (DISPLAY, f, &ch, 1, &g); |
1249 | XftTextExtents32 (DISPLAY, f, &ch, 1, &g); |
1197 | |
1250 | |
1198 | int w = g.width; |
1251 | int w = g.width - g.x; |
1199 | int wcw = wcwidth (unicode); |
1252 | int wcw = wcwidth (unicode); |
1200 | if (wcw > 0) w /= wcw; |
1253 | if (wcw > 0) w = (w + wcw - 1) / wcw; |
1201 | |
1254 | |
1202 | careful = w > prop->width; |
1255 | careful = w > prop->width; |
1203 | if (careful && w > prop->width * MAX_OVERLAP >> 2) |
1256 | if (careful && w > prop->width * MAX_OVERLAP >> 2) |
1204 | return false; |
1257 | return false; |
1205 | |
1258 | |
… | |
… | |
1217 | |
1270 | |
1218 | XGlyphInfo extents; |
1271 | XGlyphInfo extents; |
1219 | FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); |
1272 | FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); |
1220 | FcChar32 *ep = enc; |
1273 | FcChar32 *ep = enc; |
1221 | int ewidth = 0; |
1274 | int ewidth = 0; |
1222 | int xoff = 0; |
|
|
1223 | |
1275 | |
1224 | while (len) |
1276 | while (len) |
1225 | { |
1277 | { |
1226 | int cwidth = r->TermWin.fwidth; |
1278 | int cwidth = r->TermWin.fwidth; |
1227 | FcChar32 fc = *text++; len--; |
1279 | FcChar32 fc = *text++; len--; |
1228 | FT_UInt gl; |
|
|
1229 | |
1280 | |
1230 | while (len && *text == NOCHAR) |
1281 | while (len && *text == NOCHAR) |
1231 | text++, len--, cwidth += r->TermWin.fwidth; |
1282 | text++, len--, cwidth += r->TermWin.fwidth; |
1232 | |
1283 | |
1233 | gl = XftCharIndex (d.display->display, f, fc); |
|
|
1234 | XftGlyphExtents (d.display->display, f, &gl, 1, &extents); |
|
|
1235 | |
|
|
1236 | if (extents.xOff != cwidth && ep != enc) |
|
|
1237 | { |
|
|
1238 | if (xoff > ewidth) xoff = ewidth; |
|
|
1239 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
|
|
1240 | x + (ewidth - xoff >> 1), |
|
|
1241 | y + base, enc, ep - enc); |
|
|
1242 | x += ewidth; |
|
|
1243 | |
|
|
1244 | ep = enc; |
|
|
1245 | ewidth = 0; |
|
|
1246 | xoff = 0; |
|
|
1247 | } |
|
|
1248 | |
|
|
1249 | if (fc == ' ' && ep == enc) // skip leading spaces |
1284 | if (fc == ' ' && ep == enc) // skip leading spaces |
1250 | { |
|
|
1251 | x += cwidth; |
1285 | x += cwidth; |
1252 | continue; |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | else |
1286 | else |
1256 | { |
1287 | { |
|
|
1288 | FT_UInt gl = XftCharIndex (d.display->display, f, fc); |
|
|
1289 | XftGlyphExtents (d.display->display, f, &gl, 1, &extents); |
|
|
1290 | |
|
|
1291 | if (extents.xOff != cwidth) |
|
|
1292 | { |
|
|
1293 | if (ewidth) |
|
|
1294 | { |
|
|
1295 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
|
|
1296 | x, y + base, enc, ep - enc); |
|
|
1297 | x += ewidth; |
|
|
1298 | |
|
|
1299 | ep = enc; |
|
|
1300 | ewidth = 0; |
|
|
1301 | } |
|
|
1302 | |
|
|
1303 | if (extents.xOff > cwidth) |
|
|
1304 | extents.xOff = cwidth; |
|
|
1305 | |
|
|
1306 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
|
|
1307 | x + (cwidth - extents.xOff >> 1), |
|
|
1308 | y + base, &gl, 1); |
|
|
1309 | x += cwidth; |
|
|
1310 | } |
|
|
1311 | else |
|
|
1312 | { |
1257 | *ep++ = gl; |
1313 | *ep++ = gl; |
1258 | ewidth += cwidth; |
1314 | ewidth += cwidth; |
1259 | xoff += extents.xOff; |
1315 | } |
1260 | } |
1316 | } |
1261 | } |
1317 | } |
1262 | |
1318 | |
1263 | if (ep != enc) |
1319 | if (ep != enc) |
1264 | { |
|
|
1265 | if (xoff > ewidth) xoff = ewidth; |
|
|
1266 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
1320 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
1267 | x + (ewidth - xoff >> 1), |
|
|
1268 | y + base, enc, ep - enc); |
1321 | x, y + base, enc, ep - enc); |
1269 | } |
|
|
1270 | } |
1322 | } |
1271 | #endif |
1323 | #endif |
1272 | |
1324 | |
1273 | ///////////////////////////////////////////////////////////////////////////// |
1325 | ///////////////////////////////////////////////////////////////////////////// |
1274 | |
1326 | |
1275 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
1327 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
1276 | : r (r), fontdesc (0) |
1328 | : fontdesc (0), r (r) |
1277 | { |
1329 | { |
1278 | clear (); |
1330 | clear (); |
1279 | } |
1331 | } |
1280 | |
1332 | |
1281 | rxvt_fontset::~rxvt_fontset () |
1333 | rxvt_fontset::~rxvt_fontset () |
… | |
… | |
1346 | char buf[512]; |
1398 | char buf[512]; |
1347 | const char *end; |
1399 | const char *end; |
1348 | |
1400 | |
1349 | do |
1401 | do |
1350 | { |
1402 | { |
1351 | while (*desc <= ' ') desc++; |
1403 | while (*desc && *desc <= ' ') |
|
|
1404 | desc++; |
1352 | |
1405 | |
1353 | codeset cs = CS_UNICODE; |
1406 | codeset cs = CS_UNICODE; |
1354 | |
1407 | |
1355 | if (*desc == '[') |
1408 | if (*desc == '[') |
1356 | { |
1409 | { |
… | |
… | |
1514 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
1567 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
1515 | FcPatternAddBool (p, FC_MINSPACE, 1); |
1568 | FcPatternAddBool (p, FC_MINSPACE, 1); |
1516 | //FcPatternAddBool (p, FC_ANTIALIAS, 1); |
1569 | //FcPatternAddBool (p, FC_ANTIALIAS, 1); |
1517 | |
1570 | |
1518 | XftResult result; |
1571 | XftResult result; |
1519 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
1572 | FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result); |
1520 | |
1573 | |
1521 | FcPatternDestroy (p); |
1574 | FcPatternDestroy (p); |
1522 | |
1575 | |
1523 | if (match) |
1576 | if (match) |
1524 | { |
1577 | { |