Filter:   InfoImg
download fl_font_x.cxx
Language: C++
Copyright: Copyright 1998-2004 by Bill Spitzak and others.
LOC: 530
Project Info
Fast Light Tool Kit(fltk)
Server: SourceForge
Type: cvs
...Forge\f\fltk\fltk\fltk\src\
   allfiles.xbm
   ARRAY.h
   browser_minus.xbm
   browser_plus.xbm
   cmap.cxx
   CycleButton.cxx
   d1.xbm
   d1_mask.xbm
   dlload_osx.cxx
   dump_compose.c
   ew.xbm
   ew_mask.xbm
   fastarrow.h
   filename_absolute.cxx
   filename_expand.cxx
   filename_ext.cxx
   filename_isdir.cxx
   filename_list.cxx
   filename_match.cxx
   filename_setext.cxx
   Fl.cxx
   Fl_abort.cxx
   Fl_add_idle.cxx
   Fl_Adjuster.cxx
   Fl_Align_Group.cxx
   fl_arc.cxx
   fl_arci.cxx
   Fl_arg.cxx
   fl_ask.cxx
   Fl_Bar.cxx
   Fl_Bitmap.cxx
   fl_bmp.cxx
   Fl_Box.cxx
   Fl_Boxtype.cxx
   Fl_Browser.cxx
   Fl_Browser_load.cxx
   Fl_Button.cxx
   fl_call_main.c
   Fl_Check_Button.cxx
   Fl_Choice.cxx
   fl_clip.cxx
   Fl_Clock.cxx
   fl_cmap.h
   fl_color.cxx
   Fl_Color_Chooser.cxx
   fl_color_mac.cxx
   fl_color_win32.cxx
   fl_color_x.cxx
   Fl_compose.cxx
   fl_cursor.cxx
   fl_curve.cxx
   Fl_Dial.cxx
   fl_diamond_box.cxx
   Fl_display.cxx
   fl_dnd.cxx
   fl_dnd_mac.cxx
   fl_dnd_win32.cxx
   fl_dnd_x.cxx
   fl_draw.cxx
   fl_draw_image.cxx
   fl_draw_image_mac.cxx
   fl_draw_image_win32.cxx
   fl_draw_image_x.cxx
   fl_draw_pixmap.cxx
   fl_engraved_label.cxx
   fl_file_chooser.cxx
   Fl_FileBrowser.cxx
   Fl_FileChooser.cxx
   Fl_FileChooser.fl
   Fl_FileChooser2.cxx
   Fl_FileIcon.cxx
   Fl_FileIcon2.cxx
   Fl_FileInput.cxx
   Fl_Float_Input.cxx
   fl_font.cxx
   fl_font_mac.cxx
   fl_font_win32.cxx
   fl_font_x.cxx
   fl_font_xft.cxx
   Fl_get_key.cxx
   Fl_get_key_mac.cxx
   Fl_get_key_win32.cxx
   fl_gif.cxx
   fl_glyph.cxx
   Fl_Group.cxx
   Fl_Help_Dialog.cxx
   Fl_Help_View.cxx
   Fl_Highlight_Button.cxx
   Fl_Image.cxx
   Fl_Input.cxx
   Fl_Input_Browser.cxx
   Fl_Item.cxx
   Fl_key_name.cxx
   fl_labeltype.cxx
   Fl_Light_Button.cxx
   fl_list_fonts.cxx
   fl_list_fonts_mac.cxx
   fl_list_fonts_win32.cxx
   fl_list_fonts_x.cxx
   fl_load_plugin.cxx
   Fl_lock.cxx
   Fl_mac.cxx
   Fl_Menu.cxx
   Fl_Menu_.cxx
   Fl_Menu_add.cxx
   Fl_Menu_Bar.cxx
   Fl_Menu_Button.cxx
   Fl_Menu_global.cxx
   Fl_Menu_Item.cxx
   Fl_Menu_Window.cxx
   Fl_Multi_Image.cxx
   Fl_Numeric_Input.cxx
   Fl_Output.cxx
   fl_oval_box.cxx
   fl_overlay.cxx
   fl_overlay_visual.cxx
   Fl_own_colormap.cxx
   Fl_Pack.cxx
   Fl_Pixmap.cxx
   fl_plastic_box.cxx
   Fl_ProgressBar.cxx
   Fl_Radio_Button.cxx
   fl_rect.cxx
   Fl_Repeat_Button.cxx
   Fl_Return_Button.cxx
   fl_rgb.cxx
   Fl_RGB_Image.cxx
   Fl_Roller.cxx
   fl_round_box.cxx
   fl_rounded_box.cxx
   Fl_Scroll.cxx
   fl_scroll_area.cxx
   Fl_Scrollbar.cxx
   fl_set_fonts_mac.cxx
   fl_shadow_box.cxx
   Fl_Shaped_Window.cxx
   Fl_Shared_Image.cxx
   fl_show_colormap.cxx
   Fl_Single_Window.cxx
   Fl_Slider.cxx
   Fl_String_List.cxx
   Fl_Style.cxx
   Fl_Style_Set.cxx
   fl_symbols.cxx
   Fl_Tabs.cxx
   Fl_Text_Buffer.cxx
   Fl_Text_Display.cxx
   Fl_Text_Editor.cxx
   Fl_Tile.cxx
   Fl_Tiled_Image.cxx
   Fl_Tooltip.cxx
   Fl_Valuator.cxx
   Fl_Value_Input.cxx
   Fl_Value_Slider.cxx
   fl_vertex.cxx
   Fl_visual.cxx
   Fl_Widget.cxx
   Fl_win32.cxx
   Fl_Window.cxx
   Fl_Window_fullscreen.cxx
   Fl_Window_hotspot.cxx
   Fl_Window_iconize.cxx
   Fl_x.cxx
   Fl_XColor.h
   fl_xpm.cxx
   fltk_theme.cxx
   fltk_theme_win32.cxx
   fltk_theme_x.cxx
   IFont.h
   mediumarrow.h
   new.xbm
   ns.xbm
   ns_mask.xbm
   pktdef.h
   scandir.c
   scandir_win32.c
   shortcuts.cxx
   slowarrow.h
   string.c
   up.xbm
   utf.c
   ValueOutput.cxx
   vsnprintf.c
   wintab.h
   wintabx.h
   XColorMap.h
   xutf8.cxx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// "$Id: fl_font_x.cxx,v 1.26 2004/12/16 18:40:43 spitzak Exp $"
//
// Copyright 1998-2004 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//

/* Donated code that selects from an alternative font if the character
  cannot be printed in the current font. Did not work very well for me
  and it slows down normal printing a lot. */
#define X_UTF8_FONT 0

#include <fltk/draw.h>
#include <fltk/error.h>
#include <fltk/x.h>
#include <fltk/math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fltk/utf.h>
#include "IFont.h"
using namespace fltk;

// One of these is created for each font+size+encoding combination we use:
struct FontSize {
  FontSize* next;	// linked list for a single Font
  XFontStruct* font;
  const char* encoding;
  FontSize(const char* xfontname, const char*);
  const char* name;
  unsigned minsize;	// smallest point size that should use this
  unsigned maxsize;	// largest point size that should use this
  //  ~FontSize();
};

fltk::Font* fltk::Font::plus(int x) {
  IFont* font = (IFont*)this;
  if (x & BOLD) font = font->bold;
  if (x & ITALIC) font = font->italic;
  return &(font->f);
}

const char* fltk::Font::system_name() {
  return ((IFont*)this)->system_name;
}

static FontSize *current;
static GC font_gc; // which gc the font was set in last time

/*! Returns the operating-system dependent structure defining the
  current font. You must include <fltk/x.h> to use this. */
XFontStruct* fltk::xfont() {return current->font;}

/*!
  Return the full X11 name for the currently selected font+size+encoding.
  This is mostly useful for debugging, though maybe you need to copy
  it to another program.

  On non-X systems, and on modern X systems with Xft (antialiased fonts)
  this returns current_font()->system_name().
*/
const char* fltk::Font::current_name() {return current->name;}

FontSize::FontSize(const char* name, const char* nname) {
  this->name = nname ? nname : name;
  font = XLoadQueryFont(xdisplay, name);
  if (!font) {
    warning("bad font: %s", name);
    font = XLoadQueryFont(xdisplay, "fixed"); // if fixed fails we crash
  }
  encoding = 0;
}

#if 0 // this is never called!
FontSize::~FontSize() {
  if (this == current) current = 0;
  XFreeFont(xdisplay, font);
}
#endif

////////////////////////////////////////////////////////////////
// Things you can do once the font+size has been selected:

// I see no sign of "FontSets" working. Instead this supposedly will
// draw the correct letters if you happen to pick an iso10646-1 font.

// This is similar to utf8towc() but works with the big-endian-only
// structure X seems to want, and does not bother with surrogate
// pairs.  If all characters are 1 byte or errors it returns
// null. Otherwise it converts it to 16-bit and returns the allocated
// buffer and size:
static XChar2b* utf8to2b(const char* text, int n, int* charcount) {
  const char* p = text;
  const char* e = text+n;
  bool sawutf8 = false;
  int count = 0;
  while (p < e) {
    if (*(unsigned char*)p < 0x80) p++; // ascii
    else if (*(unsigned char*)p < 0xa0) {sawutf8 = 1; p++;} //cp1252
    else if (*(unsigned char*)p < 0xC2) p++; // other bad code
    else {
      int len; utf8decode(p,e,&len);
      if (len > 1) sawutf8 = true;
      else if (!len) len = 1;
      p += len;
    }
    count++;
  }
  if (!sawutf8) return 0;
  *charcount = count;
  XChar2b* buffer = new XChar2b[count];
  count = 0;
  p = text;
  while (p < e) {
    unsigned char c = *(unsigned char*)p;
    if (c < 0xC2) { // ascii letter or bad code
      buffer[count].byte1 = 0;
      buffer[count].byte2 = c;
      p++;
    } else {
      int len;
      unsigned n = utf8decode(p,e,&len);
      if (n > 0xffff) n = '?';
      p += len;
      buffer[count].byte1 = n>>8;
      buffer[count].byte2 = n;
    }
    count++;
  }
  return buffer;
}

// return dash number N, or pointer to ending null if none:
static const char *
font_word(const char* p, int n) {
  while (*p) {if (*p=='-') {if (!--n) break;} p++;}
  return p;
}

#if X_UTF8_FONT

typedef struct {
  const char* name;
  XFontStruct* font;
  bool enable;
} XFontUtf8;
XFontUtf8 xfont_utf8[32] = {0};

#define FONT_INDEX_DISABLE -1
#define FONT_INDEX_FULL    -2
//#define FONT_DEBUG

static void get_font_entry(char *buf, int buf_size, char **pp)
{
  char *p, *r;
  int len;

  r = *pp;
  p = strchr(r, '-');
  if (!p) {
    if (buf)
      buf[0] = '\0';
  } else {
    if (buf) {
      len = p - r;
      if (len >= buf_size)
        len = buf_size - 1;
      memcpy(buf, r, len);
      buf[len] = '\0';
    }
    *pp = p + 1;
  }
}

static const char* get_utf8_font_name_from_basefont(const char *basefont, int safelevel = 0)
{
  char *pname = strdup(basefont), *pnameold;
  char weight[16], slant[16], ptsize[16];
  static char fontname[256];

  pnameold = pname;
  get_font_entry(NULL, 0, &pname);
  get_font_entry(NULL, 0, &pname);
  get_font_entry(NULL, 0, &pname);
  get_font_entry(weight, sizeof(weight), &pname); /* weight */
  get_font_entry(slant, sizeof(slant), &pname); /* slant */
  get_font_entry(NULL, 0, &pname); /* adstyle */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  get_font_entry(ptsize, sizeof(ptsize), &pname); /* size */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  get_font_entry(NULL, 0, &pname); /* pixsize */
  free(pnameold);

  switch(safelevel) {
  case 0:
    sprintf(fontname, "-*-*-%s-%s-normal--%s-*-*-*-*-*-iso10646-1",
      weight, slant, ptsize);
    break;
  case 1:
    sprintf(fontname, "-gnu-unifont-%s-%s-normal--%s-*-*-*-*-*-iso10646-1",
      weight, slant, "*");
    break;
  case 2:
    sprintf(fontname, "-*-*-%s-%s-normal--%s-*-*-*-*-*-iso10646-1",
      weight, slant, "*");
    break;
  case 3:
    sprintf(fontname, "-*-*-%s-%s-normal--%s-*-*-*-*-*-iso10646-1",
      "*", "*", ptsize);
    break;
  default:
    return NULL;
  }
  return fontname;
}

static int get_xfont_utf8_index(const char *fontname)
{
  for(unsigned int i = 0; i < sizeof(xfont_utf8)/sizeof(XFontUtf8); i++) {
    if (xfont_utf8[i].name == NULL)
      return i;
    if (!strcmp(xfont_utf8[i].name, fontname)) {
      if (xfont_utf8[i].enable == false)
        return -1;
      return i;
    }
  }
  return -2;
}

static bool font_is_showable(XFontStruct *f, unsigned int cc)
{
  unsigned int b1, b2;
  XCharStruct *cs;

  if (f->min_byte1 == 0 && f->max_byte1 == 0) {
    if (cc > f->max_char_or_byte2) return false;
    if (cc < f->min_char_or_byte2) return false;
    cc -= f->min_char_or_byte2;
  } else {
    b1 = (cc >> 8) & 0xff;
    b2 = cc & 0xff;
    if (b1 > f->max_byte1) return false;
    if (b1 < f->min_byte1) return false;
    b1 -= f->min_byte1;
    if (b2 > f->max_char_or_byte2) return false;
    if (b2 < f->min_char_or_byte2) return false;
    b2 -= f->min_char_or_byte2;
    cc = b1 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1) + b2;
    if (cc >= 0xffff) return false;
  }
  cs = f->per_char;
  if (!cs)
    return &f->min_bounds; /* all char have same metrics */
  cs += cc;
  /* fast test for non existant char */
  if (cs && cs->width == 0 &&
    (cs->ascent | cs->descent | cs->rbearing | cs->lbearing) == 0) {
    return false;
  } else {
    return true;
  }
}

static XFontStruct* get_xfont_utf8(const char* basefont, int cc)
{
  XFontStruct* f = 0;
  char fontname[256];
  char buf[256];
  char familyold[256];
  char family[256];
  char *pname;
  unsigned int realsize;

  strcpy(fontname, basefont);
  // replace fontname if ptsize = 0
  realsize = -atol(font_word(fontname, 7));
  if (!realsize) {
    pname = fontname;
    char realname[256];

    realname[0] = 0;
    for(int entry = 0; entry < 14; entry++) {
      get_font_entry(buf, sizeof(buf), &pname);
      if (entry == 7)
        sprintf(buf, "%d", current->maxsize);
      strcat(realname, buf);
      strcat(realname, "-");
      if (entry == 13)
        strcat(realname, pname);
    }
    strcpy(fontname, realname);
  }

  int font_index = get_xfont_utf8_index(fontname);
  switch(font_index) {
  case FONT_INDEX_FULL:
    //printf("full request = %s\n", fontname);
    break;
  case FONT_INDEX_DISABLE:
    //printf("disable request = %s\n", fontname);
    break;
  default:
    if (!xfont_utf8[font_index].name) {
      int safelevel = 0;
      const char* utf8_fontname;
#ifdef FONT_DEBUG
      printf("basefont = %s\n", basefont);
#endif

      xfont_utf8[font_index].name = strdup(fontname);

      // try some safety fonts
      while((utf8_fontname = get_utf8_font_name_from_basefont(fontname, safelevel))) {
        char** xlist;
        int nlist = 0;
        unsigned int acceptold = 0, ptsize;
        char acceptfont[256];
        acceptfont[0] = 0;
        familyold[0] = 0;

#ifdef FONT_DEBUG
        printf(" pattern = %s\n", utf8_fontname);
#endif
        xlist = XListFonts(xdisplay, utf8_fontname, 1000, &nlist);
        for(int nfont = 0; nfont < nlist; nfont++) {
          // get family name for skipping entry
          pname = xlist[nfont];
          get_font_entry(NULL, 0, &pname);
          get_font_entry(buf, sizeof(buf), &pname);
          strcpy(family, buf);
          if (!strcmp(family, familyold)) {
            continue;
          } else
          if (acceptfont[0])
            break;

          // don't use scalable font
          if (atol(font_word(xlist[nfont], 12)) == 0) {
            strcpy(familyold, family);
            continue;
          }
          ptsize = -atol(font_word(xlist[nfont], 7));
          if (ptsize > current->maxsize+2) {
            strcpy(familyold, family);
            continue;
          }
#ifdef FONT_DEBUG
		  printf("trying %s\n", xlist[nfont]);
#endif
          XFontStruct *ff = XLoadQueryFont(xdisplay, xlist[nfont]);
          if (font_is_showable(ff, cc)) {
            strcpy(familyold, family);
            if (safelevel == 0) {
#ifdef FONT_DEBUG
              printf("   match = %s\n", xlist[nfont]);
#endif
              xfont_utf8[font_index].font = ff;
              break;
            } else {
              if (ptsize > acceptold) {
#ifdef FONT_DEBUG
                printf("   match = %s\n", xlist[nfont]);
#endif
                strcpy(acceptfont, xlist[nfont]);
                acceptold = ptsize;
				if (ptsize == realsize)
                  break;
              }
            }
		  } else {
            strcpy(familyold, family);
          }
          XFreeFont(xdisplay, ff);
        }
        XFreeFontNames(xlist);
        if (safelevel > 0 && acceptfont[0]) {
          xfont_utf8[font_index].font = XLoadQueryFont(xdisplay, acceptfont);
        }
        if (!xfont_utf8[font_index].font) {
          xfont_utf8[font_index].enable = false;
        } else {
          xfont_utf8[font_index].enable = true;
          f = xfont_utf8[font_index].font;
          break;
        }
        safelevel++;
      }
    } else {
      f = xfont_utf8[font_index].font;
    }
  }
  return f;
}

#endif // X_UTF8_FONT

void fltk::drawtext_transformed(const char *text, int n, float x, float y) {
  if (font_gc != gc) {
    // I removed this, the user MUST set the font before drawing: (was)
    // if (!current) setfont(HELVETICA, NORMAL_SIZE);
    font_gc = gc;
    XSetFont(xdisplay, gc, current->font->fid);
  }
  int count;
  XChar2b* buffer = utf8to2b(text,n,&count);
  if (buffer) {
#if !X_UTF8_FONT
    XDrawString16(xdisplay, xwindow, gc,
		  int(floorf(x+.5f)),
		  int(floorf(y+.5f)), buffer, n);
#else
#ifdef FONT_DEBUG
	//printf("current = %s\n", current->name);
#endif
    XFontStruct *f = current->font, *oldf = f;
    for(int i = 0; i < count; i++) {
      int cc = (buffer[i].byte1 << 8) + buffer[i].byte2;
      if (!font_is_showable(current->font, cc)) {
        f = get_xfont_utf8(current->name, cc);
	if (!f) {
	  f = current->font;
#ifdef FONT_DEBUG
	  printf("can't draw %C\n");
#endif
	  buffer[i].byte1 = 0;
	  buffer[i].byte2 = '?';
	}
      } else
	f = current->font;
      if (f != oldf) {
        XSetFont(xdisplay, gc, f->fid);
	oldf = f;
      }
      XDrawString16(xdisplay, xwindow, gc,
		  int(floorf(x+.5f)),
		  int(floorf(y+.5f)), &buffer[i], 1);
      x += XTextWidth16(f, &buffer[i], 1);
    }
    XSetFont(xdisplay, gc, current->font->fid);
#endif
    delete[] buffer;
  } else {
    XDrawString(xdisplay, xwindow, gc,
		int(floorf(x+.5f)),
		int(floorf(y+.5f)), text, n);
  }
}

float fltk::getascent()  { return current->font->ascent; }

float fltk::getdescent() { return current->font->descent; }

float fltk::getwidth(const char *text, int n) {
  int count;
  XChar2b* buffer = utf8to2b(text,n,&count);
  if (buffer) {
#if !X_UTF8_FONT
    float r = XTextWidth16(current->font, buffer, count);
#else
    float r = 0;
    XFontStruct *f = current->font;
    for(int i = 0; i < count; i++) {
      int cc = (buffer[i].byte1 << 8) + buffer[i].byte2;
      if (!font_is_showable(current->font, cc)) {
        f = get_xfont_utf8(current->name, cc);
	if (!f) {
	  f = current->font;
	  buffer[i].byte1 = 0;
	  buffer[i].byte2 = '?';
	}
      } else
	f = current->font;
      r += XTextWidth16(f, &buffer[i], 1);
    }
#endif
    delete[] buffer;
    return r;
  } else {
    return XTextWidth(current->font, text, n);
  }
}

////////////////////////////////////////////////////////////////
// The rest of this file is the enormous amount of crap you have to
// do to get a font & size out of X.  To select a font+size, all
// matchine X fonts are listed with XListFonts, and then the first
// of the following is found and used:
//
//	pixelsize == size
//	pixelsize == 0 (which indicates a scalable font)
//	the largest pixelsize < size
//	the smallest pixelsize > size
//
// If any fonts match the encoding() then the search is limited
// to those matching fonts. Otherwise all fonts are searched and thus
// a random encoding is chosen.
//
// I have not been able to find any other method than a search
// that will reliably return a bitmap version of the font if one is
// available at the correct size.  This is because X will not use a
// bitmap font unless all the extra fields are filled in correctly.
//
// Fltk uses pixelsize, not "pointsize".  This is what everybody wants!

void fltk::setfont(Font* font, float psize) {
  FontSize* f = current;
  IFont* t = (IFont*)font;

  // only integers supported right now (this can be improved):
  psize = int(psize+.5);
  unsigned size = unsigned(psize);

  // See if the current font is correct:
  if (font == current_font_ && psize == current_size_ &&
      (f->encoding==encoding_ ||
	   (!encoding_ || !strcmp(f->encoding, encoding_))))
    return;
  current_font_ = font; current_size_ = psize;

  // search the FontSize we have generated already:
  for (f = t->first; f; f = f->next)
    if (f->minsize <= size && f->maxsize >= size
        && (f->encoding==encoding_ ||
 	    !f->encoding ||
		(!encoding_ || !strcmp(f->encoding, encoding_)))) {
      goto DONE;
    }

  // run XListFonts if it has not been done yet:
  if (!t->xlist) {
    open_display();
    t->xlist = XListFonts(xdisplay, t->system_name, 100, &(t->n));
    if (!t->xlist || t->n<=0) {	// use variable if no matching font...
      t->first = f = new FontSize("variable",0);
      f->minsize = 0;
      f->maxsize = 32767;
      goto DONE;
    }
  }

  // now search the XListFonts results:
  {char* name = t->xlist[0];
  unsigned ptsize = 0;	// best one found so far
  char namebuffer[1024];	// holds scalable font name
  bool found_encoding = false;
  int m = t->n; if (m<0) m = -m;
  char* nname = 0;

  for (int n=0; n < m; n++) {

    char* thisname = t->xlist[n];
    // get the encoding field:
    const char* this_encoding = font_word(thisname, 13);
    if (*this_encoding++ && !strcmp(this_encoding, encoding_)) {
      // forget any wrong encodings when we find a correct one:
      if (!found_encoding) ptsize = 0;
      found_encoding = true;
    } else {
      // ignore later wrong encodings after we find a correct one:
      if (found_encoding) continue;
    }
    // get the pixelsize field:
    const char* c = font_word(thisname,7);
    unsigned thissize = *c ? atoi(++c) : 32767;
    if (thissize == size) {
      // Use an exact match:
      name = nname = thisname;
      ptsize = size;
      if (found_encoding) break;
    } else if (!thissize && ptsize!=size) {
      // Use a scalable font if no exact match found:
      int l = c-thisname;
      memcpy(namebuffer,thisname,l);
      // print the pointsize into it:
      if (size>=100) namebuffer[l++] = size/100+'0';
      if (size>=10) namebuffer[l++] = (size/10)%10+'0';
      namebuffer[l++] = (size%10)+'0';
      while (*c == '0') c++;
      strcpy(namebuffer+l,c);
      name = namebuffer;
      nname = thisname;
      ptsize = size;
    } else if (!ptsize ||	// no fonts yet
               thissize < ptsize && ptsize > size || // current font too big
               thissize > ptsize && thissize <= size // current too small
      ) {
      // Use the nearest fixed size font:
      name = nname = thisname;
      ptsize = thissize;
    }
  }

  // If we didn't find an exact match, search the list to see if we already
  // found this font:
  if (ptsize != size || !found_encoding) {
    for (f = t->first; f; f = f->next) {
      if (f->minsize <= ptsize && f->maxsize >= ptsize &&
	  (!found_encoding || !strcmp(f->encoding, encoding_))) {
	if (f->minsize > size) f->minsize = size;
	if (f->maxsize < size) f->maxsize = size;
	goto DONE;
      }
    }
  }

  // okay, we definately have some name, make the font:
  f = new FontSize(name,nname);
  // we pretend it has the current encoding even if it does not, so that
  // it is quickly matched when searching for it again with the same
  // encoding:
  f->encoding = encoding_;
  if (ptsize < size) {f->minsize = ptsize; f->maxsize = size;}
  else {f->minsize = size; f->maxsize = ptsize;}
  f->next = t->first;
  t->first = f;}
 DONE:
  if (f != current) {
    current = f;
    font_gc = 0;
  }
}

////////////////////////////////////////////////////////////////

// The predefined fonts that fltk has:
static IFont fonts [] = {
  {{"helvetica",0}, "-*-helvetica-medium-r-normal--*",	fonts+1, fonts+2},
  {{"helvetica",1}, "-*-helvetica-bold-r-normal--*", 	fonts+1, fonts+3},
  {{"helvetica",2}, "-*-helvetica-medium-o-normal--*",	fonts+3, fonts+2},
  {{"helvetica",3}, "-*-helvetica-bold-o-normal--*",	fonts+3, fonts+3},
  {{"courier", 0},  "-*-courier-medium-r-normal--*",	fonts+5, fonts+6},
  {{"courier", 1},  "-*-courier-bold-r-normal--*",	fonts+5, fonts+7},
  {{"courier", 2},  "-*-courier-medium-o-normal--*",	fonts+7, fonts+6},
  {{"courier", 3},  "-*-courier-bold-o-normal--*",	fonts+7, fonts+7},
  {{"times", 0},    "-*-times-medium-r-normal--*",	fonts+9, fonts+10},
  {{"times", 1},    "-*-times-bold-r-normal--*",	fonts+9, fonts+11},
  {{"times", 2},    "-*-times-medium-i-normal--*",	fonts+11,fonts+10},
  {{"times", 3},    "-*-times-bold-i-normal--*",	fonts+11,fonts+11},
  {{"symbol", 0},   "-*-symbol-*",			fonts+12,fonts+12},
  {{"lucidatypewriter", 0}, "-*-lucidatypewriter-medium-r-normal-sans-*", fonts+14,fonts+14},
  {{"lucidatypewriter", 1}, "-*-lucidatypewriter-bold-r-normal-sans-*", fonts+14,fonts+14},
  {{"zapf dingbats", 0}, "-*-*zapf dingbats-*",		fonts+15,fonts+15},
};

fltk::Font* const fltk::HELVETICA 		= &(fonts[0].f);
fltk::Font* const fltk::HELVETICA_BOLD		= &(fonts[1].f);
fltk::Font* const fltk::HELVETICA_ITALIC	= &(fonts[2].f);
fltk::Font* const fltk::HELVETICA_BOLD_ITALIC	= &(fonts[3].f);
fltk::Font* const fltk::COURIER 		= &(fonts[4].f);
fltk::Font* const fltk::COURIER_BOLD		= &(fonts[5].f);
fltk::Font* const fltk::COURIER_ITALIC		= &(fonts[6].f);
fltk::Font* const fltk::COURIER_BOLD_ITALIC	= &(fonts[7].f);
fltk::Font* const fltk::TIMES 			= &(fonts[8].f);
fltk::Font* const fltk::TIMES_BOLD		= &(fonts[9].f);
fltk::Font* const fltk::TIMES_ITALIC		= &(fonts[10].f);
fltk::Font* const fltk::TIMES_BOLD_ITALIC	= &(fonts[11].f);
fltk::Font* const fltk::SYMBOL_FONT		= &(fonts[12].f);
fltk::Font* const fltk::SCREEN_FONT		= &(fonts[13].f);
fltk::Font* const fltk::SCREEN_BOLD_FONT	= &(fonts[14].f);
fltk::Font* const fltk::ZAPF_DINGBATS		= &(fonts[15].f);

/*! For back-compatabilty with FLTK1, this turns an integer into one
  of the built-in fonts. 0 = HELVETICA. */
fltk::Font* fltk::font(int i) {return &(fonts[i%16].f);}

//
// End of "$Id: fl_font_x.cxx,v 1.26 2004/12/16 18:40:43 spitzak Exp $"
//