4 * map dvi fonts to X fonts
8 #include <X11/IntrinsicP.h>
9 #include <X11/StringDefs.h>
15 #include "XFontName.h"
18 /* forward reference */
19 static void DisposeFontSizes(DviWidget, DviFontSizeList *);
22 savestr (const char *s)
28 n = XtMalloc (strlen (s) + 1);
35 LookupFontByPosition (DviWidget dw, int position)
39 for (f = dw->dvi.fonts; f; f = f->next)
40 if (f->dvi_number == position)
46 MaxFontPosition (DviWidget dw)
51 for (f = dw->dvi.fonts; f; f = f->next)
52 if (f->dvi_number > n)
57 static DviFontSizeList *
58 LookupFontSizeBySize (DviWidget dw, DviFontList *f, int size)
60 DviFontSizeList *fs, *best = 0, *smallest = 0;
63 unsigned int fontNameAttributes;
64 char fontNameString[2048];
68 decipointsize = (10*size)/dw->dvi.sizescale;
69 for (best = f->sizes; best; best = best->next)
70 if (best->size == decipointsize)
72 best = (DviFontSizeList *) XtMalloc(sizeof *best);
73 best->next = f->sizes;
74 best->size = decipointsize;
76 XParseFontName (f->x_name, &fontName, &fontNameAttributes);
77 fontNameAttributes &= ~(FontNamePixelSize|FontNameAverageWidth);
78 fontNameAttributes |= FontNameResolutionX;
79 fontNameAttributes |= FontNameResolutionY;
80 fontNameAttributes |= FontNamePointSize;
81 fontName.ResolutionX = dw->dvi.display_resolution;
82 fontName.ResolutionY = dw->dvi.display_resolution;
83 fontName.PointSize = decipointsize;
84 XFormatFontName (&fontName, fontNameAttributes, fontNameString);
85 best->x_name = savestr (fontNameString);
86 best->doesnt_exist = 0;
90 for (fs = f->sizes; fs; fs=fs->next) {
91 if (dw->dvi.sizescale*fs->size <= 10*size
92 && fs->size >= bestsize) {
96 if (smallest == 0 || fs->size < smallest->size)
99 return best ? best : smallest;
103 SkipFontNameElement (char *n)
111 # define SizePosition 8
112 # define EncodingPosition 13
115 ConvertFontNameToSize (char *n)
119 for (i = 0; i < SizePosition; i++) {
120 n = SkipFontNameElement (n);
129 ConvertFontNameToEncoding (char *n)
132 for (i = 0; i < EncodingPosition; i++) {
133 n = SkipFontNameElement (n);
140 static DviFontSizeList *
141 InstallFontSizes (DviWidget dw, const char *x_name, Boolean *scalablep)
143 char fontNameString[2048];
147 DviFontSizeList *sizes, *new_size;
149 unsigned int fontNameAttributes;
152 if (!XParseFontName ((XFontNameString)x_name, &fontName,
153 &fontNameAttributes))
155 fontNameAttributes &= ~(FontNamePixelSize|FontNamePointSize
156 |FontNameAverageWidth);
157 fontNameAttributes |= FontNameResolutionX;
158 fontNameAttributes |= FontNameResolutionY;
159 fontName.ResolutionX = dw->dvi.display_resolution;
160 fontName.ResolutionY = dw->dvi.display_resolution;
161 XFormatFontName (&fontName, fontNameAttributes, fontNameString);
162 fonts = XListFonts (XtDisplay (dw), fontNameString, 10000000, &count);
164 for (i = 0; i < count; i++) {
165 size = ConvertFontNameToSize (fonts[i]);
167 DisposeFontSizes (dw, sizes);
173 new_size = (DviFontSizeList *) XtMalloc (sizeof *new_size);
174 new_size->next = sizes;
175 new_size->size = size;
176 new_size->x_name = savestr (fonts[i]);
177 new_size->doesnt_exist = 0;
182 XFreeFontNames (fonts);
187 DisposeFontSizes (DviWidget dw, DviFontSizeList *fs)
189 DviFontSizeList *next;
191 for (; fs; fs=next) {
195 if (fs->font && fs->font != dw->dvi.default_font) {
196 XUnloadFont (XtDisplay (dw), fs->font->fid);
197 XFree ((char *)fs->font);
199 XtFree ((char *) fs);
204 InstallFont (DviWidget dw, int position,
205 const char *dvi_name, const char *x_name)
210 if ((f = LookupFontByPosition (dw, position)) != NULL) {
212 * ignore gratuitous font loading
214 if (!strcmp (f->dvi_name, dvi_name) &&
215 !strcmp (f->x_name, x_name))
218 DisposeFontSizes (dw, f->sizes);
220 XtFree (f->dvi_name);
225 f = (DviFontList *) XtMalloc (sizeof (*f));
226 f->next = dw->dvi.fonts;
229 f->initialized = FALSE;
230 f->dvi_name = savestr (dvi_name);
231 f->device_font = device_find_font (dw->dvi.device, dvi_name);
232 f->x_name = savestr (x_name);
233 f->dvi_number = position;
237 encoding = ConvertFontNameToEncoding (f->x_name);
238 f->char_map = DviFindMap (encoding);
242 * force requery of fonts
245 dw->dvi.font_number = -1;
246 dw->dvi.cache.font = 0;
247 dw->dvi.cache.font_number = -1;
248 dw->dvi.device_font = 0;
249 dw->dvi.device_font_number = -1;
254 ForgetFonts (DviWidget dw)
256 DviFontList *f = dw->dvi.fonts;
259 DviFontList *tem = f;
262 DisposeFontSizes (dw, f->sizes);
264 XtFree (f->dvi_name);
268 XtFree ((char *) tem);
272 * force requery of fonts
275 dw->dvi.font_number = -1;
276 dw->dvi.cache.font = 0;
277 dw->dvi.cache.font_number = -1;
278 dw->dvi.device_font = 0;
279 dw->dvi.device_font_number = -1;
285 MapDviNameToXName (DviWidget dw, const char *dvi_name)
289 for (fm = dw->dvi.font_map; fm; fm=fm->next)
290 if (!strcmp (fm->dvi_name, dvi_name))
297 MapXNameToDviName (DviWidget dw, const char *x_name)
301 for (fm = dw->dvi.font_map; fm; fm=fm->next)
302 if (!strcmp (fm->x_name, x_name))
309 ParseFontMap (DviWidget dw)
314 DviFontMap *fm, *new_map;
316 if (dw->dvi.font_map)
317 DestroyFontMap (dw->dvi.font_map);
319 m = dw->dvi.font_map_string;
322 while (*m && !isspace (*m))
324 strncpy (dvi_name, s, m-s);
325 dvi_name[m-s] = '\0';
329 while (*m && *m != '\n')
331 strncpy (x_name, s, m-s);
333 new_map = (DviFontMap *) XtMalloc (sizeof *new_map);
334 new_map->x_name = savestr (x_name);
335 new_map->dvi_name = savestr (dvi_name);
340 dw->dvi.font_map = fm;
344 DestroyFontMap (DviFontMap *font_map)
348 for (; font_map; font_map = next) {
349 next = font_map->next;
350 if (font_map->x_name)
351 XtFree (font_map->x_name);
352 if (font_map->dvi_name)
353 XtFree (font_map->dvi_name);
354 XtFree ((char *) font_map);
361 SetFontPosition (DviWidget dw, int position,
362 const char *dvi_name, const char *extra)
366 x_name = MapDviNameToXName (dw, dvi_name);
368 (void) InstallFont (dw, position, dvi_name, x_name);
370 extra = extra; /* unused; suppress compiler warning */
374 QueryFont (DviWidget dw, int position, int size)
379 f = LookupFontByPosition (dw, position);
381 return dw->dvi.default_font;
382 if (!f->initialized) {
383 f->sizes = InstallFontSizes (dw, f->x_name, &f->scalable);
384 f->initialized = TRUE;
386 fs = LookupFontSizeBySize (dw, f, size);
388 return dw->dvi.default_font;
391 fs->font = XLoadQueryFont (XtDisplay (dw), fs->x_name);
393 fs->font = dw->dvi.default_font;
399 QueryDeviceFont (DviWidget dw, int position)
403 f = LookupFontByPosition (dw, position);
406 return f->device_font;
410 QueryFontMap (DviWidget dw, int position)
414 f = LookupFontByPosition (dw, position);
422 LoadFont (DviWidget dw, int position, int size)
426 font = QueryFont (dw, position, size);
427 dw->dvi.font_number = position;
428 dw->dvi.font_size = size;
430 XSetFont (XtDisplay (dw), dw->dvi.normal_GC, font->fid);
438 c-continued-statement-offset: 8
442 c-tab-always-indent: nil