Completed
Push — develop ( e67d8e...ee77db )
by Franck
15s
created

AbstractDecoratorWriter::writeFill()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

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