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