ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
Revision: 1.2
Committed: Tue Nov 25 15:25:17 2003 UTC (20 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +37 -34 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.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 pcg 1.2 #define DISPLAY r->Xdisplay
27     #define DRAWABLE r->TermWin.vt
28     #define GC r->TermWin.gc
29 pcg 1.1
30     const struct rxvt_fallback_font {
31     codeset cs;
32     const char *name;
33     } fallback_fonts[] = {
34     { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
35     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
36     { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
37    
38     #if ENCODING_EU
39     // cyrillic
40     { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
41     { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
42    
43     { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
44     { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
45     { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
46     { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
47     { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
48     { CS_ISO8859_7, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-7" },
49     { CS_ISO8859_8, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-8" },
50     { CS_ISO8859_9, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-9" },
51     { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
52     { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
53     { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
54     { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
55     { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
56     #endif
57    
58     // japanese
59     #if ENCODING_JP || ENCODING_JP_EXT
60     # if XFT
61     // prefer xft for complex scripts
62     { CS_UNICODE, "xft:Kochi Gothic" },
63     # endif
64     { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
65     { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
66     { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
67     #endif
68    
69     #if ENCODING_CN || ENCODING_CN_EXT
70     # if XFT
71     { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
72     { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
73     { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
74     { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
75     # endif
76     { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" },
77     { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" },
78     { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" },
79     { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" },
80     { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" },
81     { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" },
82     { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" },
83     { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" },
84     #endif
85    
86     #if XFT
87     { CS_UNICODE, "xft:Andale Mono" },
88     { CS_UNICODE, "xft:Arial Unicode MS" },
89     #endif
90     { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
91     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
93     { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
94    
95     { CS_UNKNOWN, 0 }
96     };
97    
98     /////////////////////////////////////////////////////////////////////////////
99    
100     static void *enc_buf;
101     static uint32_t enc_len;
102    
103     static inline void *
104     get_enc_buf (int len)
105     {
106     if (len > enc_len)
107     {
108     free (enc_buf);
109     enc_buf = malloc (len);
110     }
111    
112     return enc_buf;
113     }
114    
115     static const char *
116     enc_char (const text_t *text, int len, codeset cs, bool &zero)
117     {
118     uint8_t *buf = (uint8_t *)get_enc_buf (len);
119    
120     while (len--)
121     {
122     uint32_t c = FROM_UNICODE (cs, *text++);
123    
124     if (c == NOCHAR)
125     {
126     c = 0;
127     zero = true;
128     }
129    
130     *buf++ = c;
131     }
132    
133     return (const char *)enc_buf;
134     }
135    
136     static const XChar2b *
137     enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero)
138     {
139     XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
140    
141     while (len--)
142     {
143     uint32_t c = FROM_UNICODE (cs, *text++);
144    
145     if (c == NOCHAR)
146     {
147     c = 0;
148     zero = true;
149     }
150    
151     buf->byte1 = c >> 8;
152     buf->byte2 = c;
153     buf++;
154     }
155    
156     return (XChar2b *)enc_buf;
157     }
158    
159     /////////////////////////////////////////////////////////////////////////////
160    
161     void
162     rxvt_font::clear_rect (int x, int y, int w, int h, int color)
163     {
164     if (color == Color_bg)
165     XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE);
166     else if (color >= 0)
167     {
168 pcg 1.2 XSetForeground (DISPLAY, GC, r->PixColors[color]);
169 pcg 1.1 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h);
170     }
171     }
172    
173     struct rxvt_font_default : rxvt_font {
174     bool load (int maxheight)
175     {
176     width = 1; height = 1;
177     ascent = 1; descent = 0;
178    
179     return true;
180     }
181    
182     bool has_codepoint (uint32_t unicode)
183     {
184     if (unicode <= 0x001f
185     || (unicode >= 0x80 && unicode <= 0x9f))
186     return true;
187    
188     switch (unicode)
189     {
190     case ZERO_WIDTH_CHAR:
191     return true;
192     }
193    
194     return false;
195     }
196    
197     void draw (int x, int y,
198     const text_t *text, int len,
199     int fg, int bg);
200     };
201    
202     void
203     rxvt_font_default::draw (int x, int y,
204     const text_t *text, int len,
205     int fg, int bg)
206     {
207 pcg 1.2 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
208 pcg 1.1
209     while (len--)
210     {
211     switch (*text++)
212     {
213     case NOCHAR:
214     case ZERO_WIDTH_CHAR:
215     break;
216     default:
217 pcg 1.2 XSetForeground (DISPLAY, GC, r->PixColors[fg]);
218     XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
219 pcg 1.1 }
220    
221 pcg 1.2 x += r->TermWin.fwidth;
222 pcg 1.1 }
223     }
224    
225     /////////////////////////////////////////////////////////////////////////////
226    
227     struct rxvt_font_x11 : rxvt_font {
228     rxvt_font_x11 () { f = 0; }
229    
230     void clear ();
231    
232     bool load (int maxheight);
233    
234     bool has_codepoint (uint32_t unicode);
235    
236     void draw (int x, int y,
237     const text_t *text, int len,
238     int fg, int bg);
239    
240     XFontStruct *f;
241     codeset cs;
242     bool enc2b, encm;
243    
244     const char *get_property (const char *property, const char *repl) const;
245     };
246    
247     const char *
248     rxvt_font_x11::get_property (const char *property, const char *repl) const
249     {
250     unsigned long value;
251    
252     if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
253     return XGetAtomName (DISPLAY, value);
254     else
255     return repl;
256     }
257    
258     bool
259     rxvt_font_x11::load (int maxheight)
260     {
261     clear ();
262    
263     f = XLoadQueryFont (DISPLAY, name);
264    
265     if (!f)
266     return false;
267    
268     unsigned long value;
269    
270     const char *registry = get_property ("CHARSET_REGISTRY", 0);
271     const char *encoding = get_property ("CHARSET_ENCODING", 0);
272    
273     if (registry && encoding)
274     {
275     char charset[64];
276     snprintf (charset, 64, "%s-%s", registry, encoding);
277    
278     cs = codeset_from_name (charset);
279     }
280     else
281     {
282     const char *charset = get_property ("FONT", 0);
283    
284     if (!charset)
285     charset = name;
286    
287     int count = 13;
288     while (*charset)
289     if (*charset++ == '-' && !--count)
290     break;
291    
292     cs = codeset_from_name (charset);
293     }
294    
295     if (cs == CS_UNICODE)
296     cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
297    
298     encm = f->min_byte1 != 0 || f->max_byte1 != 0;
299     enc2b = encm || f->max_char_or_byte2 > 255;
300    
301     ascent = f->ascent;
302     descent = f->descent;
303     height = ascent + descent;
304    
305     prop = false;
306    
307     if (f->min_bounds.width == f->max_bounds.width)
308     width = f->min_bounds.width;
309     else if (f->per_char == NULL)
310     width = f->max_bounds.width;
311     else
312     {
313     prop = true;
314    
315     int N = f->max_char_or_byte2 - f->min_char_or_byte2;
316    
317     if (encm)
318     N += (f->max_byte1 - f->min_byte1)
319     * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
320    
321     while (N)
322     {
323     if (f->per_char[N].width > width)
324     width = f->per_char[N].width;
325    
326     --N;
327     }
328     }
329    
330     if (cs == CS_UNKNOWN)
331     {
332     fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
333    
334     clear ();
335    
336     return false;
337     }
338    
339     return true;
340     }
341    
342     void
343     rxvt_font_x11::clear ()
344     {
345     if (f)
346     {
347     XFreeFont (DISPLAY, f);
348     f = 0;
349     }
350     }
351    
352     bool
353     rxvt_font_x11::has_codepoint (uint32_t unicode)
354     {
355     uint32_t ch = FROM_UNICODE (cs, unicode);
356    
357     if (ch == NOCHAR)
358     return false;
359    
360     /* check wether the character exists in _this_ font. horrible. */
361     XCharStruct *xcs;
362    
363     if (encm)
364     {
365     int byte1 = ch >> 8;
366     int byte2 = ch & 255;
367    
368     if (byte1 < f->min_byte1 || byte1 > f->max_byte1
369     || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
370     return false;
371    
372     if (!f->per_char)
373     return true;
374    
375     int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
376     int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
377    
378     xcs = f->per_char + N;
379     }
380     else
381     {
382     if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
383     return false;
384    
385     if (!f->per_char)
386     return true;
387    
388     xcs = f->per_char + (ch - f->min_char_or_byte2);
389     }
390    
391     if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
392     && xcs->ascent == 0 && xcs->descent == 0)
393     return false;
394    
395     return true;
396     }
397    
398     void
399     rxvt_font_x11::draw (int x, int y,
400     const text_t *text, int len,
401     int fg, int bg)
402     {
403     // this looks like a mess /.
404     // and it is a mess /.
405     // yet we are trying to be perfect /.
406     // but the result still isn't perfect /.
407    
408     bool slow = prop
409 pcg 1.2 || width != r->TermWin.fwidth
410     || height != r->TermWin.fheight;
411 pcg 1.1
412 pcg 1.2 int base = r->TermWin.fbase;
413 pcg 1.1
414     XGCValues v;
415 pcg 1.2 v.foreground = r->PixColors[fg];
416     v.background = r->PixColors[bg];
417 pcg 1.1 v.font = f->fid;
418    
419     if (enc2b)
420     {
421     const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
422    
423     if (bg == Color_bg && !slow)
424     {
425     XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
426     XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
427     }
428     else
429     {
430 pcg 1.2 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
431 pcg 1.1
432     XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
433    
434     if (slow)
435     {
436     do
437     {
438     if (xc->byte1 || xc->byte2)
439     XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
440    
441 pcg 1.2 x += r->TermWin.fwidth;
442 pcg 1.1 xc++; len--;
443     }
444     while (len);
445     }
446     else
447     XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
448     }
449     }
450     else
451     {
452     const char *xc = enc_char (text, len, cs, slow);
453    
454     if (bg == Color_bg && !slow)
455     {
456     XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v);
457     XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
458     }
459     else
460     {
461 pcg 1.2 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
462 pcg 1.1
463     XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v);
464    
465     if (slow)
466     {
467     do
468     {
469     if (*xc)
470     XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1);
471    
472 pcg 1.2 x += r->TermWin.fwidth;
473 pcg 1.1 xc++; len--;
474     }
475     while (len);
476     }
477     else
478     XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len);
479     }
480     }
481     }
482    
483     /////////////////////////////////////////////////////////////////////////////
484    
485     #if XFT
486     #if 0
487     #define UNIBITS 21
488     //#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
489     #define SWATHBITS 8
490     #endif
491    
492     struct rxvt_font_xft : rxvt_font {
493     #if 0
494     enum {
495     SWATHCOUNT = 1 << (21 - UNIBITS),
496     SWATHSIZE = 1 << (SWATHBITS - 5)
497     };
498     typedef uint32_t swath[SWATHSIZE];
499    
500     swath *cvr[SWATHCOUNT];
501     #endif
502    
503     #if 0
504     void gen_coverage_swath (unsigned int page);
505    
506     bool has_char (uint32_t ch)
507     {
508     unsigned int page = ch >> SWATHBITS;
509     unsigned int idx = ch & ((1 << SWATHBITS) - 1);
510    
511     if (page >= SWATHCOUNT)
512     return false;
513    
514     if (!cvr[page]) gen_coverage_swath (page);
515    
516     return cvr[page][idx >> 5] & (1 << (idx & 31));
517     }
518     #endif
519     rxvt_font_xft () { f = 0; d = 0; }
520    
521     void clear ();
522    
523     bool load (int maxheight);
524    
525     void draw (int x, int y,
526     const text_t *text, int len,
527     int fg, int bg);
528    
529     bool has_codepoint (uint32_t unicode);
530    
531     protected:
532     XftFont *f;
533     XftDraw *d;
534    
535     #if 0
536     virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
537     void set_swath (uint32_t ch)
538     {
539     cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
540     }
541     #endif
542     };
543    
544     void
545     rxvt_font_xft::clear ()
546     {
547     if (f)
548     {
549 pcg 1.2 XftFontClose (DISPLAY, f);
550 pcg 1.1 f = 0;
551     }
552    
553     if (d)
554     {
555     XftDrawDestroy (d);
556     d = 0;
557     }
558    
559     #if 0
560     for (int i = 0; i < SWATHCOUNT; i++)
561     delete cvr[i];
562     #endif
563     }
564    
565     bool
566     rxvt_font_xft::load (int maxheight)
567     {
568     #if 0
569     for (int i = 0; i < SWATHCOUNT; i++)
570     cvr[i] = 0;
571     #endif
572    
573     clear ();
574    
575 pcg 1.2 f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name);
576 pcg 1.1
577     if (!f)
578     return false;
579    
580     FT_Face face = XftLockFace (f);
581    
582     prop = !FT_IS_FIXED_WIDTH (face);
583    
584     int ftheight = 0;
585    
586     for (;;)
587     {
588     XGlyphInfo g1, g2;
589     FcChar8 c;
590    
591 pcg 1.2 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
592     c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
593 pcg 1.1
594     prop = prop || g1.xOff != g2.xOff; // don't simply trust the font
595    
596     width = g2.xOff;
597     ascent = (face->size->metrics.ascender + 63) >> 6;
598     descent = (-face->size->metrics.descender + 63) >> 6;
599     height = ascent + descent;
600    
601     if (height <= maxheight || !maxheight)
602     break;
603    
604     if (ftheight)
605     {
606     // take smaller steps near the end
607     if (height > maxheight + 1) ftheight++;
608     if (height > maxheight + 2) ftheight++;
609     if (height > maxheight + 3) ftheight++;
610    
611     FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight);
612     }
613     else
614     FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight);
615     }
616    
617     XftUnlockFace (f);
618    
619     return true;
620     }
621    
622     #if 0
623     void rxvt_font::gen_coverage_swath (unsigned int page)
624     {
625     cvr[page] = new swath;
626    
627     for (int i = 0; i < SWATHSIZE; i++)
628     cvr[page][i] = 0;
629    
630     populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
631     }
632     #endif
633    
634     bool
635     rxvt_font_xft::has_codepoint (uint32_t unicode)
636     {
637 pcg 1.2 return XftCharExists (DISPLAY, f, unicode);
638 pcg 1.1 }
639    
640     void
641     rxvt_font_xft::draw (int x, int y,
642     const text_t *text, int len,
643     int fg, int bg)
644     {
645     if (!d)
646 pcg 1.2 {
647     dR;
648     d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
649     }
650 pcg 1.1
651     if (bg >= 0 && bg != Color_bg)
652 pcg 1.2 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
653 pcg 1.1 else
654 pcg 1.2 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
655 pcg 1.1
656 pcg 1.2 if (!prop && width == r->TermWin.fwidth)
657 pcg 1.1 {
658     if (sizeof (text_t) == sizeof (FcChar16))
659 pcg 1.2 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
660 pcg 1.1 else
661 pcg 1.2 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
662 pcg 1.1 }
663     else
664     {
665     while (len)
666     {
667     if (*text != NOCHAR && *text != ' ')
668     {
669     if (sizeof (text_t) == sizeof (FcChar16))
670 pcg 1.2 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1);
671 pcg 1.1 else
672 pcg 1.2 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1);
673 pcg 1.1 }
674    
675 pcg 1.2 x += r->TermWin.fwidth;
676 pcg 1.1 text++;
677     len--;
678     }
679     }
680     }
681     #endif
682    
683     /////////////////////////////////////////////////////////////////////////////
684    
685 pcg 1.2 rxvt_fontset::rxvt_fontset (rxvt_t r)
686 pcg 1.1 #ifdef EXPLICIT_CONTEXT
687 pcg 1.2 : r(r)
688 pcg 1.1 #endif
689     {
690     clear ();
691     }
692    
693     rxvt_fontset::~rxvt_fontset ()
694     {
695     clear ();
696     }
697    
698     void
699     rxvt_fontset::clear ()
700     {
701     for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
702     FONT_UNREF (*i);
703    
704     fonts.clear ();
705     base_id = 0;
706     height = 0x7fffffff;
707    
708     fallback = fallback_fonts;
709     }
710    
711     rxvt_font *
712     rxvt_fontset::new_font (const char *name, codeset cs)
713     {
714     rxvt_font *f;
715    
716     if (!name || !*name)
717     {
718     name = "";
719     f = new rxvt_font_default;
720     }
721     #if XFT
722     else if (!strncmp (name, "xft:", 4))
723     {
724     name += 4;
725     f = new rxvt_font_xft;
726     }
727     #endif
728     else if (!strncmp (name, "x:", 2))
729     {
730     name += 2;
731     f = new rxvt_font_x11;
732     }
733     else
734     f = new rxvt_font_x11;
735    
736 pcg 1.2 f->set_term (r);
737 pcg 1.1 f->set_name (strdup (name));
738    
739     f->cs = cs;
740     f->loaded = false;
741    
742     return f;
743     }
744    
745     /////////////////////////////////////////////////////////////////////////////
746    
747     void
748     rxvt_fontset::add_fonts (const char *desc)
749     {
750     if (desc)
751     {
752     char buf[512];
753     const char *end;
754    
755     do
756     {
757     while (*desc <= ' ') desc++;
758    
759     if (*desc == '[')
760     {
761     fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
762    
763     const char *extra = desc++;
764    
765     desc = strchr (desc, ']');
766    
767     if (!desc)
768     {
769     fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
770     break;
771     }
772    
773     desc++;
774     while (*desc <= ' ') desc++;
775     }
776    
777     end = strchr (desc, ',');
778     if (!end)
779     end = desc + strlen (desc);
780    
781     if (end - desc < 511)
782     {
783     strncpy (buf, desc, end - desc);
784     buf[end - desc] = 0;
785    
786     fonts.push_back (new_font (buf, CS_UNICODE));
787     }
788    
789     desc = end + 1;
790     }
791     while (*end);
792     }
793     }
794    
795     bool
796     rxvt_fontset::realize_font (int i)
797     {
798     if (fonts[i]->loaded)
799     return true;
800    
801     if (fonts[i]->load (height))
802     return fonts[i]->loaded = true;
803    
804     delete fonts[i];
805     fonts.erase (fonts.begin () + i);
806    
807     return false;
808     }
809    
810     void
811     rxvt_fontset::populate (const char *desc)
812     {
813     clear ();
814    
815     fonts.push_back (new_font (0, CS_UNICODE));
816     realize_font (0);
817    
818     add_fonts (desc);
819    
820     if (!base_id)
821     base_id = 1;
822    
823     // we currently need a base-font, no matter what
824     if (fonts.size () <= base_id)
825     {
826     add_fonts ("fixed");
827     base_id = 1;
828     }
829    
830     if (fonts.size () <= base_id || !realize_font (base_id))
831     {
832     fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
833     exit (1);
834     }
835    
836     height = fonts[base_id]->height;
837    
838     /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
839     }
840    
841     int
842     rxvt_fontset::find_font (uint32_t unicode)
843     {
844     for (int i = 0; i < fonts.size (); i++)
845     {
846     rxvt_font *f = fonts[i];
847    
848     if (!f->loaded)
849     {
850     if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
851     goto next_font;
852    
853     if (!realize_font (i))
854     {
855     --i;
856     goto next_font;
857     }
858    
859     //printf ("added font %s for %04lx\n", f->name, unicode);
860     }
861    
862     if (f->has_codepoint (unicode))
863     return i;
864    
865     next_font:
866     if (i == fonts.size () - 1 && fallback->name)
867     {
868     fonts.push_back (new_font (fallback->name, fallback->cs));
869     fallback++;
870     }
871     }
872    
873     return 0; /* we must return SOME font */
874     }
875    
876    
877