From d4094c0b58643d96972c5c770153fa00a2e066c5 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 5 Jul 2020 20:43:59 +0200 Subject: [PATCH] Minor refactoring --- src/PhpSpreadsheet/Reader/Gnumeric.php | 122 +-------------- .../Reader/Gnumeric/PageSetup.php | 143 ++++++++++++++++++ 2 files changed, 149 insertions(+), 116 deletions(-) create mode 100644 src/PhpSpreadsheet/Reader/Gnumeric/PageSetup.php diff --git a/src/PhpSpreadsheet/Reader/Gnumeric.php b/src/PhpSpreadsheet/Reader/Gnumeric.php index cbc22beb..a29a2b9d 100644 --- a/src/PhpSpreadsheet/Reader/Gnumeric.php +++ b/src/PhpSpreadsheet/Reader/Gnumeric.php @@ -5,6 +5,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\NamedRange; +use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\PageSetup; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\ReferenceHelper; use PhpOffice\PhpSpreadsheet\RichText\RichText; @@ -17,8 +18,6 @@ use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Borders; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; -use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; -use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use SimpleXMLElement; use XMLReader; @@ -405,118 +404,6 @@ class Gnumeric extends BaseReader } } - private function printInformation(SimpleXMLElement $sheet): void - { - if (!$this->readDataOnly && isset($sheet->PrintInformation)) { - $printInformation = $sheet->PrintInformation[0]; - $scale = (string) $printInformation->Scale->attributes()['percentage']; - $pageOrder = (string) $printInformation->order; - $orientation = (string) $printInformation->orientation; - $horizontalCentered = (string) $printInformation->hcenter->attributes()['value']; - $verticalCentered = (string) $printInformation->vcenter->attributes()['value']; - - $this->spreadsheet->getActiveSheet()->getPageSetup() - ->setPageOrder($pageOrder === 'r_then_d' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER) - ->setScale((int) $scale) - ->setOrientation($orientation ?? PageSetup::ORIENTATION_DEFAULT) - ->setHorizontalCentered((bool) $horizontalCentered) - ->setVerticalCentered((bool) $verticalCentered); - } - } - - private function sheetMargins(SimpleXMLElement $sheet): void - { - if (!$this->readDataOnly && isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { - $marginSet = [ - // Default Settings - 'top' => 0.75, - 'header' => 0.3, - 'left' => 0.7, - 'right' => 0.7, - 'bottom' => 0.75, - 'footer' => 0.3, - ]; - - $marginSet = $this->buildMarginSet($sheet, $marginSet); - $this->adjustMargins($marginSet); - } - } - - private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array - { - foreach ($sheet->PrintInformation->Margins->children($this->gnm, true) as $key => $margin) { - $marginAttributes = $margin->attributes(); - $marginSize = ($marginAttributes['Points']) ?? 72; // Default is 72pt - // Convert value in points to inches - $marginSize = PageMargins::fromPoints((float)$marginSize); - $marginSet[$key] = $marginSize; - } - - return $marginSet; - } - - private function adjustMargins(array $marginSet): void - { - foreach ($marginSet as $key => $marginSize) { - // Gnumeric is quirky in the way it displays the header/footer values: - // header is actually the sum of top and header; footer is actually the sum of bottom and footer - // then top is actually the header value, and bottom is actually the footer value - switch ($key) { - case 'left': - case 'right': - $this->sheetMargin($key, $marginSize); - - break; - case 'top': - $this->sheetMargin($key, $marginSet['header'] ?? 0); - - break; - case 'bottom': - $this->sheetMargin($key, $marginSet['footer'] ?? 0); - - break; - case 'header': - $this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize); - - break; - case 'footer': - $this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize); - - break; - } - } - } - - private function sheetMargin(string $key, float $marginSize): void - { - switch ($key) { - case 'top': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize); - - break; - case 'bottom': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize); - - break; - case 'left': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize); - - break; - case 'right': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize); - - break; - case 'header': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize); - - break; - case 'footer': - $this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize); - - break; - } - } - private function processComments(SimpleXMLElement $sheet): void { if ((!$this->readDataOnly) && (isset($sheet->Objects))) { @@ -582,8 +469,11 @@ class Gnumeric extends BaseReader // name in line with the formula, not the reverse $this->spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false); - $this->printInformation($sheet); - $this->sheetMargins($sheet); + if (!$this->readDataOnly) { + (new PageSetup($this->spreadsheet, $this->gnm)) + ->printInformation($sheet) + ->sheetMargins($sheet); + } foreach ($sheet->Cells->Cell as $cell) { $cellAttributes = $cell->attributes(); diff --git a/src/PhpSpreadsheet/Reader/Gnumeric/PageSetup.php b/src/PhpSpreadsheet/Reader/Gnumeric/PageSetup.php new file mode 100644 index 00000000..dbe30061 --- /dev/null +++ b/src/PhpSpreadsheet/Reader/Gnumeric/PageSetup.php @@ -0,0 +1,143 @@ +spreadsheet = $spreadsheet; + $this->gnm = $gnm; + } + + public function printInformation(SimpleXMLElement $sheet): self + { + if (isset($sheet->PrintInformation)) { + $printInformation = $sheet->PrintInformation[0]; + $scale = (string) $printInformation->Scale->attributes()['percentage']; + $pageOrder = (string) $printInformation->order; + $orientation = (string) $printInformation->orientation; + $horizontalCentered = (string) $printInformation->hcenter->attributes()['value']; + $verticalCentered = (string) $printInformation->vcenter->attributes()['value']; + + $this->spreadsheet->getActiveSheet()->getPageSetup() + ->setPageOrder($pageOrder === 'r_then_d' ? WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN : WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER) + ->setScale((int) $scale) + ->setOrientation($orientation ?? WorksheetPageSetup::ORIENTATION_DEFAULT) + ->setHorizontalCentered((bool) $horizontalCentered) + ->setVerticalCentered((bool) $verticalCentered); + } + + return $this; + } + + public function sheetMargins(SimpleXMLElement $sheet): self + { + if (isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { + $marginSet = [ + // Default Settings + 'top' => 0.75, + 'header' => 0.3, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.75, + 'footer' => 0.3, + ]; + + $marginSet = $this->buildMarginSet($sheet, $marginSet); + $this->adjustMargins($marginSet); + } + + return $this; + } + + private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array + { + foreach ($sheet->PrintInformation->Margins->children($this->gnm, true) as $key => $margin) { + $marginAttributes = $margin->attributes(); + $marginSize = ($marginAttributes['Points']) ?? 72; // Default is 72pt + // Convert value in points to inches + $marginSize = PageMargins::fromPoints((float) $marginSize); + $marginSet[$key] = $marginSize; + } + + return $marginSet; + } + + private function adjustMargins(array $marginSet): void + { + foreach ($marginSet as $key => $marginSize) { + // Gnumeric is quirky in the way it displays the header/footer values: + // header is actually the sum of top and header; footer is actually the sum of bottom and footer + // then top is actually the header value, and bottom is actually the footer value + switch ($key) { + case 'left': + case 'right': + $this->sheetMargin($key, $marginSize); + + break; + case 'top': + $this->sheetMargin($key, $marginSet['header'] ?? 0); + + break; + case 'bottom': + $this->sheetMargin($key, $marginSet['footer'] ?? 0); + + break; + case 'header': + $this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize); + + break; + case 'footer': + $this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize); + + break; + } + } + } + + private function sheetMargin(string $key, float $marginSize): void + { + switch ($key) { + case 'top': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize); + + break; + case 'bottom': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize); + + break; + case 'left': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize); + + break; + case 'right': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize); + + break; + case 'header': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize); + + break; + case 'footer': + $this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize); + + break; + } + } +}