Skip to content

Commit 420cf2b

Browse files
committed
Merge pull request #258 from ivanlanin/develop
New drawing shapes (arc, curve, line, polyline, rect, oval) for #123
2 parents b38dd82 + 61d8dbd commit 420cf2b

29 files changed

+1715
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
66

77
### Features
88

9-
None yet.
9+
- Element: Ability to add drawing shapes (arc, curve, line, polyline, rect, oval) using new `Shape` element - @ivanlanin GH-123
1010

1111
### Bugfixes
1212

docs/elements.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ column shows the containers while the rows lists the elements.
4747
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
4848
| 19 | Line | v | v | v | v | v | v |
4949
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
50+
| 20 | Shapes | v | v | v | v | v | v |
51+
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
5052

5153
Legend:
5254

@@ -490,7 +492,12 @@ Fields
490492

491493
To be completed
492494

493-
Line
495+
Lines
496+
-----
497+
498+
To be completed
499+
500+
Shapes
494501
------
495502

496503
To be completed

docs/intro.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ Writers
9999
+---------------------------+----------------------+--------+-------+-------+--------+-------+
100100
| | Endnote || | || |
101101
+---------------------------+----------------------+--------+-------+-------+--------+-------+
102-
| **Graphs** | 2D basic graphs | | | | | |
102+
| **Graphs** | 2D basic graphs | | | | | |
103103
+---------------------------+----------------------+--------+-------+-------+--------+-------+
104104
| | 2D advanced graphs | | | | | |
105105
+---------------------------+----------------------+--------+-------+-------+--------+-------+
106-
| | 3D graphs | | | | | |
106+
| | 3D graphs | | | | | |
107107
+---------------------------+----------------------+--------+-------+-------+--------+-------+
108108
| **Math** | OMML support | | | | | |
109109
+---------------------------+----------------------+--------+-------+-------+--------+-------+

docs/src/documentation.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst
3636
- [Textboxes](#textboxes)
3737
- [Fields](#fields)
3838
- [Lines](#lines)
39+
- [Shapes](#shapes)
3940
- [Templates](#templates)
4041
- [Writers & readers](#writers-readers)
4142
- [OOXML](#ooxml)
@@ -101,9 +102,9 @@ Below are the supported features for each file formats.
101102
| | Footer || | | | |
102103
| | Footnote || | || |
103104
| | Endnote || | || |
104-
| **Graphs** | 2D basic graphs | | | | | |
105+
| **Graphs** | 2D basic graphs | | | | | |
105106
| | 2D advanced graphs | | | | | |
106-
| | 3D graphs | | | | | |
107+
| | 3D graphs | | | | | |
107108
| **Math** | OMML support | | | | | |
108109
| | MathML support | | | | | |
109110
| **Bonus** | Encryption | | | | | |
@@ -465,6 +466,7 @@ Below are the matrix of element availability in each container. The column shows
465466
| 17 | TextBox | v | v | v | v | - | - |
466467
| 18 | Field | v | v | v | v | v | v |
467468
| 19 | Line | v | v | v | v | v | v |
469+
| 20 | Shape | v | v | v | v | v | v |
468470

469471
Legend:
470472

@@ -850,6 +852,10 @@ To be completed.
850852

851853
To be completed.
852854

855+
## Shapes
856+
857+
To be completed.
858+
853859
# Templates
854860

855861
You can create a docx template with included search-patterns that can be replaced by any value you wish. Only single-line values can be replaced. To load a template file, use the `loadTemplate` method. After loading the docx template, you can use the `setValue` method to change the value of a search pattern. The search-pattern model is: `${search-pattern}`. It is not possible to add new PHPWord elements to a loaded template file.

phpmd.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<rule ref="rulesets/design.xml/NumberOfChildren">
1919
<!-- AbstractStyle needs more children (default: 15) -->
2020
<properties>
21-
<property name="minimum" value="20" />
21+
<property name="minimum" value="30" />
2222
</properties>
2323
</rule>
2424
<rule ref="rulesets/unusedcode.xml" />

samples/Sample_31_Shape.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word document
5+
echo date('H:i:s'), " Create new PhpWord object", EOL;
6+
7+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
8+
$phpWord->addTitleStyle(1, array('size' => 14, 'bold' => true));
9+
10+
$section = $phpWord->addSection();
11+
12+
// Arc
13+
$section->addTitle('Arc', 1);
14+
$section->addShape(
15+
'arc',
16+
array(
17+
'points' => '-90 20',
18+
'frame' => array('width' => 120, 'height' => 120),
19+
'outline' => array('color' => '#333333', 'weight' => 2, 'startArrow' => 'oval', 'endArrow' => 'open'),
20+
)
21+
);
22+
23+
// Curve
24+
$section->addTitle('Curve', 1);
25+
$section->addShape(
26+
'curve',
27+
array(
28+
'points' => '1,100 200,1 1,50 200,50', 'connector' => 'elbow',
29+
'outline' => array('color' => '#66cc00', 'weight' => 2, 'dash' => 'dash', 'startArrow' => 'diamond', 'endArrow' => 'block'),
30+
)
31+
);
32+
33+
// Line
34+
$section->addTitle('Line', 1);
35+
$section->addShape(
36+
'line',
37+
array(
38+
'points' => '1,1 150,30',
39+
'outline' => array('color' => '#cc00ff', 'line' => 'thickThin', 'weight' => 3, 'startArrow' => 'oval', 'endArrow' => 'classic'),
40+
)
41+
);
42+
43+
// Polyline
44+
$section->addTitle('Polyline', 1);
45+
$section->addShape(
46+
'polyline',
47+
array(
48+
'points' => '1,30 20,10 55,20 75,10 100,40 115,50, 120,15 200,50',
49+
'outline' => array('color' => '#cc6666', 'weight' => 2, 'startArrow' => 'none', 'endArrow' => 'classic'),
50+
)
51+
);
52+
53+
// Rectangle
54+
$section->addTitle('Rectangle', 1);
55+
$section->addShape(
56+
'rect',
57+
array(
58+
'roundness' => 0.2,
59+
'frame' => array('width' => 100, 'height' => 100, 'left' => 1, 'top' => 1),
60+
'fill' => array('color' => '#FFCC33'),
61+
'outline' => array('color' => '#990000', 'weight' => 1),
62+
'shadow' => array(),
63+
)
64+
);
65+
66+
// Oval
67+
$section->addTitle('Oval', 1);
68+
$section->addShape(
69+
'oval',
70+
array(
71+
'frame' => array('width' => 100, 'height' => 70, 'left' => 1, 'top' => 1),
72+
'fill' => array('color' => '#33CC99'),
73+
'outline' => array('color' => '#333333', 'weight' => 2),
74+
'extrusion' => array(),
75+
)
76+
);
77+
78+
// Save file
79+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
80+
if (!CLI) {
81+
include_once 'Sample_Footer.php';
82+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @method TextBox addTextBox($style = null)
4040
* @method Field addField($type = null, $properties = array(), $options = array())
4141
* @method Line addLine($lineStyle = null)
42+
* @method Shape addObject($type, $style = null)
4243
*
4344
* @since 0.10.0
4445
*/
@@ -74,7 +75,7 @@ public function __call($function, $args)
7475
{
7576
$elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak',
7677
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote',
77-
'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line');
78+
'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape');
7879
$functions = array();
7980
for ($i = 0; $i < count($elements); $i++) {
8081
$functions[$i] = 'add' . $elements[$i];
@@ -242,6 +243,7 @@ private function checkValidity($method)
242243
'Object' => $allContainers,
243244
'Field' => $allContainers,
244245
'Line' => $allContainers,
246+
'Shape' => $allContainers,
245247
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
246248
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
247249
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/AbstractElement.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,25 @@ protected function setStyle($styleObject, $styleValue = null, $returnObject = fa
262262

263263
return $style;
264264
}
265+
266+
/**
267+
* Set enum value
268+
*
269+
* @param mixed $value
270+
* @param array $enum
271+
* @param mixed $default
272+
* @return mixed
273+
* @throws \InvalidArgumentException
274+
* @todo Merge with the same method in AbstractStyle
275+
*/
276+
protected function setEnumVal($value = null, $enum = array(), $default = null)
277+
{
278+
if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
279+
throw new \InvalidArgumentException("Invalid style value: {$value}");
280+
} elseif ($value === null || trim($value) == '') {
281+
$value = $default;
282+
}
283+
284+
return $value;
285+
}
265286
}

src/PhpWord/Element/Shape.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Element;
19+
20+
use PhpOffice\PhpWord\Style\Shape as ShapeStyle;
21+
22+
/**
23+
* Shape element
24+
*
25+
* @since 0.12.0
26+
*/
27+
class Shape extends AbstractElement
28+
{
29+
/**
30+
* Shape type arc|curve|line|polyline|rect|oval
31+
*
32+
* @var string
33+
*/
34+
private $type;
35+
36+
/**
37+
* Shape style
38+
*
39+
* @var \PhpOffice\PhpWord\Style\Shape
40+
*/
41+
private $style;
42+
43+
/**
44+
* Create new instance
45+
*
46+
* @param string $type
47+
* @param mixed $style
48+
*/
49+
public function __construct($type, $style = null)
50+
{
51+
$this->setType($type);
52+
$this->style = $this->setStyle(new ShapeStyle(), $style);
53+
}
54+
55+
/**
56+
* Get type
57+
*
58+
* @return string
59+
*/
60+
public function getType()
61+
{
62+
return $this->type;
63+
}
64+
65+
/**
66+
* Set pattern
67+
*
68+
* @param string $value
69+
* @return self
70+
*/
71+
public function setType($value = null)
72+
{
73+
$enum = array('arc', 'curve', 'line', 'polyline', 'rect', 'oval');
74+
$this->type = $this->setEnumVal($value, $enum, null);
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* Get shape style
81+
*
82+
* @return \PhpOffice\PhpWord\Style\Shape
83+
*/
84+
public function getStyle()
85+
{
86+
return $this->style;
87+
}
88+
}

src/PhpWord/Style/AbstractStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ protected function setFloatVal($value, $default = null)
264264
protected function setEnumVal($value = null, $enum = array(), $default = null)
265265
{
266266
if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
267-
throw new \InvalidArgumentException('Invalid style value.');
267+
throw new \InvalidArgumentException("Invalid style value: {$value}");
268268
} elseif ($value === null || trim($value) == '') {
269269
$value = $default;
270270
}

0 commit comments

Comments
 (0)