1 /*******************************************************************************
2 * Adobe Font Metric (AFM) file parsing functions for Wine PostScript driver.
3 * See http://partners.adobe.com/asn/developer/pdfs/tn/5004.AFM_Spec.pdf.
5 * Copyright 2001 Ian Pilcher
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTE: Many of the functions in this file can return either fatal errors
22 * (memory allocation failure) or non-fatal errors (unusable AFM file).
23 * Fatal errors are indicated by returning FALSE; see individual function
24 * descriptions for how they indicate non-fatal errors.
29 #include "wine/port.h"
40 #include <limits.h> /* INT_MIN */
43 #include <float.h> /* FLT_MAX */
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
55 /*******************************************************************************
58 * Reads a line from a text file into the buffer and trims trailing whitespace.
59 * Can handle DOS and Unix text files, including weird DOS EOF. Returns FALSE
60 * for unexpected I/O errors; otherwise returns TRUE and sets *p_result to
61 * either the number of characters in the returned string or one of the
64 * 0: Blank (or all whitespace) line. This is just a special case
65 * of the normal behavior.
67 * EOF: End of file has been reached.
69 * INT_MIN: Buffer overflow. Returned string is truncated (duh!) and
70 * trailing whitespace is *not* trimmed. Remaining text in
71 * line is discarded. (I.e. the file pointer is positioned at
72 * the beginning of the next line.)
75 static BOOL
ReadLine(FILE *file
, CHAR buffer
[], INT bufsize
, INT
*p_result
)
80 if (fgets(buffer
, bufsize
, file
) == NULL
)
82 if (feof(file
) == 0) /* EOF or error? */
84 ERR("%s\n", strerror(errno
));
92 cp
= strchr(buffer
, '\n');
97 if (i
== bufsize
- 1) /* max possible; was line truncated? */
100 i
= fgetc(file
); /* find the newline or EOF */
101 while (i
!= '\n' && i
!= EOF
);
105 if (feof(file
) == 0) /* EOF or error? */
107 ERR("%s\n", strerror(errno
));
111 WARN("No newline at EOF\n");
117 else /* no newline and not truncated */
119 if (strcmp(buffer
, "\x1a") == 0) /* test for DOS EOF */
125 WARN("No newline at EOF\n");
126 cp
= buffer
+ i
; /* points to \0 where \n should have been */
132 *cp
= '\0'; /* trim trailing whitespace */
134 break; /* don't underflow buffer */
137 while (isspace(*cp
));
139 *p_result
= strlen(buffer
);
143 /*******************************************************************************
146 * Finds a line in the file that begins with the given string. Returns FALSE
147 * for unexpected I/O errors; returns an empty (zero character) string if the
148 * requested line is not found.
150 * NOTE: The file pointer *MUST* be positioned at the beginning of a line when
151 * this function is called. Otherwise, an infinite loop can result.
154 static BOOL
FindLine(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
)
156 INT len
= strlen(key
);
157 LONG start
= ftell(file
);
164 ok
= ReadLine(file
, buffer
, bufsize
, &result
);
168 if (result
> 0 && strncmp(buffer
, key
, len
) == 0)
175 else if (result
== INT_MIN
)
177 WARN("Line beginning '%32s...' is too long; ignoring\n", buffer
);
180 while (ftell(file
) != start
);
182 WARN("Couldn't find line '%s...' in AFM file\n", key
);
187 /*******************************************************************************
190 * Utility function to convert double to float while checking for overflow.
191 * Will also catch strtod overflows, since HUGE_VAL > FLT_MAX (at least on
195 static inline BOOL
DoubleToFloat(float *p_f
, double d
)
197 if (d
> (double)FLT_MAX
|| d
< -(double)FLT_MAX
)
204 /*******************************************************************************
207 * Utility function to add or subtract 0.5 before converting to integer type.
210 static inline float Round(float f
)
212 return (f
>= 0.0) ? (f
+ 0.5) : (f
- 0.5);
215 /*******************************************************************************
218 * Finds and parses a line of the form '<key> <value>', where value is a
219 * number. Sets *p_found to FALSE if a corresponding line cannot be found, or
220 * it cannot be parsed; also sets *p_ret to 0.0, so calling functions can just
221 * skip the check of *p_found if the item is not required.
224 static BOOL
ReadFloat(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
225 FLOAT
*p_ret
, BOOL
*p_found
)
230 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
233 if (buffer
[0] == '\0') /* line not found */
240 cp
= buffer
+ strlen(key
); /* first char after key */
242 d
= strtod(cp
, &end_ptr
);
244 if (end_ptr
== cp
|| errno
!= 0 || DoubleToFloat(p_ret
, d
) == FALSE
)
246 WARN("Error parsing line '%s'\n", buffer
);
256 /*******************************************************************************
259 * See description of ReadFloat.
262 static BOOL
ReadInt(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
263 INT
*p_ret
, BOOL
*p_found
)
268 retval
= ReadFloat(file
, buffer
, bufsize
, key
, &f
, p_found
);
269 if (retval
== FALSE
|| *p_found
== FALSE
)
277 if (f
> (FLOAT
)INT_MAX
|| f
< (FLOAT
)INT_MIN
)
279 WARN("Error parsing line '%s'\n", buffer
);
289 /*******************************************************************************
292 * Returns FALSE on I/O error or memory allocation failure; sets *p_str to NULL
293 * if line cannot be found or can't be parsed.
296 static BOOL
ReadString(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
301 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
304 if (buffer
[0] == '\0')
310 cp
= buffer
+ strlen(key
); /* first char after key */
317 while (isspace(*cp
)) /* find first non-whitespace char */
320 *p_str
= HeapAlloc(PSDRV_Heap
, 0, strlen(cp
) + 1);
328 /*******************************************************************************
331 * Similar to ReadFloat above.
334 static BOOL
ReadBBox(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
340 if (FindLine(file
, buffer
, bufsize
, "FontBBox") == FALSE
)
343 if (buffer
[0] == '\0')
351 cp
= buffer
+ sizeof("FontBBox");
352 d
= strtod(cp
, &end_ptr
);
353 if (end_ptr
== cp
|| errno
!= 0 ||
354 DoubleToFloat(&(afm
->FontBBox
.llx
), d
) == FALSE
)
358 d
= strtod(cp
, &end_ptr
);
359 if (end_ptr
== cp
|| errno
!= 0 ||
360 DoubleToFloat(&(afm
->FontBBox
.lly
), d
) == FALSE
)
364 d
= strtod(cp
, &end_ptr
);
365 if (end_ptr
== cp
|| errno
!= 0
366 || DoubleToFloat(&(afm
->FontBBox
.urx
), d
) == FALSE
)
370 d
= strtod(cp
, &end_ptr
);
371 if (end_ptr
== cp
|| errno
!= 0
372 || DoubleToFloat(&(afm
->FontBBox
.ury
), d
) == FALSE
)
379 WARN("Error parsing line '%s'\n", buffer
);
384 /*******************************************************************************
387 * Finds and parses the 'Weight' line of an AFM file. Only tries to determine
388 * if a font is bold (FW_BOLD) or not (FW_NORMAL) -- ignoring all those cute
389 * little FW_* typedefs in the Win32 doc. AFAICT, this is what the Windows
390 * PostScript driver does.
393 static const struct { LPCSTR keyword
; INT weight
; } afm_weights
[] =
395 { "REGULAR", FW_NORMAL
},
396 { "NORMAL", FW_NORMAL
},
397 { "ROMAN", FW_NORMAL
},
399 { "BOOK", FW_NORMAL
},
400 { "MEDIUM", FW_NORMAL
},
401 { "LIGHT", FW_NORMAL
},
402 { "BLACK", FW_BOLD
},
403 { "HEAVY", FW_BOLD
},
405 { "ULTRA", FW_BOLD
},
406 { "SUPER" , FW_BOLD
},
410 static BOOL
ReadWeight(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
417 if (ReadString(file
, buffer
, bufsize
, "Weight", &sz
) == FALSE
)
426 for (cp
= sz
; *cp
!= '\0'; ++cp
)
429 for (i
= 0; afm_weights
[i
].keyword
!= NULL
; ++i
)
431 if (strstr(sz
, afm_weights
[i
].keyword
) != NULL
)
433 afm
->Weight
= afm_weights
[i
].weight
;
435 HeapFree(PSDRV_Heap
, 0, sz
);
440 WARN("Unknown weight '%s'; treating as Roman\n", sz
);
442 afm
->Weight
= FW_NORMAL
;
444 HeapFree(PSDRV_Heap
, 0, sz
);
448 /*******************************************************************************
452 static BOOL
ReadFixedPitch(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
457 if (ReadString(file
, buffer
, bufsize
, "IsFixedPitch", &sz
) == FALSE
)
466 if (strcasecmp(sz
, "false") == 0)
468 afm
->IsFixedPitch
= FALSE
;
470 HeapFree(PSDRV_Heap
, 0, sz
);
474 if (strcasecmp(sz
, "true") == 0)
476 afm
->IsFixedPitch
= TRUE
;
478 HeapFree(PSDRV_Heap
, 0, sz
);
482 WARN("Can't parse line '%s'\n", buffer
);
485 HeapFree(PSDRV_Heap
, 0, sz
);
489 /*******************************************************************************
492 * Allocates space for the AFM on the driver heap and reads basic font metrics.
493 * Returns FALSE for memory allocation failure; sets *p_afm to NULL if AFM file
497 static BOOL
ReadFontMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
**p_afm
)
502 *p_afm
= afm
= HeapAlloc(PSDRV_Heap
, 0, sizeof(*afm
));
506 retval
= ReadWeight(file
, buffer
, bufsize
, afm
, &found
);
507 if (retval
== FALSE
|| found
== FALSE
)
510 retval
= ReadFloat(file
, buffer
, bufsize
, "ItalicAngle",
511 &(afm
->ItalicAngle
), &found
);
512 if (retval
== FALSE
|| found
== FALSE
)
515 retval
= ReadFixedPitch(file
, buffer
, bufsize
, afm
, &found
);
516 if (retval
== FALSE
|| found
== FALSE
)
519 retval
= ReadBBox(file
, buffer
, bufsize
, afm
, &found
);
520 if (retval
== FALSE
|| found
== FALSE
)
523 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlinePosition",
524 &(afm
->UnderlinePosition
), &found
);
525 if (retval
== FALSE
|| found
== FALSE
)
528 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlineThickness",
529 &(afm
->UnderlineThickness
), &found
);
530 if (retval
== FALSE
|| found
== FALSE
)
533 retval
= ReadFloat(file
, buffer
, bufsize
, "Ascender", /* optional */
534 &(afm
->Ascender
), &found
);
538 retval
= ReadFloat(file
, buffer
, bufsize
, "Descender", /* optional */
539 &(afm
->Descender
), &found
);
543 afm
->WinMetrics
.usUnitsPerEm
= 1000;
544 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->Ascender
);
545 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->Descender
);
547 if (afm
->WinMetrics
.sTypoAscender
== 0)
548 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
550 if (afm
->WinMetrics
.sTypoDescender
== 0)
551 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
553 afm
->WinMetrics
.sTypoLineGap
= 1200 -
554 (afm
->WinMetrics
.sTypoAscender
- afm
->WinMetrics
.sTypoDescender
);
555 if (afm
->WinMetrics
.sTypoLineGap
< 0)
556 afm
->WinMetrics
.sTypoLineGap
= 0;
560 cleanup_afm
: /* handle fatal or non-fatal errors */
561 HeapFree(PSDRV_Heap
, 0, afm
);
566 /*******************************************************************************
569 * Fatal error: return FALSE (none defined)
571 * Non-fatal error: leave metrics->C set to INT_MAX
574 static BOOL
ParseC(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
589 l
= strtol(cp
, &end_ptr
, base
);
590 if (end_ptr
== cp
|| errno
!= 0 || l
> INT_MAX
|| l
< INT_MIN
)
592 WARN("Error parsing character code '%s'\n", sz
);
600 /*******************************************************************************
603 * Fatal error: return FALSE (none defined)
605 * Non-fatal error: leave metrics->WX set to FLT_MAX
608 static BOOL
ParseW(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
629 d
= strtod(cp
, &end_ptr
);
630 if (end_ptr
== cp
|| errno
!= 0 ||
631 DoubleToFloat(&(metrics
->WX
), d
) == FALSE
)
637 /* Make sure that Y component of vector is zero */
639 d
= strtod(cp
, &end_ptr
); /* errno == 0 */
640 if (end_ptr
== cp
|| errno
!= 0 || d
!= 0.0)
642 metrics
->WX
= FLT_MAX
;
649 WARN("Error parsing character width '%s'\n", sz
);
653 /*******************************************************************************
657 * Fatal error: return FALSE (none defined)
659 * Non-fatal error: leave metrics->B.ury set to FLT_MAX
662 static BOOL
ParseB(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
670 d
= strtod(cp
, &end_ptr
);
671 if (end_ptr
== cp
|| errno
!= 0 ||
672 DoubleToFloat(&(metrics
->B
.llx
), d
) == FALSE
)
676 d
= strtod(cp
, &end_ptr
);
677 if (end_ptr
== cp
|| errno
!= 0 ||
678 DoubleToFloat(&(metrics
->B
.lly
), d
) == FALSE
)
682 d
= strtod(cp
, &end_ptr
);
683 if (end_ptr
== cp
|| errno
!= 0 ||
684 DoubleToFloat(&(metrics
->B
.urx
), d
) == FALSE
)
688 d
= strtod(cp
, &end_ptr
);
689 if (end_ptr
== cp
|| errno
!= 0 ||
690 DoubleToFloat(&(metrics
->B
.ury
), d
) == FALSE
)
696 WARN("Error parsing glyph bounding box '%s'\n", sz
);
700 /*******************************************************************************
703 * Fatal error: return FALSE (PSDRV_GlyphName failure)
705 * Non-fatal error: leave metrics-> set to NULL
708 static BOOL
ParseN(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
710 CHAR save
, *cp
, *end_ptr
;
719 while (*end_ptr
!= '\0' && !isspace(*end_ptr
))
724 WARN("Error parsing glyph name '%s'\n", sz
);
731 metrics
->N
= PSDRV_GlyphName(cp
);
732 if (metrics
->N
== NULL
)
739 /*******************************************************************************
742 * Parses the metrics line for a single glyph in an AFM file. Returns FALSE on
743 * fatal error; sets *metrics to 'badmetrics' on non-fatal error.
746 static const OLD_AFMMETRICS badmetrics
=
752 { FLT_MAX
, FLT_MAX
, FLT_MAX
, FLT_MAX
}, /* B */
756 static BOOL
ParseCharMetrics(LPSTR buffer
, INT len
, OLD_AFMMETRICS
*metrics
)
760 *metrics
= badmetrics
;
769 case 'C': if (ParseC(cp
, metrics
) == FALSE
)
773 case 'W': if (ParseW(cp
, metrics
) == FALSE
)
777 case 'N': if (ParseN(cp
, metrics
) == FALSE
)
781 case 'B': if (ParseB(cp
, metrics
) == FALSE
)
786 cp
= strchr(cp
, ';');
789 WARN("No terminating semicolon\n");
796 if (metrics
->C
== INT_MAX
|| metrics
->WX
== FLT_MAX
|| metrics
->N
== NULL
||
797 metrics
->B
.ury
== FLT_MAX
)
799 *metrics
= badmetrics
;
806 /*******************************************************************************
809 * Checks whether Unicode value is part of Microsoft code page 1252
812 static const LONG ansiChars
[21] =
814 0x0152, 0x0153, 0x0160, 0x0161, 0x0178, 0x017d, 0x017e, 0x0192, 0x02c6,
815 0x02c9, 0x02dc, 0x03bc, 0x2013, 0x2014, 0x2026, 0x2030, 0x2039, 0x203a,
816 0x20ac, 0x2122, 0x2219
819 static int cmpUV(const void *a
, const void *b
)
821 return (int)(*((const LONG
*)a
) - *((const LONG
*)b
));
824 static inline BOOL
IsWinANSI(LONG uv
)
826 if ((0x0020 <= uv
&& uv
<= 0x007e) || (0x00a0 <= uv
&& uv
<= 0x00ff) ||
827 (0x2018 <= uv
&& uv
<= 0x201a) || (0x201c <= uv
&& uv
<= 0x201e) ||
828 (0x2020 <= uv
&& uv
<= 0x2022))
831 if (bsearch(&uv
, ansiChars
, 21, sizeof(INT
), cmpUV
) != NULL
)
837 /*******************************************************************************
840 * Determines Unicode value (UV) for each glyph, based on font encoding.
842 * FontSpecific: Usable encodings (0x20 - 0xff) are mapped into the
843 * Unicode private use range U+F020 - U+F0FF.
845 * other: UV determined by glyph name, based on Adobe Glyph List.
847 * Also does some font metric calculations that require UVs to be known.
850 static int UnicodeGlyphByNameIndex(const void *a
, const void *b
)
852 return ((const UNICODEGLYPH
*)a
)->name
->index
-
853 ((const UNICODEGLYPH
*)b
)->name
->index
;
856 static VOID
Unicodify(AFM
*afm
, OLD_AFMMETRICS
*metrics
)
860 if (strcmp(afm
->EncodingScheme
, "FontSpecific") == 0)
862 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
864 if (metrics
[i
].C
>= 0x20 && metrics
[i
].C
<= 0xff)
866 metrics
[i
].UV
= metrics
[i
].C
| 0xf000L
;
870 TRACE("Unencoded glyph '%s'\n", metrics
[i
].N
->sz
);
875 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
876 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
878 else /* non-FontSpecific encoding */
880 UNICODEGLYPH ug
, *p_ug
;
882 PSDRV_IndexGlyphList(); /* for fast searching of glyph names */
884 afm
->WinMetrics
.sAscender
= afm
->WinMetrics
.sDescender
= 0;
886 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
888 ug
.name
= metrics
[i
].N
;
889 p_ug
= bsearch(&ug
, PSDRV_AGLbyName
, PSDRV_AGLbyNameSize
,
890 sizeof(ug
), UnicodeGlyphByNameIndex
);
893 TRACE("Glyph '%s' not in Adobe Glyph List\n", ug
.name
->sz
);
898 metrics
[i
].UV
= p_ug
->UV
;
900 if (IsWinANSI(p_ug
->UV
))
902 SHORT ury
= (SHORT
)Round(metrics
[i
].B
.ury
);
903 SHORT lly
= (SHORT
)Round(metrics
[i
].B
.lly
);
905 if (ury
> afm
->WinMetrics
.sAscender
)
906 afm
->WinMetrics
.sAscender
= ury
;
907 if (lly
< afm
->WinMetrics
.sDescender
)
908 afm
->WinMetrics
.sDescender
= lly
;
913 if (afm
->WinMetrics
.sAscender
== 0)
914 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
915 if (afm
->WinMetrics
.sDescender
== 0)
916 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
919 afm
->WinMetrics
.sLineGap
=
920 1150 - (afm
->WinMetrics
.sAscender
- afm
->WinMetrics
.sDescender
);
921 if (afm
->WinMetrics
.sLineGap
< 0)
922 afm
->WinMetrics
.sLineGap
= 0;
924 afm
->WinMetrics
.usWinAscent
= (afm
->WinMetrics
.sAscender
> 0) ?
925 afm
->WinMetrics
.sAscender
: 0;
926 afm
->WinMetrics
.usWinDescent
= (afm
->WinMetrics
.sDescender
< 0) ?
927 -(afm
->WinMetrics
.sDescender
) : 0;
930 /*******************************************************************************
933 * Reads metrics for all glyphs. *p_metrics will be NULL on non-fatal error.
936 static int OldAFMMetricsByUV(const void *a
, const void *b
)
938 return ((const OLD_AFMMETRICS
*)a
)->UV
- ((const OLD_AFMMETRICS
*)b
)->UV
;
941 static BOOL
ReadCharMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
942 AFMMETRICS
**p_metrics
)
945 OLD_AFMMETRICS
*old_metrics
, *encoded_metrics
;
949 retval
= ReadInt(file
, buffer
, bufsize
, "StartCharMetrics",
950 &(afm
->NumofMetrics
), &found
);
951 if (retval
== FALSE
|| found
== FALSE
)
957 old_metrics
= HeapAlloc(PSDRV_Heap
, 0,
958 afm
->NumofMetrics
* sizeof(*old_metrics
));
959 if (old_metrics
== NULL
)
962 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
964 retval
= ReadLine(file
, buffer
, bufsize
, &len
);
966 goto cleanup_old_metrics
;
970 retval
= ParseCharMetrics(buffer
, len
, old_metrics
+ i
);
971 if (retval
== FALSE
|| old_metrics
[i
].C
== INT_MAX
)
972 goto cleanup_old_metrics
;
982 case INT_MIN
: WARN("Ignoring long line '%32s...'\n", buffer
);
983 goto cleanup_old_metrics
; /* retval == TRUE */
985 case EOF
: WARN("Unexpected EOF\n");
986 goto cleanup_old_metrics
; /* retval == TRUE */
990 Unicodify(afm
, old_metrics
); /* wait until glyph names have been read */
992 qsort(old_metrics
, afm
->NumofMetrics
, sizeof(*old_metrics
),
995 for (i
= 0; old_metrics
[i
].UV
== -1; ++i
); /* count unencoded glyphs */
997 afm
->NumofMetrics
-= i
;
998 encoded_metrics
= old_metrics
+ i
;
1000 afm
->Metrics
= *p_metrics
= metrics
= HeapAlloc(PSDRV_Heap
, 0,
1001 afm
->NumofMetrics
* sizeof(*metrics
));
1002 if (afm
->Metrics
== NULL
)
1003 goto cleanup_old_metrics
; /* retval == TRUE */
1005 for (i
= 0; i
< afm
->NumofMetrics
; ++i
, ++metrics
, ++encoded_metrics
)
1007 metrics
->C
= encoded_metrics
->C
;
1008 metrics
->UV
= encoded_metrics
->UV
;
1009 metrics
->WX
= encoded_metrics
->WX
;
1010 metrics
->N
= encoded_metrics
->N
;
1013 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1015 afm
->WinMetrics
.sAvgCharWidth
= PSDRV_CalcAvgCharWidth(afm
);
1019 cleanup_old_metrics
: /* handle fatal or non-fatal errors */
1020 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1025 /*******************************************************************************
1028 * Builds the AFM for a PostScript font and adds it to the driver font list.
1029 * Returns FALSE only on an unexpected error (memory allocation or I/O error).
1032 static BOOL
BuildAFM(FILE *file
)
1034 CHAR buffer
[258]; /* allow for <cr>, <lf>, and <nul> */
1036 AFMMETRICS
*metrics
;
1037 LPSTR font_name
, full_name
, family_name
, encoding_scheme
;
1040 retval
= ReadFontMetrics(file
, buffer
, sizeof(buffer
), &afm
);
1041 if (retval
== FALSE
|| afm
== NULL
)
1044 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FontName", &font_name
);
1045 if (retval
== FALSE
|| font_name
== NULL
)
1048 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FullName", &full_name
);
1049 if (retval
== FALSE
|| full_name
== NULL
)
1050 goto cleanup_font_name
;
1052 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FamilyName",
1054 if (retval
== FALSE
|| family_name
== NULL
)
1055 goto cleanup_full_name
;
1057 retval
= ReadString(file
, buffer
, sizeof(buffer
), "EncodingScheme",
1059 if (retval
== FALSE
|| encoding_scheme
== NULL
)
1060 goto cleanup_family_name
;
1062 afm
->FontName
= font_name
;
1063 afm
->FullName
= full_name
;
1064 afm
->FamilyName
= family_name
;
1065 afm
->EncodingScheme
= encoding_scheme
;
1067 retval
= ReadCharMetrics(file
, buffer
, sizeof(buffer
), afm
, &metrics
);
1068 if (retval
== FALSE
|| metrics
== FALSE
)
1069 goto cleanup_encoding_scheme
;
1071 retval
= PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
, &added
);
1072 if (retval
== FALSE
|| added
== FALSE
)
1073 goto cleanup_encoding_scheme
;
1077 /* clean up after fatal or non-fatal errors */
1079 cleanup_encoding_scheme
:
1080 HeapFree(PSDRV_Heap
, 0, encoding_scheme
);
1081 cleanup_family_name
:
1082 HeapFree(PSDRV_Heap
, 0, family_name
);
1084 HeapFree(PSDRV_Heap
, 0, full_name
);
1086 HeapFree(PSDRV_Heap
, 0, font_name
);
1088 HeapFree(PSDRV_Heap
, 0, afm
);
1093 /*******************************************************************************
1096 * Reads font metrics from Type 1 AFM file. Only returns FALSE for
1097 * unexpected errors (memory allocation or I/O).
1100 static BOOL
ReadAFMFile(LPCSTR filename
)
1105 TRACE("%s\n", filename
);
1107 f
= fopen(filename
, "r");
1110 WARN("%s: %s\n", filename
, strerror(errno
));
1114 retval
= BuildAFM(f
);
1120 /*******************************************************************************
1123 * Reads all Type 1 AFM files in a directory.
1126 static BOOL
ReadAFMDir(LPCSTR dirname
)
1128 struct dirent
*dent
;
1132 dir
= opendir(dirname
);
1135 WARN("%s: %s\n", dirname
, strerror(errno
));
1139 while ((dent
= readdir(dir
)) != NULL
)
1141 CHAR
*file_extension
= strchr(dent
->d_name
, '.');
1144 if (file_extension
== NULL
|| strcasecmp(file_extension
, ".afm") != 0)
1147 fn_len
= snprintf(filename
, 256, "%s/%s", dirname
, dent
->d_name
);
1148 if (fn_len
< 0 || fn_len
> sizeof(filename
) - 1)
1150 WARN("Path '%s/%s' is too long\n", dirname
, dent
->d_name
);
1154 if (ReadAFMFile(filename
) == FALSE
)
1165 /*******************************************************************************
1166 * PSDRV_GetType1Metrics
1168 * Reads font metrics from Type 1 AFM font files in directories listed in the
1169 * [afmdirs] section of the Wine configuration file.
1171 * If this function fails (returns FALSE), the driver will fail to initialize
1172 * and the driver heap will be destroyed, so it's not necessary to HeapFree
1173 * everything in that event.
1176 BOOL
PSDRV_GetType1Metrics(void)
1178 static const WCHAR pathW
[] = {'A','F','M','P','a','t','h',0};
1184 /* @@ Wine registry key: HKCU\Software\Wine\Fonts */
1185 if (RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Fonts", &hkey
) != ERROR_SUCCESS
)
1188 if (RegQueryValueExW( hkey
, pathW
, NULL
, NULL
, NULL
, &len
) == ERROR_SUCCESS
)
1190 len
+= sizeof(WCHAR
);
1191 valueW
= HeapAlloc( PSDRV_Heap
, 0, len
);
1192 if (RegQueryValueExW( hkey
, pathW
, NULL
, NULL
, (LPBYTE
)valueW
, &len
) == ERROR_SUCCESS
)
1194 len
= WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, NULL
, 0, NULL
, NULL
);
1195 valueA
= HeapAlloc( PSDRV_Heap
, 0, len
);
1196 WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, len
, NULL
, NULL
);
1197 TRACE( "got AFM font path %s\n", debugstr_a(valueA
) );
1201 LPSTR next
= strchr( ptr
, ':' );
1202 if (next
) *next
++ = 0;
1203 if (!ReadAFMDir( ptr
))
1210 HeapFree( PSDRV_Heap
, 0, valueA
);
1212 HeapFree( PSDRV_Heap
, 0, valueW
);