File indexing completed on 2024-12-22 05:36:56

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @subpackage Fonts
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * Abstract factory class which vends {@link Zend_Pdf_Resource_Font} objects.
0026  *
0027  * Font objects themselves are normally instantiated through the factory methods
0028  * {@link fontWithName()} or {@link fontWithPath()}.
0029  *
0030  * This class is also the home for font-related constants because the name of
0031  * the true base class ({@link Zend_Pdf_Resource_Font}) is not intuitive for the
0032  * end user.
0033  *
0034  * @package    Zend_Pdf
0035  * @subpackage Fonts
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 abstract class Zend_Pdf_Font
0040 {
0041   /**** Class Constants ****/
0042 
0043 
0044   /* Font Types */
0045 
0046     /**
0047      * Unknown font type.
0048      */
0049     const TYPE_UNKNOWN = 0;
0050 
0051     /**
0052      * One of the standard 14 PDF fonts.
0053      */
0054     const TYPE_STANDARD = 1;
0055 
0056     /**
0057      * A PostScript Type 1 font.
0058      */
0059     const TYPE_TYPE_1 = 2;
0060 
0061     /**
0062      * A TrueType font or an OpenType font containing TrueType outlines.
0063      */
0064     const TYPE_TRUETYPE = 3;
0065 
0066     /**
0067      * Type 0 composite font.
0068      */
0069     const TYPE_TYPE_0 = 4;
0070 
0071     /**
0072      * CID font containing a PostScript Type 1 font.
0073      * These fonts are used only to construct Type 0 composite fonts and can't be used directly
0074      */
0075     const TYPE_CIDFONT_TYPE_0 = 5;
0076 
0077     /**
0078      * CID font containing a TrueType font or an OpenType font containing TrueType outlines.
0079      * These fonts are used only to construct Type 0 composite fonts and can't be used directly
0080      */
0081     const TYPE_CIDFONT_TYPE_2 = 6;
0082 
0083 
0084   /* Names of the Standard 14 PDF Fonts */
0085 
0086     /**
0087      * Name of the standard PDF font Courier.
0088      */
0089     const FONT_COURIER = 'Courier';
0090 
0091     /**
0092      * Name of the bold style of the standard PDF font Courier.
0093      */
0094     const FONT_COURIER_BOLD = 'Courier-Bold';
0095 
0096     /**
0097      * Name of the italic style of the standard PDF font Courier.
0098      */
0099     const FONT_COURIER_OBLIQUE = 'Courier-Oblique';
0100 
0101     /**
0102      * Convenience constant for a common misspelling of
0103      * {@link FONT_COURIER_OBLIQUE}.
0104      */
0105     const FONT_COURIER_ITALIC = 'Courier-Oblique';
0106 
0107     /**
0108      * Name of the bold and italic style of the standard PDF font Courier.
0109      */
0110     const FONT_COURIER_BOLD_OBLIQUE = 'Courier-BoldOblique';
0111 
0112     /**
0113      * Convenience constant for a common misspelling of
0114      * {@link FONT_COURIER_BOLD_OBLIQUE}.
0115      */
0116     const FONT_COURIER_BOLD_ITALIC = 'Courier-BoldOblique';
0117 
0118     /**
0119      * Name of the standard PDF font Helvetica.
0120      */
0121     const FONT_HELVETICA = 'Helvetica';
0122 
0123     /**
0124      * Name of the bold style of the standard PDF font Helvetica.
0125      */
0126     const FONT_HELVETICA_BOLD = 'Helvetica-Bold';
0127 
0128     /**
0129      * Name of the italic style of the standard PDF font Helvetica.
0130      */
0131     const FONT_HELVETICA_OBLIQUE = 'Helvetica-Oblique';
0132 
0133     /**
0134      * Convenience constant for a common misspelling of
0135      * {@link FONT_HELVETICA_OBLIQUE}.
0136      */
0137     const FONT_HELVETICA_ITALIC = 'Helvetica-Oblique';
0138 
0139     /**
0140      * Name of the bold and italic style of the standard PDF font Helvetica.
0141      */
0142     const FONT_HELVETICA_BOLD_OBLIQUE = 'Helvetica-BoldOblique';
0143 
0144     /**
0145      * Convenience constant for a common misspelling of
0146      * {@link FONT_HELVETICA_BOLD_OBLIQUE}.
0147      */
0148     const FONT_HELVETICA_BOLD_ITALIC = 'Helvetica-BoldOblique';
0149 
0150     /**
0151      * Name of the standard PDF font Symbol.
0152      */
0153     const FONT_SYMBOL = 'Symbol';
0154 
0155     /**
0156      * Name of the standard PDF font Times.
0157      */
0158     const FONT_TIMES_ROMAN = 'Times-Roman';
0159 
0160     /**
0161      * Convenience constant for a common misspelling of
0162      * {@link FONT_TIMES_ROMAN}.
0163      */
0164     const FONT_TIMES = 'Times-Roman';
0165 
0166     /**
0167      * Name of the bold style of the standard PDF font Times.
0168      */
0169     const FONT_TIMES_BOLD = 'Times-Bold';
0170 
0171     /**
0172      * Name of the italic style of the standard PDF font Times.
0173      */
0174     const FONT_TIMES_ITALIC = 'Times-Italic';
0175 
0176     /**
0177      * Name of the bold and italic style of the standard PDF font Times.
0178      */
0179     const FONT_TIMES_BOLD_ITALIC = 'Times-BoldItalic';
0180 
0181     /**
0182      * Name of the standard PDF font Zapf Dingbats.
0183      */
0184     const FONT_ZAPFDINGBATS = 'ZapfDingbats';
0185 
0186 
0187   /* Font Name String Types */
0188 
0189     /**
0190      * Full copyright notice for the font.
0191      */
0192     const NAME_COPYRIGHT =  0;
0193 
0194     /**
0195      * Font family name. Used to group similar styles of fonts together.
0196      */
0197     const NAME_FAMILY =  1;
0198 
0199     /**
0200      * Font style within the font family. Examples: Regular, Italic, Bold, etc.
0201      */
0202     const NAME_STYLE =  2;
0203 
0204     /**
0205      * Unique font identifier.
0206      */
0207     const NAME_ID =  3;
0208 
0209     /**
0210      * Full font name. Usually a combination of the {@link NAME_FAMILY} and
0211      * {@link NAME_STYLE} strings.
0212      */
0213     const NAME_FULL =  4;
0214 
0215     /**
0216      * Version number of the font.
0217      */
0218     const NAME_VERSION =  5;
0219 
0220     /**
0221      * PostScript name for the font. This is the name used to identify fonts
0222      * internally and within the PDF file.
0223      */
0224     const NAME_POSTSCRIPT =  6;
0225 
0226     /**
0227      * Font trademark notice. This is distinct from the {@link NAME_COPYRIGHT}.
0228      */
0229     const NAME_TRADEMARK =  7;
0230 
0231     /**
0232      * Name of the font manufacturer.
0233      */
0234     const NAME_MANUFACTURER =  8;
0235 
0236     /**
0237      * Name of the designer of the font.
0238      */
0239     const NAME_DESIGNER =  9;
0240 
0241     /**
0242      * Description of the font. May contain revision information, usage
0243      * recommendations, features, etc.
0244      */
0245     const NAME_DESCRIPTION = 10;
0246 
0247     /**
0248      * URL of the font vendor. Some fonts may contain a unique serial number
0249      * embedded in this URL, which is used for licensing.
0250      */
0251     const NAME_VENDOR_URL = 11;
0252 
0253     /**
0254      * URL of the font designer ({@link NAME_DESIGNER}).
0255      */
0256     const NAME_DESIGNER_URL = 12;
0257 
0258     /**
0259      * Plain language licensing terms for the font.
0260      */
0261     const NAME_LICENSE = 13;
0262 
0263     /**
0264      * URL of more detailed licensing information for the font.
0265      */
0266     const NAME_LICENSE_URL = 14;
0267 
0268     /**
0269      * Preferred font family. Used by some fonts to work around a Microsoft
0270      * Windows limitation where only four fonts styles can share the same
0271      * {@link NAME_FAMILY} value.
0272      */
0273     const NAME_PREFERRED_FAMILY = 16;
0274 
0275     /**
0276      * Preferred font style. A more descriptive string than {@link NAME_STYLE}.
0277      */
0278     const NAME_PREFERRED_STYLE = 17;
0279 
0280     /**
0281      * Suggested text to use as a representative sample of the font.
0282      */
0283     const NAME_SAMPLE_TEXT = 19;
0284 
0285     /**
0286      * PostScript CID findfont name.
0287      */
0288     const NAME_CID_NAME = 20;
0289 
0290 
0291   /* Font Weights */
0292 
0293     /**
0294      * Thin font weight.
0295      */
0296     const WEIGHT_THIN = 100;
0297 
0298     /**
0299      * Extra-light (Ultra-light) font weight.
0300      */
0301     const WEIGHT_EXTRA_LIGHT = 200;
0302 
0303     /**
0304      * Light font weight.
0305      */
0306     const WEIGHT_LIGHT = 300;
0307 
0308     /**
0309      * Normal (Regular) font weight.
0310      */
0311     const WEIGHT_NORMAL = 400;
0312 
0313     /**
0314      * Medium font weight.
0315      */
0316     const WEIGHT_MEDIUM = 500;
0317 
0318     /**
0319      * Semi-bold (Demi-bold) font weight.
0320      */
0321     const WEIGHT_SEMI_BOLD = 600;
0322 
0323     /**
0324      * Bold font weight.
0325      */
0326     const WEIGHT_BOLD = 700;
0327 
0328     /**
0329      * Extra-bold (Ultra-bold) font weight.
0330      */
0331     const WEIGHT_EXTRA_BOLD = 800;
0332 
0333     /**
0334      * Black (Heavy) font weight.
0335      */
0336     const WEIGHT_BLACK = 900;
0337 
0338 
0339   /* Font Widths */
0340 
0341     /**
0342      * Ultra-condensed font width. Typically 50% of normal.
0343      */
0344     const WIDTH_ULTRA_CONDENSED = 1;
0345 
0346     /**
0347      * Extra-condensed font width. Typically 62.5% of normal.
0348      */
0349     const WIDTH_EXTRA_CONDENSED = 2;
0350 
0351     /**
0352      * Condensed font width. Typically 75% of normal.
0353      */
0354     const WIDTH_CONDENSED = 3;
0355 
0356     /**
0357      * Semi-condensed font width. Typically 87.5% of normal.
0358      */
0359     const WIDTH_SEMI_CONDENSED = 4;
0360 
0361     /**
0362      * Normal (Medium) font width.
0363      */
0364     const WIDTH_NORMAL = 5;
0365 
0366     /**
0367      * Semi-expanded font width. Typically 112.5% of normal.
0368      */
0369     const WIDTH_SEMI_EXPANDED = 6;
0370 
0371     /**
0372      * Expanded font width. Typically 125% of normal.
0373      */
0374     const WIDTH_EXPANDED = 7;
0375 
0376     /**
0377      * Extra-expanded font width. Typically 150% of normal.
0378      */
0379     const WIDTH_EXTRA_EXPANDED = 8;
0380 
0381     /**
0382      * Ultra-expanded font width. Typically 200% of normal.
0383      */
0384     const WIDTH_ULTRA_EXPANDED = 9;
0385 
0386 
0387   /* Font Embedding Options */
0388 
0389     /**
0390      * Do not embed the font in the PDF document.
0391      */
0392     const EMBED_DONT_EMBED = 0x01;
0393 
0394     /**
0395      * Embed, but do not subset the font in the PDF document.
0396      */
0397     const EMBED_DONT_SUBSET = 0x02;
0398 
0399     /**
0400      * Embed, but do not compress the font in the PDF document.
0401      */
0402     const EMBED_DONT_COMPRESS = 0x04;
0403 
0404     /**
0405      * Suppress the exception normally thrown if the font cannot be embedded
0406      * due to its copyright bits being set.
0407      */
0408     const EMBED_SUPPRESS_EMBED_EXCEPTION = 0x08;
0409 
0410 
0411 
0412   /**** Class Variables ****/
0413 
0414 
0415     /**
0416      * Array whose keys are the unique PostScript names of instantiated fonts.
0417      * The values are the font objects themselves.
0418      * @var array
0419      */
0420     private static $_fontNames = array();
0421 
0422     /**
0423      * Array whose keys are the md5 hash of the full paths on disk for parsed
0424      * fonts. The values are the font objects themselves.
0425      * @var array
0426      */
0427     private static $_fontFilePaths = array();
0428 
0429 
0430 
0431   /**** Public Interface ****/
0432 
0433 
0434   /* Factory Methods */
0435 
0436     /**
0437      * Returns a {@link Zend_Pdf_Resource_Font} object by full name.
0438      *
0439      * This is the preferred method to obtain one of the standard 14 PDF fonts.
0440      *
0441      * The result of this method is cached, preventing unnecessary duplication
0442      * of font objects. Repetitive calls for a font with the same name will
0443      * return the same object.
0444      *
0445      * The $embeddingOptions parameter allows you to set certain flags related
0446      * to font embedding. You may combine options by OR-ing them together. See
0447      * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
0448      * available options and their descriptions. Note that this value is only
0449      * used when creating a font for the first time. If a font with the same
0450      * name already exists, you will get that object and the options you specify
0451      * here will be ignored. This is because fonts are only embedded within the
0452      * PDF file once.
0453      *
0454      * If the font name supplied does not match the name of a previously
0455      * instantiated object and it is not one of the 14 standard PDF fonts, an
0456      * exception will be thrown.
0457      *
0458      * @param string $name Full PostScript name of font.
0459      * @param integer $embeddingOptions (optional) Options for font embedding.
0460      * @return Zend_Pdf_Resource_Font
0461      * @throws Zend_Pdf_Exception
0462      */
0463     public static function fontWithName($name, $embeddingOptions = 0)
0464         {
0465         /* First check the cache. Don't duplicate font objects.
0466          */
0467         if (isset(Zend_Pdf_Font::$_fontNames[$name])) {
0468             return Zend_Pdf_Font::$_fontNames[$name];
0469         }
0470 
0471         /**
0472          * @todo It would be cool to be able to have a mapping of font names to
0473          *   file paths in a configuration file for frequently used custom
0474          *   fonts. This would allow a user to use custom fonts without having
0475          *   to hard-code file paths all over the place. Table this idea until
0476          *   {@link Zend_Config} is ready.
0477          */
0478 
0479         /* Not an existing font and no mapping in the config file. Check to see
0480          * if this is one of the standard 14 PDF fonts.
0481          */
0482         switch ($name) {
0483             case Zend_Pdf_Font::FONT_COURIER:
0484                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Courier.php';
0485                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_Courier();
0486                 break;
0487 
0488             case Zend_Pdf_Font::FONT_COURIER_BOLD:
0489                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBold.php';
0490                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBold();
0491                 break;
0492 
0493             case Zend_Pdf_Font::FONT_COURIER_OBLIQUE:
0494                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierOblique.php';
0495                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique();
0496                 break;
0497 
0498             case Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE:
0499                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBoldOblique.php';
0500                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique();
0501                 break;
0502 
0503             case Zend_Pdf_Font::FONT_HELVETICA:
0504                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Helvetica.php';
0505                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_Helvetica();
0506                 break;
0507 
0508             case Zend_Pdf_Font::FONT_HELVETICA_BOLD:
0509                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBold.php';
0510                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold();
0511                 break;
0512 
0513             case Zend_Pdf_Font::FONT_HELVETICA_OBLIQUE:
0514                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaOblique.php';
0515                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique();
0516                 break;
0517 
0518             case Zend_Pdf_Font::FONT_HELVETICA_BOLD_OBLIQUE:
0519                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBoldOblique.php';
0520                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique();
0521                 break;
0522 
0523             case Zend_Pdf_Font::FONT_SYMBOL:
0524                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Symbol.php';
0525                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_Symbol();
0526                 break;
0527 
0528             case Zend_Pdf_Font::FONT_TIMES_ROMAN:
0529                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesRoman.php';
0530                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman();
0531                 break;
0532 
0533             case Zend_Pdf_Font::FONT_TIMES_BOLD:
0534                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBold.php';
0535                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBold();
0536                 break;
0537 
0538             case Zend_Pdf_Font::FONT_TIMES_ITALIC:
0539                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesItalic.php';
0540                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic();
0541                 break;
0542 
0543             case Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC:
0544                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBoldItalic.php';
0545                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic();
0546                 break;
0547 
0548             case Zend_Pdf_Font::FONT_ZAPFDINGBATS:
0549                 // require_once 'Zend/Pdf/Resource/Font/Simple/Standard/ZapfDingbats.php';
0550                 $font = new Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats();
0551                 break;
0552 
0553             default:
0554                 // require_once 'Zend/Pdf/Exception.php';
0555                 throw new Zend_Pdf_Exception("Unknown font name: $name",
0556                                              Zend_Pdf_Exception::BAD_FONT_NAME);
0557         }
0558 
0559         /* Add this new font to the cache array and return it for use.
0560          */
0561         Zend_Pdf_Font::$_fontNames[$name] = $font;
0562         return $font;
0563     }
0564 
0565     /**
0566      * Returns a {@link Zend_Pdf_Resource_Font} object by file path.
0567      *
0568      * The result of this method is cached, preventing unnecessary duplication
0569      * of font objects. Repetitive calls for the font with the same path will
0570      * return the same object.
0571      *
0572      * The $embeddingOptions parameter allows you to set certain flags related
0573      * to font embedding. You may combine options by OR-ing them together. See
0574      * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
0575      * available options and their descriptions. Note that this value is only
0576      * used when creating a font for the first time. If a font with the same
0577      * name already exists, you will get that object and the options you specify
0578      * here will be ignored. This is because fonts are only embedded within the
0579      * PDF file once.
0580      *
0581      * If the file path supplied does not match the path of a previously
0582      * instantiated object or the font type cannot be determined, an exception
0583      * will be thrown.
0584      *
0585      * @param string $filePath Full path to the font file.
0586      * @param integer $embeddingOptions (optional) Options for font embedding.
0587      * @return Zend_Pdf_Resource_Font
0588      * @throws Zend_Pdf_Exception
0589      */
0590     public static function fontWithPath($filePath, $embeddingOptions = 0)
0591     {
0592         /* First check the cache. Don't duplicate font objects.
0593          */
0594         $filePathKey = md5($filePath);
0595         if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
0596             return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
0597         }
0598 
0599         /* Create a file parser data source object for this file. File path and
0600          * access permission checks are handled here.
0601          */
0602         // require_once 'Zend/Pdf/FileParserDataSource/File.php';
0603         $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
0604 
0605         /* Attempt to determine the type of font. We can't always trust file
0606          * extensions, but try that first since it's fastest.
0607          */
0608         $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
0609 
0610         /* If it turns out that the file is named improperly and we guess the
0611          * wrong type, we'll get null instead of a font object.
0612          */
0613         switch ($fileExtension) {
0614             case 'ttf':
0615                 $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
0616                 break;
0617 
0618             default:
0619                 /* Unrecognized extension. Try to determine the type by actually
0620                  * parsing it below.
0621                  */
0622                 $font = null;
0623                 break;
0624         }
0625 
0626 
0627         if ($font === null) {
0628             /* There was no match for the file extension or the extension was
0629              * wrong. Attempt to detect the type of font by actually parsing it.
0630              * We'll do the checks in order of most likely format to try to
0631              * reduce the detection time.
0632              */
0633 
0634             // OpenType
0635 
0636             // TrueType
0637             if (($font === null) && ($fileExtension != 'ttf')) {
0638                 $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
0639             }
0640 
0641             // Type 1 PostScript
0642 
0643             // Mac OS X dfont
0644 
0645             // others?
0646         }
0647 
0648 
0649         /* Done with the data source object.
0650          */
0651         $dataSource = null;
0652 
0653         if ($font !== null) {
0654             /* Parsing was successful. Add this font instance to the cache arrays
0655              * and return it for use.
0656              */
0657             $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
0658             Zend_Pdf_Font::$_fontNames[$fontName] = $font;
0659             $filePathKey = md5($filePath);
0660             Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
0661             return $font;
0662 
0663         } else {
0664             /* The type of font could not be determined. Give up.
0665              */
0666             // require_once 'Zend/Pdf/Exception.php';
0667             throw new Zend_Pdf_Exception("Cannot determine font type: $filePath",
0668                                          Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE);
0669          }
0670 
0671     }
0672 
0673 
0674 
0675   /**** Internal Methods ****/
0676 
0677 
0678   /* Font Extraction Methods */
0679 
0680     /**
0681      * Attempts to extract a TrueType font from the data source.
0682      *
0683      * If the font parser throws an exception that suggests the data source
0684      * simply doesn't contain a TrueType font, catches it and returns null. If
0685      * an exception is thrown that suggests the TrueType font is corrupt or
0686      * otherwise unusable, throws that exception. If successful, returns the
0687      * font object.
0688      *
0689      * @param Zend_Pdf_FileParserDataSource $dataSource
0690      * @param integer $embeddingOptions Options for font embedding.
0691      * @return Zend_Pdf_Resource_Font_OpenType_TrueType May also return null if
0692      *   the data source does not appear to contain a TrueType font.
0693      * @throws Zend_Pdf_Exception
0694      */
0695     protected static function _extractTrueTypeFont($dataSource, $embeddingOptions)
0696     {
0697         try {
0698             // require_once 'Zend/Pdf/FileParser/Font/OpenType/TrueType.php';
0699             $fontParser = new Zend_Pdf_FileParser_Font_OpenType_TrueType($dataSource);
0700 
0701             $fontParser->parse();
0702             if ($fontParser->isAdobeLatinSubset) {
0703                 // require_once 'Zend/Pdf/Resource/Font/Simple/Parsed/TrueType.php';
0704                 $font = new Zend_Pdf_Resource_Font_Simple_Parsed_TrueType($fontParser, $embeddingOptions);
0705             } else {
0706                 // require_once 'Zend/Pdf/Resource/Font/CidFont/TrueType.php';
0707                 // require_once 'Zend/Pdf/Resource/Font/Type0.php';
0708                 /* Use Composite Type 0 font which supports Unicode character mapping */
0709                 $cidFont = new Zend_Pdf_Resource_Font_CidFont_TrueType($fontParser, $embeddingOptions);
0710                 $font    = new Zend_Pdf_Resource_Font_Type0($cidFont);
0711             }
0712         } catch (Zend_Pdf_Exception $e) {
0713             /* The following exception codes suggest that this isn't really a
0714              * TrueType font. If we caught such an exception, simply return
0715              * null. For all other cases, it probably is a TrueType font but has
0716              * a problem; throw the exception again.
0717              */
0718             $fontParser = null;
0719             // require_once 'Zend/Pdf/Exception.php';
0720             switch ($e->getCode()) {
0721                 case Zend_Pdf_Exception::WRONG_FONT_TYPE:    // break intentionally omitted
0722                 case Zend_Pdf_Exception::BAD_TABLE_COUNT:    // break intentionally omitted
0723                 case Zend_Pdf_Exception::BAD_MAGIC_NUMBER:
0724                     return null;
0725 
0726                 default:
0727                     throw new Zend_Pdf_Exception($e->getMessage(), $e->getCode(), $e);
0728             }
0729         }
0730         return $font;
0731     }
0732 }