Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Font.php
Go to the documentation of this file.
1 <?php
39 abstract class Zend_Pdf_Font
40 {
41  /**** Class Constants ****/
42 
43 
44  /* Font Types */
45 
49  const TYPE_UNKNOWN = 0;
50 
54  const TYPE_STANDARD = 1;
55 
59  const TYPE_TYPE_1 = 2;
60 
64  const TYPE_TRUETYPE = 3;
65 
69  const TYPE_TYPE_0 = 4;
70 
76 
82 
83 
84  /* Names of the Standard 14 PDF Fonts */
85 
89  const FONT_COURIER = 'Courier';
90 
94  const FONT_COURIER_BOLD = 'Courier-Bold';
95 
99  const FONT_COURIER_OBLIQUE = 'Courier-Oblique';
100 
105  const FONT_COURIER_ITALIC = 'Courier-Oblique';
106 
110  const FONT_COURIER_BOLD_OBLIQUE = 'Courier-BoldOblique';
111 
116  const FONT_COURIER_BOLD_ITALIC = 'Courier-BoldOblique';
117 
121  const FONT_HELVETICA = 'Helvetica';
122 
126  const FONT_HELVETICA_BOLD = 'Helvetica-Bold';
127 
131  const FONT_HELVETICA_OBLIQUE = 'Helvetica-Oblique';
132 
137  const FONT_HELVETICA_ITALIC = 'Helvetica-Oblique';
138 
142  const FONT_HELVETICA_BOLD_OBLIQUE = 'Helvetica-BoldOblique';
143 
148  const FONT_HELVETICA_BOLD_ITALIC = 'Helvetica-BoldOblique';
149 
153  const FONT_SYMBOL = 'Symbol';
154 
158  const FONT_TIMES_ROMAN = 'Times-Roman';
159 
164  const FONT_TIMES = 'Times-Roman';
165 
169  const FONT_TIMES_BOLD = 'Times-Bold';
170 
174  const FONT_TIMES_ITALIC = 'Times-Italic';
175 
179  const FONT_TIMES_BOLD_ITALIC = 'Times-BoldItalic';
180 
184  const FONT_ZAPFDINGBATS = 'ZapfDingbats';
185 
186 
187  /* Font Name String Types */
188 
192  const NAME_COPYRIGHT = 0;
193 
197  const NAME_FAMILY = 1;
198 
202  const NAME_STYLE = 2;
203 
207  const NAME_ID = 3;
208 
213  const NAME_FULL = 4;
214 
218  const NAME_VERSION = 5;
219 
224  const NAME_POSTSCRIPT = 6;
225 
229  const NAME_TRADEMARK = 7;
230 
234  const NAME_MANUFACTURER = 8;
235 
239  const NAME_DESIGNER = 9;
240 
245  const NAME_DESCRIPTION = 10;
246 
251  const NAME_VENDOR_URL = 11;
252 
256  const NAME_DESIGNER_URL = 12;
257 
261  const NAME_LICENSE = 13;
262 
266  const NAME_LICENSE_URL = 14;
267 
274 
279 
283  const NAME_SAMPLE_TEXT = 19;
284 
288  const NAME_CID_NAME = 20;
289 
290 
291  /* Font Weights */
292 
296  const WEIGHT_THIN = 100;
297 
301  const WEIGHT_EXTRA_LIGHT = 200;
302 
306  const WEIGHT_LIGHT = 300;
307 
311  const WEIGHT_NORMAL = 400;
312 
316  const WEIGHT_MEDIUM = 500;
317 
321  const WEIGHT_SEMI_BOLD = 600;
322 
326  const WEIGHT_BOLD = 700;
327 
331  const WEIGHT_EXTRA_BOLD = 800;
332 
336  const WEIGHT_BLACK = 900;
337 
338 
339  /* Font Widths */
340 
345 
350 
354  const WIDTH_CONDENSED = 3;
355 
360 
364  const WIDTH_NORMAL = 5;
365 
370 
374  const WIDTH_EXPANDED = 7;
375 
380 
385 
386 
387  /* Font Embedding Options */
388 
392  const EMBED_DONT_EMBED = 0x01;
393 
397  const EMBED_DONT_SUBSET = 0x02;
398 
402  const EMBED_DONT_COMPRESS = 0x04;
403 
409 
410 
411 
412  /**** Class Variables ****/
413 
414 
420  private static $_fontNames = array();
421 
427  private static $_fontFilePaths = array();
428 
429 
430 
431  /**** Public Interface ****/
432 
433 
434  /* Factory Methods */
435 
463  public static function fontWithName($name, $embeddingOptions = 0)
464  {
465  /* First check the cache. Don't duplicate font objects.
466  */
467  if (isset(Zend_Pdf_Font::$_fontNames[$name])) {
468  return Zend_Pdf_Font::$_fontNames[$name];
469  }
470 
479  /* Not an existing font and no mapping in the config file. Check to see
480  * if this is one of the standard 14 PDF fonts.
481  */
482  switch ($name) {
484  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Courier.php';
486  break;
487 
489  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBold.php';
491  break;
492 
494  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierOblique.php';
496  break;
497 
499  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBoldOblique.php';
501  break;
502 
504  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Helvetica.php';
506  break;
507 
509  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBold.php';
511  break;
512 
514  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaOblique.php';
516  break;
517 
519  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBoldOblique.php';
521  break;
522 
524  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Symbol.php';
526  break;
527 
529  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesRoman.php';
531  break;
532 
534  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBold.php';
536  break;
537 
539  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesItalic.php';
541  break;
542 
544  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBoldItalic.php';
546  break;
547 
549  #require_once 'Zend/Pdf/Resource/Font/Simple/Standard/ZapfDingbats.php';
551  break;
552 
553  default:
554  #require_once 'Zend/Pdf/Exception.php';
555  throw new Zend_Pdf_Exception("Unknown font name: $name",
557  }
558 
559  /* Add this new font to the cache array and return it for use.
560  */
561  Zend_Pdf_Font::$_fontNames[$name] = $font;
562  return $font;
563  }
564 
590  public static function fontWithPath($filePath, $embeddingOptions = 0)
591  {
592  /* First check the cache. Don't duplicate font objects.
593  */
594  $filePathKey = md5($filePath);
595  if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
596  return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
597  }
598 
599  /* Create a file parser data source object for this file. File path and
600  * access permission checks are handled here.
601  */
602  #require_once 'Zend/Pdf/FileParserDataSource/File.php';
603  $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
604 
605  /* Attempt to determine the type of font. We can't always trust file
606  * extensions, but try that first since it's fastest.
607  */
608  $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
609 
610  /* If it turns out that the file is named improperly and we guess the
611  * wrong type, we'll get null instead of a font object.
612  */
613  switch ($fileExtension) {
614  case 'ttf':
615  $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
616  break;
617 
618  default:
619  /* Unrecognized extension. Try to determine the type by actually
620  * parsing it below.
621  */
622  $font = null;
623  break;
624  }
625 
626 
627  if ($font === null) {
628  /* There was no match for the file extension or the extension was
629  * wrong. Attempt to detect the type of font by actually parsing it.
630  * We'll do the checks in order of most likely format to try to
631  * reduce the detection time.
632  */
633 
634  // OpenType
635 
636  // TrueType
637  if (($font === null) && ($fileExtension != 'ttf')) {
638  $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
639  }
640 
641  // Type 1 PostScript
642 
643  // Mac OS X dfont
644 
645  // others?
646  }
647 
648 
649  /* Done with the data source object.
650  */
651  $dataSource = null;
652 
653  if ($font !== null) {
654  /* Parsing was successful. Add this font instance to the cache arrays
655  * and return it for use.
656  */
657  $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
658  Zend_Pdf_Font::$_fontNames[$fontName] = $font;
659  $filePathKey = md5($filePath);
660  Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
661  return $font;
662 
663  } else {
664  /* The type of font could not be determined. Give up.
665  */
666  #require_once 'Zend/Pdf/Exception.php';
667  throw new Zend_Pdf_Exception("Cannot determine font type: $filePath",
669  }
670 
671  }
672 
673 
674 
675  /**** Internal Methods ****/
676 
677 
678  /* Font Extraction Methods */
679 
695  protected static function _extractTrueTypeFont($dataSource, $embeddingOptions)
696  {
697  try {
698  #require_once 'Zend/Pdf/FileParser/Font/OpenType/TrueType.php';
699  $fontParser = new Zend_Pdf_FileParser_Font_OpenType_TrueType($dataSource);
700 
701  $fontParser->parse();
702  if ($fontParser->isAdobeLatinSubset) {
703  #require_once 'Zend/Pdf/Resource/Font/Simple/Parsed/TrueType.php';
704  $font = new Zend_Pdf_Resource_Font_Simple_Parsed_TrueType($fontParser, $embeddingOptions);
705  } else {
706  #require_once 'Zend/Pdf/Resource/Font/CidFont/TrueType.php';
707  #require_once 'Zend/Pdf/Resource/Font/Type0.php';
708  /* Use Composite Type 0 font which supports Unicode character mapping */
709  $cidFont = new Zend_Pdf_Resource_Font_CidFont_TrueType($fontParser, $embeddingOptions);
710  $font = new Zend_Pdf_Resource_Font_Type0($cidFont);
711  }
712  } catch (Zend_Pdf_Exception $e) {
713  /* The following exception codes suggest that this isn't really a
714  * TrueType font. If we caught such an exception, simply return
715  * null. For all other cases, it probably is a TrueType font but has
716  * a problem; throw the exception again.
717  */
718  $fontParser = null;
719  #require_once 'Zend/Pdf/Exception.php';
720  switch ($e->getCode()) {
721  case Zend_Pdf_Exception::WRONG_FONT_TYPE: // break intentionally omitted
722  case Zend_Pdf_Exception::BAD_TABLE_COUNT: // break intentionally omitted
724  return null;
725 
726  default:
727  throw new Zend_Pdf_Exception($e->getMessage(), $e->getCode(), $e);
728  }
729  }
730  return $font;
731  }
732 }
const WIDTH_EXTRA_EXPANDED
Definition: Font.php:379
const WIDTH_SEMI_EXPANDED
Definition: Font.php:369
static _extractTrueTypeFont($dataSource, $embeddingOptions)
Definition: Font.php:695
const WIDTH_CONDENSED
Definition: Font.php:354
const WIDTH_ULTRA_EXPANDED
Definition: Font.php:384
const WIDTH_EXPANDED
Definition: Font.php:374
const FONT_TIMES_ROMAN
Definition: Font.php:158
const FONT_HELVETICA_ITALIC
Definition: Font.php:137
const WIDTH_NORMAL
Definition: Font.php:364
const WEIGHT_BLACK
Definition: Font.php:336
const FONT_COURIER_BOLD_OBLIQUE
Definition: Font.php:110
const FONT_COURIER_BOLD
Definition: Font.php:94
const WEIGHT_THIN
Definition: Font.php:296
const NAME_DESIGNER
Definition: Font.php:239
const TYPE_UNKNOWN
Definition: Font.php:49
const NAME_LICENSE
Definition: Font.php:261
const FONT_HELVETICA_OBLIQUE
Definition: Font.php:131
const NAME_FULL
Definition: Font.php:213
const NAME_VENDOR_URL
Definition: Font.php:251
const NAME_MANUFACTURER
Definition: Font.php:234
const FONT_TIMES_ITALIC
Definition: Font.php:174
const NAME_TRADEMARK
Definition: Font.php:229
const WIDTH_SEMI_CONDENSED
Definition: Font.php:359
const NAME_VERSION
Definition: Font.php:218
const NAME_DESIGNER_URL
Definition: Font.php:256
const FONT_TIMES
Definition: Font.php:164
const NAME_PREFERRED_STYLE
Definition: Font.php:278
const TYPE_TYPE_0
Definition: Font.php:69
const WIDTH_ULTRA_CONDENSED
Definition: Font.php:344
const FONT_HELVETICA_BOLD_OBLIQUE
Definition: Font.php:142
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590
const WEIGHT_EXTRA_LIGHT
Definition: Font.php:301
const WEIGHT_BOLD
Definition: Font.php:326
const FONT_COURIER_OBLIQUE
Definition: Font.php:99
const NAME_COPYRIGHT
Definition: Font.php:192
const NAME_STYLE
Definition: Font.php:202
const WIDTH_EXTRA_CONDENSED
Definition: Font.php:349
const FONT_SYMBOL
Definition: Font.php:153
const FONT_HELVETICA
Definition: Font.php:121
const WEIGHT_SEMI_BOLD
Definition: Font.php:321
const FONT_TIMES_BOLD_ITALIC
Definition: Font.php:179
const NAME_LICENSE_URL
Definition: Font.php:266
const EMBED_DONT_SUBSET
Definition: Font.php:397
const FONT_HELVETICA_BOLD_ITALIC
Definition: Font.php:148
const NAME_POSTSCRIPT
Definition: Font.php:224
const FONT_COURIER_BOLD_ITALIC
Definition: Font.php:116
const WEIGHT_EXTRA_BOLD
Definition: Font.php:331
const TYPE_CIDFONT_TYPE_0
Definition: Font.php:75
const TYPE_TRUETYPE
Definition: Font.php:64
const EMBED_DONT_EMBED
Definition: Font.php:392
const CANT_DETERMINE_FONT_TYPE
Definition: Exception.php:323
const FONT_ZAPFDINGBATS
Definition: Font.php:184
const NAME_DESCRIPTION
Definition: Font.php:245
const WEIGHT_MEDIUM
Definition: Font.php:316
const FONT_COURIER_ITALIC
Definition: Font.php:105
const WEIGHT_LIGHT
Definition: Font.php:306
const EMBED_DONT_COMPRESS
Definition: Font.php:402
static fontWithName($name, $embeddingOptions=0)
Definition: Font.php:463
const TYPE_CIDFONT_TYPE_2
Definition: Font.php:81
const TYPE_STANDARD
Definition: Font.php:54
const NAME_CID_NAME
Definition: Font.php:288
const TYPE_TYPE_1
Definition: Font.php:59
const NAME_ID
Definition: Font.php:207
const NAME_SAMPLE_TEXT
Definition: Font.php:283
const WEIGHT_NORMAL
Definition: Font.php:311
const FONT_HELVETICA_BOLD
Definition: Font.php:126
const NAME_PREFERRED_FAMILY
Definition: Font.php:273
const NAME_FAMILY
Definition: Font.php:197
const FONT_COURIER
Definition: Font.php:89
const FONT_TIMES_BOLD
Definition: Font.php:169
const EMBED_SUPPRESS_EMBED_EXCEPTION
Definition: Font.php:408
if(!isset($_GET['name'])) $name
Definition: log.php:14