PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/webroot/frameworks/Zend/Pdf/Resource/Font/Extracted.php

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