ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Font.php
Go to the documentation of this file.
1<?php
2
4
5class Font extends Supervisor
6{
7 // Underline types
8 const UNDERLINE_NONE = 'none';
9 const UNDERLINE_DOUBLE = 'double';
10 const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
11 const UNDERLINE_SINGLE = 'single';
12 const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
13
19 protected $name = 'Calibri';
20
26 protected $size = 11;
27
33 protected $bold = false;
34
40 protected $italic = false;
41
47 protected $superscript = false;
48
54 protected $subscript = false;
55
62
68 protected $strikethrough = false;
69
75 protected $color;
76
81
92 public function __construct($isSupervisor = false, $isConditional = false)
93 {
94 // Supervisor?
95 parent::__construct($isSupervisor);
96
97 // Initialise values
98 if ($isConditional) {
99 $this->name = null;
100 $this->size = null;
101 $this->bold = null;
102 $this->italic = null;
103 $this->superscript = null;
104 $this->subscript = null;
105 $this->underline = null;
106 $this->strikethrough = null;
107 $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
108 } else {
110 }
111 // bind parent if we are a supervisor
112 if ($isSupervisor) {
113 $this->color->bindParent($this, 'color');
114 }
115 }
116
123 public function getSharedComponent()
124 {
125 return $this->parent->getSharedComponent()->getFont();
126 }
127
135 public function getStyleArray($array)
136 {
137 return ['font' => $array];
138 }
139
162 public function applyFromArray(array $pStyles)
163 {
164 if ($this->isSupervisor) {
165 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
166 } else {
167 if (isset($pStyles['name'])) {
168 $this->setName($pStyles['name']);
169 }
170 if (isset($pStyles['bold'])) {
171 $this->setBold($pStyles['bold']);
172 }
173 if (isset($pStyles['italic'])) {
174 $this->setItalic($pStyles['italic']);
175 }
176 if (isset($pStyles['superscript'])) {
177 $this->setSuperscript($pStyles['superscript']);
178 }
179 if (isset($pStyles['subscript'])) {
180 $this->setSubscript($pStyles['subscript']);
181 }
182 if (isset($pStyles['underline'])) {
183 $this->setUnderline($pStyles['underline']);
184 }
185 if (isset($pStyles['strikethrough'])) {
186 $this->setStrikethrough($pStyles['strikethrough']);
187 }
188 if (isset($pStyles['color'])) {
189 $this->getColor()->applyFromArray($pStyles['color']);
190 }
191 if (isset($pStyles['size'])) {
192 $this->setSize($pStyles['size']);
193 }
194 }
195
196 return $this;
197 }
198
204 public function getName()
205 {
206 if ($this->isSupervisor) {
207 return $this->getSharedComponent()->getName();
208 }
209
210 return $this->name;
211 }
212
220 public function setName($fontname)
221 {
222 if ($fontname == '') {
223 $fontname = 'Calibri';
224 }
225 if ($this->isSupervisor) {
226 $styleArray = $this->getStyleArray(['name' => $fontname]);
227 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
228 } else {
229 $this->name = $fontname;
230 }
231
232 return $this;
233 }
234
240 public function getSize()
241 {
242 if ($this->isSupervisor) {
243 return $this->getSharedComponent()->getSize();
244 }
245
246 return $this->size;
247 }
248
256 public function setSize($sizeInPoints)
257 {
258 if (is_string($sizeInPoints) || is_int($sizeInPoints)) {
259 $sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric
260 }
261
262 // Size must be a positive floating point number
263 // ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536
264 if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) {
265 $sizeInPoints = 10.0;
266 }
267
268 if ($this->isSupervisor) {
269 $styleArray = $this->getStyleArray(['size' => $sizeInPoints]);
270 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
271 } else {
272 $this->size = $sizeInPoints;
273 }
274
275 return $this;
276 }
277
283 public function getBold()
284 {
285 if ($this->isSupervisor) {
286 return $this->getSharedComponent()->getBold();
287 }
288
289 return $this->bold;
290 }
291
299 public function setBold($pValue)
300 {
301 if ($pValue == '') {
302 $pValue = false;
303 }
304 if ($this->isSupervisor) {
305 $styleArray = $this->getStyleArray(['bold' => $pValue]);
306 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
307 } else {
308 $this->bold = $pValue;
309 }
310
311 return $this;
312 }
313
319 public function getItalic()
320 {
321 if ($this->isSupervisor) {
322 return $this->getSharedComponent()->getItalic();
323 }
324
325 return $this->italic;
326 }
327
335 public function setItalic($pValue)
336 {
337 if ($pValue == '') {
338 $pValue = false;
339 }
340 if ($this->isSupervisor) {
341 $styleArray = $this->getStyleArray(['italic' => $pValue]);
342 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
343 } else {
344 $this->italic = $pValue;
345 }
346
347 return $this;
348 }
349
355 public function getSuperscript()
356 {
357 if ($this->isSupervisor) {
358 return $this->getSharedComponent()->getSuperscript();
359 }
360
361 return $this->superscript;
362 }
363
369 public function setSuperscript(bool $pValue)
370 {
371 if ($this->isSupervisor) {
372 $styleArray = $this->getStyleArray(['superscript' => $pValue]);
373 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
374 } else {
375 $this->superscript = $pValue;
376 if ($this->superscript) {
377 $this->subscript = false;
378 }
379 }
380
381 return $this;
382 }
383
389 public function getSubscript()
390 {
391 if ($this->isSupervisor) {
392 return $this->getSharedComponent()->getSubscript();
393 }
394
395 return $this->subscript;
396 }
397
403 public function setSubscript(bool $pValue)
404 {
405 if ($this->isSupervisor) {
406 $styleArray = $this->getStyleArray(['subscript' => $pValue]);
407 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
408 } else {
409 $this->subscript = $pValue;
410 if ($this->subscript) {
411 $this->superscript = false;
412 }
413 }
414
415 return $this;
416 }
417
423 public function getUnderline()
424 {
425 if ($this->isSupervisor) {
426 return $this->getSharedComponent()->getUnderline();
427 }
428
429 return $this->underline;
430 }
431
441 public function setUnderline($pValue)
442 {
443 if (is_bool($pValue)) {
444 $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
445 } elseif ($pValue == '') {
446 $pValue = self::UNDERLINE_NONE;
447 }
448 if ($this->isSupervisor) {
449 $styleArray = $this->getStyleArray(['underline' => $pValue]);
450 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
451 } else {
452 $this->underline = $pValue;
453 }
454
455 return $this;
456 }
457
463 public function getStrikethrough()
464 {
465 if ($this->isSupervisor) {
466 return $this->getSharedComponent()->getStrikethrough();
467 }
468
470 }
471
479 public function setStrikethrough($pValue)
480 {
481 if ($pValue == '') {
482 $pValue = false;
483 }
484
485 if ($this->isSupervisor) {
486 $styleArray = $this->getStyleArray(['strikethrough' => $pValue]);
487 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
488 } else {
489 $this->strikethrough = $pValue;
490 }
491
492 return $this;
493 }
494
500 public function getColor()
501 {
502 return $this->color;
503 }
504
510 public function setColor(Color $pValue)
511 {
512 // make sure parameter is a real color and not a supervisor
513 $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
514
515 if ($this->isSupervisor) {
516 $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
517 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
518 } else {
519 $this->color = $color;
520 }
521
522 return $this;
523 }
524
530 public function getHashCode()
531 {
532 if ($this->isSupervisor) {
533 return $this->getSharedComponent()->getHashCode();
534 }
535
536 return md5(
537 $this->name .
538 $this->size .
539 ($this->bold ? 't' : 'f') .
540 ($this->italic ? 't' : 'f') .
541 ($this->superscript ? 't' : 'f') .
542 ($this->subscript ? 't' : 'f') .
543 $this->underline .
544 ($this->strikethrough ? 't' : 'f') .
545 $this->color->getHashCode() .
546 __CLASS__
547 );
548 }
549
550 protected function exportArray1(): array
551 {
552 $exportedArray = [];
553 $this->exportArray2($exportedArray, 'bold', $this->getBold());
554 $this->exportArray2($exportedArray, 'color', $this->getColor());
555 $this->exportArray2($exportedArray, 'italic', $this->getItalic());
556 $this->exportArray2($exportedArray, 'name', $this->getName());
557 $this->exportArray2($exportedArray, 'size', $this->getSize());
558 $this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough());
559 $this->exportArray2($exportedArray, 'subscript', $this->getSubscript());
560 $this->exportArray2($exportedArray, 'superscript', $this->getSuperscript());
561 $this->exportArray2($exportedArray, 'underline', $this->getUnderline());
562
563 return $exportedArray;
564 }
565}
An exception for terminatinating execution or to throw for unit testing.
setBold($pValue)
Set Bold.
Definition: Font.php:299
getSubscript()
Get Subscript.
Definition: Font.php:389
getStrikethrough()
Get Strikethrough.
Definition: Font.php:463
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
Definition: Font.php:123
setColor(Color $pValue)
Set Color.
Definition: Font.php:510
setSize($sizeInPoints)
Set Size.
Definition: Font.php:256
setItalic($pValue)
Set Italic.
Definition: Font.php:335
setName($fontname)
Set Name.
Definition: Font.php:220
__construct($isSupervisor=false, $isConditional=false)
Create a new Font.
Definition: Font.php:92
applyFromArray(array $pStyles)
Apply styles from array.
Definition: Font.php:162
getSuperscript()
Get Superscript.
Definition: Font.php:355
setStrikethrough($pValue)
Set Strikethrough.
Definition: Font.php:479
getStyleArray($array)
Build style array from subcomponents.
Definition: Font.php:135
setSubscript(bool $pValue)
Set Subscript.
Definition: Font.php:403
setUnderline($pValue)
Set Underline.
Definition: Font.php:441
getUnderline()
Get Underline.
Definition: Font.php:423
setSuperscript(bool $pValue)
Set Superscript.
Definition: Font.php:369
exportArray1()
Abstract method to be implemented in anything which extends this class.
Definition: Font.php:550
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:76
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:87
getIsSupervisor()
Is this a supervisor or a cell style component?
Definition: Supervisor.php:66
exportArray2(array &$exportedArray, string $index, $objOrValue)
Populate array from exportArray1.
Definition: Supervisor.php:150
if(PHP_SAPI !='cli') color
Definition: langcheck.php:120
font size
Definition: langcheck.php:162