Extracted.php 10.0 KB

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