1: <?php
2: /**
3: * Box.php
4: *
5: * @since 2011-05-23
6: * @category Library
7: * @package PdfPage
8: * @author Nicola Asuni <info@tecnick.com>
9: * @copyright 2011-2015 Nicola Asuni - Tecnick.com LTD
10: * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
11: * @link https://github.com/tecnickcom/tc-lib-pdf-page
12: *
13: * This file is part of tc-lib-pdf-page software library.
14: */
15:
16: namespace Com\Tecnick\Pdf\Page;
17:
18: use \Com\Tecnick\Pdf\Page\Exception as PageException;
19:
20: /**
21: * Com\Tecnick\Pdf\Page\Box
22: *
23: * @since 2011-05-23
24: * @category Library
25: * @package PdfPage
26: * @author Nicola Asuni <info@tecnick.com>
27: * @copyright 2011-2015 Nicola Asuni - Tecnick.com LTD
28: * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
29: * @link https://github.com/tecnickcom/tc-lib-pdf-page
30: */
31: abstract class Box extends \Com\Tecnick\Pdf\Page\Mode
32: {
33: /**
34: * Array of page box names
35: *
36: * @var array
37: */
38: public static $box = array(
39: 'MediaBox',
40: 'CropBox',
41: 'BleedBox',
42: 'TrimBox',
43: 'ArtBox'
44: );
45:
46: /**
47: * Swap X and Y coordinates of page boxes (change page boxes orientation).
48: *
49: * @param array $dims Array of page dimensions.
50: *
51: * @return array Page dimensions.
52: *
53: */
54: public function swapCoordinates(array $dims)
55: {
56: foreach (self::$box as $type) {
57: // swap X and Y coordinates
58: if (isset($dims[$type])) {
59: $tmp = $dims[$type]['llx'];
60: $dims[$type]['llx'] = $dims[$type]['lly'];
61: $dims[$type]['lly'] = $tmp;
62: $tmp = $dims[$type]['urx'];
63: $dims[$type]['urx'] = $dims[$type]['ury'];
64: $dims[$type]['ury'] = $tmp;
65: }
66: }
67: return $dims;
68: }
69:
70: /**
71: * Set page boundaries.
72: *
73: * @param array $dims Array of page dimensions to modify
74: * @param string $type Box type: MediaBox, CropBox, BleedBox, TrimBox, ArtBox.
75: * @param float $llx Lower-left x coordinate in user units.
76: * @param float $lly Lower-left y coordinate in user units.
77: * @param float $urx Upper-right x coordinate in user units.
78: * @param float $ury Upper-right y coordinate in user units.
79: * @param array $bci BoxColorInfo: guideline style (color, width, style, dash).
80: *
81: * @return array Page dimensions.
82: */
83: public function setBox($dims, $type, $llx, $lly, $urx, $ury, array $bci = array())
84: {
85: if (empty($dims)) {
86: // initialize array
87: $dims = array();
88: }
89: if (!in_array($type, self::$box)) {
90: throw new PageException('unknown page box type: '.$type);
91: }
92: $dims[$type]['llx'] = $llx;
93: $dims[$type]['lly'] = $lly;
94: $dims[$type]['urx'] = $urx;
95: $dims[$type]['ury'] = $ury;
96:
97: if (empty($bci)) {
98: // set default values
99: $bci = array(
100: 'color' => '#000000',
101: 'width' => (1.0 / $this->kunit),
102: 'style' => 'S', // S = solid; D = dash
103: 'dash' => array(3)
104: );
105: }
106: $dims[$type]['bci'] = $bci;
107:
108: return $dims;
109: }
110:
111: /**
112: * Initialize page boxes
113: *
114: * @param float $width Page width in points
115: * @param float $height Page height in points
116: *
117: * @return array Page boxes
118: */
119: public function setPageBoxes($width, $height)
120: {
121: $dims = array();
122: foreach (self::$box as $type) {
123: $dims = $this->setBox($dims, $type, 0, 0, $width, $height);
124: }
125: return $dims;
126: }
127:
128: /**
129: * Returns the PDF command to output the specified page boxes.
130: *
131: * @param array $dims Array of page dimensions.
132: *
133: * @return string
134: */
135: protected function getBox(array $dims)
136: {
137: $out = '';
138: foreach (self::$box as $box) {
139: if (empty($dims[$box])) {
140: // @codeCoverageIgnoreStart
141: continue;
142: // @codeCoverageIgnoreEnd
143: }
144: $out .= '/'.$box.' ['.sprintf(
145: '%F %F %F %F',
146: $dims[$box]['llx'],
147: $dims[$box]['lly'],
148: $dims[$box]['urx'],
149: $dims[$box]['ury']
150: ).']'."\n";
151: }
152: return $out;
153: }
154:
155: /**
156: * Returns the PDF command to output the specified page BoxColorInfo
157: *
158: * @param array $dims Array of page dimensions.
159: *
160: * @return string
161: */
162: protected function getBoxColorInfo(array $dims)
163: {
164: $out = '/BoxColorInfo <<'."\n";
165: foreach (self::$box as $box) {
166: if (empty($dims[$box])) {
167: // @codeCoverageIgnoreStart
168: continue;
169: // @codeCoverageIgnoreEnd
170: }
171: $out .= '/'.$box.' <<'."\n";
172: if (!empty($dims[$box]['bci']['color'])) {
173: $out .= '/C ['.$this->col->getPdfRgbComponents($dims[$box]['bci']['color']).']'."\n";
174: }
175: if (!empty($dims[$box]['bci']['width'])) {
176: $out .= '/W '.sprintf('%F', ($dims[$box]['bci']['width'] * $this->kunit))."\n";
177: }
178: if (!empty($dims[$box]['bci']['style'])) {
179: $mode = strtoupper($dims[$box]['bci']['style'][0]);
180: if ($mode !== 'D') {
181: $mode = 'S';
182: }
183: $out .= '/S /'.$mode."\n";
184: }
185: if (!empty($dims[$box]['bci']['dash'])) {
186: $out .= '/D [';
187: foreach ($dims[$box]['bci']['dash'] as $dash) {
188: $out .= sprintf(' %F', ((float) $dash * $this->kunit));
189: }
190: $out .= ' ]'."\n";
191: }
192: $out .= '>>'."\n";
193: }
194: $out = '>>'."\n";
195: return $out;
196: }
197: }
198: