FontDescriptor.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Fonts
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Pdf_Font */
  23. require_once 'Zend/Pdf/Font.php';
  24. /** Zend_Pdf_Resource_Font */
  25. require_once 'Zend/Pdf/Resource/Font.php';
  26. /** Zend_Pdf_FileParser_Font_OpenType */
  27. require_once 'Zend/Pdf/FileParser/Font/OpenType.php';
  28. /**
  29. * FontDescriptor implementation
  30. *
  31. * A font descriptor specifies metrics and other attributes of a simple font or a
  32. * CIDFont as a whole, as distinct from the metrics of individual glyphs. These font
  33. * metrics provide information that enables a viewer application to synthesize a
  34. * substitute font or select a similar font when the font program is unavailable. The
  35. * font descriptor may also be used to embed the font program in the PDF file.
  36. *
  37. * @package Zend_Pdf
  38. * @subpackage Fonts
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Pdf_Resource_Font_FontDescriptor
  43. {
  44. /**
  45. * Object constructor
  46. * @throws Zend_Pdf_Exception
  47. */
  48. public function __construct()
  49. {
  50. throw new Zend_Pdf_Exception('Zend_Pdf_Resource_Font_FontDescriptor is not intended to be instantiated');
  51. }
  52. /**
  53. * Object constructor
  54. *
  55. * The $embeddingOptions parameter allows you to set certain flags related
  56. * to font embedding. You may combine options by OR-ing them together. See
  57. * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
  58. * available options and their descriptions.
  59. *
  60. * Note that it is not requried that fonts be embedded within the PDF file
  61. * to use them. If the recipient of the PDF has the font installed on their
  62. * computer, they will see the correct fonts in the document. If they don't,
  63. * the PDF viewer will substitute or synthesize a replacement.
  64. *
  65. *
  66. * @param Zend_Pdf_Resource_Font $font Font
  67. * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object containing parsed TrueType file.
  68. * @param integer $embeddingOptions Options for font embedding.
  69. * @return Zend_Pdf_Element_Dictionary
  70. * @throws Zend_Pdf_Exception
  71. */
  72. static public function factory(Zend_Pdf_Resource_Font $font, Zend_Pdf_FileParser_Font_OpenType $fontParser, $embeddingOptions)
  73. {
  74. /* The font descriptor object contains the rest of the font metrics and
  75. * the information about the embedded font program (if applicible).
  76. */
  77. $fontDescriptor = new Zend_Pdf_Element_Dictionary();
  78. $fontDescriptor->Type = new Zend_Pdf_Element_Name('FontDescriptor');
  79. $fontDescriptor->FontName = new Zend_Pdf_Element_Name($font->getResource()->BaseFont->value);
  80. /* The font flags value is a bitfield that describes the stylistic
  81. * attributes of the font. We will set as many of the bits as can be
  82. * determined from the font parser.
  83. */
  84. $flags = 0;
  85. if ($fontParser->isMonospaced) { // bit 1: FixedPitch
  86. $flags |= 1 << 0;
  87. }
  88. if ($fontParser->isSerifFont) { // bit 2: Serif
  89. $flags |= 1 << 1;
  90. }
  91. if (! $fontParser->isAdobeLatinSubset) { // bit 3: Symbolic
  92. $flags |= 1 << 2;
  93. }
  94. if ($fontParser->isScriptFont) { // bit 4: Script
  95. $flags |= 1 << 3;
  96. }
  97. if ($fontParser->isAdobeLatinSubset) { // bit 6: Nonsymbolic
  98. $flags |= 1 << 5;
  99. }
  100. if ($fontParser->isItalic) { // bit 7: Italic
  101. $flags |= 1 << 6;
  102. }
  103. // bits 17-19: AllCap, SmallCap, ForceBold; not available
  104. $fontDescriptor->Flags = new Zend_Pdf_Element_Numeric($flags);
  105. $fontBBox = array(new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->xMin)),
  106. new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->yMin)),
  107. new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->xMax)),
  108. new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->yMax)));
  109. $fontDescriptor->FontBBox = new Zend_Pdf_Element_Array($fontBBox);
  110. $fontDescriptor->ItalicAngle = new Zend_Pdf_Element_Numeric($fontParser->italicAngle);
  111. $fontDescriptor->Ascent = new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->ascent));
  112. $fontDescriptor->Descent = new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->descent));
  113. $fontDescriptor->CapHeight = new Zend_Pdf_Element_Numeric($fontParser->capitalHeight);
  114. /**
  115. * The vertical stem width is not yet extracted from the OpenType font
  116. * file. For now, record zero which is interpreted as 'unknown'.
  117. * @todo Calculate value for StemV.
  118. */
  119. $fontDescriptor->StemV = new Zend_Pdf_Element_Numeric(0);
  120. $fontDescriptor->MissingWidth = new Zend_Pdf_Element_Numeric($fontParser->glyphWidths[0]);
  121. /* Set up font embedding. This is where the actual font program itself
  122. * is embedded within the PDF document.
  123. *
  124. * Note that it is not requried that fonts be embedded within the PDF
  125. * document to use them. If the recipient of the PDF has the font
  126. * installed on their computer, they will see the correct fonts in the
  127. * document. If they don't, the PDF viewer will substitute or synthesize
  128. * a replacement.
  129. *
  130. * There are several guidelines for font embedding:
  131. *
  132. * First, the developer might specifically request not to embed the font.
  133. */
  134. if (!($embeddingOptions & Zend_Pdf_Font::EMBED_DONT_EMBED)) {
  135. /* Second, the font author may have set copyright bits that prohibit
  136. * the font program from being embedded. Yes this is controversial,
  137. * but it's the rules:
  138. * http://partners.adobe.com/public/developer/en/acrobat/sdk/FontPolicies.pdf
  139. *
  140. * To keep the developer in the loop, and to prevent surprising bug
  141. * reports of "your PDF doesn't have the right fonts," throw an
  142. * exception if the font cannot be embedded.
  143. */
  144. if (! $fontParser->isEmbeddable) {
  145. /* This exception may be suppressed if the developer decides that
  146. * it's not a big deal that the font program can't be embedded.
  147. */
  148. if (!($embeddingOptions & Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION)) {
  149. $message = 'This font cannot be embedded in the PDF document. If you would like to use '
  150. . 'it anyway, you must pass Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION '
  151. . 'in the $options parameter of the font constructor.';
  152. throw new Zend_Pdf_Exception($message, Zend_Pdf_Exception::FONT_CANT_BE_EMBEDDED);
  153. }
  154. } else {
  155. /* Otherwise, the default behavior is to embed all custom fonts.
  156. */
  157. /* This section will change soon to a stream object data
  158. * provider model so that we don't have to keep a copy of the
  159. * entire font in memory.
  160. *
  161. * We also cannot build font subsetting until the data provider
  162. * model is in place.
  163. */
  164. $fontFile = $fontParser->getDataSource()->readAllBytes();
  165. $fontFileObject = $font->getFactory()->newStreamObject($fontFile);
  166. $fontFileObject->dictionary->Length1 = new Zend_Pdf_Element_Numeric(strlen($fontFile));
  167. if (!($embeddingOptions & Zend_Pdf_Font::EMBED_DONT_COMPRESS)) {
  168. /* Compress the font file using Flate. This generally cuts file
  169. * sizes by about half!
  170. */
  171. $fontFileObject->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
  172. }
  173. if ($fontParser instanceof Zend_Pdf_FileParser_Font_OpenType_Type1 /* not implemented now */) {
  174. $fontDescriptor->FontFile = $fontFileObject;
  175. } else if ($fontParser instanceof Zend_Pdf_FileParser_Font_OpenType_TrueType) {
  176. $fontDescriptor->FontFile2 = $fontFileObject;
  177. } else {
  178. $fontDescriptor->FontFile3 = $fontFileObject;
  179. }
  180. }
  181. }
  182. return $fontDescriptor;
  183. }
  184. }