Skip to content

Commit e96295d

Browse files
committed
improvement in Default Font handling in Reader ODText & Word2017
1 parent cdf3d31 commit e96295d

File tree

8 files changed

+166
-2
lines changed

8 files changed

+166
-2
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
## Enhancements
66

7-
- Default Font: Allow specify Asisn font and Latin font separately
7+
- Default Font: Allow specify Asian font and Latin font separately
8+
-- in the datastructures in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714)
9+
-- Writer Word2007 in [#2714](https://github.com/PHPOffice/PHPWord/pull/2714)
10+
-- Reader Word2007 in [#](https://github.com/PHPOffice/PHPWord/pull/)
11+
-- Writer ODText in [#](https://github.com/PHPOffice/PHPWord/pull/)
12+
-- Reader ODText in [#](https://github.com/PHPOffice/PHPWord/pull/)
813

914
- 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)
1015
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
@@ -18,6 +23,7 @@
1823
- Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737)
1924
- Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724)
2025
- Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555)
26+
- Reader ODText: addded (limited) support for reading default font styles by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#(https://github.com/PHPOffice/PHPWord/pull/)
2127

2228
### Bug fixes
2329

src/PhpWord/Reader/ODText.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function load($docFile)
4343
$readerParts = [
4444
'content.xml' => 'Content',
4545
'meta.xml' => 'Meta',
46+
'styles.xml' => 'Styles',
4647
];
4748

4849
foreach ($readerParts as $xmlFile => $partName) {

src/PhpWord/Reader/ODText/Styles.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Reader\ODText;
20+
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Shared\XMLReader;
23+
use PhpOffice\PhpWord\Style\Language;
24+
25+
/**
26+
* Styles reader.
27+
*
28+
* @since 1.4.0
29+
*/
30+
class Styles extends AbstractPart
31+
{
32+
/**
33+
* Read styles.xml.
34+
*/
35+
public function read(PhpWord $phpWord): void
36+
{
37+
$xmlReader = new XMLReader();
38+
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
39+
$fontDefaults = $xmlReader->getElement('office:styles/style:default-style/style:text-properties');
40+
41+
if ($fontDefaults !== null) {
42+
$phpWord->setDefaultFontName($fontDefaults->getAttribute('style:font-name'));
43+
$phpWord->setDefaultAsianFontName($fontDefaults->getAttribute('style:font-name-asian'));
44+
$phpWord->setDefaultFontSize((float) (str_replace('pt', '', $fontDefaults->getAttribute('fo:font-size'))));
45+
$phpWord->setDefaultFontColor(str_replace('#', '', $fontDefaults->getAttribute('fo:color')));
46+
$phpWord->getSettings()->setThemeFontLang(new Language($fontDefaults->getAttribute('fo:language')));
47+
}
48+
}
49+
}

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ protected function readFontStyle(XMLReader $xmlReader, DOMElement $domNode)
756756
$styleDefs = [
757757
'styleName' => [self::READ_VALUE, 'w:rStyle'],
758758
'name' => [self::READ_VALUE, 'w:rFonts', ['w:ascii', 'w:hAnsi', 'w:eastAsia', 'w:cs']],
759+
'nameEastAsia' => [self::READ_VALUE, 'w:rFonts', ['w:eastAsia', 'w:ascii', 'w:hAnsi', 'w:cs']],
759760
'hint' => [self::READ_VALUE, 'w:rFonts', 'w:hint'],
760761
'size' => [self::READ_SIZE, ['w:sz', 'w:szCs']],
761762
'color' => [self::READ_VALUE, 'w:color'],

src/PhpWord/Reader/Word2007/Styles.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function read(PhpWord $phpWord): void
4444
if (array_key_exists('name', $fontDefaultStyle)) {
4545
$phpWord->setDefaultFontName($fontDefaultStyle['name']);
4646
}
47+
if (array_key_exists('nameEastAsia', $fontDefaultStyle)) {
48+
$phpWord->setDefaultAsianFontName($fontDefaultStyle['nameEastAsia']);
49+
}
4750
if (array_key_exists('size', $fontDefaultStyle)) {
4851
$phpWord->setDefaultFontSize($fontDefaultStyle['size']);
4952
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function writeDefault(XMLWriter $xmlWriter): void
9999
$xmlWriter->writeAttribute('fo:country', $latinLang[1]);
100100
$xmlWriter->writeAttribute('fo:color', '#' . Settings::getDefaultFontColor());
101101
$xmlWriter->writeAttribute('style:letter-kerning', 'true');
102-
$xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2');
102+
$xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultAsianFontName());
103103
$xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt');
104104
$xmlWriter->writeAttribute('style:language-asian', $asianLang[0]);
105105
$xmlWriter->writeAttribute('style:country-asian', $asianLang[1]);

tests/PhpWordTests/WriteReadback/ODTextTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,102 @@
3232
*/
3333
class ODTextTest extends \PHPUnit\Framework\TestCase
3434
{
35+
/**
36+
* Test default font name.
37+
*/
38+
public function testDefaultFontName(): void
39+
{
40+
$phpWordWriter = new PhpWord();
41+
$testDefaultFontName = 'Times New Roman';
42+
$phpWordWriter->setDefaultFontName($testDefaultFontName);
43+
44+
$writer = new ODText($phpWordWriter);
45+
$file = __DIR__ . '/../_files/temp.odt';
46+
$writer->save($file);
47+
48+
self::assertFileExists($file);
49+
50+
$phpWordWriter->setDefaultFontName('This text should not be set after reading back the file');
51+
52+
$phpWordReader = IOFactory::load($file, 'ODText');
53+
54+
self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultFontName());
55+
56+
unlink($file);
57+
}
58+
59+
/**
60+
* Test default Asian font name.
61+
*/
62+
public function testDefaultAsianFontName(): void
63+
{
64+
$phpWordWriter = new PhpWord();
65+
$testDefaultFontName = '標楷體';
66+
$phpWordWriter->setDefaultAsianFontName($testDefaultFontName);
67+
68+
$writer = new ODText($phpWordWriter);
69+
$file = __DIR__ . '/../_files/temp.odt';
70+
$writer->save($file);
71+
72+
self::assertFileExists($file);
73+
74+
$phpWordWriter->setDefaultAsianFontName('This text should not be set after reading back the file');
75+
76+
$phpWordReader = IOFactory::load($file, 'ODText');
77+
78+
self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultAsianFontName());
79+
80+
unlink($file);
81+
}
82+
83+
/**
84+
* Test default font size.
85+
*/
86+
public function testDefaulFontSize(): void
87+
{
88+
$phpWordWriter = new PhpWord();
89+
$testDefaultFontSize = 144;
90+
$phpWordWriter->setDefaultFontSize($testDefaultFontSize);
91+
92+
$writer = new ODText($phpWordWriter);
93+
$file = __DIR__ . '/../_files/temp.odt';
94+
$writer->save($file);
95+
96+
self::assertFileExists($file);
97+
98+
$phpWordWriter->setDefaultFontSize(987); //This value should not be set after reading back the file
99+
100+
$phpWordReader = IOFactory::load($file, 'ODText');
101+
102+
self::assertEquals($testDefaultFontSize, $phpWordReader->getDefaultFontSize());
103+
104+
unlink($file);
105+
}
106+
107+
/**
108+
* Test default font color.
109+
*/
110+
public function testDefaultFontColor(): void
111+
{
112+
$phpWordWriter = new PhpWord();
113+
$testDefaultFontColor = '00FF00';
114+
$phpWordWriter->setDefaultFontColor($testDefaultFontColor);
115+
116+
$writer = new ODText($phpWordWriter);
117+
$file = __DIR__ . '/../_files/temp.odt';
118+
$writer->save($file);
119+
120+
self::assertFileExists($file);
121+
122+
$phpWordWriter->setDefaultFontColor('C0FFEE'); //This value should not be set after reading back the file
123+
124+
$phpWordReader = IOFactory::load($file, 'ODText');
125+
126+
self::assertEquals($testDefaultFontColor, $phpWordReader->getDefaultFontColor());
127+
128+
unlink($file);
129+
}
130+
35131
/**
36132
* Test a document with one section and text.
37133
*/

tests/PhpWordTests/WriteReadback/Word2007Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function testDefaultFontName(): void
4848

4949
self::assertFileExists($file);
5050

51+
$phpWordWriter->setDefaultFontName('This text should not be set after reading back the file');
52+
5153
$phpWordReader = IOFactory::load($file, 'Word2007');
5254

5355
self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultFontName());
@@ -70,6 +72,8 @@ public function testDefaultAsianFontName(): void
7072

7173
self::assertFileExists($file);
7274

75+
$phpWordWriter->setDefaultAsianFontName('This text should not be set after reading back the file');
76+
7377
$phpWordReader = IOFactory::load($file, 'Word2007');
7478

7579
self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultAsianFontName());
@@ -114,6 +118,8 @@ public function testDefaultFontColor(): void
114118

115119
self::assertFileExists($file);
116120

121+
$phpWordWriter->setDefaultFontColor('C0FFEE'); //This value should not be set after reading back the file
122+
117123
$phpWordReader = IOFactory::load($file, 'Word2007');
118124

119125
self::assertEquals($testDefaultFontColor, $phpWordReader->getDefaultFontColor());
@@ -136,6 +142,8 @@ public function testZoom(): void
136142

137143
self::assertFileExists($file);
138144

145+
$phpWordWriter->getSettings()->setZoom(98); //This value should not be set after reading back the file
146+
139147
$phpWordReader = IOFactory::load($file, 'Word2007');
140148

141149
self::assertEquals($zoomLevel, $phpWordReader->getSettings()->getZoom());

0 commit comments

Comments
 (0)