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 /** Internally used classes */
0025 // require_once 'Zend/Pdf/Element/Array.php';
0026 // require_once 'Zend/Pdf/Element/Name.php';
0027 
0028 
0029 /** Zend_Pdf_Resource_Font */
0030 // require_once 'Zend/Pdf/Resource/Font.php';
0031 
0032 /**
0033  * Adobe PDF composite fonts implementation
0034  *
0035  * A composite font is one whose glyphs are obtained from other fonts or from fontlike
0036  * objects called CIDFonts ({@link Zend_Pdf_Resource_Font_CidFont}), organized hierarchically.
0037  * In PDF, a composite font is represented by a font dictionary whose Subtype value is Type0;
0038  * this is also called a Type 0 font (the Type 0 font at the top level of the hierarchy is the
0039  * root font).
0040  *
0041  * In PDF, a Type 0 font is a CID-keyed font.
0042  *
0043  * CID-keyed fonts provide effective method to operate with multi-byte character encodings.
0044  *
0045  * The CID-keyed font architecture specifies the external representation of certain font programs,
0046  * called CMap and CIDFont files, along with some conventions for combining and using those files.
0047  *
0048  * A CID-keyed font is the combination of a CMap with one or more CIDFonts, simple fonts,
0049  * or composite fonts containing glyph descriptions.
0050  *
0051  * The term 'CID-keyed font' reflects the fact that CID (character identifier) numbers
0052  * are used to index and access the glyph descriptions in the font.
0053  *
0054  *
0055  * Font objects should be normally be obtained from the factory methods
0056  * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
0057  *
0058  * @package    Zend_Pdf
0059  * @subpackage Fonts
0060  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0061  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0062  */
0063 class Zend_Pdf_Resource_Font_Type0 extends Zend_Pdf_Resource_Font
0064 {
0065     /**
0066      * Descendant CIDFont
0067      *
0068      * @var Zend_Pdf_Resource_Font_CidFont
0069      */
0070     private $_descendantFont;
0071 
0072 
0073     /**
0074      * Generate ToUnicode character map data
0075      *
0076      * @return string
0077      */
0078     static private function getToUnicodeCMapData()
0079     {
0080         return '/CIDInit /ProcSet findresource begin '              . "\n"
0081              . '12 dict begin '                                     . "\n"
0082              . 'begincmap '                                         . "\n"
0083              . '/CIDSystemInfo '                                    . "\n"
0084              . '<</Registry (Adobe) '                               . "\n"
0085              . '/Ordering (UCS) '                                   . "\n"
0086              . '/Supplement 0'                                      . "\n"
0087              . '>> def'                                             . "\n"
0088              . '/CMapName /Adobe-Identity-UCS def '                 . "\n"
0089              . '/CMapType 2 def '                                   . "\n"
0090              . '1 begincodespacerange'                              . "\n"
0091              . '<0000> <FFFF> '                                     . "\n"
0092              . 'endcodespacerange '                                 . "\n"
0093              . '1 beginbfrange '                                    . "\n"
0094              . '<0000> <FFFF> <0000> '                              . "\n"
0095              . 'endbfrange '                                        . "\n"
0096              . 'endcmap '                                           . "\n"
0097              . 'CMapName currentdict /CMap defineresource pop '     . "\n"
0098              . 'end '
0099              . 'end ';
0100             }
0101 
0102     /**
0103      * Object constructor
0104      *
0105      */
0106     public function __construct(Zend_Pdf_Resource_Font_CidFont $descendantFont)
0107     {
0108         parent::__construct();
0109 
0110         $this->_objectFactory->attach($descendantFont->getFactory());
0111 
0112         $this->_fontType       = Zend_Pdf_Font::TYPE_TYPE_0;
0113         $this->_descendantFont = $descendantFont;
0114 
0115 
0116         $this->_fontNames    = $descendantFont->getFontNames();
0117 
0118         $this->_isBold       = $descendantFont->isBold();
0119         $this->_isItalic     = $descendantFont->isItalic();
0120         $this->_isMonospaced = $descendantFont->isMonospace();
0121 
0122         $this->_underlinePosition  = $descendantFont->getUnderlinePosition();
0123         $this->_underlineThickness = $descendantFont->getUnderlineThickness();
0124         $this->_strikePosition     = $descendantFont->getStrikePosition();
0125         $this->_strikeThickness    = $descendantFont->getStrikeThickness();
0126 
0127         $this->_unitsPerEm = $descendantFont->getUnitsPerEm();
0128 
0129         $this->_ascent  = $descendantFont->getAscent();
0130         $this->_descent = $descendantFont->getDescent();
0131         $this->_lineGap = $descendantFont->getLineGap();
0132 
0133 
0134         $this->_resource->Subtype         = new Zend_Pdf_Element_Name('Type0');
0135         $this->_resource->BaseFont        = new Zend_Pdf_Element_Name($descendantFont->getResource()->BaseFont->value);
0136         $this->_resource->DescendantFonts = new Zend_Pdf_Element_Array(array( $descendantFont->getResource() ));
0137         $this->_resource->Encoding        = new Zend_Pdf_Element_Name('Identity-H');
0138 
0139         $toUnicode = $this->_objectFactory->newStreamObject(self::getToUnicodeCMapData());
0140         $this->_resource->ToUnicode = $toUnicode;
0141 
0142     }
0143 
0144     /**
0145      * Returns an array of glyph numbers corresponding to the Unicode characters.
0146      *
0147      * Zend_Pdf uses 'Identity-H' encoding for Type 0 fonts.
0148      * So we don't need to perform any conversion
0149      *
0150      * See also {@link glyphNumberForCharacter()}.
0151      *
0152      * @param array $characterCodes Array of Unicode character codes (code points).
0153      * @return array Array of glyph numbers.
0154      */
0155     public function glyphNumbersForCharacters($characterCodes)
0156     {
0157         return $characterCodes;
0158     }
0159 
0160     /**
0161      * Returns the glyph number corresponding to the Unicode character.
0162      *
0163      * Zend_Pdf uses 'Identity-H' encoding for Type 0 fonts.
0164      * So we don't need to perform any conversion
0165      *
0166      * @param integer $characterCode Unicode character code (code point).
0167      * @return integer Glyph number.
0168      */
0169     public function glyphNumberForCharacter($characterCode)
0170     {
0171         return $characterCode;
0172     }
0173 
0174     /**
0175      * Returns a number between 0 and 1 inclusive that indicates the percentage
0176      * of characters in the string which are covered by glyphs in this font.
0177      *
0178      * Since no one font will contain glyphs for the entire Unicode character
0179      * range, this method can be used to help locate a suitable font when the
0180      * actual contents of the string are not known.
0181      *
0182      * Note that some fonts lie about the characters they support. Additionally,
0183      * fonts don't usually contain glyphs for control characters such as tabs
0184      * and line breaks, so it is rare that you will get back a full 1.0 score.
0185      * The resulting value should be considered informational only.
0186      *
0187      * @param string $string
0188      * @param string $charEncoding (optional) Character encoding of source text.
0189      *   If omitted, uses 'current locale'.
0190      * @return float
0191      */
0192     public function getCoveredPercentage($string, $charEncoding = '')
0193     {
0194         return $this->_descendantFont->getCoveredPercentage($string, $charEncoding);
0195     }
0196 
0197     /**
0198      * Returns the widths of the glyphs.
0199      *
0200      * The widths are expressed in the font's glyph space. You are responsible
0201      * for converting to user space as necessary. See {@link unitsPerEm()}.
0202      *
0203      * Throws an exception if the glyph number is out of range.
0204      *
0205      * See also {@link widthForGlyph()}.
0206      *
0207      * @param array &$glyphNumbers Array of glyph numbers.
0208      * @return array Array of glyph widths (integers).
0209      * @throws Zend_Pdf_Exception
0210      */
0211     public function widthsForGlyphs($glyphNumbers)
0212     {
0213         return $this->_descendantFont->widthsForChars($glyphNumbers);
0214     }
0215 
0216     /**
0217      * Returns the width of the glyph.
0218      *
0219      * Like {@link widthsForGlyphs()} but used for one glyph at a time.
0220      *
0221      * @param integer $glyphNumber
0222      * @return integer
0223      * @throws Zend_Pdf_Exception
0224      */
0225     public function widthForGlyph($glyphNumber)
0226     {
0227         return $this->_descendantFont->widthForChar($glyphNumber);
0228     }
0229 
0230     /**
0231      * Convert string to the font encoding.
0232      *
0233      * The method is used to prepare string for text drawing operators
0234      *
0235      * @param string $string
0236      * @param string $charEncoding Character encoding of source text.
0237      * @return string
0238      */
0239     public function encodeString($string, $charEncoding)
0240     {
0241         return iconv($charEncoding, 'UTF-16BE', $string);
0242     }
0243 
0244     /**
0245      * Convert string from the font encoding.
0246      *
0247      * The method is used to convert strings retrieved from existing content streams
0248      *
0249      * @param string $string
0250      * @param string $charEncoding Character encoding of resulting text.
0251      * @return string
0252      */
0253         public function decodeString($string, $charEncoding)
0254     {
0255         return iconv('UTF-16BE', $charEncoding, $string);
0256     }
0257 }