From e96295dbfa9f3fee0a5a06be34a96da7b0f84a45 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Sun, 16 Feb 2025 11:25:45 +0100 Subject: [PATCH 1/2] improvement in Default Font handling in Reader ODText & Word2017 --- docs/changes/1.x/1.4.0.md | 8 +- src/PhpWord/Reader/ODText.php | 1 + src/PhpWord/Reader/ODText/Styles.php | 49 ++++++++++ src/PhpWord/Reader/Word2007/AbstractPart.php | 1 + src/PhpWord/Reader/Word2007/Styles.php | 3 + src/PhpWord/Writer/ODText/Part/Styles.php | 2 +- .../PhpWordTests/WriteReadback/ODTextTest.php | 96 +++++++++++++++++++ .../WriteReadback/Word2007Test.php | 8 ++ 8 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/PhpWord/Reader/ODText/Styles.php diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 6fdef4e74f..6e06f504ff 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -4,7 +4,12 @@ ## Enhancements -- Default Font: Allow specify Asisn font and Latin font separately +- Default Font: Allow specify Asian font and Latin font separately +-- in the datastructures in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) +-- Writer Word2007 in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) +-- Reader Word2007 in [#](https://github.com/PHPOffice/PHPWord/pull/) +-- Writer ODText in [#](https://github.com/PHPOffice/PHPWord/pull/) +-- Reader ODText in [#](https://github.com/PHPOffice/PHPWord/pull/) - Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669) - Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675) @@ -18,6 +23,7 @@ - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) - Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) +- Reader ODText: addded (limited) support for reading default font styles by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#(https://github.com/PHPOffice/PHPWord/pull/) ### Bug fixes diff --git a/src/PhpWord/Reader/ODText.php b/src/PhpWord/Reader/ODText.php index 288bf9a0dd..0f928db4aa 100644 --- a/src/PhpWord/Reader/ODText.php +++ b/src/PhpWord/Reader/ODText.php @@ -43,6 +43,7 @@ public function load($docFile) $readerParts = [ 'content.xml' => 'Content', 'meta.xml' => 'Meta', + 'styles.xml' => 'Styles', ]; foreach ($readerParts as $xmlFile => $partName) { diff --git a/src/PhpWord/Reader/ODText/Styles.php b/src/PhpWord/Reader/ODText/Styles.php new file mode 100644 index 0000000000..40a553b868 --- /dev/null +++ b/src/PhpWord/Reader/ODText/Styles.php @@ -0,0 +1,49 @@ +getDomFromZip($this->docFile, $this->xmlFile); + $fontDefaults = $xmlReader->getElement('office:styles/style:default-style/style:text-properties'); + + if ($fontDefaults !== null) { + $phpWord->setDefaultFontName($fontDefaults->getAttribute('style:font-name')); + $phpWord->setDefaultAsianFontName($fontDefaults->getAttribute('style:font-name-asian')); + $phpWord->setDefaultFontSize((float) (str_replace('pt', '', $fontDefaults->getAttribute('fo:font-size')))); + $phpWord->setDefaultFontColor(str_replace('#', '', $fontDefaults->getAttribute('fo:color'))); + $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaults->getAttribute('fo:language'))); + } + } +} diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 96d4a46f8a..e340f76aa6 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -756,6 +756,7 @@ protected function readFontStyle(XMLReader $xmlReader, DOMElement $domNode) $styleDefs = [ 'styleName' => [self::READ_VALUE, 'w:rStyle'], 'name' => [self::READ_VALUE, 'w:rFonts', ['w:ascii', 'w:hAnsi', 'w:eastAsia', 'w:cs']], + 'nameEastAsia' => [self::READ_VALUE, 'w:rFonts', ['w:eastAsia', 'w:ascii', 'w:hAnsi', 'w:cs']], 'hint' => [self::READ_VALUE, 'w:rFonts', 'w:hint'], 'size' => [self::READ_SIZE, ['w:sz', 'w:szCs']], 'color' => [self::READ_VALUE, 'w:color'], diff --git a/src/PhpWord/Reader/Word2007/Styles.php b/src/PhpWord/Reader/Word2007/Styles.php index d0777c3026..4d1b324228 100644 --- a/src/PhpWord/Reader/Word2007/Styles.php +++ b/src/PhpWord/Reader/Word2007/Styles.php @@ -44,6 +44,9 @@ public function read(PhpWord $phpWord): void if (array_key_exists('name', $fontDefaultStyle)) { $phpWord->setDefaultFontName($fontDefaultStyle['name']); } + if (array_key_exists('nameEastAsia', $fontDefaultStyle)) { + $phpWord->setDefaultAsianFontName($fontDefaultStyle['nameEastAsia']); + } if (array_key_exists('size', $fontDefaultStyle)) { $phpWord->setDefaultFontSize($fontDefaultStyle['size']); } diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index f8ec843894..537a24a284 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -99,7 +99,7 @@ private function writeDefault(XMLWriter $xmlWriter): void $xmlWriter->writeAttribute('fo:country', $latinLang[1]); $xmlWriter->writeAttribute('fo:color', '#' . Settings::getDefaultFontColor()); $xmlWriter->writeAttribute('style:letter-kerning', 'true'); - $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); + $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultAsianFontName()); $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); $xmlWriter->writeAttribute('style:language-asian', $asianLang[0]); $xmlWriter->writeAttribute('style:country-asian', $asianLang[1]); diff --git a/tests/PhpWordTests/WriteReadback/ODTextTest.php b/tests/PhpWordTests/WriteReadback/ODTextTest.php index cee0f38301..51f3fd966d 100644 --- a/tests/PhpWordTests/WriteReadback/ODTextTest.php +++ b/tests/PhpWordTests/WriteReadback/ODTextTest.php @@ -32,6 +32,102 @@ */ class ODTextTest extends \PHPUnit\Framework\TestCase { + /** + * Test default font name. + */ + public function testDefaultFontName(): void + { + $phpWordWriter = new PhpWord(); + $testDefaultFontName = 'Times New Roman'; + $phpWordWriter->setDefaultFontName($testDefaultFontName); + + $writer = new ODText($phpWordWriter); + $file = __DIR__ . '/../_files/temp.odt'; + $writer->save($file); + + self::assertFileExists($file); + + $phpWordWriter->setDefaultFontName('This text should not be set after reading back the file'); + + $phpWordReader = IOFactory::load($file, 'ODText'); + + self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultFontName()); + + unlink($file); + } + + /** + * Test default Asian font name. + */ + public function testDefaultAsianFontName(): void + { + $phpWordWriter = new PhpWord(); + $testDefaultFontName = '標楷體'; + $phpWordWriter->setDefaultAsianFontName($testDefaultFontName); + + $writer = new ODText($phpWordWriter); + $file = __DIR__ . '/../_files/temp.odt'; + $writer->save($file); + + self::assertFileExists($file); + + $phpWordWriter->setDefaultAsianFontName('This text should not be set after reading back the file'); + + $phpWordReader = IOFactory::load($file, 'ODText'); + + self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultAsianFontName()); + + unlink($file); + } + + /** + * Test default font size. + */ + public function testDefaulFontSize(): void + { + $phpWordWriter = new PhpWord(); + $testDefaultFontSize = 144; + $phpWordWriter->setDefaultFontSize($testDefaultFontSize); + + $writer = new ODText($phpWordWriter); + $file = __DIR__ . '/../_files/temp.odt'; + $writer->save($file); + + self::assertFileExists($file); + + $phpWordWriter->setDefaultFontSize(987); //This value should not be set after reading back the file + + $phpWordReader = IOFactory::load($file, 'ODText'); + + self::assertEquals($testDefaultFontSize, $phpWordReader->getDefaultFontSize()); + + unlink($file); + } + + /** + * Test default font color. + */ + public function testDefaultFontColor(): void + { + $phpWordWriter = new PhpWord(); + $testDefaultFontColor = '00FF00'; + $phpWordWriter->setDefaultFontColor($testDefaultFontColor); + + $writer = new ODText($phpWordWriter); + $file = __DIR__ . '/../_files/temp.odt'; + $writer->save($file); + + self::assertFileExists($file); + + $phpWordWriter->setDefaultFontColor('C0FFEE'); //This value should not be set after reading back the file + + $phpWordReader = IOFactory::load($file, 'ODText'); + + self::assertEquals($testDefaultFontColor, $phpWordReader->getDefaultFontColor()); + + unlink($file); + } + /** * Test a document with one section and text. */ diff --git a/tests/PhpWordTests/WriteReadback/Word2007Test.php b/tests/PhpWordTests/WriteReadback/Word2007Test.php index 572977ccf8..34d7b816cc 100644 --- a/tests/PhpWordTests/WriteReadback/Word2007Test.php +++ b/tests/PhpWordTests/WriteReadback/Word2007Test.php @@ -48,6 +48,8 @@ public function testDefaultFontName(): void self::assertFileExists($file); + $phpWordWriter->setDefaultFontName('This text should not be set after reading back the file'); + $phpWordReader = IOFactory::load($file, 'Word2007'); self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultFontName()); @@ -70,6 +72,8 @@ public function testDefaultAsianFontName(): void self::assertFileExists($file); + $phpWordWriter->setDefaultAsianFontName('This text should not be set after reading back the file'); + $phpWordReader = IOFactory::load($file, 'Word2007'); self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultAsianFontName()); @@ -114,6 +118,8 @@ public function testDefaultFontColor(): void self::assertFileExists($file); + $phpWordWriter->setDefaultFontColor('C0FFEE'); //This value should not be set after reading back the file + $phpWordReader = IOFactory::load($file, 'Word2007'); self::assertEquals($testDefaultFontColor, $phpWordReader->getDefaultFontColor()); @@ -136,6 +142,8 @@ public function testZoom(): void self::assertFileExists($file); + $phpWordWriter->getSettings()->setZoom(98); //This value should not be set after reading back the file + $phpWordReader = IOFactory::load($file, 'Word2007'); self::assertEquals($zoomLevel, $phpWordReader->getSettings()->getZoom()); From 5e16b708518ea8b7f82d22afc75dd613f61da0cc Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Sun, 16 Feb 2025 12:04:25 +0100 Subject: [PATCH 2/2] fixes --- docs/changes/1.x/1.4.0.md | 12 ++++++------ src/PhpWord/Reader/ODText/Styles.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 6e06f504ff..2b9b62fdc5 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -5,11 +5,11 @@ ## Enhancements - Default Font: Allow specify Asian font and Latin font separately --- in the datastructures in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) --- Writer Word2007 in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) --- Reader Word2007 in [#](https://github.com/PHPOffice/PHPWord/pull/) --- Writer ODText in [#](https://github.com/PHPOffice/PHPWord/pull/) --- Reader ODText in [#](https://github.com/PHPOffice/PHPWord/pull/) + - in the datastructures in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) + - Writer Word2007 in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714) + - Reader Word2007 in [#2750](https://github.com/PHPOffice/PHPWord/pull/2750) + - Writer ODText in [#2750](https://github.com/PHPOffice/PHPWord/pull/2750) + - Reader ODText in [#2750](https://github.com/PHPOffice/PHPWord/pull/2750) - Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669) - Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675) @@ -23,7 +23,7 @@ - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) - Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) -- Reader ODText: addded (limited) support for reading default font styles by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#(https://github.com/PHPOffice/PHPWord/pull/) +- Reader ODText: addded (limited) support for reading default font styles by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#(https://github.com/PHPOffice/PHPWord/pull/2750) ### Bug fixes diff --git a/src/PhpWord/Reader/ODText/Styles.php b/src/PhpWord/Reader/ODText/Styles.php index 40a553b868..50760e363b 100644 --- a/src/PhpWord/Reader/ODText/Styles.php +++ b/src/PhpWord/Reader/ODText/Styles.php @@ -41,7 +41,7 @@ public function read(PhpWord $phpWord): void if ($fontDefaults !== null) { $phpWord->setDefaultFontName($fontDefaults->getAttribute('style:font-name')); $phpWord->setDefaultAsianFontName($fontDefaults->getAttribute('style:font-name-asian')); - $phpWord->setDefaultFontSize((float) (str_replace('pt', '', $fontDefaults->getAttribute('fo:font-size')))); + $phpWord->setDefaultFontSize((int) (str_replace('pt', '', $fontDefaults->getAttribute('fo:font-size')))); $phpWord->setDefaultFontColor(str_replace('#', '', $fontDefaults->getAttribute('fo:color'))); $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaults->getAttribute('fo:language'))); }