ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.44
Committed: Tue Mar 16 04:52:20 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.43: +69 -95 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*;
2 * File: defaultfont.C
3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
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->display->display
29 #define TGC r->TermWin.gc
30
31 const struct rxvt_fallback_font {
32 codeset cs;
33 const char *name;
34 } fallback_fonts[] = {
35 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
37 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
38
39 #if ENCODING_EU
40 // cyrillic
41 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
42 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
43
44 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
45 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
46 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
47 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
48 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
49 { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
50 { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
51 { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
57 #endif
58
59 // japanese
60 #if ENCODING_JP || ENCODING_JP_EXT
61 # if XFT
62 // prefer xft for complex scripts
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
64 # endif
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 #endif
69
70 #if ENCODING_CN || ENCODING_CN_EXT
71 # if XFT
72 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
73 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
74 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
75 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
76 # endif
77 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
78 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
79 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
80 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
81 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
82 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
83 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
84 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
85 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
86 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
87 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
88 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
89 #endif
90
91 #if XFT
92 { CS_UNICODE, "xft:Andale Mono" },
93 { CS_UNICODE, "xft:Arial Unicode MS" },
94 #endif
95 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
96 { CS_UNICODE, "xft:FreeMono" },
97 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
98 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
99 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
100
101 #if UNICODE_3 && XFT
102 { CS_UNICODE, "xft:Code2001" }, // contains many plane-1 characters
103 #endif
104
105 { CS_UNKNOWN, 0 }
106 };
107
108 /////////////////////////////////////////////////////////////////////////////
109
110 #if XFT
111 rxvt_drawable::~rxvt_drawable ()
112 {
113 if (xftdrawable)
114 XftDrawDestroy (xftdrawable);
115 }
116
117 rxvt_drawable::operator XftDraw *()
118 {
119 if (!xftdrawable)
120 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
121
122 return xftdrawable;
123 }
124 #endif
125
126 /////////////////////////////////////////////////////////////////////////////
127
128 static void *enc_buf;
129 static uint32_t enc_len;
130
131 static inline void *
132 get_enc_buf (uint32_t len)
133 {
134 if (len > enc_len)
135 {
136 free (enc_buf);
137 enc_buf = malloc (len);
138 }
139
140 return enc_buf;
141 }
142
143 static const char *
144 enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
145 {
146 uint8_t *buf = (uint8_t *)get_enc_buf (len);
147
148 while (len--)
149 {
150 uint32_t c = FROM_UNICODE (cs, *text++);
151
152 if (c == NOCHAR)
153 {
154 c = 0;
155 zero = true;
156 }
157
158 *buf++ = c;
159 }
160
161 return (const char *)enc_buf;
162 }
163
164 static const XChar2b *
165 enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
166 {
167 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
168
169 while (len--)
170 {
171 uint32_t c = FROM_UNICODE (cs, *text++);
172
173 if (c == NOCHAR)
174 {
175 c = 0;
176 zero = true;
177 }
178
179 buf->byte1 = c >> 8;
180 buf->byte2 = c;
181 buf++;
182 }
183
184 return (XChar2b *)enc_buf;
185 }
186
187 /////////////////////////////////////////////////////////////////////////////
188
189 void
190 rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
191 {
192 if (color == Color_bg)
193 XClearArea (d.display->display, d, x, y, w, h, FALSE);
194 else if (color >= 0)
195 {
196 #if XFT
197 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
198 #else
199 XSetForeground (d.display->display, TGC, r->PixColors[color]);
200 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
201 #endif
202 }
203 }
204
205 struct rxvt_font_default : rxvt_font {
206 rxvt_fontprop properties ()
207 {
208 rxvt_fontprop p;
209
210 p.width = p.height = 1;
211 p.weight = rxvt_fontprop::medium;
212 p.slant = rxvt_fontprop::roman;
213
214 return p;
215 }
216
217 bool load (const rxvt_fontprop &prop)
218 {
219 width = 1; height = 1;
220 ascent = 1; descent = 0;
221
222 set_name ("built-in pseudofont");
223
224 return true;
225 }
226
227 bool has_codepoint (unicode_t unicode)
228 {
229 if (unicode <= 0x001f)
230 return true;
231
232 if (unicode >= 0x0080 && unicode <= 0x009f)
233 return true;
234
235 if (unicode >= 0x2500 && unicode <= 0x259f)
236 return true;
237
238 if (IS_COMPOSE (unicode))
239 return true;
240
241 switch (unicode)
242 {
243 case ZERO_WIDTH_CHAR:
244 return true;
245 }
246
247 return false;
248 }
249
250 void draw (rxvt_drawable &d, int x, int y,
251 const text_t *text, int len,
252 int fg, int bg);
253 };
254
255 static void rect_stipple (Display *display, Drawable d, GC gc, int s1, int s2, int x, int y, int w, int h)
256 {
257 XGCValues gcv;
258 char bm[2] = { s1, s2 };
259
260 gcv.fill_style = FillStippled;
261 gcv.stipple = XCreateBitmapFromData (display, d, bm, 2, 2);
262 gcv.ts_x_origin = x;
263 gcv.ts_y_origin = y;
264
265 if (!gcv.stipple)
266 return;
267
268 XChangeGC (display, gc, GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
269 XFillRectangle (display, d, gc, x, y, w, h);
270
271 XFreePixmap (display, gcv.stipple);
272
273 gcv.fill_style = FillSolid;
274 XChangeGC (display, gc, GCFillStyle, &gcv);
275 }
276
277 #include "table/linedraw.h"
278
279 void
280 rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
281 const text_t *text, int len,
282 int fg, int bg)
283 {
284 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
285
286 XSetForeground (d.display->display, TGC, r->PixColors[fg]);
287
288 while (len--)
289 {
290 #if ENABLE_COMBINING
291 compose_char *cc;
292 #endif
293 text_t t = *text++;
294
295 int x_[16];
296 int y_[16];
297
298 int W = r->TermWin.fwidth , w = (W - 1) / 2;
299 int H = r->TermWin.fheight, h = (H - 1) / 2;
300 int x0 = x, x1 = x + w, x2 = x + r->TermWin.fwidth ;
301 int y0 = y, y1 = y + h, y2 = y + r->TermWin.fheight;
302
303 for (int i = 0; i <= 8; i++)
304 {
305 x_[i] = x + ((W-1) * i + (i*7/8)) / 8;
306 y_[i] = y + ((H-1) * i + (i*7/8)) / 8;
307 }
308
309 x_[10] = x + (W - 1) / 2; x_[9] = x_[10] - 1; x_[11] = x_[10] + 1;
310 y_[10] = y + (H - 1) / 2; y_[9] = y_[10] - 1; y_[11] = y_[10] + 1;
311
312 int i1 = linedraw_offs[t - 0x2500];
313 int i2 = linedraw_offs[t - 0x2500 + 1];
314
315 XGCValues gcv;
316
317 gcv.cap_style = CapButt;
318 gcv.line_width = 0;
319 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv);
320
321 if (i1 != i2)
322 {
323 while (i1 < i2)
324 {
325 uint32_t command = linedraw_command [i1++];
326
327 int op = (command >> 24) & 255;
328 int a = (command >> 20) & 15;
329 int b = (command >> 16) & 15;
330 int x1 = x_[(command >> 12) & 15];
331 int y1 = y_[(command >> 8) & 15];
332 int x2 = x_[(command >> 4) & 15];
333 int y2 = y_[(command >> 0) & 15];
334
335 switch (op)
336 {
337 case 0: // line
338 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2);
339 break;
340
341 case 1: // rectangle, possibly stippled
342 if (a)
343 {
344 static char bm[] = { 0,0 , 1,3 , 2,1 , 0,1 };
345
346 gcv.fill_style = FillStippled;
347 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2);
348 gcv.ts_x_origin = x;
349 gcv.ts_y_origin = y;
350
351 XChangeGC (d.display->display, TGC,
352 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
353 &gcv);
354 }
355
356
357 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
358
359 if (a)
360 {
361 XFreePixmap (d.display->display, gcv.stipple);
362 gcv.fill_style = FillSolid;
363 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv);
364 }
365
366 break;
367 case 2: // arc
368 break;
369 }
370 }
371
372 #if 0
373 case 'A': XDrawArc (d.display->display, d, TGC, x1 , y1 , W-1, H-1, 90*64, 90*64); break;
374 case 'B': XDrawArc (d.display->display, d, TGC, x1-W+1, y1 , W-1, H-1, 0*64, 90*64); break;
375 case 'C': XDrawArc (d.display->display, d, TGC, x1-W+1, y1-H+1, W-1, H-1, 0*64, -90*64); break;
376 case 'D': XDrawArc (d.display->display, d, TGC, x1 , y1-H+1, W-1, H-1, -90*64, -90*64); break;
377
378 case 'i': XFillRectangle (d.display->display, d, TGC, x0, y0, x1 - x0 + 1, y1 - y0 + 1); break;
379 case 'j': XFillRectangle (d.display->display, d, TGC, x1, y0, x2 - x1, y1 - y0 + 1); break;
380 case 'k': XFillRectangle (d.display->display, d, TGC, x0, y1, x1 - x0 + 1, y2 - y1); break;
381 case 'l': XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1, y2 - y1); break;
382 }
383 }
384 #endif
385
386 }
387 #if ENABLE_COMBINING
388 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
389 {
390 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
391 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg);
392 if (cc->c2 != NOCHAR)
393 {
394 // prefer font of first character, for no good reasons
395 rxvt_font *f2 = f1->has_codepoint (cc->c2)
396 ? f1
397 : (*fs)[fs->find_font (cc->c2)];
398
399 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1);
400 }
401 }
402 #endif
403 else
404 switch (t)
405 {
406 case ZERO_WIDTH_CHAR:
407 break;
408
409 #if 0
410 case 0x2580: XFillRectangle (d.display->display, d, TGC, x0, y0, W, y1 - y0 + 1); break;
411 case 0x2581: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 7 - 1) / 8, W, H - (H * 7 - 1) / 8); break;
412 case 0x2582: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 6 - 2) / 8, W, H - (H * 6 - 2) / 8); break;
413 case 0x2583: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 5 - 3) / 8, W, H - (H * 5 - 3) / 8); break;
414 case 0x2584: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 4 - 4) / 8, W, H - (H * 4 - 4) / 8); break;
415 case 0x2585: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 3 - 5) / 8, W, H - (H * 3 - 5) / 8); break;
416 case 0x2586: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 2 - 6) / 8, W, H - (H * 2 - 6) / 8); break;
417 case 0x2587: XFillRectangle (d.display->display, d, TGC, x0, y0 + (H * 1 - 7) / 8, W, H - (H * 1 - 7) / 8); break;
418 case 0x2588: XFillRectangle (d.display->display, d, TGC, x0, y0, W, H); break;
419 case 0x2589: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 7 - 1) / 8, H); break;
420 case 0x258a: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 6 - 2) / 8, H); break;
421 case 0x258b: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 5 - 3) / 8, H); break;
422 case 0x258c: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 4 - 4) / 8, H); break;
423 case 0x258d: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 3 - 5) / 8, H); break;
424 case 0x258e: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 2 - 6) / 8, H); break;
425 case 0x258f: XFillRectangle (d.display->display, d, TGC, x0, y0, (W * 1 - 7) / 8, H); break;
426 case 0x2590: XFillRectangle (d.display->display, d, TGC, x1, y0, x2 - x1, H); break;
427
428 case 0x2591: rect_stipple (d.display->display, d, TGC, 0x00, 0x01, x0, y0, W, H); break;
429 case 0x2592: rect_stipple (d.display->display, d, TGC, 0x02, 0x01, x0, y0, W, H); break;
430 case 0x2593: rect_stipple (d.display->display, d, TGC, 0x01, 0x03, x0, y0, W, H); break;
431
432 case 0x2594: XFillRectangle (d.display->display, d, TGC, x0, y0, W, (H * 1 - 7) / 8); break;
433 case 0x2595: XFillRectangle (d.display->display, d, TGC, x0 + (W * 7 - 1) / 8, y0, W - (W * 7 - 1) / 8, H); break;
434 #endif
435
436 default:
437 int w = 0;
438 while (len > 0 && *text == NOCHAR)
439 {
440 ++text;
441 --len;
442 w += r->TermWin.fwidth;
443 }
444
445 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2,
446 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4);
447 x += w;
448 }
449
450 x += r->TermWin.fwidth;
451 }
452 }
453
454 /////////////////////////////////////////////////////////////////////////////
455
456 struct rxvt_font_x11 : rxvt_font {
457 rxvt_font_x11 () { f = 0; }
458
459 void clear ();
460
461 rxvt_fontprop properties ();
462
463 bool load (const rxvt_fontprop &prop);
464
465 bool has_codepoint (unicode_t unicode);
466
467 void draw (rxvt_drawable &d, int x, int y,
468 const text_t *text, int len,
469 int fg, int bg);
470
471 XFontStruct *f;
472 codeset cs;
473 bool enc2b, encm;
474
475 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
476 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
477 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
478 bool set_properties (rxvt_fontprop &p, const char *name);
479 };
480
481 const char *
482 rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
483 {
484 unsigned long value;
485
486 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
487 return XGetAtomName (DISPLAY, value);
488 else
489 return repl;
490 }
491
492 rxvt_fontprop
493 rxvt_font_x11::properties ()
494 {
495 rxvt_fontprop p;
496 set_properties (p, f);
497 return p;
498 }
499
500 bool
501 rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
502 {
503 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
504 p.height = height;
505 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
506 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
507
508 return true;
509 }
510
511 bool
512 rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
513 {
514 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
515 const char *slant = get_property (f, "SLANT", "r");
516
517 unsigned long height;
518 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
519 return false;
520
521 unsigned long avgwidth;
522 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
523 avgwidth = 0;
524
525 return set_properties (p, height, weight, slant, avgwidth);
526 }
527
528 bool
529 rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
530 {
531 int slashes = 0;
532 const char *comp[13];
533
534 for (const char *c = name; *c; c++)
535 if (*c == '-')
536 {
537 comp[slashes++] = c + 1;
538 if (slashes >= 13)
539 break;
540 }
541
542 /* can we short-circuit the costly XLoadQueryFont? */
543 if (slashes >= 13
544 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
545 && (*comp[11] >= '0' && *comp[11] <= '9'))
546 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
547
548 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
549
550 if (f)
551 {
552 // the font should really exist now. if not, we have a problem
553 // (e.g. if the user did xset fp rehash just when we were searching fonts).
554 // in that case, just return garbage.
555 bool ret = set_properties (p, f);
556 XFreeFont (DISPLAY, f);
557 return ret;
558 }
559 else
560 return false;
561 }
562
563 // fix the size of scalable fonts
564 static void
565 fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
566 {
567 int slashes = 0;
568 const char *size;
569
570 for (const char *c = name; *c; c++)
571 if (*c == '-')
572 {
573 if (slashes == 6)
574 size = c + 1;
575
576 if (++slashes >= 13)
577 break;
578 }
579
580 if (slashes >= 13 && size[0] == '0')
581 {
582 strncpy (buf, name, size - name);
583 buf += size - name;
584 buf += sprintf (buf, "%d", prop.height);
585 strcpy (buf, size + 1);
586 }
587 else
588 strcpy (buf, name);
589 }
590
591 bool
592 rxvt_font_x11::load (const rxvt_fontprop &prop)
593 {
594 clear ();
595
596 char **list;
597 int count;
598 list = XListFonts (DISPLAY, name, 512, &count);
599 set_name (0);
600
601 if (!list)
602 return false;
603
604 int bestdiff = 0x7fffffff;
605 for (int i = 0; i < count; i++)
606 {
607 rxvt_fontprop p;
608 char fname[1024];
609 fix_scalable (fname, list[i], prop);
610
611 if (!set_properties (p, fname))
612 continue;
613
614 if (p.height > prop.height) // weed out too large fonts
615 continue;
616
617 int diff = (prop.height - p.height) * 32
618 + abs (prop.weight - p.weight)
619 + abs (prop.slant - p.slant );
620
621 if (!name // compare against best found so far
622 || diff < bestdiff)
623 {
624 set_name (strdup (fname));
625 bestdiff = diff;
626 }
627 }
628
629 XFreeFontNames (list);
630
631 if (!name)
632 return false;
633
634 f = XLoadQueryFont (DISPLAY, name);
635
636 if (!f)
637 return false;
638
639 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
640 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
641
642 if (registry && encoding)
643 {
644 char charset[64];
645 snprintf (charset, 64, "%s-%s", registry, encoding);
646
647 cs = codeset_from_name (charset);
648 }
649 else
650 {
651 const char *charset = get_property (f, "FONT", 0);
652
653 if (!charset)
654 charset = name;
655
656 int count = 13;
657 while (*charset)
658 if (*charset++ == '-' && !--count)
659 break;
660
661 cs = codeset_from_name (charset);
662 }
663
664 if (cs == CS_UNICODE)
665 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
666
667 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
668 enc2b = encm || f->max_char_or_byte2 > 255;
669
670 ascent = f->ascent;
671 descent = f->descent;
672 height = ascent + descent;
673
674 slow = false;
675
676 if (f->min_bounds.width == f->max_bounds.width)
677 width = f->min_bounds.width;
678 else if (f->per_char == NULL)
679 width = f->max_bounds.width;
680 else
681 {
682 slow = true;
683
684 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
685
686 if (encm)
687 N += (f->max_byte1 - f->min_byte1)
688 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
689
690 while (N)
691 {
692 if (f->per_char[N].width > width)
693 width = f->per_char[N].width;
694
695 --N;
696 }
697 }
698
699 if (cs == CS_UNKNOWN)
700 {
701 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
702
703 clear ();
704
705 return false;
706 }
707
708 return true;
709 }
710
711 void
712 rxvt_font_x11::clear ()
713 {
714 if (f)
715 {
716 XFreeFont (DISPLAY, f);
717 f = 0;
718 }
719 }
720
721 bool
722 rxvt_font_x11::has_codepoint (unicode_t unicode)
723 {
724 uint32_t ch = FROM_UNICODE (cs, unicode);
725
726 if (ch == NOCHAR)
727 return false;
728
729 /* check wether the character exists in _this_ font. horrible. */
730 XCharStruct *xcs;
731
732 if (encm)
733 {
734 unsigned char byte1 = ch >> 8;
735 unsigned char byte2 = ch & 255;
736
737 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
738 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
739 return false;
740
741 if (!f->per_char)
742 return true;
743
744 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
745 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
746
747 xcs = f->per_char + N;
748 }
749 else
750 {
751 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
752 return false;
753
754 if (!f->per_char)
755 return true;
756
757 xcs = f->per_char + (ch - f->min_char_or_byte2);
758 }
759
760 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
761 && xcs->ascent == 0 && xcs->descent == 0)
762 return false;
763
764 return true;
765 }
766
767 void
768 rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
769 const text_t *text, int len,
770 int fg, int bg)
771 {
772 // this looks like a mess /.
773 // and it is a mess /.
774 // yet we are trying to be perfect /.
775 // but the result still isn't perfect /.
776
777 bool slow = this->slow
778 || width != r->TermWin.fwidth
779 || height != r->TermWin.fheight;
780
781 int base = r->TermWin.fbase;
782
783 XGCValues v;
784 v.foreground = r->PixColors[fg];
785 v.background = r->PixColors[bg];
786 v.font = f->fid;
787
788 if (enc2b)
789 {
790 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
791
792 if (bg == Color_bg && !slow)
793 {
794 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
795 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
796 }
797 else
798 {
799 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
800
801 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
802
803 if (slow)
804 {
805 do
806 {
807 if (xc->byte1 || xc->byte2)
808 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
809
810 x += r->TermWin.fwidth;
811 xc++; len--;
812 }
813 while (len);
814 }
815 else
816 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len);
817 }
818 }
819 else
820 {
821 const char *xc = enc_char (text, len, cs, slow);
822
823 if (bg == Color_bg && !slow)
824 {
825 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
826 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
827 }
828 else
829 {
830 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
831
832 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
833
834 if (slow)
835 {
836 do
837 {
838 if (*xc)
839 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
840
841 x += r->TermWin.fwidth;
842 xc++; len--;
843 }
844 while (len);
845 }
846 else
847 XDrawString (d.display->display, d, TGC, x, y + base, xc, len);
848 }
849 }
850 }
851
852 /////////////////////////////////////////////////////////////////////////////
853
854 #if XFT
855 #if 0
856 #define UNIBITS 21
857 //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
858 #define SWATHBITS 8
859 #endif
860
861 struct rxvt_font_xft : rxvt_font {
862 rxvt_font_xft () { f = 0; }
863
864 void clear ();
865
866 rxvt_fontprop properties ();
867
868 bool load (const rxvt_fontprop &prop);
869
870 void draw (rxvt_drawable &d, int x, int y,
871 const text_t *text, int len,
872 int fg, int bg);
873
874 bool has_codepoint (unicode_t unicode);
875
876 protected:
877 XftFont *f;
878 };
879
880 void
881 rxvt_font_xft::clear ()
882 {
883 if (f)
884 {
885 XftFontClose (DISPLAY, f);
886 f = 0;
887 }
888 }
889
890 rxvt_fontprop
891 rxvt_font_xft::properties ()
892 {
893 rxvt_fontprop p;
894
895 FT_Face face = XftLockFace (f);
896
897 p.width = width; p.height = height;
898 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
899 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
900
901 XftUnlockFace (f);
902
903 return p;
904 }
905
906 bool
907 rxvt_font_xft::load (const rxvt_fontprop &prop)
908 {
909 #if 0
910 for (int i = 0; i < SWATHCOUNT; i++)
911 cvr[i] = 0;
912 #endif
913
914 clear ();
915
916 FcPattern *p = FcNameParse ((FcChar8 *) name);
917
918 if (!p)
919 return false;
920
921 FcValue v;
922
923 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
924 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
925
926 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
927 FcPatternAddInteger (p, FC_SLANT, prop.slant);
928
929 #if 0 // clipping unfortunately destroys our precious double-width-characters
930 // clip width, we can't do better, or can we?
931 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
932 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
933 #endif
934
935 //FcPatternAddBool (p, FC_MINSPACE, 1);
936
937 XftResult result;
938 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
939
940 FcPatternDestroy (p);
941
942 if (!match)
943 return false;
944
945 f = XftFontOpenPattern (DISPLAY, match);
946
947 if (!f)
948 {
949 FcPatternDestroy (match);
950 return false;
951 }
952
953 FT_Face face = XftLockFace (f);
954
955 slow = !FT_IS_FIXED_WIDTH (face);
956
957 int ftheight = 0;
958
959 for (;;)
960 {
961 XGlyphInfo g1, g2;
962 FcChar8 c;
963
964 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
965 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
966
967 if (g1.xOff != g2.xOff) // don't simply trust the font
968 slow = true;
969
970 width = g2.xOff;
971 ascent = (face->size->metrics.ascender + 63) >> 6;
972 descent = (-face->size->metrics.descender + 63) >> 6;
973 height = ascent + descent;
974
975 if (height <= prop.height || !prop.height)
976 break;
977
978 if (ftheight)
979 {
980 // take smaller steps near the end
981 if (height > prop.height + 1) ftheight++;
982 if (height > prop.height + 2) ftheight++;
983 if (height > prop.height + 3) ftheight++;
984
985 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
986 }
987 else
988 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
989 }
990
991 XftUnlockFace (f);
992
993 return true;
994 }
995
996 bool
997 rxvt_font_xft::has_codepoint (unicode_t unicode)
998 {
999 return XftCharExists (DISPLAY, f, unicode);
1000 }
1001
1002 void
1003 rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1004 const text_t *text, int len,
1005 int fg, int bg)
1006 {
1007 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
1008
1009 if (!slow && width == r->TermWin.fwidth && 0)
1010 {
1011 if (sizeof (text_t) == sizeof (FcChar16))
1012 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
1013 else
1014 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
1015 }
1016 else
1017 {
1018 while (len)
1019 {
1020 if (*text != NOCHAR && *text != ' ')
1021 {
1022 int fwidth = r->TermWin.fwidth;
1023 if (len >= 2 && text[1] == NOCHAR)
1024 fwidth *= 2;
1025
1026 XGlyphInfo extents;
1027 if (sizeof (text_t) == sizeof (FcChar16))
1028 {
1029 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
1030 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
1031 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
1032 }
1033 else
1034 {
1035 XGlyphInfo extents;
1036 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
1037 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
1038 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
1039 }
1040 }
1041
1042 x += r->TermWin.fwidth;
1043 text++;
1044 len--;
1045 }
1046 }
1047 }
1048 #endif
1049
1050 /////////////////////////////////////////////////////////////////////////////
1051
1052 rxvt_fontset::rxvt_fontset (rxvt_t r)
1053 : r (r)
1054 {
1055 clear ();
1056 }
1057
1058 rxvt_fontset::~rxvt_fontset ()
1059 {
1060 clear ();
1061 }
1062
1063 void
1064 rxvt_fontset::clear ()
1065 {
1066 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1067 FONT_UNREF (*i);
1068
1069 fonts.clear ();
1070 base_id = 0;
1071 base_prop.height = 0x7fffffff;
1072 base_prop.weight = rxvt_fontprop::medium;
1073 base_prop.slant = rxvt_fontprop::roman;
1074
1075 fallback = fallback_fonts;
1076 }
1077
1078 rxvt_font *
1079 rxvt_fontset::new_font (const char *name, codeset cs)
1080 {
1081 rxvt_font *f;
1082
1083 if (!name || !*name)
1084 {
1085 name = "";
1086 f = new rxvt_font_default;
1087 }
1088 #if XFT
1089 else if (!strncmp (name, "xft:", 4))
1090 {
1091 name += 4;
1092 f = new rxvt_font_xft;
1093 }
1094 #endif
1095 else if (!strncmp (name, "x:", 2))
1096 {
1097 name += 2;
1098 f = new rxvt_font_x11;
1099 }
1100 else
1101 f = new rxvt_font_x11;
1102
1103 f->fs = this;
1104 f->set_term (r);
1105 f->set_name (strdup (name));
1106
1107 f->cs = cs;
1108 f->loaded = false;
1109
1110 return f;
1111 }
1112
1113 /////////////////////////////////////////////////////////////////////////////
1114
1115 void
1116 rxvt_fontset::add_fonts (const char *desc)
1117 {
1118 if (desc)
1119 {
1120 char buf[512];
1121 const char *end;
1122
1123 do
1124 {
1125 while (*desc <= ' ') desc++;
1126
1127 if (*desc == '[')
1128 {
1129 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
1130
1131 //const char *extra = desc++; // not yet used
1132
1133 desc = strchr (desc, ']');
1134
1135 if (!desc)
1136 {
1137 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
1138 break;
1139 }
1140
1141 desc++;
1142 while (*desc <= ' ') desc++;
1143 }
1144
1145 end = strchr (desc, ',');
1146 if (!end)
1147 end = desc + strlen (desc);
1148
1149 if (end - desc < 511)
1150 {
1151 strncpy (buf, desc, end - desc);
1152 buf[end - desc] = 0;
1153
1154 fonts.push_back (new_font (buf, CS_UNICODE));
1155 }
1156
1157 desc = end + 1;
1158 }
1159 while (*end);
1160 }
1161 }
1162
1163 bool
1164 rxvt_fontset::realize_font (int i)
1165 {
1166 if (fonts[i]->loaded)
1167 return true;
1168
1169 fonts[i]->loaded = true;
1170
1171 if (!fonts[i]->load (base_prop))
1172 {
1173 fonts[i]->cs = CS_UNKNOWN;
1174 return false;
1175 }
1176
1177 return true;
1178 }
1179
1180 bool
1181 rxvt_fontset::populate (const char *desc)
1182 {
1183 clear ();
1184
1185 fonts.push_back (new_font (0, CS_UNICODE));
1186 realize_font (0);
1187
1188 add_fonts (desc);
1189
1190 if (!base_id)
1191 base_id = 1;
1192
1193 // we currently need a base-font, no matter what
1194 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1195 {
1196 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1197 add_fonts ("fixed");
1198 base_id = fonts.size () - 1;
1199 }
1200
1201 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1202 return false;
1203
1204 base_prop = fonts[base_id]->properties ();
1205
1206 return true;
1207 }
1208
1209 int
1210 rxvt_fontset::find_font (unicode_t unicode)
1211 {
1212 for (unsigned int i = 0; i < fonts.size (); i++)
1213 {
1214 rxvt_font *f = fonts[i];
1215
1216 if (!f->loaded)
1217 {
1218 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1219 goto next_font;
1220
1221 if (!realize_font (i))
1222 goto next_font;
1223 }
1224
1225 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
1226 return i;
1227
1228 next_font:
1229 if (i == fonts.size () - 1 && fallback->name)
1230 {
1231 fonts.push_back (new_font (fallback->name, fallback->cs));
1232 fallback++;
1233 i = 0;
1234 }
1235 }
1236
1237 return 0; /* we must return SOME font */
1238 }
1239
1240
1241