Skip to content

Commit 337dc27

Browse files
jnlinJui-Nan LinProgi1984
authored
feat(font): add default asian font name (#2714)
* feat(font): add default asian font name * doc: examples and changes * test: add test for DefaultAsianFontName * fix: specify the return value type Co-authored-by: Progi1984 <[email protected]> * test: test set/get function in PHPWord * style: removed unused annotiation --------- Co-authored-by: Jui-Nan Lin <[email protected]> Co-authored-by: Progi1984 <[email protected]>
1 parent f9da10e commit 337dc27

File tree

7 files changed

+83
-2
lines changed

7 files changed

+83
-2
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Enhancements
66

7+
- Default Font: Allow specify Asisn font and Latin font separately
8+
79
- 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)
810
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
911
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)

docs/usage/introduction.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ $phpWord->setDefaultFontName('Times New Roman');
137137
$phpWord->setDefaultFontSize(12);
138138
```
139139

140+
Or you can specify Asian Font
141+
142+
``` php
143+
<?php
144+
145+
$phpWord->setDefaultAsianFontName('標楷體');
146+
```
147+
140148
## Document settings
141149

142150
Settings for the generated document can be set using ``$phpWord->getSettings()``
@@ -380,4 +388,4 @@ To control whether or not words in all capital letters shall be hyphenated use t
380388
<?php
381389

382390
$phpWord->getSettings()->setDoNotHyphenateCaps(true);
383-
```
391+
```

src/PhpWord/PhpWord.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ public function setDefaultFontName($fontName): void
257257
Settings::setDefaultFontName($fontName);
258258
}
259259

260+
/**
261+
* Get default asian font name.
262+
*/
263+
public function getDefaultAsianFontName(): string
264+
{
265+
return Settings::getDefaultAsianFontName();
266+
}
267+
268+
/**
269+
* Set default font name.
270+
*
271+
* @param string $fontName
272+
*/
273+
public function setDefaultAsianFontName($fontName): void
274+
{
275+
Settings::setDefaultAsianFontName($fontName);
276+
}
277+
260278
/**
261279
* Get default font size.
262280
*

src/PhpWord/Settings.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class Settings
118118
*/
119119
private static $defaultFontName = self::DEFAULT_FONT_NAME;
120120

121+
/**
122+
* Default font name.
123+
*
124+
* @var string
125+
*/
126+
private static $defaultAsianFontName = self::DEFAULT_FONT_NAME;
127+
121128
/**
122129
* Default font size.
123130
*
@@ -355,6 +362,14 @@ public static function getDefaultFontName(): string
355362
return self::$defaultFontName;
356363
}
357364

365+
/**
366+
* Get default font name.
367+
*/
368+
public static function getDefaultAsianFontName(): string
369+
{
370+
return self::$defaultAsianFontName;
371+
}
372+
358373
/**
359374
* Set default font name.
360375
*/
@@ -369,6 +384,17 @@ public static function setDefaultFontName(string $value): bool
369384
return false;
370385
}
371386

387+
public static function setDefaultAsianFontName(string $value): bool
388+
{
389+
if (trim($value) !== '') {
390+
self::$defaultAsianFontName = $value;
391+
392+
return true;
393+
}
394+
395+
return false;
396+
}
397+
372398
/**
373399
* Get default font size.
374400
*

src/PhpWord/Writer/Word2007/Part/Styles.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
8484
{
8585
$phpWord = $this->getParentWriter()->getPhpWord();
8686
$fontName = $phpWord->getDefaultFontName();
87+
$asianFontName = $phpWord->getDefaultAsianFontName();
8788
$fontSize = $phpWord->getDefaultFontSize();
8889
$language = $phpWord->getSettings()->getThemeFontLang();
8990
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
@@ -95,7 +96,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
9596
$xmlWriter->startElement('w:rFonts');
9697
$xmlWriter->writeAttribute('w:ascii', $fontName);
9798
$xmlWriter->writeAttribute('w:hAnsi', $fontName);
98-
$xmlWriter->writeAttribute('w:eastAsia', $fontName);
99+
$xmlWriter->writeAttribute('w:eastAsia', $asianFontName);
99100
$xmlWriter->writeAttribute('w:cs', $fontName);
100101
$xmlWriter->endElement(); // w:rFonts
101102
$xmlWriter->startElement('w:sz');

tests/PhpWordTests/PhpWordTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public function testSetGetDefaultFontSize(): void
8383
self::assertEquals($fontSize, $phpWord->getDefaultFontSize());
8484
}
8585

86+
/**
87+
* Test set/get default asian font name.
88+
*/
89+
public function testSetGetDefaultAsianFontName(): void
90+
{
91+
$phpWord = new PhpWord();
92+
$fontName = 'Times New Roman';
93+
self::assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultAsianFontName());
94+
$phpWord->setDefaultAsianFontName($fontName);
95+
self::assertEquals($fontName, $phpWord->getDefaultAsianFontName());
96+
}
97+
8698
/**
8799
* Test set default paragraph style.
88100
*/

tests/PhpWordTests/SettingsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ public function testSetGetDefaultFontName(): void
217217
self::assertEquals('Times New Roman', Settings::getDefaultFontName());
218218
}
219219

220+
/**
221+
* Test set/get default font name.
222+
*/
223+
public function testSetGetDefaultAsianFontName(): void
224+
{
225+
self::assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultAsianFontName());
226+
self::assertFalse(Settings::setDefaultAsianFontName(' '));
227+
self::assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultAsianFontName());
228+
self::assertTrue(Settings::setDefaultAsianFontName('Times New Roman'));
229+
self::assertEquals('Times New Roman', Settings::getDefaultAsianFontName());
230+
self::assertFalse(Settings::setDefaultAsianFontName(' '));
231+
self::assertEquals('Times New Roman', Settings::getDefaultAsianFontName());
232+
}
233+
220234
/**
221235
* Test set/get default font size.
222236
*/

0 commit comments

Comments
 (0)