PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Pdf/Resource/Font/Type0.php

https://gitlab.com/devtoannh/cafe
PHP | 257 lines | 84 code | 27 blank | 146 comment | 0 complexity | 1a8817aa321b65dc866f26b179331f2d 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. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Fonts
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Type0.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /** Internally used classes */
  23. require_once 'Zend/Pdf/Element/Array.php';
  24. require_once 'Zend/Pdf/Element/Name.php';
  25. /** Zend_Pdf_Resource_Font */
  26. require_once 'Zend/Pdf/Resource/Font.php';
  27. /**
  28. * Adobe PDF composite fonts implementation
  29. *
  30. * A composite font is one whose glyphs are obtained from other fonts or from fontlike
  31. * objects called CIDFonts ({@link Zend_Pdf_Resource_Font_CidFont}), organized hierarchically.
  32. * In PDF, a composite font is represented by a font dictionary whose Subtype value is Type0;
  33. * this is also called a Type 0 font (the Type 0 font at the top level of the hierarchy is the
  34. * root font).
  35. *
  36. * In PDF, a Type 0 font is a CID-keyed font.
  37. *
  38. * CID-keyed fonts provide effective method to operate with multi-byte character encodings.
  39. *
  40. * The CID-keyed font architecture specifies the external representation of certain font programs,
  41. * called CMap and CIDFont files, along with some conventions for combining and using those files.
  42. *
  43. * A CID-keyed font is the combination of a CMap with one or more CIDFonts, simple fonts,
  44. * or composite fonts containing glyph descriptions.
  45. *
  46. * The term 'CID-keyed font' reflects the fact that CID (character identifier) numbers
  47. * are used to index and access the glyph descriptions in the font.
  48. *
  49. *
  50. * Font objects should be normally be obtained from the factory methods
  51. * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
  52. *
  53. * @package Zend_Pdf
  54. * @subpackage Fonts
  55. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  56. * @license http://framework.zend.com/license/new-bsd New BSD License
  57. */
  58. class Zend_Pdf_Resource_Font_Type0 extends Zend_Pdf_Resource_Font
  59. {
  60. /**
  61. * Descendant CIDFont
  62. *
  63. * @var Zend_Pdf_Resource_Font_CidFont
  64. */
  65. private $_descendantFont;
  66. /**
  67. * Generate ToUnicode character map data
  68. *
  69. * @return string
  70. */
  71. static private function getToUnicodeCMapData()
  72. {
  73. return '/CIDInit /ProcSet findresource begin ' . "\n"
  74. . '12 dict begin ' . "\n"
  75. . 'begincmap ' . "\n"
  76. . '/CIDSystemInfo ' . "\n"
  77. . '<</Registry (Adobe) ' . "\n"
  78. . '/Ordering (UCS) ' . "\n"
  79. . '/Supplement 0' . "\n"
  80. . '>> def' . "\n"
  81. . '/CMapName /Adobe-Identity-UCS def ' . "\n"
  82. . '/CMapType 2 def ' . "\n"
  83. . '1 begincodespacerange' . "\n"
  84. . '<0000> <FFFF> ' . "\n"
  85. . 'endcodespacerange ' . "\n"
  86. . '1 beginbfrange ' . "\n"
  87. . '<0000> <FFFF> <0000> ' . "\n"
  88. . 'endbfrange ' . "\n"
  89. . 'endcmap ' . "\n"
  90. . 'CMapName currentdict /CMap defineresource pop ' . "\n"
  91. . 'end '
  92. . 'end ';
  93. }
  94. /**
  95. * Object constructor
  96. *
  97. */
  98. public function __construct(Zend_Pdf_Resource_Font_CidFont $descendantFont)
  99. {
  100. parent::__construct();
  101. $this->_objectFactory->attach($descendantFont->getFactory());
  102. $this->_fontType = Zend_Pdf_Font::TYPE_TYPE_0;
  103. $this->_descendantFont = $descendantFont;
  104. $this->_fontNames = $descendantFont->getFontNames();
  105. $this->_isBold = $descendantFont->isBold();
  106. $this->_isItalic = $descendantFont->isItalic();
  107. $this->_isMonospaced = $descendantFont->isMonospace();
  108. $this->_underlinePosition = $descendantFont->getUnderlinePosition();
  109. $this->_underlineThickness = $descendantFont->getUnderlineThickness();
  110. $this->_strikePosition = $descendantFont->getStrikePosition();
  111. $this->_strikeThickness = $descendantFont->getStrikeThickness();
  112. $this->_unitsPerEm = $descendantFont->getUnitsPerEm();
  113. $this->_ascent = $descendantFont->getAscent();
  114. $this->_descent = $descendantFont->getDescent();
  115. $this->_lineGap = $descendantFont->getLineGap();
  116. $this->_resource->Subtype = new Zend_Pdf_Element_Name('Type0');
  117. $this->_resource->BaseFont = new Zend_Pdf_Element_Name($descendantFont->getResource()->BaseFont->value);
  118. $this->_resource->DescendantFonts = new Zend_Pdf_Element_Array(array( $descendantFont->getResource() ));
  119. $this->_resource->Encoding = new Zend_Pdf_Element_Name('Identity-H');
  120. $toUnicode = $this->_objectFactory->newStreamObject(self::getToUnicodeCMapData());
  121. $this->_resource->ToUnicode = $toUnicode;
  122. }
  123. /**
  124. * Returns an array of glyph numbers corresponding to the Unicode characters.
  125. *
  126. * Zend_Pdf uses 'Identity-H' encoding for Type 0 fonts.
  127. * So we don't need to perform any conversion
  128. *
  129. * See also {@link glyphNumberForCharacter()}.
  130. *
  131. * @param array $characterCodes Array of Unicode character codes (code points).
  132. * @return array Array of glyph numbers.
  133. */
  134. public function glyphNumbersForCharacters($characterCodes)
  135. {
  136. return $characterCodes;
  137. }
  138. /**
  139. * Returns the glyph number corresponding to the Unicode character.
  140. *
  141. * Zend_Pdf uses 'Identity-H' encoding for Type 0 fonts.
  142. * So we don't need to perform any conversion
  143. *
  144. * @param integer $characterCode Unicode character code (code point).
  145. * @return integer Glyph number.
  146. */
  147. public function glyphNumberForCharacter($characterCode)
  148. {
  149. return $characterCode;
  150. }
  151. /**
  152. * Returns a number between 0 and 1 inclusive that indicates the percentage
  153. * of characters in the string which are covered by glyphs in this font.
  154. *
  155. * Since no one font will contain glyphs for the entire Unicode character
  156. * range, this method can be used to help locate a suitable font when the
  157. * actual contents of the string are not known.
  158. *
  159. * Note that some fonts lie about the characters they support. Additionally,
  160. * fonts don't usually contain glyphs for control characters such as tabs
  161. * and line breaks, so it is rare that you will get back a full 1.0 score.
  162. * The resulting value should be considered informational only.
  163. *
  164. * @param string $string
  165. * @param string $charEncoding (optional) Character encoding of source text.
  166. * If omitted, uses 'current locale'.
  167. * @return float
  168. */
  169. public function getCoveredPercentage($string, $charEncoding = '')
  170. {
  171. return $this->_descendantFont->getCoveredPercentage($string, $charEncoding);
  172. }
  173. /**
  174. * Returns the widths of the glyphs.
  175. *
  176. * The widths are expressed in the font's glyph space. You are responsible
  177. * for converting to user space as necessary. See {@link unitsPerEm()}.
  178. *
  179. * Throws an exception if the glyph number is out of range.
  180. *
  181. * See also {@link widthForGlyph()}.
  182. *
  183. * @param array &$glyphNumbers Array of glyph numbers.
  184. * @return array Array of glyph widths (integers).
  185. * @throws Zend_Pdf_Exception
  186. */
  187. public function widthsForGlyphs($glyphNumbers)
  188. {
  189. return $this->_descendantFont->widthsForChars($glyphNumbers);
  190. }
  191. /**
  192. * Returns the width of the glyph.
  193. *
  194. * Like {@link widthsForGlyphs()} but used for one glyph at a time.
  195. *
  196. * @param integer $glyphNumber
  197. * @return integer
  198. * @throws Zend_Pdf_Exception
  199. */
  200. public function widthForGlyph($glyphNumber)
  201. {
  202. return $this->_descendantFont->widthForChar($glyphNumber);
  203. }
  204. /**
  205. * Convert string to the font encoding.
  206. *
  207. * The method is used to prepare string for text drawing operators
  208. *
  209. * @param string $string
  210. * @param string $charEncoding Character encoding of source text.
  211. * @return string
  212. */
  213. public function encodeString($string, $charEncoding)
  214. {
  215. return iconv($charEncoding, 'UTF-16BE', $string);
  216. }
  217. /**
  218. * Convert string from the font encoding.
  219. *
  220. * The method is used to convert strings retrieved from existing content streams
  221. *
  222. * @param string $string
  223. * @param string $charEncoding Character encoding of resulting text.
  224. * @return string
  225. */
  226. public function decodeString($string, $charEncoding)
  227. {
  228. return iconv('UTF-16BE', $charEncoding, $string);
  229. }
  230. }