PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 275 lines | 69 code | 23 blank | 183 comment | 9 complexity | f406f8fec3a59fd798e43b03bc49a8c3 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. * Adobe PDF Simple fonts implementation
  26. *
  27. * PDF simple fonts functionality is presented by Adobe Type 1
  28. * (including standard PDF Type1 built-in fonts) and TrueType fonts support.
  29. *
  30. * Both fonts have the following properties:
  31. * - Glyphs in the font are selected by single-byte character codes obtained from a
  32. * string that is shown by the text-showing operators. Logically, these codes index
  33. * into a table of 256 glyphs; the mapping from codes to glyphs is called the font’s
  34. * encoding.
  35. * PDF specification provides a possibility to specify any user defined encoding in addition
  36. * to the standard built-in encodings: Standard-Encoding, MacRomanEncoding, WinAnsiEncoding,
  37. * and PDFDocEncoding, but Zend_Pdf simple fonts implementation operates only with
  38. * Windows ANSI encoding (except Symbol and ZapfDingbats built-in fonts).
  39. *
  40. * - Each glyph has a single set of metrics, including a horizontal displacement or
  41. * width. That is, simple fonts support only horizontal writing mode.
  42. *
  43. *
  44. * The code in this class is common to both types. However, you will only deal
  45. * directly with subclasses.
  46. *
  47. * Font objects should be normally be obtained from the factory methods
  48. * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
  49. *
  50. * @package Zend_Pdf
  51. * @subpackage Fonts
  52. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  53. * @license http://framework.zend.com/license/new-bsd New BSD License
  54. */
  55. abstract class Zend_Pdf_Resource_Font_Simple extends Zend_Pdf_Resource_Font
  56. {
  57. /**
  58. * Object representing the font's cmap (character to glyph map).
  59. * @var Zend_Pdf_Cmap
  60. */
  61. protected $_cmap = null;
  62. /**
  63. * Array containing the widths of each of the glyphs contained in the font.
  64. *
  65. * Keys are integers starting from 0, which coresponds to Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH.
  66. *
  67. * Font character map may contain gaps for actually used glyphs, nevertheless glyphWidths array
  68. * contains widths for all glyphs even they are unused.
  69. *
  70. * @var array
  71. */
  72. protected $_glyphWidths = null;
  73. /**
  74. * Width for glyphs missed in the font
  75. *
  76. * Note: Adobe PDF specfication (V1.4 - V1.6) doesn't define behavior for rendering
  77. * characters missed in the standard PDF fonts (such us 0x7F (DEL) Windows ANSI code)
  78. * Adobe Font Metrics files doesn't also define metrics for "missed glyph".
  79. * We provide character width as "0" for this case, but actually it depends on PDF viewer
  80. * implementation.
  81. *
  82. * @var integer
  83. */
  84. protected $_missingGlyphWidth = 0;
  85. /**** Public Interface ****/
  86. /* Object Lifecycle */
  87. /**
  88. * Object constructor
  89. *
  90. */
  91. public function __construct()
  92. {
  93. parent::__construct();
  94. /**
  95. * @todo
  96. * It's easy to add other encodings support now (Standard-Encoding, MacRomanEncoding,
  97. * PDFDocEncoding, MacExpertEncoding, Symbol, and ZapfDingbats).
  98. * Steps for the implementation:
  99. * - completely describe all PDF single byte encodings in the documentation
  100. * - implement non-WinAnsi encodings processing into encodeString()/decodeString() methods
  101. *
  102. * These encodings will be automatically supported for standard builtin PDF fonts as well
  103. * as for external fonts.
  104. */
  105. $this->_resource->Encoding = new Zend_Pdf_Element_Name('WinAnsiEncoding');
  106. }
  107. /**
  108. * Returns an array of glyph numbers corresponding to the Unicode characters.
  109. *
  110. * If a particular character doesn't exist in this font, the special 'missing
  111. * character glyph' will be substituted.
  112. *
  113. * See also {@link glyphNumberForCharacter()}.
  114. *
  115. * @param array $characterCodes Array of Unicode character codes (code points).
  116. * @return array Array of glyph numbers.
  117. */
  118. public function glyphNumbersForCharacters($characterCodes)
  119. {
  120. return $this->_cmap->glyphNumbersForCharacters($characterCodes);
  121. }
  122. /**
  123. * Returns the glyph number corresponding to the Unicode character.
  124. *
  125. * If a particular character doesn't exist in this font, the special 'missing
  126. * character glyph' will be substituted.
  127. *
  128. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  129. * operations.
  130. *
  131. * @param integer $characterCode Unicode character code (code point).
  132. * @return integer Glyph number.
  133. */
  134. public function glyphNumberForCharacter($characterCode)
  135. {
  136. return $this->_cmap->glyphNumberForCharacter($characterCode);
  137. }
  138. /**
  139. * Returns a number between 0 and 1 inclusive that indicates the percentage
  140. * of characters in the string which are covered by glyphs in this font.
  141. *
  142. * Since no one font will contain glyphs for the entire Unicode character
  143. * range, this method can be used to help locate a suitable font when the
  144. * actual contents of the string are not known.
  145. *
  146. * Note that some fonts lie about the characters they support. Additionally,
  147. * fonts don't usually contain glyphs for control characters such as tabs
  148. * and line breaks, so it is rare that you will get back a full 1.0 score.
  149. * The resulting value should be considered informational only.
  150. *
  151. * @param string $string
  152. * @param string $charEncoding (optional) Character encoding of source text.
  153. * If omitted, uses 'current locale'.
  154. * @return float
  155. */
  156. public function getCoveredPercentage($string, $charEncoding = '')
  157. {
  158. /* Convert the string to UTF-16BE encoding so we can match the string's
  159. * character codes to those found in the cmap.
  160. */
  161. if ($charEncoding != 'UTF-16BE') {
  162. $string = iconv($charEncoding, 'UTF-16BE', $string);
  163. }
  164. $charCount = iconv_strlen($string, 'UTF-16BE');
  165. if ($charCount == 0) {
  166. return 0;
  167. }
  168. /* Fetch the covered character code list from the font's cmap.
  169. */
  170. $coveredCharacters = $this->_cmap->getCoveredCharacters();
  171. /* Calculate the score by doing a lookup for each character.
  172. */
  173. $score = 0;
  174. $maxIndex = strlen($string);
  175. for ($i = 0; $i < $maxIndex; $i++) {
  176. /**
  177. * @todo Properly handle characters encoded as surrogate pairs.
  178. */
  179. $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
  180. /* This could probably be optimized a bit with a binary search...
  181. */
  182. if (in_array($charCode, $coveredCharacters)) {
  183. $score++;
  184. }
  185. }
  186. return $score / $charCount;
  187. }
  188. /**
  189. * Returns the widths of the glyphs.
  190. *
  191. * The widths are expressed in the font's glyph space. You are responsible
  192. * for converting to user space as necessary. See {@link unitsPerEm()}.
  193. *
  194. * See also {@link widthForGlyph()}.
  195. *
  196. * @param array &$glyphNumbers Array of glyph numbers.
  197. * @return array Array of glyph widths (integers).
  198. */
  199. public function widthsForGlyphs($glyphNumbers)
  200. {
  201. $widths = array();
  202. foreach ($glyphNumbers as $key => $glyphNumber) {
  203. if (!isset($this->_glyphWidths[$glyphNumber])) {
  204. $widths[$key] = $this->_missingGlyphWidth;
  205. } else {
  206. $widths[$key] = $this->_glyphWidths[$glyphNumber];
  207. }
  208. }
  209. return $widths;
  210. }
  211. /**
  212. * Returns the width of the glyph.
  213. *
  214. * Like {@link widthsForGlyphs()} but used for one glyph at a time.
  215. *
  216. * @param integer $glyphNumber
  217. * @return integer
  218. */
  219. public function widthForGlyph($glyphNumber)
  220. {
  221. if (!isset($this->_glyphWidths[$glyphNumber])) {
  222. return $this->_missingGlyphWidth;
  223. }
  224. return $this->_glyphWidths[$glyphNumber];
  225. }
  226. /**
  227. * Convert string to the font encoding.
  228. *
  229. * The method is used to prepare string for text drawing operators
  230. *
  231. * @param string $string
  232. * @param string $charEncoding Character encoding of source text.
  233. * @return string
  234. */
  235. public function encodeString($string, $charEncoding)
  236. {
  237. return iconv($charEncoding, 'CP1252//IGNORE', $string);
  238. }
  239. /**
  240. * Convert string from the font encoding.
  241. *
  242. * The method is used to convert strings retrieved from existing content streams
  243. *
  244. * @param string $string
  245. * @param string $charEncoding Character encoding of resulting text.
  246. * @return string
  247. */
  248. public function decodeString($string, $charEncoding)
  249. {
  250. return iconv('CP1252', $charEncoding, $string);
  251. }
  252. }