Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
69 / 69
DocumentLayout
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
36
100.00% covered (success)
100.00%
69 / 69
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getDocumentLayout
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setDocumentLayout
100.00% covered (success)
100.00%
1 / 1
15
100.00% covered (success)
100.00%
25 / 25
 getCX
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCY
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setCX
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setCY
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 convertUnit
100.00% covered (success)
100.00%
1 / 1
15
100.00% covered (success)
100.00%
33 / 33
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @copyright   2009-2015 PHPPresentation contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpPresentation;
22
23use PhpOffice\Common\Drawing;
24
25/**
26 * \PhpOffice\PhpPresentation\DocumentLayout.
27 */
28class DocumentLayout
29{
30    public const LAYOUT_CUSTOM = '';
31    public const LAYOUT_SCREEN_4X3 = 'screen4x3';
32    public const LAYOUT_SCREEN_16X10 = 'screen16x10';
33    public const LAYOUT_SCREEN_16X9 = 'screen16x9';
34    public const LAYOUT_35MM = '35mm';
35    public const LAYOUT_A3 = 'A3';
36    public const LAYOUT_A4 = 'A4';
37    public const LAYOUT_B4ISO = 'B4ISO';
38    public const LAYOUT_B5ISO = 'B5ISO';
39    public const LAYOUT_BANNER = 'banner';
40    public const LAYOUT_LETTER = 'letter';
41    public const LAYOUT_OVERHEAD = 'overhead';
42
43    public const UNIT_EMU = 'emu';
44    public const UNIT_CENTIMETER = 'cm';
45    public const UNIT_INCH = 'in';
46    public const UNIT_MILLIMETER = 'mm';
47    public const UNIT_PIXEL = 'px';
48    public const UNIT_POINT = 'pt';
49
50    /**
51     * Dimension types.
52     *
53     * 1 px = 9525 EMU @ 96dpi (which is seems to be the default)
54     * Absolute distances are specified in English Metric Units (EMUs),
55     * occasionally referred to as A units; there are 360000 EMUs per
56     * centimeter, 914400 EMUs per inch, 12700 EMUs per point.
57     *
58     * @var array<string, array<string, int>>
59     */
60    private $dimension = [
61        self::LAYOUT_SCREEN_4X3 => ['cx' => 9144000, 'cy' => 6858000],
62        self::LAYOUT_SCREEN_16X10 => ['cx' => 9144000, 'cy' => 5715000],
63        self::LAYOUT_SCREEN_16X9 => ['cx' => 9144000, 'cy' => 5143500],
64        self::LAYOUT_35MM => ['cx' => 10287000, 'cy' => 6858000],
65        self::LAYOUT_A3 => ['cx' => 15120000, 'cy' => 10692000],
66        self::LAYOUT_A4 => ['cx' => 10692000, 'cy' => 7560000],
67        self::LAYOUT_B4ISO => ['cx' => 10826750, 'cy' => 8120063],
68        self::LAYOUT_B5ISO => ['cx' => 7169150, 'cy' => 5376863],
69        self::LAYOUT_BANNER => ['cx' => 7315200, 'cy' => 914400],
70        self::LAYOUT_LETTER => ['cx' => 9144000, 'cy' => 6858000],
71        self::LAYOUT_OVERHEAD => ['cx' => 9144000, 'cy' => 6858000],
72    ];
73
74    /**
75     * Layout name.
76     *
77     * @var string
78     */
79    private $layout;
80
81    /**
82     * Layout X dimension.
83     *
84     * @var float
85     */
86    private $dimensionX;
87
88    /**
89     * Layout Y dimension.
90     *
91     * @var float
92     */
93    private $dimensionY;
94
95    /**
96     * Create a new \PhpOffice\PhpPresentation\DocumentLayout.
97     */
98    public function __construct()
99    {
100        $this->setDocumentLayout(self::LAYOUT_SCREEN_4X3);
101    }
102
103    /**
104     * Get Document Layout.
105     */
106    public function getDocumentLayout(): string
107    {
108        return $this->layout;
109    }
110
111    /**
112     * Set Document Layout.
113     *
114     * @param array<string, int>|string $pValue
115     * @param bool $isLandscape
116     */
117    public function setDocumentLayout($pValue = self::LAYOUT_SCREEN_4X3, $isLandscape = true): self
118    {
119        switch ($pValue) {
120            case self::LAYOUT_SCREEN_4X3:
121            case self::LAYOUT_SCREEN_16X10:
122            case self::LAYOUT_SCREEN_16X9:
123            case self::LAYOUT_35MM:
124            case self::LAYOUT_A3:
125            case self::LAYOUT_A4:
126            case self::LAYOUT_B4ISO:
127            case self::LAYOUT_B5ISO:
128            case self::LAYOUT_BANNER:
129            case self::LAYOUT_LETTER:
130            case self::LAYOUT_OVERHEAD:
131                $this->layout = $pValue;
132                $this->dimensionX = $this->dimension[$this->layout]['cx'];
133                $this->dimensionY = $this->dimension[$this->layout]['cy'];
134                break;
135            case self::LAYOUT_CUSTOM:
136            default:
137                $this->layout = self::LAYOUT_CUSTOM;
138                $this->dimensionX = $pValue['cx'];
139                $this->dimensionY = $pValue['cy'];
140                break;
141        }
142
143        if (!$isLandscape) {
144            $tmp = $this->dimensionX;
145            $this->dimensionX = $this->dimensionY;
146            $this->dimensionY = $tmp;
147        }
148
149        return $this;
150    }
151
152    /**
153     * Get Document Layout cx.
154     */
155    public function getCX(string $unit = self::UNIT_EMU): float
156    {
157        return $this->convertUnit($this->dimensionX, self::UNIT_EMU, $unit);
158    }
159
160    /**
161     * Get Document Layout cy.
162     */
163    public function getCY(string $unit = self::UNIT_EMU): float
164    {
165        return $this->convertUnit($this->dimensionY, self::UNIT_EMU, $unit);
166    }
167
168    /**
169     * Get Document Layout cx.
170     */
171    public function setCX(float $value, string $unit = self::UNIT_EMU): self
172    {
173        $this->layout = self::LAYOUT_CUSTOM;
174        $this->dimensionX = $this->convertUnit($value, $unit, self::UNIT_EMU);
175
176        return $this;
177    }
178
179    /**
180     * Get Document Layout cy.
181     */
182    public function setCY(float $value, string $unit = self::UNIT_EMU): self
183    {
184        $this->layout = self::LAYOUT_CUSTOM;
185        $this->dimensionY = $this->convertUnit($value, $unit, self::UNIT_EMU);
186
187        return $this;
188    }
189
190    /**
191     * Convert EMUs to differents units.
192     */
193    protected function convertUnit(float $value, string $fromUnit, string $toUnit): float
194    {
195        // Convert from $fromUnit to EMU
196        switch ($fromUnit) {
197            case self::UNIT_MILLIMETER:
198                $value *= 36000;
199                break;
200            case self::UNIT_CENTIMETER:
201                $value *= 360000;
202                break;
203            case self::UNIT_INCH:
204                $value *= 914400;
205                break;
206            case self::UNIT_PIXEL:
207                $value = Drawing::pixelsToEmu($value);
208                break;
209            case self::UNIT_POINT:
210                $value *= 12700;
211                break;
212            case self::UNIT_EMU:
213            default:
214                // no changes
215        }
216
217        // Convert from EMU to $toUnit
218        switch ($toUnit) {
219            case self::UNIT_MILLIMETER:
220                $value /= 36000;
221                break;
222            case self::UNIT_CENTIMETER:
223                $value /= 360000;
224                break;
225            case self::UNIT_INCH:
226                $value /= 914400;
227                break;
228            case self::UNIT_PIXEL:
229                $value = Drawing::emuToPixels((int) $value);
230                break;
231            case self::UNIT_POINT:
232                $value /= 12700;
233                break;