~ubuntu-branches/debian/experimental/cups-filters/experimental

« back to all changes in this revision

Viewing changes to pdftopdf/PDFFTrueTypeFont.h

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-07-22 18:57:32 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20120722185732-26kkte5p1lth3rt5
Tags: 1.0.20-0bzr1
* New upstream release
   - pdftops: Added another workaround for Kyocera printers: Some
     models get very slow on images which request interpolation,
     so now we remove the image interpolation requests by additional
     PostScript code only inserted for Kyocera printers (LP: #1026974).
   - Made the Poppler-based filters pdftopdf and pdftoopvp build with
     both Poppler 0.18.x and 0.20.x (Upstream bug #1055).
   - Fixes according to Coverity scan results (Upstream bug #1054).
   - Switched build system to autotools. This especially fixes several
     build problems in Gentoo. Also build-tested with CUPS 1.6.0b1.
   - Fixes for compatibility with clang/gcc-4.7.
   - textonly: Filter did not work as a pipe with copies=1 (Upstream bug
     #1032).
   - texttopdf: Avoid trimming the results of FcFontSort(), as this may
     miss some reasonable candidates under certain circumstances. BTW,
     fix passing a non-pointer as a pointer to "result" (Closes: #670055).
   - Corrected documentation. The option for the maximum image rendering
     resolution in pdftops is "pdftops-max-image-resolution", not
     "pdftops-max-image-resolution-default".
* debian/patches/fcfontsort-no-trim.patch: Removed, fixed upstream.
* debian/rules: Updated options for ./configure and make for the new autotools
  build system.
* debian/watch: Switched to bz2 upstream packages.
* debian/rules, debian/copyright, debian/cups-filters.docs: Updated for
  renamed documentation files.
* debian/control, debian/libfontembed1.install,
  debian/libfontembed-dev.install: Added new binary packages for libfontembed.
* debian/copyright: Updated for recent file additions, and rearrangement of
  directories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2006-2007, BBR Inc.  All rights reserved.
4
 
 
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:
12
 
 
13
 
The above copyright notice and this permission notice shall be included
14
 
in all copies or substantial portions of the Software.
15
 
 
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.
23
 
 
24
 
*/
25
 
/*
26
 
 PDFFTrueTypeFont.h
27
 
 TrueType Font manager
28
 
*/
29
 
#ifndef _PDFFTRUTYPEFONT_H_
30
 
#define _PDFFTRUTYPEFONT_H_
31
 
 
32
 
#include <endian.h>
33
 
#include "UGooString.h"
34
 
#ifdef PDFTOPDF
35
 
#include "P2POutputStream.h"
36
 
#endif
37
 
 
38
 
class PDFFTrueTypeFont {
39
 
public:
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;
54
 
 
55
 
  PDFFTrueTypeFont();
56
 
  ~PDFFTrueTypeFont();
57
 
  int init(const char *fileNameA, unsigned int faceIndexA);
58
 
  UGooString *getFontName();
59
 
  int setupCmap();
60
 
#ifdef PDFTOPDF
61
 
  unsigned long getGID(unsigned long unicode, int wmode, int whole);
62
 
  void output(P2POutputStream *str);
63
 
  int getLength() { return outputLength; }
64
 
#else
65
 
  unsigned long getGID(unsigned long unicode, int wmode);
66
 
#endif
67
 
  unsigned long mapToVertGID(unsigned long code, unsigned long orgGID);
68
 
  int setupGSUB(const char *tagName);
69
 
private:
70
 
 
71
 
  /* table directory entry */
72
 
  class TableDirectory {
73
 
  public:
74
 
    ULONG tag;
75
 
    ULONG checkSum;
76
 
    ULONG offset;
77
 
    ULONG length;
78
 
#ifdef PDFTOPDF
79
 
    void output(PDFFTrueTypeFont *font, P2POutputStream *str);
80
 
    unsigned long calcCheckSum();
81
 
#endif
82
 
  };
83
 
 
84
 
#ifdef PDFTOPDF
85
 
  /* hhea table */
86
 
  class Hhea {
87
 
  public:
88
 
    Fixed version;
89
 
    FWORD Ascender;
90
 
    FWORD Descender;
91
 
    FWORD LineGap;
92
 
    UFWORD advanceWidthMax;
93
 
    FWORD minLeftSideBearing;
94
 
    FWORD minRightSideBearing;
95
 
    FWORD xMacExtent;
96
 
    SHORT caretSlopeRise;
97
 
    SHORT caretSlopeRun;
98
 
    SHORT caretOffset;
99
 
    SHORT metricDataFormat;
100
 
    USHORT numberOfHMetrics;
101
 
    int read(PDFFTrueTypeFont *font);
102
 
    void output(PDFFTrueTypeFont *font, P2POutputStream *str);
103
 
    unsigned long checkSum();
104
 
  };
105
 
 
106
 
  /* long format metrics */
107
 
  class LongHorMetric {
108
 
  public:
109
 
    USHORT advanceWidth;
110
 
    SHORT lsb;
111
 
    void output(PDFFTrueTypeFont *font, P2POutputStream *str) {
112
 
      font->write(str,advanceWidth);
113
 
      font->write(str,lsb);
114
 
    }
115
 
    unsigned long checkSum() {
116
 
      return (advanceWidth << 16)+(lsb & 0xffff);
117
 
    }
118
 
  };
119
 
 
120
 
  /* maxp table */
121
 
  class Maxp {
122
 
  public:
123
 
    Fixed version;
124
 
    USHORT numGlyphs;
125
 
    USHORT maxPoints;
126
 
    USHORT maxContours;
127
 
    USHORT maxCompositePoints;
128
 
    USHORT maxCompositeContours;
129
 
    USHORT maxZones;
130
 
    USHORT maxTwilightPoints;
131
 
    USHORT maxStorage;
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();
141
 
  };
142
 
 
143
 
  /* head table */
144
 
  class Head {
145
 
  public:
146
 
    Fixed version;
147
 
    Fixed fontRevision;
148
 
    ULONG checkSumAdjustment;
149
 
    ULONG magicNumber;
150
 
    USHORT flags;
151
 
    USHORT unitsPerEm;
152
 
    LONGDATETIME created;
153
 
    LONGDATETIME modified;
154
 
    SHORT xMin;
155
 
    SHORT yMin;
156
 
    SHORT xMax;
157
 
    SHORT yMax;
158
 
    USHORT macStyle;
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();
166
 
  };
167
 
 
168
 
  int readOrgTables();
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);
184
 
  }
185
 
  void outputHhea(P2POutputStream *str) {
186
 
    hhea.output(this,str);
187
 
  }
188
 
  void outputLoca(P2POutputStream *str);
189
 
  void outputMaxp(P2POutputStream *str) {
190
 
    maxp.output(this,str);
191
 
  }
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) {
205
 
    v = toBE(v);
206
 
    str->write(&v,sizeof(T));
207
 
  }
208
 
 
209
 
  int outputLength;
210
 
  unsigned long *GIDMap;
211
 
  unsigned long GIDMapSize;
212
 
  unsigned long maxGID;
213
 
  unsigned char *glyf;
214
 
  unsigned long glyfSize;
215
 
  unsigned long cGlyfIndex;
216
 
  LongHorMetric *hmtx;
217
 
  unsigned long hmtxSize;
218
 
  ULONG *loca;
219
 
  unsigned long locaSize;
220
 
  unsigned long locaEntrySize;
221
 
  Hhea hhea;
222
 
  Maxp maxp;
223
 
  /* table directory */
224
 
  TableDirectory *tDir;
225
 
  TableDirectory *orgTDir;
226
 
  Head head;
227
 
#endif
228
 
 
229
 
  /* cmap */
230
 
  class Cmap {
231
 
  public:
232
 
    virtual unsigned long getGID(PDFFTrueTypeFont *font,
233
 
      unsigned long unicode) = 0;
234
 
    virtual void read(PDFFTrueTypeFont *font) = 0;
235
 
    virtual ~Cmap() {};
236
 
  };
237
 
 
238
 
  /* cmap format 4 */
239
 
  class CmapFormat4 : public Cmap {
240
 
  public:
241
 
    CmapFormat4() {
242
 
      endCode = startCode = idRangeOffset = 0;
243
 
      idDelta = 0;
244
 
    }
245
 
 
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;
251
 
    }
252
 
    virtual unsigned long getGID(PDFFTrueTypeFont *font,
253
 
      unsigned long unicode);
254
 
    virtual void read(PDFFTrueTypeFont *font);
255
 
  private:
256
 
    USHORT length;
257
 
    USHORT language;
258
 
    USHORT segCountX2;
259
 
    USHORT segCount;
260
 
    USHORT searchRange;
261
 
    USHORT entrySelector;
262
 
    USHORT rangeShift;
263
 
    USHORT *endCode;
264
 
    USHORT *startCode;
265
 
    SHORT *idDelta;
266
 
    ULONG idRangeOffsetTop;
267
 
    USHORT *idRangeOffset;
268
 
 
269
 
  };
270
 
 
271
 
  /* cmap format 12 */
272
 
  class CmapFormat12 : public Cmap {
273
 
  public:
274
 
    CmapFormat12() {
275
 
      startCharCode = endCharCode = startGlyphID = 0;
276
 
    }
277
 
 
278
 
    virtual ~CmapFormat12() {
279
 
      if (startCharCode != 0) delete[] startCharCode;
280
 
      if (endCharCode != 0) delete[] endCharCode;
281
 
      if (startGlyphID != 0) delete[] startGlyphID;
282
 
    }
283
 
    virtual unsigned long getGID(PDFFTrueTypeFont *font,
284
 
      unsigned long unicode);
285
 
    virtual void read(PDFFTrueTypeFont *font);
286
 
  private:
287
 
    ULONG length;
288
 
    ULONG language;
289
 
    ULONG nGroups;
290
 
    ULONG *startCharCode;
291
 
    ULONG *endCharCode;
292
 
    ULONG *startGlyphID;
293
 
  };
294
 
 
295
 
 
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;
306
 
    unsigned char *e;
307
 
 
308
 
    if (offset+len > fileLength) {
309
 
      e = top+fileLength-1; 
310
 
    } else {
311
 
      e = top+offset+len-1;
312
 
    }
313
 
#if __BYTE_ORDER == __LITTLE_ENDIAN
314
 
    while (s <= e) {
315
 
      *d++ = *e--;
316
 
    }
317
 
#else /* BIG ENDIAN */
318
 
    while (s <= e) {
319
 
      *d++ = *s++;
320
 
    }
321
 
#endif
322
 
    advance(len);
323
 
  }
324
 
  void read(unsigned long offsetA, void *p, unsigned long len) {
325
 
    setOffset(offsetA);
326
 
    read(p,len);
327
 
  }
328
 
  template<class T> void read(unsigned long offsetA, T *p) {
329
 
    read(offsetA, p, sizeof(T));
330
 
  }
331
 
  template<class T> void read(T *p) {
332
 
    read(p, sizeof(T));
333
 
  }
334
 
  template<class T> void skip() { advance(sizeof(T)); }
335
 
 
336
 
  /* convert to Big Endian */
337
 
  template<class T> static T toBE(T v) {
338
 
#if __BYTE_ORDER == __LITTLE_ENDIAN
339
 
    T r = 0;
340
 
    unsigned int i;
341
 
 
342
 
    for (i = 0;i < sizeof(T);i++) {
343
 
      r <<= 8;
344
 
      r |= (v & 0xff);
345
 
      v >>= 8;
346
 
    }
347
 
    return r;
348
 
#else /* BIG ENDIAN */
349
 
    return v;
350
 
#endif
351
 
  }
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);
356
 
 
357
 
  Cmap *cmap;
358
 
  char *fileName;
359
 
  int fontFileFD;
360
 
  unsigned char *top;
361
 
  unsigned long offset;
362
 
  unsigned long fileLength;
363
 
  ULONG offsetTable;
364
 
  ULONG featureTable;
365
 
  ULONG lookupList;
366
 
  UGooString *fontName;
367
 
  USHORT numTables;
368
 
  ULONG scriptTag;
369
 
};
370
 
 
371
 
#endif