Skip to content

Commit

Permalink
feat(font): add default asian font name (#2714)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jan 15, 2025
1 parent f9da10e commit 337dc27
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
Expand Down
10 changes: 9 additions & 1 deletion docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ $phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontSize(12);
```

Or you can specify Asian Font

``` php
<?php

$phpWord->setDefaultAsianFontName('標楷體');
```

## Document settings

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

$phpWord->getSettings()->setDoNotHyphenateCaps(true);
```
```
18 changes: 18 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ public function setDefaultFontName($fontName): void
Settings::setDefaultFontName($fontName);
}

/**
* Get default asian font name.
*/
public function getDefaultAsianFontName(): string
{
return Settings::getDefaultAsianFontName();
}

/**
* Set default font name.
*
* @param string $fontName
*/
public function setDefaultAsianFontName($fontName): void
{
Settings::setDefaultAsianFontName($fontName);
}

/**
* Get default font size.
*
Expand Down
26 changes: 26 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,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.
*
Expand Down Expand Up @@ -355,6 +362,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.
*/
Expand All @@ -369,6 +384,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.
*
Expand Down
3 changes: 2 additions & 1 deletion src/PhpWord/Writer/Word2007/Part/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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();
Expand All @@ -95,7 +96,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');
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpWordTests/PhpWordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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.
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,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.
*/
Expand Down

0 comments on commit 337dc27

Please sign in to comment.