2 * Adobe Font Metric (AFM) file parsing
3 * See http://partners.adobe.com/asn/developer/PDFS/TN/5004.AFM_Spec.pdf
5 * Copyright 1998 Huw D M Davies
13 #include "winnt.h" /* HEAP_ZERO_MEMORY */
16 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(psdrv
);
22 /* ptr to fonts for which we have afm files */
23 FONTFAMILY
*PSDRV_AFMFontList
= NULL
;
26 /***********************************************************
28 * PSDRV_AFMGetCharMetrics
30 * Parses CharMetric section of AFM file.
32 * Actually only collects the widths of numbered chars and puts then in
35 static void PSDRV_AFMGetCharMetrics(AFM
*afm
, FILE *fp
)
37 unsigned char line
[256], valbuf
[256];
38 unsigned char *cp
, *item
, *value
, *curpos
, *endpos
;
42 afm
->Metrics
= metric
= HeapAlloc( PSDRV_Heap
, HEAP_ZERO_MEMORY
,
43 afm
->NumofMetrics
* sizeof(AFMMETRICS
) );
44 for(i
= 0; i
< afm
->NumofMetrics
; i
++, metric
++) {
47 if(!fgets(line
, sizeof(line
), fp
)) {
48 ERR("Unexpected EOF\n");
51 cp
= line
+ strlen(line
);
55 } while(cp
>= line
&& isspace(*cp
));
63 value
= strpbrk(item
, " \t");
64 if (!value
) { ERR("No whitespace found.\n");return;}
65 while(isspace(*value
))
67 cp
= endpos
= strchr(value
, ';');
68 if (!cp
) { ERR("missing ;, failed. [%s]\n", line
); return; }
71 memcpy(valbuf
, value
, cp
- value
+ 1);
72 valbuf
[cp
- value
+ 1] = '\0';
75 if(!strncmp(item
, "C ", 2)) {
76 value
= strchr(item
, ' ');
77 sscanf(value
, " %d", &metric
->C
);
79 } else if(!strncmp(item
, "CH ", 3)) {
80 value
= strrchr(item
, ' ');
81 sscanf(value
, " %x", &metric
->C
);
84 else if(!strncmp("WX ", item
, 3) || !strncmp("W0X ", item
, 4)) {
85 sscanf(value
, "%f", &metric
->WX
);
86 if(metric
->C
>= 0 && metric
->C
<= 0xff)
87 afm
->CharWidths
[metric
->C
] = metric
->WX
;
90 else if(!strncmp("N ", item
, 2)) {
91 strncpy( metric
->N
, value
, sizeof(metric
->N
) );
94 else if(!strncmp("B ", item
, 2)) {
95 sscanf(value
, "%f%f%f%f", &metric
->B
.llx
, &metric
->B
.lly
,
96 &metric
->B
.urx
, &metric
->B
.ury
);
98 /* Store height of Aring to use as lfHeight */
99 if(metric
->N
&& !strncmp(metric
->N
, "Aring", 5))
100 afm
->FullAscender
= metric
->B
.ury
;
103 /* Ligatures go here... */
108 TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
109 metric
->N
, metric
->WX
, metric
->B
.llx
, metric
->B
.lly
,
110 metric
->B
.urx
, metric
->B
.ury
);
116 /***********************************************************
120 * Fills out an AFM structure and associated substructures (see psdrv.h)
121 * for a given AFM file. All memory is allocated from the process heap.
122 * Returns a ptr to the AFM structure or NULL on error.
124 * This is not complete (we don't handle kerning yet) and not efficient
126 static AFM
*PSDRV_AFMParse(char const *file
)
129 unsigned char buf
[256];
130 unsigned char *value
;
136 TRACE("parsing '%s'\n", file
);
138 if((fp
= fopen(file
, "r")) == NULL
) {
139 MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file
);
143 afm
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
, sizeof(AFM
));
150 while ( ( c
= fgetc ( fp
) ) != EOF
) {
152 if ( *cp
== '\r' || *cp
== '\n' || cp
- buf
== sizeof(buf
)-2 ) {
162 cp
= buf
+ strlen(buf
);
166 } while(cp
> buf
&& isspace(*cp
));
170 if ( afmfile
== 0 && strncmp ( buf
, "StartFontMetrics", 16 ) )
174 value
= strchr(buf
, ' ');
176 while(isspace(*value
))
179 if(!strncmp("FontName", buf
, 8)) {
180 afm
->FontName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
184 if(!strncmp("FullName", buf
, 8)) {
185 afm
->FullName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
189 if(!strncmp("FamilyName", buf
, 10)) {
190 afm
->FamilyName
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
194 if(!strncmp("Weight", buf
, 6)) {
195 if(!strncmp("Roman", value
, 5) || !strncmp("Medium", value
, 6)
196 || !strncmp("Book", value
, 4) || !strncmp("Regular", value
, 7)
197 || !strncmp("Normal", value
, 6))
198 afm
->Weight
= FW_NORMAL
;
199 else if(!strncmp("Demi", value
, 4))
200 afm
->Weight
= FW_DEMIBOLD
;
201 else if(!strncmp("Bold", value
, 4))
202 afm
->Weight
= FW_BOLD
;
203 else if(!strncmp("Light", value
, 5))
204 afm
->Weight
= FW_LIGHT
;
205 else if(!strncmp("Black", value
, 5))
206 afm
->Weight
= FW_BLACK
;
208 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
210 afm
->Weight
= FW_NORMAL
;
215 if(!strncmp("ItalicAngle", buf
, 11)) {
216 sscanf(value
, "%f", &(afm
->ItalicAngle
));
220 if(!strncmp("IsFixedPitch", buf
, 12)) {
221 if(!strncasecmp("false", value
, 5))
222 afm
->IsFixedPitch
= FALSE
;
224 afm
->IsFixedPitch
= TRUE
;
228 if(!strncmp("FontBBox", buf
, 8)) {
229 sscanf(value
, "%f %f %f %f", &(afm
->FontBBox
.llx
),
230 &(afm
->FontBBox
.lly
), &(afm
->FontBBox
.urx
),
231 &(afm
->FontBBox
.ury
) );
235 if(!strncmp("UnderlinePosition", buf
, 17)) {
236 sscanf(value
, "%f", &(afm
->UnderlinePosition
) );
240 if(!strncmp("UnderlineThickness", buf
, 18)) {
241 sscanf(value
, "%f", &(afm
->UnderlineThickness
) );
245 if(!strncmp("CapHeight", buf
, 9)) {
246 sscanf(value
, "%f", &(afm
->CapHeight
) );
250 if(!strncmp("XHeight", buf
, 7)) {
251 sscanf(value
, "%f", &(afm
->XHeight
) );
255 if(!strncmp("Ascender", buf
, 8)) {
256 sscanf(value
, "%f", &(afm
->Ascender
) );
260 if(!strncmp("Descender", buf
, 9)) {
261 sscanf(value
, "%f", &(afm
->Descender
) );
265 if(!strncmp("StartCharMetrics", buf
, 16)) {
266 sscanf(value
, "%d", &(afm
->NumofMetrics
) );
267 PSDRV_AFMGetCharMetrics(afm
, fp
);
271 if(!strncmp("EncodingScheme", buf
, 14)) {
272 afm
->EncodingScheme
= HEAP_strdupA(PSDRV_Heap
, 0, value
);
280 HeapFree ( PSDRV_Heap
, 0, afm
);
284 if(afm
->FontName
== NULL
) {
285 WARN("%s contains no FontName.\n", file
);
286 afm
->FontName
= HEAP_strdupA(PSDRV_Heap
, 0, "nofont");
288 if(afm
->FullName
== NULL
)
289 afm
->FullName
= HEAP_strdupA(PSDRV_Heap
, 0, afm
->FontName
);
290 if(afm
->FamilyName
== NULL
)
291 afm
->FamilyName
= HEAP_strdupA(PSDRV_Heap
, 0, afm
->FontName
);
292 if(afm
->Ascender
== 0.0)
293 afm
->Ascender
= afm
->FontBBox
.ury
;
294 if(afm
->Descender
== 0.0)
295 afm
->Descender
= afm
->FontBBox
.lly
;
296 if(afm
->FullAscender
== 0.0)
297 afm
->FullAscender
= afm
->Ascender
;
299 afm
->Weight
= FW_NORMAL
;
304 /***********************************************************
308 * Frees the family and afmlistentry structures in list head
310 void PSDRV_FreeAFMList( FONTFAMILY
*head
)
312 AFMLISTENTRY
*afmle
, *nexta
;
313 FONTFAMILY
*family
, *nextf
;
315 for(nextf
= family
= head
; nextf
; family
= nextf
) {
316 for(nexta
= afmle
= family
->afmlist
; nexta
; afmle
= nexta
) {
318 HeapFree( PSDRV_Heap
, 0, afmle
);
320 nextf
= family
->next
;
321 HeapFree( PSDRV_Heap
, 0, family
);
327 /***********************************************************
329 * PSDRV_FindAFMinList
330 * Returns ptr to an AFM if name (which is a PS font name) exists in list
333 AFM
*PSDRV_FindAFMinList(FONTFAMILY
*head
, char *name
)
338 for(family
= head
; family
; family
= family
->next
) {
339 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
340 if(!strcmp(afmle
->afm
->FontName
, name
))
347 /***********************************************************
351 * Adds an afm to the list whose head is pointed to by head. Creates new
352 * family node if necessary and always creates a new AFMLISTENTRY.
354 void PSDRV_AddAFMtoList(FONTFAMILY
**head
, AFM
*afm
)
356 FONTFAMILY
*family
= *head
;
357 FONTFAMILY
**insert
= head
;
358 AFMLISTENTRY
*tmpafmle
, *newafmle
;
360 newafmle
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
,
365 if(!strcmp(family
->FamilyName
, afm
->FamilyName
))
367 insert
= &(family
->next
);
368 family
= family
->next
;
372 family
= HeapAlloc(PSDRV_Heap
, HEAP_ZERO_MEMORY
,
375 family
->FamilyName
= HEAP_strdupA(PSDRV_Heap
, 0,
377 family
->afmlist
= newafmle
;
381 tmpafmle
= family
->afmlist
;
382 while(tmpafmle
->next
)
383 tmpafmle
= tmpafmle
->next
;
385 tmpafmle
->next
= newafmle
;
390 /**********************************************************
392 * PSDRV_ReencodeCharWidths
394 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
397 static void PSDRV_ReencodeCharWidths(AFM
*afm
)
402 for(i
= 0; i
< 256; i
++) {
405 if(PSDRV_ANSIVector
[i
] == NULL
) {
406 afm
->CharWidths
[i
] = 0.0;
409 for (j
= 0, metric
= afm
->Metrics
; j
< afm
->NumofMetrics
; j
++, metric
++) {
410 if(!strcmp(metric
->N
, PSDRV_ANSIVector
[i
])) {
411 afm
->CharWidths
[i
] = metric
->WX
;
415 if(j
== afm
->NumofMetrics
) {
416 WARN("Couldn't find glyph '%s' in font '%s'\n",
417 PSDRV_ANSIVector
[i
], afm
->FontName
);
418 afm
->CharWidths
[i
] = 0.0;
425 /***********************************************************
430 static void PSDRV_DumpFontList(void)
435 for(family
= PSDRV_AFMFontList
; family
; family
= family
->next
) {
436 TRACE("Family '%s'\n", family
->FamilyName
);
437 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
438 TRACE("\tFontName '%s'\n", afmle
->afm
->FontName
);
445 /***********************************************************
447 * PSDRV_GetFontMetrics
449 * Only exported function in this file. Parses all afm files listed in
450 * [afmfiles] of wine.conf .
453 static void PSDRV_ReadAFMDir(const char* afmdir
) {
457 dir
= opendir(afmdir
);
460 while ((dent
=readdir(dir
))) {
461 if (strstr(dent
->d_name
,".afm")) {
464 afmfn
=(char*)HeapAlloc(GetProcessHeap(),0,strlen(afmdir
)+strlen(dent
->d_name
)+2);
465 strcpy(afmfn
,afmdir
);
467 strcat(afmfn
,dent
->d_name
);
468 TRACE("loading AFM %s\n",afmfn
);
469 afm
= PSDRV_AFMParse(afmfn
);
471 if(afm
->EncodingScheme
&&
472 !strcmp(afm
->EncodingScheme
,"AdobeStandardEncoding")) {
473 PSDRV_ReencodeCharWidths(afm
);
475 PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
);
477 HeapFree(GetProcessHeap(),0,afmfn
);
484 BOOL
PSDRV_GetFontMetrics(void)
490 while (PROFILE_EnumWineIniString( "afmfiles", idx
++, key
, sizeof(key
), value
, sizeof(value
)))
492 AFM
* afm
= PSDRV_AFMParse(value
);
495 if(afm
->EncodingScheme
&&
496 !strcmp(afm
->EncodingScheme
, "AdobeStandardEncoding")) {
497 PSDRV_ReencodeCharWidths(afm
);
499 PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
);
503 for (idx
= 0; PROFILE_EnumWineIniString ("afmdirs", idx
, key
, sizeof (key
),
504 value
, sizeof (value
)); ++idx
)
505 PSDRV_ReadAFMDir (value
);
507 PSDRV_DumpFontList();