Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(font): add default asian font name #2714

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Expand Down
10 changes: 9 additions & 1 deletion docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -381,4 +389,4 @@ To control whether or not words in all capital letters shall be hyphenated use t
<?php

$phpWord->getSettings()->setDoNotHyphenateCaps(true);
```
```
20 changes: 20 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
26 changes: 26 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*
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 @@ -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();
Expand All @@ -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');
Expand Down
14 changes: 14 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading