PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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