1 //========================================================================
2 //
3 // GfxFont.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
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>
26 //
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
29 //
30 //========================================================================
31 
32 #ifndef GFXFONT_H
33 #define GFXFONT_H
34 
35 #ifdef USE_GCC_PRAGMAS
36 #pragma interface
37 #endif
38 
39 #include "goo/gtypes.h"
40 #include "goo/GooString.h"
41 #include "Object.h"
42 #include "CharTypes.h"
43 
44 class Dict;
45 class CMap;
46 class CharCodeToUnicode;
47 class FoFiTrueType;
48 class PSOutputDev;
49 struct GfxFontCIDWidths;
50 struct Base14FontMapEntry;
51 
52 //------------------------------------------------------------------------
53 // GfxFontType
54 //------------------------------------------------------------------------
55 
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
71 };
72 
73 //------------------------------------------------------------------------
74 // GfxFontCIDWidths
75 //------------------------------------------------------------------------
76 
77 struct GfxFontCIDWidthExcep {
78   CID first;			// this record applies to
79   CID last;			//   CIDs <first>..<last>
80   double width;			// char width
81 };
82 
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
88 };
89 
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
99 };
100 
101 //------------------------------------------------------------------------
102 // GfxFontLoc
103 //------------------------------------------------------------------------
104 
105 enum GfxFontLocType {
106   gfxFontLocEmbedded,		// font embedded in PDF file
107   gfxFontLocExternal,		// external font file
108   gfxFontLocResident		// font resident in PS printer
109 };
110 
111 class GfxFontLoc {
112 public:
113 
114   GfxFontLoc();
115   ~GfxFontLoc();
116 
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)
134 };
135 
136 //------------------------------------------------------------------------
137 // GfxFont
138 //------------------------------------------------------------------------
139 
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)
145 
146 class GfxFont {
147 public:
148 
149   enum Stretch {
150 	StretchNotDefined,
151 	UltraCondensed,
152 	ExtraCondensed,
153 	Condensed,
154 	SemiCondensed,
155 	Normal,
156 	SemiExpanded,
157 	Expanded,
158 	ExtraExpanded,
159 	UltraExpanded };
160 
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 };
172 
173   // Build a GfxFont object.
174   static GfxFont *makeFont(XRef *xref, const char *tagA, Ref idA, Dict *fontDict);
175 
176   GfxFont(const char *tagA, Ref idA, GooString *nameA,
177 	  GfxFontType typeA, Ref embFontIDA);
178 
isOk()179   GBool isOk() { return ok; }
180 
181   void incRefCnt();
182   void decRefCnt();
183 
184   // Get font tag.
getTag()185   GooString *getTag() { return tag; }
186 
187   // Get font dictionary ID.
getID()188   Ref *getID() { return &id; }
189 
190   // Does this font match the tag?
matches(char * tagA)191   GBool matches(char *tagA) { return !tag->cmp(tagA); }
192 
193   // Get font family name.
getFamily()194   GooString *getFamily() { return family; }
195 
196   // Get font stretch.
getStretch()197   Stretch getStretch() { return stretch; }
198 
199   // Get font weight.
getWeight()200   Weight getWeight() { return weight; }
201 
202   // Get the original font name (ignornig any munging that might have
203   // been done to map to a canonical Base-14 font name).
getName()204   GooString *getName() { return name; }
205 
206   // Get font type.
getType()207   GfxFontType getType() { return type; }
isCIDFont()208   virtual GBool isCIDFont() { return gFalse; }
209 
210   // Get embedded font ID, i.e., a ref for the font file stream.
211   // Returns false if there is no embedded font.
getEmbeddedFontID(Ref * embID)212   GBool getEmbeddedFontID(Ref *embID)
213     { *embID = embFontID; return embFontID.num >= 0; }
214 
215   // Invalidate an embedded font
216   // Returns false if there is no embedded font.
invalidateEmbeddedFont()217   GBool invalidateEmbeddedFont() {
218     if (embFontID.num >= 0) {
219       embFontID.num = -1;
220       return gTrue;
221     }
222     return gFalse;
223   }
224 
225   // Get the PostScript font name for the embedded font.  Returns
226   // NULL if there is no embedded font.
getEmbeddedFontName()227   GooString *getEmbeddedFontName() { return embFontName; }
228 
229   // Get font descriptor flags.
getFlags()230   int getFlags() { return flags; }
isFixedWidth()231   GBool isFixedWidth() { return flags & fontFixedWidth; }
isSerif()232   GBool isSerif() { return flags & fontSerif; }
isSymbolic()233   GBool isSymbolic() { return flags & fontSymbolic; }
isItalic()234   GBool isItalic() { return flags & fontItalic; }
isBold()235   GBool isBold() { return flags & fontBold; }
236 
237   // Return the Unicode map.
238   virtual CharCodeToUnicode *getToUnicode() = 0;
239 
240   // Return the font matrix.
getFontMatrix()241   double *getFontMatrix() { return fontMat; }
242 
243   // Return the font bounding box.
getFontBBox()244   double *getFontBBox() { return fontBBox; }
245 
246   // Return the ascent and descent values.
getAscent()247   double getAscent() { return ascent; }
getDescent()248   double getDescent() { return descent; }
249 
250   // Return the writing mode (0=horizontal, 1=vertical).
getWMode()251   virtual int getWMode() { return 0; }
252 
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);
256 
257   // Locate a Base-14 font file for a specified font name.
258   static GfxFontLoc *locateBase14Font(GooString *base14Name);
259 
260   // Read an external or embedded font file into a buffer.
261   char *readEmbFontFile(XRef *xref, int *len);
262 
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;
272 
273   // Does this font have a toUnicode map?
hasToUnicodeCMap()274   GBool hasToUnicodeCMap() { return hasToUnicode; }
275 
276   // Return the name of the encoding
getEncodingName()277   GooString *getEncodingName() { return encodingName; }
278 
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);
283 
284 protected:
285 
286   virtual ~GfxFont();
287 
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);
293 
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;
313 };
314 
315 //------------------------------------------------------------------------
316 // Gfx8BitFont
317 //------------------------------------------------------------------------
318 
319 class Gfx8BitFont: public GfxFont {
320 public:
321 
322   Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA,
323 	      GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
324 
325   virtual int getNextChar(char *s, int len, CharCode *code,
326 			  Unicode **u, int *uLen,
327 			  double *dx, double *dy, double *ox, double *oy);
328 
329   // Return the encoding.
getEncoding()330   char **getEncoding() { return enc; }
331 
332   // Return the Unicode map.
333   CharCodeToUnicode *getToUnicode();
334 
335   // Return the character name associated with <code>.
getCharName(int code)336   char *getCharName(int code) { return enc[code]; }
337 
338   // Returns true if the PDF font specified an encoding.
getHasEncoding()339   GBool getHasEncoding() { return hasEncoding; }
340 
341   // Returns true if the PDF font specified MacRomanEncoding.
getUsesMacRomanEnc()342   GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
343 
344   // Get width of a character.
getWidth(Guchar c)345   double getWidth(Guchar c) { return widths[c]; }
346 
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);
350 
351   // Return the Type 3 CharProc dictionary, or NULL if none.
352   Dict *getCharProcs();
353 
354   // Return the Type 3 CharProc for the character associated with <code>.
355   Object *getCharProc(int code, Object *proc);
356 
357   // Return the Type 3 Resources dictionary, or NULL if none.
358   Dict *getResources();
359 
360 private:
361   virtual ~Gfx8BitFont();
362 
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
373 
374   friend class GfxFont;
375 };
376 
377 //------------------------------------------------------------------------
378 // GfxCIDFont
379 //------------------------------------------------------------------------
380 
381 class GfxCIDFont: public GfxFont {
382 public:
383 
384   GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA,
385 	     GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
386 
isCIDFont()387   virtual GBool isCIDFont() { return gTrue; }
388 
389   virtual int getNextChar(char *s, int len, CharCode *code,
390 			  Unicode **u, int *uLen,
391 			  double *dx, double *dy, double *ox, double *oy);
392 
393   // Return the writing mode (0=horizontal, 1=vertical).
394   virtual int getWMode();
395 
396   // Return the Unicode map.
397   CharCodeToUnicode *getToUnicode();
398 
399   // Get the collection name (<registry>-<ordering>).
400   GooString *getCollection();
401 
402   // Return the CID-to-GID mapping table.  These should only be called
403   // if type is fontCIDType2.
getCIDToGID()404   int *getCIDToGID() { return cidToGID; }
getCIDToGIDLen()405   int getCIDToGIDLen() { return cidToGIDLen; }
406 
407   int *getCodeToGIDMap(FoFiTrueType *ff, int *length);
408 
409   double getWidth(char* s, int len);
410 
411 private:
412   virtual ~GfxCIDFont();
413 
414   int mapCodeToGID(FoFiTrueType *ff, int cmapi,
415     Unicode unicode, GBool wmode);
416   double getWidth(CID cid);	// Get width of a character.
417 
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;
427 };
428 
429 //------------------------------------------------------------------------
430 // GfxFontDict
431 //------------------------------------------------------------------------
432 
433 class GfxFontDict {
434 public:
435 
436   // Build the font dictionary, given the PDF font dictionary.
437   GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
438 
439   // Destructor.
440   ~GfxFontDict();
441 
442   // Get the specified font.
443   GfxFont *lookup(char *tag);
444 
445   // Iterative access.
getNumFonts()446   int getNumFonts() { return numFonts; }
getFont(int i)447   GfxFont *getFont(int i) { return fonts[i]; }
448 
449 private:
450 
451   GfxFont **fonts;		// list of fonts
452   int numFonts;			// number of fonts
453 };
454 
455 #endif
456