4 * map dvi fonts to X fonts
10 #include <X11/IntrinsicP.h>
11 #include <X11/StringDefs.h>
17 #include "XFontName.h"
20 /* forward reference */
21 static void DisposeFontSizes(DviWidget, DviFontSizeList *);
24 savestr (const char *s)
30 n = XtMalloc (strlen (s) + 1);
37 LookupFontByPosition (DviWidget dw, int position)
41 for (f = dw->dvi.fonts; f; f = f->next)
42 if (f->dvi_number == position)
48 MaxFontPosition (DviWidget dw)
53 for (f = dw->dvi.fonts; f; f = f->next)
54 if (f->dvi_number > n)
59 static DviFontSizeList *
60 LookupFontSizeBySize (DviWidget dw, DviFontList *f, int size)
62 DviFontSizeList *fs, *best = 0, *smallest = 0;
65 unsigned int fontNameAttributes;
66 char fontNameString[2048];
70 decipointsize = (10*size)/dw->dvi.sizescale;
71 for (best = f->sizes; best; best = best->next)
72 if (best->size == decipointsize)
74 best = (DviFontSizeList *) XtMalloc(sizeof *best);
75 best->next = f->sizes;
76 best->size = decipointsize;
78 XParseFontName (f->x_name, &fontName, &fontNameAttributes);
79 fontNameAttributes &= ~(FontNamePixelSize|FontNameAverageWidth);
80 fontNameAttributes |= FontNameResolutionX;
81 fontNameAttributes |= FontNameResolutionY;
82 fontNameAttributes |= FontNamePointSize;
83 fontName.ResolutionX = dw->dvi.display_resolution;
84 fontName.ResolutionY = dw->dvi.display_resolution;
85 fontName.PointSize = decipointsize;
86 XFormatFontName (&fontName, fontNameAttributes, fontNameString);
87 best->x_name = savestr (fontNameString);
88 best->doesnt_exist = 0;
92 for (fs = f->sizes; fs; fs=fs->next) {
93 if (dw->dvi.sizescale*fs->size <= 10*size
94 && fs->size >= bestsize) {
98 if (smallest == 0 || fs->size < smallest->size)
101 return best ? best : smallest;
105 SkipFontNameElement (char *n)
113 # define SizePosition 8
114 # define EncodingPosition 13
117 ConvertFontNameToSize (char *n)
121 for (i = 0; i < SizePosition; i++) {
122 n = SkipFontNameElement (n);
131 ConvertFontNameToEncoding (char *n)
134 for (i = 0; i < EncodingPosition; i++) {
135 n = SkipFontNameElement (n);
142 static DviFontSizeList *
143 InstallFontSizes (DviWidget dw, const char *x_name, Boolean *scalablep)
145 char fontNameString[2048];
149 DviFontSizeList *sizes, *new_size;
151 unsigned int fontNameAttributes;
154 if (!XParseFontName ((XFontNameString)x_name, &fontName,
155 &fontNameAttributes))
157 fontNameAttributes &= ~(FontNamePixelSize|FontNamePointSize
158 |FontNameAverageWidth);
159 fontNameAttributes |= FontNameResolutionX;
160 fontNameAttributes |= FontNameResolutionY;
161 fontName.ResolutionX = dw->dvi.display_resolution;
162 fontName.ResolutionY = dw->dvi.display_resolution;
163 XFormatFontName (&fontName, fontNameAttributes, fontNameString);
164 fonts = XListFonts (XtDisplay (dw), fontNameString, 10000000, &count);
166 for (i = 0; i < count; i++) {
167 size = ConvertFontNameToSize (fonts[i]);
169 DisposeFontSizes (dw, sizes);
175 new_size = (DviFontSizeList *) XtMalloc (sizeof *new_size);
176 new_size->next = sizes;
177 new_size->size = size;
178 new_size->x_name = savestr (fonts[i]);
179 new_size->doesnt_exist = 0;
184 XFreeFontNames (fonts);
189 DisposeFontSizes (DviWidget dw, DviFontSizeList *fs)
191 DviFontSizeList *next;
193 for (; fs; fs=next) {
197 if (fs->font && fs->font != dw->dvi.default_font) {
198 XUnloadFont (XtDisplay (dw), fs->font->fid);
199 XFree ((char *)fs->font);
201 XtFree ((char *) fs);
206 InstallFont (DviWidget dw, int position,
207 const char *dvi_name, const char *x_name)
212 if ((f = LookupFontByPosition (dw, position)) != NULL) {
214 * ignore gratuitous font loading
216 if (!strcmp (f->dvi_name, dvi_name) &&
217 !strcmp (f->x_name, x_name))
220 DisposeFontSizes (dw, f->sizes);
222 XtFree (f->dvi_name);
227 f = (DviFontList *) XtMalloc (sizeof (*f));
228 f->next = dw->dvi.fonts;
231 f->initialized = FALSE;
232 f->dvi_name = savestr (dvi_name);
233 f->device_font = device_find_font (dw->dvi.device, dvi_name);
234 f->x_name = savestr (x_name);
235 f->dvi_number = position;
239 encoding = ConvertFontNameToEncoding (f->x_name);
240 f->char_map = DviFindMap (encoding);
244 * force requery of fonts
247 dw->dvi.font_number = -1;
248 dw->dvi.cache.font = 0;
249 dw->dvi.cache.font_number = -1;
250 dw->dvi.device_font = 0;
251 dw->dvi.device_font_number = -1;
256 ForgetFonts (DviWidget dw)
258 DviFontList *f = dw->dvi.fonts;
261 DviFontList *tem = f;
264 DisposeFontSizes (dw, f->sizes);
266 XtFree (f->dvi_name);
270 XtFree ((char *) tem);
274 * force requery of fonts
277 dw->dvi.font_number = -1;
278 dw->dvi.cache.font = 0;
279 dw->dvi.cache.font_number = -1;
280 dw->dvi.device_font = 0;
281 dw->dvi.device_font_number = -1;
287 MapDviNameToXName (DviWidget dw, const char *dvi_name)
291 for (fm = dw->dvi.font_map; fm; fm=fm->next)
292 if (!strcmp (fm->dvi_name, dvi_name))
299 MapXNameToDviName (DviWidget dw, const char *x_name)
303 for (fm = dw->dvi.font_map; fm; fm=fm->next)
304 if (!strcmp (fm->x_name, x_name))
311 ParseFontMap (DviWidget dw)
316 DviFontMap *fm, *new_map;
318 if (dw->dvi.font_map)
319 DestroyFontMap (dw->dvi.font_map);
321 m = dw->dvi.font_map_string;
324 while (*m && !isspace (*m))
326 strncpy (dvi_name, s, m-s);
327 dvi_name[m-s] = '\0';
331 while (*m && *m != '\n')
333 strncpy (x_name, s, m-s);
335 new_map = (DviFontMap *) XtMalloc (sizeof *new_map);
336 new_map->x_name = savestr (x_name);
337 new_map->dvi_name = savestr (dvi_name);
342 dw->dvi.font_map = fm;
346 DestroyFontMap (DviFontMap *font_map)
350 for (; font_map; font_map = next) {
351 next = font_map->next;
352 if (font_map->x_name)
353 XtFree (font_map->x_name);
354 if (font_map->dvi_name)
355 XtFree (font_map->dvi_name);
356 XtFree ((char *) font_map);
363 SetFontPosition (DviWidget dw, int position,
364 const char *dvi_name, const char *extra)
368 x_name = MapDviNameToXName (dw, dvi_name);
370 (void) InstallFont (dw, position, dvi_name, x_name);
372 extra = extra; /* unused; suppress compiler warning */
376 QueryFont (DviWidget dw, int position, int size)
381 f = LookupFontByPosition (dw, position);
383 return dw->dvi.default_font;
384 if (!f->initialized) {
385 f->sizes = InstallFontSizes (dw, f->x_name, &f->scalable);
386 f->initialized = TRUE;
388 fs = LookupFontSizeBySize (dw, f, size);
390 return dw->dvi.default_font;
393 fs->font = XLoadQueryFont (XtDisplay (dw), fs->x_name);
395 fs->font = dw->dvi.default_font;
401 QueryDeviceFont (DviWidget dw, int position)
405 f = LookupFontByPosition (dw, position);
408 return f->device_font;
412 QueryFontMap (DviWidget dw, int position)
416 f = LookupFontByPosition (dw, position);
424 LoadFont (DviWidget dw, int position, int size)
428 font = QueryFont (dw, position, size);
429 dw->dvi.font_number = position;
430 dw->dvi.font_size = size;
432 XSetFont (XtDisplay (dw), dw->dvi.normal_GC, font->fid);
440 c-continued-statement-offset: 8
444 c-tab-always-indent: nil