forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFontTest.php
More file actions
137 lines (119 loc) · 4.58 KB
/
FontTest.php
File metadata and controls
137 lines (119 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace PhpOffice\PhpSpreadsheetTests\Shared;
use PhpOffice\PhpSpreadsheet\Shared\Font;
use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont;
use PHPUnit\Framework\TestCase;
class FontTest extends TestCase
{
const FONT_PRECISION = 1.0E-12;
public function testGetAutoSizeMethod(): void
{
$expectedResult = Font::AUTOSIZE_METHOD_APPROX;
$result = Font::getAutoSizeMethod();
self::assertEquals($expectedResult, $result);
}
public function testSetAutoSizeMethod(): void
{
$autosizeMethodValues = [
Font::AUTOSIZE_METHOD_EXACT,
Font::AUTOSIZE_METHOD_APPROX,
];
foreach ($autosizeMethodValues as $autosizeMethodValue) {
$result = Font::setAutoSizeMethod($autosizeMethodValue);
self::assertTrue($result);
}
}
public function testSetAutoSizeMethodWithInvalidValue(): void
{
$unsupportedAutosizeMethod = 'guess';
$result = Font::setAutoSizeMethod($unsupportedAutosizeMethod);
self::assertFalse($result);
}
/**
* @dataProvider providerFontSizeToPixels
*
* @param mixed $expectedResult
* @param mixed $size
*/
public function testFontSizeToPixels($expectedResult, $size): void
{
$result = Font::fontSizeToPixels($size);
self::assertEquals($expectedResult, $result);
}
public function providerFontSizeToPixels(): array
{
return require 'tests/data/Shared/FontSizeToPixels.php';
}
/**
* @dataProvider providerInchSizeToPixels
*
* @param mixed $expectedResult
* @param mixed $size
*/
public function testInchSizeToPixels($expectedResult, $size): void
{
$result = Font::inchSizeToPixels($size);
self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
}
public function providerInchSizeToPixels(): array
{
return require 'tests/data/Shared/InchSizeToPixels.php';
}
/**
* @dataProvider providerCentimeterSizeToPixels
*
* @param mixed $expectedResult
* @param mixed $size
*/
public function testCentimeterSizeToPixels($expectedResult, $size): void
{
$result = Font::centimeterSizeToPixels($size);
self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
}
public function providerCentimeterSizeToPixels(): array
{
return require 'tests/data/Shared/CentimeterSizeToPixels.php';
}
public function testVerdanaRotation(): void
{
$font = new StyleFont();
$font->setName('Verdana')->setSize(10);
$width = Font::getTextWidthPixelsApprox('n', $font, 0);
self::assertEquals(8, $width);
$width = Font::getTextWidthPixelsApprox('n', $font, 45);
self::assertEquals(7, $width);
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
self::assertEquals(4, $width);
}
/**
* @dataProvider providerCalculateApproximateColumnWidth
*/
public function testCalculateApproximateColumnWidth(
float $expectedWidth,
StyleFont $font,
string $text,
int $rotation,
StyleFont $defaultFont,
bool $filter,
int $indent
): void {
$columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent);
self::assertEquals($expectedWidth, $columnWidth);
}
public function providerCalculateApproximateColumnWidth(): array
{
return [
[13.9966, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
[16.2817, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
[16.2817, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
[18.7097, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
[20.9949, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
[6.9983, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
[9.2834, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
[17.5671, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
[19.8523, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
'CJK characters width must be >= 43.00' => [55.2722, new StyleFont(), '如果某一列是CJK 其中的一种,这样的设置方式无效', 0, new StyleFont(), false, 0],
'non-CJK characters width must be >= 24.73' => [31.7065, new StyleFont(), 'abcdefghijklmnopqrstuvwxyz', 0, new StyleFont(), false, 0],
];
}
}