4 * map dvi fonts to X fonts
8 #include <X11/IntrinsicP.h>
9 #include <X11/StringDefs.h>
14 #include "XFontName.h"
16 static void DisposeFontSizes();
17 void DestroyFontMap();
20 extern Bool
XParseFontName();
21 extern Bool
XFormatFontName();
31 n
= XtMalloc (strlen (s
) + 1);
38 LookupFontByPosition (dw
, position
)
44 for (f
= dw
->dvi
.fonts
; f
; f
= f
->next
)
45 if (f
->dvi_number
== position
)
57 for (f
= dw
->dvi
.fonts
; f
; f
= f
->next
)
58 if (f
->dvi_number
> n
)
63 static DviFontSizeList
*
64 LookupFontSizeBySize (dw
, f
, size
)
69 DviFontSizeList
*fs
, *best
= 0, *smallest
= 0;
72 unsigned int fontNameAttributes
;
73 char fontNameString
[2048];
77 decipointsize
= (10*size
)/dw
->dvi
.sizescale
;
78 for (best
= f
->sizes
; best
; best
= best
->next
)
79 if (best
->size
== decipointsize
)
81 best
= (DviFontSizeList
*) XtMalloc(sizeof *best
);
82 best
->next
= f
->sizes
;
83 best
->size
= decipointsize
;
85 XParseFontName (f
->x_name
, &fontName
, &fontNameAttributes
);
86 fontNameAttributes
&= ~(FontNamePixelSize
|FontNameAverageWidth
);
87 fontNameAttributes
|= FontNameResolutionX
;
88 fontNameAttributes
|= FontNameResolutionY
;
89 fontNameAttributes
|= FontNamePointSize
;
90 fontName
.ResolutionX
= dw
->dvi
.display_resolution
;
91 fontName
.ResolutionY
= dw
->dvi
.display_resolution
;
92 fontName
.PointSize
= decipointsize
;
93 XFormatFontName (&fontName
, fontNameAttributes
, fontNameString
);
94 best
->x_name
= savestr (fontNameString
);
95 best
->doesnt_exist
= 0;
99 for (fs
= f
->sizes
; fs
; fs
=fs
->next
) {
100 if (dw
->dvi
.sizescale
*fs
->size
<= 10*size
101 && fs
->size
>= bestsize
) {
105 if (smallest
== 0 || fs
->size
< smallest
->size
)
108 return best
? best
: smallest
;
112 SkipFontNameElement (n
)
121 # define SizePosition 8
122 # define EncodingPosition 13
125 ConvertFontNameToSize (n
)
130 for (i
= 0; i
< SizePosition
; i
++) {
131 n
= SkipFontNameElement (n
);
140 ConvertFontNameToEncoding (n
)
144 for (i
= 0; i
< EncodingPosition
; i
++) {
145 n
= SkipFontNameElement (n
);
153 InstallFontSizes (dw
, x_name
, scalablep
)
158 char fontNameString
[2048];
162 DviFontSizeList
*sizes
, *new;
164 unsigned int fontNameAttributes
;
167 if (!XParseFontName (x_name
, &fontName
, &fontNameAttributes
))
169 fontNameAttributes
&= ~(FontNamePixelSize
|FontNamePointSize
170 |FontNameAverageWidth
);
171 fontNameAttributes
|= FontNameResolutionX
;
172 fontNameAttributes
|= FontNameResolutionY
;
173 fontName
.ResolutionX
= dw
->dvi
.display_resolution
;
174 fontName
.ResolutionY
= dw
->dvi
.display_resolution
;
175 XFormatFontName (&fontName
, fontNameAttributes
, fontNameString
);
176 fonts
= XListFonts (XtDisplay (dw
), fontNameString
, 10000000, &count
);
178 for (i
= 0; i
< count
; i
++) {
179 size
= ConvertFontNameToSize (fonts
[i
]);
181 DisposeFontSizes (dw
, sizes
);
187 new = (DviFontSizeList
*) XtMalloc (sizeof *new);
190 new->x_name
= savestr (fonts
[i
]);
191 new->doesnt_exist
= 0;
196 XFreeFontNames (fonts
);
201 DisposeFontSizes (dw
, fs
)
205 DviFontSizeList
*next
;
207 for (; fs
; fs
=next
) {
211 if (fs
->font
&& fs
->font
!= dw
->dvi
.default_font
) {
212 XUnloadFont (XtDisplay (dw
), fs
->font
->fid
);
213 XFree ((char *)fs
->font
);
215 XtFree ((char *) fs
);
220 InstallFont (dw
, position
, dvi_name
, x_name
)
229 if ((f
= LookupFontByPosition (dw
, position
)) != NULL
) {
231 * ignore gratuitous font loading
233 if (!strcmp (f
->dvi_name
, dvi_name
) &&
234 !strcmp (f
->x_name
, x_name
))
237 DisposeFontSizes (dw
, f
->sizes
);
239 XtFree (f
->dvi_name
);
244 f
= (DviFontList
*) XtMalloc (sizeof (*f
));
245 f
->next
= dw
->dvi
.fonts
;
248 f
->initialized
= FALSE
;
249 f
->dvi_name
= savestr (dvi_name
);
250 f
->device_font
= device_find_font (dw
->dvi
.device
, dvi_name
);
251 f
->x_name
= savestr (x_name
);
252 f
->dvi_number
= position
;
256 encoding
= ConvertFontNameToEncoding (f
->x_name
);
257 f
->char_map
= DviFindMap (encoding
);
261 * force requery of fonts
264 dw
->dvi
.font_number
= -1;
265 dw
->dvi
.cache
.font
= 0;
266 dw
->dvi
.cache
.font_number
= -1;
267 dw
->dvi
.device_font
= 0;
268 dw
->dvi
.device_font_number
= -1;
276 DviFontList
*f
= dw
->dvi
.fonts
;
279 DviFontList
*tem
= f
;
282 DisposeFontSizes (dw
, f
->sizes
);
284 XtFree (f
->dvi_name
);
288 XtFree ((char *) tem
);
292 * force requery of fonts
295 dw
->dvi
.font_number
= -1;
296 dw
->dvi
.cache
.font
= 0;
297 dw
->dvi
.cache
.font_number
= -1;
298 dw
->dvi
.device_font
= 0;
299 dw
->dvi
.device_font_number
= -1;
305 MapDviNameToXName (dw
, dvi_name
)
311 for (fm
= dw
->dvi
.font_map
; fm
; fm
=fm
->next
)
312 if (!strcmp (fm
->dvi_name
, dvi_name
))
319 MapXNameToDviName (dw
, x_name
)
325 for (fm
= dw
->dvi
.font_map
; fm
; fm
=fm
->next
)
326 if (!strcmp (fm
->x_name
, x_name
))
339 DviFontMap
*fm
, *new;
341 if (dw
->dvi
.font_map
)
342 DestroyFontMap (dw
->dvi
.font_map
);
344 m
= dw
->dvi
.font_map_string
;
347 while (*m
&& !isspace (*m
))
349 strncpy (dvi_name
, s
, m
-s
);
350 dvi_name
[m
-s
] = '\0';
354 while (*m
&& *m
!= '\n')
356 strncpy (x_name
, s
, m
-s
);
358 new = (DviFontMap
*) XtMalloc (sizeof *new);
359 new->x_name
= savestr (x_name
);
360 new->dvi_name
= savestr (dvi_name
);
365 dw
->dvi
.font_map
= fm
;
369 DestroyFontMap (font_map
)
370 DviFontMap
*font_map
;
374 for (; font_map
; font_map
= next
) {
375 next
= font_map
->next
;
376 if (font_map
->x_name
)
377 XtFree (font_map
->x_name
);
378 if (font_map
->dvi_name
)
379 XtFree (font_map
->dvi_name
);
380 XtFree ((char *) font_map
);
387 SetFontPosition (dw
, position
, dvi_name
, extra
)
391 char *extra
; /* unused */
395 x_name
= MapDviNameToXName (dw
, dvi_name
);
397 (void) InstallFont (dw
, position
, dvi_name
, x_name
);
401 QueryFont (dw
, position
, size
)
409 f
= LookupFontByPosition (dw
, position
);
411 return dw
->dvi
.default_font
;
412 if (!f
->initialized
) {
413 f
->sizes
= InstallFontSizes (dw
, f
->x_name
, &f
->scalable
);
414 f
->initialized
= TRUE
;
416 fs
= LookupFontSizeBySize (dw
, f
, size
);
418 return dw
->dvi
.default_font
;
421 fs
->font
= XLoadQueryFont (XtDisplay (dw
), fs
->x_name
);
423 fs
->font
= dw
->dvi
.default_font
;
429 QueryDeviceFont (dw
, position
)
435 f
= LookupFontByPosition (dw
, position
);
438 return f
->device_font
;
442 QueryFontMap (dw
, position
)
448 f
= LookupFontByPosition (dw
, position
);
456 LoadFont (dw
, position
, size
)
463 font
= QueryFont (dw
, position
, size
);
464 dw
->dvi
.font_number
= position
;
465 dw
->dvi
.font_size
= size
;
467 XSetFont (XtDisplay (dw
), dw
->dvi
.normal_GC
, font
->fid
);
475 c-continued-statement-offset: 8
479 c-tab-always-indent: nil