X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fttf.h;h=2ea45be926f220cc29ced99bd2c378f38cc02189;hb=c84263bb8b0d16e4c6da49aa0b7d0bc904ae02b1;hp=07aac36a044eff6397e9a04c2fec3aed38d0e4e8;hpb=5c7c36b21d0f2a88accd8ff12c05dcc3004ce0e8;p=ttedit diff --git a/src/ttf.h b/src/ttf.h index 07aac36..2ea45be 100755 --- a/src/ttf.h +++ b/src/ttf.h @@ -1,296 +1,296 @@ -// -// TTF.H: TTF Class -// by James L. Hammons -// (C) 1999 Underground Software -// -// This class encapsulates all the complexity of a TrueType Font File -// database. Included are functions to load, save, & initialize the -// TTF database. -// - -// These really shouldn't be included here, but are for now... -//#include -////#include // This is needed only for things like BOOL, etc. -//#include -//#include -//#include // Both for malloc, free, _msize, realloc - -#ifndef __TTF_H__ -#define __TTF_H__ - -#include "list.h" -#include "types.h" -#include "glyphpoints.h" - -#define MAXGLYPHS 4096 // Number of glyphs in object -#define MAXPOINTS 2000 // Number of points in a glyph -#define MAXHINTS 2000 // Number of hint uint8s in glyph - -// -// "head" struct -// -struct Head -{ - uint32 version; // Fixed - Table version number (0x00010000 for version 1.0.) - uint32 fontRevision; // Fixed - fontRevision (Set by font manufacturer.) - uint32 checkSumAdjustment; // To compute: Set it to 0, sum the entire font - // as ULONG, then store 0xB1B0AFBA - sum. - uint32 magicNumber; // Set to 0x5F0F3CF5. - uint16 flags; /* Bit 0 - baseline for font at y=0; - Bit 1 - left sidebearing at x=0; - Bit 2 - instructions may depend on point size; - Bit 3 - force ppem to integer values for all - internal scaler math; may use - fractional ppem sizes if this bit is clear; - Bit 4 - instructions may alter advance width - (the advance widths might not scale linearly); - Note: All other bits must be zero.*/ - uint16 unitsPerEm; // Valid range is from 16 to 16384 - uint32 /*longDateTime*/ createdh, createdl; // International date (8-uint8 field). - uint32 /*longDateTime*/ modifiedh, modifiedl; // International date (8-uint8 field). - uint16 xMin; // For all glyph bounding boxes. - uint16 yMin; // For all glyph bounding boxes. - uint16 xMax; // For all glyph bounding boxes. - uint16 yMax; // For all glyph bounding boxes. - uint16 macStyle; /* Bit 0 bold (if set to 1); Bit 1 italic (if set to 1) - Bits 2-15 reserved (set to 0).*/ - uint16 lowestRecPPEM; // Smallest readable size in pixels. - int16 fontDirectionHint; /* 0 Fully mixed directional glyphs; - 1 Only strongly left to right; - 2 Like 1 but also contains neutrals; - -1 Only strongly right to left; - -2 Like -1 but also contains neutrals.*/ - int16 indexToLocFormat; // 0 for short offsets, 1 for long. - int16 glyphDataFormat; // 0 for current format. -}; - -// -// "maxp" struct -// -struct Maxp -{ - uint32 version; // Table version number 0x00010000 for version 1.0. - uint16 numGlyphs; // The number of glyphs in the font. - uint16 maxPoints; // Maximum points in a non-composite glyph. - uint16 maxContours; // Maximum contours in a non-composite glyph. - uint16 maxCompositePoints; // Maximum points in a composite glyph. - uint16 maxCompositeContours; // Maximum contours in a composite glyph. - uint16 maxZones; // 1 if instructions do not use the twilight - // zone (Z0), or 2 if instructions do use Z0; - // should be set to 2 in most cases. - uint16 maxTwilightPoints; // Maximum points used in Z0. - uint16 maxStorage; // Number of Storage Area locations. - uint16 maxFunctionDefs; // Number of FDEFs. - uint16 maxInstructionDefs; // Number of IDEFs. - uint16 maxStackElements; // Maximum stack depth. - uint16 maxSizeOfInstructions; // Maximum uint8 count for glyph instructions. - uint16 maxComponentElements; // Maximum number of components referenced at - // "top level" for any composite glyph. - uint16 maxComponentDepth; // Maximum levels of recursion; 1 for simple components. -}; - -// -// "cmap" struct -// -/* -USHORT Table version number (0). -USHORT Number of encoding tables, n. - -This is followed by an entry for each of the n encoding table specifying the -particular encoding, and the offset to the actual subtable: - -Type Description -USHORT Platform ID. -USHORT Platform-specific encoding ID. -ULONG uint8 offset from beginning of table to the subtable for this encoding. - -USHORT format Format number is set to 4. -USHORT length Length in uint8s. -USHORT version Version number (starts at 0). -USHORT segCountX2 2 x segCount. -USHORT searchRange 2 x (2**floor(log2(segCount))) -USHORT entrySelector log2(searchRange/2) -USHORT rangeShift 2 x segCount - searchRange -USHORT endCount[segCount] End characterCode for each segment, - last =0xFFFF. -USHORT reservedPad Set to 0. -USHORT startCount[segCount] Start character code for each segment. -USHORT idDelta[segCount] Delta for all character codes in segment. -USHORT idRangeOffset[segCount] Offsets into glyphIdArray or 0 -USHORT glyphIdArray[ ] Glyph index array (arbitrary length) - -The number of segments is specified by segCount, which is not explicitly in -the header; however, all of the header parameters are derived from it. The -searchRange value is twice the largest power of 2 that is less than or equal -to segCount. For example, if segCount=39, we have the following: - -segCountX2 78 -searchRange 64 (2 * largest power of 2 <= 39) -entrySelector 5 log2(32) -rangeShift 14 2 x 39 - 64 -*/ - -struct GlyphPt -{ - int x, y; - bool onCurve; - - // Default constructor - GlyphPt(int xx = 0, int yy = 0, bool oc = true): x(xx), y(yy), onCurve(oc) {} -}; - -struct Box -{ - int16 llx, lly, urx, ury; - - // Default constructor - Box(int16 lx = 0, int16 ly = 0, int16 rx = 0, int16 ry = 0): - llx(lx), lly(ly), urx(rx), ury(ry) {} -}; - -struct NameRec -{ - uint16 platformID, platformSpecID, languageID, nameID, length; - uint8 * str; - - NameRec(): str(NULL) {} - - NameRec(NameRec &c): str(NULL) - { - *this = c; - } - - ~NameRec() - { - if (str) - delete[] str; - } - - void operator=(const NameRec &c) - { - if (this == &c) - return; // Take care of self-assignment - - if (str) - delete[] str; - - platformID = c.platformID; - platformSpecID = c.platformSpecID; - languageID = c.languageID; - nameID = c.nameID; - length = c.length; -//Fix this str = (uint8 *)strdup((char *)c.str); - } -}; - -struct Composite -{ - uint16 flags, glyphIndex; - int16 arg1, arg2; - float xScale, yScale, scale01, scale10; - - Composite(uint16 fl = 0, uint16 gl = 0, int16 a1 = 0, int16 a2 = 0, - float xs = 1.0f, float ys = 1.0f, float s01 = 1.0f, float s10 = 1.0f): - flags(fl), glyphIndex(gl), arg1(a1), arg2(a2), xScale(xs), yScale(ys), - scale01(s01), scale10(s10) {} -}; - -// -// The TTF class -// -// The following is a set of member functions that needs to be implemented -// to fully encapsulate a TTF database. -// -// SetGlyph(glyphno) -// DeleteGlyph(glyphno) -// AddGlyph() -// GetNumberOfPolys() -// GetPolyStart(polyno) -// SetPolyStart(polyno) -// AddPoly(polyno) -// DeletePoly(polyno) -// IsCompositeGlyph() -// MovePoint(pointno, x, y) -// AddPoint(pointno, x, y) -// DeletePoint(pointno) -// maybe: TGlyphPt GetPointInfo(pointno) instead of three following... -// GetPointX(pointno) -// GetPointY(pointno) -// GetOnCurve(pointno) -// SetOnCurve(pointno) -// ConnectGlyphToChar(glyphno, charno) -// EncodeGlyph(glyphno) -// DecodeGlyph(glyphno) -// GetNumberOfPoints() -// SwapPoints(pointno1, pointno2) -// -// More to be added, as necessary... -// -class TTF -{ - private: - bool EncodeGlyph(uint16); - bool DecodeGlyph(uint16); - bool ExtractTables(void); - bool BuildTables(void); - - public: - TTF(void); // Constructor - ~TTF(); // Destructor - void Init(void); // Initalize font database - bool Load(const char *); // Load font database - bool Save(const char *); // Save font database - void ClearTables(void); // Deallocate memory... - bool SetGlyph(uint16 glyphnum); // Set internal pointer/encode/decode - bool AddGlyph(uint16 glyphnum); - bool DeleteGlyph(uint16 glyphnum); - Box GetBoundingBox(void); - GlyphPt GetPoint(uint16 pointno); - uint16 GetNumberOfPolys(void); - uint16 GetPolyEnd(uint16 polynum); - int GetPointX(uint16 pointno); - int GetPointY(uint16 pointno); - bool GetOnCurve(uint16 pointno); - bool SetOnCurve(uint16 pointno, bool state); - bool MovePoint(uint16 pointno, int x, int y); - bool MovePoint(uint16 pointno, GlyphPt p); - bool AddPoint(uint16 pointno, int x, int y, bool state); - bool DeletePoint(uint16 pointno); - bool IsCompositeGlyph(void); - GlyphPoints GetGlyphPoints(uint16); - GlyphPoints GetAllCompositePoints(uint16); - void GetCharName(int, uint8 *); - - private: - bool isDirty; // Has glyph been modified? - bool isCompositeGlyph; // Is it a composite? (need structs to store composite info...) - uint16 currentGlyph; // The glyph currently decoded - uint16 numberOfPoints; - int gx[MAXPOINTS]; // Internal point arrays... - int gy[MAXPOINTS]; // These could be dynamically allocated in the - bool onCurve[MAXPOINTS]; // constructor, with default values being set - uint16 numberOfHints; - uint8 hint[MAXHINTS]; // Hints storage - uint16 numberOfPolys; // Number of polygons in glyph - uint16 poly[200]; // End points of polygons - int16 llx, lly, urx, ury; // Lower left/Upper right X/Y -// public://remove after debugging... - uint8 * glyph[MAXGLYPHS]; // Placeholders for glyphs - uint32 glyphLen[MAXGLYPHS]; // Glyph lengths are stored since malloc() is sloppy - Maxp myMaxp; // Internal placeholders for this shiat - Head myHead; -// public: //private: later... - List compositeList; // Composite glyph list - uint8 * EBDT, * EBLC, * EBSC, * LTSH, * OS_2, * PCLT, * VDMX, * cmap, * cvt, * fpgm, - * gasp, * glyf, * hdmx, * head, * hhea, * hmtx, * kern, * loca, * maxp, * name, - * post, * prep, * vhea, * vmtx; - uint32 EBDT_len, EBLC_len, EBSC_len, LTSH_len, OS_2_len, PCLT_len, VDMX_len, cmap_len, - cvt_len, fpgm_len, gasp_len, glyf_len, hdmx_len, head_len, hhea_len, hmtx_len, - kern_len, loca_len, maxp_len, name_len, post_len, prep_len, vhea_len, vmtx_len; - uint8 * * parray[24]; // Pointer array - uint32 * larray[24]; // Length array - bool loaded; // Font loaded/init'ed? -}; - -#endif // __TTF_H__ - +// +// TTF.H: TTF Class +// by James L. Hammons +// (C) 1999 Underground Software +// +// This class encapsulates all the complexity of a TrueType Font File +// database. Included are functions to load, save, & initialize the +// TTF database. +// + +// These really shouldn't be included here, but are for now... +//#include +////#include // This is needed only for things like BOOL, etc. +//#include +//#include +//#include // Both for malloc, free, _msize, realloc + +#ifndef __TTF_H__ +#define __TTF_H__ + +#include "list.h" +#include "types.h" +#include "glyphpoints.h" + +#define MAXGLYPHS 4096 // Number of glyphs in object +#define MAXPOINTS 2000 // Number of points in a glyph +#define MAXHINTS 2000 // Number of hint uint8s in glyph + +// +// "head" struct +// +struct Head +{ + uint32 version; // Fixed - Table version number (0x00010000 for version 1.0.) + uint32 fontRevision; // Fixed - fontRevision (Set by font manufacturer.) + uint32 checkSumAdjustment; // To compute: Set it to 0, sum the entire font + // as ULONG, then store 0xB1B0AFBA - sum. + uint32 magicNumber; // Set to 0x5F0F3CF5. + uint16 flags; /* Bit 0 - baseline for font at y=0; + Bit 1 - left sidebearing at x=0; + Bit 2 - instructions may depend on point size; + Bit 3 - force ppem to integer values for all + internal scaler math; may use + fractional ppem sizes if this bit is clear; + Bit 4 - instructions may alter advance width + (the advance widths might not scale linearly); + Note: All other bits must be zero.*/ + uint16 unitsPerEm; // Valid range is from 16 to 16384 + uint32 /*longDateTime*/ createdh, createdl; // International date (8-uint8 field). + uint32 /*longDateTime*/ modifiedh, modifiedl; // International date (8-uint8 field). + uint16 xMin; // For all glyph bounding boxes. + uint16 yMin; // For all glyph bounding boxes. + uint16 xMax; // For all glyph bounding boxes. + uint16 yMax; // For all glyph bounding boxes. + uint16 macStyle; /* Bit 0 bold (if set to 1); Bit 1 italic (if set to 1) + Bits 2-15 reserved (set to 0).*/ + uint16 lowestRecPPEM; // Smallest readable size in pixels. + int16 fontDirectionHint; /* 0 Fully mixed directional glyphs; + 1 Only strongly left to right; + 2 Like 1 but also contains neutrals; + -1 Only strongly right to left; + -2 Like -1 but also contains neutrals.*/ + int16 indexToLocFormat; // 0 for short offsets, 1 for long. + int16 glyphDataFormat; // 0 for current format. +}; + +// +// "maxp" struct +// +struct Maxp +{ + uint32 version; // Table version number 0x00010000 for version 1.0. + uint16 numGlyphs; // The number of glyphs in the font. + uint16 maxPoints; // Maximum points in a non-composite glyph. + uint16 maxContours; // Maximum contours in a non-composite glyph. + uint16 maxCompositePoints; // Maximum points in a composite glyph. + uint16 maxCompositeContours; // Maximum contours in a composite glyph. + uint16 maxZones; // 1 if instructions do not use the twilight + // zone (Z0), or 2 if instructions do use Z0; + // should be set to 2 in most cases. + uint16 maxTwilightPoints; // Maximum points used in Z0. + uint16 maxStorage; // Number of Storage Area locations. + uint16 maxFunctionDefs; // Number of FDEFs. + uint16 maxInstructionDefs; // Number of IDEFs. + uint16 maxStackElements; // Maximum stack depth. + uint16 maxSizeOfInstructions; // Maximum uint8 count for glyph instructions. + uint16 maxComponentElements; // Maximum number of components referenced at + // "top level" for any composite glyph. + uint16 maxComponentDepth; // Maximum levels of recursion; 1 for simple components. +}; + +// +// "cmap" struct +// +/* +USHORT Table version number (0). +USHORT Number of encoding tables, n. + +This is followed by an entry for each of the n encoding table specifying the +particular encoding, and the offset to the actual subtable: + +Type Description +USHORT Platform ID. +USHORT Platform-specific encoding ID. +ULONG uint8 offset from beginning of table to the subtable for this encoding. + +USHORT format Format number is set to 4. +USHORT length Length in uint8s. +USHORT version Version number (starts at 0). +USHORT segCountX2 2 x segCount. +USHORT searchRange 2 x (2**floor(log2(segCount))) +USHORT entrySelector log2(searchRange/2) +USHORT rangeShift 2 x segCount - searchRange +USHORT endCount[segCount] End characterCode for each segment, + last =0xFFFF. +USHORT reservedPad Set to 0. +USHORT startCount[segCount] Start character code for each segment. +USHORT idDelta[segCount] Delta for all character codes in segment. +USHORT idRangeOffset[segCount] Offsets into glyphIdArray or 0 +USHORT glyphIdArray[ ] Glyph index array (arbitrary length) + +The number of segments is specified by segCount, which is not explicitly in +the header; however, all of the header parameters are derived from it. The +searchRange value is twice the largest power of 2 that is less than or equal +to segCount. For example, if segCount=39, we have the following: + +segCountX2 78 +searchRange 64 (2 * largest power of 2 <= 39) +entrySelector 5 log2(32) +rangeShift 14 2 x 39 - 64 +*/ + +struct GlyphPt +{ + int x, y; + bool onCurve; + + // Default constructor + GlyphPt(int xx = 0, int yy = 0, bool oc = true): x(xx), y(yy), onCurve(oc) {} +}; + +struct Box +{ + int16 llx, lly, urx, ury; + + // Default constructor + Box(int16 lx = 0, int16 ly = 0, int16 rx = 0, int16 ry = 0): + llx(lx), lly(ly), urx(rx), ury(ry) {} +}; + +struct NameRec +{ + uint16 platformID, platformSpecID, languageID, nameID, length; + uint8 * str; + + NameRec(): str(NULL) {} + + NameRec(NameRec &c): str(NULL) + { + *this = c; + } + + ~NameRec() + { + if (str) + delete[] str; + } + + void operator=(const NameRec &c) + { + if (this == &c) + return; // Take care of self-assignment + + if (str) + delete[] str; + + platformID = c.platformID; + platformSpecID = c.platformSpecID; + languageID = c.languageID; + nameID = c.nameID; + length = c.length; +//Fix this str = (uint8 *)strdup((char *)c.str); + } +}; + +struct Composite +{ + uint16 flags, glyphIndex; + int16 arg1, arg2; + float xScale, yScale, scale01, scale10; + + Composite(uint16 fl = 0, uint16 gl = 0, int16 a1 = 0, int16 a2 = 0, + float xs = 1.0f, float ys = 1.0f, float s01 = 1.0f, float s10 = 1.0f): + flags(fl), glyphIndex(gl), arg1(a1), arg2(a2), xScale(xs), yScale(ys), + scale01(s01), scale10(s10) {} +}; + +// +// The TTF class +// +// The following is a set of member functions that needs to be implemented +// to fully encapsulate a TTF database. +// +// SetGlyph(glyphno) +// DeleteGlyph(glyphno) +// AddGlyph() +// GetNumberOfPolys() +// GetPolyStart(polyno) +// SetPolyStart(polyno) +// AddPoly(polyno) +// DeletePoly(polyno) +// IsCompositeGlyph() +// MovePoint(pointno, x, y) +// AddPoint(pointno, x, y) +// DeletePoint(pointno) +// maybe: TGlyphPt GetPointInfo(pointno) instead of three following... +// GetPointX(pointno) +// GetPointY(pointno) +// GetOnCurve(pointno) +// SetOnCurve(pointno) +// ConnectGlyphToChar(glyphno, charno) +// EncodeGlyph(glyphno) +// DecodeGlyph(glyphno) +// GetNumberOfPoints() +// SwapPoints(pointno1, pointno2) +// +// More to be added, as necessary... +// +class TTF +{ + private: + bool EncodeGlyph(uint16); + bool DecodeGlyph(uint16); + bool ExtractTables(void); + bool BuildTables(void); + + public: + TTF(void); // Constructor + ~TTF(); // Destructor + void Init(void); // Initalize font database + bool Load(const char *); // Load font database + bool Save(const char *); // Save font database + void ClearTables(void); // Deallocate memory... + bool SetGlyph(uint16 glyphnum); // Set internal pointer/encode/decode + bool AddGlyph(uint16 glyphnum); + bool DeleteGlyph(uint16 glyphnum); + Box GetBoundingBox(void); + GlyphPt GetPoint(uint16 pointno); + uint16 GetNumberOfPolys(void); + uint16 GetPolyEnd(uint16 polynum); + int GetPointX(uint16 pointno); + int GetPointY(uint16 pointno); + bool GetOnCurve(uint16 pointno); + bool SetOnCurve(uint16 pointno, bool state); + bool MovePoint(uint16 pointno, int x, int y); + bool MovePoint(uint16 pointno, GlyphPt p); + bool AddPoint(uint16 pointno, int x, int y, bool state); + bool DeletePoint(uint16 pointno); + bool IsCompositeGlyph(void); + GlyphPoints GetGlyphPoints(uint16); + GlyphPoints GetAllCompositePoints(uint16); + void GetCharName(int, uint8 *); + + private: + bool isDirty; // Has glyph been modified? + bool isCompositeGlyph; // Is it a composite? (need structs to store composite info...) + uint16 currentGlyph; // The glyph currently decoded + uint16 numberOfPoints; + int gx[MAXPOINTS]; // Internal point arrays... + int gy[MAXPOINTS]; // These could be dynamically allocated in the + bool onCurve[MAXPOINTS]; // constructor, with default values being set + uint16 numberOfHints; + uint8 hint[MAXHINTS]; // Hints storage + uint16 numberOfPolys; // Number of polygons in glyph + uint16 poly[200]; // End points of polygons + int16 llx, lly, urx, ury; // Lower left/Upper right X/Y +// public://remove after debugging... + uint8 * glyph[MAXGLYPHS]; // Placeholders for glyphs + uint32 glyphLen[MAXGLYPHS]; // Glyph lengths are stored since malloc() is sloppy + Maxp myMaxp; // Internal placeholders for this shiat + Head myHead; +// public: //private: later... + List compositeList; // Composite glyph list + uint8 * EBDT, * EBLC, * EBSC, * LTSH, * OS_2, * PCLT, * VDMX, * cmap, * cvt, * fpgm, + * gasp, * glyf, * hdmx, * head, * hhea, * hmtx, * kern, * loca, * maxp, * name, + * post, * prep, * vhea, * vmtx; + uint32 EBDT_len, EBLC_len, EBSC_len, LTSH_len, OS_2_len, PCLT_len, VDMX_len, cmap_len, + cvt_len, fpgm_len, gasp_len, glyf_len, hdmx_len, head_len, hhea_len, hmtx_len, + kern_len, loca_len, maxp_len, name_len, post_len, prep_len, vhea_len, vmtx_len; + uint8 * * parray[24]; // Pointer array + uint32 * larray[24]; // Length array + bool loaded; // Font loaded/init'ed? +}; + +#endif // __TTF_H__ +