wpdatatables
/
lib
/
phpoffice
/
phpspreadsheet
/
src
/
PhpSpreadsheet
/
Writer
/
Ods
/
Cell
/
Style.php
Style.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin 6.5.1.7, at lib/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php
| 1 | <?php |
| 2 | |
| 3 | namespace PhpOffice\PhpSpreadsheet\Writer\Ods\Cell; |
| 4 | |
| 5 | use PhpOffice\PhpSpreadsheet\Helper\Dimension; |
| 6 | use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; |
| 7 | use PhpOffice\PhpSpreadsheet\Style\Alignment; |
| 8 | use PhpOffice\PhpSpreadsheet\Style\Fill; |
| 9 | use PhpOffice\PhpSpreadsheet\Style\Font; |
| 10 | use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle; |
| 11 | use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; |
| 12 | use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; |
| 13 | use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 14 | |
| 15 | class Style |
| 16 | { |
| 17 | public const CELL_STYLE_PREFIX = 'ce'; |
| 18 | public const COLUMN_STYLE_PREFIX = 'co'; |
| 19 | public const ROW_STYLE_PREFIX = 'ro'; |
| 20 | public const TABLE_STYLE_PREFIX = 'ta'; |
| 21 | |
| 22 | /** @var XMLWriter */ |
| 23 | private $writer; |
| 24 | |
| 25 | public function __construct(XMLWriter $writer) |
| 26 | { |
| 27 | $this->writer = $writer; |
| 28 | } |
| 29 | |
| 30 | private function mapHorizontalAlignment(string $horizontalAlignment): string |
| 31 | { |
| 32 | switch ($horizontalAlignment) { |
| 33 | case Alignment::HORIZONTAL_CENTER: |
| 34 | case Alignment::HORIZONTAL_CENTER_CONTINUOUS: |
| 35 | case Alignment::HORIZONTAL_DISTRIBUTED: |
| 36 | return 'center'; |
| 37 | case Alignment::HORIZONTAL_RIGHT: |
| 38 | return 'end'; |
| 39 | case Alignment::HORIZONTAL_FILL: |
| 40 | case Alignment::HORIZONTAL_JUSTIFY: |
| 41 | return 'justify'; |
| 42 | } |
| 43 | |
| 44 | return 'start'; |
| 45 | } |
| 46 | |
| 47 | private function mapVerticalAlignment(string $verticalAlignment): string |
| 48 | { |
| 49 | switch ($verticalAlignment) { |
| 50 | case Alignment::VERTICAL_TOP: |
| 51 | return 'top'; |
| 52 | case Alignment::VERTICAL_CENTER: |
| 53 | return 'middle'; |
| 54 | case Alignment::VERTICAL_DISTRIBUTED: |
| 55 | case Alignment::VERTICAL_JUSTIFY: |
| 56 | return 'automatic'; |
| 57 | } |
| 58 | |
| 59 | return 'bottom'; |
| 60 | } |
| 61 | |
| 62 | private function writeFillStyle(Fill $fill): void |
| 63 | { |
| 64 | switch ($fill->getFillType()) { |
| 65 | case Fill::FILL_SOLID: |
| 66 | $this->writer->writeAttribute('fo:background-color', sprintf( |
| 67 | '#%s', |
| 68 | strtolower($fill->getStartColor()->getRGB()) |
| 69 | )); |
| 70 | |
| 71 | break; |
| 72 | case Fill::FILL_GRADIENT_LINEAR: |
| 73 | case Fill::FILL_GRADIENT_PATH: |
| 74 | /// TODO :: To be implemented |
| 75 | break; |
| 76 | case Fill::FILL_NONE: |
| 77 | default: |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private function writeCellProperties(CellStyle $style): void |
| 82 | { |
| 83 | // Align |
| 84 | $hAlign = $style->getAlignment()->getHorizontal(); |
| 85 | $vAlign = $style->getAlignment()->getVertical(); |
| 86 | $wrap = $style->getAlignment()->getWrapText(); |
| 87 | |
| 88 | $this->writer->startElement('style:table-cell-properties'); |
| 89 | if (!empty($vAlign) || $wrap) { |
| 90 | if (!empty($vAlign)) { |
| 91 | $vAlign = $this->mapVerticalAlignment($vAlign); |
| 92 | $this->writer->writeAttribute('style:vertical-align', $vAlign); |
| 93 | } |
| 94 | if ($wrap) { |
| 95 | $this->writer->writeAttribute('fo:wrap-option', 'wrap'); |
| 96 | } |
| 97 | } |
| 98 | $this->writer->writeAttribute('style:rotation-align', 'none'); |
| 99 | |
| 100 | // Fill |
| 101 | $this->writeFillStyle($style->getFill()); |
| 102 | |
| 103 | $this->writer->endElement(); |
| 104 | |
| 105 | if (!empty($hAlign)) { |
| 106 | $hAlign = $this->mapHorizontalAlignment($hAlign); |
| 107 | $this->writer->startElement('style:paragraph-properties'); |
| 108 | $this->writer->writeAttribute('fo:text-align', $hAlign); |
| 109 | $this->writer->endElement(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | protected function mapUnderlineStyle(Font $font): string |
| 114 | { |
| 115 | switch ($font->getUnderline()) { |
| 116 | case Font::UNDERLINE_DOUBLE: |
| 117 | case Font::UNDERLINE_DOUBLEACCOUNTING: |
| 118 | return'double'; |
| 119 | case Font::UNDERLINE_SINGLE: |
| 120 | case Font::UNDERLINE_SINGLEACCOUNTING: |
| 121 | return'single'; |
| 122 | } |
| 123 | |
| 124 | return 'none'; |
| 125 | } |
| 126 | |
| 127 | protected function writeTextProperties(CellStyle $style): void |
| 128 | { |
| 129 | // Font |
| 130 | $this->writer->startElement('style:text-properties'); |
| 131 | |
| 132 | $font = $style->getFont(); |
| 133 | |
| 134 | if ($font->getBold()) { |
| 135 | $this->writer->writeAttribute('fo:font-weight', 'bold'); |
| 136 | $this->writer->writeAttribute('style:font-weight-complex', 'bold'); |
| 137 | $this->writer->writeAttribute('style:font-weight-asian', 'bold'); |
| 138 | } |
| 139 | |
| 140 | if ($font->getItalic()) { |
| 141 | $this->writer->writeAttribute('fo:font-style', 'italic'); |
| 142 | } |
| 143 | |
| 144 | $this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB())); |
| 145 | |
| 146 | if ($family = $font->getName()) { |
| 147 | $this->writer->writeAttribute('fo:font-family', $family); |
| 148 | } |
| 149 | |
| 150 | if ($size = $font->getSize()) { |
| 151 | $this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size)); |
| 152 | } |
| 153 | |
| 154 | if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) { |
| 155 | $this->writer->writeAttribute('style:text-underline-style', 'solid'); |
| 156 | $this->writer->writeAttribute('style:text-underline-width', 'auto'); |
| 157 | $this->writer->writeAttribute('style:text-underline-color', 'font-color'); |
| 158 | |
| 159 | $underline = $this->mapUnderlineStyle($font); |
| 160 | $this->writer->writeAttribute('style:text-underline-type', $underline); |
| 161 | } |
| 162 | |
| 163 | $this->writer->endElement(); // Close style:text-properties |
| 164 | } |
| 165 | |
| 166 | protected function writeColumnProperties(ColumnDimension $columnDimension): void |
| 167 | { |
| 168 | $this->writer->startElement('style:table-column-properties'); |
| 169 | $this->writer->writeAttribute( |
| 170 | 'style:column-width', |
| 171 | round($columnDimension->getWidth(Dimension::UOM_CENTIMETERS), 3) . 'cm' |
| 172 | ); |
| 173 | $this->writer->writeAttribute('fo:break-before', 'auto'); |
| 174 | |
| 175 | // End |
| 176 | $this->writer->endElement(); // Close style:table-column-properties |
| 177 | } |
| 178 | |
| 179 | public function writeColumnStyles(ColumnDimension $columnDimension, int $sheetId): void |
| 180 | { |
| 181 | $this->writer->startElement('style:style'); |
| 182 | $this->writer->writeAttribute('style:family', 'table-column'); |
| 183 | $this->writer->writeAttribute( |
| 184 | 'style:name', |
| 185 | sprintf('%s_%d_%d', self::COLUMN_STYLE_PREFIX, $sheetId, $columnDimension->getColumnNumeric()) |
| 186 | ); |
| 187 | |
| 188 | $this->writeColumnProperties($columnDimension); |
| 189 | |
| 190 | // End |
| 191 | $this->writer->endElement(); // Close style:style |
| 192 | } |
| 193 | |
| 194 | protected function writeRowProperties(RowDimension $rowDimension): void |
| 195 | { |
| 196 | $this->writer->startElement('style:table-row-properties'); |
| 197 | $this->writer->writeAttribute( |
| 198 | 'style:row-height', |
| 199 | round($rowDimension->getRowHeight(Dimension::UOM_CENTIMETERS), 3) . 'cm' |
| 200 | ); |
| 201 | $this->writer->writeAttribute('style:use-optimal-row-height', 'false'); |
| 202 | $this->writer->writeAttribute('fo:break-before', 'auto'); |
| 203 | |
| 204 | // End |
| 205 | $this->writer->endElement(); // Close style:table-row-properties |
| 206 | } |
| 207 | |
| 208 | public function writeRowStyles(RowDimension $rowDimension, int $sheetId): void |
| 209 | { |
| 210 | $this->writer->startElement('style:style'); |
| 211 | $this->writer->writeAttribute('style:family', 'table-row'); |
| 212 | $this->writer->writeAttribute( |
| 213 | 'style:name', |
| 214 | sprintf('%s_%d_%d', self::ROW_STYLE_PREFIX, $sheetId, $rowDimension->getRowIndex()) |
| 215 | ); |
| 216 | |
| 217 | $this->writeRowProperties($rowDimension); |
| 218 | |
| 219 | // End |
| 220 | $this->writer->endElement(); // Close style:style |
| 221 | } |
| 222 | |
| 223 | public function writeTableStyle(Worksheet $worksheet, int $sheetId): void |
| 224 | { |
| 225 | $this->writer->startElement('style:style'); |
| 226 | $this->writer->writeAttribute('style:family', 'table'); |
| 227 | $this->writer->writeAttribute( |
| 228 | 'style:name', |
| 229 | sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId) |
| 230 | ); |
| 231 | |
| 232 | $this->writer->startElement('style:table-properties'); |
| 233 | |
| 234 | $this->writer->writeAttribute( |
| 235 | 'table:display', |
| 236 | $worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false' |
| 237 | ); |
| 238 | |
| 239 | $this->writer->endElement(); // Close style:table-properties |
| 240 | $this->writer->endElement(); // Close style:style |
| 241 | } |
| 242 | |
| 243 | public function write(CellStyle $style): void |
| 244 | { |
| 245 | $this->writer->startElement('style:style'); |
| 246 | $this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex()); |
| 247 | $this->writer->writeAttribute('style:family', 'table-cell'); |
| 248 | $this->writer->writeAttribute('style:parent-style-name', 'Default'); |
| 249 | |
| 250 | // Alignment, fill colour, etc |
| 251 | $this->writeCellProperties($style); |
| 252 | |
| 253 | // style:text-properties |
| 254 | $this->writeTextProperties($style); |
| 255 | |
| 256 | // End |
| 257 | $this->writer->endElement(); // Close style:style |
| 258 | } |
| 259 | } |
| 260 |