2
0

CidFont.php 18 KB

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