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 */
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
56 /*******************************************************************************
59 * Reads a line from a text file into the buffer and trims trailing whitespace.
60 * Can handle DOS and Unix text files, including weird DOS EOF. Returns FALSE
61 * for unexpected I/O errors; otherwise returns TRUE and sets *p_result to
62 * either the number of characters in the returned string or one of the
65 * 0: Blank (or all whitespace) line. This is just a special case
66 * of the normal behavior.
68 * EOF: End of file has been reached.
70 * INT_MIN: Buffer overflow. Returned string is truncated (duh!) and
71 * trailing whitespace is *not* trimmed. Remaining text in
72 * line is discarded. (I.e. the file pointer is positioned at
73 * the beginning of the next line.)
76 static BOOL
ReadLine(FILE *file
, CHAR buffer
[], INT bufsize
, INT
*p_result
)
81 if (fgets(buffer
, bufsize
, file
) == NULL
)
83 if (feof(file
) == 0) /* EOF or error? */
85 ERR("%s\n", strerror(errno
));
93 cp
= strchr(buffer
, '\n');
98 if (i
== bufsize
- 1) /* max possible; was line truncated? */
101 i
= fgetc(file
); /* find the newline or EOF */
102 while (i
!= '\n' && i
!= EOF
);
106 if (feof(file
) == 0) /* EOF or error? */
108 ERR("%s\n", strerror(errno
));
112 WARN("No newline at EOF\n");
118 else /* no newline and not truncated */
120 if (strcmp(buffer
, "\x1a") == 0) /* test for DOS EOF */
126 WARN("No newline at EOF\n");
127 cp
= buffer
+ i
; /* points to \0 where \n should have been */
133 *cp
= '\0'; /* trim trailing whitespace */
135 break; /* don't underflow buffer */
138 while (isspace(*cp
));
140 *p_result
= strlen(buffer
);
144 /*******************************************************************************
147 * Finds a line in the file that begins with the given string. Returns FALSE
148 * for unexpected I/O errors; returns an empty (zero character) string if the
149 * requested line is not found.
151 * NOTE: The file pointer *MUST* be positioned at the beginning of a line when
152 * this function is called. Otherwise, an infinite loop can result.
155 static BOOL
FindLine(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
)
157 INT len
= strlen(key
);
158 LONG start
= ftell(file
);
165 ok
= ReadLine(file
, buffer
, bufsize
, &result
);
169 if (result
> 0 && strncmp(buffer
, key
, len
) == 0)
176 else if (result
== INT_MIN
)
178 WARN("Line beginning '%32s...' is too long; ignoring\n", buffer
);
181 while (ftell(file
) != start
);
183 WARN("Couldn't find line '%s...' in AFM file\n", key
);
188 /*******************************************************************************
191 * Utility function to convert double to float while checking for overflow.
192 * Will also catch strtod overflows, since HUGE_VAL > FLT_MAX (at least on
196 static inline BOOL
DoubleToFloat(float *p_f
, double d
)
198 if (d
> (double)FLT_MAX
|| d
< -(double)FLT_MAX
)
205 /*******************************************************************************
208 * Utility function to add or subtract 0.5 before converting to integer type.
211 static inline float Round(float f
)
213 return (f
>= 0.0) ? (f
+ 0.5) : (f
- 0.5);
216 /*******************************************************************************
219 * Finds and parses a line of the form '<key> <value>', where value is a
220 * number. Sets *p_found to FALSE if a corresponding line cannot be found, or
221 * it cannot be parsed; also sets *p_ret to 0.0, so calling functions can just
222 * skip the check of *p_found if the item is not required.
225 static BOOL
ReadFloat(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
226 FLOAT
*p_ret
, BOOL
*p_found
)
231 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
234 if (buffer
[0] == '\0') /* line not found */
241 cp
= buffer
+ strlen(key
); /* first char after key */
243 d
= strtod(cp
, &end_ptr
);
245 if (end_ptr
== cp
|| errno
!= 0 || DoubleToFloat(p_ret
, d
) == FALSE
)
247 WARN("Error parsing line '%s'\n", buffer
);
257 /*******************************************************************************
260 * See description of ReadFloat.
263 static BOOL
ReadInt(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
264 INT
*p_ret
, BOOL
*p_found
)
269 retval
= ReadFloat(file
, buffer
, bufsize
, key
, &f
, p_found
);
270 if (retval
== FALSE
|| *p_found
== FALSE
)
278 if (f
> (FLOAT
)INT_MAX
|| f
< (FLOAT
)INT_MIN
)
280 WARN("Error parsing line '%s'\n", buffer
);
290 /*******************************************************************************
293 * Returns FALSE on I/O error or memory allocation failure; sets *p_str to NULL
294 * if line cannot be found or can't be parsed.
297 static BOOL
ReadString(FILE *file
, CHAR buffer
[], INT bufsize
, LPCSTR key
,
302 if (FindLine(file
, buffer
, bufsize
, key
) == FALSE
)
305 if (buffer
[0] == '\0')
311 cp
= buffer
+ strlen(key
); /* first char after key */
318 while (isspace(*cp
)) /* find first non-whitespace char */
321 *p_str
= HeapAlloc(PSDRV_Heap
, 0, strlen(cp
) + 1);
329 /*******************************************************************************
332 * Similar to ReadFloat above.
335 static BOOL
ReadBBox(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
341 if (FindLine(file
, buffer
, bufsize
, "FontBBox") == FALSE
)
344 if (buffer
[0] == '\0')
352 cp
= buffer
+ sizeof("FontBBox");
353 d
= strtod(cp
, &end_ptr
);
354 if (end_ptr
== cp
|| errno
!= 0 ||
355 DoubleToFloat(&(afm
->FontBBox
.llx
), d
) == FALSE
)
359 d
= strtod(cp
, &end_ptr
);
360 if (end_ptr
== cp
|| errno
!= 0 ||
361 DoubleToFloat(&(afm
->FontBBox
.lly
), d
) == FALSE
)
365 d
= strtod(cp
, &end_ptr
);
366 if (end_ptr
== cp
|| errno
!= 0
367 || DoubleToFloat(&(afm
->FontBBox
.urx
), d
) == FALSE
)
371 d
= strtod(cp
, &end_ptr
);
372 if (end_ptr
== cp
|| errno
!= 0
373 || DoubleToFloat(&(afm
->FontBBox
.ury
), d
) == FALSE
)
380 WARN("Error parsing line '%s'\n", buffer
);
385 /*******************************************************************************
388 * Finds and parses the 'Weight' line of an AFM file. Only tries to determine
389 * if a font is bold (FW_BOLD) or not (FW_NORMAL) -- ignoring all those cute
390 * little FW_* typedefs in the Win32 doc. AFAICT, this is what the Windows
391 * PostScript driver does.
394 static const struct { LPCSTR keyword
; INT weight
; } afm_weights
[] =
396 { "REGULAR", FW_NORMAL
},
397 { "NORMAL", FW_NORMAL
},
398 { "ROMAN", FW_NORMAL
},
400 { "BOOK", FW_NORMAL
},
401 { "MEDIUM", FW_NORMAL
},
402 { "LIGHT", FW_NORMAL
},
403 { "BLACK", FW_BOLD
},
404 { "HEAVY", FW_BOLD
},
406 { "ULTRA", FW_BOLD
},
407 { "SUPER" , FW_BOLD
},
411 static BOOL
ReadWeight(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
418 if (ReadString(file
, buffer
, bufsize
, "Weight", &sz
) == FALSE
)
427 for (cp
= sz
; *cp
!= '\0'; ++cp
)
430 for (i
= 0; afm_weights
[i
].keyword
!= NULL
; ++i
)
432 if (strstr(sz
, afm_weights
[i
].keyword
) != NULL
)
434 afm
->Weight
= afm_weights
[i
].weight
;
436 HeapFree(PSDRV_Heap
, 0, sz
);
441 WARN("Unknown weight '%s'; treating as Roman\n", sz
);
443 afm
->Weight
= FW_NORMAL
;
445 HeapFree(PSDRV_Heap
, 0, sz
);
449 /*******************************************************************************
453 static BOOL
ReadFixedPitch(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
458 if (ReadString(file
, buffer
, bufsize
, "IsFixedPitch", &sz
) == FALSE
)
467 if (strcasecmp(sz
, "false") == 0)
469 afm
->IsFixedPitch
= FALSE
;
471 HeapFree(PSDRV_Heap
, 0, sz
);
475 if (strcasecmp(sz
, "true") == 0)
477 afm
->IsFixedPitch
= TRUE
;
479 HeapFree(PSDRV_Heap
, 0, sz
);
483 WARN("Can't parse line '%s'\n", buffer
);
486 HeapFree(PSDRV_Heap
, 0, sz
);
490 /*******************************************************************************
493 * Allocates space for the AFM on the driver heap and reads basic font metrics.
494 * Returns FALSE for memory allocation failure; sets *p_afm to NULL if AFM file
498 static BOOL
ReadFontMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
**p_afm
)
503 *p_afm
= afm
= HeapAlloc(PSDRV_Heap
, 0, sizeof(*afm
));
507 retval
= ReadWeight(file
, buffer
, bufsize
, afm
, &found
);
508 if (retval
== FALSE
|| found
== FALSE
)
511 retval
= ReadFloat(file
, buffer
, bufsize
, "ItalicAngle",
512 &(afm
->ItalicAngle
), &found
);
513 if (retval
== FALSE
|| found
== FALSE
)
516 retval
= ReadFixedPitch(file
, buffer
, bufsize
, afm
, &found
);
517 if (retval
== FALSE
|| found
== FALSE
)
520 retval
= ReadBBox(file
, buffer
, bufsize
, afm
, &found
);
521 if (retval
== FALSE
|| found
== FALSE
)
524 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlinePosition",
525 &(afm
->UnderlinePosition
), &found
);
526 if (retval
== FALSE
|| found
== FALSE
)
529 retval
= ReadFloat(file
, buffer
, bufsize
, "UnderlineThickness",
530 &(afm
->UnderlineThickness
), &found
);
531 if (retval
== FALSE
|| found
== FALSE
)
534 retval
= ReadFloat(file
, buffer
, bufsize
, "Ascender", /* optional */
535 &(afm
->Ascender
), &found
);
539 retval
= ReadFloat(file
, buffer
, bufsize
, "Descender", /* optional */
540 &(afm
->Descender
), &found
);
544 afm
->WinMetrics
.usUnitsPerEm
= 1000;
545 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->Ascender
);
546 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->Descender
);
548 if (afm
->WinMetrics
.sTypoAscender
== 0)
549 afm
->WinMetrics
.sTypoAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
551 if (afm
->WinMetrics
.sTypoDescender
== 0)
552 afm
->WinMetrics
.sTypoDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
554 afm
->WinMetrics
.sTypoLineGap
= 1200 -
555 (afm
->WinMetrics
.sTypoAscender
- afm
->WinMetrics
.sTypoDescender
);
556 if (afm
->WinMetrics
.sTypoLineGap
< 0)
557 afm
->WinMetrics
.sTypoLineGap
= 0;
561 cleanup_afm
: /* handle fatal or non-fatal errors */
562 HeapFree(PSDRV_Heap
, 0, afm
);
567 /*******************************************************************************
570 * Fatal error: return FALSE (none defined)
572 * Non-fatal error: leave metrics->C set to INT_MAX
575 static BOOL
ParseC(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
590 l
= strtol(cp
, &end_ptr
, base
);
591 if (end_ptr
== cp
|| errno
!= 0 || l
> INT_MAX
|| l
< INT_MIN
)
593 WARN("Error parsing character code '%s'\n", sz
);
601 /*******************************************************************************
604 * Fatal error: return FALSE (none defined)
606 * Non-fatal error: leave metrics->WX set to FLT_MAX
609 static BOOL
ParseW(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
630 d
= strtod(cp
, &end_ptr
);
631 if (end_ptr
== cp
|| errno
!= 0 ||
632 DoubleToFloat(&(metrics
->WX
), d
) == FALSE
)
638 /* Make sure that Y component of vector is zero */
640 d
= strtod(cp
, &end_ptr
); /* errno == 0 */
641 if (end_ptr
== cp
|| errno
!= 0 || d
!= 0.0)
643 metrics
->WX
= FLT_MAX
;
650 WARN("Error parsing character width '%s'\n", sz
);
654 /*******************************************************************************
658 * Fatal error: return FALSE (none defined)
660 * Non-fatal error: leave metrics->B.ury set to FLT_MAX
663 static BOOL
ParseB(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
671 d
= strtod(cp
, &end_ptr
);
672 if (end_ptr
== cp
|| errno
!= 0 ||
673 DoubleToFloat(&(metrics
->B
.llx
), d
) == FALSE
)
677 d
= strtod(cp
, &end_ptr
);
678 if (end_ptr
== cp
|| errno
!= 0 ||
679 DoubleToFloat(&(metrics
->B
.lly
), d
) == FALSE
)
683 d
= strtod(cp
, &end_ptr
);
684 if (end_ptr
== cp
|| errno
!= 0 ||
685 DoubleToFloat(&(metrics
->B
.urx
), d
) == FALSE
)
689 d
= strtod(cp
, &end_ptr
);
690 if (end_ptr
== cp
|| errno
!= 0 ||
691 DoubleToFloat(&(metrics
->B
.ury
), d
) == FALSE
)
697 WARN("Error parsing glyph bounding box '%s'\n", sz
);
701 /*******************************************************************************
704 * Fatal error: return FALSE (PSDRV_GlyphName failure)
706 * Non-fatal error: leave metrics-> set to NULL
709 static BOOL
ParseN(LPSTR sz
, OLD_AFMMETRICS
*metrics
)
711 CHAR save
, *cp
, *end_ptr
;
720 while (*end_ptr
!= '\0' && !isspace(*end_ptr
))
725 WARN("Error parsing glyph name '%s'\n", sz
);
732 metrics
->N
= PSDRV_GlyphName(cp
);
733 if (metrics
->N
== NULL
)
740 /*******************************************************************************
743 * Parses the metrics line for a single glyph in an AFM file. Returns FALSE on
744 * fatal error; sets *metrics to 'badmetrics' on non-fatal error.
747 static const OLD_AFMMETRICS badmetrics
=
753 { FLT_MAX
, FLT_MAX
, FLT_MAX
, FLT_MAX
}, /* B */
757 static BOOL
ParseCharMetrics(LPSTR buffer
, INT len
, OLD_AFMMETRICS
*metrics
)
761 *metrics
= badmetrics
;
770 case 'C': if (ParseC(cp
, metrics
) == FALSE
)
774 case 'W': if (ParseW(cp
, metrics
) == FALSE
)
778 case 'N': if (ParseN(cp
, metrics
) == FALSE
)
782 case 'B': if (ParseB(cp
, metrics
) == FALSE
)
787 cp
= strchr(cp
, ';');
790 WARN("No terminating semicolon\n");
797 if (metrics
->C
== INT_MAX
|| metrics
->WX
== FLT_MAX
|| metrics
->N
== NULL
||
798 metrics
->B
.ury
== FLT_MAX
)
800 *metrics
= badmetrics
;
807 /*******************************************************************************
810 * Checks whether Unicode value is part of Microsoft code page 1252
813 static const LONG ansiChars
[21] =
815 0x0152, 0x0153, 0x0160, 0x0161, 0x0178, 0x017d, 0x017e, 0x0192, 0x02c6,
816 0x02c9, 0x02dc, 0x03bc, 0x2013, 0x2014, 0x2026, 0x2030, 0x2039, 0x203a,
817 0x20ac, 0x2122, 0x2219
820 static int cmpUV(const void *a
, const void *b
)
822 return (int)(*((const LONG
*)a
) - *((const LONG
*)b
));
825 static inline BOOL
IsWinANSI(LONG uv
)
827 if ((0x0020 <= uv
&& uv
<= 0x007e) || (0x00a0 <= uv
&& uv
<= 0x00ff) ||
828 (0x2018 <= uv
&& uv
<= 0x201a) || (0x201c <= uv
&& uv
<= 0x201e) ||
829 (0x2020 <= uv
&& uv
<= 0x2022))
832 if (bsearch(&uv
, ansiChars
, 21, sizeof(INT
), cmpUV
) != NULL
)
838 /*******************************************************************************
841 * Determines Unicode value (UV) for each glyph, based on font encoding.
843 * FontSpecific: Usable encodings (0x20 - 0xff) are mapped into the
844 * Unicode private use range U+F020 - U+F0FF.
846 * other: UV determined by glyph name, based on Adobe Glyph List.
848 * Also does some font metric calculations that require UVs to be known.
851 static int UnicodeGlyphByNameIndex(const void *a
, const void *b
)
853 return ((const UNICODEGLYPH
*)a
)->name
->index
-
854 ((const UNICODEGLYPH
*)b
)->name
->index
;
857 static VOID
Unicodify(AFM
*afm
, OLD_AFMMETRICS
*metrics
)
861 if (strcmp(afm
->EncodingScheme
, "FontSpecific") == 0)
863 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
865 if (metrics
[i
].C
>= 0x20 && metrics
[i
].C
<= 0xff)
867 metrics
[i
].UV
= metrics
[i
].C
| 0xf000L
;
871 TRACE("Unencoded glyph '%s'\n", metrics
[i
].N
->sz
);
876 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
877 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
879 else /* non-FontSpecific encoding */
881 UNICODEGLYPH ug
, *p_ug
;
883 PSDRV_IndexGlyphList(); /* for fast searching of glyph names */
885 afm
->WinMetrics
.sAscender
= afm
->WinMetrics
.sDescender
= 0;
887 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
889 ug
.name
= metrics
[i
].N
;
890 p_ug
= bsearch(&ug
, PSDRV_AGLbyName
, PSDRV_AGLbyNameSize
,
891 sizeof(ug
), UnicodeGlyphByNameIndex
);
894 TRACE("Glyph '%s' not in Adobe Glyph List\n", ug
.name
->sz
);
899 metrics
[i
].UV
= p_ug
->UV
;
901 if (IsWinANSI(p_ug
->UV
))
903 SHORT ury
= (SHORT
)Round(metrics
[i
].B
.ury
);
904 SHORT lly
= (SHORT
)Round(metrics
[i
].B
.lly
);
906 if (ury
> afm
->WinMetrics
.sAscender
)
907 afm
->WinMetrics
.sAscender
= ury
;
908 if (lly
< afm
->WinMetrics
.sDescender
)
909 afm
->WinMetrics
.sDescender
= lly
;
914 if (afm
->WinMetrics
.sAscender
== 0)
915 afm
->WinMetrics
.sAscender
= (SHORT
)Round(afm
->FontBBox
.ury
);
916 if (afm
->WinMetrics
.sDescender
== 0)
917 afm
->WinMetrics
.sDescender
= (SHORT
)Round(afm
->FontBBox
.lly
);
920 afm
->WinMetrics
.sLineGap
=
921 1150 - (afm
->WinMetrics
.sAscender
- afm
->WinMetrics
.sDescender
);
922 if (afm
->WinMetrics
.sLineGap
< 0)
923 afm
->WinMetrics
.sLineGap
= 0;
925 afm
->WinMetrics
.usWinAscent
= (afm
->WinMetrics
.sAscender
> 0) ?
926 afm
->WinMetrics
.sAscender
: 0;
927 afm
->WinMetrics
.usWinDescent
= (afm
->WinMetrics
.sDescender
< 0) ?
928 -(afm
->WinMetrics
.sDescender
) : 0;
931 /*******************************************************************************
934 * Reads metrics for all glyphs. *p_metrics will be NULL on non-fatal error.
937 static int OldAFMMetricsByUV(const void *a
, const void *b
)
939 return ((const OLD_AFMMETRICS
*)a
)->UV
- ((const OLD_AFMMETRICS
*)b
)->UV
;
942 static BOOL
ReadCharMetrics(FILE *file
, CHAR buffer
[], INT bufsize
, AFM
*afm
,
943 AFMMETRICS
**p_metrics
)
946 OLD_AFMMETRICS
*old_metrics
, *encoded_metrics
;
950 retval
= ReadInt(file
, buffer
, bufsize
, "StartCharMetrics",
951 &(afm
->NumofMetrics
), &found
);
952 if (retval
== FALSE
|| found
== FALSE
)
958 old_metrics
= HeapAlloc(PSDRV_Heap
, 0,
959 afm
->NumofMetrics
* sizeof(*old_metrics
));
960 if (old_metrics
== NULL
)
963 for (i
= 0; i
< afm
->NumofMetrics
; ++i
)
965 retval
= ReadLine(file
, buffer
, bufsize
, &len
);
967 goto cleanup_old_metrics
;
971 retval
= ParseCharMetrics(buffer
, len
, old_metrics
+ i
);
972 if (retval
== FALSE
|| old_metrics
[i
].C
== INT_MAX
)
973 goto cleanup_old_metrics
;
983 case INT_MIN
: WARN("Ignoring long line '%32s...'\n", buffer
);
984 goto cleanup_old_metrics
; /* retval == TRUE */
986 case EOF
: WARN("Unexpected EOF\n");
987 goto cleanup_old_metrics
; /* retval == TRUE */
991 Unicodify(afm
, old_metrics
); /* wait until glyph names have been read */
993 qsort(old_metrics
, afm
->NumofMetrics
, sizeof(*old_metrics
),
996 for (i
= 0; old_metrics
[i
].UV
== -1; ++i
); /* count unencoded glyphs */
998 afm
->NumofMetrics
-= i
;
999 encoded_metrics
= old_metrics
+ i
;
1001 afm
->Metrics
= *p_metrics
= metrics
= HeapAlloc(PSDRV_Heap
, 0,
1002 afm
->NumofMetrics
* sizeof(*metrics
));
1003 if (afm
->Metrics
== NULL
)
1004 goto cleanup_old_metrics
; /* retval == TRUE */
1006 for (i
= 0; i
< afm
->NumofMetrics
; ++i
, ++metrics
, ++encoded_metrics
)
1008 metrics
->C
= encoded_metrics
->C
;
1009 metrics
->UV
= encoded_metrics
->UV
;
1010 metrics
->WX
= encoded_metrics
->WX
;
1011 metrics
->N
= encoded_metrics
->N
;
1014 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1016 afm
->WinMetrics
.sAvgCharWidth
= PSDRV_CalcAvgCharWidth(afm
);
1020 cleanup_old_metrics
: /* handle fatal or non-fatal errors */
1021 HeapFree(PSDRV_Heap
, 0, old_metrics
);
1026 /*******************************************************************************
1029 * Builds the AFM for a PostScript font and adds it to the driver font list.
1030 * Returns FALSE only on an unexpected error (memory allocation or I/O error).
1033 static BOOL
BuildAFM(FILE *file
)
1035 CHAR buffer
[258]; /* allow for <cr>, <lf>, and <nul> */
1037 AFMMETRICS
*metrics
;
1038 LPSTR font_name
, full_name
, family_name
, encoding_scheme
;
1041 retval
= ReadFontMetrics(file
, buffer
, sizeof(buffer
), &afm
);
1042 if (retval
== FALSE
|| afm
== NULL
)
1045 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FontName", &font_name
);
1046 if (retval
== FALSE
|| font_name
== NULL
)
1049 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FullName", &full_name
);
1050 if (retval
== FALSE
|| full_name
== NULL
)
1051 goto cleanup_font_name
;
1053 retval
= ReadString(file
, buffer
, sizeof(buffer
), "FamilyName",
1055 if (retval
== FALSE
|| family_name
== NULL
)
1056 goto cleanup_full_name
;
1058 retval
= ReadString(file
, buffer
, sizeof(buffer
), "EncodingScheme",
1060 if (retval
== FALSE
|| encoding_scheme
== NULL
)
1061 goto cleanup_family_name
;
1063 afm
->FontName
= font_name
;
1064 afm
->FullName
= full_name
;
1065 afm
->FamilyName
= family_name
;
1066 afm
->EncodingScheme
= encoding_scheme
;
1068 retval
= ReadCharMetrics(file
, buffer
, sizeof(buffer
), afm
, &metrics
);
1069 if (retval
== FALSE
|| metrics
== FALSE
)
1070 goto cleanup_encoding_scheme
;
1072 retval
= PSDRV_AddAFMtoList(&PSDRV_AFMFontList
, afm
, &added
);
1073 if (retval
== FALSE
|| added
== FALSE
)
1074 goto cleanup_encoding_scheme
;
1078 /* clean up after fatal or non-fatal errors */
1080 cleanup_encoding_scheme
:
1081 HeapFree(PSDRV_Heap
, 0, encoding_scheme
);
1082 cleanup_family_name
:
1083 HeapFree(PSDRV_Heap
, 0, family_name
);
1085 HeapFree(PSDRV_Heap
, 0, full_name
);
1087 HeapFree(PSDRV_Heap
, 0, font_name
);
1089 HeapFree(PSDRV_Heap
, 0, afm
);
1094 /*******************************************************************************
1097 * Reads font metrics from Type 1 AFM file. Only returns FALSE for
1098 * unexpected errors (memory allocation or I/O).
1101 static BOOL
ReadAFMFile(LPCSTR filename
)
1106 TRACE("%s\n", filename
);
1108 f
= fopen(filename
, "r");
1111 WARN("%s: %s\n", filename
, strerror(errno
));
1115 retval
= BuildAFM(f
);
1121 /*******************************************************************************
1124 * Reads all Type 1 AFM files in a directory.
1127 static BOOL
ReadAFMDir(LPCSTR dirname
)
1129 struct dirent
*dent
;
1133 dir
= opendir(dirname
);
1136 WARN("%s: %s\n", dirname
, strerror(errno
));
1140 while ((dent
= readdir(dir
)) != NULL
)
1142 CHAR
*file_extension
= strchr(dent
->d_name
, '.');
1145 if (file_extension
== NULL
|| strcasecmp(file_extension
, ".afm") != 0)
1148 fn_len
= snprintf(filename
, 256, "%s/%s", dirname
, dent
->d_name
);
1149 if (fn_len
< 0 || fn_len
> sizeof(filename
) - 1)
1151 WARN("Path '%s/%s' is too long\n", dirname
, dent
->d_name
);
1155 if (ReadAFMFile(filename
) == FALSE
)
1166 /*******************************************************************************
1167 * PSDRV_GetType1Metrics
1169 * Reads font metrics from Type 1 AFM font files in directories listed in the
1170 * [afmdirs] section of the Wine configuration file.
1172 * If this function fails (returns FALSE), the driver will fail to initialize
1173 * and the driver heap will be destroyed, so it's not necessary to HeapFree
1174 * everything in that event.
1177 BOOL
PSDRV_GetType1Metrics(void)
1179 static const WCHAR pathW
[] = {'A','F','M','P','a','t','h',0};
1185 /* @@ Wine registry key: HKCU\Software\Wine\Fonts */
1186 if (RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Fonts", &hkey
) != ERROR_SUCCESS
)
1189 if (RegQueryValueExW( hkey
, pathW
, NULL
, NULL
, NULL
, &len
) == ERROR_SUCCESS
)
1191 len
+= sizeof(WCHAR
);
1192 valueW
= HeapAlloc( PSDRV_Heap
, 0, len
);
1193 if (RegQueryValueExW( hkey
, pathW
, NULL
, NULL
, (LPBYTE
)valueW
, &len
) == ERROR_SUCCESS
)
1195 len
= WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, NULL
, 0, NULL
, NULL
);
1196 valueA
= HeapAlloc( PSDRV_Heap
, 0, len
);
1197 WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, len
, NULL
, NULL
);
1198 TRACE( "got AFM font path %s\n", debugstr_a(valueA
) );
1202 LPSTR next
= strchr( ptr
, ':' );
1203 if (next
) *next
++ = 0;
1204 if (!ReadAFMDir( ptr
))
1211 HeapFree( PSDRV_Heap
, 0, valueA
);
1213 HeapFree( PSDRV_Heap
, 0, valueW
);