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
10 #include <stdlib.h> /* qsort() & bsearch() */
14 #include <limits.h> /* INT_MIN */
15 #include <float.h> /* FLT_MAX */
16 #include "winnt.h" /* HEAP_ZERO_MEMORY */
19 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(psdrv);
25 /* ptr to fonts for which we have afm files */
26 FONTFAMILY *PSDRV_AFMFontList = NULL;
28 /* qsort/bsearch callback functions */
29 typedef int (*compar_callback_fn) (const void *, const void *);
31 /*******************************************************************************
34 * Check an AFMMETRICS structure to make sure all elements have been properly
35 * filled in. (Don't check UV or L.)
38 static const AFMMETRICS badMetrics =
44 { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX }, /* B */
48 inline static BOOL CheckMetrics(const AFMMETRICS *metrics)
50 if ( metrics->C == badMetrics.C ||
51 metrics->WX == badMetrics.WX ||
52 metrics->N == badMetrics.N ||
53 metrics->B.llx == badMetrics.B.llx ||
54 metrics->B.lly == badMetrics.B.lly ||
55 metrics->B.urx == badMetrics.B.urx ||
56 metrics->B.ury == badMetrics.B.ury )
62 /*******************************************************************************
65 * Free an AFM structure and any subsidiary objects that have been allocated.
66 * AFM must have been allocated with HEAP_ZERO_MEMORY.
69 static void FreeAFM(AFM *afm)
71 if (afm->FontName != NULL)
72 HeapFree(PSDRV_Heap, 0, afm->FontName);
73 if (afm->FullName != NULL)
74 HeapFree(PSDRV_Heap, 0, afm->FullName);
75 if (afm->FamilyName != NULL)
76 HeapFree(PSDRV_Heap, 0, afm->FamilyName);
77 if (afm->EncodingScheme != NULL)
78 HeapFree(PSDRV_Heap, 0, afm->EncodingScheme);
79 if (afm->Metrics != NULL)
80 HeapFree(PSDRV_Heap, 0, afm->Metrics);
82 HeapFree(PSDRV_Heap, 0, afm);
85 /***********************************************************
87 * PSDRV_AFMGetCharMetrics
89 * Parses CharMetric section of AFM file.
91 * Actually only collects the widths of numbered chars and puts then in
94 static BOOL PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
96 unsigned char line[256], valbuf[256];
97 unsigned char *cp, *item, *value, *curpos, *endpos;
101 afm->Metrics = metric = HeapAlloc( PSDRV_Heap, 0,
102 afm->NumofMetrics * sizeof(AFMMETRICS) );
106 for(i = 0; i < afm->NumofMetrics; i++, metric++) {
108 *metric = badMetrics;
111 if(!fgets(line, sizeof(line), fp)) {
112 ERR("Unexpected EOF\n");
113 HeapFree(PSDRV_Heap, 0, afm->Metrics);
117 cp = line + strlen(line);
121 } while(cp >= line && isspace(*cp));
127 while(isspace(*item))
129 value = strpbrk(item, " \t");
131 ERR("No whitespace found.\n");
132 HeapFree(PSDRV_Heap, 0, afm->Metrics);
136 while(isspace(*value))
138 cp = endpos = strchr(value, ';');
140 ERR("missing ;, failed. [%s]\n", line);
141 HeapFree(PSDRV_Heap, 0, afm->Metrics);
145 while(isspace(*--cp))
147 memcpy(valbuf, value, cp - value + 1);
148 valbuf[cp - value + 1] = '\0';
151 if(!strncmp(item, "C ", 2)) {
152 value = strchr(item, ' ');
153 sscanf(value, " %d", &metric->C);
155 } else if(!strncmp(item, "CH ", 3)) {
156 value = strrchr(item, ' ');
157 sscanf(value, " %x", &metric->C);
160 else if(!strncmp("WX ", item, 3) || !strncmp("W0X ", item, 4)) {
161 sscanf(value, "%f", &metric->WX);
162 if(metric->C >= 0 && metric->C <= 0xff)
163 afm->CharWidths[metric->C] = metric->WX;
166 else if(!strncmp("N ", item, 2)) {
167 metric->N = PSDRV_GlyphName(value);
170 else if(!strncmp("B ", item, 2)) {
171 sscanf(value, "%f%f%f%f", &metric->B.llx, &metric->B.lly,
172 &metric->B.urx, &metric->B.ury);
174 /* Store height of Aring to use as lfHeight */
175 if(metric->N && !strncmp(metric->N->sz, "Aring", 5))
176 afm->FullAscender = metric->B.ury;
179 /* Ligatures go here... */
184 if (CheckMetrics(metric) == FALSE) {
185 ERR("Error parsing character metrics\n");
186 HeapFree(PSDRV_Heap, 0, afm->Metrics);
191 TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
192 metric->N->sz, metric->WX, metric->B.llx, metric->B.lly,
193 metric->B.urx, metric->B.ury);
199 /*******************************************************************************
202 * Builds a custom encoding vector if necessary. Leaves vector in the same
203 * order as the afm->Metrics array; see SortFontMetrics().
206 static BOOL BuildEncoding(AFM *afm)
212 if (strcmp(afm->EncodingScheme, "FontSpecific") != 0)
214 afm->Encoding = &PSDRV_AdobeGlyphList;
218 uv = HeapAlloc(PSDRV_Heap, 0, sizeof(UNICODEVECTOR) +
219 afm->NumofMetrics * sizeof(UNICODEGLYPH));
224 ug = (UNICODEGLYPH *)(uv + 1);
226 uv->size = afm->NumofMetrics;
228 for (i = 0; i < afm->NumofMetrics; ++i)
230 ug[i].name = afm->Metrics[i].N;
232 if (afm->Metrics[i].C < 0) /* unencoded glyph */
234 WARN("Glyph '%s' in font '%s' has no encoding\n", ug[i].name->sz,
240 ug[i].UV = afm->Metrics[i].C | 0xf000; /* private use area? */
248 /***********************************************************
252 * Fills out an AFM structure and associated substructures (see psdrv.h)
253 * for a given AFM file. All memory is allocated from the process heap.
254 * Returns a ptr to the AFM structure or NULL on error.
256 * This is not complete (we don't handle kerning yet) and not efficient
259 static AFM *PSDRV_AFMParse(char const *file)
262 unsigned char buf[256];
263 unsigned char *value;
269 TRACE("parsing '%s'\n", file);
271 if((fp = fopen(file, "r")) == NULL) {
272 MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
276 afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
283 while ( ( c = fgetc ( fp ) ) != EOF ) {
285 if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
295 cp = buf + strlen(buf);
299 } while(cp > buf && isspace(*cp));
303 if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
307 value = strchr(buf, ' ');
309 while(isspace(*value))
312 if(!strncmp("FontName", buf, 8)) {
313 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
314 if (afm->FontName == NULL) {
322 if(!strncmp("FullName", buf, 8)) {
323 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
324 if (afm->FullName == NULL) {
332 if(!strncmp("FamilyName", buf, 10)) {
333 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
334 if (afm->FamilyName == NULL) {
342 if(!strncmp("Weight", buf, 6)) {
343 if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
344 || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
345 || !strncmp("Normal", value, 6))
346 afm->Weight = FW_NORMAL;
347 else if(!strncmp("Demi", value, 4))
348 afm->Weight = FW_DEMIBOLD;
349 else if(!strncmp("Bold", value, 4))
350 afm->Weight = FW_BOLD;
351 else if(!strncmp("Light", value, 5))
352 afm->Weight = FW_LIGHT;
353 else if(!strncmp("Black", value, 5))
354 afm->Weight = FW_BLACK;
356 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
358 afm->Weight = FW_NORMAL;
363 if(!strncmp("ItalicAngle", buf, 11)) {
364 sscanf(value, "%f", &(afm->ItalicAngle));
368 if(!strncmp("IsFixedPitch", buf, 12)) {
369 if(!strncasecmp("false", value, 5))
370 afm->IsFixedPitch = FALSE;
372 afm->IsFixedPitch = TRUE;
376 if(!strncmp("FontBBox", buf, 8)) {
377 sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx),
378 &(afm->FontBBox.lly), &(afm->FontBBox.urx),
379 &(afm->FontBBox.ury) );
383 if(!strncmp("UnderlinePosition", buf, 17)) {
384 sscanf(value, "%f", &(afm->UnderlinePosition) );
388 if(!strncmp("UnderlineThickness", buf, 18)) {
389 sscanf(value, "%f", &(afm->UnderlineThickness) );
393 if(!strncmp("CapHeight", buf, 9)) {
394 sscanf(value, "%f", &(afm->CapHeight) );
398 if(!strncmp("XHeight", buf, 7)) {
399 sscanf(value, "%f", &(afm->XHeight) );
403 if(!strncmp("Ascender", buf, 8)) {
404 sscanf(value, "%f", &(afm->Ascender) );
408 if(!strncmp("Descender", buf, 9)) {
409 sscanf(value, "%f", &(afm->Descender) );
413 if(!strncmp("StartCharMetrics", buf, 16)) {
414 sscanf(value, "%d", &(afm->NumofMetrics) );
415 if (PSDRV_AFMGetCharMetrics(afm, fp) == FALSE) {
423 if(!strncmp("EncodingScheme", buf, 14)) {
424 afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
425 if (afm->EncodingScheme == NULL) {
437 HeapFree ( PSDRV_Heap, 0, afm );
441 if(afm->FontName == NULL) {
442 WARN("%s contains no FontName.\n", file);
443 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
444 if (afm->FontName == NULL) {
450 if(afm->FullName == NULL)
451 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
452 if(afm->FamilyName == NULL)
453 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
454 if (afm->FullName == NULL || afm->FamilyName == NULL) {
459 if(afm->Ascender == 0.0)
460 afm->Ascender = afm->FontBBox.ury;
461 if(afm->Descender == 0.0)
462 afm->Descender = afm->FontBBox.lly;
463 if(afm->FullAscender == 0.0)
464 afm->FullAscender = afm->Ascender;
466 afm->Weight = FW_NORMAL;
468 if (BuildEncoding(afm) == FALSE)
477 /***********************************************************
481 * Frees the family and afmlistentry structures in list head
483 void PSDRV_FreeAFMList( FONTFAMILY *head )
485 AFMLISTENTRY *afmle, *nexta;
486 FONTFAMILY *family, *nextf;
488 for(nextf = family = head; nextf; family = nextf) {
489 for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
491 HeapFree( PSDRV_Heap, 0, afmle );
493 nextf = family->next;
494 HeapFree( PSDRV_Heap, 0, family );
500 /***********************************************************
502 * PSDRV_FindAFMinList
503 * Returns ptr to an AFM if name (which is a PS font name) exists in list
506 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
511 for(family = head; family; family = family->next) {
512 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
513 if(!strcmp(afmle->afm->FontName, name))
520 /***********************************************************
524 * Adds an afm to the list whose head is pointed to by head. Creates new
525 * family node if necessary and always creates a new AFMLISTENTRY.
527 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
529 FONTFAMILY *family = *head;
530 FONTFAMILY **insert = head;
531 AFMLISTENTRY *tmpafmle, *newafmle;
533 newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
535 if (newafmle == NULL)
541 if(!strcmp(family->FamilyName, afm->FamilyName))
543 insert = &(family->next);
544 family = family->next;
548 family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
550 if (family == NULL) {
551 HeapFree(PSDRV_Heap, 0, newafmle);
555 family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
557 if (family->FamilyName == NULL) {
558 HeapFree(PSDRV_Heap, 0, family);
559 HeapFree(PSDRV_Heap, 0, newafmle);
562 family->afmlist = newafmle;
566 tmpafmle = family->afmlist;
567 while(tmpafmle->next)
568 tmpafmle = tmpafmle->next;
570 tmpafmle->next = newafmle;
575 /**********************************************************
577 * PSDRV_ReencodeCharWidths
579 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
582 static void PSDRV_ReencodeCharWidths(AFM *afm)
587 for(i = 0; i < 256; i++) {
590 if(PSDRV_ANSIVector[i] == NULL) {
591 afm->CharWidths[i] = 0.0;
594 for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
595 if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
596 afm->CharWidths[i] = metric->WX;
600 if(j == afm->NumofMetrics) {
601 WARN("Couldn't find glyph '%s' in font '%s'\n",
602 PSDRV_ANSIVector[i], afm->FontName);
603 afm->CharWidths[i] = 0.0;
610 /***********************************************************
615 static void PSDRV_DumpFontList(void)
620 for(family = PSDRV_AFMFontList; family; family = family->next) {
621 TRACE("Family '%s'\n", family->FamilyName);
622 for(afmle = family->afmlist; afmle; afmle = afmle->next)
626 TRACE("\tFontName '%s' (%i glyphs):\n", afmle->afm->FontName,
627 afmle->afm->NumofMetrics);
629 for (i = 0; i < afmle->afm->NumofMetrics; ++i)
631 TRACE("\t\tU+%.4lX; C %i; N '%s'\n", afmle->afm->Metrics[i].UV,
632 afmle->afm->Metrics[i].C, afmle->afm->Metrics[i].N->sz);
639 /*******************************************************************************
642 * Initializes the UV member of each glyph's AFMMETRICS and sorts each font's
643 * Metrics by Unicode Value.
646 static int UnicodeGlyphByNameIndex(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
648 return a->name->index - b->name->index;
651 static int UnicodeGlyphByUV(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
653 return a->UV - b->UV;
656 static int AFMMetricsByUV(const AFMMETRICS *a, const AFMMETRICS *b)
658 return a->UV - b->UV;
661 static BOOL SortFontMetrics()
663 UNICODEGLYPH *aglCopy = NULL;
664 FONTFAMILY *family = PSDRV_AFMFontList;
666 while (family != NULL)
668 AFMLISTENTRY *afmle = family->afmlist;
670 while (afmle != NULL)
672 AFM *afm = afmle->afm; /* should always be valid */
675 if (afm->Encoding == &PSDRV_AdobeGlyphList)
677 if (aglCopy == NULL) /* do this once, if necessary */
679 aglCopy = HeapAlloc(PSDRV_Heap, 0,
680 PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
684 memcpy(aglCopy, PSDRV_AdobeGlyphList.glyphs,
685 PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
687 qsort(aglCopy, PSDRV_AdobeGlyphList.size,
688 sizeof(UNICODEGLYPH),
689 (compar_callback_fn)UnicodeGlyphByNameIndex);
692 for (i = 0; i < afm->NumofMetrics; ++i)
694 UNICODEGLYPH ug, *pug;
696 ug.name = afm->Metrics[i].N;
699 pug = bsearch(&ug, aglCopy, PSDRV_AdobeGlyphList.size,
700 sizeof(UNICODEGLYPH),
701 (compar_callback_fn)UnicodeGlyphByNameIndex);
704 WARN("Glyph '%s' in font '%s' does not have a UV\n",
705 ug.name->sz, afm->FullName);
706 afm->Metrics[i].UV = -1;
710 afm->Metrics[i].UV = pug->UV;
714 else /* FontSpecific encoding or TrueType font */
716 for (i = 0; i < afm->NumofMetrics; ++i)
717 afm->Metrics[i].UV = afm->Encoding->glyphs[i].UV;
719 /* typecast avoids compiler warning */
720 qsort((void *)(afm->Encoding->glyphs), afm->Encoding->size,
721 sizeof(UNICODEGLYPH),
722 (compar_callback_fn)UnicodeGlyphByUV);
724 for (i = 0; i < afm->Encoding->size; ++i)
725 if (afm->Encoding->glyphs[i].UV >= 0)
728 afm->Encoding->size -= i; /* Ignore unencoded glyphs */
729 afm->Encoding->glyphs += i; /* from now on */
732 qsort(afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
733 (compar_callback_fn)AFMMetricsByUV);
735 for (i = 0; i < afm->NumofMetrics; ++i)
736 if (afm->Metrics[i].UV >= 0)
739 afm->NumofMetrics -= i; /* Ignore unencoded glyphs here too */
745 family = family->next;
749 HeapFree(PSDRV_Heap, 0, aglCopy);
754 /***********************************************************
756 * PSDRV_GetFontMetrics
758 * Parses all afm files listed in [afmfiles] and [afmdirs] of wine.conf
760 * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
761 * about freeing all the memory that's been allocated.
764 static BOOL PSDRV_ReadAFMDir(const char* afmdir) {
768 dir = opendir(afmdir);
771 while ((dent=readdir(dir))) {
772 if (strstr(dent->d_name,".afm")) {
775 afmfn=(char*)HeapAlloc(PSDRV_Heap,0,
776 strlen(afmdir)+strlen(dent->d_name)+2);
781 strcpy(afmfn,afmdir);
783 strcat(afmfn,dent->d_name);
784 TRACE("loading AFM %s\n",afmfn);
785 afm = PSDRV_AFMParse(afmfn);
787 if(afm->EncodingScheme &&
788 !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
789 PSDRV_ReencodeCharWidths(afm);
791 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
798 WARN("Error parsing %s\n", afmfn);
800 HeapFree(PSDRV_Heap,0,afmfn);
806 WARN("Error opening %s\n", afmdir);
812 BOOL PSDRV_GetFontMetrics(void)
818 if (PSDRV_GlyphListInit() != 0)
821 while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key),
822 value, sizeof(value)))
824 AFM* afm = PSDRV_AFMParse(value);
827 if(afm->EncodingScheme &&
828 !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
829 PSDRV_ReencodeCharWidths(afm);
831 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
836 WARN("Error parsing %s\n", value);
840 for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
841 value, sizeof (value)); ++idx)
842 if (PSDRV_ReadAFMDir (value) == FALSE)
845 PSDRV_IndexGlyphList();
846 if (SortFontMetrics() == FALSE)
848 PSDRV_DumpFontList();