PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mercysam/zfs
PHP | 281 lines | 74 code | 24 blank | 183 comment | 14 complexity | bcc882f4f97b8fcc98a79eba0b21cb3d 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. if (PHP_OS != 'AIX') { // AIX doesnt know what UTF-16BE is
  163. $string = iconv($charEncoding, 'UTF-16BE', $string);
  164. }
  165. }
  166. $charCount = (PHP_OS != 'AIX') ? iconv_strlen($string, 'UTF-16BE') : strlen($string);
  167. if ($charCount == 0) {
  168. return 0;
  169. }
  170. /* Fetch the covered character code list from the font's cmap.
  171. */
  172. $coveredCharacters = $this->_cmap->getCoveredCharacters();
  173. /* Calculate the score by doing a lookup for each character.
  174. */
  175. $score = 0;
  176. $maxIndex = strlen($string);
  177. for ($i = 0; $i < $maxIndex; $i++) {
  178. /**
  179. * @todo Properly handle characters encoded as surrogate pairs.
  180. */
  181. $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
  182. /* This could probably be optimized a bit with a binary search...
  183. */
  184. if (in_array($charCode, $coveredCharacters)) {
  185. $score++;
  186. }
  187. }
  188. return $score / $charCount;
  189. }
  190. /**
  191. * Returns the widths of the glyphs.
  192. *
  193. * The widths are expressed in the font's glyph space. You are responsible
  194. * for converting to user space as necessary. See {@link unitsPerEm()}.
  195. *
  196. * See also {@link widthForGlyph()}.
  197. *
  198. * @param array &$glyphNumbers Array of glyph numbers.
  199. * @return array Array of glyph widths (integers).
  200. */
  201. public function widthsForGlyphs($glyphNumbers)
  202. {
  203. $widths = array();
  204. foreach ($glyphNumbers as $key => $glyphNumber) {
  205. if (!isset($this->_glyphWidths[$glyphNumber])) {
  206. $widths[$key] = $this->_missingGlyphWidth;
  207. } else {
  208. $widths[$key] = $this->_glyphWidths[$glyphNumber];
  209. }
  210. }
  211. return $widths;
  212. }
  213. /**
  214. * Returns the width of the glyph.
  215. *
  216. * Like {@link widthsForGlyphs()} but used for one glyph at a time.
  217. *
  218. * @param integer $glyphNumber
  219. * @return integer
  220. */
  221. public function widthForGlyph($glyphNumber)
  222. {
  223. if (!isset($this->_glyphWidths[$glyphNumber])) {
  224. return $this->_missingGlyphWidth;
  225. }
  226. return $this->_glyphWidths[$glyphNumber];
  227. }
  228. /**
  229. * Convert string to the font encoding.
  230. *
  231. * The method is used to prepare string for text drawing operators
  232. *
  233. * @param string $string
  234. * @param string $charEncoding Character encoding of source text.
  235. * @return string
  236. */
  237. public function encodeString($string, $charEncoding)
  238. {
  239. if (PHP_OS == 'AIX') {
  240. return $string; // returning here b/c AIX doesnt know what CP1252 is
  241. }
  242. return iconv($charEncoding, 'CP1252//IGNORE', $string);
  243. }
  244. /**
  245. * Convert string from the font encoding.
  246. *
  247. * The method is used to convert strings retrieved from existing content streams
  248. *
  249. * @param string $string
  250. * @param string $charEncoding Character encoding of resulting text.
  251. * @return string
  252. */
  253. public function decodeString($string, $charEncoding)
  254. {
  255. return iconv('CP1252', $charEncoding, $string);
  256. }
  257. }