Forum


Replies: 2   Views: 1469
Chinese font is not applied to chinese characters
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by tjchen  · 03-12-2020 - 09:28

Please see the following code.

Both Font A and Font B are built-in Chinese fonts in Windows.

Font A is the default Chinese font in Word.

If I set "Font B" as the default font in php, "Font B" is only applied to "Some English characters" but not applied to "Some Chinese characters". 

$docx = new Phpdocx\Create\CreateDocx();
$docx->setDefaultFont('Font B');
$docx->addText('Some English characters');
$docx->addText('Some Chinese characters');
$docx->addText('Some Chinese characters', ['font'=>'Font B']);
$docx->createDocx('hello_world');

 

Because I can not type Chinese in the forum, please also see the result in the following screenshot.

https://drive.google.com/file/d/15_nAXQQZnQn65ROFzlTW-Y3rDJPOJC47/view?usp=sharing

Posted by admin  · 03-12-2020 - 10:21

Hello,

We have done some quick tests and the DOCX output is correct in all cases. Maybe the font name is not being set correctly? Please send the sample script (as file) and its DOCX output to contact[at]phpdocx.com and we'll check it.

Regards.

Posted by admin  · 04-12-2020 - 07:26

Hello,

Thanks for sending the requested files. The problem comes from one missing attribute support when using the font option with addText: w:eastAsia is not being applied and your font needs it.

We recommend you to use custom paragraph styles (and custom character styles if needed), both methods createParagraphStyle and createCharacterStyle support w:eastAsian attributes:

// style options
$style = array(
  'font' => 'fontname',
);
// create custom style
$docx->createParagraphStyle('myStyle', $style);

If you want to add support for w:eastAsian attribute in addText method with the font option without using custom paragraph/character styles, please edit CreateElement.php, and go to generateRFONTS method, instead of:

protected function generateRFONTS($font)
{
  $xml = '<' . CreateElement::NAMESPACEWORD .
    ':rFonts ' . CreateElement::NAMESPACEWORD .
    ':ascii="' . $font . '" ' . CreateElement::NAMESPACEWORD .
    ':hAnsi="' . $font . '" ' . CreateElement::NAMESPACEWORD .
    ':cs="' . $font . '"></' . CreateElement::NAMESPACEWORD .
    ':rFonts>__GENERATERPR__';

  $this->_xml = str_replace('__GENERATERPR__', $xml, $this->_xml);
}

replace it by:

protected function generateRFONTS($font)
{
  $xml = '<' . CreateElement::NAMESPACEWORD .
    ':rFonts ' . CreateElement::NAMESPACEWORD .
    ':ascii="' . $font . '" ' . CreateElement::NAMESPACEWORD .
    ':hAnsi="' . $font . '" ' . CreateElement::NAMESPACEWORD .
    ':eastAsia="' . $font . '" ' . CreateElement::NAMESPACEWORD .
    ':cs="' . $font . '"></' . CreateElement::NAMESPACEWORD .
    ':rFonts>__GENERATERPR__';

  $this->_xml = str_replace('__GENERATERPR__', $xml, $this->_xml);
}

The same change has been included in the current development branch and will be added to the next release of phpdocx.

Regards.