Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
74 / 74 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Frame | |
100.00% |
74 / 74 |
|
100.00% |
4 / 4 |
17 | |
100.00% |
1 / 1 |
write | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
4 | |||
writeAlignment | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
writeWrap | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
6 | |||
getStyles | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
5 | * word processing documents. |
6 | * |
7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
8 | * General Public License version 3 as published by the Free Software Foundation. |
9 | * |
10 | * For the full copyright and license information, please read the LICENSE |
11 | * file that was distributed with this source code. For the full list of |
12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
13 | * |
14 | * @see https://github.com/PHPOffice/PHPWord |
15 | * |
16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
17 | */ |
18 | |
19 | namespace PhpOffice\PhpWord\Writer\Word2007\Style; |
20 | |
21 | use PhpOffice\PhpWord\Shared\XMLWriter; |
22 | use PhpOffice\PhpWord\Style\Frame as FrameStyle; |
23 | use PhpOffice\PhpWord\Writer\Word2007\Element\ParagraphAlignment; |
24 | |
25 | /** |
26 | * Frame style writer. |
27 | * |
28 | * @since 0.12.0 |
29 | */ |
30 | class Frame extends AbstractStyle |
31 | { |
32 | const PHP_32BIT_INT_MAX = 2147483647; |
33 | |
34 | /** |
35 | * Write style. |
36 | */ |
37 | public function write(): void |
38 | { |
39 | $style = $this->getStyle(); |
40 | if (!$style instanceof FrameStyle) { |
41 | return; |
42 | } |
43 | $xmlWriter = $this->getXmlWriter(); |
44 | |
45 | $maxZIndex = min(PHP_INT_MAX, self::PHP_32BIT_INT_MAX); |
46 | $zIndices = [FrameStyle::WRAP_INFRONT => $maxZIndex, FrameStyle::WRAP_BEHIND => -$maxZIndex]; |
47 | |
48 | $properties = [ |
49 | 'width' => 'width', |
50 | 'height' => 'height', |
51 | 'left' => 'margin-left', |
52 | 'top' => 'margin-top', |
53 | 'wrapDistanceTop' => 'mso-wrap-distance-top', |
54 | 'wrapDistanceBottom' => 'mso-wrap-distance-bottom', |
55 | 'wrapDistanceLeft' => 'mso-wrap-distance-left', |
56 | 'wrapDistanceRight' => 'mso-wrap-distance-right', |
57 | ]; |
58 | $sizeStyles = $this->getStyles($style, $properties, $style->getUnit()); |
59 | |
60 | $properties = [ |
61 | 'pos' => 'position', |
62 | 'hPos' => 'mso-position-horizontal', |
63 | 'vPos' => 'mso-position-vertical', |
64 | 'hPosRelTo' => 'mso-position-horizontal-relative', |
65 | 'vPosRelTo' => 'mso-position-vertical-relative', |
66 | ]; |
67 | $posStyles = $this->getStyles($style, $properties); |
68 | |
69 | $styles = array_merge($sizeStyles, $posStyles); |
70 | |
71 | // zIndex for infront & behind wrap |
72 | $wrap = $style->getWrap(); |
73 | if ($wrap !== null && isset($zIndices[$wrap])) { |
74 | $styles['z-index'] = $zIndices[$wrap]; |
75 | $wrap = null; |
76 | } |
77 | |
78 | // Style attribute |
79 | $xmlWriter->writeAttribute('style', $this->assembleStyle($styles)); |
80 | |
81 | $this->writeWrap($xmlWriter, $style, $wrap); |
82 | } |
83 | |
84 | /** |
85 | * Write alignment. |
86 | */ |
87 | public function writeAlignment(): void |
88 | { |
89 | $style = $this->getStyle(); |
90 | if (!$style instanceof FrameStyle) { |
91 | return; |
92 | } |
93 | |
94 | $xmlWriter = $this->getXmlWriter(); |
95 | $xmlWriter->startElement('w:pPr'); |
96 | |
97 | if ('' !== $style->getAlignment()) { |
98 | $paragraphAlignment = new ParagraphAlignment($style->getAlignment()); |
99 | $xmlWriter->startElement($paragraphAlignment->getName()); |
100 | foreach ($paragraphAlignment->getAttributes() as $attributeName => $attributeValue) { |
101 | $xmlWriter->writeAttribute($attributeName, $attributeValue); |
102 | } |
103 | $xmlWriter->endElement(); |
104 | } |
105 | |
106 | $xmlWriter->endElement(); |
107 | } |
108 | |
109 | /** |
110 | * Write wrap. |
111 | * |
112 | * @param string $wrap |
113 | */ |
114 | private function writeWrap(XMLWriter $xmlWriter, FrameStyle $style, $wrap): void |
115 | { |
116 | if ($wrap !== null) { |
117 | $xmlWriter->startElement('w10:wrap'); |
118 | $xmlWriter->writeAttribute('type', $wrap); |
119 | |
120 | $relativePositions = [ |
121 | FrameStyle::POS_RELTO_MARGIN => 'margin', |
122 | FrameStyle::POS_RELTO_PAGE => 'page', |
123 | FrameStyle::POS_RELTO_TMARGIN => 'margin', |
124 | FrameStyle::POS_RELTO_BMARGIN => 'page', |
125 | FrameStyle::POS_RELTO_LMARGIN => 'margin', |
126 | FrameStyle::POS_RELTO_RMARGIN => 'page', |
127 | ]; |
128 | $pos = $style->getPos(); |
129 | $hPos = $style->getHPosRelTo(); |
130 | $vPos = $style->getVPosRelTo(); |
131 | |
132 | if ($pos == FrameStyle::POS_ABSOLUTE) { |
133 | $xmlWriter->writeAttribute('anchorx', 'page'); |
134 | $xmlWriter->writeAttribute('anchory', 'page'); |
135 | } elseif ($pos == FrameStyle::POS_RELATIVE) { |
136 | if (isset($relativePositions[$hPos])) { |
137 | $xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]); |
138 | } |
139 | if (isset($relativePositions[$vPos])) { |
140 | $xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]); |
141 | } |
142 | } |
143 | |
144 | $xmlWriter->endElement(); // w10:wrap |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * Get style values in associative array. |
150 | * |
151 | * @param array $properties |
152 | * @param string $suffix |
153 | * |
154 | * @return array |
155 | */ |
156 | private function getStyles(FrameStyle $style, $properties, $suffix = '') |
157 | { |
158 | $styles = []; |
159 | |
160 | foreach ($properties as $key => $property) { |
161 | $method = "get{$key}"; |
162 | $value = $style->$method(); |
163 | if ($value !== null) { |
164 | $styles[$property] = $style->$method() . $suffix; |
165 | } |
166 | } |
167 | |
168 | return $styles; |
169 | } |
170 | } |