FontTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Shared;
  3. use PhpOffice\PhpSpreadsheet\Shared\Font;
  4. use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont;
  5. use PHPUnit\Framework\TestCase;
  6. class FontTest extends TestCase
  7. {
  8. const FONT_PRECISION = 1.0E-12;
  9. public function testGetAutoSizeMethod(): void
  10. {
  11. $expectedResult = Font::AUTOSIZE_METHOD_APPROX;
  12. $result = Font::getAutoSizeMethod();
  13. self::assertEquals($expectedResult, $result);
  14. }
  15. public function testSetAutoSizeMethod(): void
  16. {
  17. $autosizeMethodValues = [
  18. Font::AUTOSIZE_METHOD_EXACT,
  19. Font::AUTOSIZE_METHOD_APPROX,
  20. ];
  21. foreach ($autosizeMethodValues as $autosizeMethodValue) {
  22. $result = Font::setAutoSizeMethod($autosizeMethodValue);
  23. self::assertTrue($result);
  24. }
  25. }
  26. public function testSetAutoSizeMethodWithInvalidValue(): void
  27. {
  28. $unsupportedAutosizeMethod = 'guess';
  29. $result = Font::setAutoSizeMethod($unsupportedAutosizeMethod);
  30. self::assertFalse($result);
  31. }
  32. /**
  33. * @dataProvider providerFontSizeToPixels
  34. *
  35. * @param mixed $expectedResult
  36. * @param mixed $size
  37. */
  38. public function testFontSizeToPixels($expectedResult, $size): void
  39. {
  40. $result = Font::fontSizeToPixels($size);
  41. self::assertEquals($expectedResult, $result);
  42. }
  43. public function providerFontSizeToPixels(): array
  44. {
  45. return require 'tests/data/Shared/FontSizeToPixels.php';
  46. }
  47. /**
  48. * @dataProvider providerInchSizeToPixels
  49. *
  50. * @param mixed $expectedResult
  51. * @param mixed $size
  52. */
  53. public function testInchSizeToPixels($expectedResult, $size): void
  54. {
  55. $result = Font::inchSizeToPixels($size);
  56. self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
  57. }
  58. public function providerInchSizeToPixels(): array
  59. {
  60. return require 'tests/data/Shared/InchSizeToPixels.php';
  61. }
  62. /**
  63. * @dataProvider providerCentimeterSizeToPixels
  64. *
  65. * @param mixed $expectedResult
  66. * @param mixed $size
  67. */
  68. public function testCentimeterSizeToPixels($expectedResult, $size): void
  69. {
  70. $result = Font::centimeterSizeToPixels($size);
  71. self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
  72. }
  73. public function providerCentimeterSizeToPixels(): array
  74. {
  75. return require 'tests/data/Shared/CentimeterSizeToPixels.php';
  76. }
  77. public function testVerdanaRotation(): void
  78. {
  79. $font = new StyleFont();
  80. $font->setName('Verdana')->setSize(10);
  81. $width = Font::getTextWidthPixelsApprox('n', $font, 0);
  82. self::assertEquals(8, $width);
  83. $width = Font::getTextWidthPixelsApprox('n', $font, 45);
  84. self::assertEquals(7, $width);
  85. $width = Font::getTextWidthPixelsApprox('n', $font, -165);
  86. self::assertEquals(4, $width);
  87. }
  88. /**
  89. * @dataProvider providerCalculateApproximateColumnWidth
  90. */
  91. public function testCalculateApproximateColumnWidth(
  92. int $expectedWidth,
  93. StyleFont $font,
  94. string $text,
  95. int $rotation,
  96. StyleFont $defaultFont,
  97. bool $filter,
  98. int $indent
  99. ): void {
  100. $columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent);
  101. self::assertEquals($expectedWidth, $columnWidth);
  102. }
  103. public function providerCalculateApproximateColumnWidth(): array
  104. {
  105. return [
  106. [13, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
  107. [16, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
  108. [16, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
  109. [18, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
  110. [20, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
  111. [6, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
  112. [9, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
  113. [17, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
  114. [19, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
  115. ];
  116. }
  117. }