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 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 //
24 // To see a description of the changes please see the Changelog file that
25 // came with your tarball or type make ChangeLog if you are building from git
26 //
27 //========================================================================
28 
29 #ifndef GFXFONT_H
30 #define GFXFONT_H
31 
32 #ifdef USE_GCC_PRAGMAS
33 #pragma interface
34 #endif
35 
36 #include "goo/gtypes.h"
37 #include "goo/GooString.h"
38 #include "Object.h"
39 #include "CharTypes.h"
40 
41 class Dict;
42 class CMap;
43 class CharCodeToUnicode;
44 class FoFiTrueType;
45 class DisplayFontParam;
46 struct GfxFontCIDWidths;
47 
48 //------------------------------------------------------------------------
49 // GfxFontType
50 //------------------------------------------------------------------------
51 
52 enum GfxFontType {
53   //----- Gfx8BitFont
54   fontUnknownType,
55   fontType1,
56   fontType1C,
57   fontType1COT,
58   fontType3,
59   fontTrueType,
60   fontTrueTypeOT,
61   //----- GfxCIDFont
62   fontCIDType0,
63   fontCIDType0C,
64   fontCIDType0COT,
65   fontCIDType2,
66   fontCIDType2OT
67 };
68 
69 //------------------------------------------------------------------------
70 // GfxFontCIDWidths
71 //------------------------------------------------------------------------
72 
73 struct GfxFontCIDWidthExcep {
74   CID first;			// this record applies to
75   CID last;			//   CIDs <first>..<last>
76   double width;			// char width
77 };
78 
79 struct GfxFontCIDWidthExcepV {
80   CID first;			// this record applies to
81   CID last;			//   CIDs <first>..<last>
82   double height;		// char height
83   double vx, vy;		// origin position
84 };
85 
86 struct GfxFontCIDWidths {
87   double defWidth;		// default char width
88   double defHeight;		// default char height
89   double defVY;			// default origin position
90   GfxFontCIDWidthExcep *exceps;	// exceptions
91   int nExceps;			// number of valid entries in exceps
92   GfxFontCIDWidthExcepV *	// exceptions for vertical font
93     excepsV;
94   int nExcepsV;			// number of valid entries in excepsV
95 };
96 
97 //------------------------------------------------------------------------
98 // GfxFont
99 //------------------------------------------------------------------------
100 
101 #define fontFixedWidth (1 << 0)
102 #define fontSerif      (1 << 1)
103 #define fontSymbolic   (1 << 2)
104 #define fontItalic     (1 << 6)
105 #define fontBold       (1 << 18)
106 
107 class GfxFont {
108 public:
109 
110   enum Stretch {
111 	StretchNotDefined,
112 	UltraCondensed,
113 	ExtraCondensed,
114 	Condensed,
115 	SemiCondensed,
116 	Normal,
117 	SemiExpanded,
118 	Expanded,
119 	ExtraExpanded,
120 	UltraExpanded };
121 
122   enum Weight {
123 	WeightNotDefined,
124 	W100,
125 	W200,
126 	W300,
127 	W400, // Normal
128 	W500,
129 	W600,
130 	W700, // Bold
131 	W800,
132 	W900 };
133 
134   // Build a GfxFont object.
135   static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
136 
137   GfxFont(char *tagA, Ref idA, GooString *nameA);
138 
isOk()139   GBool isOk() { return ok; }
140 
141   void incRefCnt();
142   void decRefCnt();
143 
144   // Get font tag.
getTag()145   GooString *getTag() { return tag; }
146 
147   // Get font dictionary ID.
getID()148   Ref *getID() { return &id; }
149 
150   // Does this font match the tag?
matches(char * tagA)151   GBool matches(char *tagA) { return !tag->cmp(tagA); }
152 
153   // Get base font name.
getName()154   GooString *getName() { return name; }
155 
156   // Get font family name.
getFamily()157   GooString *getFamily() { return family; }
158 
159   // Get font stretch.
getStretch()160   Stretch getStretch() { return stretch; }
161 
162   // Get font weight.
getWeight()163   Weight getWeight() { return weight; }
164 
165   // Get the original font name (ignornig any munging that might have
166   // been done to map to a canonical Base-14 font name).
getOrigName()167   GooString *getOrigName() { return origName; }
168 
169   // Get font type.
getType()170   GfxFontType getType() { return type; }
isCIDFont()171   virtual GBool isCIDFont() { return gFalse; }
172 
173   // Get embedded font ID, i.e., a ref for the font file stream.
174   // Returns false if there is no embedded font.
getEmbeddedFontID(Ref * embID)175   GBool getEmbeddedFontID(Ref *embID)
176     { *embID = embFontID; return embFontID.num >= 0; }
177 
178   // Get the PostScript font name for the embedded font.  Returns
179   // NULL if there is no embedded font.
getEmbeddedFontName()180   GooString *getEmbeddedFontName() { return embFontName; }
181 
182   // Get the name of the external font file.  Returns NULL if there
183   // is no external font file.
getExtFontFile()184   GooString *getExtFontFile() { return extFontFile; }
185 
186   // Get font descriptor flags.
getFlags()187   int getFlags() { return flags; }
isFixedWidth()188   GBool isFixedWidth() { return flags & fontFixedWidth; }
isSerif()189   GBool isSerif() { return flags & fontSerif; }
isSymbolic()190   GBool isSymbolic() { return flags & fontSymbolic; }
isItalic()191   GBool isItalic() { return flags & fontItalic; }
isBold()192   GBool isBold() { return flags & fontBold; }
193 
194   // Return the Unicode map.
195   virtual CharCodeToUnicode *getToUnicode() = 0;
196 
197   // Return the font matrix.
getFontMatrix()198   double *getFontMatrix() { return fontMat; }
199 
200   // Return the font bounding box.
getFontBBox()201   double *getFontBBox() { return fontBBox; }
202 
203   // Return the ascent and descent values.
getAscent()204   double getAscent() { return ascent; }
getDescent()205   double getDescent() { return descent; }
206 
207   // Return the writing mode (0=horizontal, 1=vertical).
getWMode()208   virtual int getWMode() { return 0; }
209 
210   // Read an external or embedded font file into a buffer.
211   char *readExtFontFile(int *len);
212   char *readEmbFontFile(XRef *xref, int *len);
213 
214   // Get the next char from a string <s> of <len> bytes, returning the
215   // char <code>, its Unicode mapping <u>, its displacement vector
216   // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>).  <uSize>
217   // is the number of entries available in <u>, and <uLen> is set to
218   // the number actually used.  Returns the number of bytes used by
219   // the char code.
220   virtual int getNextChar(char *s, int len, CharCode *code,
221 			  Unicode **u, int *uLen,
222 			  double *dx, double *dy, double *ox, double *oy) = 0;
223 
224   /* XXX: dfp shouldn't be public, however the font finding code is currently in
225    * GlobalParams. Instead it should be inside the GfxFont class. However,
226    * getDisplayFont currently uses FCcfg so moving it is not as simple. */
227   /* XXX: related to this is the fact that all of the extFontFile stuff is dead */
228   DisplayFontParam *dfp;
229 protected:
230 
231   virtual ~GfxFont();
232 
233   void readFontDescriptor(XRef *xref, Dict *fontDict);
234   CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
235 				       CharCodeToUnicode *ctu);
236   void findExtFontFile();
237 
238   GooString *tag;			// PDF font tag
239   Ref id;			// reference (used as unique ID)
240   GooString *name;		// font name
241   GooString *family;		// font family
242   Stretch stretch;			// font stretch
243   Weight weight;			// font weight
244   GooString *origName;		// original font name
245   GfxFontType type;		// type of font
246   int flags;			// font descriptor flags
247   GooString *embFontName;		// name of embedded font
248   Ref embFontID;		// ref to embedded font file stream
249   GooString *extFontFile;		// external font file name
250   double fontMat[6];		// font matrix (Type 3 only)
251   double fontBBox[4];		// font bounding box (Type 3 only)
252   double missingWidth;		// "default" width
253   double ascent;		// max height above baseline
254   double descent;		// max depth below baseline
255   int refCnt;
256   GBool ok;
257   GBool hasToUnicode;
258 };
259 
260 //------------------------------------------------------------------------
261 // Gfx8BitFont
262 //------------------------------------------------------------------------
263 
264 class Gfx8BitFont: public GfxFont {
265 public:
266 
267   Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GooString *nameA,
268 	      GfxFontType typeA, Dict *fontDict);
269 
270   virtual int getNextChar(char *s, int len, CharCode *code,
271 			  Unicode **u, int *uLen,
272 			  double *dx, double *dy, double *ox, double *oy);
273 
274   // Return the encoding.
getEncoding()275   char **getEncoding() { return enc; }
276 
277   // Return the Unicode map.
278   CharCodeToUnicode *getToUnicode();
279 
280   // Return the character name associated with <code>.
getCharName(int code)281   char *getCharName(int code) { return enc[code]; }
282 
283   // Returns true if the PDF font specified an encoding.
getHasEncoding()284   GBool getHasEncoding() { return hasEncoding; }
285 
286   // Returns true if the PDF font specified MacRomanEncoding.
getUsesMacRomanEnc()287   GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
288 
289   // Get width of a character.
getWidth(Guchar c)290   double getWidth(Guchar c) { return widths[c]; }
291 
292   // Return a char code-to-GID mapping for the provided font file.
293   // (This is only useful for TrueType fonts.)
294   Gushort *getCodeToGIDMap(FoFiTrueType *ff);
295 
296   // Return the Type 3 CharProc dictionary, or NULL if none.
297   Dict *getCharProcs();
298 
299   // Return the Type 3 CharProc for the character associated with <code>.
300   Object *getCharProc(int code, Object *proc);
301 
302   // Return the Type 3 Resources dictionary, or NULL if none.
303   Dict *getResources();
304 
305 private:
306   virtual ~Gfx8BitFont();
307 
308   char *enc[256];		// char code --> char name
309   char encFree[256];		// boolean for each char name: if set,
310 				//   the string is malloc'ed
311   CharCodeToUnicode *ctu;	// char code --> Unicode
312   GBool hasEncoding;
313   GBool usesMacRomanEnc;
314   double widths[256];		// character widths
315   Object charProcs;		// Type 3 CharProcs dictionary
316   Object resources;		// Type 3 Resources dictionary
317 };
318 
319 //------------------------------------------------------------------------
320 // GfxCIDFont
321 //------------------------------------------------------------------------
322 
323 class GfxCIDFont: public GfxFont {
324 public:
325 
326   GfxCIDFont(XRef *xref, char *tagA, Ref idA, GooString *nameA,
327 	     Dict *fontDict);
328 
isCIDFont()329   virtual GBool isCIDFont() { return gTrue; }
330 
331   virtual int getNextChar(char *s, int len, CharCode *code,
332 			  Unicode **u, int *uLen,
333 			  double *dx, double *dy, double *ox, double *oy);
334 
335   // Return the writing mode (0=horizontal, 1=vertical).
336   virtual int getWMode();
337 
338   // Return the Unicode map.
339   CharCodeToUnicode *getToUnicode();
340 
341   // Get the collection name (<registry>-<ordering>).
342   GooString *getCollection();
343 
344   // Return the CID-to-GID mapping table.  These should only be called
345   // if type is fontCIDType2.
getCIDToGID()346   Gushort *getCIDToGID() { return cidToGID; }
getCIDToGIDLen()347   int getCIDToGIDLen() { return cidToGIDLen; }
348 
349   Gushort *getCodeToGIDMap(FoFiTrueType *ff, int *length);
350 
351   double getWidth(char* s, int len);
352 
353 private:
354   virtual ~GfxCIDFont();
355 
356   Gushort mapCodeToGID(FoFiTrueType *ff, int cmapi,
357     Unicode unicode, GBool wmode);
358 
359   CMap *cMap;			// char code --> CID
360   CharCodeToUnicode *ctu;	// CID --> Unicode
361   GfxFontCIDWidths widths;	// character widths
362   Gushort *cidToGID;		// CID --> GID mapping (for embedded
363 				//   TrueType fonts)
364   int cidToGIDLen;
365 };
366 
367 //------------------------------------------------------------------------
368 // GfxFontDict
369 //------------------------------------------------------------------------
370 
371 class GfxFontDict {
372 public:
373 
374   // Build the font dictionary, given the PDF font dictionary.
375   GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
376 
377   // Destructor.
378   ~GfxFontDict();
379 
380   // Get the specified font.
381   GfxFont *lookup(char *tag);
382 
383   // Iterative access.
getNumFonts()384   int getNumFonts() { return numFonts; }
getFont(int i)385   GfxFont *getFont(int i) { return fonts[i]; }
386 
387 private:
388 
389   GfxFont **fonts;		// list of fonts
390   int numFonts;			// number of fonts
391 };
392 
393 #endif
394