Skip to content

Commit b91e320

Browse files
committed
Refactor ODT writer to give enable some additional features
1 parent 710e147 commit b91e320

22 files changed

+561
-285
lines changed

.scrutinizer.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ tools:
1616
external_code_coverage:
1717
enabled: true
1818
timeout: 900
19-
php_sim:
20-
min_mass: 40
19+
php_cpd: true
20+
# php_sim: # Temporarily disabled to allow focus on things other than duplicates
21+
# min_mass: 40
2122
php_pdepend: true
2223
php_analyzer: true
2324
sensiolabs_security_checker: true

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
2020
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196
2121
- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin
2222
- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200
23+
- ODT: Enable inline font style in TextRun - @ivanlanin
24+
- ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
2325

2426
### Bugfixes
2527

phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<directory suffix=".php">./src</directory>
1919
<exclude>
2020
<directory suffix=".php">./src/PhpWord/Shared/PCLZip</directory>
21-
<directory suffix=".php">./src/PhpWord/Shared/Spyc</directory>
2221
</exclude>
2322
</whitelist>
2423
</filter>
1.05 KB
Binary file not shown.

src/PhpWord/PhpWord.php

+23-16
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,23 @@ public function setDefaultFontSize($fontSize)
240240
* Set default paragraph style definition to styles.xml
241241
*
242242
* @param array $styles Paragraph style definition
243+
* @return \PhpOffice\PhpWord\Style\Paragraph
243244
*/
244245
public function setDefaultParagraphStyle($styles)
245246
{
246-
Style::setDefaultParagraphStyle($styles);
247+
return Style::setDefaultParagraphStyle($styles);
247248
}
248249

249250
/**
250251
* Adds a paragraph style definition to styles.xml
251252
*
252253
* @param string $styleName
253254
* @param array $styles
255+
* @return \PhpOffice\PhpWord\Style\Paragraph
254256
*/
255257
public function addParagraphStyle($styleName, $styles)
256258
{
257-
Style::addParagraphStyle($styleName, $styles);
259+
return Style::addParagraphStyle($styleName, $styles);
258260
}
259261

260262
/**
@@ -263,10 +265,11 @@ public function addParagraphStyle($styleName, $styles)
263265
* @param string $styleName
264266
* @param mixed $fontStyle
265267
* @param mixed $paragraphStyle
268+
* @return \PhpOffice\PhpWord\Style\Font
266269
*/
267270
public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
268271
{
269-
Style::addFontStyle($styleName, $fontStyle, $paragraphStyle);
272+
return Style::addFontStyle($styleName, $fontStyle, $paragraphStyle);
270273
}
271274

272275
/**
@@ -275,44 +278,48 @@ public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
275278
* @param string $styleName
276279
* @param mixed $styleTable
277280
* @param mixed $styleFirstRow
281+
* @return \PhpOffice\PhpWord\Style\Table
278282
*/
279283
public function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
280284
{
281-
Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
285+
return Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
282286
}
283287

284288
/**
285-
* Adds a heading style definition to styles.xml
289+
* Adds a numbering style
286290
*
287-
* @param int $depth
288-
* @param mixed $fontStyle
289-
* @param mixed $paragraphStyle
291+
* @param string $styleName
292+
* @param mixed $styles
293+
* @return \PhpOffice\PhpWord\Style\Numbering
290294
*/
291-
public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
295+
public function addNumberingStyle($styleName, $styles)
292296
{
293-
Style::addTitleStyle($depth, $fontStyle, $paragraphStyle);
297+
return Style::addNumberingStyle($styleName, $styles);
294298
}
295299

296300
/**
297301
* Adds a hyperlink style to styles.xml
298302
*
299303
* @param string $styleName
300304
* @param mixed $styles
305+
* @return \PhpOffice\PhpWord\Style\Font
301306
*/
302307
public function addLinkStyle($styleName, $styles)
303308
{
304-
Style::addLinkStyle($styleName, $styles);
309+
return Style::addLinkStyle($styleName, $styles);
305310
}
306311

307312
/**
308-
* Adds a numbering style
313+
* Adds a heading style definition to styles.xml
309314
*
310-
* @param string $styleName
311-
* @param mixed $styles
315+
* @param int $depth
316+
* @param mixed $fontStyle
317+
* @param mixed $paragraphStyle
318+
* @return \PhpOffice\PhpWord\Style\Font
312319
*/
313-
public function addNumberingStyle($styleName, $styles)
320+
public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
314321
{
315-
Style::addNumberingStyle($styleName, $styles);
322+
return Style::addTitleStyle($depth, $fontStyle, $paragraphStyle);
316323
}
317324

318325
/**

src/PhpWord/Reader/Word2007/AbstractPart.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
222222
$style = array();
223223
$mapping = array(
224224
'w:rStyle' => 'styleName',
225-
'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color',
226-
'w:strike' => 'strikethrough', 'w:u' => 'underline',
225+
'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color', 'w:u' => 'underline',
226+
'w:strike' => 'strikethrough', 'w:dstrike' => 'doubleStrikethrough',
227227
'w:highlight' => 'fgColor', 'w:sz' => 'size',
228228
'w:rFonts' => 'name', 'w:vertAlign' => 'superScript',
229+
'w:smallCaps' => 'smallCaps', 'w:caps' => 'allCaps',
229230
);
230231

231232
$nodes = $xmlReader->getElements('w:rPr/*', $domNode);
@@ -244,6 +245,9 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
244245
case 'w:b':
245246
case 'w:i':
246247
case 'w:strike':
248+
case 'w:dstrike':
249+
case 'w:smallCaps':
250+
case 'w:caps':
247251
$style[$property] = true;
248252
break;
249253

src/PhpWord/Style.php

+46-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord;
1919

20+
use PhpOffice\PhpWord\Style\AbstractStyle;
2021
use PhpOffice\PhpWord\Style\Font;
2122
use PhpOffice\PhpWord\Style\Numbering;
2223
use PhpOffice\PhpWord\Style\Paragraph;
@@ -39,10 +40,11 @@ class Style
3940
*
4041
* @param string $styleName
4142
* @param array $styles
43+
* @return \PhpOffice\PhpWord\Style\Paragraph
4244
*/
4345
public static function addParagraphStyle($styleName, $styles)
4446
{
45-
self::setStyleValues($styleName, new Paragraph(), $styles);
47+
return self::setStyleValues($styleName, new Paragraph(), $styles);
4648
}
4749

4850
/**
@@ -51,33 +53,36 @@ public static function addParagraphStyle($styleName, $styles)
5153
* @param string $styleName
5254
* @param array $fontStyle
5355
* @param array $paragraphStyle
56+
* @return \PhpOffice\PhpWord\Style\Font
5457
*/
5558
public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
5659
{
57-
self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
60+
return self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
5861
}
5962

6063
/**
6164
* Add link style
6265
*
6366
* @param string $styleName
6467
* @param array $styles
68+
* @return \PhpOffice\PhpWord\Style\Font
6569
*/
6670
public static function addLinkStyle($styleName, $styles)
6771
{
68-
self::setStyleValues($styleName, new Font('link'), $styles);
72+
return self::setStyleValues($styleName, new Font('link'), $styles);
6973
}
7074

7175
/**
72-
* Add table style
76+
* Add numbering style
7377
*
7478
* @param string $styleName
75-
* @param array $styleTable
76-
* @param array|null $styleFirstRow
79+
* @param array $styleValues
80+
* @return \PhpOffice\PhpWord\Style\Numbering
81+
* @since 0.10.0
7782
*/
78-
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
83+
public static function addNumberingStyle($styleName, $styleValues)
7984
{
80-
self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
85+
return self::setStyleValues($styleName, new Numbering(), $styleValues);
8186
}
8287

8388
/**
@@ -86,29 +91,30 @@ public static function addTableStyle($styleName, $styleTable, $styleFirstRow = n
8691
* @param int $depth
8792
* @param array $fontStyle
8893
* @param array $paragraphStyle
94+
* @return \PhpOffice\PhpWord\Style\Font
8995
*/
9096
public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
9197
{
92-
self::setStyleValues("Heading_{$depth}", new Font('title', $paragraphStyle), $fontStyle);
98+
return self::setStyleValues("Heading_{$depth}", new Font('title', $paragraphStyle), $fontStyle);
9399
}
94100

95101
/**
96-
* Add numbering style
102+
* Add table style
97103
*
98104
* @param string $styleName
99-
* @param array $styleValues
100-
* @return Numbering
101-
* @since 0.10.0
105+
* @param array $styleTable
106+
* @param array|null $styleFirstRow
107+
* @return \PhpOffice\PhpWord\Style\Table
102108
*/
103-
public static function addNumberingStyle($styleName, $styleValues)
109+
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
104110
{
105-
self::setStyleValues($styleName, new Numbering(), $styleValues);
111+
return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
106112
}
107113

108114
/**
109115
* Count styles
110116
*
111-
* @return integer
117+
* @return int
112118
* @since 0.10.0
113119
*/
114120
public static function countStyles()
@@ -129,16 +135,17 @@ public static function resetStyles()
129135
* Set default paragraph style
130136
*
131137
* @param array $styles Paragraph style definition
138+
* @return \PhpOffice\PhpWord\Style\Paragraph
132139
*/
133140
public static function setDefaultParagraphStyle($styles)
134141
{
135-
self::addParagraphStyle('Normal', $styles);
142+
return self::addParagraphStyle('Normal', $styles);
136143
}
137144

138145
/**
139146
* Get all styles
140147
*
141-
* @return array
148+
* @return \PhpOffice\PhpWord\Style\AbstractStyle[]
142149
*/
143150
public static function getStyles()
144151
{
@@ -149,7 +156,7 @@ public static function getStyles()
149156
* Get style by name
150157
*
151158
* @param string $styleName
152-
* @return Paragraph|Font|Table|Numbering|null
159+
* @return \PhpOffice\PhpWord\Style\AbstractStyle Paragraph|Font|Table|Numbering
153160
*/
154161
public static function getStyle($styleName)
155162
{
@@ -163,21 +170,30 @@ public static function getStyle($styleName)
163170
/**
164171
* Set style values and put it to static style collection
165172
*
166-
* @param string $styleName
167-
* @param Paragraph|Font|Table|Numbering $styleObject
168-
* @param array|null $styleValues
173+
* The $styleValues could be an array or object
174+
*
175+
* @param string $name
176+
* @param \PhpOffice\PhpWord\Style\AbstractStyle $style
177+
* @param array|\PhpOffice\PhpWord\Style\AbstractStyle $value
178+
* @return \PhpOffice\PhpWord\Style\AbstractStyle
169179
*/
170-
private static function setStyleValues($styleName, $styleObject, $styleValues = null)
180+
private static function setStyleValues($name, $style, $value = null)
171181
{
172-
if (!array_key_exists($styleName, self::$styles)) {
173-
if (!is_null($styleValues) && is_array($styleValues)) {
174-
foreach ($styleValues as $key => $value) {
175-
$styleObject->setStyleValue($key, $value);
182+
if (!array_key_exists($name, self::$styles)) {
183+
if ($value !== null) {
184+
if (is_array($value)) {
185+
$style->setStyleByArray($value);
186+
} elseif ($value instanceof AbstractStyle) {
187+
if (get_class($style) == get_class($value)) {
188+
$style = $value;
189+
}
176190
}
177191
}
178-
$styleObject->setStyleName($styleName);
179-
$styleObject->setIndex(self::countStyles() + 1); // One based index
180-
self::$styles[$styleName] = $styleObject;
192+
$style->setStyleName($name);
193+
$style->setIndex(self::countStyles() + 1); // One based index
194+
self::$styles[$name] = $style;
181195
}
196+
197+
return self::getStyle($name);
182198
}
183199
}

src/PhpWord/Style/AbstractStyle.php

+34-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ abstract class AbstractStyle
4949
*/
5050
protected $aliases = array();
5151

52+
/**
53+
* Is this an automatic style? (Used primarily in OpenDocument driver)
54+
*
55+
* @var bool
56+
* @since 0.11.0
57+
*/
58+
private $isAuto = false;
59+
5260
/**
5361
* Get style name
5462
*
@@ -95,6 +103,29 @@ public function setIndex($value = null)
95103
return $this;
96104
}
97105

106+
/**
107+
* Get is automatic style flag
108+
*
109+
* @return bool
110+
*/
111+
public function isAuto()
112+
{
113+
return $this->isAuto;
114+
}
115+
116+
/**
117+
* Set is automatic style flag
118+
*
119+
* @param bool $value
120+
* @return self
121+
*/
122+
public function setAuto($value = true)
123+
{
124+
$this->isAuto = $this->setBoolVal($value, $this->isAuto);
125+
126+
return $this;
127+
}
128+
98129
/**
99130
* Set style value template method
100131
*
@@ -123,12 +154,12 @@ public function setStyleValue($key, $value)
123154
/**
124155
* Set style by using associative array
125156
*
126-
* @param array $styles
157+
* @param array $values
127158
* @return self
128159
*/
129-
public function setStyleByArray($styles = array())
160+
public function setStyleByArray($values = array())
130161
{
131-
foreach ($styles as $key => $value) {
162+
foreach ($values as $key => $value) {
132163
$this->setStyleValue($key, $value);
133164
}
134165

0 commit comments

Comments
 (0)