Completed
Pull Request — develop (#190)
by Franck
08:58
created

AbstractDecoratorWriter::writeOutline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\Drawing as CommonDrawing;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\HashTable;
9
use PhpOffice\PhpPresentation\PhpPresentation;
10
use PhpOffice\PhpPresentation\Style\Border;
11
use PhpOffice\PhpPresentation\Style\Fill;
12
use PhpOffice\PhpPresentation\Style\Outline;
13
use \ZipArchive;
14
15
abstract class AbstractDecoratorWriter
16
{
17
    /**
18
     * @return ZipInterface
19
     */
20
    abstract public function render();
21
22
23
    /**
24
     * Write relationship
25
     *
26
     * @param  \PhpOffice\Common\XMLWriter $objWriter   XML Writer
27
     * @param  int                            $pId         Relationship ID. rId will be prepended!
28
     * @param  string                         $pType       Relationship type
29
     * @param  string                         $pTarget     Relationship target
30
     * @param  string                         $pTargetMode Relationship target mode
31
     * @throws \Exception
32
     */
33 88
    protected function writeRelationship(XMLWriter $objWriter, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
34
    {
35 88
        if ($pType != '' && $pTarget != '') {
36 88
            if (strpos($pId, 'rId') === false) {
37 88
                $pId = 'rId' . $pId;
38 88
            }
39
40
            // Write relationship
41 88
            $objWriter->startElement('Relationship');
42 88
            $objWriter->writeAttribute('Id', $pId);
43 88
            $objWriter->writeAttribute('Type', $pType);
44 88
            $objWriter->writeAttribute('Target', $pTarget);
45
46 88
            if ($pTargetMode != '') {
47 4
                $objWriter->writeAttribute('TargetMode', $pTargetMode);
48 4
            }
49
50 88
            $objWriter->endElement();
51 88
        } else {
52
            throw new \Exception("Invalid parameters passed.");
53
        }
54 88
    }
55
56
    /**
57
     * @var \PhpOffice\PhpPresentation\HashTable
58
     */
59
    protected $hashTable;
60
61 88
    public function setDrawingHashTable(HashTable $hashTable)
62
    {
63 88
        $this->hashTable = $hashTable;
64 88
        return $this;
65
    }
66
67
    /**
68
     * @return HashTable
69
     */
70 88
    public function getDrawingHashTable()
71
    {
72 88
        return $this->hashTable;
73
    }
74
75
    /**
76
     * @var ZipInterface
77
     */
78
    protected $oZip;
79
80 88
    public function setZip(ZipInterface $oZip)
81
    {
82 88
        $this->oZip = $oZip;
83 88
        return $this;
84
    }
85
86
    /**
87
     * @return ZipInterface
88
     */
89 88
    public function getZip()
90
    {
91 88
        return $this->oZip;
92
    }
93
94
    /**
95
     * @var PhpPresentation
96
     */
97
    protected $oPresentation;
98
99 88
    public function setPresentation(PhpPresentation $oPresentation)
100
    {
101 88
        $this->oPresentation = $oPresentation;
102 88
        return $this;
103
    }
104
105
    /**
106
     * @return PhpPresentation
107
     */
108 88
    public function getPresentation()
109
    {
110 88
        return $this->oPresentation;
111
    }
112
113
    /**
114
     * Write Border
115
     *
116
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
117
     * @param  \PhpOffice\PhpPresentation\Style\Border     $pBorder      Border
118
     * @param  string                         $pElementName Element name
119
     * @throws \Exception
120
     */
121 34
    protected function writeBorder(XMLWriter $objWriter, Border $pBorder, $pElementName = 'L')
122
    {
123
        // Line style
124 34
        $lineStyle = $pBorder->getLineStyle();
125 34
        if ($lineStyle == Border::LINE_NONE) {
126 9
            $lineStyle = Border::LINE_SINGLE;
127 9
        }
128
129
        // Line width
130 34
        $lineWidth = 12700 * $pBorder->getLineWidth();
131
132
        // a:ln $pElementName
133 34
        $objWriter->startElement('a:ln' . $pElementName);
134 34
        $objWriter->writeAttribute('w', $lineWidth);
135 34
        $objWriter->writeAttribute('cap', 'flat');
136 34
        $objWriter->writeAttribute('cmpd', $lineStyle);
137 34
        $objWriter->writeAttribute('algn', 'ctr');
138
139
        // Fill?
140 34
        if ($pBorder->getLineStyle() == Border::LINE_NONE) {
141
            // a:noFill
142 9
            $objWriter->writeElement('a:noFill', null);
143 9
        } else {
144
            // a:solidFill
145 34
            $objWriter->startElement('a:solidFill');
146
147
            // a:srgbClr
148 34
            $objWriter->startElement('a:srgbClr');
149 34
            $objWriter->writeAttribute('val', $pBorder->getColor()->getRGB());
150 34
            $objWriter->endElement();
151
152 34
            $objWriter->endElement();
153
        }
154
155
        // Dash
156 34
        $objWriter->startElement('a:prstDash');
157 34
        $objWriter->writeAttribute('val', $pBorder->getDashStyle());
158 34
        $objWriter->endElement();
159
160
        // a:round
161 34
        $objWriter->writeElement('a:round', null);
162
163
        // a:headEnd
164 34
        $objWriter->startElement('a:headEnd');
165 34
        $objWriter->writeAttribute('type', 'none');
166 34
        $objWriter->writeAttribute('w', 'med');
167 34
        $objWriter->writeAttribute('len', 'med');
168 34
        $objWriter->endElement();
169
170
        // a:tailEnd
171 34
        $objWriter->startElement('a:tailEnd');
172 34
        $objWriter->writeAttribute('type', 'none');
173 34
        $objWriter->writeAttribute('w', 'med');
174 34
        $objWriter->writeAttribute('len', 'med');
175 34
        $objWriter->endElement();
176
177 34
        $objWriter->endElement();
178 34
    }
179
180
    /**
181
     * Write Fill
182
     *
183
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
184
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
185
     * @throws \Exception
186
     */
187 52
    protected function writeFill(XMLWriter $objWriter, Fill $pFill)
188
    {
189
        // Is it a fill?
190 52
        if ($pFill->getFillType() == Fill::FILL_NONE) {
191 45
            return;
192
        }
193
194
        // Is it a solid fill?
195 13
        if ($pFill->getFillType() == Fill::FILL_SOLID) {
196 8
            $this->writeSolidFill($objWriter, $pFill);
197 8
            return;
198
        }
199
200
        // Check if this is a pattern type or gradient type
201 5
        if ($pFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $pFill->getFillType() == Fill::FILL_GRADIENT_PATH) {
202
            // Gradient fill
203 4
            $this->writeGradientFill($objWriter, $pFill);
204 4
        } else {
205
            // Pattern fill
206 1
            $this->writePatternFill($objWriter, $pFill);
207
        }
208 5
    }
209
210
    /**
211
     * Write Solid Fill
212
     *
213
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
214
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
215
     * @throws \Exception
216
     */
217 8
    protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill)
218
    {
219
        // a:gradFill
220 8
        $objWriter->startElement('a:solidFill');
221
222
        // srgbClr
223 8
        $objWriter->startElement('a:srgbClr');
224 8
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
225 8
        $objWriter->endElement();
226
227 8
        $objWriter->endElement();
228 8
    }
229
230
    /**
231
     * Write Gradient Fill
232
     *
233
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
234
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
235
     * @throws \Exception
236
     */
237 4
    protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill)
238
    {
239
        // a:gradFill
240 4
        $objWriter->startElement('a:gradFill');
241
242
        // a:gsLst
243 4
        $objWriter->startElement('a:gsLst');
244
        // a:gs
245 4
        $objWriter->startElement('a:gs');
246 4
        $objWriter->writeAttribute('pos', '0');
247
248
        // srgbClr
249 4
        $objWriter->startElement('a:srgbClr');
250 4
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
251 4
        $objWriter->endElement();
252
253 4
        $objWriter->endElement();
254
255
        // a:gs
256 4
        $objWriter->startElement('a:gs');
257 4
        $objWriter->writeAttribute('pos', '100000');
258
259
        // srgbClr
260 4
        $objWriter->startElement('a:srgbClr');
261 4
        $objWriter->writeAttribute('val', $pFill->getEndColor()->getRGB());
262 4
        $objWriter->endElement();
263
264 4
        $objWriter->endElement();
265
266 4
        $objWriter->endElement();
267
268
        // a:lin
269 4
        $objWriter->startElement('a:lin');
270 4
        $objWriter->writeAttribute('ang', CommonDrawing::degreesToAngle($pFill->getRotation()));
271 4
        $objWriter->writeAttribute('scaled', '0');
272 4
        $objWriter->endElement();
273
274 4
        $objWriter->endElement();
275 4
    }
276
277
    /**
278
     * Write Pattern Fill
279
     *
280
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
281
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
282
     * @throws \Exception
283
     */
284 1
    protected function writePatternFill(XMLWriter $objWriter, Fill $pFill)
285
    {
286
        // a:pattFill
287 1
        $objWriter->startElement('a:pattFill');
288
289
        // fgClr
290 1
        $objWriter->startElement('a:fgClr');
291
292
        // srgbClr
293 1
        $objWriter->startElement('a:srgbClr');
294 1
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
295 1
        $objWriter->endElement();
296
297 1
        $objWriter->endElement();
298
299
        // bgClr
300 1
        $objWriter->startElement('a:bgClr');
301
302
        // srgbClr
303 1
        $objWriter->startElement('a:srgbClr');
304 1
        $objWriter->writeAttribute('val', $pFill->getEndColor()->getRGB());
305 1
        $objWriter->endElement();
306
307 1
        $objWriter->endElement();
308
309 1
        $objWriter->endElement();
310 1
    }
311
312
    /**
313
     * Write Outline
314
     * @param XMLWriter $objWriter
315
     * @param Outline $oOutline
316
     */
317 10
    protected function writeOutline(XMLWriter $objWriter, $oOutline)
318
    {
319 10
        if (!$oOutline instanceof Outline) {
320 10
            return;
321
        }
322
        // Width : pts
323 2
        $width = $oOutline->getWidth();
324
        // Width : pts => px
325 2
        $width = CommonDrawing::pointsToPixels($width);
326
        // Width : px => emu
327 2
        $width = CommonDrawing::pixelsToEmu($width);
328
329
        // a:ln
330 2
        $objWriter->startElement('a:ln');
331 2
        $objWriter->writeAttribute('w', $width);
332
333
        // Fill
334 2
        $this->writeFill($objWriter, $oOutline->getFill());
335
336
        // > a:ln
337 2
        $objWriter->endElement();
338 2
    }
339
340
    /**
341
     * Determine absolute zip path
342
     *
343
     * @param  string $path
344
     * @return string
345
     */
346
    protected function absoluteZipPath($path)
347
    {
348
        $path      = str_replace(array(
349
            '/',
350
            '\\'
351
        ), DIRECTORY_SEPARATOR, $path);
352
        $parts     = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
353
        $absolutes = array();
354
        foreach ($parts as $part) {
355
            if ('.' == $part) {
356
                continue;
357
            }
358
            if ('..' == $part) {
359
                array_pop($absolutes);
360
            } else {
361
                $absolutes[] = $part;
362
            }
363
        }
364
365
        return implode('/', $absolutes);
366
    }
367
}
368