Svg.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Barcode
  17. * @subpackage Renderer
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Image.php 20366 2010-01-18 03:56:52Z ralph $
  21. */
  22. /** @see Zend_Barcode_Renderer_RendererAbstract*/
  23. require_once 'Zend/Barcode/Renderer/RendererAbstract.php';
  24. /**
  25. * Class for rendering the barcode as svg
  26. *
  27. * @category Zend
  28. * @package Zend_Barcode
  29. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Barcode_Renderer_Svg extends Zend_Barcode_Renderer_RendererAbstract
  33. {
  34. /**
  35. * Resource for the image
  36. * @var DOMDocument
  37. */
  38. protected $_resource = null;
  39. /**
  40. * Root element of the XML structure
  41. * @var DOMElement
  42. */
  43. protected $_rootElement = null;
  44. /**
  45. * Height of the rendered image wanted by user
  46. * @var integer
  47. */
  48. protected $_userHeight = 0;
  49. /**
  50. * Width of the rendered image wanted by user
  51. * @var integer
  52. */
  53. protected $_userWidth = 0;
  54. /**
  55. * Set height of the result image
  56. *
  57. * @param null|integer $value
  58. * @return Zend_Image_Barcode_Abstract
  59. * @throws Zend_Barcode_Renderer_Exception
  60. */
  61. public function setHeight($value)
  62. {
  63. if (!is_numeric($value) || intval($value) < 0) {
  64. require_once 'Zend/Barcode/Renderer/Exception.php';
  65. throw new Zend_Barcode_Renderer_Exception(
  66. 'Svg height must be greater than or equals 0'
  67. );
  68. }
  69. $this->_userHeight = intval($value);
  70. return $this;
  71. }
  72. /**
  73. * Get barcode height
  74. *
  75. * @return int
  76. */
  77. public function getHeight()
  78. {
  79. return $this->_userHeight;
  80. }
  81. /**
  82. * Set barcode width
  83. *
  84. * @param mixed $value
  85. * @return self
  86. * @throws Zend_Barcode_Renderer_Exception
  87. */
  88. public function setWidth($value)
  89. {
  90. if (!is_numeric($value) || intval($value) < 0) {
  91. require_once 'Zend/Barcode/Renderer/Exception.php';
  92. throw new Zend_Barcode_Renderer_Exception(
  93. 'Svg width must be greater than or equals 0'
  94. );
  95. }
  96. $this->_userWidth = intval($value);
  97. return $this;
  98. }
  99. /**
  100. * Get barcode width
  101. *
  102. * @return int
  103. */
  104. public function getWidth()
  105. {
  106. return $this->_userWidth;
  107. }
  108. /**
  109. * Set an image resource to draw the barcode inside
  110. *
  111. * @param $svg
  112. * @return Zend_Barcode_Renderer
  113. * @throws Zend_Barcode_Renderer_Exception
  114. */
  115. public function setResource($svg)
  116. {
  117. if (!$svg instanceof DOMDocument) {
  118. require_once 'Zend/Barcode/Renderer/Exception.php';
  119. throw new Zend_Barcode_Renderer_Exception(
  120. 'Invalid DOMDocument resource provided to setResource()'
  121. );
  122. }
  123. $this->_resource = $svg;
  124. return $this;
  125. }
  126. /**
  127. * Initialize the image resource
  128. *
  129. * @return void
  130. */
  131. protected function _initRenderer()
  132. {
  133. $barcodeWidth = $this->_barcode->getWidth(true);
  134. $barcodeHeight = $this->_barcode->getHeight(true);
  135. $backgroundColor = $this->_barcode->getBackgroundColor();
  136. $imageBackgroundColor = 'rgb(' . implode(', ', array(($backgroundColor & 0xFF0000) >> 16,
  137. ($backgroundColor & 0x00FF00) >> 8,
  138. ($backgroundColor & 0x0000FF))) . ')';
  139. $width = $barcodeWidth;
  140. $height = $barcodeHeight;
  141. if ($this->_userWidth && $this->_barcode->getType() != 'error') {
  142. $width = $this->_userWidth;
  143. }
  144. if ($this->_userHeight && $this->_barcode->getType() != 'error') {
  145. $height = $this->_userHeight;
  146. }
  147. if ($this->_resource === null) {
  148. $this->_resource = new DOMDocument('1.0', 'utf-8');
  149. $this->_resource->formatOutput = true;
  150. $this->_rootElement = $this->_resource->createElement('svg');
  151. $this->_rootElement->setAttribute('xmlns', "http://www.w3.org/2000/svg");
  152. $this->_rootElement->setAttribute('version', '1.1');
  153. $this->_rootElement->setAttribute('width', $width);
  154. $this->_rootElement->setAttribute('height', $height);
  155. $this->_appendRootElement('title',
  156. array(),
  157. "Barcode " . strtoupper($this->_barcode->getType()) . " " . $this->_barcode->getText());
  158. } else {
  159. $this->_readRootElement();
  160. $width = $this->_rootElement->getAttribute('width');
  161. $height = $this->_rootElement->getAttribute('height');
  162. }
  163. $this->_adjustPosition($height, $width);
  164. $this->_appendRootElement('rect',
  165. array('x' => $this->_leftOffset,
  166. 'y' => $this->_topOffset,
  167. 'width' => ($this->_leftOffset + $barcodeWidth - 1),
  168. 'height' => ($this->_topOffset + $barcodeHeight - 1),
  169. 'fill' => $imageBackgroundColor));
  170. }
  171. protected function _readRootElement()
  172. {
  173. if ($this->_resource !== null) {
  174. $this->_rootElement = $this->_resource->documentElement;
  175. }
  176. }
  177. /**
  178. * Append a new DOMElement to the root element
  179. *
  180. * @param string $tagName
  181. * @param array $attributes
  182. * @param string $textContent
  183. */
  184. protected function _appendRootElement($tagName, $attributes = array(), $textContent = null)
  185. {
  186. $newElement = $this->_createElement($tagName, $attributes, $textContent);
  187. $this->_rootElement->appendChild($newElement);
  188. }
  189. /**
  190. * Create DOMElement
  191. *
  192. * @param string $tagName
  193. * @param array $attributes
  194. * @param string $textContent
  195. * @return DOMElement
  196. */
  197. protected function _createElement($tagName, $attributes = array(), $textContent = null)
  198. {
  199. $element = $this->_resource->createElement($tagName);
  200. foreach ($attributes as $k =>$v) {
  201. $element->setAttribute($k, $v);
  202. }
  203. if ($textContent !== null) {
  204. $element->appendChild(new DOMText((string) $textContent));
  205. }
  206. return $element;
  207. }
  208. /**
  209. * Check barcode parameters
  210. *
  211. * @return void
  212. */
  213. protected function _checkParams()
  214. {
  215. $this->_checkDimensions();
  216. }
  217. /**
  218. * Check barcode dimensions
  219. *
  220. * @return void
  221. * @throws Zend_Barcode_Renderer_Exception
  222. */
  223. protected function _checkDimensions()
  224. {
  225. if ($this->_resource !== null) {
  226. $this->_readRootElement();
  227. $height = (float) $this->_rootElement->getAttribute('height');
  228. if ($height < $this->_barcode->getHeight(true)) {
  229. require_once 'Zend/Barcode/Renderer/Exception.php';
  230. throw new Zend_Barcode_Renderer_Exception(
  231. 'Barcode is define outside the image (height)'
  232. );
  233. }
  234. } else {
  235. if ($this->_userHeight) {
  236. $height = $this->_barcode->getHeight(true);
  237. if ($this->_userHeight < $height) {
  238. require_once 'Zend/Barcode/Renderer/Exception.php';
  239. throw new Zend_Barcode_Renderer_Exception(sprintf(
  240. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  241. $height,
  242. $this->_userHeight
  243. ));
  244. }
  245. }
  246. }
  247. if ($this->_resource !== null) {
  248. $this->_readRootElement();
  249. $width = $this->_rootElement->getAttribute('width');
  250. if ($width < $this->_barcode->getWidth(true)) {
  251. require_once 'Zend/Barcode/Renderer/Exception.php';
  252. throw new Zend_Barcode_Renderer_Exception(
  253. 'Barcode is define outside the image (width)'
  254. );
  255. }
  256. } else {
  257. if ($this->_userWidth) {
  258. $width = (float) $this->_barcode->getWidth(true);
  259. if ($this->_userWidth < $width) {
  260. require_once 'Zend/Barcode/Renderer/Exception.php';
  261. throw new Zend_Barcode_Renderer_Exception(sprintf(
  262. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  263. $width,
  264. $this->_userWidth
  265. ));
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Draw the barcode in the rendering resource
  272. * @return mixed
  273. */
  274. public function draw()
  275. {
  276. parent::draw();
  277. $this->_resource->appendChild($this->_rootElement);
  278. return $this->_resource;
  279. }
  280. /**
  281. * Draw and render the barcode with correct headers
  282. *
  283. * @return mixed
  284. */
  285. public function render()
  286. {
  287. $this->draw();
  288. header("Content-Type: image/svg+xml");
  289. echo $this->_resource->saveXML();
  290. }
  291. /**
  292. * Draw a polygon in the svg resource
  293. *
  294. * @param array $points
  295. * @param integer $color
  296. * @param boolean $filled
  297. */
  298. protected function _drawPolygon($points, $color, $filled = true)
  299. {
  300. $color = 'rgb(' . implode(', ', array(($color & 0xFF0000) >> 16,
  301. ($color & 0x00FF00) >> 8,
  302. ($color & 0x0000FF))) . ')';
  303. $orientation = $this->getBarcode()->getOrientation();
  304. $newPoints = array(
  305. $points[0][0] + $this->_leftOffset,
  306. $points[0][1] + $this->_topOffset,
  307. $points[1][0] + $this->_leftOffset,
  308. $points[1][1] + $this->_topOffset,
  309. $points[2][0] + $this->_leftOffset + cos(-$orientation),
  310. $points[2][1] + $this->_topOffset - sin($orientation),
  311. $points[3][0] + $this->_leftOffset + cos(-$orientation),
  312. $points[3][1] + $this->_topOffset - sin($orientation),
  313. );
  314. $newPoints = implode(' ', $newPoints);
  315. $attributes['points'] = $newPoints;
  316. $attributes['fill'] = $color;
  317. $this->_appendRootElement('polygon', $attributes);
  318. }
  319. /**
  320. * Draw a polygon in the svg resource
  321. *
  322. * @param string $text
  323. * @param float $size
  324. * @param array $position
  325. * @param string $font
  326. * @param integer $color
  327. * @param string $alignment
  328. * @param float|int $orientation
  329. */
  330. protected function _drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
  331. {
  332. $color = 'rgb(' . implode(', ', array(($color & 0xFF0000) >> 16,
  333. ($color & 0x00FF00) >> 8,
  334. ($color & 0x0000FF))) . ')';
  335. $attributes['x'] = $position[0] + $this->_leftOffset;
  336. $attributes['y'] = $position[1] + $this->_topOffset;
  337. //$attributes['font-family'] = $font;
  338. $attributes['color'] = $color;
  339. $attributes['font-size'] = $size * 1.2;
  340. switch ($alignment) {
  341. case 'left':
  342. $textAnchor = 'start';
  343. break;
  344. case 'right':
  345. $textAnchor = 'end';
  346. break;
  347. case 'center':
  348. default:
  349. $textAnchor = 'middle';
  350. }
  351. $attributes['style'] = 'text-anchor: ' . $textAnchor;
  352. $attributes['transform'] = 'rotate('
  353. . (- $orientation)
  354. . ', '
  355. . ($position[0] + $this->_leftOffset)
  356. . ', ' . ($position[1] + $this->_topOffset)
  357. . ')';
  358. $this->_appendRootElement('text', $attributes, $text);
  359. }
  360. }