Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CidFont.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Pdf/Element/Array.php';
25 #require_once 'Zend/Pdf/Element/Dictionary.php';
26 #require_once 'Zend/Pdf/Element/Name.php';
27 #require_once 'Zend/Pdf/Element/Numeric.php';
28 #require_once 'Zend/Pdf/Element/String.php';
29 
30 
32 #require_once 'Zend/Pdf/Resource/Font.php';
33 
58 {
63  protected $_cmap = null;
64 
70  protected $_charWidths = null;
71 
77  protected $_missingCharWidth = 0;
78 
79 
88  public function __construct(Zend_Pdf_FileParser_Font_OpenType $fontParser)
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  }
266 
267 
268 
280  public function glyphNumbersForCharacters($characterCodes)
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  }
292 
305  public function glyphNumberForCharacter($characterCode)
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  }
317 
318 
337  public function getCoveredPercentage($string, $charEncoding = '')
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  }
368 
380  public function widthsForChars($charCodes)
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  }
392 
401  public function widthForChar($charCode)
402  {
403  if (!isset($this->_charWidths[$charCode])) {
405  }
406  return $this->_charWidths[$charCode];
407  }
408 
416  public function widthsForGlyphs($glyphNumbers)
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  }
428 
438  public function widthForGlyph($glyphNumber)
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  }
450 
459  public function encodeString($string, $charEncoding)
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  }
471 
480  public function decodeString($string, $charEncoding)
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  }
492 }
decodeString($string, $charEncoding)
Definition: CidFont.php:480
getFontName($nameType, $language, $characterSet=null)
Definition: Font.php:224
getCoveredPercentage($string, $charEncoding='')
Definition: CidFont.php:337
encodeString($string, $charEncoding)
Definition: CidFont.php:459
const NAME_POSTSCRIPT
Definition: Font.php:224
__construct(Zend_Pdf_FileParser_Font_OpenType $fontParser)
Definition: CidFont.php:88
glyphNumberForCharacter($characterCode)
Definition: CidFont.php:305
$i
Definition: gallery.phtml:31
glyphNumbersForCharacters($characterCodes)
Definition: CidFont.php:280
widthsForGlyphs($glyphNumbers)
Definition: CidFont.php:416