Styles::writeBackgroundStyle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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