3
Copyright (c) 2006-2007, BBR Inc. All rights reserved.
5
Permission is hereby granted, free of charge, to any person obtaining
6
a copy of this software and associated documentation files (the
7
"Software"), to deal in the Software without restriction, including
8
without limitation the rights to use, copy, modify, merge, publish,
9
distribute, sublicense, and/or sell copies of the Software, and to
10
permit persons to whom the Software is furnished to do so, subject to
11
the following conditions:
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
#ifndef _PDFFTRUTYPEFONT_H_
30
#define _PDFFTRUTYPEFONT_H_
33
#include "UGooString.h"
35
#include "P2POutputStream.h"
38
class PDFFTrueTypeFont {
40
typedef unsigned char BYTE;
41
typedef signed char CHAR;
42
typedef unsigned short USHORT;
43
typedef signed short SHORT;
44
typedef unsigned long ULONG;
45
typedef signed long LONG;
46
typedef unsigned long Fixed;
47
typedef unsigned int FUNIT;
48
typedef signed short FWORD;
49
typedef unsigned short UFWORD;
50
typedef unsigned short F2Dot14;
51
typedef signed long long LONGDATETIME;
52
typedef unsigned long TAG;
53
typedef unsigned short GlyphID;
57
int init(const char *fileNameA, unsigned int faceIndexA);
58
UGooString *getFontName();
61
unsigned long getGID(unsigned long unicode, int wmode, int whole);
62
void output(P2POutputStream *str);
63
int getLength() { return outputLength; }
65
unsigned long getGID(unsigned long unicode, int wmode);
67
unsigned long mapToVertGID(unsigned long code, unsigned long orgGID);
68
int setupGSUB(const char *tagName);
71
/* table directory entry */
72
class TableDirectory {
79
void output(PDFFTrueTypeFont *font, P2POutputStream *str);
80
unsigned long calcCheckSum();
92
UFWORD advanceWidthMax;
93
FWORD minLeftSideBearing;
94
FWORD minRightSideBearing;
99
SHORT metricDataFormat;
100
USHORT numberOfHMetrics;
101
int read(PDFFTrueTypeFont *font);
102
void output(PDFFTrueTypeFont *font, P2POutputStream *str);
103
unsigned long checkSum();
106
/* long format metrics */
107
class LongHorMetric {
111
void output(PDFFTrueTypeFont *font, P2POutputStream *str) {
112
font->write(str,advanceWidth);
113
font->write(str,lsb);
115
unsigned long checkSum() {
116
return (advanceWidth << 16)+(lsb & 0xffff);
127
USHORT maxCompositePoints;
128
USHORT maxCompositeContours;
130
USHORT maxTwilightPoints;
132
USHORT maxFunctionDefs;
133
USHORT maxInstructionDefs;
134
USHORT maxStackElements;
135
USHORT maxSizeOfInstructions;
136
USHORT maxComponentElements;
137
USHORT maxComponentDepth;
138
int read(PDFFTrueTypeFont *font);
139
void output(PDFFTrueTypeFont *font, P2POutputStream *str);
140
unsigned long checkSum();
148
ULONG checkSumAdjustment;
152
LONGDATETIME created;
153
LONGDATETIME modified;
159
USHORT lowestRecPPEM;
160
SHORT fontDirectionHint;
161
SHORT indexToLocFormat;
162
SHORT glyphDataFormat;
163
int read(PDFFTrueTypeFont *font);
164
void output(PDFFTrueTypeFont *font, P2POutputStream *str);
165
unsigned long checkSum();
169
void freeNewTables();
170
void outputWholeFile(P2POutputStream *str);
171
void setupNewTableDirectory();
172
/* allocate new loca, gfyf, hmtx table */
173
void allocNewLGHTable();
174
/* reallocate new loca and hmtx table */
175
void reallocNewLHTable();
176
/* create loca, gfyf, hmtx table */
177
void createLGHTable();
178
void outputOrgData(P2POutputStream *str,
179
unsigned long offset, unsigned long len);
180
void outputOffsetTable(P2POutputStream *str);
181
void outputTableDirectory(P2POutputStream *str);
182
void outputHead(P2POutputStream *str) {
183
head.output(this,str);
185
void outputHhea(P2POutputStream *str) {
186
hhea.output(this,str);
188
void outputLoca(P2POutputStream *str);
189
void outputMaxp(P2POutputStream *str) {
190
maxp.output(this,str);
192
void outputCvt(P2POutputStream *str);
193
void outputPrep(P2POutputStream *str);
194
void outputGlyf(P2POutputStream *str);
195
void outputHmtx(P2POutputStream *str);
196
void outputFpgm(P2POutputStream *str);
197
unsigned long locaCheckSum();
198
unsigned long glyfCheckSum();
199
unsigned long hmtxCheckSum();
200
unsigned long offsetTableCheckSum();
201
unsigned long tableDirectoryCheckSum();
202
unsigned long allCheckSum();
203
unsigned long mapGID(unsigned long orgGID);
204
template<class T> void write(P2POutputStream *str, T v) {
206
str->write(&v,sizeof(T));
210
unsigned long *GIDMap;
211
unsigned long GIDMapSize;
212
unsigned long maxGID;
214
unsigned long glyfSize;
215
unsigned long cGlyfIndex;
217
unsigned long hmtxSize;
219
unsigned long locaSize;
220
unsigned long locaEntrySize;
223
/* table directory */
224
TableDirectory *tDir;
225
TableDirectory *orgTDir;
232
virtual unsigned long getGID(PDFFTrueTypeFont *font,
233
unsigned long unicode) = 0;
234
virtual void read(PDFFTrueTypeFont *font) = 0;
239
class CmapFormat4 : public Cmap {
242
endCode = startCode = idRangeOffset = 0;
246
virtual ~CmapFormat4() {
247
if (endCode != 0) delete[] endCode;
248
if (startCode != 0) delete[] startCode;
249
if (idDelta != 0) delete[] idDelta;
250
if (idRangeOffset != 0) delete[] idRangeOffset;
252
virtual unsigned long getGID(PDFFTrueTypeFont *font,
253
unsigned long unicode);
254
virtual void read(PDFFTrueTypeFont *font);
261
USHORT entrySelector;
266
ULONG idRangeOffsetTop;
267
USHORT *idRangeOffset;
272
class CmapFormat12 : public Cmap {
275
startCharCode = endCharCode = startGlyphID = 0;
278
virtual ~CmapFormat12() {
279
if (startCharCode != 0) delete[] startCharCode;
280
if (endCharCode != 0) delete[] endCharCode;
281
if (startGlyphID != 0) delete[] startGlyphID;
283
virtual unsigned long getGID(PDFFTrueTypeFont *font,
284
unsigned long unicode);
285
virtual void read(PDFFTrueTypeFont *font);
290
ULONG *startCharCode;
296
int setOffsetTable(unsigned int faceIndex);
297
unsigned long doMapToVertGID(unsigned long orgGID);
298
int getTableDirEntry(TAG tableTag, TableDirectory *ent);
299
int getTable(TAG tableTag, ULONG *tableOffset = 0, ULONG *tableLength = 0);
300
void setOffset(unsigned long offsetA) { offset = offsetA; }
301
unsigned long getOffset() { return offset; }
302
void advance(unsigned long adv) { offset += adv; }
303
void read(void *p, unsigned long len) {
304
unsigned char *d = static_cast<unsigned char *>(p);
305
unsigned char *s = top+offset;
308
if (offset+len > fileLength) {
309
e = top+fileLength-1;
311
e = top+offset+len-1;
313
#if __BYTE_ORDER == __LITTLE_ENDIAN
317
#else /* BIG ENDIAN */
324
void read(unsigned long offsetA, void *p, unsigned long len) {
328
template<class T> void read(unsigned long offsetA, T *p) {
329
read(offsetA, p, sizeof(T));
331
template<class T> void read(T *p) {
334
template<class T> void skip() { advance(sizeof(T)); }
336
/* convert to Big Endian */
337
template<class T> static T toBE(T v) {
338
#if __BYTE_ORDER == __LITTLE_ENDIAN
342
for (i = 0;i < sizeof(T);i++) {
348
#else /* BIG ENDIAN */
352
unsigned long scanLookupList(USHORT listIndex, unsigned long orgGID);
353
unsigned long scanLookupSubTable(ULONG subTable, unsigned long orgGID);
354
int checkGIDInCoverage(ULONG coverage, unsigned long orgGID);
355
unsigned long charToTag(const char *tagName);
361
unsigned long offset;
362
unsigned long fileLength;
366
UGooString *fontName;