From c2b54cc3432a003ccb79ba874dea4368567234e8 Mon Sep 17 00:00:00 2001 From: Alex Morozov Date: Sun, 9 Dec 2018 01:21:59 +0300 Subject: [PATCH] add support for hidden text (#1527) * added hidden text word 2007 * update changelog * update documentation * added unit test * docx reader * html reader/writer * odt writer * updated samples --- CHANGELOG.md | 1 + composer.json | 2 +- docs/styles.rst | 1 + samples/Sample_04_Textrun.php | 3 ++ samples/Sample_26_Html.php | 3 ++ src/PhpWord/Reader/Word2007/AbstractPart.php | 1 + src/PhpWord/Reader/Word2007/Styles.php | 1 + src/PhpWord/Shared/Html.php | 3 ++ src/PhpWord/Style/Font.php | 32 ++++++++++++++++++++ src/PhpWord/Writer/HTML/Style/Font.php | 1 + src/PhpWord/Writer/ODText/Style/Font.php | 3 ++ src/PhpWord/Writer/Word2007/Style/Font.php | 3 ++ tests/PhpWord/Reader/Word2007/StyleTest.php | 24 +++++++++++++++ tests/PhpWord/Style/FontTest.php | 2 ++ 14 files changed, 79 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 735f6d4d..d56a1cff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ v0.16.0 (xx dec 2018) ---------------------- ### Added - Add setting Chart Title and Legend visibility @Tom-Magill #1433 +- Add support for hidden text @Alexmg86 #1527 ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 diff --git a/composer.json b/composer.json index 5d8a855b..24d8532b 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "php-cs-fixer fix --ansi --dry-run --diff", "phpcs --report-width=200 --report-summary --report-full samples/ src/ tests/ --ignore=src/PhpWord/Shared/PCLZip --standard=PSR2 -n", "phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php", - "@test" + "@test-no-coverage" ], "fix": [ "php-cs-fixer fix --ansi" diff --git a/docs/styles.rst b/docs/styles.rst index 855eab79..31d04a3b 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -61,6 +61,7 @@ Available Font style options: - ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes. - ``position``. The text position, raised or lowered, in half points +- ``hidden``. Hidden text, *true* or *false*. .. _paragraph-style: diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index 48978dd3..ecd0c88a 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -39,6 +39,9 @@ $textrun->addText(' Sample Object: '); $textrun->addObject('resources/_sheet.xls'); $textrun->addText(' Here is some more text. '); +$textrun = $section->addTextRun(); +$textrun->addText('This text is not visible.', array('hidden' => true)); + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/samples/Sample_26_Html.php b/samples/Sample_26_Html.php index e1823c43..82a5cf6e 100644 --- a/samples/Sample_26_Html.php +++ b/samples/Sample_26_Html.php @@ -89,6 +89,9 @@ $html .= '
Cell in parent table
'; +$html .= '

The text below is not visible, click on show/hide to reveil it:

'; +$html .= '

This is hidden text

'; + \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false); // Save file diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 5e5eb1d6..eada60ee 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -444,6 +444,7 @@ abstract class AbstractPart 'rtl' => array(self::READ_TRUE, 'w:rtl'), 'lang' => array(self::READ_VALUE, 'w:lang'), 'position' => array(self::READ_VALUE, 'w:position'), + 'hidden' => array(self::READ_TRUE, 'w:vanish'), ); return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs); diff --git a/src/PhpWord/Reader/Word2007/Styles.php b/src/PhpWord/Reader/Word2007/Styles.php index f343ad92..554f4565 100644 --- a/src/PhpWord/Reader/Word2007/Styles.php +++ b/src/PhpWord/Reader/Word2007/Styles.php @@ -68,6 +68,7 @@ class Styles extends AbstractPart if (is_null($name)) { $name = $xmlReader->getAttribute('w:val', $node, 'w:name'); } + $headingMatches = array(); preg_match('/Heading(\d)/', $name, $headingMatches); // $default = ($xmlReader->getAttribute('w:default', $node) == 1); switch ($type) { diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 3f07a058..7f4bf825 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -515,6 +515,9 @@ class Html case 'text-align': $styles['alignment'] = self::mapAlign($cValue); break; + case 'display': + $styles['hidden'] = $cValue === 'none'; + break; case 'direction': $styles['rtl'] = $cValue === 'rtl'; break; diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index d60ebafe..018604d3 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -252,6 +252,14 @@ class Font extends AbstractStyle */ private $lang; + /** + * Hidden text + * + * @var bool + * @see http://www.datypic.com/sc/ooxml/e-w_vanish-1.html + */ + private $hidden = false; + /** * Vertically Raised or Lowered Text * @@ -299,6 +307,7 @@ class Font extends AbstractStyle 'smallCaps' => $this->isSmallCaps(), 'allCaps' => $this->isAllCaps(), 'fgColor' => $this->getFgColor(), + 'hidden' => $this->isHidden(), ), 'spacing' => array( 'scale' => $this->getScale(), @@ -938,6 +947,29 @@ class Font extends AbstractStyle return $this->getParagraph(); } + /** + * Get hidden text + * + * @return bool + */ + public function isHidden() + { + return $this->hidden; + } + + /** + * Set hidden text + * + * @param bool $value + * @return self + */ + public function setHidden($value = true) + { + $this->hidden = $this->setBoolVal($value, $this->hidden); + + return $this; + } + /** * Get position * diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php index 1aeaa347..75c98b9b 100644 --- a/src/PhpWord/Writer/HTML/Style/Font.php +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -60,6 +60,7 @@ class Font extends AbstractStyle $css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through '); $css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase'); $css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps'); + $css['display'] = $this->getValueIf($style->isHidden(), 'none'); $spacing = $style->getSpacing(); $css['letter-spacing'] = $this->getValueIf(!is_null($spacing), ($spacing / 20) . 'pt'); diff --git a/src/PhpWord/Writer/ODText/Style/Font.php b/src/PhpWord/Writer/ODText/Style/Font.php index 7c7d20dd..29657c5a 100644 --- a/src/PhpWord/Writer/ODText/Style/Font.php +++ b/src/PhpWord/Writer/ODText/Style/Font.php @@ -75,6 +75,9 @@ class Font extends AbstractStyle $xmlWriter->writeAttributeIf($style->isSmallCaps(), 'fo:font-variant', 'small-caps'); $xmlWriter->writeAttributeIf($style->isAllCaps(), 'fo:text-transform', 'uppercase'); + //Hidden text + $xmlWriter->writeAttributeIf($style->isHidden(), 'text:display', 'none'); + // Superscript/subscript $xmlWriter->writeAttributeIf($style->isSuperScript(), 'style:text-position', 'super'); $xmlWriter->writeAttributeIf($style->isSubScript(), 'style:text-position', 'sub'); diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 58282d15..f299d8ef 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -120,6 +120,9 @@ class Font extends AbstractStyle $xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps'); $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps'); + //Hidden text + $xmlWriter->writeElementIf($style->isHidden(), 'w:vanish'); + // Underline $xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline()); diff --git a/tests/PhpWord/Reader/Word2007/StyleTest.php b/tests/PhpWord/Reader/Word2007/StyleTest.php index d64079fa..364c79c2 100644 --- a/tests/PhpWord/Reader/Word2007/StyleTest.php +++ b/tests/PhpWord/Reader/Word2007/StyleTest.php @@ -145,4 +145,28 @@ class StyleTest extends AbstractTestReader $this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType()); $this->assertSame(2160, $tableStyle->getIndent()->getValue()); } + + public function testReadHidden() + { + $documentXml = ' + + + + + This text is hidden + + '; + + $phpWord = $this->getDocumentFromString(array('document' => $documentXml)); + + $elements = $phpWord->getSection(0)->getElements(); + /** @var \PhpOffice\PhpWord\Element\TextRun $elements */ + $textRun = $elements[0]; + $this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $textRun); + $this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $textRun->getElement(0)); + $this->assertInstanceOf('PhpOffice\PhpWord\Style\Font', $textRun->getElement(0)->getFontStyle()); + /** @var \PhpOffice\PhpWord\Style\Font $fontStyle */ + $fontStyle = $textRun->getElement(0)->getFontStyle(); + $this->assertTrue($fontStyle->isHidden()); + } } diff --git a/tests/PhpWord/Style/FontTest.php b/tests/PhpWord/Style/FontTest.php index 6a934579..84916fc2 100644 --- a/tests/PhpWord/Style/FontTest.php +++ b/tests/PhpWord/Style/FontTest.php @@ -76,6 +76,7 @@ class FontTest extends \PHPUnit\Framework\TestCase 'spacing' => null, 'kerning' => null, 'lang' => null, + 'hidden' => false, ); foreach ($attributes as $key => $default) { $get = is_bool($default) ? "is{$key}" : "get{$key}"; @@ -117,6 +118,7 @@ class FontTest extends \PHPUnit\Framework\TestCase 'rtl' => true, 'noProof' => true, 'lang' => new Language(Language::EN_US), + 'hidden' => true, ); $object->setStyleByArray($attributes); foreach ($attributes as $key => $value) {