BarcodeRenderer.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Pickup\Pdf;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Zend\Barcode\Renderer\AbstractRenderer;
  8. /**
  9. * Temando Pickup Pdf Barcode Renderer
  10. *
  11. * This renderer draws a ZF2 barcode on a ZF1 PDF document.
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. *
  18. */
  19. class BarcodeRenderer extends AbstractRenderer
  20. {
  21. /**
  22. * PDF resource.
  23. *
  24. * @var \Zend_Pdf
  25. */
  26. protected $resource = null;
  27. /**
  28. * Page number in PDF resource.
  29. *
  30. * @var int
  31. */
  32. private $page = 0;
  33. /**
  34. * Checking of parameters after all settings
  35. * @return void
  36. */
  37. protected function checkSpecificParams()
  38. {
  39. }
  40. /**
  41. * Initialize the rendering resource
  42. * @return void
  43. */
  44. protected function initRenderer()
  45. {
  46. if ($this->resource === null) {
  47. $this->resource = new \Zend_Pdf();
  48. $this->resource->pages[] = new \Zend_Pdf_Page(\Zend_Pdf_Page::SIZE_A4);
  49. }
  50. /** @var \Zend_Pdf_Page $pdfPage */
  51. $pdfPage = $this->resource->pages[$this->page];
  52. $this->adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth());
  53. }
  54. /**
  55. * Calculate the width of a string:
  56. * in case of using alignment parameter in drawText
  57. * @param string $text
  58. * @param \Zend_Pdf_Resource_Font $font
  59. * @param float $fontSize
  60. *
  61. * @return float
  62. * @throws \Zend_Pdf_Exception
  63. */
  64. public function widthForStringUsingFontSize($text, $font, $fontSize)
  65. {
  66. $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $text);
  67. $characters = [];
  68. for ($i = 0; $i < strlen($drawingString); $i ++) {
  69. $characters[] = (ord($drawingString[$i ++]) << 8) | ord($drawingString[$i]);
  70. }
  71. $glyphs = $font->glyphNumbersForCharacters($characters);
  72. $widths = $font->widthsForGlyphs($glyphs);
  73. $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
  74. return $stringWidth;
  75. }
  76. /**
  77. * Draw a polygon in the rendering resource
  78. * @param string $text
  79. * @param float $size
  80. * @param array $position
  81. * @param string $font
  82. * @param int $color
  83. * @param string $alignment
  84. * @param float|int $orientation
  85. *
  86. * @throws \Zend_Pdf_Exception
  87. */
  88. protected function drawText(
  89. $text,
  90. $size,
  91. $position,
  92. $font,
  93. $color,
  94. $alignment = 'center',
  95. $orientation = 0
  96. ) {
  97. /** @var \Zend_Pdf_Page $page */
  98. $page = $this->resource->pages[$this->page];
  99. $color = new \Zend_Pdf_Color_Rgb(
  100. (($color & 0xFF0000) >> 16) / 255.0,
  101. (($color & 0x00FF00) >> 8) / 255.0,
  102. ($color & 0x0000FF) / 255.0
  103. );
  104. $page->setLineColor($color);
  105. $page->setFillColor($color);
  106. $page->setFont(\Zend_Pdf_Font::fontWithPath($font), $size * $this->moduleSize * 1.2);
  107. $width = $this->widthForStringUsingFontSize(
  108. $text,
  109. \Zend_Pdf_Font::fontWithPath($font),
  110. $size * $this->moduleSize
  111. );
  112. $angle = pi() * $orientation / 180;
  113. $left = $position[0] * $this->moduleSize + $this->leftOffset;
  114. $top = $page->getHeight() - $position[1] * $this->moduleSize - $this->topOffset;
  115. switch ($alignment) {
  116. case 'center':
  117. $left -= ($width / 2) * cos($angle);
  118. $top -= ($width / 2) * sin($angle);
  119. break;
  120. case 'right':
  121. $left -= $width;
  122. break;
  123. }
  124. $page->rotate($left, $top, $angle);
  125. $page->drawText($text, $left, $top);
  126. $page->rotate($left, $top, - $angle);
  127. }
  128. /**
  129. * Draw a polygon in the rendering resource
  130. * @param array $points
  131. * @param int $color
  132. * @param bool $filled
  133. */
  134. protected function drawPolygon($points, $color, $filled = true)
  135. {
  136. /** @var \Zend_Pdf_Page $page */
  137. $page = $this->resource->pages[$this->page];
  138. $x = [];
  139. $y = [];
  140. foreach ($points as $point) {
  141. $x[] = $point[0] * $this->moduleSize + $this->leftOffset;
  142. $y[] = $page->getHeight() - $point[1] * $this->moduleSize - $this->topOffset;
  143. }
  144. if (count($y) == 4) {
  145. if ($x[0] != $x[3] && $y[0] == $y[3]) {
  146. $y[0] -= ($this->moduleSize / 2);
  147. $y[3] -= ($this->moduleSize / 2);
  148. }
  149. if ($x[1] != $x[2] && $y[1] == $y[2]) {
  150. $y[1] += ($this->moduleSize / 2);
  151. $y[2] += ($this->moduleSize / 2);
  152. }
  153. }
  154. $color = new \Zend_Pdf_Color_Rgb(
  155. (($color & 0xFF0000) >> 16) / 255.0,
  156. (($color & 0x00FF00) >> 8) / 255.0,
  157. ($color & 0x0000FF) / 255.0
  158. );
  159. $page->setLineColor($color);
  160. $page->setFillColor($color);
  161. $page->setLineWidth($this->moduleSize);
  162. $fillType = ($filled)
  163. ? \Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE
  164. : \Zend_Pdf_Page::SHAPE_DRAW_STROKE;
  165. $page->drawPolygon($x, $y, $fillType);
  166. }
  167. /**
  168. * Set a PDF resource to draw the barcode inside
  169. *
  170. * @param \Zend_Pdf $pdf
  171. * @param int $page
  172. * @return void
  173. */
  174. public function setResource(\Zend_Pdf $pdf, int $page = 0)
  175. {
  176. $this->resource = $pdf;
  177. $this->page = $page;
  178. if (empty($this->resource->pages)) {
  179. $this->page = 0;
  180. $this->resource->pages[] = new \Zend_Pdf_Page(\Zend_Pdf_Page::SIZE_A4);
  181. }
  182. }
  183. /**
  184. * Render the resource by sending headers and drawn resource.
  185. *
  186. * Note: This barcode renderer does not support direct output. Obtain the
  187. * drawn resource instead and proceed from there.
  188. *
  189. * @see draw
  190. * @return mixed
  191. * @throws LocalizedException
  192. */
  193. public function render()
  194. {
  195. throw new LocalizedException(__('Direct barcode output is not supported.'));
  196. }
  197. }