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.
35 #include <limits.h> /* INT_MIN */
36 #include <float.h> /* FLT_MAX */
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
49 /*******************************************************************************
52 * Reads a line from a text file into the buffer and trims trailing whitespace.
53 * Can handle DOS and Unix text files, including weird DOS EOF. Returns FALSE
54 * for unexpected I/O errors; otherwise returns TRUE and sets *p_result to
55 * either the number of characters in the returned string or one of the
58 * 0: Blank (or all whitespace) line. This is just a special case
59 * of the normal behavior.
61 * EOF: End of file has been reached.
63 * INT_MIN: Buffer overflow. Returned string is truncated (duh!) and
64 * trailing whitespace is *not* trimmed. Remaining text in
65 * line is discarded. (I.e. the file pointer is positioned at
66 * the beginning of the next line.)
69 static BOOL
ReadLine(FILE *file
, CHAR buffer
[], INT bufsize
, INT
*p_result
)
74 if (fgets(buffer
, bufsize
, file
) == NULL
)
76 if (feof(file
) == 0) /* EOF or error? */
78 ERR("%s\n", strerror(errno
));
86 cp
= strchr(buffer
, '\n');
91 if (i
== bufsize
- 1) /* max possible; was line truncated? */
94 i
= fgetc(file
); /* find the newline or EOF */
95 while (i
!= '\n' && i
!= EOF
);
99 if (feof(file
) == 0) /* EOF or error? */
101 ERR("%s\n", strerror(errno
));
105 WARN("No newline at EOF\n");
111 else /* no newline and not truncated */
113 if (strcmp(buffer
, "\x1a") == 0) /* test for DOS EOF */
119 WARN("No newline at EOF\n");
120 cp
= buffer
+ i
; /* points to \0 where \n should have been */
126 *cp
= '\0'; /* trim trailing whitespace */
128 break; /* don't underflow buffer */
131 while (isspace(*cp
));
133 *p_result
= strlen(buffer
);
137 /*******************************************************************************
140 * Finds a line in the file that begins with the given string. Returns FALSE
141 * for unexpected I/O errors; returns an empty (zero character) string if the
142 * requested line is not found.
144 * NOTE: The file pointer *MUST* be positioned at the beginning of a line when
145 * this function is called. Otherwise, an infinite loop can result.
148 static BOOL
FindLine(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
)
150 INT len
= strlen(key
);
151 LONG start
= ftell(file
);
158 ok
= ReadLine(file
, buffer
, bufsize
, &result
);
162 if (result
> 0 && strncmp(buffer
, key
, len
) == 0)
169 else if (result
== INT_MIN
)
171 WARN("Line beginning '%32s...' is too long; ignoring\n", buffer
);
174 while (ftell(file
) != start
);
176 WARN("Couldn't find line '%s...' in AFM file\n", key
);
181 /*******************************************************************************
184 * Utility function to convert double to float while checking for overflow.
185 * Will also catch strtod overflows, since HUGE_VAL > FLT_MAX (at least on
189 static inline BOOL
DoubleToFloat(float *p_f
, double d
)
191 if (d
> (double)FLT_MAX
|| d
< -(double)FLT_MAX
)
198 /*******************************************************************************
201 * Utility function to add or subtract 0.5 before converting to integer type.
204 static inline float Round(float f
)
206 return (f
>= 0.0) ? (f
+ 0.5) : (f
- 0.5);
209 /*******************************************************************************
212 * Finds and parses a line of the form '<key> <value>', where value is a
213 * number. Sets *p_found to FALSE if a corresponding line cannot be found, or
214 * it cannot be parsed; also sets *p_ret to 0.0, so calling functions can just
215 * skip the check of *p_found if the item is not required.
218 static BOOL
ReadFloat(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
219 FLOAT
*p_ret
, BOOL
*p_found
)
224 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
227 if (buffer
[0] == '\0') /* line not found */
234 cp
= buffer
+ strlen(key
); /* first char after key */
236 d
= strtod(cp
, &end_ptr
);
238 if (end_ptr
== cp
|| errno
!= 0 || DoubleToFloat(p_ret
, d
) == FALSE
)
240 WARN("Error parsing line '%s'\n", buffer
);
250 /*******************************************************************************
253 * See description of ReadFloat.
256 static BOOL
ReadInt(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
257 INT
*p_ret
, BOOL
*p_found
)
262 retval
= ReadFloat(file
, buffer
, bufsize
, key
, &f
, p_found
);
263 if (retval
== FALSE
|| *p_found
== FALSE
)
271 if (f
> (FLOAT
)INT_MAX
|| f
< (FLOAT
)INT_MIN
)
273 WARN("Error parsing line '%s'\n", buffer
);
283 /*******************************************************************************
286 * Returns FALSE on I/O error or memory allocation failure; sets *p_str to NULL
287 * if line cannot be found or can't be parsed.
290 static BOOL
ReadString(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
295 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
298 if (buffer
[0] == '\0')
304 cp
= buffer
+ strlen(key
); /* first char after key */
311 while (isspace(*cp
)) /* find first non-whitespace char */
314 *p_str
= HeapAlloc(PSDRV_Heap
, 0, strlen(cp
) + 1);
322 /*******************************************************************************
325 * Similar to ReadFloat above.
328 static BOOL
ReadBBox(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
334 if (FindLine(file
, buffer
, bufsize
, "FontBBox") == FALSE
)
337 if (buffer
[0] == '\0')
345 cp
= buffer
+ sizeof("FontBBox");
346 d
= strtod(cp
, &end_ptr
);
347 if (end_ptr
== cp
|| errno
!= 0 ||
348 DoubleToFloat(&(afm
->FontBBox
.llx
), d
) == FALSE
)
352 d
= strtod(cp
, &end_ptr
);
353 if (end_ptr
== cp
|| errno
!= 0 ||
354 DoubleToFloat(&(afm
->FontBBox
.lly
), d
) == FALSE
)
358 d
= strtod(cp
, &end_ptr
);
359 if (end_ptr
== cp
|| errno
!= 0
360 || DoubleToFloat(&(afm
->FontBBox
.urx
), d
) == FALSE
)
364 d
= strtod(cp
, &end_ptr
);
365 if (end_ptr
== cp
|| errno
!= 0
366 || DoubleToFloat(&(afm
->FontBBox
.ury
), d
) == FALSE
)
373 WARN("Error parsing line '%s'\n", buffer
);
378 /*******************************************************************************
381 * Finds and parses the 'Weight' line of an AFM file. Only tries to determine
382 * if a font is bold (FW_BOLD) or not (FW_NORMAL) -- ignoring all those cute
383 * little FW_* typedefs in the Win32 doc. AFAICT, this is what the Windows
384 * PostScript driver does.
387 static const struct { LPCSTR keyword
; INT weight
; } afm_weights
[] =
389 { "REGULAR", FW_NORMAL
},
390 { "NORMAL", FW_NORMAL
},
391 { "ROMAN", FW_NORMAL
},
393 { "BOOK", FW_NORMAL
},
394 { "MEDIUM", FW_NORMAL
},
395 { "LIGHT", FW_NORMAL
},
396 { "BLACK", FW_BOLD
},
397 { "HEAVY", FW_BOLD
},
399 { "ULTRA", FW_BOLD
},
400 { "SUPER" , FW_BOLD
},
404 static BOOL
ReadWeight(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
411 if (ReadString(file
, buffer
, bufsize
, "Weight", &sz
) == FALSE
)
420 for (cp
= sz
; *cp
!= '\0'; ++cp
)
423 for (i
= 0; afm_weights
[i
].keyword
!= NULL
; ++i
)
425 if (strstr(sz
, afm_weights
[i
].keyword
) != NULL
)
427 afm
->Weight
= afm_weights
[i
].weight
;
429 HeapFree(PSDRV_Heap
, 0, sz
);
434 WARN("Unknown weight '%s'; treating as Roman\n", sz
);
436 afm
->Weight
= FW_NORMAL
;
438 HeapFree(PSDRV_Heap
, 0, sz
);
442 /*******************************************************************************
446 static BOOL
ReadFixedPitch(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
451 if (ReadString(file
, buffer
, bufsize
, "IsFixedPitch", &sz
) == FALSE
)
460 if (stricmp(sz
, "false") == 0)
462 afm
->IsFixedPitch
= FALSE
;
464 HeapFree(PSDRV_Heap
, 0, sz
);
468 if (stricmp(sz
, "true") == 0)
470 afm
->IsFixedPitch
= TRUE
;
472 HeapFree(PSDRV_Heap
, 0, sz
);
476 WARN("Can't parse line '%s'\n", buffer
);
479 HeapFree(PSDRV_Heap
, 0, sz
);
483 /*******************************************************************************
486 * Allocates space for the AFM on the driver heap and reads basic font metrics.
487 * Returns FALSE for memory allocation failure; sets *p_afm to NULL if AFM file
491 static BOOL
ReadFontMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
**p_afm
)
496 *p_afm
= afm
= HeapAlloc(PSDRV_Heap
, 0, sizeof(*afm
));
500 retval
= ReadWeight(file
, buffer
, bufsize
, afm
, &found
);
501 if (retval
== FALSE
|| found
== FALSE
)
504 retval
= ReadFloat(file
, buffer
, bufsize
, "ItalicAngle",
505 &(afm
->ItalicAngle
), &found
);
506 if (retval
== FALSE
|| found
== FALSE
)
509 retval
= ReadFixedPitch(file
, buffer
, bufsize
, afm
, &found
);
510 if (retval
== FALSE
|| found
== FALSE
)
513 retval
= ReadBBox(file
, buffer
, bufsize
, afm
, &found
);
514 if (retval
== FALSE
|| found
== FALSE
)
517 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlinePosition",
518 &(afm
->UnderlinePosition
), &found
);
519 if (retval
== FALSE
|| found
== FALSE
)
522 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlineThickness",
523 &(afm
->UnderlineThickness
), &found
);
524 if (retval
== FALSE
|| found
== FALSE
)
527 retval
= ReadFloat(file
, buffer
, bufsize
, "Ascender", /* optional */
528 &(afm
->Ascender
), &found
);
532 retval
= ReadFloat(file
, buffer
, bufsize
, "Descender", /* optional */
533 &(afm
->Descender
), &found
);
537 afm
->WinMetrics
.usUnitsPerEm
= 1000;
538 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->Ascender
);
539 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->Descender
);
541 if (afm
->WinMetrics
.sTypoAscender
== 0)
542 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
544 if (afm
->WinMetrics
.sTypoDescender
== 0)
545 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
547 afm
->WinMetrics
.sTypoLineGap
= 1200 -
548 (afm
->WinMetrics
.sTypoAscender
- afm
->WinMetrics
.sTypoDescender
);
549 if (afm
->WinMetrics
.sTypoLineGap
< 0)
550 afm
->WinMetrics
.sTypoLineGap
= 0;
554 cleanup_afm
: /* handle fatal or non-fatal errors */
555 HeapFree(PSDRV_Heap
, 0, afm
);
560 /*******************************************************************************
563 * Fatal error: return FALSE (none defined)
565 * Non-fatal error: leave metrics->C set to INT_MAX
568 static BOOL
ParseC(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
583 l
= strtol(cp
, &end_ptr
, base
);
584 if (end_ptr
== cp
|| errno
!= 0 || l
> INT_MAX
|| l
< INT_MIN
)
586 WARN("Error parsing character code '%s'\n", sz
);
594 /*******************************************************************************
597 * Fatal error: return FALSE (none defined)
599 * Non-fatal error: leave metrics->WX set to FLT_MAX
602 static BOOL
ParseW(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
623 d
= strtod(cp
, &end_ptr
);
624 if (end_ptr
== cp
|| errno
!= 0 ||
625 DoubleToFloat(&(metrics
->WX
), d
) == FALSE
)
631 /* Make sure that Y component of vector is zero */
633 d
= strtod(cp
, &end_ptr
); /* errno == 0 */
634 if (end_ptr
== cp
|| errno
!= 0 || d
!= 0.0)
636 metrics
->WX
= FLT_MAX
;
643 WARN("Error parsing character width '%s'\n", sz
);
647 /*******************************************************************************
651 * Fatal error: return FALSE (none defined)
653 * Non-fatal error: leave metrics->B.ury set to FLT_MAX
656 static BOOL
ParseB(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
664 d
= strtod(cp
, &end_ptr
);
665 if (end_ptr
== cp
|| errno
!= 0 ||
666 DoubleToFloat(&(metrics
->B
.llx
), d
) == FALSE
)
670 d
= strtod(cp
, &end_ptr
);
671 if (end_ptr
== cp
|| errno
!= 0 ||
672 DoubleToFloat(&(metrics
->B
.lly
), d
) == FALSE
)
676 d
= strtod(cp
, &end_ptr
);
677 if (end_ptr
== cp
|| errno
!= 0 ||
678 DoubleToFloat(&(metrics
->B
.urx
), d
) == FALSE
)
682 d
= strtod(cp
, &end_ptr
);
683 if (end_ptr
== cp
|| errno
!= 0 ||
684 DoubleToFloat(&(metrics
->B
.ury
), d
) == FALSE
)
690 WARN("Error parsing glyph bounding box '%s'\n", sz
);
694 /*******************************************************************************
697 * Fatal error: return FALSE (PSDRV_GlyphName failure)
699 * Non-fatal error: leave metrics-> set to NULL
702 static BOOL
ParseN(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
704 CHAR save
, *cp
, *end_ptr
;
713 while (*end_ptr
!= '\0' && !isspace(*end_ptr
))
718 WARN("Error parsing glyph name '%s'\n", sz
);
725 metrics
->N
= PSDRV_GlyphName(cp
);
726 if (metrics
->N
== NULL
)
733 /*******************************************************************************
736 * Parses the metrics line for a single glyph in an AFM file. Returns FALSE on
737 * fatal error; sets *metrics to 'badmetrics' on non-fatal error.
740 static const OLD_AFMMETRICS badmetrics
=
746 { FLT_MAX
, FLT_MAX
, FLT_MAX
, FLT_MAX
}, /* B */
750 static BOOL
ParseCharMetrics(LPSTR buffer
, INT len
, OLD_AFMMETRICS
*metrics
)
754 *metrics
= badmetrics
;
763 case 'C': if (ParseC(cp
, metrics
) == FALSE
)
767 case 'W': if (ParseW(cp
, metrics
) == FALSE
)
771 case 'N': if (ParseN(cp
, metrics
) == FALSE
)
775 case 'B': if (ParseB(cp
, metrics
) == FALSE
)
780 cp
= strchr(cp
, ';');
783 WARN("No terminating semicolon\n");
790 if (metrics
->C
== INT_MAX
|| metrics
->WX
== FLT_MAX
|| metrics
->N
== NULL
||
791 metrics
->B
.ury
== FLT_MAX
)
793 *metrics
= badmetrics
;
800 /*******************************************************************************
803 * Checks whether Unicode value is part of Microsoft code page 1252
806 static const LONG ansiChars
[21] =
808 0x0152, 0x0153, 0x0160, 0x0161, 0x0178, 0x017d, 0x017e, 0x0192, 0x02c6,
809 0x02c9, 0x02dc, 0x03bc, 0x2013, 0x2014, 0x2026, 0x2030, 0x2039, 0x203a,
810 0x20ac, 0x2122, 0x2219
813 static int __cdecl
cmpUV(const void *a
, const void *b
)
815 return (int)(*((const LONG
*)a
) - *((const LONG
*)b
));
818 static inline BOOL
IsWinANSI(LONG uv
)
820 if ((0x0020 <= uv
&& uv
<= 0x007e) || (0x00a0 <= uv
&& uv
<= 0x00ff) ||
821 (0x2018 <= uv
&& uv
<= 0x201a) || (0x201c <= uv
&& uv
<= 0x201e) ||
822 (0x2020 <= uv
&& uv
<= 0x2022))
825 if (bsearch(&uv
, ansiChars
, 21, sizeof(INT
), cmpUV
) != NULL
)
831 /*******************************************************************************
834 * Determines Unicode value (UV) for each glyph, based on font encoding.
836 * FontSpecific: Usable encodings (0x20 - 0xff) are mapped into the
837 * Unicode private use range U+F020 - U+F0FF.
839 * other: UV determined by glyph name, based on Adobe Glyph List.
841 * Also does some font metric calculations that require UVs to be known.
844 static int __cdecl
UnicodeGlyphByNameIndex(const void *a
, const void *b
)
846 return ((const UNICODEGLYPH
*)a
)->name
->index
-
847 ((const UNICODEGLYPH
*)b
)->name
->index
;
850 static VOID
Unicodify(AFM
*afm
, OLD_AFMMETRICS
*metrics
)
854 if (strcmp(afm
->EncodingScheme
, "FontSpecific") == 0)
856 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
858 if (metrics
[i
].C
>= 0x20 && metrics
[i
].C
<= 0xff)
860 metrics
[i
].UV
= metrics
[i
].C
| 0xf000L
;
864 TRACE("Unencoded glyph '%s'\n", metrics
[i
].N
->sz
);
869 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
870 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
872 else /* non-FontSpecific encoding */
874 UNICODEGLYPH ug
, *p_ug
;
876 PSDRV_IndexGlyphList(); /* for fast searching of glyph names */
878 afm
->WinMetrics
.sAscender
= afm
->WinMetrics
.sDescender
= 0;
880 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
882 ug
.name
= metrics
[i
].N
;
883 p_ug
= bsearch(&ug
, PSDRV_AGLbyName
, PSDRV_AGLbyNameSize
,
884 sizeof(ug
), UnicodeGlyphByNameIndex
);
887 TRACE("Glyph '%s' not in Adobe Glyph List\n", ug
.name
->sz
);
892 metrics
[i
].UV
= p_ug
->UV
;
894 if (IsWinANSI(p_ug
->UV
))
896 SHORT ury
= (SHORT
)Round(metrics
[i
].B
.ury
);
897 SHORT lly
= (SHORT
)Round(metrics
[i
].B
.lly
);
899 if (ury
> afm
->WinMetrics
.sAscender
)
900 afm
->WinMetrics
.sAscender
= ury
;
901 if (lly
< afm
->WinMetrics
.sDescender
)
902 afm
->WinMetrics
.sDescender
= lly
;
907 if (afm
->WinMetrics
.sAscender
== 0)
908 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
909 if (afm
->WinMetrics
.sDescender
== 0)
910 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
913 afm
->WinMetrics
.sLineGap
=
914 1150 - (afm
->WinMetrics
.sAscender
- afm
->WinMetrics
.sDescender
);
915 if (afm
->WinMetrics
.sLineGap
< 0)
916 afm
->WinMetrics
.sLineGap
= 0;
918 afm
->WinMetrics
.usWinAscent
= (afm
->WinMetrics
.sAscender
> 0) ?
919 afm
->WinMetrics
.sAscender
: 0;
920 afm
->WinMetrics
.usWinDescent
= (afm
->WinMetrics
.sDescender
< 0) ?
921 -(afm
->WinMetrics
.sDescender
) : 0;
924 /*******************************************************************************
927 * Reads metrics for all glyphs. *p_metrics will be NULL on non-fatal error.
930 static int __cdecl
OldAFMMetricsByUV(const void *a
, const void *b
)
932 return ((const OLD_AFMMETRICS
*)a
)->UV
- ((const OLD_AFMMETRICS
*)b
)->UV
;
935 static BOOL
ReadCharMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
936 AFMMETRICS
**p_metrics
)
939 OLD_AFMMETRICS
*old_metrics
, *encoded_metrics
;
943 retval
= ReadInt(file
, buffer
, bufsize
, "StartCharMetrics",
944 &(afm
->NumofMetrics
), &found
);
945 if (retval
== FALSE
|| found
== FALSE
)
951 old_metrics
= HeapAlloc(PSDRV_Heap
, 0,
952 afm
->NumofMetrics
* sizeof(*old_metrics
));
953 if (old_metrics
== NULL
)
956 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
958 retval
= ReadLine(file
, buffer
, bufsize
, &len
);
960 goto cleanup_old_metrics
;
964 retval
= ParseCharMetrics(buffer
, len
, old_metrics
+ i
);
965 if (retval
== FALSE
|| old_metrics
[i
].C
== INT_MAX
)
966 goto cleanup_old_metrics
;
976 case INT_MIN
: WARN("Ignoring long line '%32s...'\n", buffer
);
977 goto cleanup_old_metrics
; /* retval == TRUE */
979 case EOF
: WARN("Unexpected EOF\n");
980 goto cleanup_old_metrics
; /* retval == TRUE */
984 Unicodify(afm
, old_metrics
); /* wait until glyph names have been read */
986 qsort(old_metrics
, afm
->NumofMetrics
, sizeof(*old_metrics
),
989 for (i
= 0; old_metrics
[i
].UV
== -1; ++i
); /* count unencoded glyphs */
991 afm
->NumofMetrics
-= i
;
992 encoded_metrics
= old_metrics
+ i
;
994 afm
->Metrics
= *p_metrics
= metrics
= HeapAlloc(PSDRV_Heap
, 0,
995 afm
->NumofMetrics
* sizeof(*metrics
));
996 if (afm
->Metrics
== NULL
)
997 goto cleanup_old_metrics
; /* retval == TRUE */
999 for (i
= 0; i
< afm
->NumofMetrics
; ++i
, ++metrics
, ++encoded_metrics
)
1001 metrics
->C
= encoded_metrics
->C
;
1002 metrics
->UV
= encoded_metrics
->UV
;
1003 metrics
->WX
= encoded_metrics
->WX
;
1004 metrics
->N
= encoded_metrics
->N
;
1007 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1009 afm
->WinMetrics
.sAvgCharWidth
= PSDRV_CalcAvgCharWidth(afm
);
1013 cleanup_old_metrics
: /* handle fatal or non-fatal errors */
1014 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1019 /*******************************************************************************
1022 * Builds the AFM for a PostScript font and adds it to the driver font list.
1023 * Returns FALSE only on an unexpected error (memory allocation or I/O error).
1026 static BOOL
BuildAFM(FILE *file
)
1028 CHAR buffer
[258]; /* allow for <cr>, <lf>, and <nul> */
1030 AFMMETRICS
*metrics
;
1031 LPSTR font_name
, full_name
, family_name
, encoding_scheme
;
1034 retval
= ReadFontMetrics(file
, buffer
, sizeof(buffer
), &afm
);
1035 if (retval
== FALSE
|| afm
== NULL
)
1038 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FontName", &font_name
);
1039 if (retval
== FALSE
|| font_name
== NULL
)
1042 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FullName", &full_name
);
1043 if (retval
== FALSE
|| full_name
== NULL
)
1044 goto cleanup_font_name
;
1046 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FamilyName",
1048 if (retval
== FALSE
|| family_name
== NULL
)
1049 goto cleanup_full_name
;
1051 retval
= ReadString(file
, buffer
, sizeof(buffer
), "EncodingScheme",
1053 if (retval
== FALSE
|| encoding_scheme
== NULL
)
1054 goto cleanup_family_name
;
1056 afm
->FontName
= font_name
;
1057 afm
->FullName
= full_name
;
1058 afm
->FamilyName
= family_name
;
1059 afm
->EncodingScheme
= encoding_scheme
;
1061 retval
= ReadCharMetrics(file
, buffer
, sizeof(buffer
), afm
, &metrics
);
1062 if (retval
== FALSE
|| metrics
== FALSE
)
1063 goto cleanup_encoding_scheme
;
1065 retval
= PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
, &added
);
1066 if (retval
== FALSE
|| added
== FALSE
)
1067 goto cleanup_encoding_scheme
;
1071 /* clean up after fatal or non-fatal errors */
1073 cleanup_encoding_scheme
:
1074 HeapFree(PSDRV_Heap
, 0, encoding_scheme
);
1075 cleanup_family_name
:
1076 HeapFree(PSDRV_Heap
, 0, family_name
);
1078 HeapFree(PSDRV_Heap
, 0, full_name
);
1080 HeapFree(PSDRV_Heap
, 0, font_name
);
1082 HeapFree(PSDRV_Heap
, 0, afm
);
1087 /*******************************************************************************
1090 * Reads font metrics from Type 1 AFM file. Only returns FALSE for
1091 * unexpected errors (memory allocation or I/O).
1094 static BOOL
ReadAFMFile(LPCWSTR filename
)
1099 TRACE("%s\n", debugstr_w(filename
));
1101 f
= _wfopen(filename
, L
"r");
1104 WARN("%s: %s\n", debugstr_w(filename
), strerror(errno
));
1108 retval
= BuildAFM(f
);
1114 /*******************************************************************************
1117 * Reads all Type 1 AFM files in a directory.
1120 static BOOL
ReadAFMDir(LPCSTR dirname
)
1122 WCHAR
*path
= wine_get_dos_file_name( dirname
);
1123 struct _wfinddata_t data
;
1125 WCHAR
*p
, *filename
;
1128 if (!path
) return TRUE
;
1129 if (!(filename
= HeapAlloc( GetProcessHeap(), 0, lstrlenW(path
) + 256 )))
1131 HeapFree( GetProcessHeap(), 0, path
);
1134 lstrcpyW( filename
, path
);
1135 HeapFree( GetProcessHeap(), 0, path
);
1136 p
= filename
+ lstrlenW(filename
);
1138 lstrcpyW( p
, L
"*" );
1140 handle
= _wfindfirst( filename
, &data
);
1145 WCHAR
*ext
= wcschr( data
.name
, '.' );
1146 if (!ext
|| wcsicmp(ext
, L
".afm")) continue;
1147 lstrcpyW( p
, data
.name
);
1148 if (!(ret
= ReadAFMFile( filename
))) break;
1149 } while (!_wfindnext( handle
, &data
));
1151 _findclose( handle
);
1153 HeapFree( GetProcessHeap(), 0, filename
);
1157 /*******************************************************************************
1158 * PSDRV_GetType1Metrics
1160 * Reads font metrics from Type 1 AFM font files in directories listed in the
1161 * HKEY_CURRENT_USER\\Software\\Wine\\Fonts\\AFMPath registry string.
1163 * If this function fails (returns FALSE), the driver will fail to initialize
1164 * and the driver heap will be destroyed, so it's not necessary to HeapFree
1165 * everything in that event.
1168 BOOL
PSDRV_GetType1Metrics(void)
1175 /* @@ Wine registry key: HKCU\Software\Wine\Fonts */
1176 if (RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Fonts", &hkey
) != ERROR_SUCCESS
)
1179 if (RegQueryValueExW( hkey
, L
"AFMPath", NULL
, NULL
, NULL
, &len
) == ERROR_SUCCESS
)
1181 len
+= sizeof(WCHAR
);
1182 valueW
= HeapAlloc( PSDRV_Heap
, 0, len
);
1183 if (RegQueryValueExW( hkey
, L
"AFMPath", NULL
, NULL
, (BYTE
*)valueW
, &len
) == ERROR_SUCCESS
)
1185 len
= WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, NULL
, 0, NULL
, NULL
);
1186 valueA
= HeapAlloc( PSDRV_Heap
, 0, len
);
1187 WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, len
, NULL
, NULL
);
1188 TRACE( "got AFM font path %s\n", debugstr_a(valueA
) );
1192 LPSTR next
= strchr( ptr
, ':' );
1193 if (next
) *next
++ = 0;
1194 if (!ReadAFMDir( ptr
))
1201 HeapFree( PSDRV_Heap
, 0, valueA
);
1203 HeapFree( PSDRV_Heap
, 0, valueW
);