Skip to content

Commit f0ca95f

Browse files
committed
Tests and Documentation
1 parent cbf194c commit f0ca95f

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

docs/topics/recipes.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,21 @@ All cells bound to the theme fonts (via the `Font::setScheme` method) can be eas
10991099
$spreadsheet->resetThemeFonts();
11001100
```
11011101

1102+
### Charset for Arabic and Persian Fonts
1103+
1104+
It is unknown why this should be needed. However, some Excel
1105+
users have reported better results if the internal declaration for an
1106+
Arabic/Persian font includes a `charset` declaration.
1107+
This seems like a bug in Excel, but, starting with release 4.4,
1108+
this can be accomplished at the spreadsheet level, via:
1109+
```php
1110+
$spreadsheet->addFontCharset('C Nazanin');
1111+
```
1112+
As many charsets as desired can be added in this manner.
1113+
There is a second optional parameter specifying the charset id
1114+
to this method, but, since this seems to be needed only for
1115+
Arabic/Persian, that is its default value.
1116+
11021117
### Styling cell borders
11031118

11041119
In PhpSpreadsheet it is easy to apply various borders on a rectangular

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ class Spreadsheet implements JsonSerializable
183183
'B Nazanin' => SharedFont::CHARSET_ANSI_ARABIC,
184184
];
185185

186-
public function addFontCharset(string $fontName, int $charset): void
186+
/**
187+
* @param int $charset uses any value from Shared\Font,
188+
* but defaults to ARABIC because that is the only known
189+
* charset for which this declaration might be needed
190+
*/
191+
public function addFontCharset(string $fontName, int $charset = SharedFont::CHARSET_ANSI_ARABIC): void
187192
{
188193
$this->fontCharsets[$fontName] = $charset;
189194
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
9+
10+
class FontCharsetTest extends AbstractFunctional
11+
{
12+
private ?Spreadsheet $spreadsheet = null;
13+
14+
private ?Spreadsheet $reloadedSpreadsheet = null;
15+
16+
protected function tearDown(): void
17+
{
18+
if ($this->spreadsheet !== null) {
19+
$this->spreadsheet->disconnectWorksheets();
20+
$this->spreadsheet = null;
21+
}
22+
if ($this->reloadedSpreadsheet !== null) {
23+
$this->reloadedSpreadsheet->disconnectWorksheets();
24+
$this->reloadedSpreadsheet = null;
25+
}
26+
}
27+
28+
public function testFontCharset(): void
29+
{
30+
$spreadsheet = $this->spreadsheet = new Spreadsheet();
31+
$sheet = $this->spreadsheet->getActiveSheet();
32+
$sheet->getStyle('A1')->getFont()->setName('Nazanin');
33+
$spreadsheet->addFontCharset('Nazanin');
34+
35+
$spreadsheet2 = $this->reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
36+
$sheet2 = $spreadsheet2->getActiveSheet();
37+
$sheet2->getStyle('B1')->getFont()->setName('B Nazanin');
38+
$sheet2->getStyle('C1')->getFont()->setName('C Nazanin');
39+
$sheet2->getStyle('D1')->getFont()->setName('D Nazanin');
40+
$spreadsheet2->addFontCharset('C Nazanin');
41+
// Do not add D Nazanin for this test.
42+
self::assertSame(
43+
[
44+
'B Nazanin' => 178, // default entry
45+
'Nazanin' => 178, // should have been set by Xlsx Reader
46+
'C Nazanin' => 178, // explicitly set in this test
47+
],
48+
$spreadsheet2->getFontCharsets()
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)