Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.31% covered (warning)
88.31%
68 / 77
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PptSlideLayouts
88.31% covered (warning)
88.31%
68 / 77
0.00% covered (danger)
0.00%
0 / 3
10.16
0.00% covered (danger)
0.00%
0 / 1
 render
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 writeSlideLayoutRelationships
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
2.04
 writeSlideLayout
90.91% covered (success)
90.91%
50 / 55
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation 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/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
21
22use PhpOffice\Common\Adapter\Zip\ZipInterface;
23use PhpOffice\Common\Drawing as CommonDrawing;
24use PhpOffice\Common\XMLWriter;
25use PhpOffice\PhpPresentation\Slide;
26use PhpOffice\PhpPresentation\Slide\Background\Image;
27use PhpOffice\PhpPresentation\Slide\SlideLayout;
28use PhpOffice\PhpPresentation\Style\ColorMap;
29
30class PptSlideLayouts extends AbstractSlide
31{
32    public function render(): ZipInterface
33    {
34        foreach ($this->oPresentation->getAllMasterSlides() as $oSlideMaster) {
35            foreach ($oSlideMaster->getAllSlideLayouts() as $oSlideLayout) {
36                $this->oZip->addFromString('ppt/slideLayouts/_rels/slideLayout' . $oSlideLayout->layoutNr . '.xml.rels', $this->writeSlideLayoutRelationships($oSlideLayout));
37                $this->oZip->addFromString('ppt/slideLayouts/slideLayout' . $oSlideLayout->layoutNr . '.xml', $this->writeSlideLayout($oSlideLayout));
38
39                // Add background image slide
40                $oBkgImage = $oSlideLayout->getBackground();
41                if ($oBkgImage instanceof Image) {
42                    $this->oZip->addFromString('ppt/media/' . $oBkgImage->getIndexedFilename($oSlideLayout->getRelsIndex()), file_get_contents($oBkgImage->getPath()));
43                }
44            }
45        }
46
47        return $this->oZip;
48    }
49
50    /**
51     * Write slide layout relationships to XML format.
52     *
53     * @return string XML Output
54     */
55    protected function writeSlideLayoutRelationships(SlideLayout $oSlideLayout): string
56    {
57        // Create XML writer
58        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
59
60        // XML header
61        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
62
63        // Relationships
64        $objWriter->startElement('Relationships');
65        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
66
67        $relId = 0;
68
69        // Write slideMaster relationship
70        $this->writeRelationship($objWriter, ++$relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', '../slideMasters/slideMaster' . $oSlideLayout->getSlideMaster()->getRelsIndex() . '.xml');
71
72        // Write drawing relationships?
73        $relId = $this->writeDrawingRelations($oSlideLayout, $objWriter, ++$relId);
74
75        // Write background relationships?
76        $oBackground = $oSlideLayout->getBackground();
77        if ($oBackground instanceof Image) {
78            $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($oSlideLayout->getRelsIndex()));
79            $oBackground->relationId = 'rId' . $relId;
80
81            ++$relId;
82        }
83
84        $objWriter->endElement();
85
86        // Return
87        return $objWriter->getData();
88    }
89
90    /**
91     * Write slide to XML format.
92     *
93     * @return string XML Output
94     */
95    protected function writeSlideLayout(SlideLayout $pSlideLayout): string
96    {
97        // Create XML writer
98        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
99        // XML header
100        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
101        // p:sldLayout
102        $objWriter->startElement('p:sldLayout');
103        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
104        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
105        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
106        $objWriter->writeAttribute('preserve', 1);
107        // p:sldLayout\p:cSld
108        $objWriter->startElement('p:cSld');
109        $objWriter->writeAttributeIf('' != $pSlideLayout->getLayoutName(), 'name', $pSlideLayout->getLayoutName());
110        // Background
111        $this->writeSlideBackground($pSlideLayout, $objWriter);
112        // p:sldLayout\p:cSld\p:spTree
113        $objWriter->startElement('p:spTree');
114        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
115        $objWriter->startElement('p:nvGrpSpPr');
116        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
117        $objWriter->startElement('p:cNvPr');
118        $objWriter->writeAttribute('id', '1');
119        $objWriter->writeAttribute('name', '');
120        $objWriter->endElement();
121        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
122        $objWriter->writeElement('p:cNvGrpSpPr', null);
123        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
124        $objWriter->writeElement('p:nvPr', null);
125        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
126        $objWriter->endElement();
127        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr
128        $objWriter->startElement('p:grpSpPr');
129        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm
130        $objWriter->startElement('a:xfrm');
131        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
132        $objWriter->startElement('a:off');
133        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
134        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
135        $objWriter->endElement();
136        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
137        $objWriter->startElement('a:ext');
138        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
139        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
140        $objWriter->endElement();
141        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
142        $objWriter->startElement('a:chOff');
143        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
144        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
145        $objWriter->endElement();
146        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
147        $objWriter->startElement('a:chExt');
148        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
149        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
150        $objWriter->endElement();
151        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
152        $objWriter->endElement();
153        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\
154        $objWriter->endElement();
155
156        // Loop shapes
157        $this->writeShapeCollection($objWriter, $pSlideLayout->getShapeCollection());
158        // p:sldLayout\p:cSld\p:spTree\
159        $objWriter->endElement();
160        // p:sldLayout\p:cSld\
161        $objWriter->endElement();
162
163        // p:sldLayout\p:clrMapOvr
164        $objWriter->startElement('p:clrMapOvr');
165        $arrayDiff = array_diff_assoc(ColorMap::$mappingDefault, $pSlideLayout->colorMap->getMapping());
166        if (!empty($arrayDiff)) {
167            // p:sldLayout\p:clrMapOvr\a:overrideClrMapping
168            $objWriter->startElement('a:overrideClrMapping');
169            foreach ($pSlideLayout->colorMap->getMapping() as $n => $v) {
170                $objWriter->writeAttribute($n, $v);
171            }
172            $objWriter->endElement();
173        } else {
174            // p:sldLayout\p:clrMapOvr\a:masterClrMapping
175            $objWriter->writeElement('a:masterClrMapping');
176        }
177        // p:sldLayout\p:clrMapOvr\
178        $objWriter->endElement();
179
180        if (null !== $pSlideLayout->getTransition()) {
181            $this->writeSlideTransition($objWriter, $pSlideLayout->getTransition());
182        }
183
184        // p:sldLayout\
185        $objWriter->endElement();
186
187        return $objWriter->getData();
188    }
189}