You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
10 KiB

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