beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / GfxFont.h
blob85bdea137855b511c8aa52ae9d5cdf881b98a456
1 //========================================================================
2 //
3 // GfxFont.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 //========================================================================
11 // Modified under the Poppler project - http://poppler.freedesktop.org
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
16 // Copyright (C) 2005, 2008, 2015 Albert Astals Cid <aacid@kde.org>
17 // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
18 // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19 // Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
20 // Copyright (C) 2007 Jeff Muizelaar <jeff@infidigm.net>
21 // Copyright (C) 2007 Koji Otani <sho@bbr.jp>
22 // Copyright (C) 2011 Axel Strübing <axel.struebing@freenet.de>
23 // Copyright (C) 2011, 2012, 2014 Adrian Johnson <ajohnson@redneon.com>
24 // Copyright (C) 2015 Jason Crain <jason@aquaticape.us>
25 // Copyright (C) 2015 Thomas Freitag <Thomas.Freitag@alfa.de>
27 // To see a description of the changes please see the Changelog file that
28 // came with your tarball or type make ChangeLog if you are building from git
30 //========================================================================
32 #ifndef GFXFONT_H
33 #define GFXFONT_H
35 #ifdef USE_GCC_PRAGMAS
36 #pragma interface
37 #endif
39 #include "goo/gtypes.h"
40 #include "goo/GooString.h"
41 #include "Object.h"
42 #include "CharTypes.h"
44 class Dict;
45 class CMap;
46 class CharCodeToUnicode;
47 class FoFiTrueType;
48 class PSOutputDev;
49 struct GfxFontCIDWidths;
50 struct Base14FontMapEntry;
52 //------------------------------------------------------------------------
53 // GfxFontType
54 //------------------------------------------------------------------------
56 enum GfxFontType {
57 //----- Gfx8BitFont
58 fontUnknownType,
59 fontType1,
60 fontType1C,
61 fontType1COT,
62 fontType3,
63 fontTrueType,
64 fontTrueTypeOT,
65 //----- GfxCIDFont
66 fontCIDType0,
67 fontCIDType0C,
68 fontCIDType0COT,
69 fontCIDType2,
70 fontCIDType2OT
73 //------------------------------------------------------------------------
74 // GfxFontCIDWidths
75 //------------------------------------------------------------------------
77 struct GfxFontCIDWidthExcep {
78 CID first; // this record applies to
79 CID last; // CIDs <first>..<last>
80 double width; // char width
83 struct GfxFontCIDWidthExcepV {
84 CID first; // this record applies to
85 CID last; // CIDs <first>..<last>
86 double height; // char height
87 double vx, vy; // origin position
90 struct GfxFontCIDWidths {
91 double defWidth; // default char width
92 double defHeight; // default char height
93 double defVY; // default origin position
94 GfxFontCIDWidthExcep *exceps; // exceptions
95 int nExceps; // number of valid entries in exceps
96 GfxFontCIDWidthExcepV * // exceptions for vertical font
97 excepsV;
98 int nExcepsV; // number of valid entries in excepsV
101 //------------------------------------------------------------------------
102 // GfxFontLoc
103 //------------------------------------------------------------------------
105 enum GfxFontLocType {
106 gfxFontLocEmbedded, // font embedded in PDF file
107 gfxFontLocExternal, // external font file
108 gfxFontLocResident // font resident in PS printer
111 class GfxFontLoc {
112 public:
114 GfxFontLoc();
115 ~GfxFontLoc();
117 GfxFontLocType locType;
118 GfxFontType fontType;
119 Ref embFontID; // embedded stream obj ID
120 // (if locType == gfxFontLocEmbedded)
121 GooString *path; // font file path
122 // (if locType == gfxFontLocExternal)
123 // PS font name
124 // (if locType == gfxFontLocResident)
125 int fontNum; // for TrueType collections
126 // (if locType == gfxFontLocExternal)
127 GooString *encoding; // PS font encoding, only for 16-bit fonts
128 // (if locType == gfxFontLocResident)
129 int wMode; // writing mode, only for 16-bit fonts
130 // (if locType == gfxFontLocResident)
131 int substIdx; // substitute font index
132 // (if locType == gfxFontLocExternal,
133 // and a Base-14 substitution was made)
136 //------------------------------------------------------------------------
137 // GfxFont
138 //------------------------------------------------------------------------
140 #define fontFixedWidth (1 << 0)
141 #define fontSerif (1 << 1)
142 #define fontSymbolic (1 << 2)
143 #define fontItalic (1 << 6)
144 #define fontBold (1 << 18)
146 class GfxFont {
147 public:
149 enum Stretch {
150 StretchNotDefined,
151 UltraCondensed,
152 ExtraCondensed,
153 Condensed,
154 SemiCondensed,
155 Normal,
156 SemiExpanded,
157 Expanded,
158 ExtraExpanded,
159 UltraExpanded };
161 enum Weight {
162 WeightNotDefined,
163 W100,
164 W200,
165 W300,
166 W400, // Normal
167 W500,
168 W600,
169 W700, // Bold
170 W800,
171 W900 };
173 // Build a GfxFont object.
174 static GfxFont *makeFont(XRef *xref, const char *tagA, Ref idA, Dict *fontDict);
176 GfxFont(const char *tagA, Ref idA, GooString *nameA,
177 GfxFontType typeA, Ref embFontIDA);
179 GBool isOk() { return ok; }
181 void incRefCnt();
182 void decRefCnt();
184 // Get font tag.
185 GooString *getTag() { return tag; }
187 // Get font dictionary ID.
188 Ref *getID() { return &id; }
190 // Does this font match the tag?
191 GBool matches(char *tagA) { return !tag->cmp(tagA); }
193 // Get font family name.
194 GooString *getFamily() { return family; }
196 // Get font stretch.
197 Stretch getStretch() { return stretch; }
199 // Get font weight.
200 Weight getWeight() { return weight; }
202 // Get the original font name (ignornig any munging that might have
203 // been done to map to a canonical Base-14 font name).
204 GooString *getName() { return name; }
206 // Get font type.
207 GfxFontType getType() { return type; }
208 virtual GBool isCIDFont() { return gFalse; }
210 // Get embedded font ID, i.e., a ref for the font file stream.
211 // Returns false if there is no embedded font.
212 GBool getEmbeddedFontID(Ref *embID)
213 { *embID = embFontID; return embFontID.num >= 0; }
215 // Invalidate an embedded font
216 // Returns false if there is no embedded font.
217 GBool invalidateEmbeddedFont() {
218 if (embFontID.num >= 0) {
219 embFontID.num = -1;
220 return gTrue;
222 return gFalse;
225 // Get the PostScript font name for the embedded font. Returns
226 // NULL if there is no embedded font.
227 GooString *getEmbeddedFontName() { return embFontName; }
229 // Get font descriptor flags.
230 int getFlags() { return flags; }
231 GBool isFixedWidth() { return flags & fontFixedWidth; }
232 GBool isSerif() { return flags & fontSerif; }
233 GBool isSymbolic() { return flags & fontSymbolic; }
234 GBool isItalic() { return flags & fontItalic; }
235 GBool isBold() { return flags & fontBold; }
237 // Return the Unicode map.
238 virtual CharCodeToUnicode *getToUnicode() = 0;
240 // Return the font matrix.
241 double *getFontMatrix() { return fontMat; }
243 // Return the font bounding box.
244 double *getFontBBox() { return fontBBox; }
246 // Return the ascent and descent values.
247 double getAscent() { return ascent; }
248 double getDescent() { return descent; }
250 // Return the writing mode (0=horizontal, 1=vertical).
251 virtual int getWMode() { return 0; }
253 // Locate the font file for this font. If <ps> is not null, includes PS
254 // printer-resident fonts. Returns NULL on failure.
255 GfxFontLoc *locateFont(XRef *xref, PSOutputDev *ps);
257 // Locate a Base-14 font file for a specified font name.
258 static GfxFontLoc *locateBase14Font(GooString *base14Name);
260 // Read an external or embedded font file into a buffer.
261 char *readEmbFontFile(XRef *xref, int *len);
263 // Get the next char from a string <s> of <len> bytes, returning the
264 // char <code>, its Unicode mapping <u>, its displacement vector
265 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize>
266 // is the number of entries available in <u>, and <uLen> is set to
267 // the number actually used. Returns the number of bytes used by
268 // the char code.
269 virtual int getNextChar(char *s, int len, CharCode *code,
270 Unicode **u, int *uLen,
271 double *dx, double *dy, double *ox, double *oy) = 0;
273 // Does this font have a toUnicode map?
274 GBool hasToUnicodeCMap() { return hasToUnicode; }
276 // Return the name of the encoding
277 GooString *getEncodingName() { return encodingName; }
279 // Return AGLFN names of ligatures in the Standard and Expert encodings
280 // for use with fonts that are not compatible with the Standard 14 fonts.
281 // http://sourceforge.net/adobe/aglfn/wiki/AGL%20Specification/
282 static const char *getAlternateName(const char *name);
284 protected:
286 virtual ~GfxFont();
288 static GfxFontType getFontType(XRef *xref, Dict *fontDict, Ref *embID);
289 void readFontDescriptor(XRef *xref, Dict *fontDict);
290 CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
291 CharCodeToUnicode *ctu);
292 static GfxFontLoc *getExternalFont(GooString *path, GBool cid);
294 GooString *tag; // PDF font tag
295 Ref id; // reference (used as unique ID)
296 GooString *name; // font name
297 GooString *family; // font family
298 Stretch stretch; // font stretch
299 Weight weight; // font weight
300 GfxFontType type; // type of font
301 int flags; // font descriptor flags
302 GooString *embFontName; // name of embedded font
303 Ref embFontID; // ref to embedded font file stream
304 double fontMat[6]; // font matrix (Type 3 only)
305 double fontBBox[4]; // font bounding box (Type 3 only)
306 double missingWidth; // "default" width
307 double ascent; // max height above baseline
308 double descent; // max depth below baseline
309 int refCnt;
310 GBool ok;
311 GBool hasToUnicode;
312 GooString *encodingName;
315 //------------------------------------------------------------------------
316 // Gfx8BitFont
317 //------------------------------------------------------------------------
319 class Gfx8BitFont: public GfxFont {
320 public:
322 Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA,
323 GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
325 virtual int getNextChar(char *s, int len, CharCode *code,
326 Unicode **u, int *uLen,
327 double *dx, double *dy, double *ox, double *oy);
329 // Return the encoding.
330 char **getEncoding() { return enc; }
332 // Return the Unicode map.
333 CharCodeToUnicode *getToUnicode();
335 // Return the character name associated with <code>.
336 char *getCharName(int code) { return enc[code]; }
338 // Returns true if the PDF font specified an encoding.
339 GBool getHasEncoding() { return hasEncoding; }
341 // Returns true if the PDF font specified MacRomanEncoding.
342 GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
344 // Get width of a character.
345 double getWidth(Guchar c) { return widths[c]; }
347 // Return a char code-to-GID mapping for the provided font file.
348 // (This is only useful for TrueType fonts.)
349 int *getCodeToGIDMap(FoFiTrueType *ff);
351 // Return the Type 3 CharProc dictionary, or NULL if none.
352 Dict *getCharProcs();
354 // Return the Type 3 CharProc for the character associated with <code>.
355 Object *getCharProc(int code, Object *proc);
357 // Return the Type 3 Resources dictionary, or NULL if none.
358 Dict *getResources();
360 private:
361 virtual ~Gfx8BitFont();
363 const Base14FontMapEntry *base14; // for Base-14 fonts only; NULL otherwise
364 char *enc[256]; // char code --> char name
365 char encFree[256]; // boolean for each char name: if set,
366 // the string is malloc'ed
367 CharCodeToUnicode *ctu; // char code --> Unicode
368 GBool hasEncoding;
369 GBool usesMacRomanEnc;
370 double widths[256]; // character widths
371 Object charProcs; // Type 3 CharProcs dictionary
372 Object resources; // Type 3 Resources dictionary
374 friend class GfxFont;
377 //------------------------------------------------------------------------
378 // GfxCIDFont
379 //------------------------------------------------------------------------
381 class GfxCIDFont: public GfxFont {
382 public:
384 GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA,
385 GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
387 virtual GBool isCIDFont() { return gTrue; }
389 virtual int getNextChar(char *s, int len, CharCode *code,
390 Unicode **u, int *uLen,
391 double *dx, double *dy, double *ox, double *oy);
393 // Return the writing mode (0=horizontal, 1=vertical).
394 virtual int getWMode();
396 // Return the Unicode map.
397 CharCodeToUnicode *getToUnicode();
399 // Get the collection name (<registry>-<ordering>).
400 GooString *getCollection();
402 // Return the CID-to-GID mapping table. These should only be called
403 // if type is fontCIDType2.
404 int *getCIDToGID() { return cidToGID; }
405 int getCIDToGIDLen() { return cidToGIDLen; }
407 int *getCodeToGIDMap(FoFiTrueType *ff, int *length);
409 double getWidth(char* s, int len);
411 private:
412 virtual ~GfxCIDFont();
414 int mapCodeToGID(FoFiTrueType *ff, int cmapi,
415 Unicode unicode, GBool wmode);
416 double getWidth(CID cid); // Get width of a character.
418 GooString *collection; // collection name
419 CMap *cMap; // char code --> CID
420 CharCodeToUnicode *ctu; // CID --> Unicode
421 GBool ctuUsesCharCode; // true: ctu maps char code to Unicode;
422 // false: ctu maps CID to Unicode
423 GfxFontCIDWidths widths; // character widths
424 int *cidToGID; // CID --> GID mapping (for embedded
425 // TrueType fonts)
426 int cidToGIDLen;
429 //------------------------------------------------------------------------
430 // GfxFontDict
431 //------------------------------------------------------------------------
433 class GfxFontDict {
434 public:
436 // Build the font dictionary, given the PDF font dictionary.
437 GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
439 // Destructor.
440 ~GfxFontDict();
442 // Get the specified font.
443 GfxFont *lookup(char *tag);
445 // Iterative access.
446 int getNumFonts() { return numFonts; }
447 GfxFont *getFont(int i) { return fonts[i]; }
449 private:
451 GfxFont **fonts; // list of fonts
452 int numFonts; // number of fonts
455 #endif