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

/Pdf/Resource/Font/Simple.php

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