Skip to content

Commit 2fac9ee

Browse files
authored
Stacked Alignment - Use Class Constant Rather than Literal (#1716)
* Stacked Alignment - Use Class Constant Rather than Literal PR #1580 defined constants for "stacked" alignment in cells. Using those constants outside of Style/Alignment was beyond the scope of the original PR, but I said I would get to it. This PR replaces all uses of literal -165, and appropriate uses of literal 255, with the named constants, and adds tests to make sure that the changed code is covered in the test suite.
1 parent 30717fd commit 2fac9ee

File tree

8 files changed

+95
-52
lines changed

8 files changed

+95
-52
lines changed

samples/Basic/30_Templatebiff5.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Shared\Date;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$helper->log('Load from Xls template');
9+
$reader = IOFactory::createReader('Xls');
10+
$spreadsheet = $reader->load(__DIR__ . '/../templates/30templatebiff5.xls');
11+
12+
$helper->log('Add new data to the template');
13+
$data = [['title' => 'Excel for dummies',
14+
'price' => 17.99,
15+
'quantity' => 2,
16+
],
17+
['title' => 'PHP for dummies',
18+
'price' => 15.99,
19+
'quantity' => 1,
20+
],
21+
['title' => 'Inside OOP',
22+
'price' => 12.95,
23+
'quantity' => 1,
24+
],
25+
];
26+
27+
$spreadsheet->getActiveSheet()->setCellValue('D1', Date::PHPToExcel(time()));
28+
29+
$baseRow = 5;
30+
foreach ($data as $r => $dataRow) {
31+
$row = $baseRow + $r;
32+
$spreadsheet->getActiveSheet()->insertNewRowBefore($row, 1);
33+
34+
$spreadsheet->getActiveSheet()->setCellValue('A' . $row, $r + 1)
35+
->setCellValue('B' . $row, $dataRow['title'])
36+
->setCellValue('C' . $row, $dataRow['price'])
37+
->setCellValue('D' . $row, $dataRow['quantity'])
38+
->setCellValue('E' . $row, '=C' . $row . '*D' . $row);
39+
}
40+
$spreadsheet->getActiveSheet()->removeRow($baseRow - 1, 1);
41+
42+
// Save
43+
$helper->write($spreadsheet, __FILE__);

samples/templates/30template.xls

11.5 KB
Binary file not shown.
331 KB
Binary file not shown.

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,8 +2286,8 @@ private function readXf(): void
22862286
$rotation = $angle;
22872287
} elseif ($angle <= 180) {
22882288
$rotation = 90 - $angle;
2289-
} elseif ($angle == 255) {
2290-
$rotation = -165;
2289+
} elseif ($angle == Alignment::TEXTROTATION_STACK_EXCEL) {
2290+
$rotation = Alignment::TEXTROTATION_STACK_PHPSPREADSHEET;
22912291
}
22922292
$objStyle->getAlignment()->setTextRotation($rotation);
22932293

@@ -2389,7 +2389,7 @@ private function readXf(): void
23892389

23902390
break;
23912391
case 1:
2392-
$objStyle->getAlignment()->setTextRotation(-165);
2392+
$objStyle->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET);
23932393

23942394
break;
23952395
case 2:

src/PhpSpreadsheet/Shared/Font.php

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

55
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
66
use PhpOffice\PhpSpreadsheet\RichText\RichText;
7+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
78

89
class Font
910
{
@@ -342,7 +343,7 @@ public static function getTextWidthPixelsApprox($columnText, \PhpOffice\PhpSprea
342343

343344
// Calculate approximate rotated column width
344345
if ($rotation !== 0) {
345-
if ($rotation == -165) {
346+
if ($rotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
346347
// stacked text
347348
$columnWidth = 4; // approximation
348349
} else {

src/PhpSpreadsheet/Writer/Xls/Xf.php

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public function writeXf()
165165
self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
166166
self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
167167
self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle())) ? 1 : 0;
168-
$atr_pat = (($this->foregroundColor != 0x40) ||
169-
($this->backgroundColor != 0x41) ||
170-
self::mapFillType($this->_style->getFill()->getFillType())) ? 1 : 0;
168+
$atr_pat = ($this->foregroundColor != 0x40) ? 1 : 0;
169+
$atr_pat = ($this->backgroundColor != 0x41) ? 1 : $atr_pat;
170+
$atr_pat = self::mapFillType($this->_style->getFill()->getFillType()) ? 1 : $atr_pat;
171171
$atr_prot = self::mapLocked($this->_style->getProtection()->getLocked())
172172
| self::mapHidden($this->_style->getProtection()->getHidden());
173173

@@ -375,11 +375,7 @@ public function setFontIndex($value): void
375375
*/
376376
private static function mapBorderStyle($borderStyle)
377377
{
378-
if (isset(self::$mapBorderStyles[$borderStyle])) {
379-
return self::$mapBorderStyles[$borderStyle];
380-
}
381-
382-
return 0x00;
378+
return self::$mapBorderStyles[$borderStyle] ?? 0;
383379
}
384380

385381
/**
@@ -420,11 +416,7 @@ private static function mapBorderStyle($borderStyle)
420416
*/
421417
private static function mapFillType($fillType)
422418
{
423-
if (isset(self::$mapFillTypes[$fillType])) {
424-
return self::$mapFillTypes[$fillType];
425-
}
426-
427-
return 0x00;
419+
return self::$mapFillTypes[$fillType] ?? 0;
428420
}
429421

430422
/**
@@ -451,11 +443,7 @@ private static function mapFillType($fillType)
451443
*/
452444
private function mapHAlign($hAlign)
453445
{
454-
if (isset(self::$mapHAlignments[$hAlign])) {
455-
return self::$mapHAlignments[$hAlign];
456-
}
457-
458-
return 0;
446+
return self::$mapHAlignments[$hAlign] ?? 0;
459447
}
460448

461449
/**
@@ -479,11 +467,7 @@ private function mapHAlign($hAlign)
479467
*/
480468
private static function mapVAlign($vAlign)
481469
{
482-
if (isset(self::$mapVAlignments[$vAlign])) {
483-
return self::$mapVAlignments[$vAlign];
484-
}
485-
486-
return 2;
470+
return self::$mapVAlignments[$vAlign] ?? 2;
487471
}
488472

489473
/**
@@ -497,34 +481,38 @@ private static function mapTextRotation($textRotation)
497481
{
498482
if ($textRotation >= 0) {
499483
return $textRotation;
500-
} elseif ($textRotation == -165) {
501-
return 255;
502-
} elseif ($textRotation < 0) {
503-
return 90 - $textRotation;
504484
}
485+
if ($textRotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
486+
return Alignment::TEXTROTATION_STACK_EXCEL;
487+
}
488+
489+
return 90 - $textRotation;
505490
}
506491

492+
private const LOCK_ARRAY = [
493+
Protection::PROTECTION_INHERIT => 1,
494+
Protection::PROTECTION_PROTECTED => 1,
495+
Protection::PROTECTION_UNPROTECTED => 0,
496+
];
497+
507498
/**
508-
* Map locked.
499+
* Map locked values.
509500
*
510501
* @param string $locked
511502
*
512503
* @return int
513504
*/
514505
private static function mapLocked($locked)
515506
{
516-
switch ($locked) {
517-
case Protection::PROTECTION_INHERIT:
518-
return 1;
519-
case Protection::PROTECTION_PROTECTED:
520-
return 1;
521-
case Protection::PROTECTION_UNPROTECTED:
522-
return 0;
523-
default:
524-
return 1;
525-
}
507+
return array_key_exists($locked, self::LOCK_ARRAY) ? self::LOCK_ARRAY[$locked] : 1;
526508
}
527509

510+
private const HIDDEN_ARRAY = [
511+
Protection::PROTECTION_INHERIT => 0,
512+
Protection::PROTECTION_PROTECTED => 1,
513+
Protection::PROTECTION_UNPROTECTED => 0,
514+
];
515+
528516
/**
529517
* Map hidden.
530518
*
@@ -534,15 +522,6 @@ private static function mapLocked($locked)
534522
*/
535523
private static function mapHidden($hidden)
536524
{
537-
switch ($hidden) {
538-
case Protection::PROTECTION_INHERIT:
539-
return 0;
540-
case Protection::PROTECTION_PROTECTED:
541-
return 1;
542-
case Protection::PROTECTION_UNPROTECTED:
543-
return 0;
544-
default:
545-
return 0;
546-
}
525+
return array_key_exists($hidden, self::HIDDEN_ARRAY) ? self::HIDDEN_ARRAY[$hidden] : 0;
547526
}
548527
}

tests/PhpSpreadsheetTests/Functional/ActiveSheetTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Functional;
44

55
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
use PhpOffice\PhpSpreadsheet\Style\Protection;
67

78
class ActiveSheetTest extends AbstractFunctional
89
{
@@ -46,6 +47,12 @@ public function testActiveSheet($format): void
4647

4748
$spreadsheet->createSheet(2);
4849

50+
$spreadsheet->setActiveSheetIndex(2)
51+
->setCellValue('F1', 2)
52+
->getCell('F1')
53+
->getStyle()
54+
->getProtection()
55+
->setHidden(Protection::PROTECTION_PROTECTED);
4956
$spreadsheet->setActiveSheetIndex(2)
5057
->setTitle('Test3')
5158
->setCellValue('A1', 4)

tests/PhpSpreadsheetTests/Shared/FontTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Shared;
44

55
use PhpOffice\PhpSpreadsheet\Shared\Font;
6+
use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont;
67
use PHPUnit\Framework\TestCase;
78

89
class FontTest extends TestCase
@@ -83,4 +84,16 @@ public function providerCentimeterSizeToPixels()
8384
{
8485
return require 'tests/data/Shared/CentimeterSizeToPixels.php';
8586
}
87+
88+
public function testVerdanaRotation(): void
89+
{
90+
$font = new StyleFont();
91+
$font->setName('Verdana')->setSize(10);
92+
$width = Font::getTextWidthPixelsApprox('n', $font, 0);
93+
self::assertEquals(8, $width);
94+
$width = Font::getTextWidthPixelsApprox('n', $font, 45);
95+
self::assertEquals(7, $width);
96+
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
97+
self::assertEquals(4, $width);
98+
}
8699
}

0 commit comments

Comments
 (0)