]> Shamusworld >> Repos - ttedit/blob - src/ttf.h
2ea45be926f220cc29ced99bd2c378f38cc02189
[ttedit] / src / ttf.h
1 //
2 // TTF.H: TTF Class
3 // by James L. Hammons
4 // (C) 1999 Underground Software
5 //
6 // This class encapsulates all the complexity of a TrueType Font File
7 // database.  Included are functions to load, save, & initialize the
8 // TTF database.
9 //
10
11 // These really shouldn't be included here, but are for now...
12 //#include <string.h>
13 ////#include <afxwin.h>  // This is needed only for things like BOOL, etc.
14 //#include <fstream.h>
15 //#include <stdlib.h>
16 //#include <malloc.h>  // Both for malloc, free, _msize, realloc
17
18 #ifndef __TTF_H__
19 #define __TTF_H__
20
21 #include "list.h"
22 #include "types.h"
23 #include "glyphpoints.h"
24
25 #define MAXGLYPHS 4096                  // Number of glyphs in object
26 #define MAXPOINTS 2000                  // Number of points in a glyph
27 #define MAXHINTS  2000                  // Number of hint uint8s in glyph
28
29 //
30 // "head" struct
31 //
32 struct Head
33 {
34         uint32 version;                         // Fixed - Table version number (0x00010000 for version 1.0.)
35         uint32 fontRevision;            // Fixed - fontRevision (Set by font manufacturer.)
36         uint32 checkSumAdjustment;      // To compute: Set it to 0, sum the entire font
37                                                                 // as ULONG, then store 0xB1B0AFBA - sum.
38         uint32 magicNumber;                     // Set to 0x5F0F3CF5.
39         uint16 flags;                           /* Bit 0 - baseline for font at y=0;
40                                                                    Bit 1 - left sidebearing at x=0;
41                                                                    Bit 2 - instructions may depend on point     size;
42                                                                    Bit 3 - force ppem to integer values for all
43                                                                                         internal scaler math; may use
44                                                                                         fractional ppem sizes if this bit is clear;
45                                                                    Bit 4 - instructions may alter advance width
46                                                                                         (the advance widths might not scale linearly);
47                                                                    Note: All other bits must be zero.*/
48         uint16 unitsPerEm;                      // Valid range is from 16 to 16384
49         uint32 /*longDateTime*/ createdh, createdl;             // International date (8-uint8 field).
50         uint32 /*longDateTime*/ modifiedh, modifiedl;   // International date (8-uint8 field).
51         uint16 xMin;                            // For all glyph bounding boxes.
52         uint16 yMin;                            // For all glyph bounding boxes.
53         uint16 xMax;                            // For all glyph bounding boxes.
54         uint16 yMax;                            // For all glyph bounding boxes.
55         uint16 macStyle;                        /* Bit 0 bold (if set to 1); Bit 1 italic (if set to 1)
56                                                                    Bits 2-15 reserved (set to 0).*/
57         uint16 lowestRecPPEM;           // Smallest readable size in pixels.
58         int16  fontDirectionHint;       /* 0   Fully mixed directional glyphs;
59                                                                    1   Only strongly left to right;
60                                                                    2   Like 1 but also contains neutrals;
61                                                                   -1   Only strongly right to left;
62                                                                   -2   Like -1 but also contains neutrals.*/
63         int16  indexToLocFormat;        // 0 for short offsets, 1 for long.
64         int16  glyphDataFormat;         // 0 for current format.
65 };
66
67 //
68 // "maxp" struct
69 //
70 struct Maxp
71 {
72         uint32 version;                                 // Table version number 0x00010000 for version 1.0. 
73         uint16 numGlyphs;                               // The number of glyphs in the font. 
74         uint16 maxPoints;                               // Maximum points in a non-composite glyph. 
75         uint16 maxContours;                             // Maximum contours in a non-composite glyph. 
76         uint16 maxCompositePoints;              // Maximum points in a composite glyph. 
77         uint16 maxCompositeContours;    // Maximum contours in a composite glyph. 
78         uint16 maxZones;                                // 1 if instructions do not use the twilight
79                                                                         // zone (Z0), or 2 if instructions do use Z0;
80                                                                         // should be set to 2 in most cases. 
81         uint16 maxTwilightPoints;               // Maximum points used in Z0. 
82         uint16 maxStorage;                              // Number of Storage Area locations.  
83         uint16 maxFunctionDefs;                 // Number of FDEFs. 
84         uint16 maxInstructionDefs;              // Number of IDEFs. 
85         uint16 maxStackElements;                // Maximum stack depth. 
86         uint16 maxSizeOfInstructions;   // Maximum uint8 count for glyph instructions. 
87         uint16 maxComponentElements;    // Maximum number of components referenced at
88                                                                         // "top level" for any composite glyph. 
89         uint16 maxComponentDepth;               // Maximum levels of recursion; 1 for simple components.
90 };
91
92 //
93 // "cmap" struct
94 //
95 /*
96 USHORT  Table version number (0).
97 USHORT  Number of encoding tables, n.   
98
99 This is followed by an entry for each of the n encoding table specifying the
100 particular encoding, and the offset to the actual subtable:
101
102 Type    Description     
103 USHORT  Platform ID.    
104 USHORT  Platform-specific encoding ID.  
105 ULONG   uint8 offset from beginning of table to the subtable for this encoding. 
106
107 USHORT  format          Format number is set to 4.  
108 USHORT  length          Length in uint8s.  
109 USHORT  version Version number (starts at 0).   
110 USHORT  segCountX2      2 x segCount.   
111 USHORT  searchRange     2 x (2**floor(log2(segCount)))  
112 USHORT  entrySelector   log2(searchRange/2)     
113 USHORT  rangeShift      2 x segCount - searchRange      
114 USHORT  endCount[segCount]      End characterCode for each segment,
115                         last =0xFFFF. 
116 USHORT  reservedPad     Set to 0.       
117 USHORT  startCount[segCount]    Start character code for each segment.  
118 USHORT  idDelta[segCount]       Delta for all character codes in segment.       
119 USHORT  idRangeOffset[segCount] Offsets into glyphIdArray or 0  
120 USHORT  glyphIdArray[ ] Glyph index array (arbitrary length)    
121
122 The number of segments is specified by segCount, which is not explicitly in
123 the header; however, all of the header parameters are derived from it. The
124 searchRange value is twice the largest power of 2 that is less than or equal
125 to  segCount. For example, if segCount=39, we have the following:
126
127 segCountX2      78
128 searchRange     64      (2 * largest power of 2 <= 39)
129 entrySelector   5       log2(32)
130 rangeShift      14      2 x 39 - 64
131 */
132
133 struct GlyphPt
134 {
135         int x, y;
136         bool onCurve;
137
138         // Default constructor
139         GlyphPt(int xx = 0, int yy = 0, bool oc = true): x(xx), y(yy), onCurve(oc) {}
140 };
141
142 struct Box
143 {
144         int16 llx, lly, urx, ury;
145
146         // Default constructor
147         Box(int16 lx = 0, int16 ly = 0, int16 rx = 0, int16 ry = 0):
148                 llx(lx), lly(ly), urx(rx), ury(ry) {}
149 };
150
151 struct NameRec
152 {
153         uint16 platformID, platformSpecID, languageID, nameID, length;
154         uint8 * str;
155
156         NameRec(): str(NULL) {}
157
158         NameRec(NameRec &c): str(NULL)
159         {
160                 *this = c;
161         }
162
163         ~NameRec()
164         {
165                 if (str)
166                         delete[] str;
167         }
168
169         void operator=(const NameRec &c)
170         {
171                 if (this == &c)
172                         return;                                                         // Take care of self-assignment
173
174                 if (str)
175                         delete[] str;
176
177                 platformID     = c.platformID;
178                 platformSpecID = c.platformSpecID;
179                 languageID     = c.languageID;
180                 nameID         = c.nameID;
181                 length         = c.length;
182 //Fix this              str            = (uint8 *)strdup((char *)c.str);
183         }
184 };
185
186 struct Composite
187 {
188         uint16 flags, glyphIndex;
189         int16 arg1, arg2;
190         float xScale, yScale, scale01, scale10;
191
192         Composite(uint16 fl = 0, uint16 gl = 0, int16 a1 = 0, int16 a2 = 0,
193                 float xs = 1.0f, float ys = 1.0f, float s01 = 1.0f, float s10 = 1.0f):
194                 flags(fl), glyphIndex(gl), arg1(a1), arg2(a2), xScale(xs), yScale(ys),
195                 scale01(s01), scale10(s10) {}
196 };
197
198 //
199 // The TTF class
200 //
201 // The following is a set of member functions that needs to be implemented
202 // to fully encapsulate a TTF database.
203 //
204 // SetGlyph(glyphno)
205 // DeleteGlyph(glyphno)
206 // AddGlyph()
207 // GetNumberOfPolys()
208 // GetPolyStart(polyno)
209 // SetPolyStart(polyno)
210 // AddPoly(polyno)
211 // DeletePoly(polyno)
212 // IsCompositeGlyph()
213 // MovePoint(pointno, x, y)
214 // AddPoint(pointno, x, y)
215 // DeletePoint(pointno)
216 // maybe:  TGlyphPt GetPointInfo(pointno) instead of three following...
217 // GetPointX(pointno)
218 // GetPointY(pointno)
219 // GetOnCurve(pointno)
220 // SetOnCurve(pointno)
221 // ConnectGlyphToChar(glyphno, charno)
222 // EncodeGlyph(glyphno)
223 // DecodeGlyph(glyphno)
224 // GetNumberOfPoints()
225 // SwapPoints(pointno1, pointno2)
226 //
227 // More to be added, as necessary...
228 //
229 class TTF
230 {
231         private:
232                 bool EncodeGlyph(uint16);
233                 bool DecodeGlyph(uint16);
234                 bool ExtractTables(void);
235                 bool BuildTables(void);
236
237         public:
238                 TTF(void);                                                              // Constructor
239                 ~TTF();                                                                 // Destructor
240                 void Init(void);                                                // Initalize font database
241                 bool Load(const char *);                                // Load font database
242                 bool Save(const char *);                                // Save font database
243                 void ClearTables(void);                                 // Deallocate memory...
244                 bool SetGlyph(uint16 glyphnum);                 // Set internal pointer/encode/decode
245                 bool AddGlyph(uint16 glyphnum);
246                 bool DeleteGlyph(uint16 glyphnum);
247                 Box GetBoundingBox(void);
248                 GlyphPt GetPoint(uint16 pointno);
249                 uint16 GetNumberOfPolys(void);
250                 uint16 GetPolyEnd(uint16 polynum);
251                 int GetPointX(uint16 pointno);
252                 int GetPointY(uint16 pointno);
253                 bool GetOnCurve(uint16 pointno);
254                 bool SetOnCurve(uint16 pointno, bool state);
255                 bool MovePoint(uint16 pointno, int x, int y);
256                 bool MovePoint(uint16 pointno, GlyphPt p);
257                 bool AddPoint(uint16 pointno, int x, int y, bool state);
258                 bool DeletePoint(uint16 pointno);
259                 bool IsCompositeGlyph(void);
260                 GlyphPoints GetGlyphPoints(uint16);
261                 GlyphPoints GetAllCompositePoints(uint16);
262                 void GetCharName(int, uint8 *);
263
264         private:
265                 bool isDirty;                                                   // Has glyph been modified?
266                 bool isCompositeGlyph;                                  // Is it a composite? (need structs to store composite info...)
267                 uint16 currentGlyph;                                    // The glyph currently decoded
268                 uint16 numberOfPoints;
269                 int gx[MAXPOINTS];                                              // Internal point arrays...
270                 int gy[MAXPOINTS];                                              // These could be dynamically allocated in the
271                 bool onCurve[MAXPOINTS];                                // constructor, with default values being set
272                 uint16 numberOfHints;
273                 uint8 hint[MAXHINTS];                                   // Hints storage
274                 uint16 numberOfPolys;                                   // Number of polygons in glyph
275                 uint16 poly[200];                                               // End points of polygons
276                 int16 llx, lly, urx, ury;                               // Lower left/Upper right X/Y
277 //      public://remove after debugging...
278                 uint8 * glyph[MAXGLYPHS];                               // Placeholders for glyphs
279                 uint32 glyphLen[MAXGLYPHS];                             // Glyph lengths are stored since malloc() is sloppy
280                 Maxp myMaxp;                                                    // Internal placeholders for this shiat
281                 Head myHead;
282 //      public: //private: later...
283                 List<Composite> compositeList;                  // Composite glyph list
284                 uint8 * EBDT, * EBLC, * EBSC, * LTSH, * OS_2, * PCLT, * VDMX, * cmap, * cvt,  * fpgm,
285                         * gasp, * glyf, * hdmx, * head, * hhea, * hmtx, * kern, * loca, * maxp, * name,
286                         * post, * prep, * vhea, * vmtx;
287                 uint32 EBDT_len, EBLC_len, EBSC_len, LTSH_len, OS_2_len, PCLT_len, VDMX_len, cmap_len,
288                         cvt_len,  fpgm_len, gasp_len, glyf_len, hdmx_len, head_len, hhea_len, hmtx_len,
289                         kern_len, loca_len, maxp_len, name_len, post_len, prep_len, vhea_len, vmtx_len;
290                 uint8 * * parray[24];                                   // Pointer array
291                 uint32 * larray[24];                                    // Length array
292                 bool loaded;                                                    // Font loaded/init'ed?
293 };
294
295 #endif  // __TTF_H__
296