Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.87% covered (success)
95.87%
116 / 121
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PptSlideMasters
95.87% covered (success)
95.87%
116 / 121
0.00% covered (danger)
0.00%
0 / 3
14
0.00% covered (danger)
0.00%
0 / 1
 render
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 writeSlideMasterRelationships
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
3.05
 writeSlideMaster
98.97% covered (success)
98.97%
96 / 97
0.00% covered (danger)
0.00%
0 / 1
8
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\Shape\RichText;
26use PhpOffice\PhpPresentation\Slide\Background\Image;
27use PhpOffice\PhpPresentation\Slide\SlideMaster;
28use PhpOffice\PhpPresentation\Style\SchemeColor;
29
30class PptSlideMasters extends AbstractSlide
31{
32    public function render(): ZipInterface
33    {
34        foreach ($this->oPresentation->getAllMasterSlides() as $oMasterSlide) {
35            // Add the relations from the masterSlide to the ZIP file
36            $this->oZip->addFromString('ppt/slideMasters/_rels/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml.rels', $this->writeSlideMasterRelationships($oMasterSlide));
37            // Add the information from the masterSlide to the ZIP file
38            $this->oZip->addFromString('ppt/slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeSlideMaster($oMasterSlide));
39
40            // Add background image slide
41            $oBkgImage = $oMasterSlide->getBackground();
42            if ($oBkgImage instanceof Image) {
43                $this->oZip->addFromString('ppt/media/' . $oBkgImage->getIndexedFilename($oMasterSlide->getRelsIndex()), file_get_contents($oBkgImage->getPath()));
44            }
45        }
46
47        return $this->oZip;
48    }
49
50    /**
51     * Write slide master relationships to XML format.
52     *
53     * @todo Set method in protected
54     *
55     * @return string XML Output
56     */
57    public function writeSlideMasterRelationships(SlideMaster $oMasterSlide): string
58    {
59        // Create XML writer
60        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
61        // XML header
62        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
63        // Relationships
64        $objWriter->startElement('Relationships');
65        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
66        // Starting relation id
67        $relId = 0;
68        // Write all the relations to the Layout Slides
69        foreach ($oMasterSlide->getAllSlideLayouts() as $slideLayout) {
70            $this->writeRelationship($objWriter, ++$relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout', '../slideLayouts/slideLayout' . $slideLayout->layoutNr . '.xml');
71            // Save the used relationId
72            $slideLayout->relationId = 'rId' . $relId;
73        }
74
75        // Write drawing relationships?
76        $relId = $this->writeDrawingRelations($oMasterSlide, $objWriter, ++$relId);
77
78        // Write background relationships?
79        $oBackground = $oMasterSlide->getBackground();
80        if ($oBackground instanceof Image) {
81            $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($oMasterSlide->getRelsIndex()));
82            $oBackground->relationId = 'rId' . $relId;
83
84            ++$relId;
85        }
86
87        // TODO: Write hyperlink relationships?
88        // TODO: Write comment relationships
89        // Relationship theme/theme1.xml
90        $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
91        $objWriter->endElement();
92
93        return $objWriter->getData();
94    }
95
96    /**
97     * Write slide to XML format.
98     *
99     * @return string XML Output
100     */
101    protected function writeSlideMaster(SlideMaster $pSlide): string
102    {
103        // Create XML writer
104        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
105        // XML header
106        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
107        // p:sldMaster
108        $objWriter->startElement('p:sldMaster');
109        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
110        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
111        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
112        // p:sldMaster\p:cSld
113        $objWriter->startElement('p:cSld');
114        // Background
115        $this->writeSlideBackground($pSlide, $objWriter);
116        // p:sldMaster\p:cSld\p:spTree
117        $objWriter->startElement('p:spTree');
118        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr
119        $objWriter->startElement('p:nvGrpSpPr');
120        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
121        $objWriter->startElement('p:cNvPr');
122        $objWriter->writeAttribute('id', '1');
123        $objWriter->writeAttribute('name', '');
124        $objWriter->endElement();
125        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
126        $objWriter->writeElement('p:cNvGrpSpPr', null);
127        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
128        $objWriter->writeElement('p:nvPr', null);
129        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr
130        $objWriter->endElement();
131        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr
132        $objWriter->startElement('p:grpSpPr');
133        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm
134        $objWriter->startElement('a:xfrm');
135        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
136        $objWriter->startElement('a:off');
137        $objWriter->writeAttribute('x', 0);
138        $objWriter->writeAttribute('y', 0);
139        $objWriter->endElement();
140        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
141        $objWriter->startElement('a:ext');
142        $objWriter->writeAttribute('cx', 0);
143        $objWriter->writeAttribute('cy', 0);
144        $objWriter->endElement();
145        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
146        $objWriter->startElement('a:chOff');
147        $objWriter->writeAttribute('x', 0);
148        $objWriter->writeAttribute('y', 0);
149        $objWriter->endElement();
150        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
151        $objWriter->startElement('a:chExt');
152        $objWriter->writeAttribute('cx', 0);
153        $objWriter->writeAttribute('cy', 0);
154        $objWriter->endElement();
155        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
156        $objWriter->endElement();
157        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\
158        $objWriter->endElement();
159        // Loop shapes
160        $this->writeShapeCollection($objWriter, $pSlide->getShapeCollection());
161        // p:sldMaster\p:cSld\p:spTree\
162        $objWriter->endElement();
163        // p:sldMaster\p:cSld\
164        $objWriter->endElement();
165
166        // p:sldMaster\p:clrMap
167        $objWriter->startElement('p:clrMap');
168        foreach ($pSlide->colorMap->getMapping() as $n => $v) {
169            $objWriter->writeAttribute($n, $v);
170        }
171        $objWriter->endElement();
172        // p:sldMaster\p:clrMap\
173
174        // p:sldMaster\p:sldLayoutIdLst
175        $objWriter->startElement('p:sldLayoutIdLst');
176        foreach ($pSlide->getAllSlideLayouts() as $layout) {
177            // @var $layout Slide\SlideLayout
178            $objWriter->startElement('p:sldLayoutId');
179            $objWriter->writeAttribute('id', $layout->layoutId);
180            $objWriter->writeAttribute('r:id', $layout->relationId);
181            $objWriter->endElement();
182        }
183        $objWriter->endElement();
184        // p:sldMaster\p:sldLayoutIdLst\
185
186        // p:sldMaster\p:txStyles
187        $objWriter->startElement('p:txStyles');
188        foreach ([
189            'p:titleStyle' => $pSlide->getTextStyles()->getTitleStyle(),
190            'p:bodyStyle' => $pSlide->getTextStyles()->getBodyStyle(),
191            'p:otherStyle' => $pSlide->getTextStyles()->getOtherStyle(),
192        ] as $startElement => $stylesArray) {
193            // titleStyle
194            $objWriter->startElement($startElement);
195            foreach ($stylesArray as $lvl => $oParagraph) {
196                /** @var RichText\Paragraph $oParagraph */
197                $elementName = (0 == $lvl ? 'a:defPPr' : 'a:lvl' . $lvl . 'pPr');
198                $objWriter->startElement($elementName);
199                $objWriter->writeAttribute('algn', $oParagraph->getAlignment()->getHorizontal());
200                $objWriter->writeAttributeIf(
201                    0 != $oParagraph->getAlignment()->getMarginLeft(),
202                    'marL',
203                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getMarginLeft())
204                );
205                $objWriter->writeAttributeIf(
206                    0 != $oParagraph->getAlignment()->getMarginRight(),
207                    'marR',
208                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getMarginRight())
209                );
210                $objWriter->writeAttributeIf(
211                    0 != $oParagraph->getAlignment()->getIndent(),
212                    'indent',
213                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getIndent())
214                );
215                $objWriter->startElement('a:defRPr');
216                $objWriter->writeAttributeIf(10 != $oParagraph->getFont()->getSize(), 'sz', $oParagraph->getFont()->getSize() * 100);
217                $objWriter->writeAttributeIf($oParagraph->getFont()->isBold(), 'b', 1);
218                $objWriter->writeAttributeIf($oParagraph->getFont()->isItalic(), 'i', 1);
219                $objWriter->writeAttribute('kern', '1200');
220                if ($oParagraph->getFont()->getColor() instanceof SchemeColor) {
221                    $objWriter->startElement('a:solidFill');
222                    $objWriter->startElement('a:schemeClr');
223                    $objWriter->writeAttribute('val', $oParagraph->getFont()->getColor()->getValue());
224                    $objWriter->endElement();
225                    $objWriter->endElement();
226                }
227                $objWriter->endElement();
228                $objWriter->endElement();
229            }
230            $objWriter->writeElement('a:extLst');
231            $objWriter->endElement();
232        }
233        $objWriter->endElement();
234        // p:sldMaster\p:txStyles\
235
236        if (null !== $pSlide->getTransition()) {
237            $this->writeSlideTransition($objWriter, $pSlide->getTransition());
238        }
239
240        // p:sldMaster\
241        $objWriter->endElement();
242
243        return $objWriter->getData();
244    }
245}