PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/framework/experimental/graphic/ZendPdfContext.php

http://zoop.googlecode.com/
PHP | 292 lines | 75 code | 19 blank | 198 comment | 3 complexity | 708f2dc5302337ff51950d5cf6e8ef69 MD5 | raw file
  1<?php
  2class ZendPdfContext extends PdfContext
  3{
  4	private $zpdf;
  5	private $curPage;
  6	private $curFontName;
  7	private $curFontSize;
  8    
  9	public function init($size = PdfContext::sizeA4, $fontName = PdfContext::fontHelvetica, $fontSize = 12)
 10	{
 11		//	extract the dimentions
 12		$this->assignSize($size);
 13		
 14		//	create the pdf object
 15		$this->zpdf = new Zend_Pdf();
 16		
 17		// create the first page
 18		$this->newPage();
 19		
 20		// Set font 
 21		$this->setFont($fontName, $fontSize);
 22	}
 23	
 24	public function assignSize($size)
 25	{
 26		parent::assignSize($size);
 27	}
 28	
 29	private function fixy(&$x, &$y)
 30	{
 31		$y = $this->height - $y;
 32	}
 33	
 34	public function newPage($size = NULL)
 35	{
 36		if($size)
 37			$this->assignSize($size);
 38		
 39		$sizeString = $this->width . ':' . $this->height . ':';
 40		
 41		$this->curPage = $this->zpdf->newPage($sizeString); 
 42		$this->zpdf->pages[] = $this->curPage;
 43	}
 44	
 45	public function setFont($fontName, $fontSize)
 46	{
 47		$this->curFontName = $fontName;
 48		$this->curFontSize = $fontSize;
 49		$this->curPage->setFont(Zend_Pdf_Font::fontWithName($fontName), $fontSize);
 50	}
 51	
 52	public function addText($x, $y, $text)
 53	{
 54		$this->fixy($x, $y);
 55		$this->curPage->drawText($text, $x, $y);
 56	}
 57	
 58	public function getCharWidths($string)
 59	{
 60		//	I don't know how  intensive creating these font objects is but this is probably something that we should cache
 61		//	in a static class variable
 62		// var_dump($this->curFontName);
 63		$font = Zend_Pdf_Font::fontWithName($this->curFontName);
 64		
 65		$drawingString = iconv('', 'UTF-16BE', $string);
 66	    $characters = array();
 67	    for ($i = 0; $i < strlen($drawingString); $i++) {
 68	        $characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
 69	    }
 70		
 71		$glyphs = $font->cmap->glyphNumbersForCharacters($characters);
 72		// echo_r($glyphs);
 73		$widths = $font->widthsForGlyphs($glyphs);
 74		foreach($widths as $index => $width)
 75			$widths[$index] = ($width / $font->getUnitsPerEm()) * $this->curFontSize;
 76		
 77		return $widths;
 78	}
 79	
 80	function stringWidth($string, $fontName = PdfContext::fontHelvetica, $fontSize = 12)
 81	{
 82		$font = Zend_Pdf_Font::fontWithName($fontName);
 83	    $drawingString = iconv('', 'UTF-16BE', $string);
 84	    $characters = array();
 85	    for ($i = 0; $i < strlen($drawingString); $i++) {
 86	        $characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
 87	    }
 88	    $glyphs = $font->cmap->glyphNumbersForCharacters($characters);
 89	    $widths = $font->widthsForGlyphs($glyphs);
 90	    $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
 91	    return $stringWidth;
 92	}
 93	
 94	public function display()
 95	{
 96		// Get PDF document as a string 
 97		$pdfData = $this->zpdf->render(); 
 98
 99		// send header for the browser
100		Header('Content-Type: application/pdf');
101		echo $pdfData;
102	}
103	
104	/*
105	function getPageWidth()
106	{
107		return $this->width;
108	}
109	
110	function getPageHeight()
111	{
112		return $this->height;
113	}
114	
115	function addColor($name, $r, $g, $b)
116	{
117		$this->colors[$name] = array($r, $g, $b);
118	}
119	
120	
121	function setCurLineColor($colorName)
122	{
123		parent::setCurLineColor($colorName);
124		$color = $this->_getCurLineColor();
125		$this->fpdf->setDrawColor($color[0], $color[1], $color[2]);
126	}
127	
128	function setCurFillColor($colorName)
129	{
130		parent::setCurFillColor($colorName);
131		$color = $this->_getCurFillColor();
132		$this->fpdf->setFillColor($color[0], $color[1], $color[2]);
133	}
134	
135	function setCurTextColor($colorName)
136	{
137		parent::setCurTextColor($colorName);
138		$color = $this->_getCurTextColor();
139		$this->fpdf->setTextColor($color[0], $color[1], $color[2]);
140	}
141	
142	function breakPage()
143	{
144		$this->fpdf->AddPage();
145	}
146	
147	function getStringWidth($string)
148	{
149		return $this->fpdf->GetStringWidth($string);
150	}
151	
152	function addLine($x1, $y1, $x2, $y2, $lineWidth = 0.57)
153	{
154		$this->fpdf->SetLineWidth($lineWidth);
155		$this->fpdf->Line($x1, $y1, $x2, $y2);
156		//$this->fpdf->SetLineWidth(0.57);
157	}
158	
159	function addHorizLine($left, $right, $top, $lineWidth = 0.57)
160	{
161		$halfLineWidth = $lineWidth / 2;
162		
163		$x1 = $left + $halfLineWidth;
164		$x2 = $right - $halfLineWidth;
165		$y1 = $y2 = $top + $halfLineWidth;
166		$this->fpdf->SetLineWidth($lineWidth);
167		$this->fpdf->Line($x1, $y1, $x2, $y2);
168		//$this->fpdf->SetLineWidth(0.57);
169	}
170	
171	function addVertLine($top, $bottom, $left, $lineWidth = 0.57)
172	{
173		
174		$halfLineWidth = $lineWidth / 2;
175		
176		$x1 = $x2 = $left + $halfLineWidth;
177		$y1 = $top + $halfLineWidth;
178		$y2 = $bottom - $halfLineWidth;
179		$this->fpdf->SetLineWidth($lineWidth);
180		$this->fpdf->Line($x1, $y1, $x2, $y2);
181		//$this->fpdf->SetLineWidth(0.57);
182	}
183	
184	function addRect($x, $y, $w, $h, $style = 'D')
185	{
186		$this->fpdf->Rect($x, $y, $w, $h, $style);
187	}
188	
189	function addImage($file, $x, $y, $w=0, $h=0)
190	{
191		$this->fpdf->Image($file, $x, $y, $w, $h);
192	}
193	
194	function addPolygon($points, $style = 'D')
195	{
196		$this->fpdf->Polygon($points, $style);
197	}
198	
199	function addCircle($x, $y, $r, $style='D')
200	{
201		$this->fpdf->Circle($x, $y, $r, $style );
202	}
203	
204	function addEllipse($x, $y, $rx, $ry, $style='D')
205	{
206		$this->fpdf->Ellipse($x, $y, $rx, $ry, $style);
207	}
208	
209	//	This uses the tweaked logic of imagearc and imagefilledarc below for evaluating angles
210	//		not logic that would actually draw the angles passed in.  This may need to be tweaked
211	//		for compatibility with other context modules.
212	
213	function addArc($x, $y, $w, $h, $startAngle, $endAngle, $style='D')
214	{
215		$this->fpdf->Arc($x, $y, $w / 2, $h / 2, $startAngle, $endAngle, $style);
216	}
217	
218	//	This uses the tweaked logic of imagearc and imagefilledarc for evaluating angles
219	//		not logic that would actually draw the angles passed in.  This may need to be tweaked
220	//		for compatibility with other context modules.
221
222	function addCylinderSlice($cx, $cy, $w, $h, $sTheta, $eTheta, $depth, $style='D')
223	{
224		$this->fpdf->CylinderSlice($cx, $cy, $w / 2, $h / 2, $sTheta, $eTheta, $depth, $style);
225	}
226	
227	function setTextFont($newFontStyle)
228	{
229		assert( is_a($newFontStyle, 'GraphicTextStyle') );
230		
231		$style = '';
232		if($newFontStyle->getUnderline())
233			$style .= 'U';
234		if($newFontStyle->getBold())
235			$style .= 'B';
236		if($newFontStyle->getItalics())
237			$style .= 'I';
238		
239		$this->fpdf->SetFont($newFontStyle->getFont(), $style, $newFontStyle->getTextSize());
240	}
241	
242	function setTextSize($size)
243	{
244		parent::setTextSize($size);
245		$this->fpdf->SetFontSize($size);
246	}
247	
248	function setTextStyle($style)
249	{
250		parent::setTextStyle($style);
251		$this->setCurFont();
252	}
253	
254	function setTextFontName($fontName)
255	{
256		parent::setTextFontName($fontname);
257		$this->setCurFont();
258	}
259	
260	function setCurFont()
261	{
262		$this->fpdf->SetFont($this->getTextFontName(), $this->getTextStyle(), $this->getTextSize());
263	}
264	
265	function setTextColor($r, $g, $b)
266	{
267		$this->fpdf->SetTextColor($r, $g, $b);
268	}
269	
270	function setLineWidth($lineWidth)
271	{
272		$this->fpdf->SetLineWidth($lineWidth);
273	}
274	
275	function addRaw($rawData)
276	{
277		$this->fpdf->Raw($rawData);
278	}
279	
280	function save($filename)
281	{
282		//	we should all them to set the name here
283		$this->fpdf->Output($filename, 'F');
284	}
285	
286	function display()
287	{
288		//	we should all them to set the name here
289		$this->fpdf->Output('', 'I');
290	}
291	*/
292}