4
* map dvi fonts to X fonts
6
/* $XFree86: xc/programs/xditview/font.c,v 1.6 2002/06/19 20:09:19 keithp Exp $ */
9
#include <X11/IntrinsicP.h>
10
#include <X11/StringDefs.h>
15
#include "XFontName.h"
24
n = XtMalloc (strlen (s) + 1);
31
LookupFontByPosition (DviWidget dw, int position)
35
for (f = dw->dvi.fonts; f; f=f->next)
36
if (f->dvi_number == position)
41
static DviFontSizeList *
42
LookupFontSizeBySize (DviWidget dw, DviFontList *f, int size)
44
DviFontSizeList *fs, *best = 0;
46
char fontNameString[2048];
48
unsigned int fontNameAttributes;
53
for (best = f->sizes; best; best = best->next)
54
if (best->size == size)
56
best = (DviFontSizeList *) XtMalloc (sizeof *best);
57
best->next = f->sizes;
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);
71
* Force a match of a core font for adobe-fontspecific
72
* encodings; we dont have a scalable font in
76
if (!strcmp (fontName.CharSetRegistry, "adobe") &&
77
!strcmp (fontName.CharSetEncoding, "fontspecific"))
82
best->doesnt_exist = 0;
89
for (fs = f->sizes; fs; fs=fs->next) {
90
dist = size - fs->size;
104
SkipFontNameElement (char *n)
112
# define SizePosition 8
113
# define EncodingPosition 13
117
ConvertFontNameToSize (char *n)
121
for (i = 0; i < SizePosition; i++) {
122
n = SkipFontNameElement (n);
132
ConvertFontNameToEncoding (char *n)
135
for (i = 0; i < EncodingPosition; i++) {
136
n = SkipFontNameElement (n);
144
DisposeFontSizes (DviWidget dw, DviFontSizeList *fs)
146
DviFontSizeList *next;
148
for (; fs; fs=next) {
155
XftFontClose (XtDisplay (dw), fs->font);
157
XUnloadFont (XtDisplay (dw), fs->font->fid);
158
XFree ((char *)fs->font);
161
XtFree ((char *) fs);
166
ResetFonts (DviWidget dw)
170
for (f = dw->dvi.fonts; f; f = f->next)
174
DisposeFontSizes (dw, f->sizes);
176
f->initialized = FALSE;
181
* force requery of fonts
184
dw->dvi.font_number = -1;
185
dw->dvi.cache.font = 0;
186
dw->dvi.cache.font_number = -1;
189
static DviFontSizeList *
190
InstallFontSizes (DviWidget dw, char *x_name, Boolean *scalablep)
193
char fontNameString[2048];
197
DviFontSizeList *new;
199
unsigned int fontNameAttributes;
201
DviFontSizeList *sizes;
208
if (!XParseFontName (x_name, &fontName, &fontNameAttributes))
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]);
222
DisposeFontSizes (dw, sizes);
228
new = (DviFontSizeList *) XtMalloc (sizeof *new);
231
new->x_name = savestr (fonts[i]);
232
new->doesnt_exist = 0;
237
XFreeFontNames (fonts);
243
InstallFont (DviWidget dw, int position, char *dvi_name, char *x_name)
248
f = LookupFontByPosition (dw, position);
251
* ignore gratuitous font loading
253
if (!strcmp (f->dvi_name, dvi_name) && !strcmp (f->x_name, x_name))
256
DisposeFontSizes (dw, f->sizes);
258
XtFree (f->dvi_name);
262
f = (DviFontList *) XtMalloc (sizeof (*f));
263
f->next = dw->dvi.fonts;
266
f->initialized = FALSE;
267
f->dvi_name = savestr (dvi_name);
268
f->x_name = savestr (x_name);
269
f->dvi_number = position;
273
encoding = ConvertFontNameToEncoding (f->x_name);
274
f->char_map = DviFindMap (encoding);
278
* force requery of fonts
281
dw->dvi.font_number = -1;
282
dw->dvi.cache.font = 0;
283
dw->dvi.cache.font_number = -1;
288
MapDviNameToXName (DviWidget dw, char *dvi_name)
292
for (fm = dw->dvi.font_map; fm; fm=fm->next)
293
if (!strcmp (fm->dvi_name, dvi_name))
296
for (fm = dw->dvi.font_map; fm; fm=fm->next)
297
if (!strcmp (fm->dvi_name, "R"))
299
if (dw->dvi.font_map->x_name)
300
return dw->dvi.font_map->x_name;
301
return "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1";
306
MapXNameToDviName (dw, x_name)
312
for (fm = dw->dvi.font_map; fm; fm=fm->next)
313
if (!strcmp (fm->x_name, x_name))
326
DviFontMap *fm, *new;
328
if (dw->dvi.font_map)
329
DestroyFontMap (dw->dvi.font_map);
331
m = dw->dvi.font_map_string;
334
while (*m && !isspace (*m))
336
strncpy (dvi_name, s, m-s);
337
dvi_name[m-s] = '\0';
341
while (*m && *m != '\n')
343
strncpy (x_name, s, m-s);
345
new = (DviFontMap *) XtMalloc (sizeof *new);
346
new->x_name = savestr (x_name);
347
new->dvi_name = savestr (dvi_name);
352
dw->dvi.font_map = fm;
356
DestroyFontMap (font_map)
357
DviFontMap *font_map;
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);
373
SetFontPosition (dw, position, dvi_name, extra)
377
char *extra; /* unused */
381
x_name = MapDviNameToXName (dw, dvi_name);
382
(void) InstallFont (dw, position, dvi_name, x_name);
390
QueryFont (dw, position, size)
398
f = LookupFontByPosition (dw, position);
400
return dw->dvi.default_font;
401
if (!f->initialized) {
402
f->sizes = InstallFontSizes (dw, f->x_name, &f->scalable);
403
f->initialized = TRUE;
405
fs = LookupFontSizeBySize (dw, f, size);
407
return dw->dvi.default_font;
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),
421
XftPatternDestroy (pat);
424
fs->font = XftFontOpenPattern (XtDisplay (dw),
427
XftPatternDestroy (match);
432
fs->font = XLoadQueryFont (XtDisplay (dw), fs->x_name);
436
fs->font = dw->dvi.default_font;
442
QueryFontMap (dw, position)
448
f = LookupFontByPosition (dw, position);
456
DviCharIsLigature (map, name)
462
for (i = 0; i < DVI_MAX_LIGATURES; i++) {
463
if (!map->ligatures[i][0])
465
if (!strcmp (name, map->ligatures[i][0]))
466
return (unsigned char *) map->ligatures[i][1];
472
LoadFont (dw, position, size)
479
font = QueryFont (dw, position, size);
480
dw->dvi.font_number = position;
481
dw->dvi.font_size = size;
483
XSetFont (XtDisplay (dw), dw->dvi.normal_GC, font->fid);