PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/experimental/graphic/GraphicParser.php

http://zoop.googlecode.com/
PHP | 147 lines | 110 code | 19 blank | 18 comment | 34 complexity | 9ee8d9d00eac39937c8627fb98ac0349 MD5 | raw file
  1<?php
  2class GraphicParser extends XmlParser
  3{
  4	private $context;
  5	private $pageSize;
  6	private $pageOrientation;
  7	private $documentSize = PdfContext::sizeA4;
  8	
  9	function __construct($contextType)
 10	{
 11		parent::__construct();
 12		
 13		switch($contextType)
 14		{
 15			case 'ZendPdf':
 16				$this->context = new ZendPdfContext();
 17				break;
 18			
 19			default:
 20				trigger_error("unhandled context type specified: $contextType");
 21				break;
 22		}
 23	}
 24	
 25	function parseText($xml)
 26	{
 27		$document = parent::parseText($xml);
 28		$document->getContext()->init($this->documentSize);
 29		$document->setMaxWidth($document->getContext()->getWidth());
 30		return $document;
 31	}
 32	
 33	protected function initRootContainer()
 34	{
 35		return new GraphicDocument($this->context);
 36	}
 37	
 38	protected function initExtra()
 39	{
 40		return new GraphicStyleStack();
 41	}
 42	
 43	function handleTextNode($node, $container, $styleStack)
 44	{
 45		$textRun = $container->getNewTextRun();
 46		echo_r($node->getTextContent());
 47		$textRun->setTextInfo($node->getTextContent());
 48		// echo $textRun;
 49		$textRun->setStyle($styleStack->getTopStyle());
 50		
 51		// echo 'handleTextNode<br>';
 52		// $this->container->drawRenderTree();
 53		
 54		// echo_r($container);
 55		// echo_r($node->getTextContent());
 56		// echo_r($extra);
 57		// die();
 58	}
 59	
 60	protected function preNodeHandler($node, $container, $styleStack)
 61	{
 62		$topStyle = $styleStack->cloneTop();
 63		$this->applyStyleInfo($node, $topStyle);
 64	}
 65	
 66	protected function postNodeHandler($node, $container, $styleStack)
 67	{
 68		$styleStack->pop();
 69	}
 70	
 71	private function applyStyleInfo($node, $style)
 72	{
 73		if($node->hasAttribute('style'))
 74		{
 75			$styleText = $node->getAttribute('style');
 76			$styleAttributes = explode(';', $style);
 77
 78			$styleInfo = array();
 79			foreach($styleAttributes as $thisAttribute)
 80			{
 81				if(trim($thisAttribute))
 82				{
 83					$styleAttParts = explode(':', $thisAttribute);
 84					$styleAttName = strtolower(trim($styleAttParts[0]));
 85					$styleAttValue = strtolower(trim($styleAttParts[1]));
 86					$styleInfo[$styleAttName] = $styleAttValue;
 87				}
 88			}
 89
 90			$style->add($styleInfo);
 91		}
 92	}
 93	
 94	protected function handleMeta($node, $container, $extra)
 95	{
 96		if(strtolower($node->getAttribute('name')) == 'pagesize')
 97		{
 98			if(strtolower($node->getAttribute('content')) == 'a4')
 99				$this->pageSize = 'a4';
100			if(strtolower($node->getAttribute('content')) == 'letter')
101				$this->pageSize = 'letter';
102			else
103				$this->documentSize = $node->getAttribute('content');
104		}
105		else if(strtolower($node->getAttribute('name')) == 'orientation')
106		{
107			if(strtolower($node->getAttribute('content')) == 'portrait')
108				$this->pageOrientation = 'portrait';
109			if(strtolower($node->getAttribute('content')) == 'landscape')
110				$this->pageOrientation = 'landscape';
111			else
112				trigger_error("invalid orientation: " . $node->getAttribute('content'));
113		}
114		
115		if($this->pageSize == 'a4' && $pageOrientation == 'portrait')
116			$this->documentSize = PdfContext::sizeA4;
117		else if($this->pageSize == 'a4' && $pageOrientation == 'landscape')
118			$this->documentSize = PdfContext::sizeA4Landscape;
119		else if($this->pageSize == 'letter' && $pageOrientation == 'portrait')
120			$this->documentSize = PdfContext::sizeLetter;
121		else if($this->pageSize == 'letter' && $pageOrientation == 'landscape')
122			$this->documentSize = PdfContext::sizeLetterLandscape;
123	}
124	
125	protected function handleDiv($node, $container, $extra)
126	{
127		$div = $container->getNewDiv();
128		return $div;
129	}
130	
131	protected function handleB($node, $container, $extra)
132	{
133		return $container;
134	}
135	
136	/*
137	private function __call($name, $arguments)
138	{
139		if(str_prefix($name, 'handle'))
140		{
141			
142		}
143		
144		trigger_error("method call to $name not handled");
145	}
146	*/
147}