ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
(Generate patch)

Comparing rxvt-unicode/src/defaultfont.C (file contents):
Revision 1.4 by pcg, Sat Nov 29 18:42:07 2003 UTC vs.
Revision 1.56 by root, Mon Aug 2 04:42:11 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines