Location: PHPKode > projects > pdfDOM > pdfdom-0.5/classes/class.textcontainer.php
<?php
/******************************************************************************
* File      	:	$RCSfile: class.textcontainer.php,v $
* Project   	:   pdfDOM
* Description	:   Text object
* Author    	:   Timo A. Hummel <hide@address.com>
* Created   	:   29.01.2004
* Modified  	:   $Date: 2005/06/23 13:36:22 $
*
* This file is part of the pdfDOM project.
*
* More information: 
* http://www.pdfdom.org
* http://www.sf.net/projects/pdfdom
*
* © four for business AG, www.4fb.de
*
* $Id: class.textcontainer.php,v 1.3 2005/06/23 13:36:22 timo_h Exp $
******************************************************************************/

class pdTextContainer extends pdContainer {

    // Attributes
    /**
     * Represent ...
     */
    var $_text;

    /**
     * Represent ...
     */
    var $_linkTo;

    /**
     * Represent ...
     */
    var $_expanding;

    /**
     * Represent ...
     */
    var $_pageTemplate;
    
    var $_remainingText;

	var $_textcolor;
	
	var $_size;
	
	var $_lineSpacing;
	
	var $_font;
	
	var $_bold;
	var $_italic;
	
	var $_alignment;
	
	function pdTextContainer ()
	{
		parent::pdContainer();
		$this->_type = "text";
		$this->setTextColor(0,0,0);	
		$this->setFontSize(10);
		$this->setLineSpacing(0.5);
		$this->setBackgroundMode(pdContainerBackgroundHollow);
		$this->setFont("Arial");
		$this->setBold(false);
		$this->setItalic(false);
		
		$this->_alignment = left;
	}
	
	function setBold ($bold = true)
	{
		$this->_bold = $bold;	
	}
	
	function setItalic ($italic = true)
	{
		$this->_italic = $italic;	
	}	
	
	function setFontColor ($r, $g, $b)
	{
		$this->setTextColor($r,$g,$b);	
	}
	
	function setFont( $font)
	{
		$this->_font = $font;	
	}
	
	function setTextColor ($r, $g, $b)
	{
		$this->_textcolor["r"] = $r;
		$this->_textcolor["g"] = $g;
		$this->_textcolor["b"] = $b;	
	}
    // Operations
    /**
     * Does ...
     * @param text 
     */
    function setText($text) 
    {
        $this->_text = $text;
    } // end operation setText

    /**
     * Does ...
     * @param container 
     */
    function linkTo(&$container) 
    {
        $this->_linkContainer = &$container;
    } // end operation linkTo

    /**
     * Does ...
     * @param expanding 
     */
    function setExpanding($expanding) 
    {
        $this->_expanding = $expanding;
    } // end operation setExpanding

    /**
     * Does ...
     * @param template 
     */
    function setPageTemplate($template) 
    {
        // your code here
    } // end operation setPageTemplate

	function customRender ()
	{
		$padding = $this->getPadding();
		
		$maxwidth = ($this->coordinates->getX1() -
					 $padding["right"]) -
					($this->coordinates->getX() +
					 $padding["left"]);
					 
		$this->_remainingText = "";
		
		$this->renderText($this->_text, 0, $maxwidth, $padding);
		 
	}
	
	function setLineSpacing ($spacing)
	{
		$this->_lineSpacing = $spacing;
	}
	
	function preRender ()
	{
		global $_pdfDom_PDF;
		
		$this->_remainingText = "";
		
		$padding = $this->getPadding();
		
		$maxwidth = ($this->coordinates->getX1() -
					 $padding["right"]) -
					($this->coordinates->getX() +
					 $padding["left"]);
			
		
		if ($this->_expanding == true)
		{
			
    		$wantedLines = $this->renderText($this->_text, 0, $maxwidth, $padding, true, true)+1;
    		$h = $this->coordinates->getY1() - $padding["botton"] - $this->coordinates->getY() + $padding["top"];
    		
    		$gotLines = floor($h / $_pdfDom_PDF->FontSize);

			
    		if ($wantedLines > $gotLines)
    		{
    			/* Expand the container */
    			$neededHeight = ceil(($wantedLines) * ($_pdfDom_PDF->FontSize + $this->_lineSpacing));
    			
    			/* Add top and bottom padding */
    			$neededHeight += $this->_padding["top"] + $this->_padding["bottom"];
    			$this->coordinates->setHeight($neededHeight);
    			
    			if ($this->coordinates->getY1() +
    				$this->_margin["bottom"] +
    				$this->_border["bottom"] >
    				$this->_page->coordinates->getY1() - 
    				$this->_page->_padding["bottom"])
    			{
    				$maxY1 = 	$this->_page->coordinates->getY1() -
    							$this->_page->_padding["bottom"] -
    							$this->_margin["bottom"] -
    							$this->_border["bottom"];
    							
    				$this->coordinates->setY1($maxY1);
    			}
    		}
		}
		
		$this->_remainingText = "";
		
		/* Do a prerender run again */
		$this->renderText($this->_text, 0, $maxwidth, $padding, true, false);

		/* If there is remaining text, call the event handler */
		if ($this->_remainingText != "")
		{
			$this->onRemainingText($this->_remainingText);
		}
				
		/* Check if we link to another container */
		if (is_object($this->_linkContainer))
		{
			$this->_linkContainer->setText($this->_remainingText);
		}
		
	}
	
	function onRemainingText ($text)
	{
	}
	
	function setFontSize ($size)
	{
		$this->_fontsize = $size;
	}
	
	function renderText ($text, $line, $maxwidth, $padding, $prerender = false, $ignoreHeight = false)
	{
		global $_pdfDom_PDF;
		
		if ($text == "")
		{
			return $line;
		}
		
		$_pdfDom_PDF->SetFontSize($this->_fontsize);
		$_pdfDom_PDF->setFont($this->_font);
		
		
		$x = $this->coordinates->getX() + $padding["left"];
		$y = $this->coordinates->getY() + $padding["top"]+(($line)*($_pdfDom_PDF->FontSize + $this->_lineSpacing));
		$x1 = $this->coordinates->getX1() - $padding["right"];
		$w = $this->coordinates->getWidth();

		$rects = $this->getSafeRects($x, $y, $x1, $y + $_pdfDom_PDF->FontSize);

		if ($prerender == false)
		{
			$_pdfDom_PDF->setDrawColor(255,0,0);
			$_pdfDom_PDF->setLineWidth(0.2);
		}
		
		foreach ($rects as $rect)
		{
			$remainingText = "";
			
			/* Add left and right padding */
			if ($rect[0] != $x)
			{
				$rect[0] = $rect[0] + $this->_padding["left"];
			}
			
			if ($rect[2] != $x1)
			{
				$rect[2] = $rect[2] - $this->_padding["right"];
			}
			
			if (($rect[3] >= $this->coordinates->getY1() - $padding["bottom"]) && $ignoreHeight == false)
			{
				return $line;
			}
			
			$maxwidth = $rect[2]-$rect[0];
		
    		$break = false;
    		for ($i=0;$i<strlen($text);$i++)
    		{
    			$ds = substr($text, 0, $i);
    			
    			if ($_pdfDom_PDF->getStringWidth($ds) >= $maxwidth)
    			{
    				$break = true;
    				$linebreak = false;
    				break;
    			}
    			
    			if (substr($text, $i,1) == "\n")
    			{
    				$linebreak = true;
    				$breakpos = $i;
    				$break = false;
    				break;
    			}
    		}
    		
    		
    		if ($break == true)
    		{
    			if (strlen($text) >= $i-1)
    			{
    				list($mangledText, $when) = capiStrTrimAfterWord($text,$i-1);
    				
   					$remainingText = substr($text, $when);
    		
   					$mangledText = trim($mangledText);
    			} else {
    				$mangledText = $text;
    				$remainingText = "";
    				$this->_remainingText = "";
    			}
    		} else {
    			if ($linebreak == true)
    			{
    				$mangledText = substr($text, 0, $breakpos);
    				$remainingText = substr($text, $breakpos+1);
    				$break = true;
    			} else {
        			$mangledText = trim($text);
        			$remainingText = "";
        			$this->_remainingText = "";
    			}
    		}
    		
    		
    		
    		/* Render the text */
    		if ($prerender == false)
    		{
    			$_pdfDom_PDF->SetTextColor($this->_textcolor["r"],$this->_textcolor["g"],$this->_textcolor["b"]);
    			
    			$_pdfDom_PDF->SetFont($this->_font, $this->_getStyleArguments());
    			
    			if ($this->_alignment == "right")
    			{
	    			$mtextwidth = $_pdfDom_PDF->getStringWidth(trim($mangledText));
	    			$_pdfDom_PDF->Text($rect[2] - $mtextwidth, $y+$_pdfDom_PDF->FontSize, trim($mangledText));
    			} else {
    				$_pdfDom_PDF->Text($rect[0], $y+$_pdfDom_PDF->FontSize, trim($mangledText));
    			}
    		}
    
    		if ($break == false)
    		{
    			return $line;
    		}
			
			if ($prerender == false)
			{
				//$_pdfDom_PDF->rect($rect[0], $rect[1], $rect[2]-$rect[0], $rect[3]-$rect[1]);
			}

			$text = $remainingText;

			
			$this->_remainingText = $remainingText;
			
		}
		
		return $this->renderText($remainingText, $line+1, $maxwidth, $padding, $prerender, $ignoreHeight);
		
		
		
	}
	
	function _getStyleArguments ()
	{
		$stylearguments = "";
		
		if ($this->_bold == true)
		{
			$stylearguments .= "B";	
		}
		
		if ($this->_italic == true)
		{
			$stylearguments .= "I";	
		}
		
		return ($stylearguments);
	}

} // end class pdTextContainer

function capiStrTrimAfterWord ($string, $maxlen)
{
	/* If the string is smaller than the maximum
       lenght, it makes no sense to process it any
       further. Return it. */
       
	if (strlen($string) <= $maxlen)
	{
		return array($string,$maxlen);
	}

	/* If the character after the $maxlen
       position is a space, we can return
       the string until $maxlen */	
	if (substr($string, $maxlen,1) == ' ')
	{
		return (array(substr($string, 0, $maxlen), $maxlen));
	}
	
	/* Cut the string up to $maxlen so we can
       use strrpos (reverse str position) */
	$cutted_string = substr($string, 0, $maxlen);
	
	/* Extract the end of the last word */
	$last_word_position = strrpos($cutted_string, ' ');
	
	if ($last_word_position === false || $last_word_position == 0)
	{
		return(array(substr($cutted_string,0,$maxlen),$maxlen));
	}
	
	if ($last_word_position >= $maxlen)
	{
		return array(substr($cutted_string, 0, $maxlen), $maxlen);			
	} else {
		$text = substr($cutted_string, 0, $last_word_position);
	}
	
		
	return array($text, $last_word_position);
}
?>
Return current item: pdfDOM