File: FontListCvt.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (371 lines) | stat: -rw-r--r-- 11,922 bytes parent folder | download | duplicates (3)
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
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.2  2002/08/22 15:06:10  andre
 * AA-2002-08-22-0  I18N:      bunch of fixes for l10n
 *                  Bugtraq:   #4733802, #4733201, #4733089, #4733043,
 *                             #4731976, #4731990, #4731967, #4731958,
 *                             #4731944, #4731935, #4731273, #4729700
 *
 * Revision 1.1.1.1  2001/07/18 11:06:02  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <Xmt/Xmt.h>
#include <Xmt/ConvertersP.h>
#include <Xmt/LookupP.h>

/* ARGSUSED */
#if NeedFunctionPrototypes
Boolean XmtConvertStringToXmFontList(Display *dpy,
                                     XrmValue *args, Cardinal *num_args,
                                     XrmValue *from, XrmValue *to,
                                     XtPointer *converter_data)
#else
Boolean XmtConvertStringToXmFontList(dpy, args, num_args,
                                     from, to, converter_data)
Display *dpy;
XrmValue *args;
Cardinal *num_args;
XrmValue *from;
XrmValue *to;
XtPointer *converter_data;
#endif
{
    Screen *screen = *(Screen **)args[0].addr;
    String copy = XtNewString((String) from->addr);
    String s, fs, bs;
    String next_entry;
    String tag;
    String tmp;
    XmFontType font_type = XmFONT_IS_FONT;
    XmFontListEntry entry;
    XmFontList fontlist = NULL;
    int j;
    static Boolean fontdebug = False;
    
    if (getenv("XMTDEBUGFONT"))
       fontdebug = True;

    /*
     * This XmFontList converter does not handle XFontSets defined in
     * X11R5 and Motif 1.2.  This is to make it easier to write, and
     * for portablity to R4 and 1.1.  Also, this converter uses the 1.1
     * XmFontList API.  Anyone using multi-charset Asian languages will
     * not want to use this converter.
     * This converter also does not support the (undocumented?) features
     * of the Motif converter that allows quotes in font names.
     * The main reason to use the converter is because it handles
     * font list indirection.  And because it doesn't have the bug
     * that 1.2.3 does...
     *
     * The syntax is:
     *    font-name [ `=' tag ] { `,' font-name [ `=' tag ] }
     * 
     * and we extend this also to allow:
     *    `$' symbolic-font-list-name
     * A symbolic name like this will be looked up under _Fonts_,
     * depending on display resolution, and other factors.
     */
    
    s = copy;

    /* handle a symbolic font list name by recursing */
    if (s[0] == '$') {
        String value;
        XrmValue new_from;

        /* get symbolic name */
        s++;
        while(isspace(*s)) s++;
        
        /*
         * lookup value of that symbolic name under:
         *   _FontLists_.fontFamily.language.territory.codeset
         * Note that we don't have to free the return value.
         */
        value = _XmtLookupResource(screen, "Ffltc", s);
        
        /* if no value defined, warn and fail */
        if (!value) {
            XmtWarningMsg("XmtConvertStringToFontList", "nosymbol",
                          "No font list with name '%s' defined in resource file under _FontLists_",
                          s);
            goto fail;
        }

        /*
         * Otherwise, recurse to convert the definition we've found 
         * The programmer must be smart enough to avoid infinite recursion
         */
        XtFree(copy);
        new_from.addr = (XPointer) value;
        new_from.size = strlen(value) + 1;
        return XmtConvertStringToXmFontList(dpy, args, num_args,
                                            &new_from, to, converter_data);
    }
    
    if (fontdebug) {
       printf("FontListCvt:\n->%s<-\n------------------------\n", 
                  s ? s : "empty" );    
    }
       

    /* Otherwise, it is not a symbolic font name */
    while(s && *s) {
        /* skip white-space */
        while(isspace(*s)) s++;
        if (!*s) break;

        /* isolate the first entry, and remember start of next entry */
        next_entry = strchr(s, ',');
        if (next_entry) {
            *next_entry = '\0';
            next_entry++;
        }

        bs = s;
        j = 0;
#if 0
        while (bs && *bs) {
           bs_next = strchr(bs, ';');
           if (bs_next) {
               *bs_next = '\0';
               bs_next++;
           }
#endif
           if (fontdebug) {       
              printf("baselist[%d]: '%s'\n", j, bs);
           }  
           /*
            * see if it is a fontset
            */
           fs = strchr(bs, ':');
           if (fs)
               font_type = XmFONT_IS_FONTSET;
           else     
               font_type = XmFONT_IS_FONT;
               
           if (fontdebug) {       
              printf("font_type: '%s'\n", (font_type==XmFONT_IS_FONT) ? 
                           "XmFONT_IS_FONT": "XmFONT_IS_FONTSET");
           }  
           /*
            * find the start of the font tag, if any
            * and isolate the font name from it.
            * remove whitespace from beginning and end of tag
            */
           if (font_type == XmFONT_IS_FONT) 
               tag = strchr(bs, '=');
           else
               tag = strchr(bs, ':');

           if (tag) {
               *tag = '\0';
               tag++;
               while(isspace(*tag)) tag++;
               for(tmp=tag; *tmp && !isspace(*tmp); tmp++)  ;
               *tmp = '\0';
           }
           else {
   #if (XmVersion < 1002)
               tag = XmSTRING_DEFAULT_CHARSET;
   #else
               tag = XmFONTLIST_DEFAULT_TAG;
   #endif            
           }

           /* remove any whitespace from the end of the font name */
           tmp = bs + strlen(bs) - 1;
           while (isspace(*tmp)) tmp--;
           *(++tmp) = '\0';

#if 0
           /*
           ** fontset support it is a bit tricky, but needed for multibyte chars
           ** FIXME: figure out how to do it
           */
           if ((font_type == XmFONT_IS_FONTSET))  {
              XFontSet fontset;
              char **missing_list;
              int missing_count;
              int i;
              char *def_string = NULL; 
              if (fontdebug)
                 printf("FontSet: %s\n--------------\n", bs);

              fontset = XCreateFontSet(dpy, bs, &missing_list, &missing_count,
                                              &def_string);
              for (i=0; i<missing_count; i++) {
                  printf("missing_list[%d]: '%s'\n", i, missing_list[i]);
/*                        XtFree(missing_list[i]); */
              }
/*                    XtFree((char *)missing_list); */
           }
#if 1           
           else {
              XFontStruct *font;
              font = XLoadQueryFont(dpy, s);
              if (!font) {
                 XmtWarningMsg("XmtConvertStringToFontList", "badfont",
                          "unknown font '%s'.\n\tUsing default.",
                          s);
                 /* this default is from the X11R5 font converter */
                 font = XLoadQueryFont(dpy, "-*-*-*-R-*-*-*-120-*-*-*-*-ISO8859-1");

                 /* if still no font, then we fail */
                 if (!font) {
                    XmtWarningMsg("XmtConvertStringToFontList", "nodefault",
                              "cannot load any default font.");
                    goto fail;
                 }
              }     
           }
#endif

#endif
           if (fontdebug) {
               printf("bs: '%s (%s)'\n", bs, tag);
           }    
           entry = XmFontListEntryLoad(dpy, bs, font_type, tag ? tag : XmFONTLIST_DEFAULT_TAG);
           fontlist = XmFontListAppendEntry(fontlist, entry);
           XmFontListEntryFree(&entry);
#if 0
         bs = bs_next;
         j++;
      }
#endif

#if 0
        font = XLoadQueryFont(dpy, s);
        if (!font) {
            XmtWarningMsg("XmtConvertStringToFontList", "badfont",
                          "unknown font '%s'.\n\tUsing default.",
                          s);
            /* this default is from the X11R5 font converter */
            font = XLoadQueryFont(dpy, "-*-*-*-R-*-*-*-120-*-*-*-*-ISO8859-1");

            /* if still no font, then we fail */
            if (!font) {
                XmtWarningMsg("XmtConvertStringToFontList", "nodefault",
                              "cannot load any default font.");
                goto fail;
            }
        }

        /* and use this font and tag to create or append to the font list */
        if (!fontlist)
            fontlist = XmFontListCreate(font, tag);
        else 
            fontlist = XmFontListAdd(fontlist, font, tag);

#endif

        /* finally, move on to the next entry, and start the loop over */
        s = next_entry;
    }


    if (fontlist) {
        /* FIXME */
        if (fontdebug) {
                XmFontContext context;
                XmFontListEntry entry;
                XtPointer f;
                XmFontType type;
                char *tag;
                XmFontListInitFontContext(&context, fontlist);
                do {
                        entry = XmFontListNextEntry(context);
                        f = XmFontListEntryGetFont(entry, &type);
                        tag = XmFontListEntryGetTag(entry);
                        if (type == XmFONT_IS_FONT) {
                                printf("XmFONT_IS_FONT: %s\n", tag ? tag : "no tag");
                        } else {        
                                printf("XmFONT_IS_FONTSET: %s\n", tag ? tag : "no tag");
                                printf("BaseFonts: %s\n", f ? XBaseFontNameListOfFontSet((XFontSet)f): "--none--");
                        }        
                        XtFree(tag);
                                
                }while (entry);        
                        
                XmFontListFreeFontContext(context);
        }
        
        /* FIXME */
        XtFree(copy);
        done(XmFontList, fontlist);  /* this macro returns */
    }

 fail:
    XtDisplayStringConversionWarning(dpy, copy, XmRFontList);
    XtFree(copy);
    return False;
}


/* ARGSUSED */
#if NeedFunctionPrototypes
static void FreeConvertedXmFontList(XtAppContext app, XrmValue *to,
                                    XtPointer closure,
                                    XrmValue *args, Cardinal *num_args)
#else
static void FreeConvertedXmFontList(app, to, closure, args, num_args)
XtAppContext app;
XrmValue *to;
XtPointer closure;
XrmValue *args;
Cardinal *num_args;
#endif
{
    XmFontList fontlist = *((XmFontList *) to->addr);
    Screen *screen = *(Screen **)args[0].addr;
    Display *dpy = DisplayOfScreen(screen);
    XmFontContext context;
    XFontStruct *font;
    XmStringCharSet tag;
    
    if (XmFontListInitFontContext(&context, fontlist)) {
        while(XmFontListGetNextFont(context, &tag, &font))
            XFreeFont(dpy, font);
        XmFontListFreeFontContext(context);
    }
    XmFontListFree(fontlist);
}

#if NeedFunctionPrototypes
void XmtRegisterXmFontListConverter(void)
#else
void XmtRegisterXmFontListConverter()
#endif
{
    static Boolean registered = False;

    if (!registered) {
        XtSetTypeConverter(XtRString, XmRFontList,
                           XmtConvertStringToXmFontList,
                           (XtConvertArgRec *)screenConvertArg, (Cardinal) 1,
                           XtCacheByDisplay,
                           FreeConvertedXmFontList);
        registered = True;
    }
}