Add possibility to show/hide spelling and grammatical errors (#985)

* Add possibility to show/hide spelling and grammatical errors
This commit is contained in:
troosan 2017-07-02 00:37:29 +02:00 committed by GitHub
parent 6a3135bff2
commit e7c551a0bf
6 changed files with 137 additions and 7 deletions

3
.gitignore vendored
View File

@ -11,9 +11,10 @@ composer.lock
composer.phar composer.phar
vendor vendor
/report /report
/build
/samples/resources /samples/resources
/samples/results /samples/results
/.settings /.settings
phpword.ini phpword.ini
/.buildpath /.buildpath
/.project /.project

View File

@ -109,8 +109,8 @@ Zip class
By default, PHPWord uses `Zip extension <http://php.net/manual/en/book.zip.php>`__ By default, PHPWord uses `Zip extension <http://php.net/manual/en/book.zip.php>`__
to deal with ZIP compressed archives and files inside them. If you can't have 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 Zip extension installed on your server, you can use pure PHP library
alternative, `PclZip <http://www.phpconcept.net/pclzip/>`__, which alternative, `PclZip <http://www.phpconcept.net/pclzip/>`__, which is
included with PHPWord. included in PHPWord.
.. code-block:: php .. 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); \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 Default font
~~~~~~~~~~~~ ~~~~~~~~~~~~
@ -183,3 +194,16 @@ points to twips.
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
// 2 cm right margin // 2 cm right margin
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); $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);

View File

@ -119,6 +119,18 @@ class Settings
*/ */
private static $defaultFontSize = self::DEFAULT_FONT_SIZE; 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. * The user defined temporary directory.
* *
@ -416,6 +428,46 @@ class Settings
return false; 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 * Load setting from phpword.yml or phpword.yml.dist
* *

View File

@ -107,6 +107,8 @@ class Settings extends AbstractPart
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')), 'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')),
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), '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:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
'w:compat' => array(), 'w:compat' => array(),

View File

@ -114,6 +114,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertFalse(Settings::setDefaultFontSize(null)); $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 * Test set/get even and odd headers
*/ */

View File

@ -68,6 +68,43 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($phpWord->getCompatibility()->getOoxmlVersion(), 15); $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 * Test even and odd headers
*/ */
@ -75,14 +112,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
{ {
$phpWord = new PhpWord(); $phpWord = new PhpWord();
Settings::setEvenAndOddHeaders(true); Settings::setEvenAndOddHeaders(true);
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/settings.xml'; $file = 'word/settings.xml';
$path = '/w:settings/w:evenAndOddHeaders'; $path = '/w:settings/w:evenAndOddHeaders';
$this->assertTrue($doc->elementExists($path, $file)); $this->assertTrue($doc->elementExists($path, $file));
$element = $doc->getElement($path, $file); $element = $doc->getElement($path, $file);
$this->assertEquals('true', $element->getAttribute('w:val')); $this->assertEquals('true', $element->getAttribute('w:val'));
} }