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