| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Gnumeric; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Reader\Gnumeric; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup as WorksheetPageSetup; |
| 9 |
use SimpleXMLElement; |
| 10 |
|
| 11 |
class PageSetup |
| 12 |
{ |
| 13 |
private Spreadsheet $spreadsheet; |
| 14 |
|
| 15 |
public function __construct(Spreadsheet $spreadsheet) |
| 16 |
{ |
| 17 |
$this->spreadsheet = $spreadsheet; |
| 18 |
} |
| 19 |
|
| 20 |
public function printInformation(SimpleXMLElement $sheet): self |
| 21 |
{ |
| 22 |
if (isset($sheet->PrintInformation, $sheet->PrintInformation[0])) { |
| 23 |
$printInformation = $sheet->PrintInformation[0]; |
| 24 |
$setup = $this->spreadsheet->getActiveSheet()->getPageSetup(); |
| 25 |
|
| 26 |
$attributes = $printInformation->Scale->attributes(); |
| 27 |
if (isset($attributes['percentage'])) { |
| 28 |
$setup->setScale((int) $attributes['percentage']); |
| 29 |
} |
| 30 |
$pageOrder = (string) $printInformation->order; |
| 31 |
if ($pageOrder === 'r_then_d') { |
| 32 |
$setup->setPageOrder(WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN); |
| 33 |
} elseif ($pageOrder === 'd_then_r') { |
| 34 |
$setup->setPageOrder(WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER); |
| 35 |
} |
| 36 |
$orientation = (string) $printInformation->orientation; |
| 37 |
if ($orientation !== '') { |
| 38 |
$setup->setOrientation($orientation); |
| 39 |
} |
| 40 |
$attributes = $printInformation->hcenter->attributes(); |
| 41 |
if (isset($attributes['value'])) { |
| 42 |
$setup->setHorizontalCentered((bool) (string) $attributes['value']); |
| 43 |
} |
| 44 |
$attributes = $printInformation->vcenter->attributes(); |
| 45 |
if (isset($attributes['value'])) { |
| 46 |
$setup->setVerticalCentered((bool) (string) $attributes['value']); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function sheetMargins(SimpleXMLElement $sheet): self |
| 54 |
{ |
| 55 |
if (isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { |
| 56 |
$marginSet = [ |
| 57 |
// Default Settings |
| 58 |
'top' => 0.75, |
| 59 |
'header' => 0.3, |
| 60 |
'left' => 0.7, |
| 61 |
'right' => 0.7, |
| 62 |
'bottom' => 0.75, |
| 63 |
'footer' => 0.3, |
| 64 |
]; |
| 65 |
|
| 66 |
$marginSet = $this->buildMarginSet($sheet, $marginSet); |
| 67 |
$this->adjustMargins($marginSet); |
| 68 |
} |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @param float[] $marginSet |
| 75 |
* |
| 76 |
* @return float[] |
| 77 |
*/ |
| 78 |
private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array |
| 79 |
{ |
| 80 |
foreach ($sheet->PrintInformation->Margins->children(Gnumeric::NAMESPACE_GNM) as $key => $margin) { |
| 81 |
$marginAttributes = $margin->attributes(); |
| 82 |
$marginSize = ($marginAttributes['Points']) ?? 72; // Default is 72pt |
| 83 |
// Convert value in points to inches |
| 84 |
$marginSize = PageMargins::fromPoints((float) $marginSize); |
| 85 |
$marginSet[$key] = $marginSize; |
| 86 |
} |
| 87 |
|
| 88 |
return $marginSet; |
| 89 |
} |
| 90 |
|
| 91 |
/** @param float[] $marginSet */ |
| 92 |
private function adjustMargins(array $marginSet): void |
| 93 |
{ |
| 94 |
foreach ($marginSet as $key => $marginSize) { |
| 95 |
// Gnumeric is quirky in the way it displays the header/footer values: |
| 96 |
// header is actually the sum of top and header; footer is actually the sum of bottom and footer |
| 97 |
// then top is actually the header value, and bottom is actually the footer value |
| 98 |
switch ($key) { |
| 99 |
case 'left': |
| 100 |
case 'right': |
| 101 |
$this->sheetMargin($key, $marginSize); |
| 102 |
|
| 103 |
break; |
| 104 |
case 'top': |
| 105 |
$this->sheetMargin($key, $marginSet['header'] ?? 0); |
| 106 |
|
| 107 |
break; |
| 108 |
case 'bottom': |
| 109 |
$this->sheetMargin($key, $marginSet['footer'] ?? 0); |
| 110 |
|
| 111 |
break; |
| 112 |
case 'header': |
| 113 |
$this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize); |
| 114 |
|
| 115 |
break; |
| 116 |
case 'footer': |
| 117 |
$this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize); |
| 118 |
|
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
private function sheetMargin(string $key, float $marginSize): void |
| 125 |
{ |
| 126 |
switch ($key) { |
| 127 |
case 'top': |
| 128 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize); |
| 129 |
|
| 130 |
break; |
| 131 |
case 'bottom': |
| 132 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize); |
| 133 |
|
| 134 |
break; |
| 135 |
case 'left': |
| 136 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize); |
| 137 |
|
| 138 |
break; |
| 139 |
case 'right': |
| 140 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize); |
| 141 |
|
| 142 |
break; |
| 143 |
case 'header': |
| 144 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize); |
| 145 |
|
| 146 |
break; |
| 147 |
case 'footer': |
| 148 |
$this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize); |
| 149 |
|
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|