Complex classes like Slide often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Slide, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Slide extends AbstractPart |
||
45 | { |
||
46 | /** |
||
47 | * Write slide to XML format |
||
48 | * |
||
49 | * @param \PhpOffice\PhpPresentation\Slide $pSlide |
||
50 | * @return string XML Output |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | 61 | public function writeSlide(SlideElement $pSlide = null) |
|
54 | { |
||
55 | // Check slide |
||
56 | 61 | if (is_null($pSlide)) { |
|
57 | 1 | throw new \Exception("Invalid \PhpOffice\PhpPresentation\Slide object passed."); |
|
58 | } |
||
59 | |||
60 | // Create XML writer |
||
61 | 60 | $objWriter = $this->getXMLWriter(); |
|
62 | |||
63 | // XML header |
||
64 | 60 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
65 | |||
66 | // p:sld |
||
67 | 60 | $objWriter->startElement('p:sld'); |
|
68 | 60 | $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main'); |
|
69 | 60 | $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
|
70 | 60 | $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main'); |
|
71 | |||
72 | // p:cSld |
||
73 | 60 | $objWriter->startElement('p:cSld'); |
|
74 | |||
75 | // Background |
||
76 | 60 | if ($pSlide->getBackground() instanceof SlideElement\AbstractBackground) { |
|
77 | $oBackground = $pSlide->getBackground(); |
||
78 | // p:bg |
||
79 | $objWriter->startElement('p:bg'); |
||
80 | |||
81 | // p:bgPr |
||
82 | $objWriter->startElement('p:bgPr'); |
||
83 | |||
84 | if ($oBackground instanceof SlideElement\Background\Color) { |
||
85 | // a:solidFill |
||
86 | $objWriter->startElement('a:solidFill'); |
||
87 | |||
88 | // a:srgbClr |
||
89 | $objWriter->startElement('a:srgbClr'); |
||
90 | $objWriter->writeAttribute('val', $oBackground->getColor()->getRGB()); |
||
91 | $objWriter->endElement(); |
||
92 | |||
93 | // > a:solidFill |
||
94 | $objWriter->endElement(); |
||
95 | } |
||
96 | |||
97 | if ($oBackground instanceof SlideElement\Background\Image) { |
||
98 | // a:blipFill |
||
99 | $objWriter->startElement('a:blipFill'); |
||
100 | |||
101 | // a:blip |
||
102 | $objWriter->startElement('a:blip'); |
||
103 | $objWriter->writeAttribute('r:embed', $oBackground->relationId); |
||
104 | |||
105 | // > a:blipFill |
||
106 | $objWriter->endElement(); |
||
107 | |||
108 | // a:stretch |
||
109 | $objWriter->startElement('a:stretch'); |
||
110 | |||
111 | // a:fillRect |
||
112 | $objWriter->writeElement('a:fillRect'); |
||
113 | |||
114 | // > a:stretch |
||
115 | $objWriter->endElement(); |
||
116 | |||
117 | // > a:blipFill |
||
118 | $objWriter->endElement(); |
||
119 | } |
||
120 | |||
121 | // > p:bgPr |
||
122 | $objWriter->endElement(); |
||
123 | |||
124 | // > p:bg |
||
125 | $objWriter->endElement(); |
||
126 | } |
||
127 | |||
128 | // p:spTree |
||
129 | 60 | $objWriter->startElement('p:spTree'); |
|
130 | |||
131 | // p:nvGrpSpPr |
||
132 | 60 | $objWriter->startElement('p:nvGrpSpPr'); |
|
133 | |||
134 | // p:cNvPr |
||
135 | 60 | $objWriter->startElement('p:cNvPr'); |
|
136 | 60 | $objWriter->writeAttribute('id', '1'); |
|
137 | 60 | $objWriter->writeAttribute('name', ''); |
|
138 | 60 | $objWriter->endElement(); |
|
139 | |||
140 | // p:cNvGrpSpPr |
||
141 | 60 | $objWriter->writeElement('p:cNvGrpSpPr', null); |
|
142 | |||
143 | // p:nvPr |
||
144 | 60 | $objWriter->writeElement('p:nvPr', null); |
|
145 | |||
146 | 60 | $objWriter->endElement(); |
|
147 | |||
148 | // p:grpSpPr |
||
149 | 60 | $objWriter->startElement('p:grpSpPr'); |
|
150 | |||
151 | // a:xfrm |
||
152 | 60 | $objWriter->startElement('a:xfrm'); |
|
153 | |||
154 | // a:off |
||
155 | 60 | $objWriter->startElement('a:off'); |
|
156 | 60 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlide->getOffsetX())); |
|
157 | 60 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlide->getOffsetY())); |
|
158 | 60 | $objWriter->endElement(); // a:off |
|
159 | |||
160 | // a:ext |
||
161 | 60 | $objWriter->startElement('a:ext'); |
|
162 | 60 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlide->getExtentX())); |
|
163 | 60 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlide->getExtentY())); |
|
164 | 60 | $objWriter->endElement(); // a:ext |
|
165 | |||
166 | // a:chOff |
||
167 | 60 | $objWriter->startElement('a:chOff'); |
|
168 | 60 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlide->getOffsetX())); |
|
169 | 60 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlide->getOffsetY())); |
|
170 | 60 | $objWriter->endElement(); // a:chOff |
|
171 | |||
172 | // a:chExt |
||
173 | 60 | $objWriter->startElement('a:chExt'); |
|
174 | 60 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlide->getExtentX())); |
|
175 | 60 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlide->getExtentY())); |
|
176 | 60 | $objWriter->endElement(); // a:chExt |
|
177 | |||
178 | 60 | $objWriter->endElement(); |
|
179 | |||
180 | 60 | $objWriter->endElement(); |
|
181 | |||
182 | // Loop shapes |
||
183 | 60 | $shapeId = 1; |
|
184 | 60 | $shapes = $pSlide->getShapeCollection(); |
|
185 | 60 | foreach ($shapes as $shape) { |
|
186 | // Increment $shapeId |
||
187 | 53 | ++$shapeId; |
|
188 | |||
189 | // Check type |
||
190 | 53 | if ($shape instanceof RichText) { |
|
191 | 21 | $this->writeShapeText($objWriter, $shape, $shapeId); |
|
192 | 53 | } elseif ($shape instanceof Table) { |
|
193 | 10 | $this->writeShapeTable($objWriter, $shape, $shapeId); |
|
194 | 33 | } elseif ($shape instanceof Line) { |
|
195 | 2 | $this->writeShapeLine($objWriter, $shape, $shapeId); |
|
196 | 24 | } elseif ($shape instanceof ShapeChart) { |
|
197 | 20 | $this->writeShapeChart($objWriter, $shape, $shapeId); |
|
198 | 23 | } elseif ($shape instanceof AbstractDrawing) { |
|
199 | 4 | $this->writeShapePic($objWriter, $shape, $shapeId); |
|
200 | 4 | } elseif ($shape instanceof Group) { |
|
201 | $this->writeShapeGroup($objWriter, $shape, $shapeId); |
||
202 | } |
||
203 | 60 | } |
|
204 | |||
205 | // TODO |
||
206 | 60 | $objWriter->endElement(); |
|
207 | |||
208 | 60 | $objWriter->endElement(); |
|
209 | |||
210 | // p:clrMapOvr |
||
211 | 60 | $objWriter->startElement('p:clrMapOvr'); |
|
212 | |||
213 | // a:masterClrMapping |
||
214 | 60 | $objWriter->writeElement('a:masterClrMapping', null); |
|
215 | |||
216 | 60 | $objWriter->endElement(); |
|
217 | |||
218 | 60 | if (!is_null($pSlide->getTransition())) { |
|
219 | 1 | $this->writeTransition($objWriter, $pSlide->getTransition()); |
|
220 | 1 | } |
|
221 | |||
222 | //do animation writing |
||
223 | 60 | $shapeId = 1; |
|
224 | 60 | $shapes = $pSlide->getShapeCollection(); |
|
225 | |||
226 | 60 | $animations = $pSlide->getAnimations(); |
|
227 | |||
228 | 60 | if(count($animations) > 0){ |
|
|
|||
229 | |||
230 | $hashToIdMap = ARRAY(); |
||
231 | |||
232 | foreach($shapes as $shape){ |
||
233 | $shapeId += 1; |
||
234 | $hash = $shape->getHashCode(); |
||
235 | $hashToIdMap[$hash] = $shapeId; |
||
236 | } |
||
237 | |||
238 | $animationIds = ARRAY(); |
||
239 | |||
240 | foreach($animations as $animation){ |
||
241 | $shapesInAnimation = $animation->getShapeCollection(); |
||
242 | foreach($shapesInAnimation as $shape){ |
||
243 | $hash = $shape->getHashCode(); |
||
244 | $animationIds[] = $hashToIdMap[$hash]; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | |||
249 | $idCount = 1; |
||
250 | |||
251 | $objWriter->startElement('p:timing'); |
||
252 | |||
253 | $objWriter->startElement('p:tnLst'); |
||
254 | |||
255 | $objWriter->startElement('p:par'); |
||
256 | |||
257 | $objWriter->startElement('p:cTn'); |
||
258 | $objWriter->writeAttribute('id',$idCount); |
||
259 | $idCount += 1; |
||
260 | $objWriter->writeAttribute('dur','indefinite'); |
||
261 | $objWriter->writeAttribute('restart','never'); |
||
262 | $objWriter->writeAttribute('nodeType','tmRoot'); |
||
263 | |||
264 | $objWriter->startElement('p:childTnLst'); |
||
265 | |||
266 | $objWriter->startElement('p:seq'); |
||
267 | $objWriter->writeAttribute('concurrent','1'); |
||
268 | $objWriter->writeAttribute('nextAc','seek'); |
||
269 | |||
270 | $objWriter->startElement('p:cTn'); |
||
271 | $objWriter->writeAttribute('id',$idCount); |
||
272 | $idCount += 1; |
||
273 | $objWriter->writeAttribute('dur','indefinite'); |
||
274 | $objWriter->writeAttribute('nodeType','mainSeq'); |
||
275 | |||
276 | $objWriter->startElement("p:childTnLst"); |
||
277 | |||
278 | //each animation has multiple shapes |
||
279 | foreach($animations as $animation){ |
||
280 | |||
281 | $objWriter->startElement("p:par"); |
||
282 | |||
283 | $objWriter->startElement("p:cTn"); |
||
284 | $objWriter->writeAttribute("id",$idCount); |
||
285 | $idCount += 1; |
||
286 | $objWriter->writeAttribute("fill","hold"); |
||
287 | |||
288 | $objWriter->startElement('p:stCondLst'); |
||
289 | |||
290 | $objWriter->startElement('p:cond'); |
||
291 | $objWriter->writeAttribute("delay","indefinite"); |
||
292 | $objWriter->endElement(); |
||
293 | |||
294 | $objWriter->endElement(); |
||
295 | |||
296 | $objWriter->startElement("p:childTnLst"); |
||
297 | |||
298 | $objWriter->startElement("p:par"); |
||
299 | |||
300 | $objWriter->startElement("p:cTn"); |
||
301 | $objWriter->writeAttribute("id",$idCount); |
||
302 | $idCount += 1; |
||
303 | $objWriter->writeAttribute("fill","hold"); |
||
304 | |||
305 | $objWriter->startElement('p:stCondLst'); |
||
306 | |||
307 | $objWriter->startElement('p:cond'); |
||
308 | $objWriter->writeAttribute("delay","0"); |
||
309 | $objWriter->endElement(); |
||
310 | |||
311 | $objWriter->endElement(); |
||
312 | |||
313 | $objWriter->startElement("p:childTnLst"); |
||
314 | |||
315 | $shapesInAnimation = $animation->getShapeCollection(); |
||
316 | |||
317 | $firstShapeInAnimation = true; |
||
318 | |||
319 | foreach($shapesInAnimation as $shape){ |
||
320 | $hash = $shape->getHashCode(); |
||
321 | $shapeId = $hashToIdMap[$hash]; |
||
322 | |||
323 | $objWriter->startElement("p:par"); |
||
1 ignored issue
–
show
|
|||
324 | |||
325 | $objWriter->startElement("p:cTn"); |
||
1 ignored issue
–
show
|
|||
326 | $objWriter->writeAttribute("id", $idCount); |
||
1 ignored issue
–
show
|
|||
327 | $idCount += 1; |
||
328 | $objWriter->writeAttribute("presetID", "1"); |
||
1 ignored issue
–
show
|
|||
329 | $objWriter->writeAttribute("presetClass", "entr"); |
||
330 | $objWriter->writeAttribute("fill", "hold"); |
||
1 ignored issue
–
show
|
|||
331 | $objWriter->writeAttribute("presetSubtype", "0"); |
||
1 ignored issue
–
show
|
|||
332 | $objWriter->writeAttribute("grpId", "0"); |
||
333 | |||
334 | $nodeType = $firstShapeInAnimation ? "clickEffect" : "withEffect"; |
||
335 | |||
336 | $objWriter->writeAttribute("nodeType",$nodeType); |
||
337 | |||
338 | $objWriter->startElement("p:stCondLst"); |
||
1 ignored issue
–
show
|
|||
339 | |||
340 | $objWriter->startElement("p:cond"); |
||
1 ignored issue
–
show
|
|||
341 | $objWriter->writeAttribute("delay","0"); |
||
1 ignored issue
–
show
|
|||
342 | $objWriter->endElement(); |
||
343 | |||
344 | $objWriter->endElement(); |
||
345 | |||
346 | $objWriter->startElement("p:childTnLst"); |
||
1 ignored issue
–
show
|
|||
347 | |||
348 | $objWriter->startElement("p:set"); |
||
1 ignored issue
–
show
|
|||
349 | |||
350 | $objWriter->startElement("p:cBhvr"); |
||
1 ignored issue
–
show
|
|||
351 | |||
352 | $objWriter->startElement("p:cTn"); |
||
1 ignored issue
–
show
|
|||
353 | $objWriter->writeAttribute("id",$idCount); |
||
1 ignored issue
–
show
|
|||
354 | $idCount += 1; |
||
355 | $objWriter->writeAttribute("dur","1"); |
||
1 ignored issue
–
show
|
|||
356 | $objWriter->writeAttribute("fill","hold"); |
||
1 ignored issue
–
show
|
|||
357 | |||
358 | $objWriter->startElement("p:stCondLst"); |
||
1 ignored issue
–
show
|
|||
359 | |||
360 | $objWriter->startElement("p:cond"); |
||
1 ignored issue
–
show
|
|||
361 | $objWriter->writeAttribute("delay","0"); |
||
1 ignored issue
–
show
|
|||
362 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
363 | |||
364 | $objWriter->endElement(); |
||
365 | |||
366 | |||
367 | |||
368 | $objWriter->endElement(); |
||
369 | |||
370 | $objWriter->startElement("p:tgtEl"); |
||
1 ignored issue
–
show
|
|||
371 | |||
372 | $objWriter->startElement("p:spTgt"); |
||
1 ignored issue
–
show
|
|||
373 | //shape id |
||
374 | $objWriter->writeAttribute("spid",$shapeId); |
||
1 ignored issue
–
show
|
|||
375 | $objWriter->endElement(); |
||
376 | |||
377 | $objWriter->endElement(); |
||
378 | |||
379 | $objWriter->startElement("p:attrNameLst"); |
||
380 | |||
381 | $objWriter->writeElement("p:attrName","style.visibility"); |
||
382 | |||
383 | $objWriter->endElement(); |
||
384 | |||
385 | $objWriter->endElement(); |
||
386 | |||
387 | $objWriter->startElement("p:to"); |
||
1 ignored issue
–
show
|
|||
388 | |||
389 | $objWriter->startElement("p:strVal"); |
||
390 | $objWriter->writeAttribute("val","visible"); |
||
391 | $objWriter->endElement(); |
||
392 | |||
393 | $objWriter->endElement(); |
||
394 | |||
395 | $objWriter->endElement(); |
||
396 | |||
397 | $objWriter->endElement(); //end child tn lst |
||
398 | |||
399 | $objWriter->endElement(); //end ctn |
||
400 | |||
401 | $objWriter->endElement(); |
||
402 | |||
403 | $firstShapeInAnimation = false; |
||
404 | } |
||
405 | |||
406 | |||
407 | $objWriter->endElement(); //end child lst |
||
408 | |||
409 | $objWriter->endElement(); //end ctn |
||
410 | |||
411 | $objWriter->endElement(); //end par |
||
412 | |||
413 | |||
414 | $objWriter->endElement(); //end childtnlist |
||
415 | |||
416 | $objWriter->endElement(); //end ctn |
||
417 | |||
418 | $objWriter->endElement(); //end par |
||
419 | |||
420 | } |
||
1 ignored issue
–
show
|
|||
421 | |||
422 | |||
423 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
424 | |||
425 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
426 | |||
427 | ///prevCondLst |
||
428 | $objWriter->startElement("p:prevCondLst"); |
||
1 ignored issue
–
show
|
|||
429 | |||
430 | $objWriter->startElement('p:cond'); |
||
1 ignored issue
–
show
|
|||
431 | $objWriter->writeAttribute('evt','onPrev'); |
||
1 ignored issue
–
show
|
|||
432 | $objWriter->writeAttribute('delay','0'); |
||
1 ignored issue
–
show
|
|||
433 | |||
434 | $objWriter->startElement('p:tgtEl'); |
||
1 ignored issue
–
show
|
|||
435 | |||
436 | $objWriter->writeElement('p:sldTgt', null); |
||
1 ignored issue
–
show
|
|||
437 | |||
438 | $objWriter->endElement(); |
||
439 | |||
440 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
441 | |||
442 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
443 | |||
444 | //nextCondLst |
||
445 | $objWriter->startElement("p:nextCondLst"); |
||
1 ignored issue
–
show
|
|||
446 | |||
447 | $objWriter->startElement('p:cond'); |
||
1 ignored issue
–
show
|
|||
448 | $objWriter->writeAttribute('evt','onNext'); |
||
1 ignored issue
–
show
|
|||
449 | $objWriter->writeAttribute('delay','0'); |
||
1 ignored issue
–
show
|
|||
450 | |||
451 | $objWriter->startElement('p:tgtEl'); |
||
1 ignored issue
–
show
|
|||
452 | |||
453 | $objWriter->writeElement('p:sldTgt', null); |
||
1 ignored issue
–
show
|
|||
454 | |||
455 | $objWriter->endElement(); |
||
1 ignored issue
–
show
|
|||
456 | |||
457 | $objWriter->endElement(); |
||
458 | |||
459 | $objWriter->endElement(); |
||
460 | |||
461 | $objWriter->endElement(); //end p:seq |
||
462 | |||
463 | $objWriter->endElement(); //end p:childTnLst |
||
464 | |||
465 | $objWriter->endElement(); //end ctn |
||
466 | |||
467 | $objWriter->endElement(); //end par |
||
468 | |||
469 | $objWriter->endElement(); // end tnLst |
||
470 | |||
471 | $objWriter->startElement('p:bldLst'); |
||
1 ignored issue
–
show
|
|||
472 | |||
473 | //add in ids of all shapes in this slides animations |
||
474 | |||
475 | foreach($animationIds as $id){ |
||
476 | $objWriter->startElement('p:bldP'); |
||
1 ignored issue
–
show
|
|||
477 | $objWriter->writeAttribute("spid",$id); |
||
478 | $objWriter->writeAttribute('grpId',0); |
||
479 | $objWriter->endELement(); |
||
480 | } |
||
1 ignored issue
–
show
|
|||
481 | |||
482 | |||
483 | $objWriter->endElement(); // end bldLst |
||
484 | |||
485 | $objWriter->endElement(); //end timing |
||
486 | |||
487 | $objWriter->endElement(); |
||
488 | } |
||
1 ignored issue
–
show
|
|||
489 | |||
1 ignored issue
–
show
|
|||
490 | |||
491 | 60 | $objWriter->endElement(); |
|
492 | |||
493 | // Return |
||
494 | 60 | return $objWriter->getData(); |
|
495 | } |
||
496 | |||
497 | /** |
||
498 | * Write group |
||
499 | * |
||
500 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
501 | * @param \PhpOffice\PhpPresentation\Shape\Group $group |
||
502 | * @param int $shapeId |
||
503 | */ |
||
504 | private function writeShapeGroup(XMLWriter $objWriter, Group $group, &$shapeId) |
||
505 | { |
||
506 | // p:grpSp |
||
507 | $objWriter->startElement('p:grpSp'); |
||
508 | |||
509 | // p:nvGrpSpPr |
||
510 | $objWriter->startElement('p:nvGrpSpPr'); |
||
511 | |||
512 | // p:cNvPr |
||
513 | $objWriter->startElement('p:cNvPr'); |
||
514 | $objWriter->writeAttribute('name', 'Group '.$shapeId++); |
||
515 | $objWriter->writeAttribute('id', $shapeId); |
||
516 | $objWriter->endElement(); // p:cNvPr |
||
517 | // NOTE: Re: $shapeId This seems to be how PowerPoint 2010 does business. |
||
518 | |||
519 | // p:cNvGrpSpPr |
||
520 | $objWriter->writeElement('p:cNvGrpSpPr', null); |
||
521 | |||
522 | // p:nvPr |
||
523 | $objWriter->writeElement('p:nvPr', null); |
||
524 | |||
525 | $objWriter->endElement(); // p:nvGrpSpPr |
||
526 | |||
527 | // p:grpSpPr |
||
528 | $objWriter->startElement('p:grpSpPr'); |
||
529 | |||
530 | // a:xfrm |
||
531 | $objWriter->startElement('a:xfrm'); |
||
532 | |||
533 | // a:off |
||
534 | $objWriter->startElement('a:off'); |
||
535 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($group->getOffsetX())); |
||
536 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($group->getOffsetY())); |
||
537 | $objWriter->endElement(); // a:off |
||
538 | |||
539 | // a:ext |
||
540 | $objWriter->startElement('a:ext'); |
||
541 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getExtentX())); |
||
542 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getExtentY())); |
||
543 | $objWriter->endElement(); // a:ext |
||
544 | |||
545 | // a:chOff |
||
546 | $objWriter->startElement('a:chOff'); |
||
547 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($group->getOffsetX())); |
||
548 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($group->getOffsetY())); |
||
549 | $objWriter->endElement(); // a:chOff |
||
550 | |||
551 | // a:chExt |
||
552 | $objWriter->startElement('a:chExt'); |
||
553 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getExtentX())); |
||
554 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getExtentY())); |
||
555 | $objWriter->endElement(); // a:chExt |
||
556 | |||
557 | $objWriter->endElement(); // a:xfrm |
||
558 | |||
559 | $objWriter->endElement(); // p:grpSpPr |
||
560 | |||
561 | $shapes = $group->getShapeCollection(); |
||
562 | foreach ($shapes as $shape) { |
||
563 | // Increment $shapeId |
||
564 | ++$shapeId; |
||
565 | |||
566 | // Check type |
||
567 | if ($shape instanceof RichText) { |
||
568 | $this->writeShapeText($objWriter, $shape, $shapeId); |
||
569 | } elseif ($shape instanceof Table) { |
||
570 | $this->writeShapeTable($objWriter, $shape, $shapeId); |
||
571 | } elseif ($shape instanceof Line) { |
||
572 | $this->writeShapeLine($objWriter, $shape, $shapeId); |
||
573 | } elseif ($shape instanceof ShapeChart) { |
||
574 | $this->writeShapeChart($objWriter, $shape, $shapeId); |
||
575 | } elseif ($shape instanceof AbstractDrawing) { |
||
576 | $this->writeShapePic($objWriter, $shape, $shapeId); |
||
577 | } elseif ($shape instanceof Group) { |
||
578 | $this->writeShapeGroup($objWriter, $shape, $shapeId); |
||
579 | } |
||
580 | } |
||
581 | |||
582 | $objWriter->endElement(); // p:grpSp |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Write chart |
||
587 | * |
||
588 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
589 | * @param \PhpOffice\PhpPresentation\Shape\Chart $shape |
||
590 | * @param int $shapeId |
||
591 | */ |
||
592 | 20 | private function writeShapeChart(XMLWriter $objWriter, ShapeChart $shape, $shapeId) |
|
653 | |||
654 | /** |
||
655 | * Write pic |
||
656 | * |
||
657 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
658 | * @param \PhpOffice\PhpPresentation\Shape\AbstractDrawing $shape |
||
659 | * @param int $shapeId |
||
660 | * @throws \Exception |
||
661 | */ |
||
662 | 4 | private function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId) |
|
663 | { |
||
664 | // p:pic |
||
665 | 4 | $objWriter->startElement('p:pic'); |
|
666 | |||
667 | // p:nvPicPr |
||
668 | 4 | $objWriter->startElement('p:nvPicPr'); |
|
669 | |||
670 | // p:cNvPr |
||
671 | 4 | $objWriter->startElement('p:cNvPr'); |
|
672 | 4 | $objWriter->writeAttribute('id', $shapeId); |
|
673 | 4 | $objWriter->writeAttribute('name', $shape->getName()); |
|
674 | 4 | $objWriter->writeAttribute('descr', $shape->getDescription()); |
|
675 | |||
676 | // a:hlinkClick |
||
677 | 4 | if ($shape->hasHyperlink()) { |
|
678 | 1 | $this->writeHyperlink($objWriter, $shape); |
|
679 | 1 | } |
|
680 | |||
681 | 4 | $objWriter->endElement(); |
|
682 | |||
683 | // p:cNvPicPr |
||
684 | 4 | $objWriter->startElement('p:cNvPicPr'); |
|
685 | |||
686 | // a:picLocks |
||
687 | 4 | $objWriter->startElement('a:picLocks'); |
|
688 | 4 | $objWriter->writeAttribute('noChangeAspect', '1'); |
|
689 | 4 | $objWriter->endElement(); |
|
690 | |||
691 | 4 | $objWriter->endElement(); |
|
692 | |||
693 | // p:nvPr |
||
694 | 4 | $objWriter->writeElement('p:nvPr', null); |
|
695 | 4 | $objWriter->endElement(); |
|
696 | |||
697 | // p:blipFill |
||
698 | 4 | $objWriter->startElement('p:blipFill'); |
|
699 | |||
700 | // a:blip |
||
701 | 4 | $objWriter->startElement('a:blip'); |
|
702 | 4 | $objWriter->writeAttribute('r:embed', $shape->relationId); |
|
703 | 4 | $objWriter->endElement(); |
|
704 | |||
705 | // a:stretch |
||
706 | 4 | $objWriter->startElement('a:stretch'); |
|
707 | 4 | $objWriter->writeElement('a:fillRect', null); |
|
708 | 4 | $objWriter->endElement(); |
|
709 | |||
710 | 4 | $objWriter->endElement(); |
|
711 | |||
712 | // p:spPr |
||
713 | 4 | $objWriter->startElement('p:spPr'); |
|
714 | |||
715 | // a:xfrm |
||
716 | 4 | $objWriter->startElement('a:xfrm'); |
|
717 | 4 | $objWriter->writeAttribute('rot', CommonDrawing::degreesToAngle($shape->getRotation())); |
|
718 | |||
719 | // a:off |
||
720 | 4 | $objWriter->startElement('a:off'); |
|
721 | 4 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); |
|
722 | 4 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); |
|
723 | 4 | $objWriter->endElement(); |
|
724 | |||
725 | // a:ext |
||
726 | 4 | $objWriter->startElement('a:ext'); |
|
727 | 4 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); |
|
728 | 4 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); |
|
729 | 4 | $objWriter->endElement(); |
|
730 | |||
731 | 4 | $objWriter->endElement(); |
|
732 | |||
733 | // a:prstGeom |
||
734 | 4 | $objWriter->startElement('a:prstGeom'); |
|
735 | 4 | $objWriter->writeAttribute('prst', 'rect'); |
|
736 | |||
737 | // a:avLst |
||
738 | 4 | $objWriter->writeElement('a:avLst', null); |
|
739 | |||
740 | 4 | $objWriter->endElement(); |
|
741 | |||
742 | 4 | if ($shape->getBorder()->getLineStyle() != Border::LINE_NONE) { |
|
743 | 1 | $this->writeBorder($objWriter, $shape->getBorder(), ''); |
|
744 | 1 | } |
|
745 | |||
746 | 4 | if ($shape->getShadow()->isVisible()) { |
|
747 | 1 | $this->writeShadow($objWriter, $shape->getShadow()); |
|
748 | 1 | } |
|
749 | |||
750 | 4 | $objWriter->endElement(); |
|
751 | |||
752 | 4 | $objWriter->endElement(); |
|
753 | 4 | } |
|
754 | |||
755 | /** |
||
756 | * Write txt |
||
757 | * |
||
758 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
759 | * @param \PhpOffice\PhpPresentation\Shape\RichText $shape |
||
760 | * @param int $shapeId |
||
761 | * @throws \Exception |
||
762 | */ |
||
763 | 21 | private function writeShapeText(XMLWriter $objWriter, RichText $shape, $shapeId) |
|
764 | { |
||
765 | // p:sp |
||
766 | 21 | $objWriter->startElement('p:sp'); |
|
767 | |||
768 | // p:sp\p:nvSpPr |
||
769 | 21 | $objWriter->startElement('p:nvSpPr'); |
|
770 | |||
771 | // p:sp\p:nvSpPr\p:cNvPr |
||
772 | 21 | $objWriter->startElement('p:cNvPr'); |
|
773 | 21 | $objWriter->writeAttribute('id', $shapeId); |
|
774 | 21 | $objWriter->writeAttribute('name', ''); |
|
775 | |||
776 | // Hyperlink |
||
777 | 21 | if ($shape->hasHyperlink()) { |
|
778 | 1 | $this->writeHyperlink($objWriter, $shape); |
|
779 | 1 | } |
|
780 | // > p:sp\p:nvSpPr |
||
781 | 21 | $objWriter->endElement(); |
|
782 | |||
783 | // p:sp\p:cNvSpPr |
||
784 | 21 | $objWriter->startElement('p:cNvSpPr'); |
|
785 | 21 | $objWriter->writeAttribute('txBox', '1'); |
|
786 | 21 | $objWriter->endElement(); |
|
787 | // p:sp\p:cNvSpPr\p:nvPr |
||
788 | 21 | $objWriter->writeElement('p:nvPr', null); |
|
789 | // > p:sp\p:cNvSpPr |
||
790 | 21 | $objWriter->endElement(); |
|
791 | |||
792 | // p:sp\p:spPr |
||
793 | 21 | $objWriter->startElement('p:spPr'); |
|
794 | |||
795 | // p:sp\p:spPr\a:xfrm |
||
796 | 21 | $objWriter->startElement('a:xfrm'); |
|
797 | 21 | $objWriter->writeAttribute('rot', CommonDrawing::degreesToAngle($shape->getRotation())); |
|
798 | |||
799 | // p:sp\p:spPr\a:xfrm\a:off |
||
800 | 21 | $objWriter->startElement('a:off'); |
|
801 | 21 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); |
|
802 | 21 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); |
|
803 | 21 | $objWriter->endElement(); |
|
804 | |||
805 | // p:sp\p:spPr\a:xfrm\a:ext |
||
806 | 21 | $objWriter->startElement('a:ext'); |
|
807 | 21 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); |
|
808 | 21 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); |
|
809 | 21 | $objWriter->endElement(); |
|
810 | |||
811 | // > p:sp\p:spPr\a:xfrm |
||
812 | 21 | $objWriter->endElement(); |
|
813 | |||
814 | // p:sp\p:spPr\a:prstGeom |
||
815 | 21 | $objWriter->startElement('a:prstGeom'); |
|
816 | 21 | $objWriter->writeAttribute('prst', 'rect'); |
|
817 | 21 | $objWriter->endElement(); |
|
818 | |||
819 | 21 | if ($shape->getFill()) { |
|
820 | 21 | $this->writeFill($objWriter, $shape->getFill()); |
|
821 | 21 | } |
|
822 | 21 | if ($shape->getBorder()->getLineStyle() != Border::LINE_NONE) { |
|
823 | $this->writeBorder($objWriter, $shape->getBorder(), ''); |
||
824 | } |
||
825 | 21 | if ($shape->getShadow()->isVisible()) { |
|
826 | 1 | $this->writeShadow($objWriter, $shape->getShadow()); |
|
827 | 1 | } |
|
828 | // > p:sp\p:spPr |
||
829 | 21 | $objWriter->endElement(); |
|
830 | |||
831 | // p:txBody |
||
832 | 21 | $objWriter->startElement('p:txBody'); |
|
833 | |||
834 | // a:bodyPr |
||
835 | //@link :http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.bodyproperties%28v=office.14%29.aspx |
||
836 | 21 | $objWriter->startElement('a:bodyPr'); |
|
837 | 21 | $verticalAlign = $shape->getActiveParagraph()->getAlignment()->getVertical(); |
|
838 | 21 | if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) { |
|
839 | 3 | $objWriter->writeAttribute('anchor', $verticalAlign); |
|
840 | 3 | } |
|
841 | 21 | if ($shape->getWrap() != RichText::WRAP_SQUARE) { |
|
842 | $objWriter->writeAttribute('wrap', $shape->getWrap()); |
||
843 | } |
||
844 | 21 | $objWriter->writeAttribute('rtlCol', '0'); |
|
845 | |||
846 | 21 | if ($shape->getHorizontalOverflow() != RichText::OVERFLOW_OVERFLOW) { |
|
847 | $objWriter->writeAttribute('horzOverflow', $shape->getHorizontalOverflow()); |
||
848 | } |
||
849 | 21 | if ($shape->getVerticalOverflow() != RichText::OVERFLOW_OVERFLOW) { |
|
850 | $objWriter->writeAttribute('vertOverflow', $shape->getVerticalOverflow()); |
||
851 | } |
||
852 | |||
853 | 21 | if ($shape->isUpright()) { |
|
854 | 1 | $objWriter->writeAttribute('upright', '1'); |
|
855 | 1 | } |
|
856 | 21 | if ($shape->isVertical()) { |
|
857 | 1 | $objWriter->writeAttribute('vert', 'vert'); |
|
858 | 1 | } |
|
859 | |||
860 | 21 | $objWriter->writeAttribute('bIns', CommonDrawing::pixelsToEmu($shape->getInsetBottom())); |
|
861 | 21 | $objWriter->writeAttribute('lIns', CommonDrawing::pixelsToEmu($shape->getInsetLeft())); |
|
862 | 21 | $objWriter->writeAttribute('rIns', CommonDrawing::pixelsToEmu($shape->getInsetRight())); |
|
863 | 21 | $objWriter->writeAttribute('tIns', CommonDrawing::pixelsToEmu($shape->getInsetTop())); |
|
864 | |||
865 | 21 | if ($shape->getColumns() <> 1) { |
|
866 | $objWriter->writeAttribute('numCol', $shape->getColumns()); |
||
867 | } |
||
868 | |||
869 | // a:spAutoFit |
||
870 | 21 | $objWriter->startElement('a:' . $shape->getAutoFit()); |
|
871 | 21 | if ($shape->getAutoFit() == RichText::AUTOFIT_NORMAL) { |
|
872 | 1 | if (!is_null($shape->getFontScale())) { |
|
873 | 1 | $objWriter->writeAttribute('fontScale', (int)($shape->getFontScale() * 1000)); |
|
874 | 1 | } |
|
875 | 1 | if (!is_null($shape->getLineSpaceReduction())) { |
|
876 | 1 | $objWriter->writeAttribute('lnSpcReduction', (int)($shape->getLineSpaceReduction() * 1000)); |
|
877 | 1 | } |
|
878 | 1 | } |
|
879 | |||
880 | 21 | $objWriter->endElement(); |
|
881 | |||
882 | 21 | $objWriter->endElement(); |
|
883 | |||
884 | // a:lstStyle |
||
885 | 21 | $objWriter->writeElement('a:lstStyle', null); |
|
886 | |||
887 | // Write paragraphs |
||
888 | 21 | $this->writeParagraphs($objWriter, $shape->getParagraphs()); |
|
889 | |||
890 | 21 | $objWriter->endElement(); |
|
891 | |||
892 | 21 | $objWriter->endElement(); |
|
893 | 21 | } |
|
894 | |||
895 | /** |
||
896 | * Write table |
||
897 | * |
||
898 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
899 | * @param \PhpOffice\PhpPresentation\Shape\Table $shape |
||
900 | * @param int $shapeId |
||
901 | * @throws \Exception |
||
902 | */ |
||
903 | 10 | private function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId) |
|
904 | { |
||
905 | // p:graphicFrame |
||
906 | 10 | $objWriter->startElement('p:graphicFrame'); |
|
907 | |||
908 | // p:nvGraphicFramePr |
||
909 | 10 | $objWriter->startElement('p:nvGraphicFramePr'); |
|
910 | |||
911 | // p:cNvPr |
||
912 | 10 | $objWriter->startElement('p:cNvPr'); |
|
913 | 10 | $objWriter->writeAttribute('id', $shapeId); |
|
914 | 10 | $objWriter->writeAttribute('name', $shape->getName()); |
|
915 | 10 | $objWriter->writeAttribute('descr', $shape->getDescription()); |
|
916 | |||
917 | 10 | $objWriter->endElement(); |
|
918 | |||
919 | // p:cNvGraphicFramePr |
||
920 | 10 | $objWriter->startElement('p:cNvGraphicFramePr'); |
|
921 | |||
922 | // a:graphicFrameLocks |
||
923 | 10 | $objWriter->startElement('a:graphicFrameLocks'); |
|
924 | 10 | $objWriter->writeAttribute('noGrp', '1'); |
|
925 | 10 | $objWriter->endElement(); |
|
926 | |||
927 | 10 | $objWriter->endElement(); |
|
928 | |||
929 | // p:nvPr |
||
930 | 10 | $objWriter->writeElement('p:nvPr', null); |
|
931 | |||
932 | 10 | $objWriter->endElement(); |
|
933 | |||
934 | // p:xfrm |
||
935 | 10 | $objWriter->startElement('p:xfrm'); |
|
936 | |||
937 | // a:off |
||
938 | 10 | $objWriter->startElement('a:off'); |
|
939 | 10 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); |
|
940 | 10 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); |
|
941 | 10 | $objWriter->endElement(); |
|
942 | |||
943 | // a:ext |
||
944 | 10 | $objWriter->startElement('a:ext'); |
|
945 | 10 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); |
|
946 | 10 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); |
|
947 | 10 | $objWriter->endElement(); |
|
948 | |||
949 | 10 | $objWriter->endElement(); |
|
950 | |||
951 | // a:graphic |
||
952 | 10 | $objWriter->startElement('a:graphic'); |
|
953 | |||
954 | // a:graphicData |
||
955 | 10 | $objWriter->startElement('a:graphicData'); |
|
956 | 10 | $objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/table'); |
|
957 | |||
958 | // a:tbl |
||
959 | 10 | $objWriter->startElement('a:tbl'); |
|
960 | |||
961 | // a:tblPr |
||
962 | 10 | $objWriter->startElement('a:tblPr'); |
|
963 | 10 | $objWriter->writeAttribute('firstRow', '1'); |
|
964 | 10 | $objWriter->writeAttribute('bandRow', '1'); |
|
965 | |||
966 | 10 | $objWriter->endElement(); |
|
967 | |||
968 | // a:tblGrid |
||
969 | 10 | $objWriter->startElement('a:tblGrid'); |
|
970 | |||
971 | // Write cell widths |
||
972 | 10 | $countCells = count($shape->getRow(0)->getCells()); |
|
973 | 10 | for ($cell = 0; $cell < $countCells; $cell++) { |
|
974 | // a:gridCol |
||
975 | 10 | $objWriter->startElement('a:gridCol'); |
|
976 | |||
977 | // Calculate column width |
||
978 | 10 | $width = $shape->getRow(0)->getCell($cell)->getWidth(); |
|
979 | 10 | if ($width == 0) { |
|
980 | 10 | $colCount = count($shape->getRow(0)->getCells()); |
|
981 | 10 | $totalWidth = $shape->getWidth(); |
|
982 | 10 | $width = $totalWidth / $colCount; |
|
983 | 10 | } |
|
984 | |||
985 | 10 | $objWriter->writeAttribute('w', CommonDrawing::pixelsToEmu($width)); |
|
986 | 10 | $objWriter->endElement(); |
|
987 | 10 | } |
|
988 | |||
989 | 10 | $objWriter->endElement(); |
|
990 | |||
991 | // Colspan / rowspan containers |
||
992 | 10 | $colSpan = array(); |
|
993 | 10 | $rowSpan = array(); |
|
994 | |||
995 | // Default border style |
||
996 | 10 | $defaultBorder = new Border(); |
|
997 | |||
998 | // Write rows |
||
999 | 10 | $countRows = count($shape->getRows()); |
|
1000 | 10 | for ($row = 0; $row < $countRows; $row++) { |
|
1001 | // a:tr |
||
1002 | 10 | $objWriter->startElement('a:tr'); |
|
1003 | 10 | $objWriter->writeAttribute('h', CommonDrawing::pixelsToEmu($shape->getRow($row)->getHeight())); |
|
1004 | |||
1005 | // Write cells |
||
1006 | 10 | $countCells = count($shape->getRow($row)->getCells()); |
|
1007 | 10 | for ($cell = 0; $cell < $countCells; $cell++) { |
|
1008 | // Current cell |
||
1009 | 10 | $currentCell = $shape->getRow($row)->getCell($cell); |
|
1010 | |||
1011 | // Next cell right |
||
1012 | 10 | $nextCellRight = $shape->getRow($row)->getCell($cell + 1, true); |
|
1013 | |||
1014 | // Next cell below |
||
1015 | 10 | $nextRowBelow = $shape->getRow($row + 1, true); |
|
1016 | 10 | $nextCellBelow = null; |
|
1017 | 10 | if ($nextRowBelow != null) { |
|
1018 | 2 | $nextCellBelow = $nextRowBelow->getCell($cell, true); |
|
1019 | 2 | } |
|
1020 | |||
1021 | // a:tc |
||
1022 | 10 | $objWriter->startElement('a:tc'); |
|
1023 | // Colspan |
||
1024 | 10 | if ($currentCell->getColSpan() > 1) { |
|
1025 | 1 | $objWriter->writeAttribute('gridSpan', $currentCell->getColSpan()); |
|
1026 | 1 | $colSpan[$row] = $currentCell->getColSpan() - 1; |
|
1027 | 10 | } elseif (isset($colSpan[$row]) && $colSpan[$row] > 0) { |
|
1028 | 1 | $colSpan[$row]--; |
|
1029 | 1 | $objWriter->writeAttribute('hMerge', '1'); |
|
1030 | 1 | } |
|
1031 | |||
1032 | // Rowspan |
||
1033 | 10 | if ($currentCell->getRowSpan() > 1) { |
|
1034 | 1 | $objWriter->writeAttribute('rowSpan', $currentCell->getRowSpan()); |
|
1035 | 1 | $rowSpan[$cell] = $currentCell->getRowSpan() - 1; |
|
1036 | 10 | } elseif (isset($rowSpan[$cell]) && $rowSpan[$cell] > 0) { |
|
1037 | 1 | $rowSpan[$cell]--; |
|
1038 | 1 | $objWriter->writeAttribute('vMerge', '1'); |
|
1039 | 1 | } |
|
1040 | |||
1041 | // a:txBody |
||
1042 | 10 | $objWriter->startElement('a:txBody'); |
|
1043 | |||
1044 | // a:bodyPr |
||
1045 | 10 | $objWriter->startElement('a:bodyPr'); |
|
1046 | 10 | $objWriter->writeAttribute('wrap', 'square'); |
|
1047 | 10 | $objWriter->writeAttribute('rtlCol', '0'); |
|
1048 | |||
1049 | // a:spAutoFit |
||
1050 | 10 | $objWriter->writeElement('a:spAutoFit', null); |
|
1051 | |||
1052 | 10 | $objWriter->endElement(); |
|
1053 | |||
1054 | // a:lstStyle |
||
1055 | 10 | $objWriter->writeElement('a:lstStyle', null); |
|
1056 | |||
1057 | // Write paragraphs |
||
1058 | 10 | $this->writeParagraphs($objWriter, $currentCell->getParagraphs()); |
|
1059 | |||
1060 | 10 | $objWriter->endElement(); |
|
1061 | |||
1062 | // a:tcPr |
||
1063 | 10 | $objWriter->startElement('a:tcPr'); |
|
1064 | // Alignment (horizontal) |
||
1065 | 10 | $firstParagraph = $currentCell->getParagraph(0); |
|
1066 | 10 | $verticalAlign = $firstParagraph->getAlignment()->getVertical(); |
|
1067 | 10 | if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) { |
|
1068 | 1 | $objWriter->writeAttribute('anchor', $verticalAlign); |
|
1069 | 1 | } |
|
1070 | |||
1071 | // Determine borders |
||
1072 | 10 | $borderLeft = $currentCell->getBorders()->getLeft(); |
|
1073 | 10 | $borderRight = $currentCell->getBorders()->getRight(); |
|
1074 | 10 | $borderTop = $currentCell->getBorders()->getTop(); |
|
1075 | 10 | $borderBottom = $currentCell->getBorders()->getBottom(); |
|
1076 | 10 | $borderDiagonalDown = $currentCell->getBorders()->getDiagonalDown(); |
|
1077 | 10 | $borderDiagonalUp = $currentCell->getBorders()->getDiagonalUp(); |
|
1078 | |||
1079 | // Fix PowerPoint implementation |
||
1080 | 10 | if (!is_null($nextCellRight) && $nextCellRight->getBorders()->getRight()->getHashCode() != $defaultBorder->getHashCode()) { |
|
1081 | 1 | $borderRight = $nextCellRight->getBorders()->getLeft(); |
|
1082 | 1 | } |
|
1083 | 10 | if (!is_null($nextCellBelow) && $nextCellBelow->getBorders()->getBottom()->getHashCode() != $defaultBorder->getHashCode()) { |
|
1084 | 1 | $borderBottom = $nextCellBelow->getBorders()->getTop(); |
|
1085 | 1 | } |
|
1086 | |||
1087 | // Write borders |
||
1088 | 10 | $this->writeBorder($objWriter, $borderLeft, 'L'); |
|
1089 | 10 | $this->writeBorder($objWriter, $borderRight, 'R'); |
|
1090 | 10 | $this->writeBorder($objWriter, $borderTop, 'T'); |
|
1091 | 10 | $this->writeBorder($objWriter, $borderBottom, 'B'); |
|
1092 | 10 | $this->writeBorder($objWriter, $borderDiagonalDown, 'TlToBr'); |
|
1093 | 10 | $this->writeBorder($objWriter, $borderDiagonalUp, 'BlToTr'); |
|
1094 | |||
1095 | // Fill |
||
1096 | 10 | $this->writeFill($objWriter, $currentCell->getFill()); |
|
1097 | |||
1098 | 10 | $objWriter->endElement(); |
|
1099 | |||
1100 | 10 | $objWriter->endElement(); |
|
1101 | 10 | } |
|
1102 | |||
1103 | 10 | $objWriter->endElement(); |
|
1104 | 10 | } |
|
1105 | |||
1106 | 10 | $objWriter->endElement(); |
|
1107 | |||
1108 | 10 | $objWriter->endElement(); |
|
1109 | |||
1110 | 10 | $objWriter->endElement(); |
|
1111 | |||
1112 | 10 | $objWriter->endElement(); |
|
1113 | 10 | } |
|
1114 | |||
1115 | /** |
||
1116 | * Write paragraphs |
||
1117 | * |
||
1118 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1119 | * @param \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs |
||
1120 | * @throws \Exception |
||
1121 | */ |
||
1122 | 31 | private function writeParagraphs(XMLWriter $objWriter, $paragraphs) |
|
1123 | { |
||
1124 | // Loop trough paragraphs |
||
1125 | 31 | foreach ($paragraphs as $paragraph) { |
|
1126 | // a:p |
||
1127 | 31 | $objWriter->startElement('a:p'); |
|
1128 | |||
1129 | // a:pPr |
||
1130 | 31 | $objWriter->startElement('a:pPr'); |
|
1131 | 31 | $objWriter->writeAttribute('algn', $paragraph->getAlignment()->getHorizontal()); |
|
1132 | 31 | $objWriter->writeAttribute('fontAlgn', $paragraph->getAlignment()->getVertical()); |
|
1133 | 31 | $objWriter->writeAttribute('marL', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginLeft())); |
|
1134 | 31 | $objWriter->writeAttribute('marR', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginRight())); |
|
1135 | 31 | $objWriter->writeAttribute('indent', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getIndent())); |
|
1136 | 31 | $objWriter->writeAttribute('lvl', $paragraph->getAlignment()->getLevel()); |
|
1137 | |||
1138 | // Bullet type specified? |
||
1139 | 31 | if ($paragraph->getBulletStyle()->getBulletType() != Bullet::TYPE_NONE) { |
|
1140 | // a:buFont |
||
1141 | 2 | $objWriter->startElement('a:buFont'); |
|
1142 | 2 | $objWriter->writeAttribute('typeface', $paragraph->getBulletStyle()->getBulletFont()); |
|
1143 | 2 | $objWriter->endElement(); |
|
1144 | |||
1145 | 2 | if ($paragraph->getBulletStyle()->getBulletType() == Bullet::TYPE_BULLET) { |
|
1146 | // a:buChar |
||
1147 | 1 | $objWriter->startElement('a:buChar'); |
|
1148 | 1 | $objWriter->writeAttribute('char', $paragraph->getBulletStyle()->getBulletChar()); |
|
1149 | 1 | $objWriter->endElement(); |
|
1150 | 2 | } elseif ($paragraph->getBulletStyle()->getBulletType() == Bullet::TYPE_NUMERIC) { |
|
1151 | // a:buAutoNum |
||
1152 | 1 | $objWriter->startElement('a:buAutoNum'); |
|
1153 | 1 | $objWriter->writeAttribute('type', $paragraph->getBulletStyle()->getBulletNumericStyle()); |
|
1154 | 1 | if ($paragraph->getBulletStyle()->getBulletNumericStartAt() != 1) { |
|
1155 | 1 | $objWriter->writeAttribute('startAt', $paragraph->getBulletStyle()->getBulletNumericStartAt()); |
|
1156 | 1 | } |
|
1157 | 1 | $objWriter->endElement(); |
|
1158 | 1 | } |
|
1159 | 2 | } |
|
1160 | |||
1161 | 31 | $objWriter->endElement(); |
|
1162 | |||
1163 | // Loop trough rich text elements |
||
1164 | 31 | $elements = $paragraph->getRichTextElements(); |
|
1165 | 31 | foreach ($elements as $element) { |
|
1166 | 26 | if ($element instanceof BreakElement) { |
|
1167 | // a:br |
||
1168 | 1 | $objWriter->writeElement('a:br', null); |
|
1169 | 26 | } elseif ($element instanceof Run || $element instanceof TextElement) { |
|
1170 | // a:r |
||
1171 | 25 | $objWriter->startElement('a:r'); |
|
1172 | |||
1173 | // a:rPr |
||
1174 | 25 | if ($element instanceof Run) { |
|
1175 | // a:rPr |
||
1176 | 25 | $objWriter->startElement('a:rPr'); |
|
1177 | |||
1178 | // Bold |
||
1179 | 25 | $objWriter->writeAttribute('b', ($element->getFont()->isBold() ? '1' : '0')); |
|
1180 | |||
1181 | // Italic |
||
1182 | 25 | $objWriter->writeAttribute('i', ($element->getFont()->isItalic() ? '1' : '0')); |
|
1183 | |||
1184 | // Strikethrough |
||
1185 | 25 | $objWriter->writeAttribute('strike', ($element->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); |
|
1186 | |||
1187 | // Size |
||
1188 | 25 | $objWriter->writeAttribute('sz', ($element->getFont()->getSize() * 100)); |
|
1189 | |||
1190 | // Underline |
||
1191 | 25 | $objWriter->writeAttribute('u', $element->getFont()->getUnderline()); |
|
1192 | |||
1193 | // Superscript / subscript |
||
1194 | 25 | if ($element->getFont()->isSuperScript() || $element->getFont()->isSubScript()) { |
|
1195 | 2 | if ($element->getFont()->isSuperScript()) { |
|
1196 | 1 | $objWriter->writeAttribute('baseline', '30000'); |
|
1197 | 2 | } elseif ($element->getFont()->isSubScript()) { |
|
1198 | 1 | $objWriter->writeAttribute('baseline', '-25000'); |
|
1199 | 1 | } |
|
1200 | 2 | } |
|
1201 | |||
1202 | // Color - a:solidFill |
||
1203 | 25 | $objWriter->startElement('a:solidFill'); |
|
1204 | |||
1205 | // a:srgbClr |
||
1206 | 25 | $objWriter->startElement('a:srgbClr'); |
|
1207 | 25 | $objWriter->writeAttribute('val', $element->getFont()->getColor()->getRGB()); |
|
1208 | 25 | $objWriter->endElement(); |
|
1209 | |||
1210 | 25 | $objWriter->endElement(); |
|
1211 | |||
1212 | // Font - a:latin |
||
1213 | 25 | $objWriter->startElement('a:latin'); |
|
1214 | 25 | $objWriter->writeAttribute('typeface', $element->getFont()->getName()); |
|
1215 | 25 | $objWriter->endElement(); |
|
1216 | |||
1217 | // a:hlinkClick |
||
1218 | 25 | if ($element->hasHyperlink()) { |
|
1219 | 3 | $this->writeHyperlink($objWriter, $element); |
|
1220 | 3 | } |
|
1221 | |||
1222 | 25 | $objWriter->endElement(); |
|
1223 | 25 | } |
|
1224 | |||
1225 | // t |
||
1226 | 25 | $objWriter->startElement('a:t'); |
|
1227 | 25 | $objWriter->writeCData(Text::controlCharacterPHP2OOXML($element->getText())); |
|
1228 | 25 | $objWriter->endElement(); |
|
1229 | |||
1230 | 25 | $objWriter->endElement(); |
|
1231 | 25 | } |
|
1232 | 31 | } |
|
1233 | |||
1234 | 31 | $objWriter->endElement(); |
|
1235 | 31 | } |
|
1236 | 31 | } |
|
1237 | |||
1238 | /** |
||
1239 | * Write Line Shape |
||
1240 | * |
||
1241 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1242 | * @param \PhpOffice\PhpPresentation\Shape\Line $shape |
||
1243 | * @param int $shapeId |
||
1244 | */ |
||
1245 | 2 | private function writeShapeLine(XMLWriter $objWriter, Line $shape, $shapeId) |
|
1246 | { |
||
1247 | // p:sp |
||
1248 | 2 | $objWriter->startElement('p:cxnSp'); |
|
1249 | |||
1250 | // p:nvSpPr |
||
1251 | 2 | $objWriter->startElement('p:nvCxnSpPr'); |
|
1252 | |||
1253 | // p:cNvPr |
||
1254 | 2 | $objWriter->startElement('p:cNvPr'); |
|
1255 | 2 | $objWriter->writeAttribute('id', $shapeId); |
|
1256 | 2 | $objWriter->writeAttribute('name', ''); |
|
1257 | |||
1258 | 2 | $objWriter->endElement(); |
|
1259 | |||
1260 | // p:cNvCxnSpPr |
||
1261 | 2 | $objWriter->writeElement('p:cNvCxnSpPr', null); |
|
1262 | |||
1263 | // p:nvPr |
||
1264 | 2 | $objWriter->writeElement('p:nvPr', null); |
|
1265 | |||
1266 | 2 | $objWriter->endElement(); |
|
1267 | |||
1268 | // p:spPr |
||
1269 | 2 | $objWriter->startElement('p:spPr'); |
|
1270 | |||
1271 | // a:xfrm |
||
1272 | 2 | $objWriter->startElement('a:xfrm'); |
|
1273 | |||
1274 | 2 | if ($shape->getWidth() >= 0 && $shape->getHeight() >= 0) { |
|
1275 | // a:off |
||
1276 | 2 | $objWriter->startElement('a:off'); |
|
1277 | 2 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); |
|
1278 | 2 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); |
|
1279 | 2 | $objWriter->endElement(); |
|
1280 | |||
1281 | // a:ext |
||
1282 | 2 | $objWriter->startElement('a:ext'); |
|
1283 | 2 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); |
|
1284 | 2 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); |
|
1285 | 2 | $objWriter->endElement(); |
|
1286 | 2 | } elseif ($shape->getWidth() < 0 && $shape->getHeight() < 0) { |
|
1287 | // a:off |
||
1288 | 1 | $objWriter->startElement('a:off'); |
|
1289 | 1 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX() + $shape->getWidth())); |
|
1290 | 1 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY() + $shape->getHeight())); |
|
1291 | 1 | $objWriter->endElement(); |
|
1292 | |||
1293 | // a:ext |
||
1294 | 1 | $objWriter->startElement('a:ext'); |
|
1295 | 1 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu(-$shape->getWidth())); |
|
1296 | 1 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu(-$shape->getHeight())); |
|
1297 | 1 | $objWriter->endElement(); |
|
1298 | 1 | } elseif ($shape->getHeight() < 0) { |
|
1299 | 1 | $objWriter->writeAttribute('flipV', 1); |
|
1300 | |||
1301 | // a:off |
||
1302 | 1 | $objWriter->startElement('a:off'); |
|
1303 | 1 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); |
|
1304 | 1 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY() + $shape->getHeight())); |
|
1305 | 1 | $objWriter->endElement(); |
|
1306 | |||
1307 | // a:ext |
||
1308 | 1 | $objWriter->startElement('a:ext'); |
|
1309 | 1 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); |
|
1310 | 1 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu(-$shape->getHeight())); |
|
1311 | 1 | $objWriter->endElement(); |
|
1312 | 1 | } elseif ($shape->getWidth() < 0) { |
|
1313 | 1 | $objWriter->writeAttribute('flipV', 1); |
|
1314 | |||
1315 | // a:off |
||
1316 | 1 | $objWriter->startElement('a:off'); |
|
1317 | 1 | $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX() + $shape->getWidth())); |
|
1318 | 1 | $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); |
|
1319 | 1 | $objWriter->endElement(); |
|
1320 | |||
1321 | // a:ext |
||
1322 | 1 | $objWriter->startElement('a:ext'); |
|
1323 | 1 | $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu(-$shape->getWidth())); |
|
1324 | 1 | $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); |
|
1325 | 1 | $objWriter->endElement(); |
|
1326 | 1 | } |
|
1327 | |||
1328 | 2 | $objWriter->endElement(); |
|
1329 | |||
1330 | // a:prstGeom |
||
1331 | 2 | $objWriter->startElement('a:prstGeom'); |
|
1332 | 2 | $objWriter->writeAttribute('prst', 'line'); |
|
1333 | 2 | $objWriter->endElement(); |
|
1334 | |||
1335 | 2 | if ($shape->getBorder()->getLineStyle() != Border::LINE_NONE) { |
|
1336 | 2 | $this->writeBorder($objWriter, $shape->getBorder(), ''); |
|
1337 | 2 | } |
|
1338 | |||
1339 | 2 | $objWriter->endElement(); |
|
1340 | |||
1341 | 2 | $objWriter->endElement(); |
|
1342 | 2 | } |
|
1343 | |||
1344 | /** |
||
1345 | * Write Border |
||
1346 | * |
||
1347 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1348 | * @param \PhpOffice\PhpPresentation\Style\Border $pBorder Border |
||
1349 | * @param string $pElementName Element name |
||
1350 | * @throws \Exception |
||
1351 | */ |
||
1352 | 30 | protected function writeBorder(XMLWriter $objWriter, Border $pBorder, $pElementName = 'L') |
|
1353 | { |
||
1354 | // Line style |
||
1355 | 30 | $lineStyle = $pBorder->getLineStyle(); |
|
1356 | 30 | if ($lineStyle == Border::LINE_NONE) { |
|
1357 | 10 | $lineStyle = Border::LINE_SINGLE; |
|
1358 | 10 | } |
|
1359 | |||
1360 | // Line width |
||
1361 | 30 | $lineWidth = 12700 * $pBorder->getLineWidth(); |
|
1362 | |||
1363 | // a:ln $pElementName |
||
1364 | 30 | $objWriter->startElement('a:ln' . $pElementName); |
|
1365 | 30 | $objWriter->writeAttribute('w', $lineWidth); |
|
1366 | 30 | $objWriter->writeAttribute('cap', 'flat'); |
|
1367 | 30 | $objWriter->writeAttribute('cmpd', $lineStyle); |
|
1368 | 30 | $objWriter->writeAttribute('algn', 'ctr'); |
|
1369 | |||
1370 | // Fill? |
||
1371 | 30 | if ($pBorder->getLineStyle() == Border::LINE_NONE) { |
|
1372 | // a:noFill |
||
1373 | 10 | $objWriter->writeElement('a:noFill', null); |
|
1374 | 10 | } else { |
|
1375 | // a:solidFill |
||
1376 | 30 | $objWriter->startElement('a:solidFill'); |
|
1377 | |||
1378 | // a:srgbClr |
||
1379 | 30 | $objWriter->startElement('a:srgbClr'); |
|
1380 | 30 | $objWriter->writeAttribute('val', $pBorder->getColor()->getRGB()); |
|
1381 | 30 | $objWriter->endElement(); |
|
1382 | |||
1383 | 30 | $objWriter->endElement(); |
|
1384 | } |
||
1385 | |||
1386 | // Dash |
||
1387 | 30 | $objWriter->startElement('a:prstDash'); |
|
1388 | 30 | $objWriter->writeAttribute('val', $pBorder->getDashStyle()); |
|
1389 | 30 | $objWriter->endElement(); |
|
1390 | |||
1391 | // a:round |
||
1392 | 30 | $objWriter->writeElement('a:round', null); |
|
1393 | |||
1394 | // a:headEnd |
||
1395 | 30 | $objWriter->startElement('a:headEnd'); |
|
1396 | 30 | $objWriter->writeAttribute('type', 'none'); |
|
1397 | 30 | $objWriter->writeAttribute('w', 'med'); |
|
1398 | 30 | $objWriter->writeAttribute('len', 'med'); |
|
1399 | 30 | $objWriter->endElement(); |
|
1400 | |||
1401 | // a:tailEnd |
||
1402 | 30 | $objWriter->startElement('a:tailEnd'); |
|
1403 | 30 | $objWriter->writeAttribute('type', 'none'); |
|
1404 | 30 | $objWriter->writeAttribute('w', 'med'); |
|
1405 | 30 | $objWriter->writeAttribute('len', 'med'); |
|
1406 | 30 | $objWriter->endElement(); |
|
1407 | |||
1408 | 30 | $objWriter->endElement(); |
|
1409 | 30 | } |
|
1410 | |||
1411 | /** |
||
1412 | * Write Fill |
||
1413 | * |
||
1414 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1415 | * @param \PhpOffice\PhpPresentation\Style\Fill $pFill Fill style |
||
1416 | * @throws \Exception |
||
1417 | */ |
||
1418 | 48 | protected function writeFill(XMLWriter $objWriter, Fill $pFill) |
|
1419 | { |
||
1420 | // Is it a fill? |
||
1421 | 48 | if ($pFill->getFillType() == Fill::FILL_NONE) { |
|
1422 | 41 | return; |
|
1423 | } |
||
1424 | |||
1425 | // Is it a solid fill? |
||
1426 | 11 | if ($pFill->getFillType() == Fill::FILL_SOLID) { |
|
1427 | 6 | $this->writeSolidFill($objWriter, $pFill); |
|
1428 | 6 | return; |
|
1429 | } |
||
1430 | |||
1431 | // Check if this is a pattern type or gradient type |
||
1432 | 5 | if ($pFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $pFill->getFillType() == Fill::FILL_GRADIENT_PATH) { |
|
1433 | // Gradient fill |
||
1434 | 4 | $this->writeGradientFill($objWriter, $pFill); |
|
1435 | 4 | } else { |
|
1436 | // Pattern fill |
||
1437 | 1 | $this->writePatternFill($objWriter, $pFill); |
|
1438 | } |
||
1439 | 5 | } |
|
1440 | |||
1441 | /** |
||
1442 | * Write Solid Fill |
||
1443 | * |
||
1444 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1445 | * @param \PhpOffice\PhpPresentation\Style\Fill $pFill Fill style |
||
1446 | * @throws \Exception |
||
1447 | */ |
||
1448 | 6 | protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill) |
|
1460 | |||
1461 | /** |
||
1462 | * Write Gradient Fill |
||
1463 | * |
||
1464 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1465 | * @param \PhpOffice\PhpPresentation\Style\Fill $pFill Fill style |
||
1466 | * @throws \Exception |
||
1467 | */ |
||
1468 | 4 | protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill) |
|
1507 | |||
1508 | /** |
||
1509 | * Write Pattern Fill |
||
1510 | * |
||
1511 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1512 | * @param \PhpOffice\PhpPresentation\Style\Fill $pFill Fill style |
||
1513 | * @throws \Exception |
||
1514 | */ |
||
1515 | 1 | protected function writePatternFill(XMLWriter $objWriter, Fill $pFill) |
|
1542 | |||
1543 | 2 | protected function writeShadow(XMLWriter $objWriter, Shadow $oShadow) |
|
1571 | |||
1572 | /** |
||
1573 | * Write hyperlink |
||
1574 | * |
||
1575 | * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
||
1576 | * @param \PhpOffice\PhpPresentation\AbstractShape|\PhpOffice\PhpPresentation\Shape\RichText\TextElement $shape |
||
1577 | */ |
||
1578 | 5 | private function writeHyperlink(XMLWriter $objWriter, $shape) |
|
1579 | { |
||
1580 | // a:hlinkClick |
||
1589 | |||
1590 | /** |
||
1591 | * Write Note Slide |
||
1592 | * @param Note $pNote |
||
1593 | * @throws \Exception |
||
1594 | */ |
||
1595 | 1 | public function writeNote(Note $pNote = null) |
|
1746 | |||
1747 | /** |
||
1748 | * Write Transition Slide |
||
1749 | * @link http://officeopenxml.com/prSlide-transitions.php |
||
1750 | * @param XMLWriter $objWriter |
||
1751 | * @param Transition $transition |
||
1752 | */ |
||
1753 | 1 | public function writeTransition(XMLWriter $objWriter, Transition $transition) |
|
1997 | } |
||
1998 |