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.
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: CidFont.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /** Zend_Pdf_Resource_Font */
24 require_once 'Zend/Pdf/Resource/Font.php';
26 /** Zend_Pdf_FileParser_Font_OpenType */
27 require_once 'Zend/Pdf/FileParser/Font/OpenType.php';
30 require_once 'Zend/Pdf/Cmap.php';
35 * Adobe PDF CIDFont font object implementation
37 * A CIDFont program contains glyph descriptions that are accessed using a CID as
38 * the character selector. There are two types of CIDFont. A Type 0 CIDFont contains
39 * glyph descriptions based on Adobe’s Type 1 font format, whereas those in a
40 * Type 2 CIDFont are based on the TrueType font format.
42 * A CIDFont dictionary is a PDF object that contains information about a CIDFont program.
43 * Although its Type value is Font, a CIDFont is not actually a font. It does not have an Encoding
44 * entry, it cannot be listed in the Font subdictionary of a resource dictionary, and it cannot be
45 * used as the operand of the Tf operator. It is used only as a descendant of a Type 0 font.
46 * The CMap in the Type 0 font is what defines the encoding that maps character codes to CIDs
49 * Font objects should be normally be obtained from the factory methods
50 * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
54 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
55 * @license http://framework.zend.com/license/new-bsd New BSD License
57 abstract class Zend_Pdf_Resource_Font_CidFont
extends Zend_Pdf_Resource_Font
60 * Object representing the font's cmap (character to glyph map).
63 protected $_cmap = null;
66 * Array containing the widths of each character that have entries in used character map.
70 protected $_charWidths = null;
73 * Width for characters missed in the font
77 protected $_missingCharWidth = 0;
83 * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object
84 * containing OpenType file.
85 * @param integer $embeddingOptions Options for font embedding.
86 * @throws Zend_Pdf_Exception
88 public function __construct(Zend_Pdf_FileParser_Font_OpenType
$fontParser)
90 parent
::__construct();
95 /* Object properties */
97 $this->_fontNames
= $fontParser->names
;
99 $this->_isBold
= $fontParser->isBold
;
100 $this->_isItalic
= $fontParser->isItalic
;
101 $this->_isMonospaced
= $fontParser->isMonospaced
;
103 $this->_underlinePosition
= $fontParser->underlinePosition
;
104 $this->_underlineThickness
= $fontParser->underlineThickness
;
105 $this->_strikePosition
= $fontParser->strikePosition
;
106 $this->_strikeThickness
= $fontParser->strikeThickness
;
108 $this->_unitsPerEm
= $fontParser->unitsPerEm
;
110 $this->_ascent
= $fontParser->ascent
;
111 $this->_descent
= $fontParser->descent
;
112 $this->_lineGap
= $fontParser->lineGap
;
115 $this->_cmap
= $fontParser->cmap
;
118 /* Resource dictionary */
120 $baseFont = $this->getFontName(Zend_Pdf_Font
::NAME_POSTSCRIPT
, 'en', 'UTF-8');
121 $this->_resource
->BaseFont
= new Zend_Pdf_Element_Name($baseFont);
125 * Prepare widths array.
127 /* Constract characters widths array using font CMap and glyphs widths array */
128 $glyphWidths = $fontParser->glyphWidths
;
129 $charGlyphs = $this->_cmap
->getCoveredCharactersGlyphs();
130 $charWidths = array();
131 foreach ($charGlyphs as $charCode => $glyph) {
132 $charWidths[$charCode] = $glyphWidths[$glyph];
134 $this->_charWidths
= $charWidths;
135 $this->_missingCharWidth
= $glyphWidths[0];
137 /* Width array optimization. Step1: extract default value */
138 $widthFrequencies = array_count_values($charWidths);
139 $defaultWidth = null;
140 $defaultWidthFrequency = -1;
141 foreach ($widthFrequencies as $width => $frequency) {
142 if ($frequency > $defaultWidthFrequency) {
143 $defaultWidth = $width;
144 $defaultWidthFrequency = $frequency;
148 // Store default value in the font dictionary
149 $this->_resource
->DW
= new Zend_Pdf_Element_Numeric($this->toEmSpace($defaultWidth));
151 // Remove characters which corresponds to default width from the widths array
152 $defWidthChars = array_keys($charWidths, $defaultWidth);
153 foreach ($defWidthChars as $charCode) {
154 unset($charWidths[$charCode]);
157 // Order cheracter widths aray by character codes
158 ksort($charWidths, SORT_NUMERIC
);
160 /* Width array optimization. Step2: Compact character codes sequences */
162 $widthsSequences = array();
163 foreach ($charWidths as $charCode => $width) {
164 if ($lastCharCode == -1) {
165 $charCodesSequense = array();
166 $sequenceStartCode = $charCode;
167 } else if ($charCode != $lastCharCode +
1) {
168 // New chracters sequence detected
169 $widthsSequences[$sequenceStartCode] = $charCodesSequense;
170 $charCodesSequense = array();
171 $sequenceStartCode = $charCode;
173 $charCodesSequense[] = $width;
174 $lastCharCode = $charCode;
176 // Save last sequence, if widths array is not empty (it may happens for monospaced fonts)
177 if (count($charWidths) != 0) {
178 $widthsSequences[$sequenceStartCode] = $charCodesSequense;
181 $pdfCharsWidths = array();
182 foreach ($widthsSequences as $startCode => $widthsSequence) {
183 /* Width array optimization. Step3: Compact widths sequences */
184 $pdfWidths = array();
186 $widthsInSequence = 0;
187 foreach ($widthsSequence as $width) {
188 if ($lastWidth != $width) {
189 // New width is detected
190 if ($widthsInSequence != 0) {
191 // Previous width value was a part of the widths sequence. Save it as 'c_1st c_last w'.
192 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
193 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode +
$widthsInSequence - 1); // Last character code
194 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
196 // Reset widths sequence
197 $startCode = $startCode +
$widthsInSequence;
198 $widthsInSequence = 0;
202 $pdfWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($width));
206 // Width is equal to previous
207 if (count($pdfWidths) != 0) {
208 // We already have some widths collected
209 // So, we've just detected new widths sequence
211 // Remove last element from widths list, since it's a part of widths sequence
212 array_pop($pdfWidths);
214 // and write the rest if it's not empty
215 if (count($pdfWidths) != 0) {
216 // Save it as 'c_1st [w1 w2 ... wn]'.
217 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
218 $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
220 // Reset widths collection
221 $startCode +
= count($pdfWidths);
222 $pdfWidths = array();
225 $widthsInSequence = 2;
227 // Continue widths sequence
233 // Check if we have widths collection or widths sequence to wite it down
234 if (count($pdfWidths) != 0) {
235 // We have some widths collected
236 // Save it as 'c_1st [w1 w2 ... wn]'.
237 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
238 $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
239 } else if ($widthsInSequence != 0){
240 // We have widths sequence
241 // Save it as 'c_1st c_last w'.
242 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
243 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode +
$widthsInSequence - 1); // Last character code
244 $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
248 /* Create the Zend_Pdf_Element_Array object and add it to the font's
249 * object factory and resource dictionary.
251 $widthsArrayElement = new Zend_Pdf_Element_Array($pdfCharsWidths);
252 $widthsObject = $this->_objectFactory
->newObject($widthsArrayElement);
253 $this->_resource
->W
= $widthsObject;
256 /* CIDSystemInfo dictionary */
257 $cidSystemInfo = new Zend_Pdf_Element_Dictionary();
258 $cidSystemInfo->Registry
= new Zend_Pdf_Element_String('Adobe');
259 $cidSystemInfo->Ordering
= new Zend_Pdf_Element_String('UCS');
260 $cidSystemInfo->Supplement
= new Zend_Pdf_Element_Numeric(0);
261 $cidSystemInfoObject = $this->_objectFactory
->newObject($cidSystemInfo);
262 $this->_resource
->CIDSystemInfo
= $cidSystemInfoObject;
268 * Returns an array of glyph numbers corresponding to the Unicode characters.
270 * If a particular character doesn't exist in this font, the special 'missing
271 * character glyph' will be substituted.
273 * See also {@link glyphNumberForCharacter()}.
275 * @param array $characterCodes Array of Unicode character codes (code points).
276 * @return array Array of glyph numbers.
278 public function glyphNumbersForCharacters($characterCodes)
281 * CIDFont object is not actually a font. It does not have an Encoding entry,
282 * it cannot be listed in the Font subdictionary of a resource dictionary, and
283 * it cannot be used as the operand of the Tf operator.
285 * Throw an exception.
287 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
291 * Returns the glyph number corresponding to the Unicode character.
293 * If a particular character doesn't exist in this font, the special 'missing
294 * character glyph' will be substituted.
296 * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
299 * @param integer $characterCode Unicode character code (code point).
300 * @return integer Glyph number.
302 public function glyphNumberForCharacter($characterCode)
305 * CIDFont object is not actually a font. It does not have an Encoding entry,
306 * it cannot be listed in the Font subdictionary of a resource dictionary, and
307 * it cannot be used as the operand of the Tf operator.
309 * Throw an exception.
311 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
316 * Returns a number between 0 and 1 inclusive that indicates the percentage
317 * of characters in the string which are covered by glyphs in this font.
319 * Since no one font will contain glyphs for the entire Unicode character
320 * range, this method can be used to help locate a suitable font when the
321 * actual contents of the string are not known.
323 * Note that some fonts lie about the characters they support. Additionally,
324 * fonts don't usually contain glyphs for control characters such as tabs
325 * and line breaks, so it is rare that you will get back a full 1.0 score.
326 * The resulting value should be considered informational only.
328 * @param string $string
329 * @param string $charEncoding (optional) Character encoding of source text.
330 * If omitted, uses 'current locale'.
333 public function getCoveredPercentage($string, $charEncoding = '')
335 /* Convert the string to UTF-16BE encoding so we can match the string's
336 * character codes to those found in the cmap.
338 if ($charEncoding != 'UTF-16BE') {
339 $string = iconv($charEncoding, 'UTF-16BE', $string);
342 $charCount = iconv_strlen($string, 'UTF-16BE');
343 if ($charCount == 0) {
347 /* Calculate the score by doing a lookup for each character.
350 $maxIndex = strlen($string);
351 for ($i = 0; $i < $maxIndex; $i++
) {
353 * @todo Properly handle characters encoded as surrogate pairs.
355 $charCode = (ord($string[$i]) << 8) |
ord($string[++
$i]);
356 /* This could probably be optimized a bit with a binary search...
358 if (isset($this->_charWidths
[$charCode])) {
362 return $score / $charCount;
366 * Returns the widths of the Chars.
368 * The widths are expressed in the font's glyph space. You are responsible
369 * for converting to user space as necessary. See {@link unitsPerEm()}.
371 * See also {@link widthForChar()}.
373 * @param array &$glyphNumbers Array of glyph numbers.
374 * @return array Array of glyph widths (integers).
376 public function widthsForChars($charCodes)
379 foreach ($charCodes as $key => $charCode) {
380 if (!isset($this->_charWidths
[$charCode])) {
381 $widths[$key] = $this->_missingCharWidth
;
383 $widths[$key] = $this->_charWidths
[$charCode];
390 * Returns the width of the character.
392 * Like {@link widthsForChars()} but used for one char at a time.
394 * @param integer $charCode
397 public function widthForChar($charCode)
399 if (!isset($this->_charWidths
[$charCode])) {
400 return $this->_missingCharWidth
;
402 return $this->_charWidths
[$charCode];
406 * Returns the widths of the glyphs.
408 * @param array &$glyphNumbers Array of glyph numbers.
409 * @return array Array of glyph widths (integers).
410 * @throws Zend_Pdf_Exception
412 public function widthsForGlyphs($glyphNumbers)
415 * CIDFont object is not actually a font. It does not have an Encoding entry,
416 * it cannot be listed in the Font subdictionary of a resource dictionary, and
417 * it cannot be used as the operand of the Tf operator.
419 * Throw an exception.
421 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
425 * Returns the width of the glyph.
427 * Like {@link widthsForGlyphs()} but used for one glyph at a time.
429 * @param integer $glyphNumber
431 * @throws Zend_Pdf_Exception
433 public function widthForGlyph($glyphNumber)
436 * CIDFont object is not actually a font. It does not have an Encoding entry,
437 * it cannot be listed in the Font subdictionary of a resource dictionary, and
438 * it cannot be used as the operand of the Tf operator.
440 * Throw an exception.
442 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
446 * Convert string to the font encoding.
448 * @param string $string
449 * @param string $charEncoding Character encoding of source text.
451 * @throws Zend_Pdf_Exception
453 public function encodeString($string, $charEncoding)
456 * CIDFont object is not actually a font. It does not have an Encoding entry,
457 * it cannot be listed in the Font subdictionary of a resource dictionary, and
458 * it cannot be used as the operand of the Tf operator.
460 * Throw an exception.
462 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
466 * Convert string from the font encoding.
468 * @param string $string
469 * @param string $charEncoding Character encoding of resulting text.
471 * @throws Zend_Pdf_Exception
473 public function decodeString($string, $charEncoding)
476 * CIDFont object is not actually a font. It does not have an Encoding entry,
477 * it cannot be listed in the Font subdictionary of a resource dictionary, and
478 * it cannot be used as the operand of the Tf operator.
480 * Throw an exception.
482 throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');