Skip to content

Commit e7c551a

Browse files
authored
Add possibility to show/hide spelling and grammatical errors (#985)
* Add possibility to show/hide spelling and grammatical errors
1 parent 6a3135b commit e7c551a

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ composer.lock
1111
composer.phar
1212
vendor
1313
/report
14+
/build
1415
/samples/resources
1516
/samples/results
1617
/.settings
1718
phpword.ini
1819
/.buildpath
19-
/.project
20+
/.project

docs/general.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ Zip class
109109
By default, PHPWord uses `Zip extension <http://php.net/manual/en/book.zip.php>`__
110110
to deal with ZIP compressed archives and files inside them. If you can't have
111111
Zip extension installed on your server, you can use pure PHP library
112-
alternative, `PclZip <http://www.phpconcept.net/pclzip/>`__, which
113-
included with PHPWord.
112+
alternative, `PclZip <http://www.phpconcept.net/pclzip/>`__, which is
113+
included in PHPWord.
114114

115115
.. code-block:: php
116116
@@ -130,6 +130,17 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c
130130
131131
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
132132
133+
Spelling and grammatical checks
134+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135+
136+
By default spelling and grammatical errors are shown as soon as you open a word document.
137+
For big documents this can slow down the opening of the document. You can hide the spelling and/or grammatical errors with:
138+
139+
.. code-block:: php
140+
141+
\PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true);
142+
\PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true);
143+
133144
Default font
134145
~~~~~~~~~~~~
135146

@@ -183,3 +194,16 @@ points to twips.
183194
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
184195
// 2 cm right margin
185196
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
197+
198+
Language
199+
--------
200+
201+
You can hide spelling errors:
202+
203+
.. code-block:: php
204+
\PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true);
205+
206+
And hide grammatical errors:
207+
208+
.. code-block:: php
209+
\PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true);

src/PhpWord/Settings.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ class Settings
119119
*/
120120
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
121121

122+
/**
123+
* Hide spelling errors
124+
* @var boolean
125+
*/
126+
private static $spellingErrorsHidden = false;
127+
128+
/**
129+
* Hide grammatical errors
130+
* @var boolean
131+
*/
132+
private static $grammaticalErrorsHidden = false;
133+
122134
/**
123135
* The user defined temporary directory.
124136
*
@@ -416,6 +428,46 @@ public static function setDefaultFontSize($value)
416428
return false;
417429
}
418430

431+
/**
432+
* Are spelling errors hidden
433+
*
434+
* @return boolean
435+
*/
436+
public static function isSpellingErrorsHidden()
437+
{
438+
return self::$spellingErrorsHidden;
439+
}
440+
441+
/**
442+
* Hide spelling errors
443+
*
444+
* @param boolean $spellingErrorsHidden
445+
*/
446+
public static function setSpellingErrorsHidden($spellingErrorsHidden)
447+
{
448+
self::$spellingErrorsHidden = $spellingErrorsHidden;
449+
}
450+
451+
/**
452+
* Are grammatical errors hidden
453+
*
454+
* @return boolean
455+
*/
456+
public static function isGrammaticalErrorsHidden()
457+
{
458+
return self::$grammaticalErrorsHidden;
459+
}
460+
461+
/**
462+
* Hide grammatical errors
463+
*
464+
* @param boolean $grammaticalErrorsHidden
465+
*/
466+
public static function setGrammaticalErrorsHidden($grammaticalErrorsHidden)
467+
{
468+
self::$grammaticalErrorsHidden = $grammaticalErrorsHidden;
469+
}
470+
419471
/**
420472
* Load setting from phpword.yml or phpword.yml.dist
421473
*

src/PhpWord/Writer/Word2007/Part/Settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ private function getSettings()
107107
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
108108
'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')),
109109
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
110+
'w:hideSpellingErrors' => array('@attributes' => array('w:val' => DocumentSettings::isSpellingErrorsHidden() ? 'true' : 'false')),
111+
'w:hideGrammaticalErrors' => array('@attributes' => array('w:val' => DocumentSettings::isGrammaticalErrorsHidden() ? 'true' : 'false')),
110112
'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
111113
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
112114
'w:compat' => array(),

tests/PhpWord/SettingsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ public function testSetGetDefaultFontSize()
114114
$this->assertFalse(Settings::setDefaultFontSize(null));
115115
}
116116

117+
/**
118+
* Test set/get spelling and grammar
119+
*/
120+
public function testSetGetSpellingGrammar()
121+
{
122+
$this->assertFalse(Settings::isSpellingErrorsHidden());
123+
Settings::setSpellingErrorsHidden(true);
124+
$this->assertTrue(Settings::isSpellingErrorsHidden());
125+
126+
$this->assertFalse(Settings::isGrammaticalErrorsHidden());
127+
Settings::setGrammaticalErrorsHidden(true);
128+
$this->assertTrue(Settings::isGrammaticalErrorsHidden());
129+
}
130+
117131
/**
118132
* Test set/get even and odd headers
119133
*/

tests/PhpWord/Writer/Word2007/Part/SettingsTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,58 @@ public function testCompatibility()
6868
$this->assertEquals($phpWord->getCompatibility()->getOoxmlVersion(), 15);
6969
}
7070

71+
/**
72+
* Test language
73+
*/
74+
public function testLanguage()
75+
{
76+
$phpWord = new PhpWord();
77+
78+
$doc = TestHelperDOCX::getDocument($phpWord);
79+
80+
$file = 'word/settings.xml';
81+
82+
$path = '/w:settings/w:themeFontLang';
83+
$this->assertTrue($doc->elementExists($path, $file));
84+
$element = $doc->getElement($path, $file);
85+
86+
$this->assertEquals('en-US', $element->getAttribute('w:val'));
87+
}
88+
89+
/**
90+
* Test spelling
91+
*/
92+
public function testSpelling()
93+
{
94+
$phpWord = new PhpWord();
95+
Settings::setSpellingErrorsHidden(true);
96+
97+
$doc = TestHelperDOCX::getDocument($phpWord);
98+
99+
$file = 'word/settings.xml';
100+
101+
$path = '/w:settings/w:hideSpellingErrors';
102+
$this->assertTrue($doc->elementExists($path, $file));
103+
$element = $doc->getElement($path, $file);
104+
105+
$this->assertEquals('true', $element->getAttribute('w:val'));
106+
}
107+
71108
/**
72109
* Test even and odd headers
73110
*/
74111
public function testEvenAndOddHeaders()
75112
{
76113
$phpWord = new PhpWord();
77114
Settings::setEvenAndOddHeaders(true);
78-
115+
79116
$doc = TestHelperDOCX::getDocument($phpWord);
80-
117+
81118
$file = 'word/settings.xml';
82-
119+
83120
$path = '/w:settings/w:evenAndOddHeaders';
84121
$this->assertTrue($doc->elementExists($path, $file));
85-
122+
86123
$element = $doc->getElement($path, $file);
87124
$this->assertEquals('true', $element->getAttribute('w:val'));
88125
}

0 commit comments

Comments
 (0)