PageRenderTime 78ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/application/library/Zend/My/Pdf/Page.php

https://github.com/sistemaskd/appcredIntegradora
PHP | 340 lines | 168 code | 51 blank | 121 comment | 18 complexity | 4583d7164c74091e759faa4b6e22c763 MD5 | raw file
  1. <?php
  2. class My_Pdf_Page extends Zend_Pdf_Page {
  3. /*
  4. * If page contains pagebreaks, pages are stored here
  5. */
  6. private $_pages=array();
  7. private $_margin;
  8. private $_defaultStyle;
  9. /**
  10. * Get Default Page Style
  11. *
  12. * @return Zend_Pdf_Style
  13. */
  14. public function getDefaultStyle(){
  15. return $this->_defaultStyle;
  16. }
  17. /**
  18. * Get all pages for this page (page overflows)
  19. *
  20. * @return array pages
  21. */
  22. public function getPages(){
  23. if(count($this->_pages)>0){
  24. return array_merge(array($this),$this->_pages);
  25. }
  26. else{
  27. return false;
  28. }
  29. }
  30. /**
  31. * Set page margins
  32. *
  33. * @param array(TOP,RIGHT,BOTTOM,LEFT)
  34. */
  35. public function setMargins($margin=array()){
  36. $this->_margin=$margin;
  37. }
  38. /**
  39. * Get Page Width
  40. *
  41. * @param bool $intContentArea
  42. * @return int
  43. */
  44. public function getWidth($intContentArea=false){
  45. $width=parent::getWidth();
  46. if($intContentArea){
  47. $width-=$this->_margin[My_Pdf::LEFT];
  48. $width-=$this->_margin[My_Pdf::RIGHT];
  49. }
  50. return $width;
  51. }
  52. /**
  53. * Get a Page margin
  54. *
  55. * @param My_Pdf::Position $position
  56. * @return int margin
  57. */
  58. public function getMargin($position){
  59. return $this->_margin[$position];
  60. }
  61. /**
  62. * Get Page Margins
  63. *
  64. * @return array(TOP,RIGHT,BOTTOM,LEFT)
  65. */
  66. public function getMargins(){
  67. return $this->_margin;
  68. }
  69. /**
  70. * Set Page Font
  71. *
  72. * @param Zend_Pdf_Resource_Font $font
  73. * @param int $fontSize
  74. */
  75. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize=10){
  76. $this->_font=$font;
  77. $this->_fontSize=$fontSize;
  78. parent::setFont($font,$fontSize);
  79. }
  80. public function __construct($param1, $param2 = null, $param3 = null){
  81. parent::__construct ( $param1, $param2, $param3 );
  82. $style=new Zend_Pdf_Style();
  83. $style->setLineColor(new Zend_Pdf_Color_Html("#000000"));
  84. $style->setFillColor(new Zend_Pdf_Color_Html("#000000"));
  85. $style->setLineWidth(0.5);
  86. $font = Zend_Pdf_Font::fontWithName ( Zend_Pdf_Font::FONT_COURIER );
  87. $style->setFont($font,10);
  88. $style->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID);
  89. $this->_defaultStyle=$style;
  90. $this->setStyle($style);
  91. }
  92. /**
  93. * Add a table to a page
  94. *
  95. * @param My_Pdf_Table $table
  96. * @param int $posX
  97. * @param int $posY
  98. */
  99. public function addTable(My_Pdf_Table $table,$posX,$posY,$inContentArea=true){
  100. //render table --> check for new pages
  101. $pages=$table->render($this,$posX,$posY,$inContentArea);
  102. if(is_array($pages))
  103. $this->_pages+=$pages;
  104. }
  105. /**
  106. * Get text properties (width, height, [#lines using $max Width]), and warps lines
  107. *
  108. * @param string $text
  109. * @param int $posX
  110. * @param int $posY
  111. * @param int $maxWidth
  112. */
  113. public function getTextProperties($text, $maxWidth=null) {
  114. $lines=$this->_textLines($text, $maxWidth);
  115. return array (
  116. 'text_width' => $lines['text_width'],
  117. 'max_width'=> $lines['max_width'],
  118. 'height'=>($this->getFontHeight()*count ( $lines['lines'] )),
  119. 'lines' => $lines['lines']
  120. );
  121. }
  122. /**
  123. * Draw Line
  124. *
  125. * @param int $x1
  126. * @param int $y1
  127. * @param int $x2
  128. * @param int $y2
  129. * @param bool $inContentArea
  130. */
  131. public function drawLine($x1,$y1,$x2,$y2,$inContentArea=true){
  132. //move origin
  133. if($inContentArea){
  134. $y1 = $this->getHeight()- $y1 - $this->getMargin(My_Pdf::TOP);
  135. $y2 = $this->getHeight()- $y2 - $this->getMargin(My_Pdf::TOP);
  136. $x1 = $x1 + $this->getMargin(My_Pdf::LEFT);
  137. $x2 = $x2 + $this->getMargin(My_Pdf::LEFT);
  138. }
  139. parent::drawLine($x1,$y1,$x2,$y2);
  140. }
  141. /**
  142. * Draw Text
  143. *
  144. * @param string $text
  145. * @param int $x1
  146. * @param int $y1
  147. * @param string $charEncoding
  148. * @param bool $inContentArea
  149. */
  150. public function drawText($text,$x1,$y1,$charEncoding="",$inContentArea=true){
  151. //move origin
  152. if($inContentArea){
  153. $y1 = $this->getHeight()- $y1 - $this->getMargin(My_Pdf::TOP);
  154. $x1 = $x1 + $this->getMargin(My_Pdf::LEFT);
  155. }
  156. parent::drawText($text,$x1,$y1,$charEncoding);
  157. }
  158. /**
  159. * Draw Rectangle
  160. *
  161. * @param int $x1
  162. * @param int $y1
  163. * @param int $x2
  164. * @param int $y2
  165. * @param string $filltype
  166. * @param bool $inContentArea
  167. */
  168. public function drawRectangle($x1,$y1,$x2,$y2,$filltype=null,$inContentArea=true){
  169. //move origin
  170. if($inContentArea){
  171. $y1 = $this->getHeight()- $y1 - $this->getMargin(My_Pdf::TOP);
  172. $y2 = $this->getHeight()- $y2 - $this->getMargin(My_Pdf::TOP);
  173. $x1 = $x1 + $this->getMargin(My_Pdf::LEFT);
  174. $x2 = $x2 + $this->getMargin(My_Pdf::LEFT);
  175. }
  176. parent::drawRectangle($x1,$y1,$x2,$y2,$filltype);
  177. }
  178. public function drawImage($image,$x1,$y1,$width,$height,$inContentArea=true){
  179. if($inContentArea){
  180. $y1 = $this->getHeight()- $y1 - $this->getMargin(My_Pdf::TOP)-$height;
  181. $x1 = $x1 + $this->getMargin(My_Pdf::LEFT);
  182. $y2=$y1+$height;
  183. $x2=$x1+$width;
  184. }
  185. parent::drawImage($image,$x1,$y1,$x2,$y2);
  186. }
  187. /**
  188. * Get Font Height
  189. *
  190. * @return int
  191. */
  192. public function getFontHeight(){
  193. $line_height=$this->getFont()->getLineHeight();
  194. $line_gap=$this->getFont()->getLineGap();
  195. $em=$this->getFont()->getUnitsPerEm();
  196. $size=$this->getFontSize();
  197. return ($line_height-$line_gap)/$em*$size;
  198. }
  199. /**
  200. * Returns the with of the text
  201. *
  202. * @param string $text
  203. * @return int $width
  204. */
  205. private function _getTextWidth($text) {
  206. $glyphs = array ();
  207. $em = $this->_font->getUnitsPerEm ();
  208. //get glyph for each character
  209. foreach ( range ( 0, strlen ( $text ) - 1 ) as $i ) {
  210. $glyphs [] = @ord ( $text [$i] );
  211. }
  212. $width = array_sum ( $this->_font->widthsForGlyphs ( $glyphs ) ) / $em * $this->_fontSize;
  213. return $width;
  214. }
  215. /**
  216. * Wrap text according to max width
  217. *
  218. * @param string $text
  219. * @param int $maxWidth
  220. * @return array lines
  221. */
  222. private function _wrapText($text,$maxWidth){
  223. $x_inc = 0;
  224. $curr_line = '';
  225. $words = explode ( ' ', trim ( $text ) );
  226. $space_width = $this->_getTextWidth ( ' ' );
  227. foreach ( $words as $word ) {
  228. //no new line found
  229. $width = $this->_getTextWidth ( $word );
  230. if (isset ( $maxWidth ) && ($x_inc + $width) <= $maxWidth) {
  231. //add word to current line
  232. $curr_line .= ' '.$word;
  233. $x_inc += $width + $space_width;
  234. } else {
  235. //store current line
  236. if (strlen( trim($curr_line,"\n") )>0)
  237. $lines [] = trim($curr_line);
  238. //new line
  239. $x_inc = 0; //reset position
  240. $curr_line = array (); //reset curr line
  241. //add word
  242. $curr_line = $word;
  243. $x_inc += $width + $space_width;
  244. }
  245. }
  246. //last line
  247. if (strlen( trim($curr_line,"\n") )>0) {
  248. $lines [] = trim($curr_line);
  249. }
  250. return $lines;
  251. }
  252. /**
  253. * Enter description here...
  254. *
  255. * @param string $text
  256. * @param int $maxWidth (optional, if not set (auto width) the max width is set by reference)
  257. * @return array line(text);
  258. */
  259. private function _textLines($text,$maxWidth=null){
  260. $trimmed_lines=array();
  261. $textWidth=0;
  262. $line_width=0;
  263. $lines=explode("\n",$text);
  264. $max_line_width=0;
  265. foreach ( $lines as $line ) {
  266. if(strlen($line)<=0) continue;
  267. $line_width=$this->_getTextWidth($line);
  268. if($maxWidth>0 && $line_width>$maxWidth){
  269. $new_lines=$this->_wrapText($line,$maxWidth);
  270. $trimmed_lines+=$new_lines;
  271. foreach ($new_lines as $nline) {
  272. $line_width=$this->_getTextWidth($nline);
  273. if($line_width>$max_line_width)
  274. $max_line_width=$line_width;
  275. }
  276. }
  277. else{
  278. $trimmed_lines[]=$line;
  279. }
  280. if($line_width>$max_line_width)
  281. $max_line_width=$line_width;
  282. }
  283. //set actual width of line
  284. if(is_null($maxWidth))
  285. $maxWidth=$max_line_width;
  286. $textWidth=$max_line_width;
  287. return array('lines'=>$trimmed_lines,'text_width'=>$textWidth,'max_width'=>$maxWidth);
  288. }
  289. }