Kaynağa Gözat

Zend_Pdf: performance improvement [ZF-6924], [ZF-6925].

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17298 44c647ce-9c0f-0410-b52a-842ac1e357ba
alexander 16 yıl önce
ebeveyn
işleme
a832f8d6d5

+ 3 - 3
library/Zend/Pdf.php

@@ -602,7 +602,7 @@ class Zend_Pdf
         $pagesContainer = $root->Pages;
 
         $pagesContainer->touch();
-        $pagesContainer->Kids->items->clear();
+        $pagesContainer->Kids->items = array();
 
         foreach ($this->pages as $page ) {
             $page->render($this->_objFactory);
@@ -692,8 +692,7 @@ class Zend_Pdf
     {
         ksort($this->_namedTargets, SORT_STRING);
 
-        $destArray = $this->_objFactory->newObject(new Zend_Pdf_Element_Array());
-        $destArrayItems = &$destArray->items;
+        $destArrayItems = array();
         foreach ($this->_namedTargets as $name => $destination) {
             $destArrayItems[] = new Zend_Pdf_Element_String($name);
 
@@ -704,6 +703,7 @@ class Zend_Pdf
                 throw new Zend_Pdf_Exception('PDF named destinations must be a Zend_Pdf_Target object.');
             }
         }
+        $destArray = $this->_objFactory->newObject(new Zend_Pdf_Element_Array($destArrayItems));
 
         $DestTree = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
         $DestTree->Names = $destArray;

+ 21 - 27
library/Zend/Pdf/Element/Array.php

@@ -23,10 +23,6 @@
 /** Zend_Pdf_Element */
 require_once 'Zend/Pdf/Element.php';
 
-/** Zend_Pdf_PhpArray */
-require_once 'Zend/Pdf/PhpArray.php';
-
-
 
 /**
  * PDF file 'array' element implementation
@@ -39,14 +35,13 @@ require_once 'Zend/Pdf/PhpArray.php';
 class Zend_Pdf_Element_Array extends Zend_Pdf_Element
 {
     /**
-     * Object value
-     * Array of Zend_Pdf_Element objects.
-     * Appropriate methods must (!) be used to modify it to provide correct
-     * work with objects and references.
+     * Array element items
      *
-     * @var Zend_Pdf_PhpArray
+     * Array of Zend_Pdf_Element objects
+     *
+     * @var array
      */
-    private $_items;
+    public $items;
 
 
     /**
@@ -57,14 +52,14 @@ class Zend_Pdf_Element_Array extends Zend_Pdf_Element
      */
     public function __construct($val = null)
     {
-        $this->_items = new Zend_Pdf_PhpArray();
+        $this->items = new ArrayObject();
 
         if ($val !== null  &&  is_array($val)) {
             foreach ($val as $element) {
                 if (!$element instanceof Zend_Pdf_Element) {
                     throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects');
                 }
-                $this->_items[] = $element;
+                $this->items[] = $element;
             }
         } else if ($val !== null){
             throw new Zend_Pdf_Exception('Argument must be an array');
@@ -73,30 +68,27 @@ class Zend_Pdf_Element_Array extends Zend_Pdf_Element
 
 
     /**
-     * Provides access to $this->_items
+     * Getter
      *
      * @param string $property
-     * @return Zend_Pdf_PhpArray
+     * @throws Zend_Pdf_Exception
      */
     public function __get($property) {
-        if ($property=='items') {
-            return $this->_items;
-        }
-        throw new Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
+    	require_once 'Zend/Pdf/Exception.php';
+        throw new Zend_Pdf_Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
     }
 
 
     /**
-     * Provides read-only access to $this->_items;
+     * Setter
      *
-     * @param unknown_type $offset
-     * @param unknown_type $value
+     * @param mixed $offset
+     * @param mixed $value
+     * @throws Zend_Pdf_Exception
      */
     public function __set($property, $value) {
-        if ($property=='items') {
-            throw new Exception('Array container cannot be overwritten');
-        }
-        throw new Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
+        require_once 'Zend/Pdf/Exception.php';
+        throw new Zend_Pdf_Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
     }
 
     /**
@@ -121,7 +113,7 @@ class Zend_Pdf_Element_Array extends Zend_Pdf_Element
         $outStr = '[';
         $lastNL = 0;
 
-        foreach ($this->_items as $element) {
+        foreach ($this->items as $element) {
             if (strlen($outStr) - $lastNL > 128)  {
                 $outStr .= "\n";
                 $lastNL = strlen($outStr);
@@ -143,7 +135,9 @@ class Zend_Pdf_Element_Array extends Zend_Pdf_Element
      */
     public function toPhp()
     {
-        foreach ($this->_items as $item) {
+    	$phpArray = array();
+
+    	foreach ($this->items as $item) {
             $phpArray[] = $item->toPhp();
         }
 

+ 0 - 6
library/Zend/Pdf/Parser.php

@@ -67,15 +67,9 @@ require_once 'Zend/Pdf/Trailer/Keeper.php';
 /** Zend_Pdf_ElementFactory_Interface */
 require_once 'Zend/Pdf/ElementFactory/Interface.php';
 
-/** Zend_Pdf_PhpArray */
-require_once 'Zend/Pdf/PhpArray.php';
-
 /** Zend_Pdf_StringParser */
 require_once 'Zend/Pdf/StringParser.php';
 
-/** Zend_Pdf_Parser_Stream */
-require_once 'Zend/Pdf/Parser/Stream.php';
-
 
 /**
  * PDF file parser

+ 0 - 61
library/Zend/Pdf/Parser/Stream.php

@@ -1,61 +0,0 @@
-<?php
-/**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category   Zend
- * @package    Zend_Pdf
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id$
- */
-
-
-/** Zend_Pdf_Parser */
-require_once 'Zend/Pdf/Parser.php';
-
-
-/**
- * PDF object stream parser
- *
- * @package    Zend_Pdf
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- */
-class Zend_Pdf_Parser_Stream extends Zend_Pdf_Parser
-{
-    /**
-     * Get Trailer object
-     *
-     * @return Zend_Pdf_Trailer_Keeper
-     */
-    public function getTrailer()
-    {
-        throw new Zend_Pdf_Exception('Stream object parser doesn\'t contain trailer information.');
-    }
-
-    /**
-     * Object constructor
-     *
-     * @param string $pdfString
-     * @param Zend_Pdf_ElementFactory $factory
-     * @throws Zend_Exception
-     */
-    public function __construct(&$source, Zend_Pdf_ElementFactory $factory)
-    {
-        $this->_current        = 0;
-        $this->_currentContext = null;
-        $this->_contextStack   = array();
-        $this->_elements       = new Zend_Pdf_PhpArray();
-        $this->_objFactory     = $factory;
-    }
-}

+ 0 - 129
library/Zend/Pdf/PhpArray.php

@@ -1,129 +0,0 @@
-<?php
-/**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category   Zend
- * @package    Zend_Pdf
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id$
- */
-
-
-/**
- * PHP Array (OO wrapper)
- * Used to be returned by reference by __get() methods
- *
- * @package    Zend_Pdf
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @todo       also implement Countable for PHP 5.1 but not yet to stay 5.0 compatible
- */
-class Zend_Pdf_PhpArray implements ArrayAccess, Iterator, Countable {
-    /**
-     * Array element
-     * @var mixed
-     */
-    protected $_items = array();
-
-
-    /**
-     * Object constructor
-     *
-     * @param array $srcArray
-     */
-    public function __construct($srcArray = null)
-    {
-        if ($srcArray === null) {
-            reset($this->_items);
-        } else if (is_array($srcArray)) {
-            $this->_items = $srcArray;
-        } else {
-            throw new Exception('Array can be initialized only by other array');
-        }
-    }
-
-
-    public function current()
-    {
-        return current($this->_items);
-    }
-
-
-    public function next()
-    {
-        return next($this->_items);
-    }
-
-
-    public function key()
-    {
-        return key($this->_items);
-    }
-
-
-    public function valid() {
-        return current($this->_items)!==false;
-    }
-
-
-    public function rewind()
-    {
-        reset($this->_items);
-    }
-
-
-    public function offsetExists($offset)
-    {
-        return array_key_exists($offset, $this->_items);
-    }
-
-
-    public function offsetGet($offset)
-    {
-        return $this->_items[$offset];
-    }
-
-
-    public function offsetSet($offset, $value)
-    {
-        if ($offset === null) {
-            $this->_items[]        = $value;
-        } else {
-            $this->_items[$offset] = $value;
-        }
-    }
-
-
-    public function offsetUnset($offset)
-    {
-        unset($this->_items[$offset]);
-    }
-
-
-    public function clear()
-    {
-        $this->_items = array();
-    }
-
-    /**
-     * Defined by Countable interface
-     *
-     * @return int
-     */
-    public function count()
-    {
-        return count($this->_items);
-    }
-}
-

+ 28 - 29
library/Zend/Pdf/Resource/Font/Extracted.php

@@ -30,10 +30,10 @@ require_once 'Zend/Pdf/Cmap.php';
 
 /**
  * Extracted fonts implementation
- * 
+ *
  * Thes class allows to extract fonts already mentioned within PDF document and use them
  * for text drawing.
- * 
+ *
  * @package    Zend_Pdf
  * @subpackage Fonts
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -43,16 +43,16 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
 {
     /**
      * Extracted font encoding
-     * 
+     *
      * Only 'Identity-H' and 'WinAnsiEncoding' encodings are supported now
-     * 
+     *
      * @var string
      */
     protected $_encoding = null;
-    
+
     /**
      * Object constructor
-     * 
+     *
      * $fontDictionary is a Zend_Pdf_Element_Reference or Zend_Pdf_Element_Object object
      *
      * @param mixed $fontDictionary
@@ -60,36 +60,35 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
      */
     public function __construct($fontDictionary)
     {
-        // Extract object factory and resource object from font dirctionary object 
+        // Extract object factory and resource object from font dirctionary object
         $this->_objectFactory = $fontDictionary->getFactory();
         $this->_resource      = $fontDictionary;
-        
+
         if ($fontDictionary->Encoding !== null) {
             $this->_encoding = $fontDictionary->Encoding->value;
-        } 
+        }
 
         switch ($fontDictionary->Subtype->value) {
             case 'Type0':
                 // Composite type 0 font
-                if ($fontDictionary->DescendantFonts->items->count() != 1) {
+                if (count($fontDictionary->DescendantFonts->items) != 1) {
                     // Multiple descendant fonts are not supported
                     throw new Zend_Pdf_Exception('Unsupported font type.');
                 }
-                
-                $descFontsArrayItems = $fontDictionary->DescendantFonts->items; 
-                $descFontsArrayItems->rewind();
-                
-                $descendantFont = $descFontsArrayItems->current();
+
+                $fontDictionaryIterator = $fontDictionary->DescendantFonts->items->getIterator();
+                $fontDictionaryIterator->rewind();
+                $descendantFont = $fontDictionaryIterator->current();
                 $fontDescriptor = $descendantFont->FontDescriptor;
                 break;
-                
+
             case 'Type1':
                 if ($fontDictionary->FontDescriptor === null) {
                     // That's one of the standard fonts
                     $standardFont = Zend_Pdf_Font::fontWithName($fontDictionary->BaseFont->value);
-                    
+
                     $this->_fontNames          = $standardFont->getFontNames();
-                    $this->_isBold             = $standardFont->isBold();  
+                    $this->_isBold             = $standardFont->isBold();
                     $this->_isItalic           = $standardFont->isItalic();
                     $this->_isMonospace        = $standardFont->isMonospace();
                     $this->_underlinePosition  = $standardFont->getUnderlinePosition();
@@ -110,14 +109,14 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
             case 'TrueType':
                 $fontDescriptor = $fontDictionary->FontDescriptor;
                 break;
-                
+
             default:
-                throw new Zend_Pdf_Exception('Unsupported font type.'); 
+                throw new Zend_Pdf_Exception('Unsupported font type.');
         }
 
         $this->_fontNames[Zend_Pdf_Font::NAME_POSTSCRIPT]['en'] = iconv('UTF-8', 'UTF-16BE', $fontDictionary->BaseFont->value);
-        
-        $this->_isBold             = false; // this property is actually not used anywhere 
+
+        $this->_isBold             = false; // this property is actually not used anywhere
         $this->_isItalic           = ( ($fontDescriptor->Flags->value & (1 << 6)) != 0 ); // Bit-7 is set
         $this->_isMonospace        = ( ($fontDescriptor->Flags->value & (1 << 0)) != 0 ); // Bit-1 is set
         $this->_underlinePosition  = null; // Can't be extracted
@@ -216,11 +215,11 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
     {
         throw new Zend_Pdf_Exception('Operation is not supported for extracted fonts');
     }
-    
+
     /**
      * Convert string to the font encoding.
-     * 
-     * The method is used to prepare string for text drawing operators 
+     *
+     * The method is used to prepare string for text drawing operators
      *
      * @param string $string
      * @param string $charEncoding Character encoding of source text.
@@ -231,11 +230,11 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
         if ($this->_encoding == 'Identity-H') {
             return iconv($charEncoding, 'UTF-16BE', $string);
         }
-        
+
         if ($this->_encoding == 'WinAnsiEncoding') {
             return iconv($charEncoding, 'CP1252//IGNORE', $string);
         }
-        
+
         throw new Zend_Pdf_Exception('Fonf encoding is not supported');
     }
 
@@ -253,11 +252,11 @@ class Zend_Pdf_Resource_Font_Extracted extends Zend_Pdf_Resource_Font
         if ($this->_encoding == 'Identity-H') {
             return iconv('UTF-16BE', $charEncoding, $string);
         }
-        
+
         if ($this->_encoding == 'WinAnsiEncoding') {
             return iconv('CP1252', $charEncoding, $string);
         }
-        
+
         throw new Zend_Pdf_Exception('Fonf encoding is not supported');
     }
 }

+ 15 - 4
library/Zend/Pdf/StringParser.php

@@ -65,9 +65,6 @@ require_once 'Zend/Pdf/Element/Reference/Table.php';
 /** Zend_Pdf_ElementFactory_Interface */
 require_once 'Zend/Pdf/ElementFactory/Interface.php';
 
-/** Zend_Pdf_PhpArray */
-require_once 'Zend/Pdf/PhpArray.php';
-
 
 /**
  * PDF string parser
@@ -246,7 +243,21 @@ class Zend_Pdf_StringParser
      */
     public function readLexeme()
     {
-        $this->skipWhiteSpace();
+        // $this->skipWhiteSpace();
+        while (true) {
+            $this->offset += strspn($this->data, "\x00\t\n\f\r ", $this->offset);
+
+            if ($this->data[$this->offset] == '%') {
+                preg_match('/[\r\n]/', $this->data, $matches, PREG_OFFSET_CAPTURE, $this->offset);
+                if (count($matches) > 0) {
+                	$this->offset += strlen($matches[0][0]) + $matches[0][1];
+                } else {
+                	$this->offset = strlen($this->data);
+                }
+            } else {
+            	break;
+            }
+        }
 
         if ($this->offset >= strlen($this->data)) {
             return '';