--- Cell.phps 2008-03-31 09:36:11.000000000 -0700 +++ Cell.php 2009-01-12 14:06:09.000000000 -0800 @@ -17,6 +17,18 @@ * @license http://framework.zend.com/license/new-bsd New BSD License */ + /* Changes: + 2008-12-02 Dominik Deobald + - Applied some changes to make it at least a little bit more UTF-8 compatible. + Umlauts were missing when a line had to be wordwrapped. + + 2009-01-12 Dominik Deobald + - Fixed bug in WordWrap where last word would just fit into a row, + but the following space would not. Resulted in infinite loop. + - Cleaned up my previous changes concerning char encodings. "Sections" + contain encoding information, so no additional parameter is required. + */ + /** Zend_Pdf_Exception */ require_once 'Zend/Pdf/Exception.php'; @@ -170,15 +182,22 @@ if ($lineWidth+$section['width'] > $maxWidth) { //section of text is greater than our box's diminsions $splitSection=explode(' ',$section['text']); - $maxTextSection=$this->_makeTextSection(array_shift($splitSection)); + $maxTextSection=$this->_makeTextSection(array_shift($splitSection), $section['encoding']); + + $spaceWidth=$this->_getTextWidth($this->_makeTextSection(' ')); + while($maxTextSection['text']!=null && $lineWidth + $maxTextSection['width'] < $maxWidth) { - $this->addText($maxTextSection['text'].' '); + $this->addText($maxTextSection['text'], null, 0, $section['encoding']); $lineWidth=$this->_text[$this->_lineNumber]['width']; - $maxTextSection=$this->_makeTextSection(array_shift($splitSection)); + if ($lineWidth + $spaceWidth < $maxWidth) { + $this->addText(' '); + $lineWidth += $spaceWidth; + } + $maxTextSection=$this->_makeTextSection(array_shift($splitSection), $section['encoding']); } $this->newLine(); $restOfText=implode(' ',$splitSection); - $this->addText($maxTextSection['text'].' '.$restOfText); + $this->addText($maxTextSection['text'].' '.$restOfText, null, 0, $section['encoding']); return true; } return false; @@ -190,13 +209,14 @@ * @param string $text String of text to turn into a section. * @return array A text section array. */ - private function _makeTextSection($text,$charEncoding='') { + private function _makeTextSection($text, $charEncoding='ISO-8859-1') { $section=array(); $section['text']=$text; $section['encoding']=$charEncoding; $section['font']=$this->_font; $section['fontSize']=$this->_fontSize; $section['width']=$this->_getTextWidth($section); + return $section; } @@ -216,7 +236,7 @@ * @param string $charEncoding (Optional) Encoding of this particular section of text. * Defaults to current locale. */ - public function addText($text, $alignment=null, $offset=0, $charEncoding='') { + public function addText($text, $alignment=null, $offset=0, $charEncoding='ISO-8859-1') { //Set the alignment if ($alignment!==null) { $this->_text[$this->_lineNumber]['alignment']=$alignment; @@ -234,8 +254,7 @@ } } - - $section=$this->_makeTextSection($text,$charEncoding); + $section=$this->_makeTextSection($text, $charEncoding); if ($this->_wordWrap(&$section)) { return; //word wrap has already taken care of calling addText @@ -246,7 +265,7 @@ $this->_text[$this->_lineNumber][$this->_section]['font']=$section['font']; $this->_text[$this->_lineNumber][$this->_section]['fontSize']=$section['fontSize']; $this->_text[$this->_lineNumber][$this->_section]['width']=$section['width']; - + //if we haven't set a width, do so and initialize it to the width of this piece of text, //otherwise just add the width to this piece of text. if (isset($this->_text[$this->_lineNumber]['width'])) { @@ -260,8 +279,7 @@ if ($this->isAutoWidth()) { $this->_autoWidth = max($this->_text[$this->_lineNumber]['width'],$this->_autoWidth); } - - + //calculate the max height of this line if we have an auto-size height cell if ($this->isAutoHeight()) { if (isset($this->_text[$this->_lineNumber]['height'])) { @@ -295,7 +313,7 @@ $this->_lineNumber++; $this->_text[$this->_lineNumber]=array(); $this->_text[$this->_lineNumber][0]['text']=''; - $this->_text[$this->_lineNumber][0]['encoding']=''; + $this->_text[$this->_lineNumber][0]['encoding']='ISO-8859-1'; $this->_text[$this->_lineNumber][0]['font']=$this->_font; $this->_text[$this->_lineNumber][0]['fontSize']=$this->_fontSize; $this->_text[$this->_lineNumber][0]['width']=0; @@ -316,7 +334,11 @@ * @return int Width of cell, in pixels, without the border. */ public function getWidth() { - return $this->_width; + if ($this->isAutoWidth()) { + return $this->_autoWidth; + } else { + return $this->_width; + } } /** @@ -325,9 +347,13 @@ * @return int Width of cell, in pixels, without the border. */ public function getHeight() { - return $this->_height; + if ($this->isAutoWidth()) { + return $this->_autoHeight; + } else { + return $this->_height; + } } - + public function getPosition() { return $this->_position; } @@ -509,7 +535,7 @@ if ($this->isAutoWidth()) { $this->_width=$this->_autoWidth; } - + //positions of the cell's box //initalize the diminsions to defaults @@ -557,7 +583,7 @@ $style->setFillColor(new Zend_Pdf_Color_RGB(0,0,0)); $this->_page->setStyle($style); } - + //draw every section of every page. for ($i=0;$i_text);$i++) { $currentX=0; @@ -598,6 +624,11 @@ private function _getTextWidth($textSection) { //make into a character array $charArray=array(); + + if (strlen($textSection['encoding']) > 0) { + $textSection['text'] = iconv($textSection['encoding'], 'ISO-8859-1', $textSection['text']); + } + for ($x=0;$x