1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
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>
26 // To see a description of the changes please see the Changelog file that
27 // came with your tarball or type make ChangeLog if you are building from git
29 //========================================================================
34 #ifdef USE_GCC_PRAGMAS
38 #include "goo/gtypes.h"
39 #include "goo/GooString.h"
41 #include "CharTypes.h"
45 class CharCodeToUnicode
;
48 struct GfxFontCIDWidths
;
49 struct Base14FontMapEntry
;
51 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
74 //------------------------------------------------------------------------
76 struct GfxFontCIDWidthExcep
{
77 CID first
; // this record applies to
78 CID last
; // CIDs <first>..<last>
79 double width
; // char width
82 struct GfxFontCIDWidthExcepV
{
83 CID first
; // this record applies to
84 CID last
; // CIDs <first>..<last>
85 double height
; // char height
86 double vx
, vy
; // origin position
89 struct GfxFontCIDWidths
{
90 double defWidth
; // default char width
91 double defHeight
; // default char height
92 double defVY
; // default origin position
93 GfxFontCIDWidthExcep
*exceps
; // exceptions
94 int nExceps
; // number of valid entries in exceps
95 GfxFontCIDWidthExcepV
* // exceptions for vertical font
97 int nExcepsV
; // number of valid entries in excepsV
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
104 enum GfxFontLocType
{
105 gfxFontLocEmbedded
, // font embedded in PDF file
106 gfxFontLocExternal
, // external font file
107 gfxFontLocResident
// font resident in PS printer
116 GfxFontLocType locType
;
117 GfxFontType fontType
;
118 Ref embFontID
; // embedded stream obj ID
119 // (if locType == gfxFontLocEmbedded)
120 GooString
*path
; // font file path
121 // (if locType == gfxFontLocExternal)
123 // (if locType == gfxFontLocResident)
124 int fontNum
; // for TrueType collections
125 // (if locType == gfxFontLocExternal)
126 GooString
*encoding
; // PS font encoding, only for 16-bit fonts
127 // (if locType == gfxFontLocResident)
128 int wMode
; // writing mode, only for 16-bit fonts
129 // (if locType == gfxFontLocResident)
130 int substIdx
; // substitute font index
131 // (if locType == gfxFontLocExternal,
132 // and a Base-14 substitution was made)
135 //------------------------------------------------------------------------
137 //------------------------------------------------------------------------
139 #define fontFixedWidth (1 << 0)
140 #define fontSerif (1 << 1)
141 #define fontSymbolic (1 << 2)
142 #define fontItalic (1 << 6)
143 #define fontBold (1 << 18)
172 // Build a GfxFont object.
173 static GfxFont
*makeFont(XRef
*xref
, const char *tagA
, Ref idA
, Dict
*fontDict
);
175 GfxFont(const char *tagA
, Ref idA
, GooString
*nameA
,
176 GfxFontType typeA
, Ref embFontIDA
);
178 GBool
isOk() { return ok
; }
184 GooString
*getTag() { return tag
; }
186 // Get font dictionary ID.
187 Ref
*getID() { return &id
; }
189 // Does this font match the tag?
190 GBool
matches(char *tagA
) { return !tag
->cmp(tagA
); }
192 // Get font family name.
193 GooString
*getFamily() { return family
; }
196 Stretch
getStretch() { return stretch
; }
199 Weight
getWeight() { return weight
; }
201 // Get the original font name (ignornig any munging that might have
202 // been done to map to a canonical Base-14 font name).
203 GooString
*getName() { return name
; }
206 GfxFontType
getType() { return type
; }
207 virtual GBool
isCIDFont() { return gFalse
; }
209 // Get embedded font ID, i.e., a ref for the font file stream.
210 // Returns false if there is no embedded font.
211 GBool
getEmbeddedFontID(Ref
*embID
)
212 { *embID
= embFontID
; return embFontID
.num
>= 0; }
214 // Get the PostScript font name for the embedded font. Returns
215 // NULL if there is no embedded font.
216 GooString
*getEmbeddedFontName() { return embFontName
; }
218 // Get font descriptor flags.
219 int getFlags() { return flags
; }
220 GBool
isFixedWidth() { return flags
& fontFixedWidth
; }
221 GBool
isSerif() { return flags
& fontSerif
; }
222 GBool
isSymbolic() { return flags
& fontSymbolic
; }
223 GBool
isItalic() { return flags
& fontItalic
; }
224 GBool
isBold() { return flags
& fontBold
; }
226 // Return the Unicode map.
227 virtual CharCodeToUnicode
*getToUnicode() = 0;
229 // Return the font matrix.
230 double *getFontMatrix() { return fontMat
; }
232 // Return the font bounding box.
233 double *getFontBBox() { return fontBBox
; }
235 // Return the ascent and descent values.
236 double getAscent() { return ascent
; }
237 double getDescent() { return descent
; }
239 // Return the writing mode (0=horizontal, 1=vertical).
240 virtual int getWMode() { return 0; }
242 // Locate the font file for this font. If <ps> is not null, includes PS
243 // printer-resident fonts. Returns NULL on failure.
244 GfxFontLoc
*locateFont(XRef
*xref
, PSOutputDev
*ps
);
246 // Locate a Base-14 font file for a specified font name.
247 static GfxFontLoc
*locateBase14Font(GooString
*base14Name
);
249 // Read an external or embedded font file into a buffer.
250 char *readEmbFontFile(XRef
*xref
, int *len
);
252 // Get the next char from a string <s> of <len> bytes, returning the
253 // char <code>, its Unicode mapping <u>, its displacement vector
254 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize>
255 // is the number of entries available in <u>, and <uLen> is set to
256 // the number actually used. Returns the number of bytes used by
258 virtual int getNextChar(char *s
, int len
, CharCode
*code
,
259 Unicode
**u
, int *uLen
,
260 double *dx
, double *dy
, double *ox
, double *oy
) = 0;
262 // Does this font have a toUnicode map?
263 GBool
hasToUnicodeCMap() { return hasToUnicode
; }
265 // Return the name of the encoding
266 GooString
*getEncodingName() { return encodingName
; }
268 // Return AGLFN names of ligatures in the Standard and Expert encodings
269 // for use with fonts that are not compatible with the Standard 14 fonts.
270 // http://sourceforge.net/adobe/aglfn/wiki/AGL%20Specification/
271 static const char *getAlternateName(const char *name
);
277 static GfxFontType
getFontType(XRef
*xref
, Dict
*fontDict
, Ref
*embID
);
278 void readFontDescriptor(XRef
*xref
, Dict
*fontDict
);
279 CharCodeToUnicode
*readToUnicodeCMap(Dict
*fontDict
, int nBits
,
280 CharCodeToUnicode
*ctu
);
281 static GfxFontLoc
*getExternalFont(GooString
*path
, GBool cid
);
283 GooString
*tag
; // PDF font tag
284 Ref id
; // reference (used as unique ID)
285 GooString
*name
; // font name
286 GooString
*family
; // font family
287 Stretch stretch
; // font stretch
288 Weight weight
; // font weight
289 GfxFontType type
; // type of font
290 int flags
; // font descriptor flags
291 GooString
*embFontName
; // name of embedded font
292 Ref embFontID
; // ref to embedded font file stream
293 double fontMat
[6]; // font matrix (Type 3 only)
294 double fontBBox
[4]; // font bounding box (Type 3 only)
295 double missingWidth
; // "default" width
296 double ascent
; // max height above baseline
297 double descent
; // max depth below baseline
301 GooString
*encodingName
;
304 //------------------------------------------------------------------------
306 //------------------------------------------------------------------------
308 class Gfx8BitFont
: public GfxFont
{
311 Gfx8BitFont(XRef
*xref
, const char *tagA
, Ref idA
, GooString
*nameA
,
312 GfxFontType typeA
, Ref embFontIDA
, Dict
*fontDict
);
314 virtual int getNextChar(char *s
, int len
, CharCode
*code
,
315 Unicode
**u
, int *uLen
,
316 double *dx
, double *dy
, double *ox
, double *oy
);
318 // Return the encoding.
319 char **getEncoding() { return enc
; }
321 // Return the Unicode map.
322 CharCodeToUnicode
*getToUnicode();
324 // Return the character name associated with <code>.
325 char *getCharName(int code
) { return enc
[code
]; }
327 // Returns true if the PDF font specified an encoding.
328 GBool
getHasEncoding() { return hasEncoding
; }
330 // Returns true if the PDF font specified MacRomanEncoding.
331 GBool
getUsesMacRomanEnc() { return usesMacRomanEnc
; }
333 // Get width of a character.
334 double getWidth(Guchar c
) { return widths
[c
]; }
336 // Return a char code-to-GID mapping for the provided font file.
337 // (This is only useful for TrueType fonts.)
338 int *getCodeToGIDMap(FoFiTrueType
*ff
);
340 // Return the Type 3 CharProc dictionary, or NULL if none.
341 Dict
*getCharProcs();
343 // Return the Type 3 CharProc for the character associated with <code>.
344 Object
*getCharProc(int code
, Object
*proc
);
346 // Return the Type 3 Resources dictionary, or NULL if none.
347 Dict
*getResources();
350 virtual ~Gfx8BitFont();
352 const Base14FontMapEntry
*base14
; // for Base-14 fonts only; NULL otherwise
353 char *enc
[256]; // char code --> char name
354 char encFree
[256]; // boolean for each char name: if set,
355 // the string is malloc'ed
356 CharCodeToUnicode
*ctu
; // char code --> Unicode
358 GBool usesMacRomanEnc
;
359 double widths
[256]; // character widths
360 Object charProcs
; // Type 3 CharProcs dictionary
361 Object resources
; // Type 3 Resources dictionary
363 friend class GfxFont
;
366 //------------------------------------------------------------------------
368 //------------------------------------------------------------------------
370 class GfxCIDFont
: public GfxFont
{
373 GfxCIDFont(XRef
*xref
, const char *tagA
, Ref idA
, GooString
*nameA
,
374 GfxFontType typeA
, Ref embFontIDA
, Dict
*fontDict
);
376 virtual GBool
isCIDFont() { return gTrue
; }
378 virtual int getNextChar(char *s
, int len
, CharCode
*code
,
379 Unicode
**u
, int *uLen
,
380 double *dx
, double *dy
, double *ox
, double *oy
);
382 // Return the writing mode (0=horizontal, 1=vertical).
383 virtual int getWMode();
385 // Return the Unicode map.
386 CharCodeToUnicode
*getToUnicode();
388 // Get the collection name (<registry>-<ordering>).
389 GooString
*getCollection();
391 // Return the CID-to-GID mapping table. These should only be called
392 // if type is fontCIDType2.
393 int *getCIDToGID() { return cidToGID
; }
394 int getCIDToGIDLen() { return cidToGIDLen
; }
396 int *getCodeToGIDMap(FoFiTrueType
*ff
, int *length
);
398 double getWidth(char* s
, int len
);
401 virtual ~GfxCIDFont();
403 int mapCodeToGID(FoFiTrueType
*ff
, int cmapi
,
404 Unicode unicode
, GBool wmode
);
405 double getWidth(CID cid
); // Get width of a character.
407 GooString
*collection
; // collection name
408 CMap
*cMap
; // char code --> CID
409 CharCodeToUnicode
*ctu
; // CID --> Unicode
410 GBool ctuUsesCharCode
; // true: ctu maps char code to Unicode;
411 // false: ctu maps CID to Unicode
412 GfxFontCIDWidths widths
; // character widths
413 int *cidToGID
; // CID --> GID mapping (for embedded
418 //------------------------------------------------------------------------
420 //------------------------------------------------------------------------
425 // Build the font dictionary, given the PDF font dictionary.
426 GfxFontDict(XRef
*xref
, Ref
*fontDictRef
, Dict
*fontDict
);
431 // Get the specified font.
432 GfxFont
*lookup(char *tag
);
435 int getNumFonts() { return numFonts
; }
436 GfxFont
*getFont(int i
) { return fonts
[i
]; }
440 GfxFont
**fonts
; // list of fonts
441 int numFonts
; // number of fonts