summaryrefslogtreecommitdiff
path: root/font.c
blob: 210a374844bf5dca749b49d8fde76891bb0a4e52 (plain)
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
/*
 * font.c
 *
 * map dvi fonts to X fonts
 */
/* $XFree86: xc/programs/xditview/font.c,v 1.5 2001/08/27 23:35:12 dawes Exp $ */

#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "DviP.h"
#include "XFontName.h"

static char *
savestr (char *s)
{
	char	*n;

	if (!s)
		return 0;
	n = XtMalloc (strlen (s) + 1);
	if (n)
		strcpy (n, s);
	return n;
}

static DviFontList *
LookupFontByPosition (DviWidget dw, int position)
{
	DviFontList	*f;

	for (f = dw->dvi.fonts; f; f=f->next)
		if (f->dvi_number == position)
			break;
	return f;
}

static DviFontSizeList *
LookupFontSizeBySize (DviWidget dw, DviFontList *f, int size)
{
    DviFontSizeList *fs, *best = 0;
    int		    bestdist;
    char	    fontNameString[2048];
    XFontName	    fontName;
    unsigned int    fontNameAttributes;
    int		    dist;

    if (f->scalable)
    {
	for (best = f->sizes; best; best = best->next)
	    if (best->size == size)
		return best;
	best = (DviFontSizeList *) XtMalloc (sizeof *best);
	best->next = f->sizes;
	best->size = size;
	XParseFontName (f->x_name, &fontName, &fontNameAttributes);
	fontNameAttributes &= ~(FontNamePixelSize|FontNameAverageWidth);
	fontNameAttributes |= FontNameResolutionX;
	fontNameAttributes |= FontNameResolutionY;
	fontNameAttributes |= FontNamePointSize;
	fontName.ResolutionX = dw->dvi.screen_resolution;
	fontName.ResolutionY = dw->dvi.screen_resolution;
	fontName.PointSize = size * 10 / dw->dvi.size_scale;
	XFormatFontName (&fontName, fontNameAttributes, fontNameString);
	best->x_name = savestr (fontNameString);
#ifdef USE_XFT
	/*
	 * Force a match of a core font for adobe-fontspecific
	 * encodings; we dont have a scalable font in
	 * the right encoding
	 */
	best->core = False;
	if (!strcmp (fontName.CharSetRegistry, "adobe") &&
	    !strcmp (fontName.CharSetEncoding, "fontspecific"))
	{
	    best->core = True;
	}
#endif
	best->doesnt_exist = 0;
	best->font = 0;
	f->sizes = best;
    }
    else
    {
	bestdist = 65536;
    	for (fs = f->sizes; fs; fs=fs->next) {
	    dist = size - fs->size;
	    if (dist < 0)
		dist = -dist * 16;
	    if (dist < bestdist)
	    {
		best = fs;
		bestdist = dist;
	    }
    	}
    }
    return best;
}

static char *
SkipFontNameElement (char *n)
{
	while (*n != '-')
		if (!*++n)
			return 0;
	return n+1;
}

# define SizePosition		8
# define EncodingPosition	13

#ifndef USE_XFT
static int
ConvertFontNameToSize (char *n)
{
	int	i, size;

	for (i = 0; i < SizePosition; i++) {
		n = SkipFontNameElement (n);
		if (!n)
			return -1;
	}
	size = atoi (n);
	return size/10;
}
#endif

static char *
ConvertFontNameToEncoding (char *n)
{
        int i;
	for (i = 0; i < EncodingPosition; i++) {
		n = SkipFontNameElement (n);
		if (!n)
			return 0;
	}
	return n;
}

static void
DisposeFontSizes (DviWidget dw, DviFontSizeList *fs)
{
    DviFontSizeList	*next;

    for (; fs; fs=next) {
	next = fs->next;
	if (fs->x_name)
		XtFree (fs->x_name);
	if (fs->font)
	{
#ifdef USE_XFT
	    XftFontClose (XtDisplay (dw), fs->font);
#else
	    XUnloadFont (XtDisplay (dw), fs->font->fid);
	    XFree ((char *)fs->font);
#endif
	}
	XtFree ((char *) fs);
    }
}

void
ResetFonts (DviWidget dw)
{
    DviFontList	*f;
    
    for (f = dw->dvi.fonts; f; f = f->next)
    {
	if (f->initialized)
	{
	    DisposeFontSizes (dw, f->sizes);
	    f->sizes = 0;
	    f->initialized = FALSE;
	    f->scalable = FALSE;
	}
    }
    /* 
     * force requery of fonts
     */
    dw->dvi.font = 0;
    dw->dvi.font_number = -1;
    dw->dvi.cache.font = 0;
    dw->dvi.cache.font_number = -1;
}

static DviFontSizeList *
InstallFontSizes (DviWidget dw, char *x_name, Boolean *scalablep)
{
#ifndef USE_XFT
    char	    fontNameString[2048];
    char	    **fonts;
    int		    i, count;
    int		    size;
    DviFontSizeList *new;
    XFontName	    fontName;
    unsigned int    fontNameAttributes;
#endif
    DviFontSizeList *sizes;

    sizes = 0;
#ifdef USE_XFT
    *scalablep = TRUE;
#else
    *scalablep = FALSE;
    if (!XParseFontName (x_name, &fontName, &fontNameAttributes))
	return 0;
    
    fontNameAttributes &= ~(FontNamePixelSize|FontNamePointSize);
    fontNameAttributes |= FontNameResolutionX;
    fontNameAttributes |= FontNameResolutionY;
    fontName.ResolutionX = dw->dvi.screen_resolution;
    fontName.ResolutionY = dw->dvi.screen_resolution;
    XFormatFontName (&fontName, fontNameAttributes, fontNameString);
    fonts = XListFonts (XtDisplay (dw), fontNameString, 10000000, &count);
    for (i = 0; i < count; i++) {
	size = ConvertFontNameToSize (fonts[i]);
	if (size == 0)
	{
	    DisposeFontSizes (dw, sizes);
	    *scalablep = TRUE;
	    sizes = 0;
	    break;
	}
	if (size != -1) {
	    new = (DviFontSizeList *) XtMalloc (sizeof *new);
	    new->next = sizes;
	    new->size = size;
	    new->x_name = savestr (fonts[i]);
	    new->doesnt_exist = 0;
	    new->font = 0;
	    sizes = new;
	}
    }
    XFreeFontNames (fonts);
#endif
    return sizes;
}

static DviFontList *
InstallFont (DviWidget dw, int position, char *dvi_name, char *x_name)
{
    DviFontList	*f;
    char		*encoding;

    f = LookupFontByPosition (dw, position);
    if (f) {
	/*
	 * ignore gratuitous font loading
	 */
	if (!strcmp (f->dvi_name, dvi_name) && !strcmp (f->x_name, x_name))
	    return f;

	DisposeFontSizes (dw, f->sizes);
	if (f->dvi_name)
	    XtFree (f->dvi_name);
	if (f->x_name)
	    XtFree (f->x_name);
    } else {
	f = (DviFontList *) XtMalloc (sizeof (*f));
	f->next = dw->dvi.fonts;
	dw->dvi.fonts = f;
    }
    f->initialized = FALSE;
    f->dvi_name = savestr (dvi_name);
    f->x_name = savestr (x_name);
    f->dvi_number = position;
    f->sizes = 0;
    f->scalable = FALSE;
    if (f->x_name) {
	encoding = ConvertFontNameToEncoding (f->x_name);
	f->char_map = DviFindMap (encoding);
    } else
	f->char_map = 0;
    /* 
     * force requery of fonts
     */
    dw->dvi.font = 0;
    dw->dvi.font_number = -1;
    dw->dvi.cache.font = 0;
    dw->dvi.cache.font_number = -1;
    return f;
}

static char *
MapDviNameToXName (DviWidget dw, char *dvi_name)
{
    DviFontMap	*fm;
    
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
	if (!strcmp (fm->dvi_name, dvi_name))
	    return fm->x_name;
    ++dvi_name;
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
	if (!strcmp (fm->dvi_name, "R"))
	    return fm->x_name;
    if (dw->dvi.font_map->x_name)
	return dw->dvi.font_map->x_name;
    return "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1";
}

#ifdef NOTUSED
static char *
MapXNameToDviName (dw, x_name)
	DviWidget	dw;
	char		*x_name;
{
    DviFontMap	*fm;
    
    for (fm = dw->dvi.font_map; fm; fm=fm->next)
	if (!strcmp (fm->x_name, x_name))
	    return fm->dvi_name;
    return 0;
}
#endif

void
ParseFontMap (dw)
	DviWidget	dw;
{
    char		dvi_name[1024];
    char		x_name[2048];
    char		*m, *s;
    DviFontMap	*fm, *new;

    if (dw->dvi.font_map)
	    DestroyFontMap (dw->dvi.font_map);
    fm = 0;
    m = dw->dvi.font_map_string;
    while (*m) {
	s = m;
	while (*m && !isspace (*m))
	    ++m;
	strncpy (dvi_name, s, m-s);
	dvi_name[m-s] = '\0';
	while (isspace (*m))
	    ++m;
	s = m;
	while (*m && *m != '\n')
	    ++m;
	strncpy (x_name, s, m-s);
	x_name[m-s] = '\0';
	new = (DviFontMap *) XtMalloc (sizeof *new);
	new->x_name = savestr (x_name);
	new->dvi_name = savestr (dvi_name);
	new->next = fm;
	fm = new;
	++m;
    }
    dw->dvi.font_map = fm;
}

void
DestroyFontMap (font_map)
    DviFontMap	*font_map;
{
    DviFontMap	*next;

    for (; font_map; font_map = next) {
	next = font_map->next;
	if (font_map->x_name)
	    XtFree (font_map->x_name);
	if (font_map->dvi_name)
	    XtFree (font_map->dvi_name);
	XtFree ((char *) font_map);
    }
}

/*ARGSUSED*/
void
SetFontPosition (dw, position, dvi_name, extra)
    DviWidget	dw;
    int		position;
    char	*dvi_name;
    char	*extra;	/* unused */
{
    char	*x_name;

    x_name = MapDviNameToXName (dw, dvi_name);
    (void) InstallFont (dw, position, dvi_name, x_name);
}

#ifdef USE_XFT
XftFont *
#else
XFontStruct *
#endif
QueryFont (dw, position, size)
    DviWidget	dw;
    int		position;
    int		size;
{
    DviFontList	*f;
    DviFontSizeList	*fs;

    f = LookupFontByPosition (dw, position);
    if (!f)
	return dw->dvi.default_font;
    if (!f->initialized) {
	f->sizes = InstallFontSizes (dw, f->x_name, &f->scalable);
	f->initialized = TRUE;
    }
    fs = LookupFontSizeBySize (dw, f, size);
    if (!fs)
	return dw->dvi.default_font;
    if (!fs->font) {
	if (fs->x_name)
	{
#ifdef USE_XFT
	    XftPattern	*pat;
	    XftPattern	*match;
	    XftResult	result;

	    pat = XftXlfdParse (fs->x_name, False, False);
	    XftPatternAddBool (pat, XFT_CORE, fs->core);
	    match = XftFontMatch (XtDisplay (dw),
				  XScreenNumberOfScreen(dw->core.screen),
				  pat, &result);
	    XftPatternDestroy (pat);
	    if (match)
	    {
		fs->font = XftFontOpenPattern (XtDisplay (dw),
					       match);
		if (!fs->font)
		    XftPatternDestroy (match);
	    }
	    else
		fs->font = 0;
#else
	    fs->font = XLoadQueryFont (XtDisplay (dw), fs->x_name);
#endif
	}
	if (!fs->font)
	    fs->font = dw->dvi.default_font;
    }
    return fs->font;
}

DviCharNameMap *
QueryFontMap (dw, position)
	DviWidget	dw;
	int		position;
{
	DviFontList	*f;

	f = LookupFontByPosition (dw, position);
	if (f)
	    return f->char_map;
	else
	    return 0;
}

unsigned char *
DviCharIsLigature (map, name)
    DviCharNameMap  *map;
    char	    *name;
{
    int	    i;

    for (i = 0; i < DVI_MAX_LIGATURES; i++) {
	if (!map->ligatures[i][0])
	    break;
	if (!strcmp (name, map->ligatures[i][0]))
	    return (unsigned char *) map->ligatures[i][1];
    }
    return 0;
}

#if 0
LoadFont (dw, position, size)
	DviWidget	dw;
	int		position;
	int		size;
{
	XFontStruct	*font;

	font = QueryFont (dw, position, size);
	dw->dvi.font_number = position;
	dw->dvi.font_size = size;
	dw->dvi.font = font;
	XSetFont (XtDisplay (dw), dw->dvi.normal_GC, font->fid);
	return;
}
#endif