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