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