Extracted.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_Resource_Font */
  23. require_once 'Zend/Pdf/Resource/Font.php';
  24. /** Zend_Pdf_Cmap */
  25. require_once 'Zend/Pdf/Cmap.php';
  26. /**
  27. * Extracted fonts implementation
  28. *
  29. * Thes class allows to extract fonts already mentioned within PDF document and use them
  30. * for text drawing.
  31. *
  32. * @package Zend_Pdf
  33. * @subpackage Fonts
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
  38. {
  39. /**
  40. * Extracted font encoding
  41. *
  42. * Only 'Identity-H' and 'WinAnsiEncoding' encodings are supported now
  43. *
  44. * @var string
  45. */
  46. protected $_encoding = null;
  47. /**
  48. * Object constructor
  49. *
  50. * $fontDictionary is a Zend_Pdf_Element_Reference or Zend_Pdf_Element_Object object
  51. *
  52. * @param mixed $fontDictionary
  53. * @throws Zend_Pdf_Exception
  54. */
  55. public function __construct($fontDictionary)
  56. {
  57. // Extract object factory and resource object from font dirctionary object
  58. $this->_objectFactory = $fontDictionary->getFactory();
  59. $this->_resource = $fontDictionary;
  60. if ($fontDictionary->Encoding !== null) {
  61. $this->_encoding = $fontDictionary->Encoding->value;
  62. }
  63. switch ($fontDictionary->Subtype->value) {
  64. case 'Type0':
  65. // Composite type 0 font
  66. if (count($fontDictionary->DescendantFonts->items) != 1) {
  67. // Multiple descendant fonts are not supported
  68. throw new Zend_Pdf_Exception('Unsupported font type.');
  69. }
  70. $fontDictionaryIterator = $fontDictionary->DescendantFonts->items->getIterator();
  71. $fontDictionaryIterator->rewind();
  72. $descendantFont = $fontDictionaryIterator->current();
  73. $fontDescriptor = $descendantFont->FontDescriptor;
  74. break;
  75. case 'Type1':
  76. if ($fontDictionary->FontDescriptor === null) {
  77. // That's one of the standard fonts
  78. $standardFont = Zend_Pdf_Font::fontWithName($fontDictionary->BaseFont->value);
  79. $this->_fontNames = $standardFont->getFontNames();
  80. $this->_isBold = $standardFont->isBold();
  81. $this->_isItalic = $standardFont->isItalic();
  82. $this->_isMonospace = $standardFont->isMonospace();
  83. $this->_underlinePosition = $standardFont->getUnderlinePosition();
  84. $this->_underlineThickness = $standardFont->getUnderlineThickness();
  85. $this->_strikePosition = $standardFont->getStrikePosition();
  86. $this->_strikeThickness = $standardFont->getStrikeThickness();
  87. $this->_unitsPerEm = $standardFont->getUnitsPerEm();
  88. $this->_ascent = $standardFont->getAscent();
  89. $this->_descent = $standardFont->getDescent();
  90. $this->_lineGap = $standardFont->getLineGap();
  91. return;
  92. }
  93. $fontDescriptor = $fontDictionary->FontDescriptor;
  94. break;
  95. case 'TrueType':
  96. $fontDescriptor = $fontDictionary->FontDescriptor;
  97. break;
  98. default:
  99. throw new Zend_Pdf_Exception('Unsupported font type.');
  100. }
  101. $this->_fontNames[Zend_Pdf_Font::NAME_POSTSCRIPT]['en'] = iconv('UTF-8', 'UTF-16BE', $fontDictionary->BaseFont->value);
  102. $this->_isBold = false; // this property is actually not used anywhere
  103. $this->_isItalic = ( ($fontDescriptor->Flags->value & (1 << 6)) != 0 ); // Bit-7 is set
  104. $this->_isMonospace = ( ($fontDescriptor->Flags->value & (1 << 0)) != 0 ); // Bit-1 is set
  105. $this->_underlinePosition = null; // Can't be extracted
  106. $this->_underlineThickness = null; // Can't be extracted
  107. $this->_strikePosition = null; // Can't be extracted
  108. $this->_strikeThickness = null; // Can't be extracted
  109. $this->_unitsPerEm = null; // Can't be extracted
  110. $this->_ascent = $fontDescriptor->Ascent->value;
  111. $this->_descent = $fontDescriptor->Descent->value;
  112. $this->_lineGap = null; // Can't be extracted
  113. }
  114. /**
  115. * Returns an array of glyph numbers corresponding to the Unicode characters.
  116. *
  117. * If a particular character doesn't exist in this font, the special 'missing
  118. * character glyph' will be substituted.
  119. *
  120. * See also {@link glyphNumberForCharacter()}.
  121. *
  122. * @param array $characterCodes Array of Unicode character codes (code points).
  123. * @return array Array of glyph numbers.
  124. */
  125. public function glyphNumbersForCharacters($characterCodes)
  126. {
  127. throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
  128. }
  129. /**
  130. * Returns the glyph number corresponding to the Unicode character.
  131. *
  132. * If a particular character doesn't exist in this font, the special 'missing
  133. * character glyph' will be substituted.
  134. *
  135. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  136. * operations.
  137. *
  138. * @param integer $characterCode Unicode character code (code point).
  139. * @return integer Glyph number.
  140. */
  141. public function glyphNumberForCharacter($characterCode)
  142. {
  143. throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
  144. }
  145. /**
  146. * Returns a number between 0 and 1 inclusive that indicates the percentage
  147. * of characters in the string which are covered by glyphs in this font.
  148. *
  149. * Since no one font will contain glyphs for the entire Unicode character
  150. * range, this method can be used to help locate a suitable font when the
  151. * actual contents of the string are not known.
  152. *
  153. * Note that some fonts lie about the characters they support. Additionally,
  154. * fonts don't usually contain glyphs for control characters such as tabs
  155. * and line breaks, so it is rare that you will get back a full 1.0 score.
  156. * The resulting value should be considered informational only.
  157. *
  158. * @param string $string
  159. * @param string $charEncoding (optional) Character encoding of source text.
  160. * If omitted, uses 'current locale'.
  161. * @return float
  162. */
  163. public function getCoveredPercentage($string, $charEncoding = '')
  164. {
  165. throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
  166. }
  167. /**
  168. * Returns the widths of the glyphs.
  169. *
  170. * The widths are expressed in the font's glyph space. You are responsible
  171. * for converting to user space as necessary. See {@link unitsPerEm()}.
  172. *
  173. * See also {@link widthForGlyph()}.
  174. *
  175. * @param array $glyphNumbers Array of glyph numbers.
  176. * @return array Array of glyph widths (integers).
  177. * @throws Zend_Pdf_Exception
  178. */
  179. public function widthsForGlyphs($glyphNumbers)
  180. {
  181. throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
  182. }
  183. /**
  184. * Returns the width of the glyph.
  185. *
  186. * Like {@link widthsForGlyphs()} but used for one glyph at a time.
  187. *
  188. * @param integer $glyphNumber
  189. * @return integer
  190. * @throws Zend_Pdf_Exception
  191. */
  192. public function widthForGlyph($glyphNumber)
  193. {
  194. throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
  195. }
  196. /**
  197. * Convert string to the font encoding.
  198. *
  199. * The method is used to prepare string for text drawing operators
  200. *
  201. * @param string $string
  202. * @param string $charEncoding Character encoding of source text.
  203. * @return string
  204. */
  205. public function encodeString($string, $charEncoding)
  206. {
  207. if ($this->_encoding == 'Identity-H') {
  208. return iconv($charEncoding, 'UTF-16BE', $string);
  209. }
  210. if ($this->_encoding == 'WinAnsiEncoding') {
  211. return iconv($charEncoding, 'CP1252//IGNORE', $string);
  212. }
  213. throw new Zend_Pdf_Exception('Fonf encoding is not supported');
  214. }
  215. /**
  216. * Convert string from the font encoding.
  217. *
  218. * The method is used to convert strings retrieved from existing content streams
  219. *
  220. * @param string $string
  221. * @param string $charEncoding Character encoding of resulting text.
  222. * @return string
  223. */
  224. public function decodeString($string, $charEncoding)
  225. {
  226. if ($this->_encoding == 'Identity-H') {
  227. return iconv('UTF-16BE', $charEncoding, $string);
  228. }
  229. if ($this->_encoding == 'WinAnsiEncoding') {
  230. return iconv('CP1252', $charEncoding, $string);
  231. }
  232. throw new Zend_Pdf_Exception('Fonf encoding is not supported');
  233. }
  234. }