From ea83d11b2e8b5146c6cfff734b3172720c750224 Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Tue, 19 Nov 2024 14:32:52 +0800 Subject: [PATCH 1/6] feat(font): add default asian font name --- src/PhpWord/PhpWord.php | 20 ++++++++++++++++ src/PhpWord/Settings.php | 26 +++++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Styles.php | 3 ++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index cf6f16ae02..bcc7131012 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -256,6 +256,26 @@ public function setDefaultFontName($fontName): void Settings::setDefaultFontName($fontName); } + /** + * Get default asian font name. + * + * @return string + */ + public function getDefaultAsianFontName() + { + return Settings::getDefaultAsianFontName(); + } + + /** + * Set default font name. + * + * @param string $fontName + */ + public function setDefaultAsianFontName($fontName): void + { + Settings::setDefaultAsianFontName($fontName); + } + /** * Get default font size. * diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 984486ccfe..02f3b036fb 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -117,6 +117,13 @@ class Settings */ private static $defaultFontName = self::DEFAULT_FONT_NAME; + /** + * Default font name. + * + * @var string + */ + private static $defaultAsianFontName = self::DEFAULT_FONT_NAME; + /** * Default font size. * @@ -354,6 +361,14 @@ public static function getDefaultFontName(): string return self::$defaultFontName; } + /** + * Get default font name. + */ + public static function getDefaultAsianFontName(): string + { + return self::$defaultAsianFontName; + } + /** * Set default font name. */ @@ -368,6 +383,17 @@ public static function setDefaultFontName(string $value): bool return false; } + public static function setDefaultAsianFontName(string $value): bool + { + if (trim($value) !== '') { + self::$defaultAsianFontName = $value; + + return true; + } + + return false; + } + /** * Get default font size. * diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 2112fd3ce6..9d1f36433e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -83,6 +83,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void { $phpWord = $this->getParentWriter()->getPhpWord(); $fontName = $phpWord->getDefaultFontName(); + $asianFontName = $phpWord->getDefaultAsianFontName(); $fontSize = $phpWord->getDefaultFontSize(); $language = $phpWord->getSettings()->getThemeFontLang(); $latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin(); @@ -94,7 +95,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $xmlWriter->startElement('w:rFonts'); $xmlWriter->writeAttribute('w:ascii', $fontName); $xmlWriter->writeAttribute('w:hAnsi', $fontName); - $xmlWriter->writeAttribute('w:eastAsia', $fontName); + $xmlWriter->writeAttribute('w:eastAsia', $asianFontName); $xmlWriter->writeAttribute('w:cs', $fontName); $xmlWriter->endElement(); // w:rFonts $xmlWriter->startElement('w:sz'); From 9edfaedae9a05d166038c06bbcd8cb72452c49c5 Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Fri, 6 Dec 2024 11:28:03 +0800 Subject: [PATCH 2/6] doc: examples and changes --- docs/changes/1.x/1.4.0.md | 2 ++ docs/usage/introduction.md | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 8db845337d..a5cfad637b 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -4,6 +4,8 @@ ## Enhancements +- Default Font: Allow specify Asisn font and Latin font separately + - 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) diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index b3a101ab0d..2e60d65b19 100644 --- a/docs/usage/introduction.md +++ b/docs/usage/introduction.md @@ -138,6 +138,14 @@ $phpWord->setDefaultFontName('Times New Roman'); $phpWord->setDefaultFontSize(12); ``` +Or you can specify Asian Font + +``` php +setDefaultAsianFontName('標楷體'); +``` + ## Document settings Settings for the generated document can be set using ``$phpWord->getSettings()`` @@ -381,4 +389,4 @@ To control whether or not words in all capital letters shall be hyphenated use t getSettings()->setDoNotHyphenateCaps(true); -``` \ No newline at end of file +``` From e14617630c9cb35ec1ae62a6cedc54e10f62b1e2 Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Fri, 6 Dec 2024 11:50:47 +0800 Subject: [PATCH 3/6] test: add test for DefaultAsianFontName --- tests/PhpWordTests/SettingsTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/PhpWordTests/SettingsTest.php b/tests/PhpWordTests/SettingsTest.php index 46c72eab28..8ba78a5012 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -216,6 +216,20 @@ public function testSetGetDefaultFontName(): void self::assertEquals('Times New Roman', Settings::getDefaultFontName()); } + /** + * Test set/get default font name. + */ + public function testSetGetDefaultAsianFontName(): void + { + self::assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultAsianFontName()); + self::assertFalse(Settings::setDefaultAsianFontName(' ')); + self::assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultAsianFontName()); + self::assertTrue(Settings::setDefaultAsianFontName('Times New Roman')); + self::assertEquals('Times New Roman', Settings::getDefaultAsianFontName()); + self::assertFalse(Settings::setDefaultAsianFontName(' ')); + self::assertEquals('Times New Roman', Settings::getDefaultAsianFontName()); + } + /** * Test set/get default font size. */ From d907fffeb9a3bf6eeee60d3dc63b7b8817f0529c Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Wed, 15 Jan 2025 10:48:12 +0800 Subject: [PATCH 4/6] fix: specify the return value type Co-authored-by: Progi1984 --- src/PhpWord/PhpWord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index bcc7131012..773b41ed78 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -261,7 +261,7 @@ public function setDefaultFontName($fontName): void * * @return string */ - public function getDefaultAsianFontName() + public function getDefaultAsianFontName(): string { return Settings::getDefaultAsianFontName(); } From 9a423cf889d1ae8526f88144dee47ccf8c50e48d Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Wed, 15 Jan 2025 11:00:30 +0800 Subject: [PATCH 5/6] test: test set/get function in PHPWord --- tests/PhpWordTests/PhpWordTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/PhpWordTests/PhpWordTest.php b/tests/PhpWordTests/PhpWordTest.php index 33118a11e8..5ed90161a5 100644 --- a/tests/PhpWordTests/PhpWordTest.php +++ b/tests/PhpWordTests/PhpWordTest.php @@ -82,6 +82,18 @@ public function testSetGetDefaultFontSize(): void self::assertEquals($fontSize, $phpWord->getDefaultFontSize()); } + /** + * Test set/get default asian font name. + */ + public function testSetGetDefaultAsianFontName(): void + { + $phpWord = new PhpWord(); + $fontName = 'Times New Roman'; + self::assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultAsianFontName()); + $phpWord->setDefaultAsianFontName($fontName); + self::assertEquals($fontName, $phpWord->getDefaultAsianFontName()); + } + /** * Test set default paragraph style. */ From b1ba078ad2e886e57c51c524bb6f91714a76e69e Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Wed, 15 Jan 2025 11:03:50 +0800 Subject: [PATCH 6/6] style: removed unused annotiation --- src/PhpWord/PhpWord.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 773b41ed78..185b1d3405 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -258,8 +258,6 @@ public function setDefaultFontName($fontName): void /** * Get default asian font name. - * - * @return string */ public function getDefaultAsianFontName(): string {