2 * Adobe Font Metric (AFM) file parsing
3 * See http://www.adobe.com/supportservice/devrelations/PDFS/TN/5004.AFM_Spec.pdf
5 * Copyright 1998 Huw D M Davies
10 #include "winnt.h" /* HEAP_ZERO_MEMORY */
16 DEFAULT_DEBUG_CHANNEL(psdrv
)
19 /* ptr to fonts for which we have afm files */
20 FONTFAMILY
*PSDRV_AFMFontList
= NULL
;
23 /***********************************************************
25 * PSDRV_AFMGetCharMetrics
27 * Parses CharMetric section of AFM file.
29 * Actually only collects the widths of numbered chars and puts then in
32 static void PSDRV_AFMGetCharMetrics(AFM
*afm
, FILE *fp
)
34 char line
[256], valbuf
[256];
35 char *cp
, *item
, *value
, *curpos
, *endpos
;
37 AFMMETRICS
**insert
= &afm
->Metrics
, *metric
;
39 for(i
= 0; i
< afm
->NumofMetrics
; i
++) {
41 if(!fgets(line
, sizeof(line
), fp
)) {
42 ERR(psdrv
, "Unexpected EOF\n");
46 metric
= *insert
= HeapAlloc( PSDRV_Heap
, HEAP_ZERO_MEMORY
,
48 insert
= &metric
->next
;
50 cp
= line
+ strlen(line
);
54 } while(cp
> line
&& isspace(*cp
));
61 value
= strpbrk(item
, " \t");
62 while(isspace(*value
))
64 cp
= endpos
= strchr(value
, ';');
67 memcpy(valbuf
, value
, cp
- value
+ 1);
68 valbuf
[cp
- value
+ 1] = '\0';
71 if(!strncmp(item
, "C ", 2)) {
72 value
= strchr(item
, ' ');
73 sscanf(value
, " %d", &metric
->C
);
75 } else if(!strncmp(item
, "CH ", 3)) {
76 value
= strrchr(item
, ' ');
77 sscanf(value
, " %x", &metric
->C
);
80 else if(!strncmp("WX ", item
, 3) || !strncmp("W0X ", item
, 4)) {
81 sscanf(value
, "%f", &metric
->WX
);
82 if(metric
->C
>= 0 && metric
->C
<= 0xff)
83 afm
->CharWidths
[metric
->C
] = metric
->WX
;
86 else if(!strncmp("N ", item
, 2)) {
87 metric
->N
= HEAP_strdupA( PSDRV_Heap
, 0, value
);
90 else if(!strncmp("B ", item
, 2)) {
91 sscanf(value
, "%f%f%f%f", &metric
->B
.llx
, &metric
->B
.lly
,
92 &metric
->B
.urx
, &metric
->B
.ury
);
94 /* Store height of Aring to use as lfHeight */
95 if(metric
->N
&& !strncmp(metric
->N
, "Aring", 5))
96 afm
->FullAscender
= metric
->B
.ury
;
99 /* Ligatures go here... */
105 TRACE(psdrv
, "Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
106 metric
->N
, metric
->WX
, metric
->B
.llx
, metric
->B
.lly
,
107 metric
->B
.urx
, metric
->B
.ury
);
114 /***********************************************************
118 * Fills out an AFM structure and associated substructures (see psdrv.h)
119 * for a given AFM file. All memory is allocated from the process heap.
120 * Returns a ptr to the AFM structure or NULL on error.
122 * This is not complete (we don't handle kerning yet) and not efficient
124 static AFM
*PSDRV_AFMParse(char const *file
)
132 TRACE(psdrv
, "parsing '%s'\n", file
);
134 if((fp
= fopen(file
, "r")) == NULL
) {
135 MSG("Can't open AFM file '%s'. Please check wine.conf .\n", file
);
139 afm
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
, sizeof(AFM
));
145 while(fgets(buf
, sizeof(buf
), fp
)) {
146 cp
= buf
+ strlen(buf
);
150 } while(cp
> buf
&& isspace(*cp
));
152 value
= strchr(buf
, ' ');
154 while(isspace(*value
))
157 if(!strncmp("FontName", buf
, 8)) {
158 afm
->FontName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
162 if(!strncmp("FullName", buf
, 8)) {
163 afm
->FullName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
167 if(!strncmp("FamilyName", buf
, 10)) {
168 afm
->FamilyName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
172 if(!strncmp("Weight", buf
, 6)) {
173 if(!strncmp("Roman", value
, 5) || !strncmp("Medium", value
, 6)
174 || !strncmp("Book", value
, 4) || !strncmp("Regular", value
, 7)
175 || !strncmp("Normal", value
, 6))
176 afm
->Weight
= FW_NORMAL
;
177 else if(!strncmp("Demi", value
, 4))
178 afm
->Weight
= FW_DEMIBOLD
;
179 else if(!strncmp("Bold", value
, 4))
180 afm
->Weight
= FW_BOLD
;
181 else if(!strncmp("Light", value
, 5))
182 afm
->Weight
= FW_LIGHT
;
184 FIXME(psdrv
, "Unkown AFM Weight '%s'\n", value
);
185 afm
->Weight
= FW_NORMAL
;
190 if(!strncmp("ItalicAngle", buf
, 11)) {
191 sscanf(value
, "%f", &(afm
->ItalicAngle
));
195 if(!strncmp("IsFixedPitch", buf
, 12)) {
196 if(!strncasecmp("false", value
, 5))
197 afm
->IsFixedPitch
= FALSE
;
199 afm
->IsFixedPitch
= TRUE
;
203 if(!strncmp("FontBBox", buf
, 8)) {
204 sscanf(value
, "%f %f %f %f", &(afm
->FontBBox
.llx
),
205 &(afm
->FontBBox
.lly
), &(afm
->FontBBox
.urx
),
206 &(afm
->FontBBox
.ury
) );
210 if(!strncmp("UnderlinePosition", buf
, 17)) {
211 sscanf(value
, "%f", &(afm
->UnderlinePosition
) );
215 if(!strncmp("UnderlineThickness", buf
, 18)) {
216 sscanf(value
, "%f", &(afm
->UnderlineThickness
) );
220 if(!strncmp("CapHeight", buf
, 9)) {
221 sscanf(value
, "%f", &(afm
->CapHeight
) );
225 if(!strncmp("XHeight", buf
, 7)) {
226 sscanf(value
, "%f", &(afm
->XHeight
) );
230 if(!strncmp("Ascender", buf
, 8)) {
231 sscanf(value
, "%f", &(afm
->Ascender
) );
235 if(!strncmp("Descender", buf
, 9)) {
236 sscanf(value
, "%f", &(afm
->Descender
) );
240 if(!strncmp("StartCharMetrics", buf
, 16)) {
241 sscanf(value
, "%d", &(afm
->NumofMetrics
) );
242 PSDRV_AFMGetCharMetrics(afm
, fp
);
246 if(!strncmp("EncodingScheme", buf
, 14)) {
247 afm
->EncodingScheme
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
254 if(afm
->Ascender
== 0.0)
255 afm
->Ascender
= afm
->FontBBox
.ury
;
256 if(afm
->Descender
== 0.0)
257 afm
->Descender
= afm
->FontBBox
.lly
;
258 if(afm
->FullAscender
== 0.0)
259 afm
->FullAscender
= afm
->Ascender
;
264 /***********************************************************
268 * Frees the family and afmlistentry structures in list head
270 void PSDRV_FreeAFMList( FONTFAMILY
*head
)
272 AFMLISTENTRY
*afmle
, *nexta
;
273 FONTFAMILY
*family
, *nextf
;
275 for(nextf
= family
= head
; nextf
; family
= nextf
) {
276 for(nexta
= afmle
= family
->afmlist
; nexta
; afmle
= nexta
) {
278 HeapFree( PSDRV_Heap
, 0, afmle
);
280 nextf
= family
->next
;
281 HeapFree( PSDRV_Heap
, 0, family
);
287 /***********************************************************
289 * PSDRV_FindAFMinList
290 * Returns ptr to an AFM if name (which is a PS font name) exists in list
293 AFM
*PSDRV_FindAFMinList(FONTFAMILY
*head
, char *name
)
298 for(family
= head
; family
; family
= family
->next
) {
299 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
300 if(!strcmp(afmle
->afm
->FontName
, name
))
307 /***********************************************************
311 * Adds an afm to the list whose head is pointed to by head. Creates new
312 * family node if necessary and always creates a new AFMLISTENTRY.
314 void PSDRV_AddAFMtoList(FONTFAMILY
**head
, AFM
*afm
)
316 FONTFAMILY
*family
= *head
;
317 FONTFAMILY
**insert
= head
;
318 AFMLISTENTRY
*tmpafmle
, *newafmle
;
320 newafmle
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
,
325 if(!strcmp(family
->FamilyName
, afm
->FamilyName
))
327 insert
= &(family
->next
);
328 family
= family
->next
;
332 family
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
,
335 family
->FamilyName
= HEAP_strdupA(PSDRV_Heap
, 0,
337 family
->afmlist
= newafmle
;
341 tmpafmle
= family
->afmlist
;
342 while(tmpafmle
->next
)
343 tmpafmle
= tmpafmle
->next
;
345 tmpafmle
->next
= newafmle
;
350 /**********************************************************
352 * PSDRV_ReencodeCharWidths
354 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
357 static void PSDRV_ReencodeCharWidths(AFM
*afm
)
362 for(i
= 0; i
< 256; i
++) {
365 if(PSDRV_ANSIVector
[i
] == NULL
) {
366 afm
->CharWidths
[i
] = 0.0;
369 for(metric
= afm
->Metrics
; metric
; metric
= metric
->next
) {
370 if(!strcmp(metric
->N
, PSDRV_ANSIVector
[i
])) {
371 afm
->CharWidths
[i
] = metric
->WX
;
376 WARN(psdrv
, "Couldn't find glyph '%s' in font '%s'\n",
377 PSDRV_ANSIVector
[i
], afm
->FontName
);
378 afm
->CharWidths
[i
] = 0.0;
384 /***********************************************************
386 * PSDRV_afmfilesCallback
388 * Callback for PROFILE_EnumerateWineIniSection
389 * Try to parse AFM file `value', alter the CharWidths field of afm struct if
390 * the font is using AdobeStandardEncoding to correspond to WinANSI, then add
391 * afm to system font list.
393 static void PSDRV_afmfilesCallback(char const *key
, char const *value
,
398 afm
= PSDRV_AFMParse(value
);
400 if(afm
->EncodingScheme
&&
401 !strcmp(afm
->EncodingScheme
, "AdobeStandardEncoding")) {
402 PSDRV_ReencodeCharWidths(afm
);
404 PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
);
410 /***********************************************************
415 static void PSDRV_DumpFontList(void)
420 for(family
= PSDRV_AFMFontList
; family
; family
= family
->next
) {
421 TRACE(psdrv
, "Family '%s'\n", family
->FamilyName
);
422 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
423 TRACE(psdrv
, "\tFontName '%s'\n", afmle
->afm
->FontName
);
430 /***********************************************************
432 * PSDRV_GetFontMetrics
434 * Only exported function in this file. Parses all afm files listed in
435 * [afmfiles] of wine.conf .
437 BOOL
PSDRV_GetFontMetrics(void)
439 PROFILE_EnumerateWineIniSection( "afmfiles", PSDRV_afmfilesCallback
, NULL
);
440 PSDRV_DumpFontList();