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