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

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

https://bitbucket.org/fabiancarlos/feature_seguimentos
PHP | 490 lines | 190 code | 54 blank | 246 comment | 32 complexity | 5f25591a66aa448c6963261da7c9fdb4 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: CidFont.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/Dictionary.php';
  25. require_once 'Zend/Pdf/Element/Name.php';
  26. require_once 'Zend/Pdf/Element/Numeric.php';
  27. require_once 'Zend/Pdf/Element/String.php';
  28. /** Zend_Pdf_Resource_Font */
  29. require_once 'Zend/Pdf/Resource/Font.php';
  30. /**
  31. * Adobe PDF CIDFont font object implementation
  32. *
  33. * A CIDFont program contains glyph descriptions that are accessed using a CID as
  34. * the character selector. There are two types of CIDFont. A Type 0 CIDFont contains
  35. * glyph descriptions based on Adobe’s Type 1 font format, whereas those in a
  36. * Type 2 CIDFont are based on the TrueType font format.
  37. *
  38. * A CIDFont dictionary is a PDF object that contains information about a CIDFont program.
  39. * Although its Type value is Font, a CIDFont is not actually a font. It does not have an Encoding
  40. * entry, it cannot be listed in the Font subdictionary of a resource dictionary, and it cannot be
  41. * used as the operand of the Tf operator. It is used only as a descendant of a Type 0 font.
  42. * The CMap in the Type 0 font is what defines the encoding that maps character codes to CIDs
  43. * in the CIDFont.
  44. *
  45. * Font objects should be normally be obtained from the factory methods
  46. * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
  47. *
  48. * @package Zend_Pdf
  49. * @subpackage Fonts
  50. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. abstract class Zend_Pdf_Resource_Font_CidFont extends Zend_Pdf_Resource_Font
  54. {
  55. /**
  56. * Object representing the font's cmap (character to glyph map).
  57. * @var Zend_Pdf_Cmap
  58. */
  59. protected $_cmap = null;
  60. /**
  61. * Array containing the widths of each character that have entries in used character map.
  62. *
  63. * @var array
  64. */
  65. protected $_charWidths = null;
  66. /**
  67. * Width for characters missed in the font
  68. *
  69. * @var integer
  70. */
  71. protected $_missingCharWidth = 0;
  72. /**
  73. * Object constructor
  74. *
  75. * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object
  76. * containing OpenType file.
  77. * @param integer $embeddingOptions Options for font embedding.
  78. * @throws Zend_Pdf_Exception
  79. */
  80. public function __construct(Zend_Pdf_FileParser_Font_OpenType $fontParser)
  81. {
  82. parent::__construct();
  83. $fontParser->parse();
  84. /* Object properties */
  85. $this->_fontNames = $fontParser->names;
  86. $this->_isBold = $fontParser->isBold;
  87. $this->_isItalic = $fontParser->isItalic;
  88. $this->_isMonospaced = $fontParser->isMonospaced;
  89. $this->_underlinePosition = $fontParser->underlinePosition;
  90. $this->_underlineThickness = $fontParser->underlineThickness;
  91. $this->_strikePosition = $fontParser->strikePosition;
  92. $this->_strikeThickness = $fontParser->strikeThickness;
  93. $this->_unitsPerEm = $fontParser->unitsPerEm;
  94. $this->_ascent = $fontParser->ascent;
  95. $this->_descent = $fontParser->descent;
  96. $this->_lineGap = $fontParser->lineGap;
  97. $this->_cmap = $fontParser->cmap;
  98. /* Resource dictionary */
  99. $baseFont = $this->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
  100. $this->_resource->BaseFont = new Zend_Pdf_Element_Name($baseFont);
  101. /**
  102. * Prepare widths array.
  103. */
  104. /* Constract characters widths array using font CMap and glyphs widths array */
  105. $glyphWidths = $fontParser->glyphWidths;
  106. $charGlyphs = $this->_cmap->getCoveredCharactersGlyphs();
  107. $charWidths = array();
  108. foreach ($charGlyphs as $charCode => $glyph) {
  109. $charWidths[$charCode] = $glyphWidths[$glyph];
  110. }
  111. $this->_charWidths = $charWidths;
  112. $this->_missingCharWidth = $glyphWidths[0];
  113. /* Width array optimization. Step1: extract default value */
  114. $widthFrequencies = array_count_values($charWidths);
  115. $defaultWidth = null;
  116. $defaultWidthFrequency = -1;
  117. foreach ($widthFrequencies as $width => $frequency) {
  118. if ($frequency > $defaultWidthFrequency) {
  119. $defaultWidth = $width;
  120. $defaultWidthFrequency = $frequency;
  121. }
  122. }
  123. // Store default value in the font dictionary
  124. $this->_resource->DW = new Zend_Pdf_Element_Numeric($this->toEmSpace($defaultWidth));
  125. // Remove characters which corresponds to default width from the widths array
  126. $defWidthChars = array_keys($charWidths, $defaultWidth);
  127. foreach ($defWidthChars as $charCode) {
  128. unset($charWidths[$charCode]);
  129. }
  130. // Order cheracter widths aray by character codes
  131. ksort($charWidths, SORT_NUMERIC);
  132. /* Width array optimization. Step2: Compact character codes sequences */
  133. $lastCharCode = -1;
  134. $widthsSequences = array();
  135. foreach ($charWidths as $charCode => $width) {
  136. if ($lastCharCode == -1) {
  137. $charCodesSequense = array();
  138. $sequenceStartCode = $charCode;
  139. } else if ($charCode != $lastCharCode + 1) {
  140. // New chracters sequence detected
  141. $widthsSequences[$sequenceStartCode] = $charCodesSequense;
  142. $charCodesSequense = array();
  143. $sequenceStartCode = $charCode;
  144. }
  145. $charCodesSequense[] = $width;
  146. $lastCharCode = $charCode;
  147. }
  148. // Save last sequence, if widths array is not empty (it may happens for monospaced fonts)
  149. if (count($charWidths) != 0) {
  150. $widthsSequences[$sequenceStartCode] = $charCodesSequense;
  151. }
  152. $pdfCharsWidths = array();
  153. foreach ($widthsSequences as $startCode => $widthsSequence) {
  154. /* Width array optimization. Step3: Compact widths sequences */
  155. $pdfWidths = array();
  156. $lastWidth = -1;
  157. $widthsInSequence = 0;
  158. foreach ($widthsSequence as $width) {
  159. if ($lastWidth != $width) {
  160. // New width is detected
  161. if ($widthsInSequence != 0) {
  162. // Previous width value was a part of the widths sequence. Save it as 'c_1st c_last w'.
  163. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
  164. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
  165. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
  166. // Reset widths sequence
  167. $startCode = $startCode + $widthsInSequence;
  168. $widthsInSequence = 0;
  169. }
  170. // Collect new width
  171. $pdfWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($width));
  172. $lastWidth = $width;
  173. } else {
  174. // Width is equal to previous
  175. if (count($pdfWidths) != 0) {
  176. // We already have some widths collected
  177. // So, we've just detected new widths sequence
  178. // Remove last element from widths list, since it's a part of widths sequence
  179. array_pop($pdfWidths);
  180. // and write the rest if it's not empty
  181. if (count($pdfWidths) != 0) {
  182. // Save it as 'c_1st [w1 w2 ... wn]'.
  183. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
  184. $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
  185. // Reset widths collection
  186. $startCode += count($pdfWidths);
  187. $pdfWidths = array();
  188. }
  189. $widthsInSequence = 2;
  190. } else {
  191. // Continue widths sequence
  192. $widthsInSequence++;
  193. }
  194. }
  195. }
  196. // Check if we have widths collection or widths sequence to wite it down
  197. if (count($pdfWidths) != 0) {
  198. // We have some widths collected
  199. // Save it as 'c_1st [w1 w2 ... wn]'.
  200. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
  201. $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
  202. } else if ($widthsInSequence != 0){
  203. // We have widths sequence
  204. // Save it as 'c_1st c_last w'.
  205. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
  206. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
  207. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
  208. }
  209. }
  210. /* Create the Zend_Pdf_Element_Array object and add it to the font's
  211. * object factory and resource dictionary.
  212. */
  213. $widthsArrayElement = new Zend_Pdf_Element_Array($pdfCharsWidths);
  214. $widthsObject = $this->_objectFactory->newObject($widthsArrayElement);
  215. $this->_resource->W = $widthsObject;
  216. /* CIDSystemInfo dictionary */
  217. $cidSystemInfo = new Zend_Pdf_Element_Dictionary();
  218. $cidSystemInfo->Registry = new Zend_Pdf_Element_String('Adobe');
  219. $cidSystemInfo->Ordering = new Zend_Pdf_Element_String('UCS');
  220. $cidSystemInfo->Supplement = new Zend_Pdf_Element_Numeric(0);
  221. $cidSystemInfoObject = $this->_objectFactory->newObject($cidSystemInfo);
  222. $this->_resource->CIDSystemInfo = $cidSystemInfoObject;
  223. }
  224. /**
  225. * Returns an array of glyph numbers corresponding to the Unicode characters.
  226. *
  227. * If a particular character doesn't exist in this font, the special 'missing
  228. * character glyph' will be substituted.
  229. *
  230. * See also {@link glyphNumberForCharacter()}.
  231. *
  232. * @param array $characterCodes Array of Unicode character codes (code points).
  233. * @return array Array of glyph numbers.
  234. */
  235. public function glyphNumbersForCharacters($characterCodes)
  236. {
  237. /**
  238. * CIDFont object is not actually a font. It does not have an Encoding entry,
  239. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  240. * it cannot be used as the operand of the Tf operator.
  241. *
  242. * Throw an exception.
  243. */
  244. require_once 'Zend/Pdf/Exception.php';
  245. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  246. }
  247. /**
  248. * Returns the glyph number corresponding to the Unicode character.
  249. *
  250. * If a particular character doesn't exist in this font, the special 'missing
  251. * character glyph' will be substituted.
  252. *
  253. * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
  254. * operations.
  255. *
  256. * @param integer $characterCode Unicode character code (code point).
  257. * @return integer Glyph number.
  258. */
  259. public function glyphNumberForCharacter($characterCode)
  260. {
  261. /**
  262. * CIDFont object is not actually a font. It does not have an Encoding entry,
  263. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  264. * it cannot be used as the operand of the Tf operator.
  265. *
  266. * Throw an exception.
  267. */
  268. require_once 'Zend/Pdf/Exception.php';
  269. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  270. }
  271. /**
  272. * Returns a number between 0 and 1 inclusive that indicates the percentage
  273. * of characters in the string which are covered by glyphs in this font.
  274. *
  275. * Since no one font will contain glyphs for the entire Unicode character
  276. * range, this method can be used to help locate a suitable font when the
  277. * actual contents of the string are not known.
  278. *
  279. * Note that some fonts lie about the characters they support. Additionally,
  280. * fonts don't usually contain glyphs for control characters such as tabs
  281. * and line breaks, so it is rare that you will get back a full 1.0 score.
  282. * The resulting value should be considered informational only.
  283. *
  284. * @param string $string
  285. * @param string $charEncoding (optional) Character encoding of source text.
  286. * If omitted, uses 'current locale'.
  287. * @return float
  288. */
  289. public function getCoveredPercentage($string, $charEncoding = '')
  290. {
  291. /* Convert the string to UTF-16BE encoding so we can match the string's
  292. * character codes to those found in the cmap.
  293. */
  294. if ($charEncoding != 'UTF-16BE') {
  295. $string = iconv($charEncoding, 'UTF-16BE', $string);
  296. }
  297. $charCount = iconv_strlen($string, 'UTF-16BE');
  298. if ($charCount == 0) {
  299. return 0;
  300. }
  301. /* Calculate the score by doing a lookup for each character.
  302. */
  303. $score = 0;
  304. $maxIndex = strlen($string);
  305. for ($i = 0; $i < $maxIndex; $i++) {
  306. /**
  307. * @todo Properly handle characters encoded as surrogate pairs.
  308. */
  309. $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
  310. /* This could probably be optimized a bit with a binary search...
  311. */
  312. if (isset($this->_charWidths[$charCode])) {
  313. $score++;
  314. }
  315. }
  316. return $score / $charCount;
  317. }
  318. /**
  319. * Returns the widths of the Chars.
  320. *
  321. * The widths are expressed in the font's glyph space. You are responsible
  322. * for converting to user space as necessary. See {@link unitsPerEm()}.
  323. *
  324. * See also {@link widthForChar()}.
  325. *
  326. * @param array &$glyphNumbers Array of glyph numbers.
  327. * @return array Array of glyph widths (integers).
  328. */
  329. public function widthsForChars($charCodes)
  330. {
  331. $widths = array();
  332. foreach ($charCodes as $key => $charCode) {
  333. if (!isset($this->_charWidths[$charCode])) {
  334. $widths[$key] = $this->_missingCharWidth;
  335. } else {
  336. $widths[$key] = $this->_charWidths[$charCode];
  337. }
  338. }
  339. return $widths;
  340. }
  341. /**
  342. * Returns the width of the character.
  343. *
  344. * Like {@link widthsForChars()} but used for one char at a time.
  345. *
  346. * @param integer $charCode
  347. * @return integer
  348. */
  349. public function widthForChar($charCode)
  350. {
  351. if (!isset($this->_charWidths[$charCode])) {
  352. return $this->_missingCharWidth;
  353. }
  354. return $this->_charWidths[$charCode];
  355. }
  356. /**
  357. * Returns the widths of the glyphs.
  358. *
  359. * @param array &$glyphNumbers Array of glyph numbers.
  360. * @return array Array of glyph widths (integers).
  361. * @throws Zend_Pdf_Exception
  362. */
  363. public function widthsForGlyphs($glyphNumbers)
  364. {
  365. /**
  366. * CIDFont object is not actually a font. It does not have an Encoding entry,
  367. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  368. * it cannot be used as the operand of the Tf operator.
  369. *
  370. * Throw an exception.
  371. */
  372. require_once 'Zend/Pdf/Exception.php';
  373. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  374. }
  375. /**
  376. * Returns the width of the glyph.
  377. *
  378. * Like {@link widthsForGlyphs()} but used for one glyph at a time.
  379. *
  380. * @param integer $glyphNumber
  381. * @return integer
  382. * @throws Zend_Pdf_Exception
  383. */
  384. public function widthForGlyph($glyphNumber)
  385. {
  386. /**
  387. * CIDFont object is not actually a font. It does not have an Encoding entry,
  388. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  389. * it cannot be used as the operand of the Tf operator.
  390. *
  391. * Throw an exception.
  392. */
  393. require_once 'Zend/Pdf/Exception.php';
  394. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  395. }
  396. /**
  397. * Convert string to the font encoding.
  398. *
  399. * @param string $string
  400. * @param string $charEncoding Character encoding of source text.
  401. * @return string
  402. * @throws Zend_Pdf_Exception
  403. * */
  404. public function encodeString($string, $charEncoding)
  405. {
  406. /**
  407. * CIDFont object is not actually a font. It does not have an Encoding entry,
  408. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  409. * it cannot be used as the operand of the Tf operator.
  410. *
  411. * Throw an exception.
  412. */
  413. require_once 'Zend/Pdf/Exception.php';
  414. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  415. }
  416. /**
  417. * Convert string from the font encoding.
  418. *
  419. * @param string $string
  420. * @param string $charEncoding Character encoding of resulting text.
  421. * @return string
  422. * @throws Zend_Pdf_Exception
  423. */
  424. public function decodeString($string, $charEncoding)
  425. {
  426. /**
  427. * CIDFont object is not actually a font. It does not have an Encoding entry,
  428. * it cannot be listed in the Font subdictionary of a resource dictionary, and
  429. * it cannot be used as the operand of the Tf operator.
  430. *
  431. * Throw an exception.
  432. */
  433. require_once 'Zend/Pdf/Exception.php';
  434. throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
  435. }
  436. }