1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
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@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Usa
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * DHL International (API v1.4) Label Creation
29: *
30: * @category Mage
31: * @package Mage_Usa
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page extends Zend_Pdf_Page
35: {
36: /**
37: * Text align constants
38: */
39: const ALIGN_RIGHT = 'right';
40: const ALIGN_LEFT = 'left';
41: const ALIGN_CENTER = 'center';
42:
43: /**
44: * Dhl International Label Creation Class Pdf Page constructor
45: * Create/Make a copy of pdf page
46: *
47: * @param Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page|string $param1
48: * @param null $param2
49: * @param null $param3
50: */
51: public function __construct($param1, $param2 = null, $param3 = null)
52: {
53: if ($param1 instanceof Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page
54: && $param2 === null && $param3 === null
55: ) {
56: $this->_contents = $param1->getContents();
57: }
58: parent::__construct($param1, $param2, $param3);
59: }
60:
61: /**
62: * Get PDF Page contents
63: *
64: * @return string
65: */
66: public function getContents()
67: {
68: return $this->_contents;
69: }
70:
71: /**
72: * Calculate the width of given text in points taking into account current font and font-size
73: *
74: * @param string $text
75: * @param Zend_Pdf_Resource_Font $font
76: * @param float $font_size
77: * @return float
78: */
79: public function getTextWidth($text, Zend_Pdf_Resource_Font $font, $font_size)
80: {
81: $drawing_text = iconv('', 'UTF-16BE', $text);
82: $characters = array();
83: for ($i = 0; $i < strlen($drawing_text); $i++) {
84: $characters[] = (ord($drawing_text[$i++]) << 8) | ord($drawing_text[$i]);
85: }
86: $glyphs = $font->glyphNumbersForCharacters($characters);
87: $widths = $font->widthsForGlyphs($glyphs);
88: $text_width = (array_sum($widths) / $font->getUnitsPerEm()) * $font_size;
89: return $text_width;
90: }
91:
92: /**
93: * Draw a line of text at the specified position.
94: *
95: * @param string $text
96: * @param float $x
97: * @param float $y
98: * @param string $charEncoding (optional) Character encoding of source text.
99: * Defaults to current locale.
100: * @param $align
101: * @throws Zend_Pdf_Exception
102: * @return Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page
103: */
104: public function drawText($text, $x, $y, $charEncoding = '', $align = self::ALIGN_LEFT)
105: {
106: $left = null;
107: switch ($align) {
108: case self::ALIGN_LEFT:
109: $left = $x;
110: break;
111:
112: case self::ALIGN_CENTER:
113: $textWidth = $this->getTextWidth($text, $this->getFont(), $this->getFontSize());
114: $left = $x - ($textWidth / 2);
115: break;
116:
117: case self::ALIGN_RIGHT:
118: $textWidth = $this->getTextWidth($text, $this->getFont(), $this->getFontSize());
119: $left = $x - $textWidth;
120: break;
121: }
122: return parent::drawText($text, $left, $y, $charEncoding);
123: }
124:
125: /**
126: * Draw a text paragraph taking into account the maximum number of symbols in a row.
127: * If line is longer - spit it.
128: *
129: * @param array $lines
130: * @param int $x
131: * @param int $y
132: * @param int $maxWidth - number of symbols
133: * @param string $align
134: * @throws Zend_Pdf_Exception
135: * @return float
136: */
137: public function drawLines($lines, $x, $y, $maxWidth, $align = self::ALIGN_LEFT)
138: {
139: foreach ($lines as $line) {
140: if (strlen($line) > $maxWidth) {
141: $subLines = Mage::helper('core/string')->str_split($line, $maxWidth, true, true);
142: $y = $this->drawLines(array_filter($subLines), $x, $y, $maxWidth, $align);
143: continue;
144: }
145: $this->drawText($line, $x, $y, null, $align);
146: $y -= ceil($this->getFontSize());
147: }
148: return $y;
149: }
150: }
151: