1 |
/*--------------------------------*-C-*---------------------------------*; |
2 |
* File: defaultfont.C |
3 |
*----------------------------------------------------------------------* |
4 |
* Copyright (c) 2003 Marc Lehmann rxvt@plan9.de> |
5 |
* - original version. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
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 |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*---------------------------------------------------------------------*/ |
21 |
|
22 |
#include "../config.h" |
23 |
#include "rxvt.h" |
24 |
#include "defaultfont.h" |
25 |
|
26 |
#include <cstdlib> |
27 |
|
28 |
#define DISPLAY r->Xdisplay |
29 |
#define DRAWABLE r->TermWin.vt |
30 |
#define GC r->TermWin.gc |
31 |
|
32 |
const struct rxvt_fallback_font { |
33 |
codeset cs; |
34 |
const char *name; |
35 |
} fallback_fonts[] = { |
36 |
{ CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" }, |
37 |
{ CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" }, |
38 |
{ CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" }, |
39 |
|
40 |
#if ENCODING_EU |
41 |
// cyrillic |
42 |
{ CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" }, |
43 |
{ CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" }, |
44 |
|
45 |
{ CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" }, |
46 |
{ CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" }, |
47 |
{ CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" }, |
48 |
{ CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" }, |
49 |
{ CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" }, |
50 |
{ CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" }, |
51 |
{ CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" }, |
52 |
{ CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" }, |
53 |
{ CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, |
54 |
{ CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, |
55 |
{ CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, |
56 |
{ CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, |
57 |
{ CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, |
58 |
#endif |
59 |
|
60 |
// japanese |
61 |
#if ENCODING_JP || ENCODING_JP_EXT |
62 |
# if XFT |
63 |
// prefer xft for complex scripts |
64 |
{ CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, |
65 |
# endif |
66 |
{ CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, |
67 |
{ CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, |
68 |
{ CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, |
69 |
#endif |
70 |
|
71 |
#if ENCODING_CN || ENCODING_CN_EXT |
72 |
# if XFT |
73 |
{ CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, |
74 |
{ CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, |
75 |
{ CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, |
76 |
{ CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, |
77 |
# endif |
78 |
{ CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-1" }, |
79 |
{ CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-2" }, |
80 |
{ CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-3" }, |
81 |
{ CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-4" }, |
82 |
{ CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-5" }, |
83 |
{ CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-6" }, |
84 |
{ CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-7" }, |
85 |
{ CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-f" }, |
86 |
#endif |
87 |
|
88 |
#if XFT |
89 |
{ CS_UNICODE, "xft:Andale Mono" }, |
90 |
{ CS_UNICODE, "xft:Arial Unicode MS" }, |
91 |
#endif |
92 |
{ CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, |
93 |
{ CS_UNICODE, "xft:FreeMono" }, |
94 |
{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, |
95 |
{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, |
96 |
{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, |
97 |
|
98 |
{ CS_UNKNOWN, 0 } |
99 |
}; |
100 |
|
101 |
///////////////////////////////////////////////////////////////////////////// |
102 |
|
103 |
static void *enc_buf; |
104 |
static uint32_t enc_len; |
105 |
|
106 |
static inline void * |
107 |
get_enc_buf (int len) |
108 |
{ |
109 |
if (len > enc_len) |
110 |
{ |
111 |
free (enc_buf); |
112 |
enc_buf = malloc (len); |
113 |
} |
114 |
|
115 |
return enc_buf; |
116 |
} |
117 |
|
118 |
static const char * |
119 |
enc_char (const text_t *text, int len, codeset cs, bool &zero) |
120 |
{ |
121 |
uint8_t *buf = (uint8_t *)get_enc_buf (len); |
122 |
|
123 |
while (len--) |
124 |
{ |
125 |
uint32_t c = FROM_UNICODE (cs, *text++); |
126 |
|
127 |
if (c == NOCHAR) |
128 |
{ |
129 |
c = 0; |
130 |
zero = true; |
131 |
} |
132 |
|
133 |
*buf++ = c; |
134 |
} |
135 |
|
136 |
return (const char *)enc_buf; |
137 |
} |
138 |
|
139 |
static const XChar2b * |
140 |
enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) |
141 |
{ |
142 |
XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); |
143 |
|
144 |
while (len--) |
145 |
{ |
146 |
uint32_t c = FROM_UNICODE (cs, *text++); |
147 |
|
148 |
if (c == NOCHAR) |
149 |
{ |
150 |
c = 0; |
151 |
zero = true; |
152 |
} |
153 |
|
154 |
buf->byte1 = c >> 8; |
155 |
buf->byte2 = c; |
156 |
buf++; |
157 |
} |
158 |
|
159 |
return (XChar2b *)enc_buf; |
160 |
} |
161 |
|
162 |
///////////////////////////////////////////////////////////////////////////// |
163 |
|
164 |
void |
165 |
rxvt_font::clear_rect (int x, int y, int w, int h, int color) |
166 |
{ |
167 |
if (color == Color_bg) |
168 |
XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); |
169 |
else if (color >= 0) |
170 |
{ |
171 |
XSetForeground (DISPLAY, GC, r->PixColors[color]); |
172 |
XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); |
173 |
} |
174 |
} |
175 |
|
176 |
static const char *linedraw_cmds[128] = { |
177 |
"1hH", "2hH", "1vV", "2vV", |
178 |
0, 0, 0, 0, |
179 |
0, 0, 0, 0, |
180 |
"1HV", "2H1V", "1H2V", "2HV", |
181 |
|
182 |
// 2510 |
183 |
"1hV", "2h1V", "1h2V", "2hV", |
184 |
"1Hv", "2H1v", "1H2v", "2Hv", |
185 |
"1hv", "2h1v", "1h2v", "2hv", |
186 |
"1HvV", "2H1vV", "1HV2v", "1Hv2V", |
187 |
|
188 |
// 2520 |
189 |
"1H2vV", "2Hv1V", "2HV1v", "2HvV", |
190 |
"1hvV", "2h1vV", "1hV2v", "1hv2V", |
191 |
"1h2vV", "2hv1V", "1v2hV", "2hvV", |
192 |
"1hHV", "2h1HV", "2H1hV", "2hH1V", |
193 |
|
194 |
// 2530 |
195 |
"1hH2V", "2hV1H", "1h2HV", "2hHV", |
196 |
"1hHv", "1vH2h", "1hv2H", "1v2hH", |
197 |
"1hH2v", "1H2hv", "1h2Hv", "2hHv", |
198 |
"1hHvV", "1vVH2h", "1hvV2H", "1vV2hH", |
199 |
|
200 |
// 2540 |
201 |
"1hHV2v", "1hHv2V", "1hH2vV", "1HV2hv", |
202 |
"1hV2Hv", "1Hv2hV", "1hv2HV", "1V2hHv", |
203 |
"1v2hHV", "1H2hvV", "1h2HvV", "2hHvV", |
204 |
0, 0, 0, 0, |
205 |
|
206 |
// 2550 |
207 |
0, 0, 0, 0, |
208 |
0, 0, 0, 0, |
209 |
0, 0, 0, 0, |
210 |
0, 0, 0, 0, |
211 |
|
212 |
// 2560 |
213 |
0, 0, 0, 0, |
214 |
0, 0, 0, 0, |
215 |
0, 0, 0, 0, |
216 |
0, 0, 0, 0, |
217 |
|
218 |
// 2570 |
219 |
0, "1a", "1b", "1ab", |
220 |
"1h", "1v", "1H", "1V", |
221 |
"2h", "2v", "2H", "2V", |
222 |
"1h2H", "1v2V", "1H2h", "1V2v" |
223 |
|
224 |
// to be done |
225 |
}; |
226 |
|
227 |
struct rxvt_font_default : rxvt_font { |
228 |
rxvt_fontprop properties () |
229 |
{ |
230 |
rxvt_fontprop p; |
231 |
|
232 |
p.width = p.height = 1; |
233 |
p.weight = rxvt_fontprop::medium; |
234 |
p.slant = rxvt_fontprop::roman; |
235 |
|
236 |
return p; |
237 |
} |
238 |
|
239 |
bool load (const rxvt_fontprop &prop) |
240 |
{ |
241 |
width = 1; height = 1; |
242 |
ascent = 1; descent = 0; |
243 |
|
244 |
return true; |
245 |
} |
246 |
|
247 |
bool has_codepoint (uint32_t unicode) |
248 |
{ |
249 |
if (unicode <= 0x001f) |
250 |
return true; |
251 |
if (unicode >= 0x0080 && unicode <= 0x009f) |
252 |
return true; |
253 |
|
254 |
if (unicode >= 0x2500 && unicode <= 0x257f |
255 |
&& linedraw_cmds[unicode - 0x2500]) |
256 |
return true; |
257 |
|
258 |
switch (unicode) |
259 |
{ |
260 |
case ZERO_WIDTH_CHAR: |
261 |
return true; |
262 |
} |
263 |
|
264 |
return false; |
265 |
} |
266 |
|
267 |
void draw (int x, int y, |
268 |
const text_t *text, int len, |
269 |
int fg, int bg); |
270 |
}; |
271 |
|
272 |
void |
273 |
rxvt_font_default::draw (int x, int y, |
274 |
const text_t *text, int len, |
275 |
int fg, int bg) |
276 |
{ |
277 |
clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
278 |
|
279 |
XSetForeground (DISPLAY, GC, r->PixColors[fg]); |
280 |
|
281 |
while (len--) |
282 |
{ |
283 |
text_t t = *text++; |
284 |
|
285 |
if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) |
286 |
{ |
287 |
const char *p = linedraw_cmds[t - 0x2500]; |
288 |
|
289 |
int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; |
290 |
int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight; |
291 |
|
292 |
XGCValues gcv; |
293 |
|
294 |
while (*p) |
295 |
{ |
296 |
switch (*p++) |
297 |
{ |
298 |
case '1': |
299 |
gcv.line_width = 0; |
300 |
XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); |
301 |
break; |
302 |
|
303 |
case '2': |
304 |
gcv.line_width = 2; |
305 |
XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); |
306 |
break; |
307 |
|
308 |
case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; |
309 |
case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break; |
310 |
case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break; |
311 |
case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break; |
312 |
case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break; |
313 |
case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break; |
314 |
} |
315 |
} |
316 |
|
317 |
gcv.line_width = 0; |
318 |
XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); |
319 |
} |
320 |
else |
321 |
switch (*text++) |
322 |
{ |
323 |
case NOCHAR: |
324 |
case ZERO_WIDTH_CHAR: |
325 |
break; |
326 |
default: |
327 |
XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); |
328 |
} |
329 |
|
330 |
x += r->TermWin.fwidth; |
331 |
} |
332 |
} |
333 |
|
334 |
///////////////////////////////////////////////////////////////////////////// |
335 |
|
336 |
struct rxvt_font_x11 : rxvt_font { |
337 |
rxvt_font_x11 () { f = 0; } |
338 |
|
339 |
void clear (); |
340 |
|
341 |
rxvt_fontprop properties (); |
342 |
|
343 |
bool load (const rxvt_fontprop &prop); |
344 |
|
345 |
bool has_codepoint (uint32_t unicode); |
346 |
|
347 |
void draw (int x, int y, |
348 |
const text_t *text, int len, |
349 |
int fg, int bg); |
350 |
|
351 |
XFontStruct *f; |
352 |
codeset cs; |
353 |
bool enc2b, encm; |
354 |
|
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); |
359 |
}; |
360 |
|
361 |
const char * |
362 |
rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const |
363 |
{ |
364 |
unsigned long value; |
365 |
|
366 |
if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) |
367 |
return XGetAtomName (DISPLAY, value); |
368 |
else |
369 |
return repl; |
370 |
} |
371 |
|
372 |
rxvt_fontprop |
373 |
rxvt_font_x11::properties () |
374 |
{ |
375 |
rxvt_fontprop p; |
376 |
set_properties (p, f); |
377 |
return p; |
378 |
} |
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 |
{ |
394 |
const char *weight = get_property (f, "WEIGHT_NAME", "medium"); |
395 |
const char *slant = get_property (f, "SLANT", "r"); |
396 |
|
397 |
unsigned long height; |
398 |
if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) |
399 |
return false; |
400 |
|
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); |
469 |
} |
470 |
|
471 |
bool |
472 |
rxvt_font_x11::load (const rxvt_fontprop &prop) |
473 |
{ |
474 |
clear (); |
475 |
|
476 |
char **list; |
477 |
int count; |
478 |
list = XListFonts (DISPLAY, name, 512, &count); |
479 |
set_name (0); |
480 |
|
481 |
if (!list) |
482 |
return false; |
483 |
|
484 |
int bestdiff = 0x7fffffff; |
485 |
for (int i = 0; i < count; i++) |
486 |
{ |
487 |
rxvt_fontprop p; |
488 |
char fname[1024]; |
489 |
fix_scalable (fname, list[i], prop); |
490 |
|
491 |
if (!set_properties (p, fname)) |
492 |
continue; |
493 |
|
494 |
if (p.height > prop.height) // weed out too large fonts |
495 |
continue; |
496 |
|
497 |
int diff = (prop.height - p.height) * 32 |
498 |
+ abs (prop.weight - p.weight) |
499 |
+ abs (prop.slant - p.slant ); |
500 |
|
501 |
if (!name // compare against best found so far |
502 |
|| diff < bestdiff) |
503 |
{ |
504 |
set_name (strdup (fname)); |
505 |
bestdiff = diff; |
506 |
} |
507 |
} |
508 |
|
509 |
XFreeFontNames (list); |
510 |
|
511 |
if (!name) |
512 |
return false; |
513 |
|
514 |
f = XLoadQueryFont (DISPLAY, name); |
515 |
|
516 |
if (!f) |
517 |
return false; |
518 |
|
519 |
unsigned long value; |
520 |
|
521 |
const char *registry = get_property (f, "CHARSET_REGISTRY", 0); |
522 |
const char *encoding = get_property (f, "CHARSET_ENCODING", 0); |
523 |
|
524 |
if (registry && encoding) |
525 |
{ |
526 |
char charset[64]; |
527 |
snprintf (charset, 64, "%s-%s", registry, encoding); |
528 |
|
529 |
cs = codeset_from_name (charset); |
530 |
} |
531 |
else |
532 |
{ |
533 |
const char *charset = get_property (f, "FONT", 0); |
534 |
|
535 |
if (!charset) |
536 |
charset = name; |
537 |
|
538 |
int count = 13; |
539 |
while (*charset) |
540 |
if (*charset++ == '-' && !--count) |
541 |
break; |
542 |
|
543 |
cs = codeset_from_name (charset); |
544 |
} |
545 |
|
546 |
if (cs == CS_UNICODE) |
547 |
cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font |
548 |
|
549 |
encm = f->min_byte1 != 0 || f->max_byte1 != 0; |
550 |
enc2b = encm || f->max_char_or_byte2 > 255; |
551 |
|
552 |
ascent = f->ascent; |
553 |
descent = f->descent; |
554 |
height = ascent + descent; |
555 |
|
556 |
slow = false; |
557 |
|
558 |
if (f->min_bounds.width == f->max_bounds.width) |
559 |
width = f->min_bounds.width; |
560 |
else if (f->per_char == NULL) |
561 |
width = f->max_bounds.width; |
562 |
else |
563 |
{ |
564 |
slow = true; |
565 |
|
566 |
int N = f->max_char_or_byte2 - f->min_char_or_byte2; |
567 |
|
568 |
if (encm) |
569 |
N += (f->max_byte1 - f->min_byte1) |
570 |
* (f->max_char_or_byte2 - f->min_char_or_byte2 + 1); |
571 |
|
572 |
while (N) |
573 |
{ |
574 |
if (f->per_char[N].width > width) |
575 |
width = f->per_char[N].width; |
576 |
|
577 |
--N; |
578 |
} |
579 |
} |
580 |
|
581 |
if (cs == CS_UNKNOWN) |
582 |
{ |
583 |
fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name); |
584 |
|
585 |
clear (); |
586 |
|
587 |
return false; |
588 |
} |
589 |
|
590 |
return true; |
591 |
} |
592 |
|
593 |
void |
594 |
rxvt_font_x11::clear () |
595 |
{ |
596 |
if (f) |
597 |
{ |
598 |
XFreeFont (DISPLAY, f); |
599 |
f = 0; |
600 |
} |
601 |
} |
602 |
|
603 |
bool |
604 |
rxvt_font_x11::has_codepoint (uint32_t unicode) |
605 |
{ |
606 |
uint32_t ch = FROM_UNICODE (cs, unicode); |
607 |
|
608 |
if (ch == NOCHAR) |
609 |
return false; |
610 |
|
611 |
/* check wether the character exists in _this_ font. horrible. */ |
612 |
XCharStruct *xcs; |
613 |
|
614 |
if (encm) |
615 |
{ |
616 |
int byte1 = ch >> 8; |
617 |
int byte2 = ch & 255; |
618 |
|
619 |
if (byte1 < f->min_byte1 || byte1 > f->max_byte1 |
620 |
|| byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) |
621 |
return false; |
622 |
|
623 |
if (!f->per_char) |
624 |
return true; |
625 |
|
626 |
int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1; |
627 |
int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2; |
628 |
|
629 |
xcs = f->per_char + N; |
630 |
} |
631 |
else |
632 |
{ |
633 |
if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2) |
634 |
return false; |
635 |
|
636 |
if (!f->per_char) |
637 |
return true; |
638 |
|
639 |
xcs = f->per_char + (ch - f->min_char_or_byte2); |
640 |
} |
641 |
|
642 |
if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0 |
643 |
&& xcs->ascent == 0 && xcs->descent == 0) |
644 |
return false; |
645 |
|
646 |
return true; |
647 |
} |
648 |
|
649 |
void |
650 |
rxvt_font_x11::draw (int x, int y, |
651 |
const text_t *text, int len, |
652 |
int fg, int bg) |
653 |
{ |
654 |
// this looks like a mess /. |
655 |
// and it is a mess /. |
656 |
// yet we are trying to be perfect /. |
657 |
// but the result still isn't perfect /. |
658 |
|
659 |
bool slow = this->slow |
660 |
|| width != r->TermWin.fwidth |
661 |
|| height != r->TermWin.fheight; |
662 |
|
663 |
int base = r->TermWin.fbase; |
664 |
|
665 |
XGCValues v; |
666 |
v.foreground = r->PixColors[fg]; |
667 |
v.background = r->PixColors[bg]; |
668 |
v.font = f->fid; |
669 |
|
670 |
if (enc2b) |
671 |
{ |
672 |
const XChar2b *xc = enc_xchar2b (text, len, cs, slow); |
673 |
|
674 |
if (bg == Color_bg && !slow) |
675 |
{ |
676 |
XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); |
677 |
XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); |
678 |
} |
679 |
else |
680 |
{ |
681 |
clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
682 |
|
683 |
XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); |
684 |
|
685 |
if (slow) |
686 |
{ |
687 |
do |
688 |
{ |
689 |
if (xc->byte1 || xc->byte2) |
690 |
XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); |
691 |
|
692 |
x += r->TermWin.fwidth; |
693 |
xc++; len--; |
694 |
} |
695 |
while (len); |
696 |
} |
697 |
else |
698 |
XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); |
699 |
} |
700 |
} |
701 |
else |
702 |
{ |
703 |
const char *xc = enc_char (text, len, cs, slow); |
704 |
|
705 |
if (bg == Color_bg && !slow) |
706 |
{ |
707 |
XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); |
708 |
XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); |
709 |
} |
710 |
else |
711 |
{ |
712 |
clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
713 |
|
714 |
XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); |
715 |
|
716 |
if (slow) |
717 |
{ |
718 |
do |
719 |
{ |
720 |
if (*xc) |
721 |
XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); |
722 |
|
723 |
x += r->TermWin.fwidth; |
724 |
xc++; len--; |
725 |
} |
726 |
while (len); |
727 |
} |
728 |
else |
729 |
XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); |
730 |
} |
731 |
} |
732 |
} |
733 |
|
734 |
///////////////////////////////////////////////////////////////////////////// |
735 |
|
736 |
#if XFT |
737 |
#if 0 |
738 |
#define UNIBITS 21 |
739 |
//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables |
740 |
#define SWATHBITS 8 |
741 |
#endif |
742 |
|
743 |
struct rxvt_font_xft : rxvt_font { |
744 |
rxvt_font_xft () { f = 0; d = 0; } |
745 |
|
746 |
void clear (); |
747 |
|
748 |
rxvt_fontprop properties (); |
749 |
|
750 |
bool load (const rxvt_fontprop &prop); |
751 |
|
752 |
void draw (int x, int y, |
753 |
const text_t *text, int len, |
754 |
int fg, int bg); |
755 |
|
756 |
bool has_codepoint (uint32_t unicode); |
757 |
|
758 |
protected: |
759 |
XftFont *f; |
760 |
XftDraw *d; |
761 |
}; |
762 |
|
763 |
void |
764 |
rxvt_font_xft::clear () |
765 |
{ |
766 |
if (f) |
767 |
{ |
768 |
XftFontClose (DISPLAY, f); |
769 |
f = 0; |
770 |
} |
771 |
|
772 |
if (d) |
773 |
{ |
774 |
XftDrawDestroy (d); |
775 |
d = 0; |
776 |
} |
777 |
} |
778 |
|
779 |
rxvt_fontprop |
780 |
rxvt_font_xft::properties () |
781 |
{ |
782 |
rxvt_fontprop p; |
783 |
|
784 |
FT_Face face = XftLockFace (f); |
785 |
|
786 |
p.width = width; p.height = height; |
787 |
p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
788 |
p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
789 |
|
790 |
XftUnlockFace (f); |
791 |
|
792 |
return p; |
793 |
} |
794 |
|
795 |
bool |
796 |
rxvt_font_xft::load (const rxvt_fontprop &prop) |
797 |
{ |
798 |
#if 0 |
799 |
for (int i = 0; i < SWATHCOUNT; i++) |
800 |
cvr[i] = 0; |
801 |
#endif |
802 |
|
803 |
clear (); |
804 |
|
805 |
FcPattern *p = FcNameParse ((FcChar8 *) name); |
806 |
|
807 |
if (!p) |
808 |
return false; |
809 |
|
810 |
FcValue v; |
811 |
|
812 |
if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) |
813 |
FcPatternAddInteger (p, FC_WEIGHT, prop.weight); |
814 |
|
815 |
if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) |
816 |
FcPatternAddInteger (p, FC_SLANT, prop.slant); |
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 |
|
824 |
//FcPatternAddBool (p, FC_MINSPACE, 1); |
825 |
|
826 |
XftResult result; |
827 |
FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); |
828 |
|
829 |
FcPatternDestroy (p); |
830 |
|
831 |
if (!match) |
832 |
return false; |
833 |
|
834 |
f = XftFontOpenPattern (DISPLAY, match); |
835 |
|
836 |
if (!f) |
837 |
{ |
838 |
FcPatternDestroy (match); |
839 |
return false; |
840 |
} |
841 |
|
842 |
FT_Face face = XftLockFace (f); |
843 |
|
844 |
slow = !FT_IS_FIXED_WIDTH (face); |
845 |
|
846 |
int ftheight = 0; |
847 |
|
848 |
for (;;) |
849 |
{ |
850 |
XGlyphInfo g1, g2; |
851 |
FcChar8 c; |
852 |
|
853 |
c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); |
854 |
c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); |
855 |
|
856 |
if (g1.xOff != g2.xOff) // don't simply trust the font |
857 |
slow = true; |
858 |
|
859 |
width = g2.xOff; |
860 |
ascent = (face->size->metrics.ascender + 63) >> 6; |
861 |
descent = (-face->size->metrics.descender + 63) >> 6; |
862 |
height = ascent + descent; |
863 |
|
864 |
if (height <= prop.height || !prop.height) |
865 |
break; |
866 |
|
867 |
if (ftheight) |
868 |
{ |
869 |
// take smaller steps near the end |
870 |
if (height > prop.height + 1) ftheight++; |
871 |
if (height > prop.height + 2) ftheight++; |
872 |
if (height > prop.height + 3) ftheight++; |
873 |
|
874 |
FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); |
875 |
} |
876 |
else |
877 |
FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); |
878 |
} |
879 |
|
880 |
XftUnlockFace (f); |
881 |
|
882 |
return true; |
883 |
} |
884 |
|
885 |
bool |
886 |
rxvt_font_xft::has_codepoint (uint32_t unicode) |
887 |
{ |
888 |
return XftCharExists (DISPLAY, f, unicode); |
889 |
} |
890 |
|
891 |
void |
892 |
rxvt_font_xft::draw (int x, int y, |
893 |
const text_t *text, int len, |
894 |
int fg, int bg) |
895 |
{ |
896 |
if (!d) |
897 |
{ |
898 |
dR; |
899 |
d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP); |
900 |
} |
901 |
|
902 |
if (bg >= 0 && bg != Color_bg) |
903 |
XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); |
904 |
else |
905 |
clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
906 |
|
907 |
if (!slow && width == r->TermWin.fwidth && 0) |
908 |
{ |
909 |
if (sizeof (text_t) == sizeof (FcChar16)) |
910 |
XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); |
911 |
else |
912 |
XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); |
913 |
} |
914 |
else |
915 |
{ |
916 |
while (len) |
917 |
{ |
918 |
if (*text != NOCHAR && *text != ' ') |
919 |
{ |
920 |
XGlyphInfo extents; |
921 |
if (sizeof (text_t) == sizeof (FcChar16)) |
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 |
} |
927 |
else |
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 |
} |
934 |
} |
935 |
|
936 |
x += r->TermWin.fwidth; |
937 |
text++; |
938 |
len--; |
939 |
} |
940 |
} |
941 |
} |
942 |
#endif |
943 |
|
944 |
///////////////////////////////////////////////////////////////////////////// |
945 |
|
946 |
rxvt_fontset::rxvt_fontset (rxvt_t r) |
947 |
#ifdef EXPLICIT_CONTEXT |
948 |
: r(r) |
949 |
#endif |
950 |
{ |
951 |
clear (); |
952 |
} |
953 |
|
954 |
rxvt_fontset::~rxvt_fontset () |
955 |
{ |
956 |
clear (); |
957 |
} |
958 |
|
959 |
void |
960 |
rxvt_fontset::clear () |
961 |
{ |
962 |
for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) |
963 |
FONT_UNREF (*i); |
964 |
|
965 |
fonts.clear (); |
966 |
base_id = 0; |
967 |
base_prop.height = 0x7fffffff; |
968 |
base_prop.weight = rxvt_fontprop::medium; |
969 |
base_prop.slant = rxvt_fontprop::roman; |
970 |
|
971 |
fallback = fallback_fonts; |
972 |
} |
973 |
|
974 |
rxvt_font * |
975 |
rxvt_fontset::new_font (const char *name, codeset cs) |
976 |
{ |
977 |
rxvt_font *f; |
978 |
|
979 |
if (!name || !*name) |
980 |
{ |
981 |
name = ""; |
982 |
f = new rxvt_font_default; |
983 |
} |
984 |
#if XFT |
985 |
else if (!strncmp (name, "xft:", 4)) |
986 |
{ |
987 |
name += 4; |
988 |
f = new rxvt_font_xft; |
989 |
} |
990 |
#endif |
991 |
else if (!strncmp (name, "x:", 2)) |
992 |
{ |
993 |
name += 2; |
994 |
f = new rxvt_font_x11; |
995 |
} |
996 |
else |
997 |
f = new rxvt_font_x11; |
998 |
|
999 |
f->set_term (r); |
1000 |
f->set_name (strdup (name)); |
1001 |
|
1002 |
f->cs = cs; |
1003 |
f->loaded = false; |
1004 |
|
1005 |
return f; |
1006 |
} |
1007 |
|
1008 |
///////////////////////////////////////////////////////////////////////////// |
1009 |
|
1010 |
void |
1011 |
rxvt_fontset::add_fonts (const char *desc) |
1012 |
{ |
1013 |
if (desc) |
1014 |
{ |
1015 |
char buf[512]; |
1016 |
const char *end; |
1017 |
|
1018 |
do |
1019 |
{ |
1020 |
while (*desc <= ' ') desc++; |
1021 |
|
1022 |
if (*desc == '[') |
1023 |
{ |
1024 |
fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); |
1025 |
|
1026 |
const char *extra = desc++; |
1027 |
|
1028 |
desc = strchr (desc, ']'); |
1029 |
|
1030 |
if (!desc) |
1031 |
{ |
1032 |
fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n"); |
1033 |
break; |
1034 |
} |
1035 |
|
1036 |
desc++; |
1037 |
while (*desc <= ' ') desc++; |
1038 |
} |
1039 |
|
1040 |
end = strchr (desc, ','); |
1041 |
if (!end) |
1042 |
end = desc + strlen (desc); |
1043 |
|
1044 |
if (end - desc < 511) |
1045 |
{ |
1046 |
strncpy (buf, desc, end - desc); |
1047 |
buf[end - desc] = 0; |
1048 |
|
1049 |
fonts.push_back (new_font (buf, CS_UNICODE)); |
1050 |
} |
1051 |
|
1052 |
desc = end + 1; |
1053 |
} |
1054 |
while (*end); |
1055 |
} |
1056 |
} |
1057 |
|
1058 |
bool |
1059 |
rxvt_fontset::realize_font (int i) |
1060 |
{ |
1061 |
if (fonts[i]->loaded) |
1062 |
return true; |
1063 |
|
1064 |
fonts[i]->loaded = true; |
1065 |
|
1066 |
if (!fonts[i]->load (base_prop)) |
1067 |
{ |
1068 |
fonts[i]->cs = CS_UNKNOWN; |
1069 |
return false; |
1070 |
} |
1071 |
|
1072 |
return true; |
1073 |
} |
1074 |
|
1075 |
void |
1076 |
rxvt_fontset::populate (const char *desc) |
1077 |
{ |
1078 |
clear (); |
1079 |
|
1080 |
fonts.push_back (new_font (0, CS_UNICODE)); |
1081 |
realize_font (0); |
1082 |
|
1083 |
add_fonts (desc); |
1084 |
|
1085 |
if (!base_id) |
1086 |
base_id = 1; |
1087 |
|
1088 |
// we currently need a base-font, no matter what |
1089 |
if (fonts.size () <= base_id || !realize_font (base_id)) |
1090 |
{ |
1091 |
add_fonts ("fixed"); |
1092 |
base_id = fonts.size () - 1; |
1093 |
} |
1094 |
|
1095 |
if (fonts.size () <= base_id || !realize_font (base_id)) |
1096 |
{ |
1097 |
fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); |
1098 |
exit (1); |
1099 |
} |
1100 |
|
1101 |
base_prop = fonts[base_id]->properties (); |
1102 |
} |
1103 |
|
1104 |
int |
1105 |
rxvt_fontset::find_font (uint32_t unicode) |
1106 |
{ |
1107 |
for (int i = 0; i < fonts.size (); i++) |
1108 |
{ |
1109 |
rxvt_font *f = fonts[i]; |
1110 |
|
1111 |
if (!f->loaded) |
1112 |
{ |
1113 |
if (FROM_UNICODE (f->cs, unicode) == NOCHAR) |
1114 |
goto next_font; |
1115 |
|
1116 |
if (!realize_font (i)) |
1117 |
goto next_font; |
1118 |
} |
1119 |
|
1120 |
if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode)) |
1121 |
return i; |
1122 |
|
1123 |
next_font: |
1124 |
if (i == fonts.size () - 1 && fallback->name) |
1125 |
{ |
1126 |
fonts.push_back (new_font (fallback->name, fallback->cs)); |
1127 |
fallback++; |
1128 |
i = 0; |
1129 |
} |
1130 |
} |
1131 |
|
1132 |
return 0; /* we must return SOME font */ |
1133 |
} |
1134 |
|
1135 |
|
1136 |
|