1 /*
2  * (C) 1988, 1989 by Adobe Systems Incorporated. All rights reserved.
3  *
4  * This file may be freely copied and redistributed as long as:
5  *   1) This entire notice continues to be included in the file,
6  *   2) If the file has been modified in any way, a notice of such
7  *      modification is conspicuously indicated.
8  *
9  * PostScript, Display PostScript, and Adobe are registered trademarks of
10  * Adobe Systems Incorporated.
11  *
12  * ************************************************************************
13  * THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO CHANGE WITHOUT
14  * NOTICE, AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY ADOBE SYSTEMS
15  * INCORPORATED. ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY OR
16  * LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO WARRANTY OF ANY
17  * KIND (EXPRESS, IMPLIED OR STATUTORY) WITH RESPECT TO THIS INFORMATION,
18  * AND EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
20  * ************************************************************************
21  */
22 
23 /* ParseAFM.h
24  *
25  * This header file is used in conjuction with the parseAFM.c file.
26  * Together these files provide the functionality to parse Adobe Font
27  * Metrics files and store the information in predefined data structures.
28  * It is intended to work with an application program that needs font metric
29  * information. The program can be used as is by making a procedure call to
30  * parse an AFM file and have the data stored, or an application developer
31  * may wish to customize the code.
32  *
33  * This header file defines the data structures used as well as the key
34  * strings that are currently recognized by this version of the AFM parser.
35  * This program is based on the document "Adobe Font Metrics Files,
36  * Specification Version 2.0".
37  *
38  * AFM files are separated into distinct sections of different data. Because
39  * of this, the parseAFM program can parse a specified file to only save
40  * certain sections of information based on the application's needs. A record
41  * containing the requested information will be returned to the application.
42  *
43  * AFM files are divided into five sections of data:
44  *	1) The Global Font Information
45  *	2) The Character Metrics Information
46  *	3) The Track Kerning Data
47  *	4) The Pair-Wise Kerning Data
48  *	5) The Composite Character Data
49  *
50  * Basically, the application can request any of these sections independent
51  * of what other sections are requested. In addition, in recognizing that
52  * many applications will want ONLY the x-width of characters and not all
53  * of the other character metrics information, there is a way to receive
54  * only the width information so as not to pay the storage cost for the
55  * unwanted data. An application should never request both the
56  * "quick and dirty" char metrics (widths only) and the Character Metrics
57  * Information since the Character Metrics Information will contain all
58  * of the character widths as well.
59  *
60  * There is a procedure in parseAFM.c, called parseFile, that can be
61  * called from any application wishing to get information from the AFM File.
62  * This procedure expects 3 parameters: a vaild file descriptor, a pointer
63  * to a (FontInfo *) variable (for which space will be allocated and then
64  * will be filled in with the data requested), and a mask specifying
65  * which data from the AFM File should be saved in the FontInfo structure.
66  *
67  * The flags that can be used to set the appropriate mask are defined below.
68  * In addition, several commonly used masks have already been defined.
69  *
70  * History:
71  *	original: DSM  Thu Oct 20 17:39:59 PDT 1988
72  *  modified: DSM  Mon Jul  3 14:17:50 PDT 1989
73  *    - added 'storageProblem' return code
74  *	  - fixed typos
75  */
76 
77 #include <stdio.h>
78 
79 
80 
81 /* your basic constants */
82 #define TRUE 1
83 #define FALSE 0
84 #define EOL '\n'                /* end-of-line indicator */
85 #define MAX_NAME 4096           /* max length for identifiers */
86 #define BOOL int
87 #define FLAGS int
88 
89 
90 
91 /* Flags that can be AND'ed together to specify exactly what
92  * information from the AFM file should be saved.
93  */
94 #define P_G	0x01	/* 0000 0001 */   /* Global Font Info      */
95 #define P_W	0x02	/* 0000 0010 */   /* Character Widths ONLY */
96 #define P_M	0x06	/* 0000 0110 */   /* All Char Metric Info  */
97 #define P_P	0x08	/* 0000 1000 */   /* Pair Kerning Info     */
98 #define P_T	0x10	/* 0001 0000 */   /* Track Kerning Info    */
99 #define P_C	0x20	/* 0010 0000 */   /* Composite Char Info   */
100 
101 
102 /* Commonly used flags
103  */
104 #define P_GW	(P_G | P_W)
105 #define P_GM	(P_G | P_M)
106 #define P_GMP	(P_G | P_M | P_P)
107 #define P_GMK	(P_G | P_M | P_P | P_T)
108 #define P_ALL	(P_G | P_M | P_P | P_T | P_C)
109 
110 
111 
112 /* Possible return codes from the parseFile procedure.
113  *
114  * ok means there were no problems parsing the file.
115  *
116  * parseError means that there was some kind of parsing error, but the
117  * parser went on. This could include problems like the count for any given
118  * section does not add up to how many entries there actually were, or
119  * there was a key that was not recognized. The return record may contain
120  * vaild data or it may not.
121  *
122  * earlyEOF means that an End of File was encountered before expected. This
123  * may mean that the AFM file had been truncated, or improperly formed.
124  *
125  * storageProblem means that there were problems allocating storage for
126  * the data structures that would have contained the AFM data.
127  */
128 #define ok 0
129 #define parseError -1
130 #define earlyEOF -2
131 #define storageProblem -3
132 
133 
134 
135 /************************* TYPES *********************************/
136 /* Below are all of the data structure definitions. These structures
137  * try to map as closely as possible to grouping and naming of data
138  * in the AFM Files.
139  */
140 
141 
142 /* Bounding box definition. Used for the Font BBox as well as the
143  * Character BBox.
144  */
145 typedef struct
146 {
147    int llx;	/* lower left x-position  */
148    int lly;	/* lower left y-position  */
149    int urx;	/* upper right x-position */
150    int ury;	/* upper right y-position */
151 } BBox;
152 
153 
154 /* Global Font information.
155  * The key that each field is associated with is in comments. For an
156  * explanation about each key and its value please refer to the AFM
157  * documentation (full title & version given above).
158  */
159 typedef struct
160 {
161    char *afmVersion;		/* key: StartFontMetrics */
162    char *fontName;		/* key: FontName */
163    char *fullName;		/* key: FullName */
164    char *familyName;		/* key: FamilyName */
165    char *weight;		/* key: Weight */
166    float italicAngle;		/* key: ItalicAngle */
167    BOOL isFixedPitch;		/* key: IsFixedPitch */
168    BBox fontBBox;		/* key: FontBBox */
169    int underlinePosition;  	/* key: UnderlinePosition */
170    int underlineThickness; 	/* key: UnderlineThickness */
171    char *version;		/* key: Version */
172    char *notice;		/* key: Notice */
173    char *encodingScheme;	/* key: EncodingScheme */
174    int capHeight;		/* key: CapHeight */
175    int xHeight;			/* key: XHeight */
176    int ascender;		/* key: Ascender */
177    int descender;		/* key: Descender */
178 } GlobalFontInfo;
179 
180 
181 /* Ligature definition is a linked list since any character can have
182  * any number of ligatures.
183  */
184 typedef struct _t_ligature
185 {
186     char *succ, *lig;
187     struct _t_ligature *next;
188 } Ligature;
189 
190 
191 /* Character Metric Information. This structure is used only if ALL
192  * character metric information is requested. If only the character
193  * widths is requested, then only an array of the character x-widths
194  * is returned.
195  *
196  * The key that each field is associated with is in comments. For an
197  * explanation about each key and its value please refer to the
198  * Character Metrics section of the AFM documentation (full title
199  * & version given above).
200  */
201 typedef struct
202 {
203     int code, 		/* key: C */
204         wx,		/* key: WX */
205         wy;		/* together wx and wy are associated with key: W */
206     char *name; 	/* key: N */
207     BBox charBBox;	/* key: B */
208     Ligature *ligs;	/* key: L (linked list; not a fixed number of Ls */
209 } CharMetricInfo;
210 
211 
212 /* Track kerning data structure.
213  * The fields of this record are the five values associated with every
214  * TrackKern entry.
215  *
216  * For an explanation about each value please refer to the
217  * Track Kerning section of the AFM documentation (full title
218  * & version given above).
219  */
220 typedef struct
221 {
222     int degree;
223     float minPtSize,
224           minKernAmt,
225           maxPtSize,
226           maxKernAmt;
227 } TrackKernData;
228 
229 
230 /* Pair Kerning data structure.
231  * The fields of this record are the four values associated with every
232  * KP entry. For KPX entries, the yamt will be zero.
233  *
234  * For an explanation about each value please refer to the
235  * Pair Kerning section of the AFM documentation (full title
236  * & version given above).
237  */
238 typedef struct
239 {
240     char *name1;
241     char *name2;
242     int xamt,
243         yamt;
244 } PairKernData;
245 
246 
247 /* PCC is a piece of a composite character. This is a sub structure of a
248  * compCharData described below.
249  * These fields will be filled in with the values from the key PCC.
250  *
251  * For an explanation about each key and its value please refer to the
252  * Composite Character section of the AFM documentation (full title
253  * & version given above).
254  */
255 typedef struct
256 {
257     char *pccName;
258     int deltax,
259         deltay;
260 } Pcc;
261 
262 
263 /* Composite Character Information data structure.
264  * The fields ccName and numOfPieces are filled with the values associated
265  * with the key CC. The field pieces points to an array (size = numOfPieces)
266  * of information about each of the parts of the composite character. That
267  * array is filled in with the values from the key PCC.
268  *
269  * For an explanation about each key and its value please refer to the
270  * Composite Character section of the AFM documentation (full title
271  * & version given above).
272  */
273 typedef struct
274 {
275     char *ccName;
276     int numOfPieces;
277     Pcc *pieces;
278 } CompCharData;
279 
280 
281 /*  FontInfo
282  *  Record type containing pointers to all of the other data
283  *  structures containing information about a font.
284  *  A a record of this type is filled with data by the
285  *  parseFile function.
286  */
287 typedef struct
288 {
289     GlobalFontInfo *gfi;	/* ptr to a GlobalFontInfo record */
290     int *cwi;			/* ptr to 256 element array of just char widths */
291     int numOfChars;		/* number of entries in char metrics array */
292     CharMetricInfo *cmi;	/* ptr to char metrics array */
293     int numOfTracks;		/* number to entries in track kerning array */
294     TrackKernData *tkd;		/* ptr to track kerning array */
295     int numOfPairs;		/* number to entries in pair kerning array */
296     PairKernData *pkd;		/* ptr to pair kerning array */
297     int numOfComps;		/* number to entries in comp char array */
298     CompCharData *ccd;		/* ptr to comp char array */
299 } FontInfo;
300 
301 
302 
303 /************************* PROCEDURES ****************************/
304 
305 /*  Call this procedure to do the grunt work of parsing an AFM file.
306  *
307  *  "fp" should be a valid file pointer to an AFM file.
308  *
309  *  "fi" is a pointer to a pointer to a FontInfo record sturcture
310  *  (defined above). Storage for the FontInfo structure will be
311  *  allocated in parseFile and the structure will be filled in
312  *  with the requested data from the AFM File.
313  *
314  *  "flags" is a mask with bits set representing what data should
315  *  be saved. Defined above are valid flags that can be used to set
316  *  the mask, as well as a few commonly used masks.
317  *
318  *  The possible return codes from parseFile are defined above.
319  */
320 
321 extern int parseFile ( /* FILE *fp; FontInfo **fi; FLAGS flags; */ );
322