Completed
Pull Request — develop (#215)
by Franck
08:29
created

AbstractDecoratorWriter::writePatternFill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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