PageRenderTime 36ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/experimental/graphic/GraphicTextRun.php

http://zoop.googlecode.com/
PHP | 325 lines | 254 code | 29 blank | 42 comment | 10 complexity | e7367cbfd5549e8affbe14fc42b90db0 MD5 | raw file
  1<?php
  2class GraphicTextRun extends GraphicObject
  3{
  4	private $text;
  5	private $leftSpace;
  6	private $rightSpace;
  7	private $widthsCalced = false;
  8	private $length;
  9	
 10	function __construct($context)
 11	{
 12		parent::__construct($context);
 13		$this->text = '';
 14		$this->length = 0;
 15		$this->leftSpace = 0;
 16		$this->rightRight = 0;
 17		// $this->style = NULL;
 18		// $this->charWidths = array();
 19		// $this->totalWidth = -1;
 20	}
 21	
 22	public function setTextInfo($text)
 23	{
 24		//	consolidate white space.  Should this be done here or in the xml layer?
 25		//	this is really an xml thing not a graphics thing
 26		$content = ereg_replace("[[:space:]]+", ' ', $text['content']);		
 27		$this->text = $content;
 28		$this->length = strlen($this->text);
 29		$this->leftSpace = $text['leftTrim'];
 30		$this->rightSpace = $text['rightTrim'];
 31	}
 32	
 33	public function setStyle($style)
 34	{
 35		
 36	}
 37	
 38	function getLength()
 39	{
 40		return $this->length;
 41	}
 42	
 43	//
 44	//	the drawing functions
 45	//
 46	
 47	private function isBreakChar($inChar)
 48	{
 49		$breakChars = array(' ' , '-');
 50		if(in_array($inChar, $breakChars))
 51			return true;
 52		else
 53			return false;
 54	}
 55	
 56	private function calcCharWidths()
 57	{
 58		if($this->widthsCalced)
 59			return;
 60		// assert($this->style !== NULL);
 61		
 62		// $this->context->setTextFont($this->style);
 63		
 64		// echo_r($this->text);
 65		$this->charWidths = $this->context->getCharWidths($this->text);
 66		$this->totalWidth = array_sum($this->charWidths);
 67		
 68		// echo_r($this->charWidths);
 69		// echo_r($this->totalWidth);
 70		
 71		$this->widthsCalced = true;
 72	}
 73	
 74	function getPartWidth($start, $length)
 75	{
 76		// assert($this->style !== NULL);
 77		
 78		$total = 0;
 79		for($i = $start; $i < $start + $length; $i++)
 80			$total += $this->charWidths[$i];
 81		return $total;
 82		
 83		// assert($this->style !== NULL);
 84		// 
 85		// if($length == NULL)
 86		// 	$string = substr($this->text, $start);
 87		// else
 88		// 	$string = substr($this->text, $start, $length);
 89		// 
 90		// $this->context->setTextFont($this->style);
 91		// 
 92		// return $this->context->getStringWidth($string);
 93	}
 94	
 95	function getHeight()
 96	{
 97		return 12;
 98		// return $this->style->getTextSize() * $this->context->getLineHeightMultiplier();
 99	}
100	
101	//	this function tell us how much of the string will fit onto the current line
102	function getFitsLength($startPos, $remainingWidth)
103	{
104		$this->calcCharWidths();
105		
106		$lastGoodPos = -1;
107		$width = 0;
108		$textLen = strlen($this->text);
109		for($i = $startPos; $i < $textLen; $i++)
110		{
111			$charWidth = $this->charWidths[$i];
112			
113			if($width + $charWidth > $remainingWidth)
114			{
115				if($lastGoodPos == -1)
116					return $i - $startPos;
117				else
118					return $lastGoodPos - $startPos + 1;
119			}
120			
121			if($this->isBreakChar($this->text{$i}))
122			{
123				$lastGoodPos = $i;
124			}
125			
126			$width += $charWidth;
127		}
128		
129		return $textLen - $startPos;
130	}
131	
132	function drawPart($x, $y, $start = 0, $length = NULL)
133	{
134		// assert($this->style !== NULL);
135		// echo "GraphicTextRun::drawPart : $x, $y, $start, $length<br>";
136		
137		$height = $this->getHeight();
138		
139		/*
140		switch($height)
141		{
142			case 14:
143				$tweak = $height * 0.12;
144				break;
145			default:
146				$tweak = 0;
147				break;
148		}
149		*/
150		
151		// $y = $y - $tweak;
152		
153		if($length == NULL)
154			$string = substr($this->text, $start);
155		else
156			$string = substr($this->text, $start, $length);
157		
158		// $this->context->setTextColor($this->style->color[0], $this->style->color[1], $this->style->color[2]);
159		// $this->context->setTextFont($this->style);
160		$this->context->addText($x, $y + $this->getHeight(), $string);
161	}
162	
163	public function draw($x, $y, $width, $reallyDraw = true)
164	{
165		trigger_error("We don't actually use draw, we use draw part.");
166	}
167	
168	
169	//
170	//	debug stuff
171	//
172	
173	public function getObjectTree($indentLevel = 0)
174	{
175		$tabs = '';
176		for($i = 0; $i < $indentLevel; $i++)
177			$tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
178		
179		//	draw the close tag
180		echo $tabs . $this;
181	}
182	
183	
184	public function drawRenderTree($indentLevel = 0)
185	{
186		$tabs = '';
187		for($i = 0; $i < $indentLevel; $i++)
188			$tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
189		
190		//	draw the close tag
191		echo $tabs . $this;
192	}
193	
194	
195	public function __toString()
196	{
197		return '&lt;GraphicTextRun text=" ' . $this->text . '"/&gt;<br>';
198	}
199	
200	/*
201	var $style;
202	var $breakPoints;
203	var $charWidths;
204	var $totalWidth;
205		
206	function getUnwrappedWidth()
207	{
208		assert($this->style !== NULL);
209		assert($this->totalWidth > -1);
210		return $this->totalWidth;
211	}
212	
213	function getWidth()
214	{
215		assert($this->style !== NULL);
216		assert($this->totalWidth > -1);
217		return $this->totalWidth;
218		
219		// $this->context->setTextFont($this->style);
220		// return $this->context->getStringWidth($this->text);
221	}
222	
223	function setText($text)
224	{
225		assert($this->style !== NULL);
226		
227		//	we need to get rid of the excess white space in the middle
228		
229		$content = ereg_replace("[[:space:]]+", ' ', $text['content']);
230		
231		$this->text = $content;
232		
233		//echo 'START' . $this->text . "END<br>\n";
234		
235		$this->leftSpace = $text['leftTrim'];
236		$this->rightSpace = $text['rightTrim'];
237		
238		$this->calcCharWidths();
239	}
240	
241	function calcCharWidths()
242	{
243		assert($this->style !== NULL);
244		
245		$this->context->setTextFont($this->style);
246		
247		$len = strlen($this->text);
248		
249		$totalWidth = 0;
250		for($i = 0; $i < $len; $i++)
251		{
252			if($this->context->useKerningHack())
253			{
254				if($i == $len - 1)
255				{
256					$width = $this->context->getStringWidth(substr($this->text, $i, 1));	//	would it be better to use $this->text{$i} here?
257					$totalWidth += $width;
258					$this->charWidths[$i] = $width;				
259				}
260				else
261				{
262					$thisAndNextWidth = $this->context->getStringWidth(substr($this->text, $i, 2));
263					$justNextWidth = $this->context->getStringWidth(substr($this->text, $i+ 1, 1));
264					$width = $thisAndNextWidth - $justNextWidth;
265					
266					$totalWidth += $width;
267					$this->charWidths[$i] = $width;
268				}
269			}
270			else
271			{
272				$width = $this->context->getStringWidth(substr($this->text, $i, 1));	//	would it be better to use $this->text{$i} here?
273				$totalWidth += $width;
274				$this->charWidths[$i] = $width;
275			}
276		}
277		
278		$this->totalWidth = $totalWidth;
279	}
280			
281	function addLeftSpace()
282	{
283		$this->text = ' ' . $this->text;
284	}
285	
286	function getLeftSpace()
287	{
288		return $this->leftSpace;
289	}
290	
291	function getRightSpace()
292	{
293		return $this->rightSpace;
294	}
295	
296	function setStyle(&$style)
297	{
298		assert( is_a($style, 'GraphicTextStyle') );
299		
300		$this->style = $style;
301	}
302	
303	function isInline()
304	{
305		return 1;
306	}
307	
308	function draw($x, $y)
309	{
310		assert($this->style !== NULL);
311		
312		$this->context->setTextColor($this->style->color[0], $this->style->color[1], $this->style->color[2]);
313		$this->context->setTextFont($this->style);
314		$this->context->addText($x, $y + $this->getHeight(), $this->text);
315	}
316	
317	function __toString()
318	{
319		$s = "<" . get_class($this) . ">";
320		$s .= '~' . $this->text . '~';
321		$s .= "</" . get_class($this) . ">";
322		return $s;
323	}
324	*/
325}