~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/programs/xditview/font.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * font.c
 
3
 *
 
4
 * map dvi fonts to X fonts
 
5
 */
 
6
/* $XFree86: xc/programs/xditview/font.c,v 1.6 2002/06/19 20:09:19 keithp Exp $ */
 
7
 
 
8
#include <X11/Xos.h>
 
9
#include <X11/IntrinsicP.h>
 
10
#include <X11/StringDefs.h>
 
11
#include <stdio.h>
 
12
#include <stdlib.h>
 
13
#include <ctype.h>
 
14
#include "DviP.h"
 
15
#include "XFontName.h"
 
16
 
 
17
static char *
 
18
savestr (char *s)
 
19
{
 
20
        char    *n;
 
21
 
 
22
        if (!s)
 
23
                return 0;
 
24
        n = XtMalloc (strlen (s) + 1);
 
25
        if (n)
 
26
                strcpy (n, s);
 
27
        return n;
 
28
}
 
29
 
 
30
static DviFontList *
 
31
LookupFontByPosition (DviWidget dw, int position)
 
32
{
 
33
        DviFontList     *f;
 
34
 
 
35
        for (f = dw->dvi.fonts; f; f=f->next)
 
36
                if (f->dvi_number == position)
 
37
                        break;
 
38
        return f;
 
39
}
 
40
 
 
41
static DviFontSizeList *
 
42
LookupFontSizeBySize (DviWidget dw, DviFontList *f, int size)
 
43
{
 
44
    DviFontSizeList *fs, *best = 0;
 
45
    int             bestdist;
 
46
    char            fontNameString[2048];
 
47
    XFontName       fontName;
 
48
    unsigned int    fontNameAttributes;
 
49
    int             dist;
 
50
 
 
51
    if (f->scalable)
 
52
    {
 
53
        for (best = f->sizes; best; best = best->next)
 
54
            if (best->size == size)
 
55
                return best;
 
56
        best = (DviFontSizeList *) XtMalloc (sizeof *best);
 
57
        best->next = f->sizes;
 
58
        best->size = size;
 
59
        XParseFontName (f->x_name, &fontName, &fontNameAttributes);
 
60
        fontNameAttributes &= ~(FontNamePixelSize|FontNameAverageWidth);
 
61
        fontNameAttributes |= FontNameResolutionX;
 
62
        fontNameAttributes |= FontNameResolutionY;
 
63
        fontNameAttributes |= FontNamePointSize;
 
64
        fontName.ResolutionX = dw->dvi.screen_resolution;
 
65
        fontName.ResolutionY = dw->dvi.screen_resolution;
 
66
        fontName.PointSize = size * 10 / dw->dvi.size_scale;
 
67
        XFormatFontName (&fontName, fontNameAttributes, fontNameString);
 
68
        best->x_name = savestr (fontNameString);
 
69
#ifdef USE_XFT
 
70
        /*
 
71
         * Force a match of a core font for adobe-fontspecific
 
72
         * encodings; we dont have a scalable font in
 
73
         * the right encoding
 
74
         */
 
75
        best->core = False;
 
76
        if (!strcmp (fontName.CharSetRegistry, "adobe") &&
 
77
            !strcmp (fontName.CharSetEncoding, "fontspecific"))
 
78
        {
 
79
            best->core = True;
 
80
        }
 
81
#endif
 
82
        best->doesnt_exist = 0;
 
83
        best->font = 0;
 
84
        f->sizes = best;
 
85
    }
 
86
    else
 
87
    {
 
88
        bestdist = 65536;
 
89
        for (fs = f->sizes; fs; fs=fs->next) {
 
90
            dist = size - fs->size;
 
91
            if (dist < 0)
 
92
                dist = -dist * 16;
 
93
            if (dist < bestdist)
 
94
            {
 
95
                best = fs;
 
96
                bestdist = dist;
 
97
            }
 
98
        }
 
99
    }
 
100
    return best;
 
101
}
 
102
 
 
103
static char *
 
104
SkipFontNameElement (char *n)
 
105
{
 
106
        while (*n != '-')
 
107
                if (!*++n)
 
108
                        return 0;
 
109
        return n+1;
 
110
}
 
111
 
 
112
# define SizePosition           8
 
113
# define EncodingPosition       13
 
114
 
 
115
#ifndef USE_XFT
 
116
static int
 
117
ConvertFontNameToSize (char *n)
 
118
{
 
119
        int     i, size;
 
120
 
 
121
        for (i = 0; i < SizePosition; i++) {
 
122
                n = SkipFontNameElement (n);
 
123
                if (!n)
 
124
                        return -1;
 
125
        }
 
126
        size = atoi (n);
 
127
        return size/10;
 
128
}
 
129
#endif
 
130
 
 
131
static char *
 
132
ConvertFontNameToEncoding (char *n)
 
133
{
 
134
        int i;
 
135
        for (i = 0; i < EncodingPosition; i++) {
 
136
                n = SkipFontNameElement (n);
 
137
                if (!n)
 
138
                        return 0;
 
139
        }
 
140
        return n;
 
141
}
 
142
 
 
143
static void
 
144
DisposeFontSizes (DviWidget dw, DviFontSizeList *fs)
 
145
{
 
146
    DviFontSizeList     *next;
 
147
 
 
148
    for (; fs; fs=next) {
 
149
        next = fs->next;
 
150
        if (fs->x_name)
 
151
                XtFree (fs->x_name);
 
152
        if (fs->font)
 
153
        {
 
154
#ifdef USE_XFT
 
155
            XftFontClose (XtDisplay (dw), fs->font);
 
156
#else
 
157
            XUnloadFont (XtDisplay (dw), fs->font->fid);
 
158
            XFree ((char *)fs->font);
 
159
#endif
 
160
        }
 
161
        XtFree ((char *) fs);
 
162
    }
 
163
}
 
164
 
 
165
void
 
166
ResetFonts (DviWidget dw)
 
167
{
 
168
    DviFontList *f;
 
169
    
 
170
    for (f = dw->dvi.fonts; f; f = f->next)
 
171
    {
 
172
        if (f->initialized)
 
173
        {
 
174
            DisposeFontSizes (dw, f->sizes);
 
175
            f->sizes = 0;
 
176
            f->initialized = FALSE;
 
177
            f->scalable = FALSE;
 
178
        }
 
179
    }
 
180
    /* 
 
181
     * force requery of fonts
 
182
     */
 
183
    dw->dvi.font = 0;
 
184
    dw->dvi.font_number = -1;
 
185
    dw->dvi.cache.font = 0;
 
186
    dw->dvi.cache.font_number = -1;
 
187
}
 
188
 
 
189
static DviFontSizeList *
 
190
InstallFontSizes (DviWidget dw, char *x_name, Boolean *scalablep)
 
191
{
 
192
#ifndef USE_XFT
 
193
    char            fontNameString[2048];
 
194
    char            **fonts;
 
195
    int             i, count;
 
196
    int             size;
 
197
    DviFontSizeList *new;
 
198
    XFontName       fontName;
 
199
    unsigned int    fontNameAttributes;
 
200
#endif
 
201
    DviFontSizeList *sizes;
 
202
 
 
203
    sizes = 0;
 
204
#ifdef USE_XFT
 
205
    *scalablep = TRUE;
 
206
#else
 
207
    *scalablep = FALSE;
 
208
    if (!XParseFontName (x_name, &fontName, &fontNameAttributes))
 
209
        return 0;
 
210
    
 
211
    fontNameAttributes &= ~(FontNamePixelSize|FontNamePointSize);
 
212
    fontNameAttributes |= FontNameResolutionX;
 
213
    fontNameAttributes |= FontNameResolutionY;
 
214
    fontName.ResolutionX = dw->dvi.screen_resolution;
 
215
    fontName.ResolutionY = dw->dvi.screen_resolution;
 
216
    XFormatFontName (&fontName, fontNameAttributes, fontNameString);
 
217
    fonts = XListFonts (XtDisplay (dw), fontNameString, 10000000, &count);
 
218
    for (i = 0; i < count; i++) {
 
219
        size = ConvertFontNameToSize (fonts[i]);
 
220
        if (size == 0)
 
221
        {
 
222
            DisposeFontSizes (dw, sizes);
 
223
            *scalablep = TRUE;
 
224
            sizes = 0;
 
225
            break;
 
226
        }
 
227
        if (size != -1) {
 
228
            new = (DviFontSizeList *) XtMalloc (sizeof *new);
 
229
            new->next = sizes;
 
230
            new->size = size;
 
231
            new->x_name = savestr (fonts[i]);
 
232
            new->doesnt_exist = 0;
 
233
            new->font = 0;
 
234
            sizes = new;
 
235
        }
 
236
    }
 
237
    XFreeFontNames (fonts);
 
238
#endif
 
239
    return sizes;
 
240
}
 
241
 
 
242
static DviFontList *
 
243
InstallFont (DviWidget dw, int position, char *dvi_name, char *x_name)
 
244
{
 
245
    DviFontList *f;
 
246
    char                *encoding;
 
247
 
 
248
    f = LookupFontByPosition (dw, position);
 
249
    if (f) {
 
250
        /*
 
251
         * ignore gratuitous font loading
 
252
         */
 
253
        if (!strcmp (f->dvi_name, dvi_name) && !strcmp (f->x_name, x_name))
 
254
            return f;
 
255
 
 
256
        DisposeFontSizes (dw, f->sizes);
 
257
        if (f->dvi_name)
 
258
            XtFree (f->dvi_name);
 
259
        if (f->x_name)
 
260
            XtFree (f->x_name);
 
261
    } else {
 
262
        f = (DviFontList *) XtMalloc (sizeof (*f));
 
263
        f->next = dw->dvi.fonts;
 
264
        dw->dvi.fonts = f;
 
265
    }
 
266
    f->initialized = FALSE;
 
267
    f->dvi_name = savestr (dvi_name);
 
268
    f->x_name = savestr (x_name);
 
269
    f->dvi_number = position;
 
270
    f->sizes = 0;
 
271
    f->scalable = FALSE;
 
272
    if (f->x_name) {
 
273
        encoding = ConvertFontNameToEncoding (f->x_name);
 
274
        f->char_map = DviFindMap (encoding);
 
275
    } else
 
276
        f->char_map = 0;
 
277
    /* 
 
278
     * force requery of fonts
 
279
     */
 
280
    dw->dvi.font = 0;
 
281
    dw->dvi.font_number = -1;
 
282
    dw->dvi.cache.font = 0;
 
283
    dw->dvi.cache.font_number = -1;
 
284
    return f;
 
285
}
 
286
 
 
287
static char *
 
288
MapDviNameToXName (DviWidget dw, char *dvi_name)
 
289
{
 
290
    DviFontMap  *fm;
 
291
    
 
292
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
 
293
        if (!strcmp (fm->dvi_name, dvi_name))
 
294
            return fm->x_name;
 
295
    ++dvi_name;
 
296
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
 
297
        if (!strcmp (fm->dvi_name, "R"))
 
298
            return fm->x_name;
 
299
    if (dw->dvi.font_map->x_name)
 
300
        return dw->dvi.font_map->x_name;
 
301
    return "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1";
 
302
}
 
303
 
 
304
#ifdef NOTUSED
 
305
static char *
 
306
MapXNameToDviName (dw, x_name)
 
307
        DviWidget       dw;
 
308
        char            *x_name;
 
309
{
 
310
    DviFontMap  *fm;
 
311
    
 
312
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
 
313
        if (!strcmp (fm->x_name, x_name))
 
314
            return fm->dvi_name;
 
315
    return 0;
 
316
}
 
317
#endif
 
318
 
 
319
void
 
320
ParseFontMap (dw)
 
321
        DviWidget       dw;
 
322
{
 
323
    char                dvi_name[1024];
 
324
    char                x_name[2048];
 
325
    char                *m, *s;
 
326
    DviFontMap  *fm, *new;
 
327
 
 
328
    if (dw->dvi.font_map)
 
329
            DestroyFontMap (dw->dvi.font_map);
 
330
    fm = 0;
 
331
    m = dw->dvi.font_map_string;
 
332
    while (*m) {
 
333
        s = m;
 
334
        while (*m && !isspace (*m))
 
335
            ++m;
 
336
        strncpy (dvi_name, s, m-s);
 
337
        dvi_name[m-s] = '\0';
 
338
        while (isspace (*m))
 
339
            ++m;
 
340
        s = m;
 
341
        while (*m && *m != '\n')
 
342
            ++m;
 
343
        strncpy (x_name, s, m-s);
 
344
        x_name[m-s] = '\0';
 
345
        new = (DviFontMap *) XtMalloc (sizeof *new);
 
346
        new->x_name = savestr (x_name);
 
347
        new->dvi_name = savestr (dvi_name);
 
348
        new->next = fm;
 
349
        fm = new;
 
350
        ++m;
 
351
    }
 
352
    dw->dvi.font_map = fm;
 
353
}
 
354
 
 
355
void
 
356
DestroyFontMap (font_map)
 
357
    DviFontMap  *font_map;
 
358
{
 
359
    DviFontMap  *next;
 
360
 
 
361
    for (; font_map; font_map = next) {
 
362
        next = font_map->next;
 
363
        if (font_map->x_name)
 
364
            XtFree (font_map->x_name);
 
365
        if (font_map->dvi_name)
 
366
            XtFree (font_map->dvi_name);
 
367
        XtFree ((char *) font_map);
 
368
    }
 
369
}
 
370
 
 
371
/*ARGSUSED*/
 
372
void
 
373
SetFontPosition (dw, position, dvi_name, extra)
 
374
    DviWidget   dw;
 
375
    int         position;
 
376
    char        *dvi_name;
 
377
    char        *extra; /* unused */
 
378
{
 
379
    char        *x_name;
 
380
 
 
381
    x_name = MapDviNameToXName (dw, dvi_name);
 
382
    (void) InstallFont (dw, position, dvi_name, x_name);
 
383
}
 
384
 
 
385
#ifdef USE_XFT
 
386
XftFont *
 
387
#else
 
388
XFontStruct *
 
389
#endif
 
390
QueryFont (dw, position, size)
 
391
    DviWidget   dw;
 
392
    int         position;
 
393
    int         size;
 
394
{
 
395
    DviFontList *f;
 
396
    DviFontSizeList     *fs;
 
397
 
 
398
    f = LookupFontByPosition (dw, position);
 
399
    if (!f)
 
400
        return dw->dvi.default_font;
 
401
    if (!f->initialized) {
 
402
        f->sizes = InstallFontSizes (dw, f->x_name, &f->scalable);
 
403
        f->initialized = TRUE;
 
404
    }
 
405
    fs = LookupFontSizeBySize (dw, f, size);
 
406
    if (!fs)
 
407
        return dw->dvi.default_font;
 
408
    if (!fs->font) {
 
409
        if (fs->x_name)
 
410
        {
 
411
#ifdef USE_XFT
 
412
            XftPattern  *pat;
 
413
            XftPattern  *match;
 
414
            XftResult   result;
 
415
 
 
416
            pat = XftXlfdParse (fs->x_name, False, False);
 
417
            XftPatternAddBool (pat, XFT_CORE, fs->core);
 
418
            match = XftFontMatch (XtDisplay (dw),
 
419
                                  XScreenNumberOfScreen(dw->core.screen),
 
420
                                  pat, &result);
 
421
            XftPatternDestroy (pat);
 
422
            if (match)
 
423
            {
 
424
                fs->font = XftFontOpenPattern (XtDisplay (dw),
 
425
                                               match);
 
426
                if (!fs->font)
 
427
                    XftPatternDestroy (match);
 
428
            }
 
429
            else
 
430
                fs->font = 0;
 
431
#else
 
432
            fs->font = XLoadQueryFont (XtDisplay (dw), fs->x_name);
 
433
#endif
 
434
        }
 
435
        if (!fs->font)
 
436
            fs->font = dw->dvi.default_font;
 
437
    }
 
438
    return fs->font;
 
439
}
 
440
 
 
441
DviCharNameMap *
 
442
QueryFontMap (dw, position)
 
443
        DviWidget       dw;
 
444
        int             position;
 
445
{
 
446
        DviFontList     *f;
 
447
 
 
448
        f = LookupFontByPosition (dw, position);
 
449
        if (f)
 
450
            return f->char_map;
 
451
        else
 
452
            return 0;
 
453
}
 
454
 
 
455
unsigned char *
 
456
DviCharIsLigature (map, name)
 
457
    DviCharNameMap  *map;
 
458
    char            *name;
 
459
{
 
460
    int     i;
 
461
 
 
462
    for (i = 0; i < DVI_MAX_LIGATURES; i++) {
 
463
        if (!map->ligatures[i][0])
 
464
            break;
 
465
        if (!strcmp (name, map->ligatures[i][0]))
 
466
            return (unsigned char *) map->ligatures[i][1];
 
467
    }
 
468
    return 0;
 
469
}
 
470
 
 
471
#if 0
 
472
LoadFont (dw, position, size)
 
473
        DviWidget       dw;
 
474
        int             position;
 
475
        int             size;
 
476
{
 
477
        XFontStruct     *font;
 
478
 
 
479
        font = QueryFont (dw, position, size);
 
480
        dw->dvi.font_number = position;
 
481
        dw->dvi.font_size = size;
 
482
        dw->dvi.font = font;
 
483
        XSetFont (XtDisplay (dw), dw->dvi.normal_GC, font->fid);
 
484
        return;
 
485
}
 
486
#endif