1<?php
2/**
3 * pageFormat: class for handling page formats.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author LarsDW223
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11/**
12 * The pageFormat class
13 */
14class pageFormat
15{
16    var $format = 'A4';
17    var $orientation = 'portrait';
18
19    // Page parameters.
20    // All values are assumed to be in 'cm' units.
21    var $width = 21;
22    var $height = 29.7;
23    var $margin_top = 2;
24    var $margin_bottom = 2;
25    var $margin_left = 2;
26    var $margin_right = 2;
27
28    /**
29     * Return given page format parameters as a single string.
30     *
31     * @param string $format
32     * @param string $orientation
33     * @param string $margin_top
34     * @param string $margin_right
35     * @param string $margin_bottom
36     * @param string $margin_left
37     * @return string
38     */
39    public static function formatToString ($format, $orientation, $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
40        $margins = $margin_top.'-'.$margin_right.'-'.$margin_bottom.'-'.$margin_left;
41        $margins = str_replace (',', '', $margins);
42        $margins = str_replace ('.', '', $margins);
43        return $format.'-'.$orientation.'-'.$margins;
44    }
45
46    /**
47     * Return currently set format parameters as a single string.
48     *
49     * @return string
50     */
51    public function toString () {
52        $margins = $this->margin_top.'-'.$this->margin_right.'-'.$this->margin_bottom.'-'.$this->margin_left;
53        $margins = str_replace (',', '', $margins);
54        $margins = str_replace ('.', '', $margins);
55        return $this->format.'-'.$this->orientation.'-'.$margins;
56    }
57
58    /**
59     * Query format data. Returns data in assoziative array $dest.
60     * Returned fields are 'format', 'orientation', 'width', 'height',
61     * 'margin-top', 'margin-bottom', 'margin-left' and 'margin-right'.
62     * If $format is unknown, then format 'A4' will be assumed.
63     *
64     * @param string $format
65     * @param string $orientation
66     */
67    public static function queryFormat (&$dest, $format, $orientation='portrait', $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
68        switch ($format) {
69            case 'A6':
70                $width = 10.5;
71                $height = 14.8;
72            break;
73
74            case 'A5':
75                $width = 14.8;
76                $height = 21;
77            break;
78
79            case 'A3':
80                $width = 29.7;
81                $height = 42;
82            break;
83
84            case 'B6 (ISO)':
85                $width = 12.5;
86                $height = 17.6;
87            break;
88
89            case 'B5 (ISO)':
90                $width = 17.6;
91                $height = 25;
92            break;
93
94            case 'B4 (ISO)':
95                $width = 25;
96                $height = 35.3;
97            break;
98
99            case 'Letter':
100                $width = 21.59;
101                $height = 27.94;
102            break;
103
104            case 'Legal':
105                $width = 21.59;
106                $height = 35.56;
107            break;
108
109            case 'Long Bond':
110                $width = 21.59;
111                $height = 33.02;
112            break;
113
114            case 'Tabloid':
115                $width = 27.94;
116                $height = 43.18;
117            break;
118
119            case 'B6 (JIS)':
120                $width = 12.8;
121                $height = 18.2;
122            break;
123
124            case 'B5 (JIS)':
125                $width = 18.2;
126                $height = 25.7;
127            break;
128
129            case 'B4 (JIS)':
130                $width = 25.7;
131                $height = 36.4;
132            break;
133
134            case '16 Kai':
135                $width = 18.4;
136                $height = 26;
137            break;
138
139            case '32 Kai':
140                $width = 13;
141                $height = 18.4;
142            break;
143
144            case 'Big 32 Kai':
145                $width = 14;
146                $height = 20.3;
147            break;
148
149            case 'DL Envelope':
150                $width = 11;
151                $height = 22;
152            break;
153
154            case 'C6 Envelope':
155                $width = 11.4;
156                $height = 16.2;
157            break;
158
159            case 'C6/5 Envelope':
160                $width = 11.4;
161                $height = 22.9;
162            break;
163
164            case 'C5 Envelope':
165                $width = 16.2;
166                $height = 22.9;
167            break;
168
169            case 'C4 Envelope':
170                $width = 22.9;
171                $height = 32.4;
172            break;
173
174            case '#6 3/4 Envelope':
175                $width = 9.21;
176                $height = 16.51;
177            break;
178
179            case '#7 3/4 (Monarch) Envelope':
180                $width = 9.84;
181                $height = 19.05;
182            break;
183
184            case '#9 Envelope':
185                $width = 9.84;
186                $height = 22.54;
187            break;
188
189            case '#10 Envelope':
190                $width = 10.48;
191                $height = 24.13;
192            break;
193
194            case '#11 Envelope':
195                $width = 11.43;
196                $height = 26.35;
197            break;
198
199            case '#12 Envelope':
200                $width = 12.07;
201                $height = 27.94;
202            break;
203
204            case 'Japanese Postcard':
205                $width = 10;
206                $height = 14.8;
207            break;
208
209            case 'A4':
210            default:
211                $format = 'A4';
212                $width = 21;
213                $height = 29.7;
214            break;
215        }
216
217        if ( $orientation != 'portrait' ) {
218            $orientation = 'landscape';
219            $help = $width;
220            $width = $height;
221            $height = $help;
222        }
223
224        // Return format data.
225        $dest ['format'] = $format;
226        $dest ['orientation'] = $orientation;
227        $dest ['width'] = $width;
228        $dest ['height'] = $height;
229
230        // Margins are currently accepted 'as is'
231        // but could be subject to further checks/adjustments in the future.
232        $dest ['margin-top'] = $margin_top;
233        $dest ['margin-bottom'] = $margin_bottom;
234        $dest ['margin-left'] = $margin_left;
235        $dest ['margin-right'] = $margin_right;
236    }
237
238    /**
239     * Set format. Sets all values according to $format.
240     *
241     * @param string $format
242     * @param string $orientation
243     */
244    public function setFormat($format, $orientation='portrait', $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
245        $data = array();
246
247        // Query format data
248        $this->queryFormat ($data, $format, $orientation, $margin_top, $margin_right, $margin_bottom, $margin_left);
249
250        // Save as page settings
251        $this->format = $data ['format'];
252        $this->orientation = $data ['orientation'];
253        $this->width = $data ['width'];
254        $this->height = $data ['height'];
255        $this->margin_top = $data ['margin-top'];
256        $this->margin_bottom = $data ['margin-bottom'];
257        $this->margin_left = $data ['margin-left'];
258        $this->margin_right = $data ['margin-right'];
259    }
260
261    /**
262     * @return string
263     */
264    public function getFormat() {
265        return $this->format;
266    }
267
268    /**
269     * @return string
270     */
271    public function getOrientation() {
272        return $this->orientation;
273    }
274
275    /**
276     * @return float
277     */
278    public function getWidth() {
279        return $this->width;
280    }
281
282    /**
283     * @return float
284     */
285    public function getHeight() {
286        return $this->height;
287    }
288
289    /**
290     * @return int
291     */
292    public function getMarginTop() {
293        return $this->margin_top;
294    }
295
296    /**
297     * @return int
298     */
299    public function getMarginBottom() {
300        return $this->margin_bottom;
301    }
302
303    /**
304     * @return int
305     */
306    public function getMarginLeft() {
307        return $this->margin_left;
308    }
309
310    /**
311     * @return int
312     */
313    public function getMarginRight() {
314        return $this->margin_right;
315    }
316
317    /**
318     * Return width percentage value if margins are taken into account.
319     * Usually "100%" means 21cm in case of A4 format.
320     * But usually you like to take care of margins. This function
321     * adjusts the percentage to the value which should be used for margins.
322     * So 100% == 21cm e.g. becomes 80.9% == 17cm (assuming a margin of 2 cm on both sides).
323     *
324     * @param string $percentage
325     * @return int|string
326     */
327    function getRelWidthMindMargins ($percentage = '100'){
328        $percentage *= $this->width - $this->margin_left - $this->margin_right;
329        $percentage /= $this->width;
330        return $percentage;
331    }
332
333    /**
334     * Like getRelWidthMindMargins but returns the absulute width
335     * in centimeters.
336     *
337     * @param string $percentage
338     * @return float
339     */
340    function getAbsWidthMindMargins ($percentage = '100'){
341        $percentage *= $this->width - $this->margin_left - $this->margin_right;
342        return ($percentage/100);
343    }
344
345    /**
346     * Return height percentage value if margins are taken into account.
347     * Usually "100%" means 29.7cm in case of A4 format.
348     * But usually you like to take care of margins. This function
349     * adjusts the percentage to the value which should be used for margins.
350     * So 100% == 29.7cm e.g. becomes 86.5% == 25.7cm (assuming a margin of 2 cm on top and bottom).
351     *
352     * @param string $percentage
353     * @return float|string
354     */
355    function getRelHeightMindMargins ($percentage = '100'){
356        $percentage *= $this->height - $this->margin_top - $this->margin_bottom;
357        $percentage /= $this->height;
358        return $percentage;
359    }
360
361    /**
362     * Like getRelHeightMindMargins but returns the absulute width
363     * in centimeters.
364     *
365     * @param string $percentage
366     * @return float
367     */
368    function getAbsHeightMindMargins ($percentage = '100'){
369        $percentage *= $this->height - $this->margin_left - $this->margin_right;
370        return ($percentage/100);
371    }
372}
373
374