Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Zend_Pdf_Resource_Font_CidFont Class Reference
Inheritance diagram for Zend_Pdf_Resource_Font_CidFont:
Zend_Pdf_Resource_Font Zend_Pdf_Resource Zend_Pdf_Resource_Font_CidFont_TrueType

Public Member Functions

 __construct (Zend_Pdf_FileParser_Font_OpenType $fontParser)
 
 glyphNumbersForCharacters ($characterCodes)
 
 glyphNumberForCharacter ($characterCode)
 
 getCoveredPercentage ($string, $charEncoding='')
 
 widthsForChars ($charCodes)
 
 widthForChar ($charCode)
 
 widthsForGlyphs ($glyphNumbers)
 
 widthForGlyph ($glyphNumber)
 
 encodeString ($string, $charEncoding)
 
 decodeString ($string, $charEncoding)
 
- Public Member Functions inherited from Zend_Pdf_Resource_Font
 __construct ()
 
 __toString ()
 
 getFontType ()
 
 getFontName ($nameType, $language, $characterSet=null)
 
 getFontNames ()
 
 isBold ()
 
 isItalic ()
 
 isMonospace ()
 
 getUnderlinePosition ()
 
 getUnderlineThickness ()
 
 getStrikePosition ()
 
 getStrikeThickness ()
 
 getUnitsPerEm ()
 
 getAscent ()
 
 getDescent ()
 
 getLineGap ()
 
 getLineHeight ()
 
 glyphNumbersForCharacters ($characterCodes)
 
 glyphNumberForCharacter ($characterCode)
 
 getCoveredPercentage ($string, $charEncoding='')
 
 widthsForGlyphs ($glyphNumbers)
 
 widthForGlyph ($glyphNumber)
 
 encodeString ($string, $charEncoding)
 
 decodeString ($string, $charEncoding)
 
 toEmSpace ($value)
 
- Public Member Functions inherited from Zend_Pdf_Resource
 __construct ($resource)
 
 __clone ()
 
 cloneResource ($factory, &$processed)
 
 getResource ()
 
 getFactory ()
 

Protected Attributes

 $_cmap = null
 
 $_charWidths = null
 
 $_missingCharWidth = 0
 
- Protected Attributes inherited from Zend_Pdf_Resource_Font
 $_fontType = Zend_Pdf_Font::TYPE_UNKNOWN
 
 $_fontNames = array()
 
 $_isBold = false
 
 $_isItalic = false
 
 $_isMonospace = false
 
 $_underlinePosition = 0
 
 $_underlineThickness = 0
 
 $_strikePosition = 0
 
 $_strikeThickness = 0
 
 $_unitsPerEm = 0
 
 $_ascent = 0
 
 $_descent = 0
 
 $_lineGap = 0
 
- Protected Attributes inherited from Zend_Pdf_Resource
 $_objectFactory
 
 $_resource
 

Detailed Description

Definition at line 57 of file CidFont.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Zend_Pdf_FileParser_Font_OpenType  $fontParser)

Object constructor

Parameters
Zend_Pdf_FileParser_Font_OpenType$fontParserFont parser object containing OpenType file.
integer$embeddingOptionsOptions for font embedding.
Exceptions
Zend_Pdf_Exception

Prepare widths array.

Definition at line 88 of file CidFont.php.

89  {
90  parent::__construct();
91 
92  $fontParser->parse();
93 
94 
95  /* Object properties */
96 
97  $this->_fontNames = $fontParser->names;
98 
99  $this->_isBold = $fontParser->isBold;
100  $this->_isItalic = $fontParser->isItalic;
101  $this->_isMonospaced = $fontParser->isMonospaced;
102 
103  $this->_underlinePosition = $fontParser->underlinePosition;
104  $this->_underlineThickness = $fontParser->underlineThickness;
105  $this->_strikePosition = $fontParser->strikePosition;
106  $this->_strikeThickness = $fontParser->strikeThickness;
107 
108  $this->_unitsPerEm = $fontParser->unitsPerEm;
109 
110  $this->_ascent = $fontParser->ascent;
111  $this->_descent = $fontParser->descent;
112  $this->_lineGap = $fontParser->lineGap;
113 
114 
115  $this->_cmap = $fontParser->cmap;
116 
117 
118  /* Resource dictionary */
119 
120  $baseFont = $this->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
121  $this->_resource->BaseFont = new Zend_Pdf_Element_Name($baseFont);
122 
123 
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  if(isset($glyphWidths[$glyph]) && !is_null($glyphWidths[$glyph])) {
133  $charWidths[$charCode] = $glyphWidths[$glyph];
134  }
135  }
136  $this->_charWidths = $charWidths;
137  $this->_missingCharWidth = $glyphWidths[0];
138 
139  /* Width array optimization. Step1: extract default value */
140  $widthFrequencies = array_count_values($charWidths);
141  $defaultWidth = null;
142  $defaultWidthFrequency = -1;
143  foreach ($widthFrequencies as $width => $frequency) {
144  if ($frequency > $defaultWidthFrequency) {
145  $defaultWidth = $width;
146  $defaultWidthFrequency = $frequency;
147  }
148  }
149 
150  // Store default value in the font dictionary
151  $this->_resource->DW = new Zend_Pdf_Element_Numeric($this->toEmSpace($defaultWidth));
152 
153  // Remove characters which corresponds to default width from the widths array
154  $defWidthChars = array_keys($charWidths, $defaultWidth);
155  foreach ($defWidthChars as $charCode) {
156  unset($charWidths[$charCode]);
157  }
158 
159  // Order cheracter widths aray by character codes
160  ksort($charWidths, SORT_NUMERIC);
161 
162  /* Width array optimization. Step2: Compact character codes sequences */
163  $lastCharCode = -1;
164  $widthsSequences = array();
165  foreach ($charWidths as $charCode => $width) {
166  if ($lastCharCode == -1) {
167  $charCodesSequense = array();
168  $sequenceStartCode = $charCode;
169  } else if ($charCode != $lastCharCode + 1) {
170  // New chracters sequence detected
171  $widthsSequences[$sequenceStartCode] = $charCodesSequense;
172  $charCodesSequense = array();
173  $sequenceStartCode = $charCode;
174  }
175  $charCodesSequense[] = $width;
176  $lastCharCode = $charCode;
177  }
178  // Save last sequence, if widths array is not empty (it may happens for monospaced fonts)
179  if (count($charWidths) != 0) {
180  $widthsSequences[$sequenceStartCode] = $charCodesSequense;
181  }
182 
183  $pdfCharsWidths = array();
184  foreach ($widthsSequences as $startCode => $widthsSequence) {
185  /* Width array optimization. Step3: Compact widths sequences */
186  $pdfWidths = array();
187  $lastWidth = -1;
188  $widthsInSequence = 0;
189  foreach ($widthsSequence as $width) {
190  if ($lastWidth != $width) {
191  // New width is detected
192  if ($widthsInSequence != 0) {
193  // Previous width value was a part of the widths sequence. Save it as 'c_1st c_last w'.
194  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
195  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
196  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
197 
198  // Reset widths sequence
199  $startCode = $startCode + $widthsInSequence;
200  $widthsInSequence = 0;
201  }
202 
203  // Collect new width
204  $pdfWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($width));
205 
206  $lastWidth = $width;
207  } else {
208  // Width is equal to previous
209  if (count($pdfWidths) != 0) {
210  // We already have some widths collected
211  // So, we've just detected new widths sequence
212 
213  // Remove last element from widths list, since it's a part of widths sequence
214  array_pop($pdfWidths);
215 
216  // and write the rest if it's not empty
217  if (count($pdfWidths) != 0) {
218  // Save it as 'c_1st [w1 w2 ... wn]'.
219  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
220  $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
221 
222  // Reset widths collection
223  $startCode += count($pdfWidths);
224  $pdfWidths = array();
225  }
226 
227  $widthsInSequence = 2;
228  } else {
229  // Continue widths sequence
230  $widthsInSequence++;
231  }
232  }
233  }
234 
235  // Check if we have widths collection or widths sequence to wite it down
236  if (count($pdfWidths) != 0) {
237  // We have some widths collected
238  // Save it as 'c_1st [w1 w2 ... wn]'.
239  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
240  $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array
241  } else if ($widthsInSequence != 0){
242  // We have widths sequence
243  // Save it as 'c_1st c_last w'.
244  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code
245  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code
246  $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width
247  }
248  }
249 
250  /* Create the Zend_Pdf_Element_Array object and add it to the font's
251  * object factory and resource dictionary.
252  */
253  $widthsArrayElement = new Zend_Pdf_Element_Array($pdfCharsWidths);
254  $widthsObject = $this->_objectFactory->newObject($widthsArrayElement);
255  $this->_resource->W = $widthsObject;
256 
257 
258  /* CIDSystemInfo dictionary */
259  $cidSystemInfo = new Zend_Pdf_Element_Dictionary();
260  $cidSystemInfo->Registry = new Zend_Pdf_Element_String('Adobe');
261  $cidSystemInfo->Ordering = new Zend_Pdf_Element_String('UCS');
262  $cidSystemInfo->Supplement = new Zend_Pdf_Element_Numeric(0);
263  $cidSystemInfoObject = $this->_objectFactory->newObject($cidSystemInfo);
264  $this->_resource->CIDSystemInfo = $cidSystemInfoObject;
265  }
getFontName($nameType, $language, $characterSet=null)
Definition: Font.php:224
const NAME_POSTSCRIPT
Definition: Font.php:224

Member Function Documentation

◆ decodeString()

decodeString (   $string,
  $charEncoding 
)

Convert string from the font encoding.

Parameters
string$string
string$charEncodingCharacter encoding of resulting text.
Returns
string
Exceptions
Zend_Pdf_Exception

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.

Definition at line 480 of file CidFont.php.

481  {
489  #require_once 'Zend/Pdf/Exception.php';
490  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
491  }

◆ encodeString()

encodeString (   $string,
  $charEncoding 
)

Convert string to the font encoding.

Parameters
string$string
string$charEncodingCharacter encoding of source text.
Returns
string
Exceptions
Zend_Pdf_Exception

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.

Definition at line 459 of file CidFont.php.

460  {
468  #require_once 'Zend/Pdf/Exception.php';
469  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
470  }

◆ getCoveredPercentage()

getCoveredPercentage (   $string,
  $charEncoding = '' 
)

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.

Parameters
string$string
string$charEncoding(optional) Character encoding of source text. If omitted, uses 'current locale'.
Returns
float
Todo:
Properly handle characters encoded as surrogate pairs.

Definition at line 337 of file CidFont.php.

338  {
339  /* Convert the string to UTF-16BE encoding so we can match the string's
340  * character codes to those found in the cmap.
341  */
342  if ($charEncoding != 'UTF-16BE') {
343  $string = iconv($charEncoding, 'UTF-16BE', $string);
344  }
345 
346  $charCount = iconv_strlen($string, 'UTF-16BE');
347  if ($charCount == 0) {
348  return 0;
349  }
350 
351  /* Calculate the score by doing a lookup for each character.
352  */
353  $score = 0;
354  $maxIndex = strlen($string);
355  for ($i = 0; $i < $maxIndex; $i++) {
359  $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
360  /* This could probably be optimized a bit with a binary search...
361  */
362  if (isset($this->_charWidths[$charCode])) {
363  $score++;
364  }
365  }
366  return $score / $charCount;
367  }
$i
Definition: gallery.phtml:31

◆ glyphNumberForCharacter()

glyphNumberForCharacter (   $characterCode)

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 glyphNumbersForCharacters() which is optimized for bulk operations.

Parameters
integer$characterCodeUnicode character code (code point).
Returns
integer Glyph number.

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.

Definition at line 305 of file CidFont.php.

306  {
314  #require_once 'Zend/Pdf/Exception.php';
315  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
316  }

◆ glyphNumbersForCharacters()

glyphNumbersForCharacters (   $characterCodes)

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 glyphNumberForCharacter().

Parameters
array$characterCodesArray of Unicode character codes (code points).
Returns
array Array of glyph numbers.

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.

Definition at line 280 of file CidFont.php.

281  {
289  #require_once 'Zend/Pdf/Exception.php';
290  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
291  }

◆ widthForChar()

widthForChar (   $charCode)

Returns the width of the character.

Like widthsForChars() but used for one char at a time.

Parameters
integer$charCode
Returns
integer

Definition at line 401 of file CidFont.php.

402  {
403  if (!isset($this->_charWidths[$charCode])) {
405  }
406  return $this->_charWidths[$charCode];
407  }

◆ widthForGlyph()

widthForGlyph (   $glyphNumber)

Returns the width of the glyph.

Like widthsForGlyphs() but used for one glyph at a time.

Parameters
integer$glyphNumber
Returns
integer
Exceptions
Zend_Pdf_Exception

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.

Definition at line 438 of file CidFont.php.

439  {
447  #require_once 'Zend/Pdf/Exception.php';
448  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
449  }

◆ widthsForChars()

widthsForChars (   $charCodes)

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 unitsPerEm().

See also widthForChar().

Parameters
array&$glyphNumbersArray of glyph numbers.
Returns
array Array of glyph widths (integers).

Definition at line 380 of file CidFont.php.

381  {
382  $widths = array();
383  foreach ($charCodes as $key => $charCode) {
384  if (!isset($this->_charWidths[$charCode])) {
385  $widths[$key] = $this->_missingCharWidth;
386  } else {
387  $widths[$key] = $this->_charWidths[$charCode];
388  }
389  }
390  return $widths;
391  }

◆ widthsForGlyphs()

widthsForGlyphs (   $glyphNumbers)

Returns the widths of the glyphs.

Parameters
array&$glyphNumbersArray of glyph numbers.
Returns
array Array of glyph widths (integers).
Exceptions
Zend_Pdf_Exception

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.

Definition at line 416 of file CidFont.php.

417  {
425  #require_once 'Zend/Pdf/Exception.php';
426  throw new Zend_Pdf_Exception('CIDFont PDF objects could not be used as the operand of the text drawing operators');
427  }

Field Documentation

◆ $_charWidths

$_charWidths = null
protected

Definition at line 70 of file CidFont.php.

◆ $_cmap

$_cmap = null
protected

Definition at line 63 of file CidFont.php.

◆ $_missingCharWidth

$_missingCharWidth = 0
protected

Definition at line 77 of file CidFont.php.


The documentation for this class was generated from the following file: