FLTK 1.3.8
fl_set_fonts_x.cxx
1//
2// "$Id$"
3//
4// X11 font utilities for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2010 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
19// This function fills in the fltk font table with all the fonts that
20// are found on the X server. It tries to place the fonts into families
21// and to sort them so the first 4 in a family are normal, bold, italic,
22// and bold italic.
23
24// Standard X fonts are matched by a pattern that is always of
25// this form, and this pattern is put in the table:
26// "-*-family-weight-slant-width1-style-*-registry-encoding"
27
28// Non-standard font names (those not starting with '-') are matched
29// by a pattern of the form "prefix*suffix", where the '*' is where
30// fltk thinks the point size is, or by the actual font name if no
31// point size is found.
32
33// Fltk knows how to pull an "attribute" out of a font name, such as
34// bold or italic, by matching known x font field values. All words
35// that don't match a known attribute are combined into the "name"
36// of the font. Names are compared before attributes for sorting, this
37// makes the bold and plain version of a font come out next to each
38// other despite the poor X font naming scheme.
39
40// By default fl_set_fonts() only does iso8859-1 encoded fonts. You can
41// do all normal X fonts by passing "-*" or every possible font with "*".
42
43// Fl::set_font will take strings other than the ones this stores
44// and can identify any font on X that way. You may want to write your
45// own system of font management and not use this code.
46
47// turn word N of a X font name into either some attribute bits
48// (right now 0, FL_BOLD, or FL_ITALIC), or into -1 indicating that
49// the word should be put into the name:
50
51static int attribute(int n, const char *p) {
52 // don't put blank things into name:
53 if (!*p || *p=='-' || *p=='*') return 0;
54 if (n == 3) { // weight
55 if (!strncmp(p,"normal",6) ||
56 !strncmp(p,"light",5) ||
57 !strncmp(p,"medium",6) ||
58 !strncmp(p,"book",4)) return 0;
59 if (!strncmp(p,"bold",4) || !strncmp(p,"demi",4)) return FL_BOLD;
60 } else if (n == 4) { // slant
61 if (*p == 'r') return 0;
62 if (*p == 'i' || *p == 'o') return FL_ITALIC;
63 } else if (n == 5) { // sWidth
64 if (!strncmp(p,"normal",6)) return 0;
65 }
66 return -1;
67}
68
69// return non-zero if the registry-encoding should be used:
70extern const char* fl_encoding;
71static int use_registry(const char *p) {
72 return *p && *p!='*' && strcmp(p,fl_encoding);
73}
74
75// Bug: older versions calculated the value for *ap as a side effect of
76// making the name, and then forgot about it. To avoid having to change
77// the header files I decided to store this value in the last character
78// of the font name array.
79#define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
80
81// turn a stored (with *'s) X font name into a pretty name:
82const char* Fl::get_font_name(Fl_Font fnum, int* ap) {
83 Fl_Fontdesc *f = fl_fonts + fnum;
84 if (!f->fontname[0]) {
85 int type = 0;
86 const char* p = f->name;
87 if (!p) {
88 if (ap) *ap = 0;
89 return "";
90 }
91 char *o = f->fontname;
92
93 if (*p != '-') { // non-standard font, just replace * with spaces:
94 if (strstr(p,"bold")) type = FL_BOLD;
95 if (strstr(p,"ital")) type |= FL_ITALIC;
96 for (;*p; p++) {
97 if (*p == '*' || *p == ' ' || *p == '-') {
98 do p++; while (*p == '*' || *p == ' ' || *p == '-');
99 if (!*p) break;
100 if (o < (f->fontname + ENDOFBUFFER - 1)) *o++ = ' ';
101 }
102 if (o < (f->fontname + ENDOFBUFFER - 1)) *o++ = *p;
103 }
104 *o = 0;
105
106 } else { // standard dash-separated font:
107
108 // get the family:
109 const char *x = fl_font_word(p,2); if (*x) x++; if (*x=='*') x++;
110 if (!*x) {
111 if (ap) *ap = 0;
112 return p;
113 }
114 const char *e = fl_font_word(x,1);
115 if ((e - x) < (int)(ENDOFBUFFER - 1)) {
116 // MRS: we want strncpy here, not strlcpy...
117 strncpy(o,x,e-x);
118 o += e-x;
119 } else {
120 strlcpy(f->fontname, x, ENDOFBUFFER);
121 o = f->fontname+ENDOFBUFFER-1;
122 }
123
124 // collect all the attribute words:
125 for (int n = 3; n <= 6; n++) {
126 // get the next word:
127 if (*e) e++; x = e; e = fl_font_word(x,1);
128 int t = attribute(n,x);
129 if (t < 0) {
130 if (o < (f->fontname + ENDOFBUFFER - 1)) *o++ = ' ';
131 if ((e - x) < (int)(ENDOFBUFFER - (o - f->fontname) - 1)) {
132 // MRS: we want strncpy here, not strlcpy...
133 strncpy(o,x,e-x);
134 o += e-x;
135 } else {
136 strlcpy(o,x, ENDOFBUFFER - (o - f->fontname) - 1);
137 o = f->fontname+ENDOFBUFFER-1;
138 }
139 } else type |= t;
140 }
141
142 // skip over the '*' for the size and get the registry-encoding:
143 x = fl_font_word(e,2);
144 if (*x) {x++; *o++ = '('; while (*x) *o++ = *x++; *o++ = ')';}
145
146 *o = 0;
147 if (type & FL_BOLD) strlcat(f->fontname, " bold", ENDOFBUFFER);
148 if (type & FL_ITALIC) strlcat(f->fontname, " italic", ENDOFBUFFER);
149 }
150 f->fontname[ENDOFBUFFER] = (char)type;
151 }
152 if (ap) *ap = f->fontname[ENDOFBUFFER];
153 return f->fontname;
154}
155
156extern "C" {
157// sort raw (non-'*') X font names into perfect order:
158
159static int ultrasort(const void *aa, const void *bb) {
160 const char *a = *(char **)aa;
161 const char *b = *(char **)bb;
162
163 // sort all non x-fonts at the end:
164 if (*a != '-') {
165 if (*b == '-') return 1;
166 // 2 non-x fonts are matched by "numeric sort"
167 int ret = 0;
168 for (;;) {
169 if (isdigit(*a) && isdigit(*b)) {
170 int na = strtol(a, (char **)&a, 10);
171 int nb = strtol(b, (char **)&b, 10);
172 if (!ret) ret = na-nb;
173 } else if (*a != *b) {
174 return (*a-*b);
175 } else if (!*a) {
176 return ret;
177 } else {
178 a++; b++;
179 }
180 }
181 } else {
182 if (*b != '-') return -1;
183 }
184
185 // skip the foundry (assume equal):
186 for (a++; *a && *a++!='-';);
187 for (b++; *b && *b++!='-';);
188
189 // compare the family and all the attribute words:
190 int atype = 0;
191 int btype = 0;
192 for (int n = 2; n <= 6; n++) {
193 int at = attribute(n,a);
194 int bt = attribute(n,b);
195 if (at < 0) {
196 if (bt >= 0) return 1;
197 for (;;) {if (*a!=*b) return *a-*b; b++; if (!*a || *a++=='-') break;}
198 } else {
199 if (bt < 0) return -1;
200 a = fl_font_word(a,1); if (*a) a++;
201 b = fl_font_word(b,1); if (*b) b++;
202 atype |= at; btype |= bt;
203 }
204 }
205
206 // remember the pixel size:
207 int asize = atoi(a);
208 int bsize = atoi(b);
209
210 // compare the registry/encoding:
211 a = fl_font_word(a,6); if (*a) a++;
212 b = fl_font_word(b,6); if (*b) b++;
213 if (use_registry(a)) {
214 if (!use_registry(b)) return 1;
215 int r = strcmp(a,b); if (r) return r;
216 } else {
217 if (use_registry(b)) return -1;
218 }
219
220 if (atype != btype) return atype-btype;
221 if (asize != bsize) return asize-bsize;
222
223 // something wrong, just do a string compare...
224 return strcmp(*(char**)aa, *(char**)bb);
225}
226}
227
228// converts a X font name to a standard starname, returns point size:
229static int to_canonical(char *to, const char *from, size_t tolen) {
230 char* c = fl_find_fontsize((char*)from);
231 if (!c) return -1; // no point size found...
232 const char* endptr;
233 int size = strtol(c,(char**)&endptr,10);
234 if (from[0] == '-') {
235 // replace the "foundry" with -*-:
236 *to++ = '-'; *to++ = '*';
237 for (from++; *from && *from != '-'; from++);
238 // skip to the registry-encoding:
239 endptr = (char*)fl_font_word(endptr,6);
240 if (*endptr && !use_registry(endptr+1)) endptr = "";
241 }
242 int n = c-from;
243 // MRS: we want strncpy here, not strlcpy...
244 if (n > (int)(tolen - 1)) return -1;
245 strncpy(to,from,n);
246 to[n++] = '*';
247 strlcpy(to+n,endptr, tolen - n);
248 return size;
249}
250
251static unsigned int fl_free_font = FL_FREE_FONT;
252
253Fl_Font Fl::set_fonts(const char* xstarname) {
254 if (fl_free_font > (unsigned)FL_FREE_FONT) // already been here
255 return (Fl_Font)fl_free_font;
256 fl_open_display();
257 int xlistsize;
258 char buf[20];
259 if (!xstarname) {
260 strcpy(buf,"-*-"); strcpy(buf+3,fl_encoding);
261 xstarname = buf;
262 }
263 char **xlist = XListFonts(fl_display, xstarname, 10000, &xlistsize);
264 if (!xlist) return (Fl_Font)fl_free_font;
265 qsort(xlist, xlistsize, sizeof(*xlist), ultrasort);
266 int used_xlist = 0;
267 for (int i=0; i<xlistsize;) {
268 int first_xlist = i;
269 const char *p = xlist[i++];
270 char canon[1024];
271 int size = to_canonical(canon, p, sizeof(canon));
272 if (size >= 0) {
273 for (;;) { // find all matching fonts:
274 if (i >= xlistsize) break;
275 const char *q = xlist[i];
276 char this_canon[1024];
277 if (to_canonical(this_canon, q, sizeof(this_canon)) < 0) break;
278 if (strcmp(canon, this_canon)) break;
279 i++;
280 }
281 /*if (*p=='-' || i > first_xlist+1)*/ p = canon;
282 }
283 unsigned int j;
284 for (j = 0;; j++) {
285 /*if (j < FL_FREE_FONT) {
286 // see if it is one of our built-in fonts:
287 // if so, set the list of x fonts, since we have it anyway
288 if (fl_fonts[j].name && !strcmp(fl_fonts[j].name, p)) break;
289 } else */{
290 j = fl_free_font++;
291 if (p == canon) p = strdup(p); else used_xlist = 1;
292 Fl::set_font((Fl_Font)j, p);
293 break;
294 }
295 }
296 if (!fl_fonts[j].xlist) {
297 fl_fonts[j].xlist = xlist+first_xlist;
298 fl_fonts[j].n = -(i-first_xlist);
299 used_xlist = 1;
300 }
301 }
302 if (!used_xlist) XFreeFontNames(xlist);
303 return (Fl_Font)fl_free_font;
304}
305
306int Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
307 Fl_Fontdesc *s = fl_fonts+fnum;
308 if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
309 if (!s->xlist) {
310 fl_open_display();
311 s->xlist = XListFonts(fl_display, s->name, 100, &(s->n));
312 if (!s->xlist) return 0;
313 }
314 int listsize = s->n; if (listsize<0) listsize = -listsize;
315 static int sizes[128];
316 int numsizes = 0;
317 for (int i = 0; i < listsize; i++) {
318 char *q = s->xlist[i];
319 char *d = fl_find_fontsize(q);
320 if (!d) continue;
321 int s = strtol(d,0,10);
322 if (!numsizes || sizes[numsizes-1] < s) {
323 sizes[numsizes++] = s;
324 } else {
325 // insert-sort the new size into list:
326 int n;
327 for (n = numsizes-1; n > 0; n--) if (sizes[n-1] < s) break;
328 if (sizes[n] != s) {
329 for (int m = numsizes; m > n; m--) sizes[m] = sizes[m-1];
330 sizes[n] = s;
331 numsizes++;
332 }
333 }
334 }
335 sizep = sizes;
336 return numsizes;
337}
338
339//
340// End of "$Id$".
341//
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:875
const Fl_Font FL_BOLD
add this to helvetica, courier, or times
Definition Enumerations.H:895
const Fl_Font FL_ITALIC
add this to helvetica, courier, or times
Definition Enumerations.H:896
const Fl_Font FL_FREE_FONT
first one to allocate
Definition Enumerations.H:894
static void set_font(Fl_Font, const char *)
Changes a face.
Definition fl_set_font.cxx:34
static int get_font_sizes(Fl_Font, int *&sizep)
Return an array of sizes in sizep.
Definition fl_set_fonts_mac.cxx:183
static const char * get_font_name(Fl_Font, int *attributes=0)
Get a human-readable string describing the family of this face.
Definition fl_set_fonts_mac.cxx:31
static Fl_Font set_fonts(const char *=0)
FLTK will open the display, and add every fonts on the server to the face table.
Definition fl_set_fonts_mac.cxx:103
static int x()
Returns the leftmost x coordinate of the main screen work area.
Definition Fl_Font.H:86