web/lib/Zend/Pdf/Resource/Font/Extracted.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Pdf
       
    17  * @subpackage Fonts
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Extracted.php 20866 2010-02-03 05:30:07Z yoshida@zend.co.jp $
       
    21  */
       
    22 
       
    23 
       
    24 /** @see Zend_Pdf_Resource_Font */
       
    25 require_once 'Zend/Pdf/Resource/Font.php';
       
    26 
       
    27 /**
       
    28  * Extracted fonts implementation
       
    29  *
       
    30  * Thes class allows to extract fonts already mentioned within PDF document and use them
       
    31  * for text drawing.
       
    32  *
       
    33  * @package    Zend_Pdf
       
    34  * @subpackage Fonts
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
       
    39 {
       
    40     /**
       
    41      * Messages
       
    42      */
       
    43     const TYPE_NOT_SUPPORTED = 'Unsupported font type.';
       
    44     const ENCODING_NOT_SUPPORTED  = 'Font encoding is not supported';
       
    45     const OPERATION_NOT_SUPPORTED = 'Operation is not supported for extracted fonts';
       
    46 
       
    47     /**
       
    48      * Extracted font encoding
       
    49      *
       
    50      * Only 'Identity-H' and 'WinAnsiEncoding' encodings are supported now
       
    51      *
       
    52      * @var string
       
    53      */
       
    54     protected $_encoding = null;
       
    55 
       
    56     /**
       
    57      * Object constructor
       
    58      *
       
    59      * $fontDictionary is a Zend_Pdf_Element_Reference or Zend_Pdf_Element_Object object
       
    60      *
       
    61      * @param mixed $fontDictionary
       
    62      * @throws Zend_Pdf_Exception
       
    63      */
       
    64     public function __construct($fontDictionary)
       
    65     {
       
    66         // Extract object factory and resource object from font dirctionary object
       
    67         $this->_objectFactory = $fontDictionary->getFactory();
       
    68         $this->_resource      = $fontDictionary;
       
    69 
       
    70         if ($fontDictionary->Encoding !== null) {
       
    71             $this->_encoding = $fontDictionary->Encoding->value;
       
    72         }
       
    73 
       
    74         switch ($fontDictionary->Subtype->value) {
       
    75             case 'Type0':
       
    76                 // Composite type 0 font
       
    77                 if (count($fontDictionary->DescendantFonts->items) != 1) {
       
    78                     // Multiple descendant fonts are not supported
       
    79                     require_once 'Zend/Pdf/Exception.php';
       
    80                     throw new Zend_Pdf_Exception(self::TYPE_NOT_SUPPORTED);
       
    81                 }
       
    82 
       
    83                 $fontDictionaryIterator = $fontDictionary->DescendantFonts->items->getIterator();
       
    84                 $fontDictionaryIterator->rewind();
       
    85                 $descendantFont = $fontDictionaryIterator->current();
       
    86                 $fontDescriptor = $descendantFont->FontDescriptor;
       
    87                 break;
       
    88 
       
    89             case 'Type1':
       
    90                 if ($fontDictionary->FontDescriptor === null) {
       
    91                     // That's one of the standard fonts
       
    92                     $standardFont = Zend_Pdf_Font::fontWithName($fontDictionary->BaseFont->value);
       
    93 
       
    94                     $this->_fontNames          = $standardFont->getFontNames();
       
    95                     $this->_isBold             = $standardFont->isBold();
       
    96                     $this->_isItalic           = $standardFont->isItalic();
       
    97                     $this->_isMonospace        = $standardFont->isMonospace();
       
    98                     $this->_underlinePosition  = $standardFont->getUnderlinePosition();
       
    99                     $this->_underlineThickness = $standardFont->getUnderlineThickness();
       
   100                     $this->_strikePosition     = $standardFont->getStrikePosition();
       
   101                     $this->_strikeThickness    = $standardFont->getStrikeThickness();
       
   102                     $this->_unitsPerEm         = $standardFont->getUnitsPerEm();
       
   103                     $this->_ascent             = $standardFont->getAscent();
       
   104                     $this->_descent            = $standardFont->getDescent();
       
   105                     $this->_lineGap            = $standardFont->getLineGap();
       
   106 
       
   107                     return;
       
   108                 }
       
   109 
       
   110                 $fontDescriptor = $fontDictionary->FontDescriptor;
       
   111                 break;
       
   112 
       
   113             case 'TrueType':
       
   114                 $fontDescriptor = $fontDictionary->FontDescriptor;
       
   115                 break;
       
   116 
       
   117             default:
       
   118                 require_once 'Zend/Pdf/Exception.php';
       
   119                 throw new Zend_Pdf_Exception(self::TYPE_NOT_SUPPORTED);
       
   120         }
       
   121 
       
   122         $this->_fontNames[Zend_Pdf_Font::NAME_POSTSCRIPT]['en'] = iconv('UTF-8', 'UTF-16BE', $fontDictionary->BaseFont->value);
       
   123 
       
   124         $this->_isBold             = false; // this property is actually not used anywhere
       
   125         $this->_isItalic           = ( ($fontDescriptor->Flags->value & (1 << 6)) != 0 ); // Bit-7 is set
       
   126         $this->_isMonospace        = ( ($fontDescriptor->Flags->value & (1 << 0)) != 0 ); // Bit-1 is set
       
   127         $this->_underlinePosition  = null; // Can't be extracted
       
   128         $this->_underlineThickness = null; // Can't be extracted
       
   129         $this->_strikePosition     = null; // Can't be extracted
       
   130         $this->_strikeThickness    = null; // Can't be extracted
       
   131         $this->_unitsPerEm         = null; // Can't be extracted
       
   132         $this->_ascent             = $fontDescriptor->Ascent->value;
       
   133         $this->_descent            = $fontDescriptor->Descent->value;
       
   134         $this->_lineGap            = null; // Can't be extracted
       
   135     }
       
   136 
       
   137     /**
       
   138      * Returns an array of glyph numbers corresponding to the Unicode characters.
       
   139      *
       
   140      * If a particular character doesn't exist in this font, the special 'missing
       
   141      * character glyph' will be substituted.
       
   142      *
       
   143      * See also {@link glyphNumberForCharacter()}.
       
   144      *
       
   145      * @param array $characterCodes Array of Unicode character codes (code points).
       
   146      * @return array Array of glyph numbers.
       
   147      */
       
   148     public function glyphNumbersForCharacters($characterCodes)
       
   149     {
       
   150         require_once 'Zend/Pdf/Exception.php';
       
   151         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
       
   152     }
       
   153 
       
   154     /**
       
   155      * Returns the glyph number corresponding to the Unicode character.
       
   156      *
       
   157      * If a particular character doesn't exist in this font, the special 'missing
       
   158      * character glyph' will be substituted.
       
   159      *
       
   160      * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
       
   161      * operations.
       
   162      *
       
   163      * @param integer $characterCode Unicode character code (code point).
       
   164      * @return integer Glyph number.
       
   165      */
       
   166     public function glyphNumberForCharacter($characterCode)
       
   167     {
       
   168         require_once 'Zend/Pdf/Exception.php';
       
   169         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
       
   170     }
       
   171 
       
   172     /**
       
   173      * Returns a number between 0 and 1 inclusive that indicates the percentage
       
   174      * of characters in the string which are covered by glyphs in this font.
       
   175      *
       
   176      * Since no one font will contain glyphs for the entire Unicode character
       
   177      * range, this method can be used to help locate a suitable font when the
       
   178      * actual contents of the string are not known.
       
   179      *
       
   180      * Note that some fonts lie about the characters they support. Additionally,
       
   181      * fonts don't usually contain glyphs for control characters such as tabs
       
   182      * and line breaks, so it is rare that you will get back a full 1.0 score.
       
   183      * The resulting value should be considered informational only.
       
   184      *
       
   185      * @param string $string
       
   186      * @param string $charEncoding (optional) Character encoding of source text.
       
   187      *   If omitted, uses 'current locale'.
       
   188      * @return float
       
   189      */
       
   190     public function getCoveredPercentage($string, $charEncoding = '')
       
   191     {
       
   192         require_once 'Zend/Pdf/Exception.php';
       
   193         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
       
   194     }
       
   195 
       
   196     /**
       
   197      * Returns the widths of the glyphs.
       
   198      *
       
   199      * The widths are expressed in the font's glyph space. You are responsible
       
   200      * for converting to user space as necessary. See {@link unitsPerEm()}.
       
   201      *
       
   202      * See also {@link widthForGlyph()}.
       
   203      *
       
   204      * @param array $glyphNumbers Array of glyph numbers.
       
   205      * @return array Array of glyph widths (integers).
       
   206      * @throws Zend_Pdf_Exception
       
   207      */
       
   208     public function widthsForGlyphs($glyphNumbers)
       
   209     {
       
   210         require_once 'Zend/Pdf/Exception.php';
       
   211         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
       
   212     }
       
   213 
       
   214     /**
       
   215      * Returns the width of the glyph.
       
   216      *
       
   217      * Like {@link widthsForGlyphs()} but used for one glyph at a time.
       
   218      *
       
   219      * @param integer $glyphNumber
       
   220      * @return integer
       
   221      * @throws Zend_Pdf_Exception
       
   222      */
       
   223     public function widthForGlyph($glyphNumber)
       
   224     {
       
   225         require_once 'Zend/Pdf/Exception.php';
       
   226         throw new Zend_Pdf_Exception(self::OPERATION_NOT_SUPPORTED);
       
   227     }
       
   228 
       
   229     /**
       
   230      * Convert string to the font encoding.
       
   231      *
       
   232      * The method is used to prepare string for text drawing operators
       
   233      *
       
   234      * @param string $string
       
   235      * @param string $charEncoding Character encoding of source text.
       
   236      * @return string
       
   237      */
       
   238     public function encodeString($string, $charEncoding)
       
   239     {
       
   240         if ($this->_encoding == 'Identity-H') {
       
   241             return iconv($charEncoding, 'UTF-16BE', $string);
       
   242         }
       
   243 
       
   244         if ($this->_encoding == 'WinAnsiEncoding') {
       
   245             return iconv($charEncoding, 'CP1252//IGNORE', $string);
       
   246         }
       
   247 
       
   248         require_once 'Zend/Pdf/Exception.php';
       
   249         throw new Zend_Pdf_Exception(self::ENCODING_NOT_SUPPORTED);
       
   250     }
       
   251 
       
   252     /**
       
   253      * Convert string from the font encoding.
       
   254      *
       
   255      * The method is used to convert strings retrieved from existing content streams
       
   256      *
       
   257      * @param string $string
       
   258      * @param string $charEncoding Character encoding of resulting text.
       
   259      * @return string
       
   260      */
       
   261     public function decodeString($string, $charEncoding)
       
   262     {
       
   263         if ($this->_encoding == 'Identity-H') {
       
   264             return iconv('UTF-16BE', $charEncoding, $string);
       
   265         }
       
   266 
       
   267         if ($this->_encoding == 'WinAnsiEncoding') {
       
   268             return iconv('CP1252', $charEncoding, $string);
       
   269         }
       
   270 
       
   271         require_once 'Zend/Pdf/Exception.php';
       
   272         throw new Zend_Pdf_Exception(self::ENCODING_NOT_SUPPORTED);
       
   273     }
       
   274 }