7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
21
/** Zend_Pdf_FileParserDataSource */
22
require_once 'Zend/Pdf/FileParserDataSource.php';
24
/** Zend_Pdf_FileParserDataSource_File */
25
require_once 'Zend/Pdf/FileParserDataSource/File.php';
27
/** Zend_Pdf_FileParserDataSource_String */
28
require_once 'Zend/Pdf/FileParserDataSource/String.php';
30
/** Zend_Pdf_FileParser_Font_OpenType_TrueType */
31
require_once 'Zend/Pdf/FileParser/Font/OpenType/TrueType.php';
33
/** Zend_Pdf_Resource_Font_Simple_Parsed_TrueType */
34
require_once 'Zend/Pdf/Resource/Font/Simple/Parsed/TrueType.php';
36
/** Zend_Pdf_Resource_Font_Type0 */
37
require_once 'Zend/Pdf/Resource/Font/Type0.php';
39
/** Zend_Pdf_Resource_Font_CidFont_TrueType */
40
require_once 'Zend/Pdf/Resource/Font/CidFont/TrueType.php';
42
/** Zend_Pdf_Resource_Font_Simple_Standard_Courier */
43
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Courier.php';
45
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierBold */
46
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBold.php';
48
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique */
49
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBoldOblique.php';
51
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique */
52
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierOblique.php';
54
/** Zend_Pdf_Resource_Font_Simple_Standard_Helvetica */
55
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Helvetica.php';
57
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold */
58
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBold.php';
60
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique */
61
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBoldOblique.php';
63
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique */
64
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaOblique.php';
66
/** Zend_Pdf_Resource_Font_Simple_Standard_Symbol */
67
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Symbol.php';
69
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesBold */
70
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBold.php';
72
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic */
73
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBoldItalic.php';
75
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic */
76
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesItalic.php';
78
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman */
79
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesRoman.php';
81
/** Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats */
82
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/ZapfDingbats.php';
84
/** Zend_Pdf_Resource_Font_Extracted */
85
require_once 'Zend/Pdf/Resource/Font/Extracted.php';
89
* Abstract factory class which vends {@link Zend_Pdf_Resource_Font} objects.
91
* Font objects themselves are normally instantiated through the factory methods
92
* {@link fontWithName()} or {@link fontWithPath()}.
94
* This class is also the home for font-related constants because the name of
95
* the true base class ({@link Zend_Pdf_Resource_Font}) is not intuitive for the
100
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
101
* @license http://framework.zend.com/license/new-bsd New BSD License
103
abstract class Zend_Pdf_Font
105
/**** Class Constants ****/
113
const TYPE_UNKNOWN = 0;
116
* One of the standard 14 PDF fonts.
118
const TYPE_STANDARD = 1;
121
* A PostScript Type 1 font.
123
const TYPE_TYPE_1 = 2;
126
* A TrueType font or an OpenType font containing TrueType outlines.
128
const TYPE_TRUETYPE = 3;
131
* Type 0 composite font.
133
const TYPE_TYPE_0 = 4;
136
* CID font containing a PostScript Type 1 font.
137
* These fonts are used only to construct Type 0 composite fonts and can't be used directly
139
const TYPE_CIDFONT_TYPE_0 = 5;
142
* CID font containing a TrueType font or an OpenType font containing TrueType outlines.
143
* These fonts are used only to construct Type 0 composite fonts and can't be used directly
145
const TYPE_CIDFONT_TYPE_2 = 6;
148
/* Names of the Standard 14 PDF Fonts */
151
* Name of the standard PDF font Courier.
153
const FONT_COURIER = 'Courier';
156
* Name of the bold style of the standard PDF font Courier.
158
const FONT_COURIER_BOLD = 'Courier-Bold';
161
* Name of the italic style of the standard PDF font Courier.
163
const FONT_COURIER_OBLIQUE = 'Courier-Oblique';
166
* Convenience constant for a common misspelling of
167
* {@link FONT_COURIER_OBLIQUE}.
169
const FONT_COURIER_ITALIC = 'Courier-Oblique';
172
* Name of the bold and italic style of the standard PDF font Courier.
174
const FONT_COURIER_BOLD_OBLIQUE = 'Courier-BoldOblique';
177
* Convenience constant for a common misspelling of
178
* {@link FONT_COURIER_BOLD_OBLIQUE}.
180
const FONT_COURIER_BOLD_ITALIC = 'Courier-BoldOblique';
183
* Name of the standard PDF font Helvetica.
185
const FONT_HELVETICA = 'Helvetica';
188
* Name of the bold style of the standard PDF font Helvetica.
190
const FONT_HELVETICA_BOLD = 'Helvetica-Bold';
193
* Name of the italic style of the standard PDF font Helvetica.
195
const FONT_HELVETICA_OBLIQUE = 'Helvetica-Oblique';
198
* Convenience constant for a common misspelling of
199
* {@link FONT_HELVETICA_OBLIQUE}.
201
const FONT_HELVETICA_ITALIC = 'Helvetica-Oblique';
204
* Name of the bold and italic style of the standard PDF font Helvetica.
206
const FONT_HELVETICA_BOLD_OBLIQUE = 'Helvetica-BoldOblique';
209
* Convenience constant for a common misspelling of
210
* {@link FONT_HELVETICA_BOLD_OBLIQUE}.
212
const FONT_HELVETICA_BOLD_ITALIC = 'Helvetica-BoldOblique';
215
* Name of the standard PDF font Symbol.
217
const FONT_SYMBOL = 'Symbol';
220
* Name of the standard PDF font Times.
222
const FONT_TIMES_ROMAN = 'Times-Roman';
225
* Convenience constant for a common misspelling of
226
* {@link FONT_TIMES_ROMAN}.
228
const FONT_TIMES = 'Times-Roman';
231
* Name of the bold style of the standard PDF font Times.
233
const FONT_TIMES_BOLD = 'Times-Bold';
236
* Name of the italic style of the standard PDF font Times.
238
const FONT_TIMES_ITALIC = 'Times-Italic';
241
* Name of the bold and italic style of the standard PDF font Times.
243
const FONT_TIMES_BOLD_ITALIC = 'Times-BoldItalic';
246
* Name of the standard PDF font Zapf Dingbats.
248
const FONT_ZAPFDINGBATS = 'ZapfDingbats';
251
/* Font Name String Types */
254
* Full copyright notice for the font.
256
const NAME_COPYRIGHT = 0;
259
* Font family name. Used to group similar styles of fonts together.
261
const NAME_FAMILY = 1;
264
* Font style within the font family. Examples: Regular, Italic, Bold, etc.
266
const NAME_STYLE = 2;
269
* Unique font identifier.
274
* Full font name. Usually a combination of the {@link NAME_FAMILY} and
275
* {@link NAME_STYLE} strings.
280
* Version number of the font.
282
const NAME_VERSION = 5;
285
* PostScript name for the font. This is the name used to identify fonts
286
* internally and within the PDF file.
288
const NAME_POSTSCRIPT = 6;
291
* Font trademark notice. This is distinct from the {@link NAME_COPYRIGHT}.
293
const NAME_TRADEMARK = 7;
296
* Name of the font manufacturer.
298
const NAME_MANUFACTURER = 8;
301
* Name of the designer of the font.
303
const NAME_DESIGNER = 9;
306
* Description of the font. May contain revision information, usage
307
* recommendations, features, etc.
309
const NAME_DESCRIPTION = 10;
312
* URL of the font vendor. Some fonts may contain a unique serial number
313
* embedded in this URL, which is used for licensing.
315
const NAME_VENDOR_URL = 11;
318
* URL of the font designer ({@link NAME_DESIGNER}).
320
const NAME_DESIGNER_URL = 12;
323
* Plain language licensing terms for the font.
325
const NAME_LICENSE = 13;
328
* URL of more detailed licensing information for the font.
330
const NAME_LICENSE_URL = 14;
333
* Preferred font family. Used by some fonts to work around a Microsoft
334
* Windows limitation where only four fonts styles can share the same
335
* {@link NAME_FAMILY} value.
337
const NAME_PREFERRED_FAMILY = 16;
340
* Preferred font style. A more descriptive string than {@link NAME_STYLE}.
342
const NAME_PREFERRED_STYLE = 17;
345
* Suggested text to use as a representative sample of the font.
347
const NAME_SAMPLE_TEXT = 19;
350
* PostScript CID findfont name.
352
const NAME_CID_NAME = 20;
360
const WEIGHT_THIN = 100;
363
* Extra-light (Ultra-light) font weight.
365
const WEIGHT_EXTRA_LIGHT = 200;
370
const WEIGHT_LIGHT = 300;
373
* Normal (Regular) font weight.
375
const WEIGHT_NORMAL = 400;
378
* Medium font weight.
380
const WEIGHT_MEDIUM = 500;
383
* Semi-bold (Demi-bold) font weight.
385
const WEIGHT_SEMI_BOLD = 600;
390
const WEIGHT_BOLD = 700;
393
* Extra-bold (Ultra-bold) font weight.
395
const WEIGHT_EXTRA_BOLD = 800;
398
* Black (Heavy) font weight.
400
const WEIGHT_BLACK = 900;
406
* Ultra-condensed font width. Typically 50% of normal.
408
const WIDTH_ULTRA_CONDENSED = 1;
411
* Extra-condensed font width. Typically 62.5% of normal.
413
const WIDTH_EXTRA_CONDENSED = 2;
416
* Condensed font width. Typically 75% of normal.
418
const WIDTH_CONDENSED = 3;
421
* Semi-condensed font width. Typically 87.5% of normal.
423
const WIDTH_SEMI_CONDENSED = 4;
426
* Normal (Medium) font width.
428
const WIDTH_NORMAL = 5;
431
* Semi-expanded font width. Typically 112.5% of normal.
433
const WIDTH_SEMI_EXPANDED = 6;
436
* Expanded font width. Typically 125% of normal.
438
const WIDTH_EXPANDED = 7;
441
* Extra-expanded font width. Typically 150% of normal.
443
const WIDTH_EXTRA_EXPANDED = 8;
446
* Ultra-expanded font width. Typically 200% of normal.
448
const WIDTH_ULTRA_EXPANDED = 9;
451
/* Font Embedding Options */
454
* Do not embed the font in the PDF document.
456
const EMBED_DONT_EMBED = 0x01;
459
* Embed, but do not subset the font in the PDF document.
461
const EMBED_DONT_SUBSET = 0x02;
464
* Embed, but do not compress the font in the PDF document.
466
const EMBED_DONT_COMPRESS = 0x04;
469
* Suppress the exception normally thrown if the font cannot be embedded
470
* due to its copyright bits being set.
472
const EMBED_SUPPRESS_EMBED_EXCEPTION = 0x08;
476
/**** Class Variables ****/
480
* Array whose keys are the unique PostScript names of instantiated fonts.
481
* The values are the font objects themselves.
484
private static $_fontNames = array();
487
* Array whose keys are the md5 hash of the full paths on disk for parsed
488
* fonts. The values are the font objects themselves.
491
private static $_fontFilePaths = array();
495
/**** Public Interface ****/
498
/* Factory Methods */
501
* Returns a {@link Zend_Pdf_Resource_Font} object by full name.
503
* This is the preferred method to obtain one of the standard 14 PDF fonts.
505
* The result of this method is cached, preventing unnecessary duplication
506
* of font objects. Repetitive calls for a font with the same name will
507
* return the same object.
509
* The $embeddingOptions parameter allows you to set certain flags related
510
* to font embedding. You may combine options by OR-ing them together. See
511
* the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
512
* available options and their descriptions. Note that this value is only
513
* used when creating a font for the first time. If a font with the same
514
* name already exists, you will get that object and the options you specify
515
* here will be ignored. This is because fonts are only embedded within the
518
* If the font name supplied does not match the name of a previously
519
* instantiated object and it is not one of the 14 standard PDF fonts, an
520
* exception will be thrown.
522
* @param string $name Full PostScript name of font.
523
* @param integer $embeddingOptions (optional) Options for font embedding.
524
* @return Zend_Pdf_Resource_Font
525
* @throws Zend_Pdf_Exception
527
public static function fontWithName($name, $embeddingOptions = 0)
529
/* First check the cache. Don't duplicate font objects.
531
if (isset(Zend_Pdf_Font::$_fontNames[$name])) {
532
return Zend_Pdf_Font::$_fontNames[$name];
536
* @todo It would be cool to be able to have a mapping of font names to
537
* file paths in a configuration file for frequently used custom
538
* fonts. This would allow a user to use custom fonts without having
539
* to hard-code file paths all over the place. Table this idea until
540
* {@link Zend_Config} is ready.
543
/* Not an existing font and no mapping in the config file. Check to see
544
* if this is one of the standard 14 PDF fonts.
547
case Zend_Pdf_Font::FONT_COURIER:
548
$font = new Zend_Pdf_Resource_Font_Simple_Standard_Courier();
551
case Zend_Pdf_Font::FONT_COURIER_BOLD:
552
$font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBold();
555
case Zend_Pdf_Font::FONT_COURIER_OBLIQUE:
556
$font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique();
559
case Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE:
560
$font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique();
563
case Zend_Pdf_Font::FONT_HELVETICA:
564
$font = new Zend_Pdf_Resource_Font_Simple_Standard_Helvetica();
567
case Zend_Pdf_Font::FONT_HELVETICA_BOLD:
568
$font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold();
571
case Zend_Pdf_Font::FONT_HELVETICA_OBLIQUE:
572
$font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique();
575
case Zend_Pdf_Font::FONT_HELVETICA_BOLD_OBLIQUE:
576
$font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique();
579
case Zend_Pdf_Font::FONT_SYMBOL:
580
$font = new Zend_Pdf_Resource_Font_Simple_Standard_Symbol();
583
case Zend_Pdf_Font::FONT_TIMES_ROMAN:
584
$font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman();
587
case Zend_Pdf_Font::FONT_TIMES_BOLD:
588
$font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBold();
591
case Zend_Pdf_Font::FONT_TIMES_ITALIC:
592
$font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic();
595
case Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC:
596
$font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic();
599
case Zend_Pdf_Font::FONT_ZAPFDINGBATS:
600
$font = new Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats();
604
throw new Zend_Pdf_Exception("Unknown font name: $name",
605
Zend_Pdf_Exception::BAD_FONT_NAME);
608
/* Add this new font to the cache array and return it for use.
610
Zend_Pdf_Font::$_fontNames[$name] = $font;
615
* Returns a {@link Zend_Pdf_Resource_Font} object by file path.
617
* The result of this method is cached, preventing unnecessary duplication
618
* of font objects. Repetitive calls for the font with the same path will
619
* return the same object.
621
* The $embeddingOptions parameter allows you to set certain flags related
622
* to font embedding. You may combine options by OR-ing them together. See
623
* the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
624
* available options and their descriptions. Note that this value is only
625
* used when creating a font for the first time. If a font with the same
626
* name already exists, you will get that object and the options you specify
627
* here will be ignored. This is because fonts are only embedded within the
630
* If the file path supplied does not match the path of a previously
631
* instantiated object or the font type cannot be determined, an exception
634
* @param string $filePath Full path to the font file.
635
* @param integer $embeddingOptions (optional) Options for font embedding.
636
* @return Zend_Pdf_Resource_Font
637
* @throws Zend_Pdf_Exception
639
public static function fontWithPath($filePath, $embeddingOptions = 0)
641
/* First check the cache. Don't duplicate font objects.
643
$filePathKey = md5($filePath);
644
if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
645
return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
648
/* Create a file parser data source object for this file. File path and
649
* access permission checks are handled here.
651
$dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
653
/* Attempt to determine the type of font. We can't always trust file
654
* extensions, but try that first since it's fastest.
656
$fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
658
/* If it turns out that the file is named improperly and we guess the
659
* wrong type, we'll get null instead of a font object.
661
switch ($fileExtension) {
663
$font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
667
/* Unrecognized extension. Try to determine the type by actually
675
if (is_null($font)) {
676
/* There was no match for the file extension or the extension was
677
* wrong. Attempt to detect the type of font by actually parsing it.
678
* We'll do the checks in order of most likely format to try to
679
* reduce the detection time.
685
if ((is_null($font)) && ($fileExtension != 'ttf')) {
686
$font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
697
/* Done with the data source object.
701
if (! is_null($font)) {
702
/* Parsing was successful. Add this font instance to the cache arrays
703
* and return it for use.
705
$fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
706
Zend_Pdf_Font::$_fontNames[$fontName] = $font;
707
$filePathKey = md5($filePath);
708
Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
712
/* The type of font could not be determined. Give up.
714
throw new Zend_Pdf_Exception("Cannot determine font type: $filePath",
715
Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE);
722
/**** Internal Methods ****/
725
/* Font Extraction Methods */
728
* Attempts to extract a TrueType font from the data source.
730
* If the font parser throws an exception that suggests the data source
731
* simply doesn't contain a TrueType font, catches it and returns null. If
732
* an exception is thrown that suggests the TrueType font is corrupt or
733
* otherwise unusable, throws that exception. If successful, returns the
736
* @param Zend_Pdf_FileParserDataSource $dataSource
737
* @param integer $embeddingOptions Options for font embedding.
738
* @return Zend_Pdf_Resource_Font_OpenType_TrueType May also return null if
739
* the data source does not appear to contain a TrueType font.
740
* @throws Zend_Pdf_Exception
742
protected static function _extractTrueTypeFont($dataSource, $embeddingOptions)
745
$fontParser = new Zend_Pdf_FileParser_Font_OpenType_TrueType($dataSource);
747
$fontParser->parse();
748
if ($fontParser->isAdobeLatinSubset) {
749
$font = new Zend_Pdf_Resource_Font_Simple_Parsed_TrueType($fontParser, $embeddingOptions);
751
/* Use Composite Type 0 font which supports Unicode character mapping */
752
$cidFont = new Zend_Pdf_Resource_Font_CidFont_TrueType($fontParser, $embeddingOptions);
753
$font = new Zend_Pdf_Resource_Font_Type0($cidFont);
755
} catch (Zend_Pdf_Exception $exception) {
756
/* The following exception codes suggest that this isn't really a
757
* TrueType font. If we caught such an exception, simply return
758
* null. For all other cases, it probably is a TrueType font but has
759
* a problem; throw the exception again.
762
switch ($exception->getCode()) {
763
case Zend_Pdf_Exception::WRONG_FONT_TYPE: // break intentionally omitted
764
case Zend_Pdf_Exception::BAD_TABLE_COUNT: // break intentionally omitted
765
case Zend_Pdf_Exception::BAD_MAGIC_NUMBER: