Example #1
0
 /**
  * Returns a {@link Zend_Pdf_Resource_Font} object by file path.
  *
  * The result of this method is cached, preventing unnecessary duplication
  * of font objects. Repetitive calls for the font with the same path will
  * return the same object.
  *
  * The $embeddingOptions parameter allows you to set certain flags related
  * to font embedding. You may combine options by OR-ing them together. See
  * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
  * available options and their descriptions. Note that this value is only
  * used when creating a font for the first time. If a font with the same
  * name already exists, you will get that object and the options you specify
  * here will be ignored. This is because fonts are only embedded within the
  * PDF file once.
  *
  * If the file path supplied does not match the path of a previously
  * instantiated object or the font type cannot be determined, an exception
  * will be thrown.
  *
  * @param string $filePath Full path to the font file.
  * @param integer $embeddingOptions (optional) Options for font embedding.
  * @return Zend_Pdf_Resource_Font
  * @throws Zend_Pdf_Exception
  */
 public static function fontWithPath($filePath, $embeddingOptions = 0)
 {
     /* First check the cache. Don't duplicate font objects.
      */
     $filePathKey = md5($filePath);
     if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
         return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
     }
     /* Create a file parser data source object for this file. File path and
      * access permission checks are handled here.
      */
     $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
     /* Attempt to determine the type of font. We can't always trust file
      * extensions, but try that first since it's fastest.
      */
     $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
     /* If it turns out that the file is named improperly and we guess the
      * wrong type, we'll get null instead of a font object.
      */
     switch ($fileExtension) {
         case 'ttf':
             $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
             break;
         default:
             /* Unrecognized extension. Try to determine the type by actually
              * parsing it below.
              */
             $font = null;
             break;
     }
     if ($font === null) {
         /* There was no match for the file extension or the extension was
          * wrong. Attempt to detect the type of font by actually parsing it.
          * We'll do the checks in order of most likely format to try to
          * reduce the detection time.
          */
         // OpenType
         // TrueType
         if ($font === null && $fileExtension != 'ttf') {
             $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
         }
         // Type 1 PostScript
         // Mac OS X dfont
         // others?
     }
     /* Done with the data source object.
      */
     $dataSource = null;
     if ($font !== null) {
         /* Parsing was successful. Add this font instance to the cache arrays
          * and return it for use.
          */
         $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
         Zend_Pdf_Font::$_fontNames[$fontName] = $font;
         $filePathKey = md5($filePath);
         Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
         return $font;
     } else {
         /* The type of font could not be determined. Give up.
          */
         throw new Zend_Pdf_Exception("Cannot determine font type: {$filePath}", Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE);
     }
 }
Example #2
0
 public static function fontWithPath($filePath, $embeddingOptions = 0)
 {
     $filePathKey = md5($filePath);
     if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
         return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
     }
     $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
     $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
     switch ($fileExtension) {
         case 'ttf':
             $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
             break;
         default:
             $font = null;
             break;
     }
     if ($font === null) {
         if ($font === null && $fileExtension != 'ttf') {
             $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
         }
     }
     $dataSource = null;
     if ($font !== null) {
         $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
         Zend_Pdf_Font::$_fontNames[$fontName] = $font;
         $filePathKey = md5($filePath);
         Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
         return $font;
     } else {
         throw new Zend_Pdf_Exception("Cannot determine font type: {$filePath}", Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE);
     }
 }