ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Style.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
11 class Style
12 {
13  public const CELL_STYLE_PREFIX = 'ce';
14 
15  private $writer;
16 
17  public function __construct(XMLWriter $writer)
18  {
19  $this->writer = $writer;
20  }
21 
22  private function mapHorizontalAlignment(string $horizontalAlignment): string
23  {
24  switch ($horizontalAlignment) {
28  return 'center';
30  return 'end';
33  return 'justify';
34  }
35 
36  return 'start';
37  }
38 
39  private function mapVerticalAlignment(string $verticalAlignment): string
40  {
41  switch ($verticalAlignment) {
43  return 'top';
45  return 'middle';
48  return 'automatic';
49  }
50 
51  return 'bottom';
52  }
53 
54  private function writeFillStyle(Fill $fill): void
55  {
56  switch ($fill->getFillType()) {
57  case Fill::FILL_SOLID:
58  $this->writer->writeAttribute('fo:background-color', sprintf(
59  '#%s',
60  strtolower($fill->getStartColor()->getRGB())
61  ));
62 
63  break;
67  break;
68  case Fill::FILL_NONE:
69  default:
70  }
71  }
72 
73  private function writeCellProperties(CellStyle $style): void
74  {
75  // Align
76  $hAlign = $style->getAlignment()->getHorizontal();
77  $vAlign = $style->getAlignment()->getVertical();
78  $wrap = $style->getAlignment()->getWrapText();
79 
80  $this->writer->startElement('style:table-cell-properties');
81  if (!empty($vAlign) || $wrap) {
82  if (!empty($vAlign)) {
83  $vAlign = $this->mapVerticalAlignment($vAlign);
84  $this->writer->writeAttribute('style:vertical-align', $vAlign);
85  }
86  if ($wrap) {
87  $this->writer->writeAttribute('fo:wrap-option', 'wrap');
88  }
89  }
90  $this->writer->writeAttribute('style:rotation-align', 'none');
91 
92  // Fill
93  if ($fill = $style->getFill()) {
94  $this->writeFillStyle($fill);
95  }
96 
97  $this->writer->endElement();
98 
99  if (!empty($hAlign)) {
100  $hAlign = $this->mapHorizontalAlignment($hAlign);
101  $this->writer->startElement('style:paragraph-properties');
102  $this->writer->writeAttribute('fo:text-align', $hAlign);
103  $this->writer->endElement();
104  }
105  }
106 
107  protected function mapUnderlineStyle(Font $font): string
108  {
109  switch ($font->getUnderline()) {
112  return'double';
115  return'single';
116  }
117 
118  return 'none';
119  }
120 
121  protected function writeTextProperties(CellStyle $style): void
122  {
123  // Font
124  $this->writer->startElement('style:text-properties');
125 
126  $font = $style->getFont();
127 
128  if ($font->getBold()) {
129  $this->writer->writeAttribute('fo:font-weight', 'bold');
130  $this->writer->writeAttribute('style:font-weight-complex', 'bold');
131  $this->writer->writeAttribute('style:font-weight-asian', 'bold');
132  }
133 
134  if ($font->getItalic()) {
135  $this->writer->writeAttribute('fo:font-style', 'italic');
136  }
137 
138  if ($color = $font->getColor()) {
139  $this->writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB()));
140  }
141 
142  if ($family = $font->getName()) {
143  $this->writer->writeAttribute('fo:font-family', $family);
144  }
145 
146  if ($size = $font->getSize()) {
147  $this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
148  }
149 
150  if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) {
151  $this->writer->writeAttribute('style:text-underline-style', 'solid');
152  $this->writer->writeAttribute('style:text-underline-width', 'auto');
153  $this->writer->writeAttribute('style:text-underline-color', 'font-color');
154 
155  $underline = $this->mapUnderlineStyle($font);
156  $this->writer->writeAttribute('style:text-underline-type', $underline);
157  }
158 
159  $this->writer->endElement(); // Close style:text-properties
160  }
161 
162  public function write(CellStyle $style): void
163  {
164  $this->writer->startElement('style:style');
165  $this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
166  $this->writer->writeAttribute('style:family', 'table-cell');
167  $this->writer->writeAttribute('style:parent-style-name', 'Default');
168 
169  // Alignment, fill colour, etc
170  $this->writeCellProperties($style);
171 
172  // style:text-properties
173  $this->writeTextProperties($style);
174 
175  // End
176  $this->writer->endElement(); // Close style:style
177  }
178 }
$style
Definition: example_012.php:70
$size
Definition: RandomTest.php:84
getStartColor()
Get Start Color.
Definition: Fill.php:239
mapVerticalAlignment(string $verticalAlignment)
Definition: Style.php:39
getUnderline()
Get Underline.
Definition: Font.php:423
getFillType()
Get Fill Type.
Definition: Fill.php:173
mapHorizontalAlignment(string $horizontalAlignment)
Definition: Style.php:22