Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Style; 4 5 use PhpOffice\PhpSpreadsheet\Chart\ChartColor; 6 7 class Font extends Supervisor 8 { 9 // Underline types 10 const UNDERLINE_NONE = 'none'; 11 const UNDERLINE_DOUBLE = 'double'; 12 const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting'; 13 const UNDERLINE_SINGLE = 'single'; 14 const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting'; 15 16 /** 17 * Font Name. 18 * 19 * @var null|string 20 */ 21 protected $name = 'Calibri'; 22 23 /** 24 * The following 7 are used only for chart titles, I think. 25 * 26 *@var string 27 */ 28 private $latin = ''; 29 30 /** @var string */ 31 private $eastAsian = ''; 32 33 /** @var string */ 34 private $complexScript = ''; 35 36 /** @var int */ 37 private $baseLine = 0; 38 39 /** @var string */ 40 private $strikeType = ''; 41 42 /** @var ?ChartColor */ 43 private $underlineColor; 44 45 /** @var ?ChartColor */ 46 private $chartColor; 47 // end of chart title items 48 49 /** 50 * Font Size. 51 * 52 * @var null|float 53 */ 54 protected $size = 11; 55 56 /** 57 * Bold. 58 * 59 * @var null|bool 60 */ 61 protected $bold = false; 62 63 /** 64 * Italic. 65 * 66 * @var null|bool 67 */ 68 protected $italic = false; 69 70 /** 71 * Superscript. 72 * 73 * @var null|bool 74 */ 75 protected $superscript = false; 76 77 /** 78 * Subscript. 79 * 80 * @var null|bool 81 */ 82 protected $subscript = false; 83 84 /** 85 * Underline. 86 * 87 * @var null|string 88 */ 89 protected $underline = self::UNDERLINE_NONE; 90 91 /** 92 * Strikethrough. 93 * 94 * @var null|bool 95 */ 96 protected $strikethrough = false; 97 98 /** 99 * Foreground color. 100 * 101 * @var Color 102 */ 103 protected $color; 104 105 /** 106 * @var null|int 107 */ 108 public $colorIndex; 109 110 /** @var string */ 111 protected $scheme = ''; 112 113 /** 114 * Create a new Font. 115 * 116 * @param bool $isSupervisor Flag indicating if this is a supervisor or not 117 * Leave this value at default unless you understand exactly what 118 * its ramifications are 119 * @param bool $isConditional Flag indicating if this is a conditional style or not 120 * Leave this value at default unless you understand exactly what 121 * its ramifications are 122 */ 123 public function __construct($isSupervisor = false, $isConditional = false) 124 { 125 // Supervisor? 126 parent::__construct($isSupervisor); 127 128 // Initialise values 129 if ($isConditional) { 130 $this->name = null; 131 $this->size = null; 132 $this->bold = null; 133 $this->italic = null; 134 $this->superscript = null; 135 $this->subscript = null; 136 $this->underline = null; 137 $this->strikethrough = null; 138 $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); 139 } else { 140 $this->color = new Color(Color::COLOR_BLACK, $isSupervisor); 141 } 142 // bind parent if we are a supervisor 143 if ($isSupervisor) { 144 $this->color->bindParent($this, 'color'); 145 } 146 } 147 148 /** 149 * Get the shared style component for the currently active cell in currently active sheet. 150 * Only used for style supervisor. 151 * 152 * @return Font 153 */ 154 public function getSharedComponent() 155 { 156 /** @var Style */ 157 $parent = $this->parent; 158 159 return $parent->getSharedComponent()->getFont(); 160 } 161 162 /** 163 * Build style array from subcomponents. 164 * 165 * @param array $array 166 * 167 * @return array 168 */ 169 public function getStyleArray($array) 170 { 171 return ['font' => $array]; 172 } 173 174 /** 175 * Apply styles from array. 176 * 177 * <code> 178 * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( 179 * [ 180 * 'name' => 'Arial', 181 * 'bold' => TRUE, 182 * 'italic' => FALSE, 183 * 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, 184 * 'strikethrough' => FALSE, 185 * 'color' => [ 186 * 'rgb' => '808080' 187 * ] 188 * ] 189 * ); 190 * </code> 191 * 192 * @param array $styleArray Array containing style information 193 * 194 * @return $this 195 */ 196 public function applyFromArray(array $styleArray) 197 { 198 if ($this->isSupervisor) { 199 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray)); 200 } else { 201 if (isset($styleArray['name'])) { 202 $this->setName($styleArray['name']); 203 } 204 if (isset($styleArray['latin'])) { 205 $this->setLatin($styleArray['latin']); 206 } 207 if (isset($styleArray['eastAsian'])) { 208 $this->setEastAsian($styleArray['eastAsian']); 209 } 210 if (isset($styleArray['complexScript'])) { 211 $this->setComplexScript($styleArray['complexScript']); 212 } 213 if (isset($styleArray['bold'])) { 214 $this->setBold($styleArray['bold']); 215 } 216 if (isset($styleArray['italic'])) { 217 $this->setItalic($styleArray['italic']); 218 } 219 if (isset($styleArray['superscript'])) { 220 $this->setSuperscript($styleArray['superscript']); 221 } 222 if (isset($styleArray['subscript'])) { 223 $this->setSubscript($styleArray['subscript']); 224 } 225 if (isset($styleArray['underline'])) { 226 $this->setUnderline($styleArray['underline']); 227 } 228 if (isset($styleArray['strikethrough'])) { 229 $this->setStrikethrough($styleArray['strikethrough']); 230 } 231 if (isset($styleArray['color'])) { 232 $this->getColor()->applyFromArray($styleArray['color']); 233 } 234 if (isset($styleArray['size'])) { 235 $this->setSize($styleArray['size']); 236 } 237 if (isset($styleArray['chartColor'])) { 238 $this->chartColor = $styleArray['chartColor']; 239 } 240 if (isset($styleArray['scheme'])) { 241 $this->setScheme($styleArray['scheme']); 242 } 243 } 244 245 return $this; 246 } 247 248 /** 249 * Get Name. 250 * 251 * @return null|string 252 */ 253 public function getName() 254 { 255 if ($this->isSupervisor) { 256 return $this->getSharedComponent()->getName(); 257 } 258 259 return $this->name; 260 } 261 262 public function getLatin(): string 263 { 264 if ($this->isSupervisor) { 265 return $this->getSharedComponent()->getLatin(); 266 } 267 268 return $this->latin; 269 } 270 271 public function getEastAsian(): string 272 { 273 if ($this->isSupervisor) { 274 return $this->getSharedComponent()->getEastAsian(); 275 } 276 277 return $this->eastAsian; 278 } 279 280 public function getComplexScript(): string 281 { 282 if ($this->isSupervisor) { 283 return $this->getSharedComponent()->getComplexScript(); 284 } 285 286 return $this->complexScript; 287 } 288 289 /** 290 * Set Name and turn off Scheme. 291 * 292 * @param string $fontname 293 */ 294 public function setName($fontname): self 295 { 296 if ($fontname == '') { 297 $fontname = 'Calibri'; 298 } 299 if ($this->isSupervisor) { 300 $styleArray = $this->getStyleArray(['name' => $fontname]); 301 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 302 } else { 303 $this->name = $fontname; 304 } 305 306 return $this->setScheme(''); 307 } 308 309 public function setLatin(string $fontname): self 310 { 311 if ($fontname == '') { 312 $fontname = 'Calibri'; 313 } 314 if (!$this->isSupervisor) { 315 $this->latin = $fontname; 316 } else { 317 // should never be true 318 // @codeCoverageIgnoreStart 319 $styleArray = $this->getStyleArray(['latin' => $fontname]); 320 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 321 // @codeCoverageIgnoreEnd 322 } 323 324 return $this; 325 } 326 327 public function setEastAsian(string $fontname): self 328 { 329 if ($fontname == '') { 330 $fontname = 'Calibri'; 331 } 332 if (!$this->isSupervisor) { 333 $this->eastAsian = $fontname; 334 } else { 335 // should never be true 336 // @codeCoverageIgnoreStart 337 $styleArray = $this->getStyleArray(['eastAsian' => $fontname]); 338 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 339 // @codeCoverageIgnoreEnd 340 } 341 342 return $this; 343 } 344 345 public function setComplexScript(string $fontname): self 346 { 347 if ($fontname == '') { 348 $fontname = 'Calibri'; 349 } 350 if (!$this->isSupervisor) { 351 $this->complexScript = $fontname; 352 } else { 353 // should never be true 354 // @codeCoverageIgnoreStart 355 $styleArray = $this->getStyleArray(['complexScript' => $fontname]); 356 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 357 // @codeCoverageIgnoreEnd 358 } 359 360 return $this; 361 } 362 363 /** 364 * Get Size. 365 * 366 * @return null|float 367 */ 368 public function getSize() 369 { 370 if ($this->isSupervisor) { 371 return $this->getSharedComponent()->getSize(); 372 } 373 374 return $this->size; 375 } 376 377 /** 378 * Set Size. 379 * 380 * @param mixed $sizeInPoints A float representing the value of a positive measurement in points (1/72 of an inch) 381 * 382 * @return $this 383 */ 384 public function setSize($sizeInPoints, bool $nullOk = false) 385 { 386 if (is_string($sizeInPoints) || is_int($sizeInPoints)) { 387 $sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric 388 } 389 390 // Size must be a positive floating point number 391 // ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536 392 if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) { 393 if (!$nullOk || $sizeInPoints !== null) { 394 $sizeInPoints = 10.0; 395 } 396 } 397 398 if ($this->isSupervisor) { 399 $styleArray = $this->getStyleArray(['size' => $sizeInPoints]); 400 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 401 } else { 402 $this->size = $sizeInPoints; 403 } 404 405 return $this; 406 } 407 408 /** 409 * Get Bold. 410 * 411 * @return null|bool 412 */ 413 public function getBold() 414 { 415 if ($this->isSupervisor) { 416 return $this->getSharedComponent()->getBold(); 417 } 418 419 return $this->bold; 420 } 421 422 /** 423 * Set Bold. 424 * 425 * @param bool $bold 426 * 427 * @return $this 428 */ 429 public function setBold($bold) 430 { 431 if ($bold == '') { 432 $bold = false; 433 } 434 if ($this->isSupervisor) { 435 $styleArray = $this->getStyleArray(['bold' => $bold]); 436 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 437 } else { 438 $this->bold = $bold; 439 } 440 441 return $this; 442 } 443 444 /** 445 * Get Italic. 446 * 447 * @return null|bool 448 */ 449 public function getItalic() 450 { 451 if ($this->isSupervisor) { 452 return $this->getSharedComponent()->getItalic(); 453 } 454 455 return $this->italic; 456 } 457 458 /** 459 * Set Italic. 460 * 461 * @param bool $italic 462 * 463 * @return $this 464 */ 465 public function setItalic($italic) 466 { 467 if ($italic == '') { 468 $italic = false; 469 } 470 if ($this->isSupervisor) { 471 $styleArray = $this->getStyleArray(['italic' => $italic]); 472 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 473 } else { 474 $this->italic = $italic; 475 } 476 477 return $this; 478 } 479 480 /** 481 * Get Superscript. 482 * 483 * @return null|bool 484 */ 485 public function getSuperscript() 486 { 487 if ($this->isSupervisor) { 488 return $this->getSharedComponent()->getSuperscript(); 489 } 490 491 return $this->superscript; 492 } 493 494 /** 495 * Set Superscript. 496 * 497 * @return $this 498 */ 499 public function setSuperscript(bool $superscript) 500 { 501 if ($this->isSupervisor) { 502 $styleArray = $this->getStyleArray(['superscript' => $superscript]); 503 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 504 } else { 505 $this->superscript = $superscript; 506 if ($this->superscript) { 507 $this->subscript = false; 508 } 509 } 510 511 return $this; 512 } 513 514 /** 515 * Get Subscript. 516 * 517 * @return null|bool 518 */ 519 public function getSubscript() 520 { 521 if ($this->isSupervisor) { 522 return $this->getSharedComponent()->getSubscript(); 523 } 524 525 return $this->subscript; 526 } 527 528 /** 529 * Set Subscript. 530 * 531 * @return $this 532 */ 533 public function setSubscript(bool $subscript) 534 { 535 if ($this->isSupervisor) { 536 $styleArray = $this->getStyleArray(['subscript' => $subscript]); 537 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 538 } else { 539 $this->subscript = $subscript; 540 if ($this->subscript) { 541 $this->superscript = false; 542 } 543 } 544 545 return $this; 546 } 547 548 public function getBaseLine(): int 549 { 550 if ($this->isSupervisor) { 551 return $this->getSharedComponent()->getBaseLine(); 552 } 553 554 return $this->baseLine; 555 } 556 557 public function setBaseLine(int $baseLine): self 558 { 559 if (!$this->isSupervisor) { 560 $this->baseLine = $baseLine; 561 } else { 562 // should never be true 563 // @codeCoverageIgnoreStart 564 $styleArray = $this->getStyleArray(['baseLine' => $baseLine]); 565 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 566 // @codeCoverageIgnoreEnd 567 } 568 569 return $this; 570 } 571 572 public function getStrikeType(): string 573 { 574 if ($this->isSupervisor) { 575 return $this->getSharedComponent()->getStrikeType(); 576 } 577 578 return $this->strikeType; 579 } 580 581 public function setStrikeType(string $strikeType): self 582 { 583 if (!$this->isSupervisor) { 584 $this->strikeType = $strikeType; 585 } else { 586 // should never be true 587 // @codeCoverageIgnoreStart 588 $styleArray = $this->getStyleArray(['strikeType' => $strikeType]); 589 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 590 // @codeCoverageIgnoreEnd 591 } 592 593 return $this; 594 } 595 596 public function getUnderlineColor(): ?ChartColor 597 { 598 if ($this->isSupervisor) { 599 return $this->getSharedComponent()->getUnderlineColor(); 600 } 601 602 return $this->underlineColor; 603 } 604 605 public function setUnderlineColor(array $colorArray): self 606 { 607 if (!$this->isSupervisor) { 608 $this->underlineColor = new ChartColor($colorArray); 609 } else { 610 // should never be true 611 // @codeCoverageIgnoreStart 612 $styleArray = $this->getStyleArray(['underlineColor' => $colorArray]); 613 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 614 // @codeCoverageIgnoreEnd 615 } 616 617 return $this; 618 } 619 620 public function getChartColor(): ?ChartColor 621 { 622 if ($this->isSupervisor) { 623 return $this->getSharedComponent()->getChartColor(); 624 } 625 626 return $this->chartColor; 627 } 628 629 public function setChartColor(array $colorArray): self 630 { 631 if (!$this->isSupervisor) { 632 $this->chartColor = new ChartColor($colorArray); 633 } else { 634 // should never be true 635 // @codeCoverageIgnoreStart 636 $styleArray = $this->getStyleArray(['chartColor' => $colorArray]); 637 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 638 // @codeCoverageIgnoreEnd 639 } 640 641 return $this; 642 } 643 644 public function setChartColorFromObject(?ChartColor $chartColor): self 645 { 646 $this->chartColor = $chartColor; 647 648 return $this; 649 } 650 651 /** 652 * Get Underline. 653 * 654 * @return null|string 655 */ 656 public function getUnderline() 657 { 658 if ($this->isSupervisor) { 659 return $this->getSharedComponent()->getUnderline(); 660 } 661 662 return $this->underline; 663 } 664 665 /** 666 * Set Underline. 667 * 668 * @param bool|string $underlineStyle \PhpOffice\PhpSpreadsheet\Style\Font underline type 669 * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, 670 * false equates to UNDERLINE_NONE 671 * 672 * @return $this 673 */ 674 public function setUnderline($underlineStyle) 675 { 676 if (is_bool($underlineStyle)) { 677 $underlineStyle = ($underlineStyle) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; 678 } elseif ($underlineStyle == '') { 679 $underlineStyle = self::UNDERLINE_NONE; 680 } 681 if ($this->isSupervisor) { 682 $styleArray = $this->getStyleArray(['underline' => $underlineStyle]); 683 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 684 } else { 685 $this->underline = $underlineStyle; 686 } 687 688 return $this; 689 } 690 691 /** 692 * Get Strikethrough. 693 * 694 * @return null|bool 695 */ 696 public function getStrikethrough() 697 { 698 if ($this->isSupervisor) { 699 return $this->getSharedComponent()->getStrikethrough(); 700 } 701 702 return $this->strikethrough; 703 } 704 705 /** 706 * Set Strikethrough. 707 * 708 * @param bool $strikethru 709 * 710 * @return $this 711 */ 712 public function setStrikethrough($strikethru) 713 { 714 if ($strikethru == '') { 715 $strikethru = false; 716 } 717 718 if ($this->isSupervisor) { 719 $styleArray = $this->getStyleArray(['strikethrough' => $strikethru]); 720 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 721 } else { 722 $this->strikethrough = $strikethru; 723 } 724 725 return $this; 726 } 727 728 /** 729 * Get Color. 730 * 731 * @return Color 732 */ 733 public function getColor() 734 { 735 return $this->color; 736 } 737 738 /** 739 * Set Color. 740 * 741 * @return $this 742 */ 743 public function setColor(Color $color) 744 { 745 // make sure parameter is a real color and not a supervisor 746 $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; 747 748 if ($this->isSupervisor) { 749 $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); 750 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 751 } else { 752 $this->color = $color; 753 } 754 755 return $this; 756 } 757 758 private function hashChartColor(?ChartColor $underlineColor): string 759 { 760 if ($underlineColor === null) { 761 return ''; 762 } 763 764 return 765 $underlineColor->getValue() 766 . $underlineColor->getType() 767 . (string) $underlineColor->getAlpha(); 768 } 769 770 /** 771 * Get hash code. 772 * 773 * @return string Hash code 774 */ 775 public function getHashCode() 776 { 777 if ($this->isSupervisor) { 778 return $this->getSharedComponent()->getHashCode(); 779 } 780 781 return md5( 782 $this->name . 783 $this->size . 784 ($this->bold ? 't' : 'f') . 785 ($this->italic ? 't' : 'f') . 786 ($this->superscript ? 't' : 'f') . 787 ($this->subscript ? 't' : 'f') . 788 $this->underline . 789 ($this->strikethrough ? 't' : 'f') . 790 $this->color->getHashCode() . 791 $this->scheme . 792 implode( 793 '*', 794 [ 795 $this->latin, 796 $this->eastAsian, 797 $this->complexScript, 798 $this->strikeType, 799 $this->hashChartColor($this->chartColor), 800 $this->hashChartColor($this->underlineColor), 801 (string) $this->baseLine, 802 ] 803 ) . 804 __CLASS__ 805 ); 806 } 807 808 protected function exportArray1(): array 809 { 810 $exportedArray = []; 811 $this->exportArray2($exportedArray, 'baseLine', $this->getBaseLine()); 812 $this->exportArray2($exportedArray, 'bold', $this->getBold()); 813 $this->exportArray2($exportedArray, 'chartColor', $this->getChartColor()); 814 $this->exportArray2($exportedArray, 'color', $this->getColor()); 815 $this->exportArray2($exportedArray, 'complexScript', $this->getComplexScript()); 816 $this->exportArray2($exportedArray, 'eastAsian', $this->getEastAsian()); 817 $this->exportArray2($exportedArray, 'italic', $this->getItalic()); 818 $this->exportArray2($exportedArray, 'latin', $this->getLatin()); 819 $this->exportArray2($exportedArray, 'name', $this->getName()); 820 $this->exportArray2($exportedArray, 'scheme', $this->getScheme()); 821 $this->exportArray2($exportedArray, 'size', $this->getSize()); 822 $this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough()); 823 $this->exportArray2($exportedArray, 'strikeType', $this->getStrikeType()); 824 $this->exportArray2($exportedArray, 'subscript', $this->getSubscript()); 825 $this->exportArray2($exportedArray, 'superscript', $this->getSuperscript()); 826 $this->exportArray2($exportedArray, 'underline', $this->getUnderline()); 827 $this->exportArray2($exportedArray, 'underlineColor', $this->getUnderlineColor()); 828 829 return $exportedArray; 830 } 831 832 public function getScheme(): string 833 { 834 if ($this->isSupervisor) { 835 return $this->getSharedComponent()->getScheme(); 836 } 837 838 return $this->scheme; 839 } 840 841 public function setScheme(string $scheme): self 842 { 843 if ($scheme === '' || $scheme === 'major' || $scheme === 'minor') { 844 if ($this->isSupervisor) { 845 $styleArray = $this->getStyleArray(['scheme' => $scheme]); 846 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 847 } else { 848 $this->scheme = $scheme; 849 } 850 } 851 852 return $this; 853 } 854 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body