fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_font_x.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Standard X11 font selection code for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 #ifndef FL_DOXYGEN
19 
22  if (!font) {
23  Fl::warning("bad font: %s", name);
25  }
26 # if HAVE_GL
27  listbase = 0;
28  for (int u = 0; u < 64; u++) glok[u] = 0;
29 # endif
30 }
31 
32 Fl_XFont_On_Demand fl_xfont;
33 
35 # if HAVE_GL
36 // Delete list created by gl_draw(). This is not done by this code
37 // as it will link in GL unnecessarily. There should be some kind
38 // of "free" routine pointer, or a subclass?
39 // if (listbase) {
40 // int base = font->min_char_or_byte2;
41 // int size = font->max_char_or_byte2-base+1;
42 // int base = 0; int size = 256;
43 // glDeleteLists(listbase+base,size);
44 // }
45 # endif
46  if (this == fl_graphics_driver->font_descriptor()) {
48  fl_xfont = 0;
49  }
51 }
52 
54 
55 // WARNING: if you add to this table, you must redefine FL_FREE_FONT
56 // in Enumerations.H & recompile!!
58 {"-*-helvetica-medium-r-normal--*"},
59 {"-*-helvetica-bold-r-normal--*"},
60 {"-*-helvetica-medium-o-normal--*"},
61 {"-*-helvetica-bold-o-normal--*"},
62 {"-*-courier-medium-r-normal--*"},
63 {"-*-courier-bold-r-normal--*"},
64 {"-*-courier-medium-o-normal--*"},
65 {"-*-courier-bold-o-normal--*"},
66 {"-*-times-medium-r-normal--*"},
67 {"-*-times-bold-r-normal--*"},
68 {"-*-times-medium-i-normal--*"},
69 {"-*-times-bold-i-normal--*"},
70 {"-*-symbol-*"},
71 {"-*-lucidatypewriter-medium-r-normal-sans-*"},
72 {"-*-lucidatypewriter-bold-r-normal-sans-*"},
73 {"-*-*zapf dingbats-*"}
74 };
75 
77 
78 #define MAXSIZE 32767
79 
80 // return dash number N, or pointer to ending null if none:
81 const char* fl_font_word(const char* p, int n) {
82  while (*p) {if (*p=='-') {if (!--n) break;} p++;}
83  return p;
84 }
85 
86 // return a pointer to a number we think is "point size":
87 char* fl_find_fontsize(char* name) {
88  char* c = name;
89  // for standard x font names, try after 7th dash:
90  if (*c == '-') {
91  c = (char*)fl_font_word(c,7);
92  if (*c++ && isdigit(*c)) return c;
93  return 0; // malformed x font name?
94  }
95  char* r = 0;
96  // find last set of digits:
97  for (c++;* c; c++)
98  if (isdigit(*c) && !isdigit(*(c-1))) r = c;
99  return r;
100 }
101 
102 //const char* fl_encoding = "iso8859-1";
103 const char* fl_encoding = "iso10646-1";
104 
105 // return true if this matches fl_encoding:
106 int fl_correct_encoding(const char* name) {
107  if (*name != '-') return 0;
108  const char* c = fl_font_word(name,13);
109  return (*c++ && !strcmp(c,fl_encoding));
110 }
111 
112 static const char *find_best_font(const char *fname, int size) {
113  int cnt;
114  static char **list = NULL;
115 // locate or create an Fl_Font_Descriptor for a given Fl_Fontdesc and size:
116  if (list) XFreeFontNames(list);
117  list = XListFonts(fl_display, fname, 100, &cnt);
118  if (!list) return "fixed";
119 
120  // search for largest <= font size:
121  char* name = list[0]; int ptsize = 0; // best one found so far
122  int matchedlength = 32767;
123  char namebuffer[1024]; // holds scalable font name
124  int found_encoding = 0;
125  int m = cnt; if (m<0) m = -m;
126  for (int n=0; n < m; n++) {
127  char* thisname = list[n];
128  if (fl_correct_encoding(thisname)) {
129  if (!found_encoding) ptsize = 0; // force it to choose this
130  found_encoding = 1;
131  } else {
132  if (found_encoding) continue;
133  }
134  char* c = (char*)fl_find_fontsize(thisname);
135  int thissize = c ? atoi(c) : MAXSIZE;
136  int thislength = strlen(thisname);
137  if (thissize == size && thislength < matchedlength) {
138  // exact match, use it:
139  name = thisname;
140  ptsize = size;
141  matchedlength = thislength;
142  } else if (!thissize && ptsize!=size) {
143  // whoa! A scalable font! Use unless exact match found:
144  int l = c-thisname;
145  memcpy(namebuffer,thisname,l);
146  l += sprintf(namebuffer+l,"%d",size);
147  while (*c == '0') c++;
148  strcpy(namebuffer+l,c);
149  name = namebuffer;
150  ptsize = size;
151  } else if (!ptsize || // no fonts yet
152  (thissize < ptsize && ptsize > size) || // current font too big
153  (thissize > ptsize && thissize <= size) // current too small
154  ) {
155  name = thisname;
156  ptsize = thissize;
157  matchedlength = thislength;
158  }
159  }
160 
161 // if (ptsize != size) { // see if we already found this unscalable font:
162 // for (f = s->first; f; f = f->next) {
163 // if (f->minsize <= ptsize && f->maxsize >= ptsize) {
164 // if (f->minsize > size) f->minsize = size;
165 // if (f->maxsize < size) f->maxsize = size;
166 // return f;
167 // }
168 // }
169 // }
170 //
171 // // okay, we definately have some name, make the font:
172 // f = new Fl_Font_Descriptor(name);
173 // if (ptsize < size) {f->minsize = ptsize; f->maxsize = size;}
174 // else {f->minsize = size; f->maxsize = ptsize;}
175 // f->next = s->first;
176 // s->first = f;
177 // return f;
178 
179  return name;
180 }
181 
182 static char *put_font_size(const char *n, int size)
183 {
184  int i = 0;
185  char *buf;
186  const char *ptr;
187  const char *f;
188  char *name;
189  int nbf = 1;
190  name = strdup(n);
191  while (name[i]) {
192  if (name[i] == ',') {nbf++; name[i] = '\0';}
193  i++;
194  }
195 
196  buf = (char*) malloc(nbf * 256);
197  buf[0] = '\0';
198  ptr = name;
199  i = 0;
200  while (ptr && nbf > 0) {
201  f = find_best_font(ptr, size);
202  while (*f) {
203  buf[i] = *f;
204  f++; i++;
205  }
206  nbf--;
207  while (*ptr) ptr++;
208  if (nbf) {
209  ptr++;
210  buf[i] = ',';
211  i++;
212  }
213  while(isspace(*ptr)) ptr++;
214  }
215  buf[i] = '\0';
216  free(name);
217  return buf;
218 }
219 
220 
221 char *fl_get_font_xfld(int fnum, int size) {
222  Fl_Fontdesc* s = fl_fonts+fnum;
223  if (!s->name) s = fl_fonts; // use font 0 if still undefined
224  fl_open_display();
225  return put_font_size(s->name, size);
226 }
227 
228 // locate or create an Fl_Font_Descriptor for a given Fl_Fontdesc and size:
229 static Fl_Font_Descriptor* find(int fnum, int size) {
230  char *name;
231  Fl_Fontdesc* s = fl_fonts+fnum;
232  if (!s->name) s = fl_fonts; // use font 0 if still undefined
234  for (f = s->first; f; f = f->next)
235  if (f->size == size) return f;
236  fl_open_display();
237 
238  name = put_font_size(s->name, size);
239  f = new Fl_Font_Descriptor(name);
240  f->size = size;
241  f->next = s->first;
242  s->first = f;
243  free(name);
244  return f;
245 }
246 
247 
249 // Public interface:
250 
251 void *fl_xftfont = 0;
252 static GC font_gc;
253 
254 XFontStruct* Fl_XFont_On_Demand::value() {
255  return ptr;
256 }
257 
259  if (fnum==-1) {
261  return;
262  }
263  if (fnum == Fl_Graphics_Driver::font() && size == Fl_Graphics_Driver::size()) return;
265  Fl_Font_Descriptor* f = find(fnum, size);
266  if (f != this->font_descriptor()) {
267  this->font_descriptor(f);
268  fl_xfont = f->font->fonts[0];
269  font_gc = 0;
270  }
271 }
272 
275  else return -1;
276 }
277 
279  if (font_descriptor()) return font_descriptor()->font->descent;
280  else return -1;
281 }
282 
283 double Fl_Xlib_Graphics_Driver::width(const char* c, int n) {
284  if (font_descriptor()) return (double) XUtf8TextWidth(font_descriptor()->font, c, n);
285  else return -1;
286 }
287 
288 double Fl_Xlib_Graphics_Driver::width(unsigned int c) {
289  if (font_descriptor()) return (double) XUtf8UcsWidth(font_descriptor()->font, c);
290  else return -1;
291 }
292 
293 void Fl_Xlib_Graphics_Driver::text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) {
294  if (font_gc != fl_gc) {
296  font_gc = fl_gc;
297  XSetFont(fl_display, fl_gc, font_descriptor()->font->fid);
298  }
299  int xx, yy, ww, hh;
300  xx = yy = ww = hh = 0;
301  if (fl_gc) XUtf8_measure_extents(fl_display, fl_window, font_descriptor()->font, fl_gc, &xx, &yy, &ww, &hh, c, n);
302 
303  W = ww; H = hh; dx = xx; dy = yy;
304 // This is the safe but mostly wrong thing we used to do...
305 // W = 0; H = 0;
306 // fl_measure(c, W, H, 0);
307 // dx = 0;
308 // dy = fl_descent() - H;
309 }
310 
311 void Fl_Xlib_Graphics_Driver::draw(const char* c, int n, int x, int y) {
312  if (font_gc != fl_gc) {
314  font_gc = fl_gc;
315  XSetFont(fl_display, fl_gc, font_descriptor()->font->fid);
316  }
318 }
319 
320 void Fl_Xlib_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) {
321  static char warning = 0; // issue warning only once
322  if (!warning && angle != 0) {
323  warning = 1;
324  fprintf(stderr,
325  "libfltk: rotated text not implemented by X backend.\n"
326  " You should use the Xft backend. Check USE_XFT in config.h.\n");
327  }
328  this->draw(str, n, (int)x, (int)y);
329 }
330 
331 void Fl_Xlib_Graphics_Driver::rtl_draw(const char* c, int n, int x, int y) {
332  if (font_gc != fl_gc) {
334  font_gc = fl_gc;
335  }
337 }
338 #endif // FL_DOXYGEN
339 //
340 // End of "$Id$".
341 //
XFreeUtf8FontStruct
void XFreeUtf8FontStruct(Display *dpy, XUtf8FontStruct *font_set)
Definition: utf8Wrap.c:1005
find_best_font
static const char * find_best_font(const char *fname, int size)
Definition: fl_font_x.cxx:112
buf
static char * buf
Definition: fl_encoding_mac_roman.cxx:76
Fl_Xlib_Graphics_Driver::rtl_draw
void rtl_draw(const char *str, int n, int x, int y)
see fl_rtl_draw(const char *str, int n, int x, int y).
Definition: fl_font_x.cxx:331
Fl_Fontdesc
Definition: Fl_Font.H:86
XUtf8_measure_extents
void XUtf8_measure_extents(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int *xx, int *yy, int *ww, int *hh, const char *string, int num_bytes)
Definition: utf8Wrap.c:560
put_font_size
static char * put_font_size(const char *n, int size)
Definition: fl_font_x.cxx:182
XUtf8UcsWidth
int XUtf8UcsWidth(XUtf8FontStruct *font_set, unsigned int ucs)
Definition: utf8Wrap.c:879
free
void free()
H
static int H
Definition: Fl_Tooltip.cxx:76
Fl_Graphics_Driver::font
Fl_Font font()
see fl_font(void).
Definition: Fl_Device.H:388
fl_fonts
Fl_Fontdesc * fl_fonts
Definition: fl_font_x.cxx:76
NULL
#define NULL
Definition: forms.H:34
XUtf8DrawString
void XUtf8DrawString(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int x, int y, const char *string, int num_bytes)
Definition: utf8Wrap.c:445
fl_xftfont
void * fl_xftfont
Definition: fl_font_x.cxx:251
built_in_table
static Fl_Fontdesc built_in_table[]
Definition: fl_font_x.cxx:57
Fl_Xlib_Graphics_Driver::descent
int descent()
see fl_descent().
Definition: fl_font_x.cxx:278
Fl_Fontdesc::name
const char * name
Definition: Fl_Font.H:87
Fl_Font_Descriptor::font
XUtf8FontStruct * font
Definition: Fl_Font.H:69
fl_font_word
const char * fl_font_word(const char *p, int n)
Definition: fl_font_x.cxx:81
font_gc
static GC font_gc
Definition: fl_font_x.cxx:252
p
static menustate * p
Definition: Fl_Menu.cxx:606
XUtf8FontStruct::descent
int descent
Definition: Xutf8.h:37
find
static Fl_Font_Descriptor * find(int fnum, int size)
Definition: fl_font_x.cxx:229
fname
static const char * fname
Definition: file.cxx:128
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
fl_xfont
Fl_XFont_On_Demand fl_xfont
Definition: fl_font_x.cxx:32
Fl_Fontsize
int Fl_Fontsize
Definition: Enumerations.H:906
fl_display
FL_EXPORT HINSTANCE fl_display
END TIMERS.
Definition: Fl_win32.cxx:2147
FL_NORMAL_SIZE
Fl_Fontsize FL_NORMAL_SIZE
normal font size
Definition: Fl_Widget.cxx:117
MAXSIZE
#define MAXSIZE
Definition: fl_font_x.cxx:78
fl_graphics_driver
FL_EXPORT Fl_Graphics_Driver * fl_graphics_driver
Points to the driver that currently receives all graphics requests.
Definition: Fl_Device.cxx:50
XUtf8FontStruct::ascent
int ascent
Definition: Xutf8.h:36
Fl_Fontdesc::first
Fl_Font_Descriptor * first
Definition: Fl_Font.H:89
Fl_Xlib_Graphics_Driver::draw
void draw(const char *str, int n, int x, int y)
see fl_draw(const char *str, int n, int x, int y).
Definition: fl_font_x.cxx:311
Fl_Xlib_Graphics_Driver::height
int height()
see fl_height().
Definition: fl_font_x.cxx:273
fl_get_font_xfld
char * fl_get_font_xfld(int fnum, int size)
Definition: fl_font_x.cxx:221
x
int x
Definition: test.c:73
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
Fl_Font_Descriptor::Fl_Font_Descriptor
FL_EXPORT Fl_Font_Descriptor(const char *xfontname)
Definition: fl_font_mac.cxx:36
Fl_Xlib_Graphics_Driver::text_extents
void text_extents(const char *, int n, int &dx, int &dy, int &w, int &h)
see fl_text_extents(const char*, int n, int& dx, int& dy, int& w, int& h).
Definition: fl_font_x.cxx:293
Fl_Graphics_Driver::size
Fl_Fontsize size()
see fl_size().
Definition: Fl_Device.H:390
dy
uchar dy
Definition: fl_boxtype.cxx:286
Fl_Xlib_Graphics_Driver::width
double width(const char *str, int n)
see fl_width(const char *str, int n).
Definition: fl_font_x.cxx:283
fl_gc
CGContextRef fl_gc
Definition: Fl_win32.cxx:2568
y
int y
Definition: test.c:74
XCreateUtf8FontStruct
XUtf8FontStruct * XCreateUtf8FontStruct(Display *dpy, const char *base_font_name_list)
Definition: utf8Wrap.c:295
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
malloc
voidp malloc()
Fl::warning
static void(* warning)(const char *,...)
Definition: Fl.H:498
fl_find_fontsize
char * fl_find_fontsize(char *name)
Definition: fl_font_x.cxx:87
fl_correct_encoding
int fl_correct_encoding(const char *name)
Definition: fl_font_x.cxx:106
XUtf8DrawRtlString
void XUtf8DrawRtlString(Display *display, Drawable d, XUtf8FontStruct *font_set, GC gc, int x, int y, const char *string, int num_bytes)
Definition: utf8Wrap.c:324
Fl_Graphics_Driver::n
int n
Definition: Fl_Device.H:124
fl_encoding
const char * fl_encoding
Definition: fl_font_x.cxx:103
fl_open_display
void fl_open_display()
Definition: Fl_win32.cxx:496
XUtf8TextWidth
int XUtf8TextWidth(XUtf8FontStruct *font_set, const char *string, int num_bytes)
Definition: utf8Wrap.c:705
name
static const char * name
Definition: Fl_arg.cxx:53
fl_window
FL_EXPORT Window fl_window
Definition: Fl_win32.cxx:2571
dx
uchar dx
Definition: fl_boxtype.cxx:286
Fl_Font_Descriptor::~Fl_Font_Descriptor
FL_EXPORT ~Fl_Font_Descriptor()
Definition: fl_font_mac.cxx:155
warning
static void warning(const char *format,...)
Definition: Fl_abort.cxx:57
Fl_Graphics_Driver::font_descriptor
Fl_Font_Descriptor * font_descriptor()
Definition: Fl_Device.H:404
Fl_Font_Descriptor
Definition: Fl_Font.H:41