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