*** empty log message ***
[dana/urxvt.git] / src / defaultfont.C
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 #define DISPLAY  R->Xdisplay
27 #define DRAWABLE R->TermWin.vt
28 #define GC       R->TermWin.gc
29
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       XSetForeground (DISPLAY, GC, R->PixColors[color]);
169       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   clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
208
209   while (len--)
210     {
211       switch (*text++)
212         {
213           case NOCHAR:
214           case ZERO_WIDTH_CHAR:
215             break;
216           default:
217             XSetForeground (DISPLAY, GC, R->PixColors[fg]);
218             XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, R->TermWin.fwidth - 5, R->TermWin.fheight - 5);
219         }
220
221       x += R->TermWin.fwidth;
222     }
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               || width != R->TermWin.fwidth
410               || height != R->TermWin.fheight;
411
412   int base = R->TermWin.fbase;
413
414   XGCValues v;
415   v.foreground = R->PixColors[fg];
416   v.background = R->PixColors[bg];
417   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           clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
431
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                   x += R->TermWin.fwidth;
442                   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           clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
462
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                   x += R->TermWin.fwidth;
473                   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       XftFontClose (R->Xdisplay, f);
550       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   f = XftFontOpenName (R->Xdisplay, DefaultScreen (R->Xdisplay), name);
576
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       c = 'i'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g1);
592       c = 'W'; XftTextExtents8 (R->Xdisplay, f, &c, 1, &g2);
593
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   return XftCharExists (R->Xdisplay, f, unicode);
638 }
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     d = XftDrawCreate (R->Xdisplay, DRAWABLE, XVISUAL, XCMAP);
647
648   if (bg >= 0 && bg != Color_bg)
649     XftDrawRect (d, &R->PixColors[bg].c, x, y, R->TermWin.fwidth * len, R->TermWin.fheight);
650   else
651     clear_rect (x, y, R->TermWin.fwidth * len, R->TermWin.fheight, bg);
652
653   if (!prop && width == R->TermWin.fwidth)
654     {
655       if (sizeof (text_t) == sizeof (FcChar16))
656         XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, len);
657       else
658         XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, len);
659     }
660   else
661     {
662       while (len)
663         {
664           if (*text != NOCHAR && *text != ' ')
665             {
666               if (sizeof (text_t) == sizeof (FcChar16))
667                 XftDrawString16 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar16 *)text, 1);
668               else
669                 XftDrawString32 (d, &R->PixColors[fg].c, f, x, y + R->TermWin.fbase, (const FcChar32 *)text, 1);
670             }
671
672           x += R->TermWin.fwidth;
673           text++;
674           len--;
675         }
676     }
677 }
678 #endif
679
680 /////////////////////////////////////////////////////////////////////////////
681
682 rxvt_fontset::rxvt_fontset (pR)
683 #ifdef EXPLICIT_CONTEXT
684 : rxvt_term(R)
685 #endif
686 {
687   clear ();
688 }
689
690 rxvt_fontset::~rxvt_fontset ()
691 {
692   clear ();
693 }
694
695 void
696 rxvt_fontset::clear ()
697 {
698   for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
699     FONT_UNREF (*i);
700
701   fonts.clear ();
702   base_id = 0;
703   height = 0x7fffffff;
704
705   fallback = fallback_fonts;
706 }
707
708 rxvt_font *
709 rxvt_fontset::new_font (const char *name, codeset cs)
710 {
711   rxvt_font *f;
712   
713   if (!name || !*name)
714     {
715       name = "";
716       f = new rxvt_font_default;
717     }
718 #if XFT
719   else if (!strncmp (name, "xft:", 4))
720     {
721       name += 4;
722       f = new rxvt_font_xft;
723     }
724 #endif
725   else if (!strncmp (name, "x:", 2))
726     {
727       name += 2;
728       f = new rxvt_font_x11;
729     }
730   else
731     f = new rxvt_font_x11;
732
733   f->set_term (aR);
734   f->set_name (strdup (name));
735
736   f->cs = cs;
737   f->loaded = false;
738
739   return f;
740 }
741
742 /////////////////////////////////////////////////////////////////////////////
743
744 void
745 rxvt_fontset::add_fonts (const char *desc)
746 {
747   if (desc)
748     {
749       char buf[512];
750       const char *end;
751
752       do
753         {
754           while (*desc <= ' ') desc++;
755
756           if (*desc == '[')
757             {
758               fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
759
760               const char *extra = desc++;
761
762               desc = strchr (desc, ']');
763
764               if (!desc)
765                 {
766                   fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n");
767                   break;
768                 }
769
770               desc++;
771               while (*desc <= ' ') desc++;
772             }
773
774           end = strchr (desc, ',');
775           if (!end)
776             end = desc + strlen (desc);
777
778           if (end - desc < 511)
779             {
780               strncpy (buf, desc, end - desc);
781               buf[end - desc] = 0;
782
783               fonts.push_back (new_font (buf, CS_UNICODE));
784             }
785
786           desc = end + 1;
787         }
788       while (*end);
789     }
790 }
791
792 bool
793 rxvt_fontset::realize_font (int i)
794 {
795   if (fonts[i]->loaded)
796     return true;
797
798   if (fonts[i]->load (height))
799     return fonts[i]->loaded = true;
800
801   delete fonts[i];
802   fonts.erase (fonts.begin () + i);
803
804   return false;
805 }
806
807 void
808 rxvt_fontset::populate (const char *desc)
809 {
810   clear ();
811
812   fonts.push_back (new_font (0, CS_UNICODE));
813   realize_font (0);
814
815   add_fonts (desc);
816
817   if (!base_id)
818     base_id = 1;
819
820   // we currently need a base-font, no matter what
821   if (fonts.size () <= base_id)
822     {
823       add_fonts ("fixed");
824       base_id = 1;
825     }
826
827   if (fonts.size () <= base_id || !realize_font (base_id))
828     {
829       fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
830       exit (1);
831     }
832
833   height = fonts[base_id]->height;
834
835   /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
836 }
837
838 int
839 rxvt_fontset::find_font (uint32_t unicode)
840 {
841   for (int i = 0; i < fonts.size (); i++)
842     {
843       rxvt_font *f = fonts[i];
844
845       if (!f->loaded)
846         {
847           if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
848             goto next_font;
849
850           if (!realize_font (i))
851             {
852               --i;
853               goto next_font;
854             }
855
856           //printf ("added font %s for %04lx\n", f->name, unicode);
857         }
858
859       if (f->has_codepoint (unicode))
860         return i;
861
862     next_font:
863       if (i == fonts.size () - 1 && fallback->name)
864         {
865           fonts.push_back (new_font (fallback->name, fallback->cs));
866           fallback++;
867         }
868     }
869
870   return 0; /* we must return SOME font */
871 }
872
873
874