Completed
Push — develop ( c1ddc2...097d50 )
by Franck
10:30 queued 08:06
created

Styles   C

Complexity

Total Complexity 37

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 99.51%

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 17
dl 0
loc 311
ccs 202
cts 203
cp 0.9951
rs 6.7571
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B writeTableStyle() 0 12 5
A writeBackgroundStyle() 0 10 1
A render() 0 5 1
D writeRichTextStyle() 0 81 16
A writeGradientFill() 0 15 1
D writePart() 0 118 9
A writeGroupStyle() 0 11 4
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
4
5
use PhpOffice\Common\Drawing as CommonDrawing;
6
use PhpOffice\Common\Text;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\Shape\Group;
9
use PhpOffice\PhpPresentation\Shape\Table;
10
use PhpOffice\PhpPresentation\Slide\Background\Image;
11
use PhpOffice\PhpPresentation\Style\Fill;
12
use PhpOffice\PhpPresentation\Shape\RichText;
13
use PhpOffice\PhpPresentation\Style\Border;
14
15
class Styles extends AbstractDecoratorWriter
16
{
17
    /**
18
     * Stores font styles draw:gradient nodes
19
     *
20
     * @var array
21
     */
22
    protected $arrayGradient = array();
23
    /**
24
     * Stores font styles draw:stroke-dash nodes
25
     *
26
     * @var array
27
     */
28
    protected $arrayStrokeDash = array();
29
30
    /**
31
     * @return ZipInterface
32
     */
33 59
    public function render()
34
    {
35 59
        $this->getZip()->addFromString('styles.xml', $this->writePart());
36 59
        return $this->getZip();
37
    }
38
39
    /**
40
     * Write Meta file to XML format
41
     *
42
     * @return string        XML Output
43
     * @throws \Exception
44
     */
45 59
    protected function writePart()
46
    {
47
        // Create XML writer
48 59
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
49 59
        $objWriter->startDocument('1.0', 'UTF-8');
50
51
        // office:document-meta
52 59
        $objWriter->startElement('office:document-styles');
53 59
        $objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
54 59
        $objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
55 59
        $objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
56 59
        $objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
57 59
        $objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
58 59
        $objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
59 59
        $objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
60 59
        $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
61 59
        $objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
62 59
        $objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
63 59
        $objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0');
64 59
        $objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
65 59
        $objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
66 59
        $objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
67 59
        $objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
68 59
        $objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
69 59
        $objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
70 59
        $objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
71 59
        $objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
72 59
        $objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
73 59
        $objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
74 59
        $objWriter->writeAttribute('xmlns:smil', 'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0');
75 59
        $objWriter->writeAttribute('xmlns:anim', 'urn:oasis:names:tc:opendocument:xmlns:animation:1.0');
76 59
        $objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
77 59
        $objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
78 59
        $objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
79 59
        $objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
80 59
        $objWriter->writeAttribute('xmlns:officeooo', 'http://openoffice.org/2009/office');
81 59
        $objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
82 59
        $objWriter->writeAttribute('xmlns:drawooo', 'http://openoffice.org/2010/draw');
83 59
        $objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
84 59
        $objWriter->writeAttribute('office:version', '1.2');
85
86
        // Variables
87 59
        $stylePageLayout = $this->getPresentation()->getLayout()->getDocumentLayout();
88 59
        if (empty($stylePageLayout)) {
89 1
            $stylePageLayout = 'sPL0';
90
        }
91
92
        // office:styles
93 59
        $objWriter->startElement('office:styles');
94
        // style:style
95 59
        $objWriter->startElement('style:style');
96 59
        $objWriter->writeAttribute('style:name', 'sPres0');
97 59
        $objWriter->writeAttribute('style:display-name', 'sPres0');
98 59
        $objWriter->writeAttribute('style:family', 'presentation');
99
        // style:graphic-properties
100 59
        $objWriter->startElement('style:graphic-properties');
101 59
        $objWriter->writeAttribute('draw:fill-color', '#ffffff');
102
        // > style:graphic-properties
103 59
        $objWriter->endElement();
104
        // > style:style
105 59
        $objWriter->endElement();
106
107 59
        foreach ($this->getPresentation()->getAllSlides() as $keySlide => $oSlide) {
108 59
            foreach ($oSlide->getShapeCollection() as $shape) {
109 51
                if ($shape instanceof Table) {
110 7
                    $this->writeTableStyle($objWriter, $shape);
111 45
                } elseif ($shape instanceof Group) {
112 1
                    $this->writeGroupStyle($objWriter, $shape);
113 44
                } elseif ($shape instanceof RichText) {
114 51
                    $this->writeRichTextStyle($objWriter, $shape);
115
                }
116
            }
117 59
            $oBkgImage = $oSlide->getBackground();
118 59
            if ($oBkgImage instanceof Image) {
119 59
                $this->writeBackgroundStyle($objWriter, $oBkgImage, $keySlide);
120
            }
121
        }
122
        // > office:styles
123 59
        $objWriter->endElement();
124
125
        // office:automatic-styles
126 59
        $objWriter->startElement('office:automatic-styles');
127
        // style:page-layout
128 59
        $objWriter->startElement('style:page-layout');
129 59
        $objWriter->writeAttribute('style:name', $stylePageLayout);
130
        // style:page-layout-properties
131 59
        $objWriter->startElement('style:page-layout-properties');
132 59
        $objWriter->writeAttribute('fo:margin-top', '0cm');
133 59
        $objWriter->writeAttribute('fo:margin-bottom', '0cm');
134 59
        $objWriter->writeAttribute('fo:margin-left', '0cm');
135 59
        $objWriter->writeAttribute('fo:margin-right', '0cm');
136 59
        $objWriter->writeAttribute('fo:page-width', Text::numberFormat(CommonDrawing::pixelsToCentimeters(CommonDrawing::emuToPixels($this->getPresentation()->getLayout()->getCX())), 1) . 'cm');
137 59
        $objWriter->writeAttribute('fo:page-height', Text::numberFormat(CommonDrawing::pixelsToCentimeters(CommonDrawing::emuToPixels($this->getPresentation()->getLayout()->getCY())), 1) . 'cm');
138 59
        $printOrientation = 'portrait';
139 59
        if ($this->getPresentation()->getLayout()->getCX() > $this->getPresentation()->getLayout()->getCY()) {
140 58
            $printOrientation = 'landscape';
141
        }
142 59
        $objWriter->writeAttribute('style:print-orientation', $printOrientation);
143 59
        $objWriter->endElement();
144 59
        $objWriter->endElement();
145 59
        $objWriter->endElement();
146
147
        // office:master-styles
148 59
        $objWriter->startElement('office:master-styles');
149
        // style:master-page
150 59
        $objWriter->startElement('style:master-page');
151 59
        $objWriter->writeAttribute('style:name', 'Standard');
152 59
        $objWriter->writeAttribute('style:display-name', 'Standard');
153 59
        $objWriter->writeAttribute('style:page-layout-name', $stylePageLayout);
154 59
        $objWriter->writeAttribute('draw:style-name', 'sPres0');
155 59
        $objWriter->endElement();
156 59
        $objWriter->endElement();
157
158 59
        $objWriter->endElement();
159
160
        // Return
161 59
        return $objWriter->getData();
162
    }
163
164
    /**
165
     * Write the default style information for a RichText shape
166
     *
167
     * @param XMLWriter $objWriter
168
     * @param RichText $shape
169
     */
170 14
    protected function writeRichTextStyle(XMLWriter $objWriter, RichText $shape)
171
    {
172 14
        $oFill = $shape->getFill();
173 14
        if ($oFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $oFill->getFillType() == Fill::FILL_GRADIENT_PATH) {
174 1
            if (!in_array($oFill->getHashCode(), $this->arrayGradient)) {
175 1
                $this->writeGradientFill($objWriter, $oFill);
176
            }
177
        }
178 14
        $oBorder = $shape->getBorder();
179 14
        if ($oBorder->getDashStyle() != Border::DASH_SOLID) {
180 2
            if (!in_array($oBorder->getDashStyle(), $this->arrayStrokeDash)) {
181 2
                $objWriter->startElement('draw:stroke-dash');
182 2
                $objWriter->writeAttribute('draw:name', 'strokeDash_'.$oBorder->getDashStyle());
183 2
                $objWriter->writeAttribute('draw:style', 'rect');
184 2
                switch ($oBorder->getDashStyle()) {
185 2
                    case Border::DASH_DASH:
186 2
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
187 2
                        $objWriter->writeAttribute('draw:dots2', '1');
188 2
                        $objWriter->writeAttribute('draw:dots2-length', '0.14cm');
189 2
                        break;
190 1
                    case Border::DASH_DASHDOT:
191 1
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
192 1
                        $objWriter->writeAttribute('draw:dots1', '1');
193 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
194 1
                        $objWriter->writeAttribute('draw:dots2', '1');
195 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.14cm');
196 1
                        break;
197 1
                    case Border::DASH_DOT:
198 1
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
199 1
                        $objWriter->writeAttribute('draw:dots1', '1');
200 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
201 1
                        break;
202 1
                    case Border::DASH_LARGEDASH:
203 1
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
204 1
                        $objWriter->writeAttribute('draw:dots2', '1');
205 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.28cm');
206 1
                        break;
207 1
                    case Border::DASH_LARGEDASHDOT:
208 1
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
209 1
                        $objWriter->writeAttribute('draw:dots1', '1');
210 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
211 1
                        $objWriter->writeAttribute('draw:dots2', '1');
212 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.28cm');
213 1
                        break;
214 1
                    case Border::DASH_LARGEDASHDOTDOT:
215 1
                        $objWriter->writeAttribute('draw:distance', '0.105cm');
216 1
                        $objWriter->writeAttribute('draw:dots1', '2');
217 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
218 1
                        $objWriter->writeAttribute('draw:dots2', '1');
219 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.28cm');
220 1
                        break;
221 1
                    case Border::DASH_SYSDASH:
222 1
                        $objWriter->writeAttribute('draw:distance', '0.035cm');
223 1
                        $objWriter->writeAttribute('draw:dots2', '1');
224 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.105cm');
225 1
                        break;
226 1
                    case Border::DASH_SYSDASHDOT:
227 1
                        $objWriter->writeAttribute('draw:distance', '0.035cm');
228 1
                        $objWriter->writeAttribute('draw:dots1', '1');
229 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
230 1
                        $objWriter->writeAttribute('draw:dots2', '1');
231 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.105cm');
232 1
                        break;
233 1
                    case Border::DASH_SYSDASHDOTDOT:
234 1
                        $objWriter->writeAttribute('draw:distance', '0.035cm');
235 1
                        $objWriter->writeAttribute('draw:dots1', '2');
236 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
237 1
                        $objWriter->writeAttribute('draw:dots2', '1');
238 1
                        $objWriter->writeAttribute('draw:dots2-length', '0.105cm');
239 1
                        break;
240 1
                    case Border::DASH_SYSDOT:
241 1
                        $objWriter->writeAttribute('draw:distance', '0.035cm');
242 1
                        $objWriter->writeAttribute('draw:dots1', '1');
243 1
                        $objWriter->writeAttribute('draw:dots1-length', '0.035cm');
244 1
                        break;
245
                }
246 2
                $objWriter->endElement();
247 2
                $this->arrayStrokeDash[] = $oBorder->getDashStyle();
248
            }
249
        }
250 14
    }
251
252
    /**
253
     * Write the default style information for a Table shape
254
     *
255
     * @param XMLWriter $objWriter
256
     * @param Table $shape
257
     */
258 7
    protected function writeTableStyle(XMLWriter $objWriter, Table $shape)
259
    {
260 7
        foreach ($shape->getRows() as $row) {
261 6
            foreach ($row->getCells() as $cell) {
262 6
                if ($cell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) {
263 1
                    if (!in_array($cell->getFill()->getHashCode(), $this->arrayGradient)) {
264 6
                        $this->writeGradientFill($objWriter, $cell->getFill());
265
                    }
266
                }
267
            }
268
        }
269 7
    }
270
271
    /**
272
     * Writes the style information for a group of shapes
273
     *
274
     * @param XMLWriter $objWriter
275
     * @param Group $group
276
     */
277 1
    protected function writeGroupStyle(XMLWriter $objWriter, Group $group)
278
    {
279 1
        $shapes = $group->getShapeCollection();
280 1
        foreach ($shapes as $shape) {
281 1
            if ($shape instanceof Table) {
282
                $this->writeTableStyle($objWriter, $shape);
283 1
            } elseif ($shape instanceof Group) {
284 1
                $this->writeGroupStyle($objWriter, $shape);
285
            }
286
        }
287 1
    }
288
289
    /**
290
     * Write the gradient style
291
     * @param XMLWriter $objWriter
292
     * @param Fill $oFill
293
     */
294 2
    protected function writeGradientFill(XMLWriter $objWriter, Fill $oFill)
295
    {
296 2
        $objWriter->startElement('draw:gradient');
297 2
        $objWriter->writeAttribute('draw:name', 'gradient_'.$oFill->getHashCode());
298 2
        $objWriter->writeAttribute('draw:display-name', 'gradient_'.$oFill->getHashCode());
299 2
        $objWriter->writeAttribute('draw:style', 'linear');
300 2
        $objWriter->writeAttribute('draw:start-intensity', '100%');
301 2
        $objWriter->writeAttribute('draw:end-intensity', '100%');
302 2
        $objWriter->writeAttribute('draw:start-color', '#'.$oFill->getStartColor()->getRGB());
303 2
        $objWriter->writeAttribute('draw:end-color', '#'.$oFill->getEndColor()->getRGB());
304 2
        $objWriter->writeAttribute('draw:border', '0%');
305 2
        $objWriter->writeAttribute('draw:angle', $oFill->getRotation() - 90);
306 2
        $objWriter->endElement();
307 2
        $this->arrayGradient[] = $oFill->getHashCode();
308 2
    }
309
310
    /**
311
     * Write the background image style
312
     * @param XMLWriter $objWriter
313
     * @param Image $oBkgImage
314
     */
315 1
    protected function writeBackgroundStyle(XMLWriter $objWriter, Image $oBkgImage, $numSlide)
316
    {
317 1
        $objWriter->startElement('draw:fill-image');
318 1
        $objWriter->writeAttribute('draw:name', 'background_'.$numSlide);
319 1
        $objWriter->writeAttribute('xlink:href', 'Pictures/'.str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
320 1
        $objWriter->writeAttribute('xlink:type', 'simple');
321 1
        $objWriter->writeAttribute('xlink:show', 'embed');
322 1
        $objWriter->writeAttribute('xlink:actuate', 'onLoad');
323 1
        $objWriter->endElement();
324 1
    }
325
}
326