File indexing completed on 2024-06-23 05:55:35

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @subpackage Fonts
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /** @see Zend_Pdf_Resource_Font */
0025 // require_once 'Zend/Pdf/Resource/Font.php';
0026 
0027 /**
0028  * Extracted fonts implementation
0029  *
0030  * Thes class allows to extract fonts already mentioned within PDF document and use them
0031  * for text drawing.
0032  *
0033  * @package    Zend_Pdf
0034  * @subpackage Fonts
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
0039 {
0040     /**
0041      * Messages
0042      */
0043     const TYPE_NOT_SUPPORTED = 'Unsupported font type.';
0044     const ENCODING_NOT_SUPPORTED  = 'Font encoding is not supported';
0045     const OPERATION_NOT_SUPPORTED = 'Operation is not supported for extracted fonts';
0046 
0047     /**
0048      * Extracted font encoding
0049      *
0050      * Only 'Identity-H' and 'WinAnsiEncoding' encodings are supported now
0051      *
0052      * @var string
0053      */
0054     protected $_encoding = null;
0055 
0056     /**
0057      * Object constructor
0058      *
0059      * $fontDictionary is a Zend_Pdf_Element_Reference or Zend_Pdf_Element_Object object
0060      *
0061      * @param mixed $fontDictionary
0062      * @throws Zend_Pdf_Exception
0063      */
0064     public function __construct($fontDictionary)
0065     {
0066         // Extract object factory and resource object from font dirctionary object
0067         $this->_objectFactory = $fontDictionary->getFactory();
0068         $this->_resource      = $fontDictionary;
0069 
0070         if ($fontDictionary->Encoding !== null) {
0071             $this->_encoding = $fontDictionary->Encoding->value;
0072         }
0073 
0074         switch ($fontDictionary->Subtype->value) {
0075             case 'Type0':
0076                 // Composite type 0 font
0077                 if (count($fontDictionary->DescendantFonts->items) != 1) {
0078                     // Multiple descendant fonts are not supported
0079                     // require_once 'Zend/Pdf/Exception.php';
0080                     throw new Zend_Pdf_Exception(self::TYPE_NOT_SUPPORTED);
0081                 }
0082 
0083                 $fontDictionaryIterator = $fontDictionary->DescendantFonts->items->getIterator();
0084                 $fontDictionaryIterator->rewind();
0085                 $descendantFont = $fontDictionaryIterator->current();
0086                 $fontDescriptor = $descendantFont->FontDescriptor;
0087                 break;
0088 
0089             case 'Type1':
0090                 if ($fontDictionary->FontDescriptor === null) {
0091                     // That's one of the standard fonts
0092                     $standardFont = Zend_Pdf_Font::fontWithName($fontDictionary->BaseFont->value);
0093 
0094                     $this->_fontNames          = $standardFont->getFontNames();
0095                     $this->_isBold             = $standardFont->isBold();
0096                     $this->_isItalic           = $standardFont->isItalic();
0097                     $this->_isMonospace        = $standardFont->isMonospace();
0098                     $this->_underlinePosition  = $standardFont->getUnderlinePosition();
0099                     $this->_underlineThickness = $standardFont->getUnderlineThickness();
0100                     $this->_strikePosition     = $standardFont->getStrikePosition();
0101                     $this->_strikeThickness    = $standardFont->getStrikeThickness();
0102                     $this->_unitsPerEm         = $standardFont->getUnitsPerEm();
0103                     $this->_ascent             = $standardFont->getAscent();
0104                     $this->_descent            = $standardFont->getDescent();
0105                     $this->_lineGap            = $standardFont->getLineGap();
0106 
0107                     return;
0108                 }
0109 
0110                 $fontDescriptor = $fontDictionary->FontDescriptor;
0111                 break;
0112 
0113             case 'TrueType':
0114                 $fontDescriptor = $fontDictionary->FontDescriptor;
0115                 break;
0116 
0117             default:
0118                 // require_once 'Zend/Pdf/Exception.php';
0119                 throw new Zend_Pdf_Exception(self::TYPE_NOT_SUPPORTED);
0120         }
0121 
0122         $this->_fontNames[Zend_Pdf_Font::NAME_POSTSCRIPT]['en'] = iconv('UTF-8', 'UTF-16BE', $fontDictionary->BaseFont->value);
0123 
0124         $this->_isBold             = false; // this property is actually not used anywhere
0125         $this->_isItalic           = ( ($fontDescriptor->Flags->value & (1 << 6)) != 0 ); // Bit-7 is set
0126         $this->_isMonospace        = ( ($fontDescriptor->Flags->value & (1 << 0)) != 0 ); // Bit-1 is set
0127         $this->_underlinePosition  = null; // Can't be extracted
0128         $this->_underlineThickness = null; // Can't be extracted
0129         $this->_strikePosition     = null; // Can't be extracted
0130         $this->_strikeThickness    = null; // Can't be extracted
0131         $this->_unitsPerEm         = null; // Can't be extracted
0132         $this->_ascent             = $fontDescriptor->Ascent->value;
0133         $this->_descent            = $fontDescriptor->Descent->value;
0134         $this->_lineGap            = null; // Can't be extracted
0135     }
0136 
0137     /**
0138      * Returns an array of glyph numbers corresponding to the Unicode characters.
0139      *
0140      * If a particular character doesn't exist in this font, the special 'missing
0141      * character glyph' will be substituted.
0142      *
0143      * See also {@link glyphNumberForCharacter()}.
0144      *
0145      * @param array $characterCodes Array of Unicode character codes (code points).
0146      * @return array Array of glyph numbers.
0147      */
0148     public function glyphNumbersForCharacters($characterCodes)
0149     {
0150         // require_once 'Zend/Pdf/Exception.php';
0151         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
0152     }
0153 
0154     /**
0155      * Returns the glyph number corresponding to the Unicode character.
0156      *
0157      * If a particular character doesn't exist in this font, the special 'missing
0158      * character glyph' will be substituted.
0159      *
0160      * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
0161      * operations.
0162      *
0163      * @param integer $characterCode Unicode character code (code point).
0164      * @return integer Glyph number.
0165      */
0166     public function glyphNumberForCharacter($characterCode)
0167     {
0168         // require_once 'Zend/Pdf/Exception.php';
0169         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
0170     }
0171 
0172     /**
0173      * Returns a number between 0 and 1 inclusive that indicates the percentage
0174      * of characters in the string which are covered by glyphs in this font.
0175      *
0176      * Since no one font will contain glyphs for the entire Unicode character
0177      * range, this method can be used to help locate a suitable font when the
0178      * actual contents of the string are not known.
0179      *
0180      * Note that some fonts lie about the characters they support. Additionally,
0181      * fonts don't usually contain glyphs for control characters such as tabs
0182      * and line breaks, so it is rare that you will get back a full 1.0 score.
0183      * The resulting value should be considered informational only.
0184      *
0185      * @param string $string
0186      * @param string $charEncoding (optional) Character encoding of source text.
0187      *   If omitted, uses 'current locale'.
0188      * @return float
0189      */
0190     public function getCoveredPercentage($string, $charEncoding = '')
0191     {
0192         // require_once 'Zend/Pdf/Exception.php';
0193         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
0194     }
0195 
0196     /**
0197      * Returns the widths of the glyphs.
0198      *
0199      * The widths are expressed in the font's glyph space. You are responsible
0200      * for converting to user space as necessary. See {@link unitsPerEm()}.
0201      *
0202      * See also {@link widthForGlyph()}.
0203      *
0204      * @param array $glyphNumbers Array of glyph numbers.
0205      * @return array Array of glyph widths (integers).
0206      * @throws Zend_Pdf_Exception
0207      */
0208     public function widthsForGlyphs($glyphNumbers)
0209     {
0210         // require_once 'Zend/Pdf/Exception.php';
0211         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
0212     }
0213 
0214     /**
0215      * Returns the width of the glyph.
0216      *
0217      * Like {@link widthsForGlyphs()} but used for one glyph at a time.
0218      *
0219      * @param integer $glyphNumber
0220      * @return integer
0221      * @throws Zend_Pdf_Exception
0222      */
0223     public function widthForGlyph($glyphNumber)
0224     {
0225         // require_once 'Zend/Pdf/Exception.php';
0226         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
0227     }
0228 
0229     /**
0230      * Convert string to the font encoding.
0231      *
0232      * The method is used to prepare string for text drawing operators
0233      *
0234      * @param string $string
0235      * @param string $charEncoding Character encoding of source text.
0236      * @return string
0237      */
0238     public function encodeString($string, $charEncoding)
0239     {
0240         if ($this->_encoding == 'Identity-H') {
0241             return iconv($charEncoding, 'UTF-16BE', $string);
0242         }
0243 
0244         if ($this->_encoding == 'WinAnsiEncoding') {
0245             return iconv($charEncoding, 'CP1252//IGNORE', $string);
0246         }
0247 
0248         // require_once 'Zend/Pdf/Exception.php';
0249         throw new Zend_Pdf_Exception(self::ENCODING_NOT_SUPPORTED);
0250     }
0251 
0252     /**
0253      * Convert string from the font encoding.
0254      *
0255      * The method is used to convert strings retrieved from existing content streams
0256      *
0257      * @param string $string
0258      * @param string $charEncoding Character encoding of resulting text.
0259      * @return string
0260      */
0261     public function decodeString($string, $charEncoding)
0262     {
0263         if ($this->_encoding == 'Identity-H') {
0264             return iconv('UTF-16BE', $charEncoding, $string);
0265         }
0266 
0267         if ($this->_encoding == 'WinAnsiEncoding') {
0268             return iconv('CP1252', $charEncoding, $string);
0269         }
0270 
0271         // require_once 'Zend/Pdf/Exception.php';
0272         throw new Zend_Pdf_Exception(self::ENCODING_NOT_SUPPORTED);
0273     }
0274 }