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