Completed
Pull Request — develop (#565)
by
unknown
06:37
created

AbstractDecoratorWriter::writeFill()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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