Completed
Pull Request — develop (#209)
by Franck
08:15
created

Styles::writeGroupStyle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

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