web/lib/Zend/Pdf/FileParser/Font.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 FileParser
       
    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: Font.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /** Internally used classes */
       
    24 require_once 'Zend/Pdf/Font.php';
       
    25 
       
    26 
       
    27 /** Zend_Pdf_FileParser */
       
    28 require_once 'Zend/Pdf/FileParser.php';
       
    29 
       
    30 /**
       
    31  * Abstract helper class for {@link Zend_Pdf_Font} that parses font files.
       
    32  *
       
    33  * Defines the public interface for concrete subclasses which are responsible
       
    34  * for parsing the raw binary data from the font file on disk. Also provides
       
    35  * a debug logging interface and a couple of shared utility methods.
       
    36  *
       
    37  * @package    Zend_Pdf
       
    38  * @subpackage FileParser
       
    39  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    40  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    41  */
       
    42 abstract class Zend_Pdf_FileParser_Font extends Zend_Pdf_FileParser
       
    43 {
       
    44   /**** Instance Variables ****/
       
    45 
       
    46 
       
    47     /**
       
    48      * Array of parsed font properties. Used with {@link __get()} and
       
    49      * {@link __set()}.
       
    50      * @var array
       
    51      */
       
    52     private $_fontProperties = array();
       
    53 
       
    54     /**
       
    55      * Flag indicating whether or not debug logging is active.
       
    56      * @var boolean
       
    57      */
       
    58     private $_debug = false;
       
    59 
       
    60 
       
    61 
       
    62   /**** Public Interface ****/
       
    63 
       
    64 
       
    65   /* Object Lifecycle */
       
    66 
       
    67     /**
       
    68      * Object constructor.
       
    69      *
       
    70      * Validates the data source and enables debug logging if so configured.
       
    71      *
       
    72      * @param Zend_Pdf_FileParserDataSource $dataSource
       
    73      * @throws Zend_Pdf_Exception
       
    74      */
       
    75     public function __construct(Zend_Pdf_FileParserDataSource $dataSource)
       
    76     {
       
    77         parent::__construct($dataSource);
       
    78         $this->fontType = Zend_Pdf_Font::TYPE_UNKNOWN;
       
    79     }
       
    80 
       
    81 
       
    82   /* Accessors */
       
    83 
       
    84     /**
       
    85      * Get handler
       
    86      *
       
    87      * @param string $property
       
    88      * @return mixed
       
    89      */
       
    90     public function __get($property)
       
    91     {
       
    92         if (isset($this->_fontProperties[$property])) {
       
    93             return $this->_fontProperties[$property];
       
    94         } else {
       
    95             return null;
       
    96         }
       
    97     }
       
    98 
       
    99     /* NOTE: The set handler is defined below in the internal methods group. */
       
   100 
       
   101 
       
   102   /* Parser Methods */
       
   103 
       
   104     /**
       
   105      * Reads the Unicode UTF-16-encoded string from the binary file at the
       
   106      * current offset location. Overridden to fix return character set at UTF-16BE.
       
   107      *
       
   108      * @todo Deal with to-dos in the parent method.
       
   109      *
       
   110      * @param integer $byteCount Number of bytes (characters * 2) to return.
       
   111      * @param integer $byteOrder (optional) Big- or little-endian byte order.
       
   112      *   Use the BYTE_ORDER_ constants defined in {@link Zend_Pdf_FileParser}. If
       
   113      *   omitted, uses big-endian.
       
   114      * @param string $characterSet (optional) --Ignored--
       
   115      * @return string
       
   116      * @throws Zend_Pdf_Exception
       
   117      */
       
   118     public function readStringUTF16($byteCount,
       
   119                                     $byteOrder = Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN,
       
   120                                     $characterSet = '')
       
   121     {
       
   122         return parent::readStringUTF16($byteCount, $byteOrder, 'UTF-16BE');
       
   123     }
       
   124 
       
   125     /**
       
   126      * Reads the Mac Roman-encoded string from the binary file at the current
       
   127      * offset location. Overridden to fix return character set at UTF-16BE.
       
   128      *
       
   129      * @param integer $byteCount Number of bytes (characters) to return.
       
   130      * @param string $characterSet (optional) --Ignored--
       
   131      * @return string
       
   132      * @throws Zend_Pdf_Exception
       
   133      */
       
   134     public function readStringMacRoman($byteCount, $characterSet = '')
       
   135     {
       
   136         return parent::readStringMacRoman($byteCount, 'UTF-16BE');
       
   137     }
       
   138 
       
   139     /**
       
   140      * Reads the Pascal string from the binary file at the current offset
       
   141      * location. Overridden to fix return character set at UTF-16BE.
       
   142      *
       
   143      * @param string $characterSet (optional) --Ignored--
       
   144      * @param integer $lengthBytes (optional) Number of bytes that make up the
       
   145      *   length. Default is 1.
       
   146      * @return string
       
   147      * @throws Zend_Pdf_Exception
       
   148      */
       
   149     public function readStringPascal($characterSet = '', $lengthBytes = 1)
       
   150     {
       
   151         return parent::readStringPascal('UTF-16BE');
       
   152     }
       
   153 
       
   154 
       
   155   /* Utility Methods */
       
   156 
       
   157     /**
       
   158      * Writes the entire font properties array to STDOUT. Used only for debugging.
       
   159      */
       
   160     public function writeDebug()
       
   161     {
       
   162         print_r($this->_fontProperties);
       
   163     }
       
   164 
       
   165 
       
   166 
       
   167   /**** Internal Methods ****/
       
   168 
       
   169 
       
   170   /* Internal Accessors */
       
   171 
       
   172     /**
       
   173      * Set handler
       
   174      *
       
   175      * NOTE: This method is protected. Other classes may freely interrogate
       
   176      * the font properties, but only this and its subclasses may set them.
       
   177      *
       
   178      * @param string $property
       
   179      * @param  mixed $value
       
   180      */
       
   181     public function __set($property, $value)
       
   182     {
       
   183         if ($value === null) {
       
   184             unset($this->_fontProperties[$property]);
       
   185         } else {
       
   186             $this->_fontProperties[$property] = $value;
       
   187         }
       
   188     }
       
   189 
       
   190 
       
   191   /* Internal Utility Methods */
       
   192 
       
   193     /**
       
   194      * If debug logging is enabled, writes the log message.
       
   195      *
       
   196      * The log message is a sprintf() style string and any number of arguments
       
   197      * may accompany it as additional parameters.
       
   198      *
       
   199      * @param string $message
       
   200      * @param mixed (optional, multiple) Additional arguments
       
   201      */
       
   202     protected function _debugLog($message)
       
   203     {
       
   204         if (! $this->_debug) {
       
   205             return;
       
   206         }
       
   207         if (func_num_args() > 1) {
       
   208             $args = func_get_args();
       
   209             $message = array_shift($args);
       
   210             $message = vsprintf($message, $args);
       
   211         }
       
   212 
       
   213         require_once 'Zend/Log.php';
       
   214         $logger = new Zend_Log();
       
   215         $logger->log($message, Zend_Log::DEBUG);
       
   216     }
       
   217 }