… | |
… | |
21 | |
21 | |
22 | #include "../config.h" |
22 | #include "../config.h" |
23 | #include "rxvt.h" |
23 | #include "rxvt.h" |
24 | #include "defaultfont.h" |
24 | #include "defaultfont.h" |
25 | |
25 | |
|
|
26 | #include <cstdlib> |
|
|
27 | |
26 | #define DISPLAY r->Xdisplay |
28 | #define DISPLAY r->display->display |
27 | #define DRAWABLE r->TermWin.vt |
29 | #define DRAWABLE r->TermWin.vt |
28 | #define GC r->TermWin.gc |
30 | #define GC r->TermWin.gc |
29 | |
31 | |
30 | const struct rxvt_fallback_font { |
32 | const struct rxvt_fallback_font { |
31 | codeset cs; |
33 | codeset cs; |
… | |
… | |
57 | |
59 | |
58 | // japanese |
60 | // japanese |
59 | #if ENCODING_JP || ENCODING_JP_EXT |
61 | #if ENCODING_JP || ENCODING_JP_EXT |
60 | # if XFT |
62 | # if XFT |
61 | // prefer xft for complex scripts |
63 | // prefer xft for complex scripts |
62 | { CS_UNICODE, "xft:Kochi Gothic" }, |
64 | { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, |
63 | # endif |
65 | # endif |
64 | { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, |
66 | { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, |
65 | { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, |
67 | { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, |
66 | { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, |
68 | { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, |
67 | #endif |
69 | #endif |
68 | |
70 | |
69 | #if ENCODING_CN || ENCODING_CN_EXT |
71 | #if ENCODING_CN || ENCODING_CN_EXT |
70 | # if XFT |
72 | # if XFT |
71 | { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, |
73 | { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, |
72 | { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, |
74 | { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, |
73 | { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, |
75 | { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, |
74 | { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, |
76 | { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, |
75 | # endif |
77 | # endif |
|
|
78 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, |
76 | { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, |
79 | { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, |
77 | { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, |
80 | { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, |
78 | { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, |
81 | { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, |
79 | { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, |
82 | { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, |
80 | { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, |
83 | { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" }, |
81 | { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, |
84 | { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, |
82 | { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, |
85 | { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, |
83 | { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, |
86 | { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, |
84 | #endif |
87 | #endif |
85 | |
88 | |
86 | #if XFT |
89 | #if XFT |
87 | { CS_UNICODE, "xft:Andale Mono" }, |
90 | { CS_UNICODE, "xft:Andale Mono" }, |
88 | { CS_UNICODE, "xft:Arial Unicode MS" }, |
91 | { CS_UNICODE, "xft:Arial Unicode MS" }, |
89 | #endif |
92 | #endif |
90 | { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
93 | { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, |
|
|
94 | { CS_UNICODE, "xft:FreeMono" }, |
|
|
95 | { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, |
|
|
96 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, |
91 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
97 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
92 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
|
|
93 | { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
|
|
94 | |
98 | |
95 | { CS_UNKNOWN, 0 } |
99 | { CS_UNKNOWN, 0 } |
96 | }; |
100 | }; |
97 | |
101 | |
98 | ///////////////////////////////////////////////////////////////////////////// |
102 | ///////////////////////////////////////////////////////////////////////////// |
99 | |
103 | |
100 | static void *enc_buf; |
104 | static void *enc_buf; |
101 | static uint32_t enc_len; |
105 | static uint32_t enc_len; |
102 | |
106 | |
103 | static inline void * |
107 | static inline void * |
104 | get_enc_buf (int len) |
108 | get_enc_buf (uint32_t len) |
105 | { |
109 | { |
106 | if (len > enc_len) |
110 | if (len > enc_len) |
107 | { |
111 | { |
108 | free (enc_buf); |
112 | free (enc_buf); |
109 | enc_buf = malloc (len); |
113 | enc_buf = malloc (len); |
… | |
… | |
111 | |
115 | |
112 | return enc_buf; |
116 | return enc_buf; |
113 | } |
117 | } |
114 | |
118 | |
115 | static const char * |
119 | static const char * |
116 | enc_char (const text_t *text, int len, codeset cs, bool &zero) |
120 | enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero) |
117 | { |
121 | { |
118 | uint8_t *buf = (uint8_t *)get_enc_buf (len); |
122 | uint8_t *buf = (uint8_t *)get_enc_buf (len); |
119 | |
123 | |
120 | while (len--) |
124 | while (len--) |
121 | { |
125 | { |
… | |
… | |
132 | |
136 | |
133 | return (const char *)enc_buf; |
137 | return (const char *)enc_buf; |
134 | } |
138 | } |
135 | |
139 | |
136 | static const XChar2b * |
140 | static const XChar2b * |
137 | enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) |
141 | enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero) |
138 | { |
142 | { |
139 | XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); |
143 | XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); |
140 | |
144 | |
141 | while (len--) |
145 | while (len--) |
142 | { |
146 | { |
… | |
… | |
220 | |
224 | |
221 | // to be done |
225 | // to be done |
222 | }; |
226 | }; |
223 | |
227 | |
224 | struct rxvt_font_default : rxvt_font { |
228 | struct rxvt_font_default : rxvt_font { |
225 | bool load (int maxheight) |
229 | rxvt_fontprop properties () |
|
|
230 | { |
|
|
231 | rxvt_fontprop p; |
|
|
232 | |
|
|
233 | p.width = p.height = 1; |
|
|
234 | p.weight = rxvt_fontprop::medium; |
|
|
235 | p.slant = rxvt_fontprop::roman; |
|
|
236 | |
|
|
237 | return p; |
|
|
238 | } |
|
|
239 | |
|
|
240 | bool load (const rxvt_fontprop &prop) |
226 | { |
241 | { |
227 | width = 1; height = 1; |
242 | width = 1; height = 1; |
228 | ascent = 1; descent = 0; |
243 | ascent = 1; descent = 0; |
229 | |
244 | |
230 | return true; |
245 | return true; |
… | |
… | |
322 | struct rxvt_font_x11 : rxvt_font { |
337 | struct rxvt_font_x11 : rxvt_font { |
323 | rxvt_font_x11 () { f = 0; } |
338 | rxvt_font_x11 () { f = 0; } |
324 | |
339 | |
325 | void clear (); |
340 | void clear (); |
326 | |
341 | |
327 | bool load (int maxheight); |
342 | rxvt_fontprop properties (); |
|
|
343 | |
|
|
344 | bool load (const rxvt_fontprop &prop); |
328 | |
345 | |
329 | bool has_codepoint (uint32_t unicode); |
346 | bool has_codepoint (uint32_t unicode); |
330 | |
347 | |
331 | void draw (int x, int y, |
348 | void draw (int x, int y, |
332 | const text_t *text, int len, |
349 | const text_t *text, int len, |
… | |
… | |
334 | |
351 | |
335 | XFontStruct *f; |
352 | XFontStruct *f; |
336 | codeset cs; |
353 | codeset cs; |
337 | bool enc2b, encm; |
354 | bool enc2b, encm; |
338 | |
355 | |
339 | const char *get_property (const char *property, const char *repl) const; |
356 | const char *get_property (XFontStruct *f, const char *property, const char *repl) const; |
|
|
357 | bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth); |
|
|
358 | bool set_properties (rxvt_fontprop &p, XFontStruct *f); |
|
|
359 | bool set_properties (rxvt_fontprop &p, const char *name); |
340 | }; |
360 | }; |
341 | |
361 | |
342 | const char * |
362 | const char * |
343 | rxvt_font_x11::get_property (const char *property, const char *repl) const |
363 | rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const |
344 | { |
364 | { |
345 | unsigned long value; |
365 | unsigned long value; |
346 | |
366 | |
347 | if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) |
367 | if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) |
348 | return XGetAtomName (DISPLAY, value); |
368 | return XGetAtomName (DISPLAY, value); |
349 | else |
369 | else |
350 | return repl; |
370 | return repl; |
351 | } |
371 | } |
352 | |
372 | |
|
|
373 | rxvt_fontprop |
|
|
374 | rxvt_font_x11::properties () |
|
|
375 | { |
|
|
376 | rxvt_fontprop p; |
|
|
377 | set_properties (p, f); |
|
|
378 | return p; |
|
|
379 | } |
|
|
380 | |
353 | bool |
381 | bool |
354 | rxvt_font_x11::load (int maxheight) |
382 | rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) |
|
|
383 | { |
|
|
384 | p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; |
|
|
385 | p.height = height; |
|
|
386 | p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
|
|
387 | p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; |
|
|
388 | |
|
|
389 | return true; |
|
|
390 | } |
|
|
391 | |
|
|
392 | bool |
|
|
393 | rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) |
|
|
394 | { |
|
|
395 | const char *weight = get_property (f, "WEIGHT_NAME", "medium"); |
|
|
396 | const char *slant = get_property (f, "SLANT", "r"); |
|
|
397 | |
|
|
398 | unsigned long height; |
|
|
399 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
|
|
400 | return false; |
|
|
401 | |
|
|
402 | unsigned long avgwidth; |
|
|
403 | if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) |
|
|
404 | avgwidth = 0; |
|
|
405 | |
|
|
406 | return set_properties (p, height, weight, slant, avgwidth); |
|
|
407 | } |
|
|
408 | |
|
|
409 | bool |
|
|
410 | rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) |
|
|
411 | { |
|
|
412 | int slashes = 0; |
|
|
413 | const char *comp[12]; |
|
|
414 | |
|
|
415 | for (const char *c = name; *c; c++) |
|
|
416 | if (*c == '-') |
|
|
417 | { |
|
|
418 | comp[slashes++] = c + 1; |
|
|
419 | if (slashes >= 13) |
|
|
420 | break; |
|
|
421 | } |
|
|
422 | |
|
|
423 | /* can we short-circuit the costly XLoadQueryFont? */ |
|
|
424 | if (slashes >= 13 |
|
|
425 | && (*comp[ 6] >= '1' && *comp[ 6] <= '9') |
|
|
426 | && (*comp[11] >= '0' && *comp[11] <= '9')) |
|
|
427 | return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11])); |
|
|
428 | |
|
|
429 | XFontStruct *f = XLoadQueryFont (DISPLAY, name); |
|
|
430 | |
|
|
431 | if (f) |
|
|
432 | { |
|
|
433 | // the font should really exist now. if not, we have a problem |
|
|
434 | // (e.g. if the user did xset fp rehash just when we were searching fonts). |
|
|
435 | // in that case, just return garbage. |
|
|
436 | bool ret = set_properties (p, f); |
|
|
437 | XFreeFont (DISPLAY, f); |
|
|
438 | return ret; |
|
|
439 | } |
|
|
440 | else |
|
|
441 | return false; |
|
|
442 | } |
|
|
443 | |
|
|
444 | // fix the size of scalable fonts |
|
|
445 | static void |
|
|
446 | fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop) |
|
|
447 | { |
|
|
448 | int slashes = 0; |
|
|
449 | const char *size; |
|
|
450 | |
|
|
451 | for (const char *c = name; *c; c++) |
|
|
452 | if (*c == '-') |
|
|
453 | { |
|
|
454 | if (slashes == 6) |
|
|
455 | size = c + 1; |
|
|
456 | |
|
|
457 | if (++slashes >= 13) |
|
|
458 | break; |
|
|
459 | } |
|
|
460 | |
|
|
461 | if (slashes >= 13 && size[0] == '0') |
|
|
462 | { |
|
|
463 | strncpy (buf, name, size - name); |
|
|
464 | buf += size - name; |
|
|
465 | buf += sprintf (buf, "%d", prop.height); |
|
|
466 | strcpy (buf, size + 1); |
|
|
467 | } |
|
|
468 | else |
|
|
469 | strcpy (buf, name); |
|
|
470 | } |
|
|
471 | |
|
|
472 | bool |
|
|
473 | rxvt_font_x11::load (const rxvt_fontprop &prop) |
355 | { |
474 | { |
356 | clear (); |
475 | clear (); |
357 | |
476 | |
358 | char **list; |
477 | char **list; |
359 | int count; |
478 | int count; |
360 | XFontStruct *info; |
|
|
361 | list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); |
479 | list = XListFonts (DISPLAY, name, 512, &count); |
|
|
480 | set_name (0); |
362 | |
481 | |
363 | if (!list) |
482 | if (!list) |
364 | return false; |
483 | return false; |
365 | |
484 | |
366 | XFontStruct *best = 0; |
485 | int bestdiff = 0x7fffffff; |
367 | for (int i = 0; i < count; i++) |
486 | for (int i = 0; i < count; i++) |
368 | { |
487 | { |
369 | XFontStruct *f = info + i; |
488 | rxvt_fontprop p; |
370 | if (f->ascent + f->descent <= maxheight) // weed out too large fonts |
489 | char fname[1024]; |
|
|
490 | fix_scalable (fname, list[i], prop); |
|
|
491 | |
|
|
492 | if (!set_properties (p, fname)) |
|
|
493 | continue; |
|
|
494 | |
|
|
495 | if (p.height > prop.height) // weed out too large fonts |
|
|
496 | continue; |
|
|
497 | |
|
|
498 | int diff = (prop.height - p.height) * 32 |
|
|
499 | + abs (prop.weight - p.weight) |
|
|
500 | + abs (prop.slant - p.slant ); |
|
|
501 | |
371 | if (!best // compare against best found so far |
502 | if (!name // compare against best found so far |
372 | || best->ascent + best->descent < f->ascent + f->descent) |
503 | || diff < bestdiff) |
|
|
504 | { |
|
|
505 | set_name (strdup (fname)); |
373 | best = f; |
506 | bestdiff = diff; |
|
|
507 | } |
374 | } |
508 | } |
375 | |
509 | |
376 | set_name (strdup (list[best - info])); |
510 | XFreeFontNames (list); |
377 | |
511 | |
378 | XFreeFontInfo (list, info, count); |
512 | if (!name) |
|
|
513 | return false; |
379 | |
514 | |
380 | f = XLoadQueryFont (DISPLAY, name); |
515 | f = XLoadQueryFont (DISPLAY, name); |
381 | |
516 | |
382 | if (!f) |
517 | if (!f) |
383 | return false; |
518 | return false; |
384 | |
519 | |
385 | unsigned long value; |
|
|
386 | |
|
|
387 | const char *registry = get_property ("CHARSET_REGISTRY", 0); |
520 | const char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
388 | const char *encoding = get_property ("CHARSET_ENCODING", 0); |
521 | const char *encoding = get_property (f, "CHARSET_ENCODING", 0); |
389 | |
522 | |
390 | if (registry && encoding) |
523 | if (registry && encoding) |
391 | { |
524 | { |
392 | char charset[64]; |
525 | char charset[64]; |
393 | snprintf (charset, 64, "%s-%s", registry, encoding); |
526 | snprintf (charset, 64, "%s-%s", registry, encoding); |
394 | |
527 | |
395 | cs = codeset_from_name (charset); |
528 | cs = codeset_from_name (charset); |
396 | } |
529 | } |
397 | else |
530 | else |
398 | { |
531 | { |
399 | const char *charset = get_property ("FONT", 0); |
532 | const char *charset = get_property (f, "FONT", 0); |
400 | |
533 | |
401 | if (!charset) |
534 | if (!charset) |
402 | charset = name; |
535 | charset = name; |
403 | |
536 | |
404 | int count = 13; |
537 | int count = 13; |
… | |
… | |
417 | |
550 | |
418 | ascent = f->ascent; |
551 | ascent = f->ascent; |
419 | descent = f->descent; |
552 | descent = f->descent; |
420 | height = ascent + descent; |
553 | height = ascent + descent; |
421 | |
554 | |
422 | prop = false; |
555 | slow = false; |
423 | |
556 | |
424 | if (f->min_bounds.width == f->max_bounds.width) |
557 | if (f->min_bounds.width == f->max_bounds.width) |
425 | width = f->min_bounds.width; |
558 | width = f->min_bounds.width; |
426 | else if (f->per_char == NULL) |
559 | else if (f->per_char == NULL) |
427 | width = f->max_bounds.width; |
560 | width = f->max_bounds.width; |
428 | else |
561 | else |
429 | { |
562 | { |
430 | prop = true; |
563 | slow = true; |
431 | |
564 | |
432 | int N = f->max_char_or_byte2 - f->min_char_or_byte2; |
565 | int N = f->max_char_or_byte2 - f->min_char_or_byte2; |
433 | |
566 | |
434 | if (encm) |
567 | if (encm) |
435 | N += (f->max_byte1 - f->min_byte1) |
568 | N += (f->max_byte1 - f->min_byte1) |
… | |
… | |
477 | /* check wether the character exists in _this_ font. horrible. */ |
610 | /* check wether the character exists in _this_ font. horrible. */ |
478 | XCharStruct *xcs; |
611 | XCharStruct *xcs; |
479 | |
612 | |
480 | if (encm) |
613 | if (encm) |
481 | { |
614 | { |
482 | int byte1 = ch >> 8; |
615 | unsigned char byte1 = ch >> 8; |
483 | int byte2 = ch & 255; |
616 | unsigned char byte2 = ch & 255; |
484 | |
617 | |
485 | if (byte1 < f->min_byte1 || byte1 > f->max_byte1 |
618 | if (byte1 < f->min_byte1 || byte1 > f->max_byte1 |
486 | || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) |
619 | || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) |
487 | return false; |
620 | return false; |
488 | |
621 | |
… | |
… | |
520 | // this looks like a mess /. |
653 | // this looks like a mess /. |
521 | // and it is a mess /. |
654 | // and it is a mess /. |
522 | // yet we are trying to be perfect /. |
655 | // yet we are trying to be perfect /. |
523 | // but the result still isn't perfect /. |
656 | // but the result still isn't perfect /. |
524 | |
657 | |
525 | bool slow = prop |
658 | bool slow = this->slow |
526 | || width != r->TermWin.fwidth |
659 | || width != r->TermWin.fwidth |
527 | || height != r->TermWin.fheight; |
660 | || height != r->TermWin.fheight; |
528 | |
661 | |
529 | int base = r->TermWin.fbase; |
662 | int base = r->TermWin.fbase; |
530 | |
663 | |
… | |
… | |
605 | //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables |
738 | //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables |
606 | #define SWATHBITS 8 |
739 | #define SWATHBITS 8 |
607 | #endif |
740 | #endif |
608 | |
741 | |
609 | struct rxvt_font_xft : rxvt_font { |
742 | struct rxvt_font_xft : rxvt_font { |
610 | #if 0 |
|
|
611 | enum { |
|
|
612 | SWATHCOUNT = 1 << (21 - UNIBITS), |
|
|
613 | SWATHSIZE = 1 << (SWATHBITS - 5) |
|
|
614 | }; |
|
|
615 | typedef uint32_t swath[SWATHSIZE]; |
|
|
616 | |
|
|
617 | swath *cvr[SWATHCOUNT]; |
|
|
618 | #endif |
|
|
619 | |
|
|
620 | #if 0 |
|
|
621 | void gen_coverage_swath (unsigned int page); |
|
|
622 | |
|
|
623 | bool has_char (uint32_t ch) |
|
|
624 | { |
|
|
625 | unsigned int page = ch >> SWATHBITS; |
|
|
626 | unsigned int idx = ch & ((1 << SWATHBITS) - 1); |
|
|
627 | |
|
|
628 | if (page >= SWATHCOUNT) |
|
|
629 | return false; |
|
|
630 | |
|
|
631 | if (!cvr[page]) gen_coverage_swath (page); |
|
|
632 | |
|
|
633 | return cvr[page][idx >> 5] & (1 << (idx & 31)); |
|
|
634 | } |
|
|
635 | #endif |
|
|
636 | rxvt_font_xft () { f = 0; d = 0; } |
743 | rxvt_font_xft () { f = 0; d = 0; } |
637 | |
744 | |
638 | void clear (); |
745 | void clear (); |
639 | |
746 | |
640 | bool load (int maxheight); |
747 | rxvt_fontprop properties (); |
|
|
748 | |
|
|
749 | bool load (const rxvt_fontprop &prop); |
641 | |
750 | |
642 | void draw (int x, int y, |
751 | void draw (int x, int y, |
643 | const text_t *text, int len, |
752 | const text_t *text, int len, |
644 | int fg, int bg); |
753 | int fg, int bg); |
645 | |
754 | |
646 | bool has_codepoint (uint32_t unicode); |
755 | bool has_codepoint (uint32_t unicode); |
647 | |
756 | |
648 | protected: |
757 | protected: |
649 | XftFont *f; |
758 | XftFont *f; |
650 | XftDraw *d; |
759 | XftDraw *d; |
651 | |
|
|
652 | #if 0 |
|
|
653 | virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0; |
|
|
654 | void set_swath (uint32_t ch) |
|
|
655 | { |
|
|
656 | cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1)); |
|
|
657 | } |
|
|
658 | #endif |
|
|
659 | }; |
760 | }; |
660 | |
761 | |
661 | void |
762 | void |
662 | rxvt_font_xft::clear () |
763 | rxvt_font_xft::clear () |
663 | { |
764 | { |
… | |
… | |
670 | if (d) |
771 | if (d) |
671 | { |
772 | { |
672 | XftDrawDestroy (d); |
773 | XftDrawDestroy (d); |
673 | d = 0; |
774 | d = 0; |
674 | } |
775 | } |
|
|
776 | } |
675 | |
777 | |
676 | #if 0 |
778 | rxvt_fontprop |
677 | for (int i = 0; i < SWATHCOUNT; i++) |
779 | rxvt_font_xft::properties () |
678 | delete cvr[i]; |
780 | { |
679 | #endif |
781 | rxvt_fontprop p; |
|
|
782 | |
|
|
783 | FT_Face face = XftLockFace (f); |
|
|
784 | |
|
|
785 | p.width = width; p.height = height; |
|
|
786 | p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
|
|
787 | p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
|
|
788 | |
|
|
789 | XftUnlockFace (f); |
|
|
790 | |
|
|
791 | return p; |
680 | } |
792 | } |
681 | |
793 | |
682 | bool |
794 | bool |
683 | rxvt_font_xft::load (int maxheight) |
795 | rxvt_font_xft::load (const rxvt_fontprop &prop) |
684 | { |
796 | { |
685 | #if 0 |
797 | #if 0 |
686 | for (int i = 0; i < SWATHCOUNT; i++) |
798 | for (int i = 0; i < SWATHCOUNT; i++) |
687 | cvr[i] = 0; |
799 | cvr[i] = 0; |
688 | #endif |
800 | #endif |
689 | |
801 | |
690 | clear (); |
802 | clear (); |
691 | |
803 | |
692 | f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name); |
804 | FcPattern *p = FcNameParse ((FcChar8 *) name); |
|
|
805 | |
|
|
806 | if (!p) |
|
|
807 | return false; |
|
|
808 | |
|
|
809 | FcValue v; |
|
|
810 | |
|
|
811 | if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
|
|
812 | FcPatternAddInteger (p, FC_WEIGHT, prop.weight); |
|
|
813 | |
|
|
814 | if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) |
|
|
815 | FcPatternAddInteger (p, FC_SLANT, prop.slant); |
|
|
816 | |
|
|
817 | #if 0 // clipping unfortunately destroys our precious double-width-characters |
|
|
818 | // clip width, we can't do better, or can we? |
|
|
819 | if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) |
|
|
820 | FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); |
|
|
821 | #endif |
|
|
822 | |
|
|
823 | //FcPatternAddBool (p, FC_MINSPACE, 1); |
|
|
824 | |
|
|
825 | XftResult result; |
|
|
826 | FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
|
|
827 | |
|
|
828 | FcPatternDestroy (p); |
|
|
829 | |
|
|
830 | if (!match) |
|
|
831 | return false; |
|
|
832 | |
|
|
833 | f = XftFontOpenPattern (DISPLAY, match); |
693 | |
834 | |
694 | if (!f) |
835 | if (!f) |
|
|
836 | { |
|
|
837 | FcPatternDestroy (match); |
695 | return false; |
838 | return false; |
|
|
839 | } |
696 | |
840 | |
697 | FT_Face face = XftLockFace (f); |
841 | FT_Face face = XftLockFace (f); |
698 | |
842 | |
699 | prop = !FT_IS_FIXED_WIDTH (face); |
843 | slow = !FT_IS_FIXED_WIDTH (face); |
700 | |
844 | |
701 | int ftheight = 0; |
845 | int ftheight = 0; |
702 | |
846 | |
703 | for (;;) |
847 | for (;;) |
704 | { |
848 | { |
… | |
… | |
706 | FcChar8 c; |
850 | FcChar8 c; |
707 | |
851 | |
708 | c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); |
852 | c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); |
709 | c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); |
853 | c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); |
710 | |
854 | |
711 | prop = prop || g1.xOff != g2.xOff; // don't simply trust the font |
855 | if (g1.xOff != g2.xOff) // don't simply trust the font |
|
|
856 | slow = true; |
712 | |
857 | |
713 | width = g2.xOff; |
858 | width = g2.xOff; |
714 | ascent = (face->size->metrics.ascender + 63) >> 6; |
859 | ascent = (face->size->metrics.ascender + 63) >> 6; |
715 | descent = (-face->size->metrics.descender + 63) >> 6; |
860 | descent = (-face->size->metrics.descender + 63) >> 6; |
716 | height = ascent + descent; |
861 | height = ascent + descent; |
717 | |
862 | |
718 | if (height <= maxheight || !maxheight) |
863 | if (height <= prop.height || !prop.height) |
719 | break; |
864 | break; |
720 | |
865 | |
721 | if (ftheight) |
866 | if (ftheight) |
722 | { |
867 | { |
723 | // take smaller steps near the end |
868 | // take smaller steps near the end |
724 | if (height > maxheight + 1) ftheight++; |
869 | if (height > prop.height + 1) ftheight++; |
725 | if (height > maxheight + 2) ftheight++; |
870 | if (height > prop.height + 2) ftheight++; |
726 | if (height > maxheight + 3) ftheight++; |
871 | if (height > prop.height + 3) ftheight++; |
727 | |
872 | |
728 | FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); |
873 | FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); |
729 | } |
874 | } |
730 | else |
875 | else |
731 | FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); |
876 | FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); |
732 | } |
877 | } |
733 | |
878 | |
734 | XftUnlockFace (f); |
879 | XftUnlockFace (f); |
735 | |
880 | |
736 | return true; |
881 | return true; |
737 | } |
882 | } |
738 | |
|
|
739 | #if 0 |
|
|
740 | void rxvt_font::gen_coverage_swath (unsigned int page) |
|
|
741 | { |
|
|
742 | cvr[page] = new swath; |
|
|
743 | |
|
|
744 | for (int i = 0; i < SWATHSIZE; i++) |
|
|
745 | cvr[page][i] = 0; |
|
|
746 | |
|
|
747 | populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1); |
|
|
748 | } |
|
|
749 | #endif |
|
|
750 | |
883 | |
751 | bool |
884 | bool |
752 | rxvt_font_xft::has_codepoint (uint32_t unicode) |
885 | rxvt_font_xft::has_codepoint (uint32_t unicode) |
753 | { |
886 | { |
754 | return XftCharExists (DISPLAY, f, unicode); |
887 | return XftCharExists (DISPLAY, f, unicode); |
… | |
… | |
758 | rxvt_font_xft::draw (int x, int y, |
891 | rxvt_font_xft::draw (int x, int y, |
759 | const text_t *text, int len, |
892 | const text_t *text, int len, |
760 | int fg, int bg) |
893 | int fg, int bg) |
761 | { |
894 | { |
762 | if (!d) |
895 | if (!d) |
763 | { |
896 | d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap); |
764 | dR; |
|
|
765 | d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP); |
|
|
766 | } |
|
|
767 | |
897 | |
768 | if (bg >= 0 && bg != Color_bg) |
898 | if (bg >= 0 && bg != Color_bg) |
769 | XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); |
899 | XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); |
770 | else |
900 | else |
771 | clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
901 | clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
772 | |
902 | |
773 | if (!prop && width == r->TermWin.fwidth) |
903 | if (!slow && width == r->TermWin.fwidth && 0) |
774 | { |
904 | { |
775 | if (sizeof (text_t) == sizeof (FcChar16)) |
905 | if (sizeof (text_t) == sizeof (FcChar16)) |
776 | XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); |
906 | XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); |
777 | else |
907 | else |
778 | XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); |
908 | XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); |
… | |
… | |
781 | { |
911 | { |
782 | while (len) |
912 | while (len) |
783 | { |
913 | { |
784 | if (*text != NOCHAR && *text != ' ') |
914 | if (*text != NOCHAR && *text != ' ') |
785 | { |
915 | { |
|
|
916 | int fwidth = r->TermWin.fwidth; |
|
|
917 | if (len >= 2 && text[1] == NOCHAR) |
|
|
918 | fwidth *= 2; |
|
|
919 | |
|
|
920 | XGlyphInfo extents; |
786 | if (sizeof (text_t) == sizeof (FcChar16)) |
921 | if (sizeof (text_t) == sizeof (FcChar16)) |
787 | 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 + (fwidth - extents.width) / 2, |
|
|
925 | y + r->TermWin.fbase, (const FcChar16 *)text, 1); |
|
|
926 | } |
788 | else |
927 | else |
789 | 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 + (fwidth - extents.width) / 2, |
|
|
932 | y + r->TermWin.fbase, (const FcChar32 *)text, 1); |
|
|
933 | } |
790 | } |
934 | } |
791 | |
935 | |
792 | x += r->TermWin.fwidth; |
936 | x += r->TermWin.fwidth; |
793 | text++; |
937 | text++; |
794 | len--; |
938 | len--; |
… | |
… | |
798 | #endif |
942 | #endif |
799 | |
943 | |
800 | ///////////////////////////////////////////////////////////////////////////// |
944 | ///////////////////////////////////////////////////////////////////////////// |
801 | |
945 | |
802 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
946 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
803 | #ifdef EXPLICIT_CONTEXT |
|
|
804 | : r(r) |
947 | : r (r) |
805 | #endif |
|
|
806 | { |
948 | { |
807 | clear (); |
949 | clear (); |
808 | } |
950 | } |
809 | |
951 | |
810 | rxvt_fontset::~rxvt_fontset () |
952 | rxvt_fontset::~rxvt_fontset () |
… | |
… | |
813 | } |
955 | } |
814 | |
956 | |
815 | void |
957 | void |
816 | rxvt_fontset::clear () |
958 | rxvt_fontset::clear () |
817 | { |
959 | { |
818 | for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) |
960 | for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) |
819 | FONT_UNREF (*i); |
961 | FONT_UNREF (*i); |
820 | |
962 | |
821 | fonts.clear (); |
963 | fonts.clear (); |
822 | base_id = 0; |
964 | base_id = 0; |
823 | height = 0x7fffffff; |
965 | base_prop.height = 0x7fffffff; |
|
|
966 | base_prop.weight = rxvt_fontprop::medium; |
|
|
967 | base_prop.slant = rxvt_fontprop::roman; |
824 | |
968 | |
825 | fallback = fallback_fonts; |
969 | fallback = fallback_fonts; |
826 | } |
970 | } |
827 | |
971 | |
828 | rxvt_font * |
972 | rxvt_font * |
… | |
… | |
875 | |
1019 | |
876 | if (*desc == '[') |
1020 | if (*desc == '[') |
877 | { |
1021 | { |
878 | fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); |
1022 | fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); |
879 | |
1023 | |
880 | const char *extra = desc++; |
1024 | //const char *extra = desc++; // not yet used |
881 | |
1025 | |
882 | desc = strchr (desc, ']'); |
1026 | desc = strchr (desc, ']'); |
883 | |
1027 | |
884 | if (!desc) |
1028 | if (!desc) |
885 | { |
1029 | { |
… | |
… | |
915 | if (fonts[i]->loaded) |
1059 | if (fonts[i]->loaded) |
916 | return true; |
1060 | return true; |
917 | |
1061 | |
918 | fonts[i]->loaded = true; |
1062 | fonts[i]->loaded = true; |
919 | |
1063 | |
920 | if (!fonts[i]->load (height)) |
1064 | if (!fonts[i]->load (base_prop)) |
921 | { |
1065 | { |
922 | fonts[i]->cs = CS_UNKNOWN; |
1066 | fonts[i]->cs = CS_UNKNOWN; |
923 | return false; |
1067 | return false; |
924 | } |
1068 | } |
925 | |
1069 | |
… | |
… | |
938 | |
1082 | |
939 | if (!base_id) |
1083 | if (!base_id) |
940 | base_id = 1; |
1084 | base_id = 1; |
941 | |
1085 | |
942 | // we currently need a base-font, no matter what |
1086 | // we currently need a base-font, no matter what |
943 | if (fonts.size () <= base_id) |
1087 | if ((int)fonts.size () <= base_id || !realize_font (base_id)) |
944 | { |
1088 | { |
|
|
1089 | puts ("unable to load specified font (s), falling back to 'fixed'\n"); |
945 | add_fonts ("fixed"); |
1090 | add_fonts ("fixed"); |
946 | base_id = 1; |
1091 | base_id = fonts.size () - 1; |
947 | } |
1092 | } |
948 | |
1093 | |
949 | if (fonts.size () <= base_id || !realize_font (base_id)) |
1094 | if ((int)fonts.size () <= base_id || !realize_font (base_id)) |
950 | { |
1095 | { |
951 | fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); |
1096 | fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); |
952 | exit (1); |
1097 | exit (1); |
953 | } |
1098 | } |
954 | |
1099 | |
955 | height = fonts[base_id]->height; |
1100 | base_prop = fonts[base_id]->properties (); |
956 | |
|
|
957 | /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/ |
|
|
958 | } |
1101 | } |
959 | |
1102 | |
960 | int |
1103 | int |
961 | rxvt_fontset::find_font (uint32_t unicode) |
1104 | rxvt_fontset::find_font (uint32_t unicode) |
962 | { |
1105 | { |
963 | for (int i = 0; i < fonts.size (); i++) |
1106 | for (unsigned int i = 0; i < fonts.size (); i++) |
964 | { |
1107 | { |
965 | rxvt_font *f = fonts[i]; |
1108 | rxvt_font *f = fonts[i]; |
966 | |
1109 | |
967 | if (!f->loaded) |
1110 | if (!f->loaded) |
968 | { |
1111 | { |