From e7c551a0bfe2b92befebf40a86f4645e540e108e Mon Sep 17 00:00:00 2001 From: troosan Date: Sun, 2 Jul 2017 00:37:29 +0200 Subject: [PATCH] Add possibility to show/hide spelling and grammatical errors (#985) * Add possibility to show/hide spelling and grammatical errors --- .gitignore | 3 +- docs/general.rst | 28 +++++++++- src/PhpWord/Settings.php | 52 +++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Settings.php | 2 + tests/PhpWord/SettingsTest.php | 14 +++++ .../Writer/Word2007/Part/SettingsTest.php | 45 ++++++++++++++-- 6 files changed, 137 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e5deb643..98f65dbf 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,10 @@ composer.lock composer.phar vendor /report +/build /samples/resources /samples/results /.settings phpword.ini /.buildpath -/.project \ No newline at end of file +/.project diff --git a/docs/general.rst b/docs/general.rst index 27d0448a..95340125 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -109,8 +109,8 @@ Zip class By default, PHPWord uses `Zip extension `__ to deal with ZIP compressed archives and files inside them. If you can't have Zip extension installed on your server, you can use pure PHP library -alternative, `PclZip `__, which -included with PHPWord. +alternative, `PclZip `__, which is +included in PHPWord. .. code-block:: php @@ -130,6 +130,17 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); +Spelling and grammatical checks +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default spelling and grammatical errors are shown as soon as you open a word document. +For big documents this can slow down the opening of the document. You can hide the spelling and/or grammatical errors with: + +.. code-block:: php + + \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); + \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); + Default font ~~~~~~~~~~~~ @@ -183,3 +194,16 @@ points to twips. $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); + +Language +-------- + +You can hide spelling errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); + +And hide grammatical errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 3fbbb0a6..3da32cbb 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -119,6 +119,18 @@ class Settings */ private static $defaultFontSize = self::DEFAULT_FONT_SIZE; + /** + * Hide spelling errors + * @var boolean + */ + private static $spellingErrorsHidden = false; + + /** + * Hide grammatical errors + * @var boolean + */ + private static $grammaticalErrorsHidden = false; + /** * The user defined temporary directory. * @@ -416,6 +428,46 @@ class Settings return false; } + /** + * Are spelling errors hidden + * + * @return boolean + */ + public static function isSpellingErrorsHidden() + { + return self::$spellingErrorsHidden; + } + + /** + * Hide spelling errors + * + * @param boolean $spellingErrorsHidden + */ + public static function setSpellingErrorsHidden($spellingErrorsHidden) + { + self::$spellingErrorsHidden = $spellingErrorsHidden; + } + + /** + * Are grammatical errors hidden + * + * @return boolean + */ + public static function isGrammaticalErrorsHidden() + { + return self::$grammaticalErrorsHidden; + } + + /** + * Hide grammatical errors + * + * @param boolean $grammaticalErrorsHidden + */ + public static function setGrammaticalErrorsHidden($grammaticalErrorsHidden) + { + self::$grammaticalErrorsHidden = $grammaticalErrorsHidden; + } + /** * Load setting from phpword.yml or phpword.yml.dist * diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 82669f91..72406462 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -107,6 +107,8 @@ class Settings extends AbstractPart 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), 'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')), 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), + 'w:hideSpellingErrors' => array('@attributes' => array('w:val' => DocumentSettings::isSpellingErrorsHidden() ? 'true' : 'false')), + 'w:hideGrammaticalErrors' => array('@attributes' => array('w:val' => DocumentSettings::isGrammaticalErrorsHidden() ? 'true' : 'false')), 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:compat' => array(), diff --git a/tests/PhpWord/SettingsTest.php b/tests/PhpWord/SettingsTest.php index 764fccd4..6347f424 100644 --- a/tests/PhpWord/SettingsTest.php +++ b/tests/PhpWord/SettingsTest.php @@ -114,6 +114,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertFalse(Settings::setDefaultFontSize(null)); } + /** + * Test set/get spelling and grammar + */ + public function testSetGetSpellingGrammar() + { + $this->assertFalse(Settings::isSpellingErrorsHidden()); + Settings::setSpellingErrorsHidden(true); + $this->assertTrue(Settings::isSpellingErrorsHidden()); + + $this->assertFalse(Settings::isGrammaticalErrorsHidden()); + Settings::setGrammaticalErrorsHidden(true); + $this->assertTrue(Settings::isGrammaticalErrorsHidden()); + } + /** * Test set/get even and odd headers */ diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index be018f8c..13210fb7 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -68,6 +68,43 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($phpWord->getCompatibility()->getOoxmlVersion(), 15); } + /** + * Test language + */ + public function testLanguage() + { + $phpWord = new PhpWord(); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:themeFontLang'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('en-US', $element->getAttribute('w:val')); + } + + /** + * Test spelling + */ + public function testSpelling() + { + $phpWord = new PhpWord(); + Settings::setSpellingErrorsHidden(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:hideSpellingErrors'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('true', $element->getAttribute('w:val')); + } + /** * Test even and odd headers */ @@ -75,14 +112,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); Settings::setEvenAndOddHeaders(true); - + $doc = TestHelperDOCX::getDocument($phpWord); - + $file = 'word/settings.xml'; - + $path = '/w:settings/w:evenAndOddHeaders'; $this->assertTrue($doc->elementExists($path, $file)); - + $element = $doc->getElement($path, $file); $this->assertEquals('true', $element->getAttribute('w:val')); }