Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Barcode / Renderer / Pdf.php
blobc87bd2d53288791cf062833b4395415e3b2629cd
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Barcode\Renderer;
12 use ZendPdf\Color;
13 use ZendPdf\Font;
14 use ZendPdf\Page;
15 use ZendPdf\PdfDocument;
17 /**
18 * Class for rendering the barcode in PDF resource
20 class Pdf extends AbstractRenderer
22 /**
23 * PDF resource
24 * @var PdfDocument
26 protected $resource = null;
28 /**
29 * Page number in PDF resource
30 * @var int
32 protected $page = 0;
34 /**
35 * Module size rendering
36 * @var float
38 protected $moduleSize = 0.5;
40 /**
41 * Set a PDF resource to draw the barcode inside
43 * @param PdfDocument $pdf
44 * @param int $page
45 * @return Pdf
47 public function setResource(PdfDocument $pdf, $page = 0)
49 $this->resource = $pdf;
50 $this->page = intval($page);
52 if (!count($this->resource->pages)) {
53 $this->page = 0;
54 $this->resource->pages[] = new Page(
55 Page::SIZE_A4
58 return $this;
61 /**
62 * Check renderer parameters
64 * @return void
66 protected function checkSpecificParams()
70 /**
71 * Draw the barcode in the PDF, send headers and the PDF
72 * @return mixed
74 public function render()
76 $this->draw();
77 header("Content-Type: application/pdf");
78 echo $this->resource->render();
81 /**
82 * Initialize the PDF resource
83 * @return void
85 protected function initRenderer()
87 if ($this->resource === null) {
88 $this->resource = new PdfDocument();
89 $this->resource->pages[] = new Page(
90 Page::SIZE_A4
94 $pdfPage = $this->resource->pages[$this->page];
95 $this->adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth());
98 /**
99 * Draw a polygon in the rendering resource
100 * @param array $points
101 * @param int $color
102 * @param bool $filled
104 protected function drawPolygon($points, $color, $filled = true)
106 $page = $this->resource->pages[$this->page];
107 foreach ($points as $point) {
108 $x[] = $point[0] * $this->moduleSize + $this->leftOffset;
109 $y[] = $page->getHeight() - $point[1] * $this->moduleSize - $this->topOffset;
111 if (count($y) == 4) {
112 if ($x[0] != $x[3] && $y[0] == $y[3]) {
113 $y[0] -= ($this->moduleSize / 2);
114 $y[3] -= ($this->moduleSize / 2);
116 if ($x[1] != $x[2] && $y[1] == $y[2]) {
117 $y[1] += ($this->moduleSize / 2);
118 $y[2] += ($this->moduleSize / 2);
122 $color = new Color\Rgb(
123 (($color & 0xFF0000) >> 16) / 255.0,
124 (($color & 0x00FF00) >> 8) / 255.0,
125 ($color & 0x0000FF) / 255.0
128 $page->setLineColor($color);
129 $page->setFillColor($color);
130 $page->setLineWidth($this->moduleSize);
132 $fillType = ($filled)
133 ? Page::SHAPE_DRAW_FILL_AND_STROKE
134 : Page::SHAPE_DRAW_STROKE;
136 $page->drawPolygon($x, $y, $fillType);
140 * Draw a polygon in the rendering resource
141 * @param string $text
142 * @param float $size
143 * @param array $position
144 * @param string $font
145 * @param int $color
146 * @param string $alignment
147 * @param float $orientation
149 protected function drawText(
150 $text,
151 $size,
152 $position,
153 $font,
154 $color,
155 $alignment = 'center',
156 $orientation = 0
158 $page = $this->resource->pages[$this->page];
159 $color = new Color\Rgb(
160 (($color & 0xFF0000) >> 16) / 255.0,
161 (($color & 0x00FF00) >> 8) / 255.0,
162 ($color & 0x0000FF) / 255.0
165 $page->setLineColor($color);
166 $page->setFillColor($color);
167 $page->setFont(Font::fontWithPath($font), $size * $this->moduleSize * 1.2);
169 $width = $this->widthForStringUsingFontSize(
170 $text,
171 Font::fontWithPath($font),
172 $size * $this->moduleSize
175 $angle = pi() * $orientation / 180;
176 $left = $position[0] * $this->moduleSize + $this->leftOffset;
177 $top = $page->getHeight() - $position[1] * $this->moduleSize - $this->topOffset;
179 switch ($alignment) {
180 case 'center':
181 $left -= ($width / 2) * cos($angle);
182 $top -= ($width / 2) * sin($angle);
183 break;
184 case 'right':
185 $left -= $width;
186 break;
188 $page->rotate($left, $top, $angle);
189 $page->drawText($text, $left, $top);
190 $page->rotate($left, $top, - $angle);
194 * Calculate the width of a string:
195 * in case of using alignment parameter in drawText
196 * @param string $text
197 * @param Font $font
198 * @param float $fontSize
199 * @return float
201 public function widthForStringUsingFontSize($text, $font, $fontSize)
203 $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $text);
204 $characters = array();
205 for ($i = 0, $len = strlen($drawingString); $i < $len; $i++) {
206 $characters[] = (ord($drawingString[$i ++]) << 8) | ord($drawingString[$i]);
208 $glyphs = $font->glyphNumbersForCharacters($characters);
209 $widths = $font->widthsForGlyphs($glyphs);
210 $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
211 return $stringWidth;