Skip to content

Commit 6a64979

Browse files
Trainmastertroosan
authored andcommitted
Allow to set "autoHyphenation" setting (#1282)
* Allow to set "autoHyphenation" for document * Allow to set "consecutiveHyphenLimit" for document * Allow to set "hyphenationZone" for document * Allow to set "doNotHyphenateCaps" for document * Allow to set "suppressAutoHyphens" for paragraph * randomize the tempDir more * Word2007 parsing title formatting (#1297) * Improve Title parsing - Title should be able to contain TextRun - Style 'Title' should be treated the same with as Heading - Add tests for Heading/Title reader * update the documentation and the changelog * PHP 7.2 build should not fail anymore * fix parsing of footnotes and endnotes * add method to remove an element from a section * add method to allow sorting of sections
1 parent 250fbd4 commit 6a64979

File tree

20 files changed

+506
-114
lines changed

20 files changed

+506
-114
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ v0.15.0 (?? ??? 2018)
1515
- Add parsing of formatting inside lists @atomicalnet @troosan #594
1616
- Added support for Vertically Raised or Lowered Text (w:position) @anrikun @troosan #640
1717
- Add support for MACROBUTTON field @phryneas @troosan #1021
18+
- Add support for Hyphenation @Trainmaster #1282 (Document: `autoHyphenation`, `consecutiveHyphenLimit`, `hyphenationZone`, `doNotHyphenateCaps`, Paragraph: `suppressAutoHyphens`)
1819

1920
### Fixed
2021
- Fix reading of docx default style - @troosan #1238
@@ -477,4 +478,4 @@ This is the first release after a long development hiatus in [CodePlex](https://
477478
- Basic CI with Travis - @Progi1984
478479
- Added PHPWord_Exception and exception when could not copy the template - @Progi1984
479480
- IMPROVED: Moved examples out of Classes directory - @Progi1984
480-
- IMPROVED: Advanced string replace in setValue for Template - @Esmeraldo [#49](http://phpword.codeplex.com/workitem/49)
481+
- IMPROVED: Advanced string replace in setValue for Template - @Esmeraldo [#49](http://phpword.codeplex.com/workitem/49)

docs/general.rst

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ The default language of the document can be change with the following.
217217
218218
$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE));
219219
220-
``Languge`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages.
220+
``Language`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages.
221221
A couple of language codes are provided in the ``PhpOffice\PhpWord\ComplexType\Language`` class but any valid code/ID can be used.
222222

223-
In case you are generating an RTF document the Language need to be set differently.
223+
In case you are generating an RTF document the language need to be set differently.
224224

225225
.. code-block:: php
226226
@@ -290,3 +290,45 @@ To force an update of the fields present in the document, set updateFields to tr
290290
.. code-block:: php
291291
292292
$phpWord->getSettings()->setUpdateFields(true);
293+
294+
Hyphenation
295+
-----------
296+
Hyphenation describes the process of breaking words with hyphens. There are several options to control hyphenation.
297+
298+
Auto hyphenation
299+
~~~~~~~~~~~~~~~~
300+
301+
To automatically hyphenate text set ``autoHyphenation`` to ``true``.
302+
303+
.. code-block:: php
304+
305+
$phpWord->getSettings()->setAutoHyphenation(true);
306+
307+
Consecutive Hyphen Limit
308+
~~~~~~~~~~~~~~~~~~~~~~~~
309+
310+
The maximum number of consecutive lines of text ending with a hyphen can be controlled by the ``consecutiveHyphenLimit`` option.
311+
There is no limit if the option is not set or the provided value is ``0``.
312+
313+
.. code-block:: php
314+
315+
$phpWord->getSettings()->setConsecutiveHyphenLimit(2);
316+
317+
Hyphenation Zone
318+
~~~~~~~~~~~~~~~~
319+
320+
The hyphenation zone (in *twip*) is the allowed amount of whitespace before hyphenation is applied.
321+
The smaller the hyphenation zone the more words are hyphenated. Or in other words, the wider the hyphenation zone the less words are hyphenated.
322+
323+
.. code-block:: php
324+
325+
$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1));
326+
327+
Hyphenate Caps
328+
~~~~~~~~~~~~~~
329+
330+
To control whether or not words in all capital letters shall be hyphenated use the `doNotHyphenateCaps` option.
331+
332+
.. code-block:: php
333+
334+
$phpWord->getSettings()->setDoNotHyphenateCaps(true);

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Available Paragraph style options:
8282
- ``spaceAfter``. Space after paragraph in *twip*.
8383
- ``spacing``. Space between lines.
8484
- ``spacingLineRule``. Line Spacing Rule. *auto*, *exact*, *atLeast*
85+
- ``suppressAutoHyphens``. Hyphenation for paragraph, *true* or *false*.
8586
- ``tabs``. Set of custom tab stops.
8687
- ``widowControl``. Allow first/last line to display on a separate page, *true* or *false*.
8788
- ``contextualSpacing``. Ignore Spacing Above and Below When Using Identical Styles, *true* or *false*.

src/PhpWord/Element/Field.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord\Element;
1919

20-
use PhpOffice\PhpWord\Style\Font;
21-
2220
/**
2321
* Field element
2422
*
@@ -215,46 +213,6 @@ public function getOptions()
215213
return $this->options;
216214
}
217215

218-
/**
219-
* Set Text style
220-
*
221-
* @param \PhpOffice\PhpWord\Style\Font $style
222-
* @return \PhpOffice\PhpWord\Style\Font
223-
*/
224-
public function setFontStyle($style = null)
225-
{
226-
if (!$style instanceof Font) {
227-
throw new \InvalidArgumentException('font style must be of type Font');
228-
}
229-
230-
if ($style->isNoProof()) {
231-
$this->fontStyle = $style;
232-
} else {
233-
// make a copy of the font so the original is not altered
234-
$this->fontStyle = clone $style;
235-
$this->fontStyle->setNoProof(true);
236-
}
237-
238-
return $this->fontStyle;
239-
}
240-
241-
/**
242-
* Get Text style
243-
*
244-
* @return \PhpOffice\PhpWord\Style\Font
245-
*/
246-
public function getFontStyle()
247-
{
248-
if ($this->fontStyle == null) {
249-
$font = new Font();
250-
$font->setNoProof(true);
251-
252-
return $font;
253-
}
254-
255-
return $this->fontStyle;
256-
}
257-
258216
/**
259217
* Set Field text
260218
*

src/PhpWord/Metadata/Settings.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,32 @@ class Settings
130130
*/
131131
private $decimalSymbol = '.';
132132

133+
/**
134+
* Automatically hyphenate document contents when displayed
135+
*
136+
* @var bool|null
137+
*/
138+
private $autoHyphenation;
139+
140+
/**
141+
* Maximum number of consecutively hyphenated lines
142+
*
143+
* @var int|null
144+
*/
145+
private $consecutiveHyphenLimit;
146+
147+
/**
148+
* The allowed amount of whitespace before hyphenation is applied
149+
* @var float|null
150+
*/
151+
private $hyphenationZone;
152+
153+
/**
154+
* Do not hyphenate words in all capital letters
155+
* @var bool|null
156+
*/
157+
private $doNotHyphenateCaps;
158+
133159
/**
134160
* @return Protection
135161
*/
@@ -387,4 +413,68 @@ public function setDecimalSymbol($decimalSymbol)
387413
{
388414
$this->decimalSymbol = $decimalSymbol;
389415
}
416+
417+
/**
418+
* @return bool|null
419+
*/
420+
public function hasAutoHyphenation()
421+
{
422+
return $this->autoHyphenation;
423+
}
424+
425+
/**
426+
* @param bool $autoHyphenation
427+
*/
428+
public function setAutoHyphenation($autoHyphenation)
429+
{
430+
$this->autoHyphenation = (bool) $autoHyphenation;
431+
}
432+
433+
/**
434+
* @return int|null
435+
*/
436+
public function getConsecutiveHyphenLimit()
437+
{
438+
return $this->consecutiveHyphenLimit;
439+
}
440+
441+
/**
442+
* @param int $consecutiveHyphenLimit
443+
*/
444+
public function setConsecutiveHyphenLimit($consecutiveHyphenLimit)
445+
{
446+
$this->consecutiveHyphenLimit = (int) $consecutiveHyphenLimit;
447+
}
448+
449+
/**
450+
* @return float|null
451+
*/
452+
public function getHyphenationZone()
453+
{
454+
return $this->hyphenationZone;
455+
}
456+
457+
/**
458+
* @param float $hyphenationZone Measurement unit is twip
459+
*/
460+
public function setHyphenationZone($hyphenationZone)
461+
{
462+
$this->hyphenationZone = $hyphenationZone;
463+
}
464+
465+
/**
466+
* @return null|bool
467+
*/
468+
public function hasDoNotHyphenateCaps()
469+
{
470+
return $this->doNotHyphenateCaps;
471+
}
472+
473+
/**
474+
* @param bool $doNotHyphenateCaps
475+
*/
476+
public function setDoNotHyphenateCaps($doNotHyphenateCaps)
477+
{
478+
$this->doNotHyphenateCaps = (bool) $doNotHyphenateCaps;
479+
}
390480
}

src/PhpWord/PhpWord.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ public function addSection($style = null)
242242
return $section;
243243
}
244244

245+
/**
246+
* Sorts the sections using the callable passed
247+
*
248+
* @see http://php.net/manual/en/function.usort.php for usage
249+
* @param callable $sorter
250+
*/
251+
public function sortSections($sorter)
252+
{
253+
usort($this->sections, $sorter);
254+
}
255+
245256
/**
246257
* Get default font name
247258
*

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,20 +364,21 @@ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode
364364

365365
$styleNode = $xmlReader->getElement('w:pPr', $domNode);
366366
$styleDefs = array(
367-
'styleName' => array(self::READ_VALUE, array('w:pStyle', 'w:name')),
368-
'alignment' => array(self::READ_VALUE, 'w:jc'),
369-
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
370-
'next' => array(self::READ_VALUE, 'w:next'),
371-
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
372-
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
373-
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
374-
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
375-
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
376-
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
377-
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
378-
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
379-
'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'),
380-
'bidi' => array(self::READ_TRUE, 'w:bidi'),
367+
'styleName' => array(self::READ_VALUE, array('w:pStyle', 'w:name')),
368+
'alignment' => array(self::READ_VALUE, 'w:jc'),
369+
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
370+
'next' => array(self::READ_VALUE, 'w:next'),
371+
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
372+
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
373+
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
374+
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
375+
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
376+
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
377+
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
378+
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
379+
'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'),
380+
'bidi' => array(self::READ_TRUE, 'w:bidi'),
381+
'suppressAutoHyphens' => array(self::READ_TRUE, 'w:suppressAutoHyphens'),
381382
);
382383

383384
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
@@ -578,7 +579,7 @@ protected function readStyleDefs(XMLReader $xmlReader, \DOMElement $parentNode =
578579
*
579580
* @param string $method
580581
* @ignoreScrutinizerPatch
581-
* @param mixed $attributeValue
582+
* @param string|null $attributeValue
582583
* @param mixed $expected
583584
* @return mixed
584585
*/

src/PhpWord/Reader/Word2007/Settings.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@
2929
*/
3030
class Settings extends AbstractPart
3131
{
32-
private static $booleanProperties = array('hideSpellingErrors', 'hideGrammaticalErrors', 'trackRevisions', 'doNotTrackMoves', 'doNotTrackFormatting', 'evenAndOddHeaders');
32+
private static $booleanProperties = array(
33+
'mirrorMargins',
34+
'hideSpellingErrors',
35+
'hideGrammaticalErrors',
36+
'trackRevisions',
37+
'doNotTrackMoves',
38+
'doNotTrackFormatting',
39+
'evenAndOddHeaders',
40+
'updateFields',
41+
'autoHyphenation',
42+
'doNotHyphenateCaps',
43+
);
3344

3445
/**
3546
* Read settings.xml.
@@ -157,4 +168,32 @@ protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOME
157168
$revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN));
158169
$phpWord->getSettings()->setRevisionView($revisionView);
159170
}
171+
172+
/**
173+
* @param XMLReader $xmlReader
174+
* @param PhpWord $phpWord
175+
* @param \DOMElement $node
176+
*/
177+
protected function setConsecutiveHyphenLimit(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
178+
{
179+
$value = $xmlReader->getAttribute('w:val', $node);
180+
181+
if ($value !== null) {
182+
$phpWord->getSettings()->setConsecutiveHyphenLimit($value);
183+
}
184+
}
185+
186+
/**
187+
* @param XMLReader $xmlReader
188+
* @param PhpWord $phpWord
189+
* @param \DOMElement $node
190+
*/
191+
protected function setHyphenationZone(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
192+
{
193+
$value = $xmlReader->getAttribute('w:val', $node);
194+
195+
if ($value !== null) {
196+
$phpWord->getSettings()->setHyphenationZone($value);
197+
}
198+
}
160199
}

src/PhpWord/Shared/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private static function parseText($node, $element, &$styles)
252252
$styles['font'] = self::recursiveParseStylesInHierarchy($node, $styles['font']);
253253

254254
//alignment applies on paragraph, not on font. Let's copy it there
255-
if (isset($styles['font']['alignment'])) {
255+
if (isset($styles['font']['alignment']) && is_array($styles['paragraph'])) {
256256
$styles['paragraph']['alignment'] = $styles['font']['alignment'];
257257
}
258258

0 commit comments

Comments
 (0)