Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Style; 4 5 class 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 14 /** 15 * Font Name. 16 * 17 * @var string 18 */ 19 protected $name = 'Calibri'; 20 21 /** 22 * Font Size. 23 * 24 * @var float 25 */ 26 protected $size = 11; 27 28 /** 29 * Bold. 30 * 31 * @var bool 32 */ 33 protected $bold = false; 34 35 /** 36 * Italic. 37 * 38 * @var bool 39 */ 40 protected $italic = false; 41 42 /** 43 * Superscript. 44 * 45 * @var bool 46 */ 47 protected $superscript = false; 48 49 /** 50 * Subscript. 51 * 52 * @var bool 53 */ 54 protected $subscript = false; 55 56 /** 57 * Underline. 58 * 59 * @var string 60 */ 61 protected $underline = self::UNDERLINE_NONE; 62 63 /** 64 * Strikethrough. 65 * 66 * @var bool 67 */ 68 protected $strikethrough = false; 69 70 /** 71 * Foreground color. 72 * 73 * @var Color 74 */ 75 protected $color; 76 77 /** 78 * @var int 79 */ 80 public $colorIndex; 81 82 /** 83 * Create a new Font. 84 * 85 * @param bool $isSupervisor Flag indicating if this is a supervisor or not 86 * Leave this value at default unless you understand exactly what 87 * its ramifications are 88 * @param bool $isConditional Flag indicating if this is a conditional style or not 89 * Leave this value at default unless you understand exactly what 90 * its ramifications are 91 */ 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 { 109 $this->color = new Color(Color::COLOR_BLACK, $isSupervisor); 110 } 111 // bind parent if we are a supervisor 112 if ($isSupervisor) { 113 $this->color->bindParent($this, 'color'); 114 } 115 } 116 117 /** 118 * Get the shared style component for the currently active cell in currently active sheet. 119 * Only used for style supervisor. 120 * 121 * @return Font 122 */ 123 public function getSharedComponent() 124 { 125 return $this->parent->getSharedComponent()->getFont(); 126 } 127 128 /** 129 * Build style array from subcomponents. 130 * 131 * @param array $array 132 * 133 * @return array 134 */ 135 public function getStyleArray($array) 136 { 137 return ['font' => $array]; 138 } 139 140 /** 141 * Apply styles from array. 142 * 143 * <code> 144 * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( 145 * [ 146 * 'name' => 'Arial', 147 * 'bold' => TRUE, 148 * 'italic' => FALSE, 149 * 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, 150 * 'strikethrough' => FALSE, 151 * 'color' => [ 152 * 'rgb' => '808080' 153 * ] 154 * ] 155 * ); 156 * </code> 157 * 158 * @param array $pStyles Array containing style information 159 * 160 * @return $this 161 */ 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 199 /** 200 * Get Name. 201 * 202 * @return string 203 */ 204 public function getName() 205 { 206 if ($this->isSupervisor) { 207 return $this->getSharedComponent()->getName(); 208 } 209 210 return $this->name; 211 } 212 213 /** 214 * Set Name. 215 * 216 * @param string $pValue 217 * 218 * @return $this 219 */ 220 public function setName($pValue) 221 { 222 if ($pValue == '') { 223 $pValue = 'Calibri'; 224 } 225 if ($this->isSupervisor) { 226 $styleArray = $this->getStyleArray(['name' => $pValue]); 227 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 228 } else { 229 $this->name = $pValue; 230 } 231 232 return $this; 233 } 234 235 /** 236 * Get Size. 237 * 238 * @return float 239 */ 240 public function getSize() 241 { 242 if ($this->isSupervisor) { 243 return $this->getSharedComponent()->getSize(); 244 } 245 246 return $this->size; 247 } 248 249 /** 250 * Set Size. 251 * 252 * @param float $pValue 253 * 254 * @return $this 255 */ 256 public function setSize($pValue) 257 { 258 if ($pValue == '') { 259 $pValue = 10; 260 } 261 if ($this->isSupervisor) { 262 $styleArray = $this->getStyleArray(['size' => $pValue]); 263 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 264 } else { 265 $this->size = $pValue; 266 } 267 268 return $this; 269 } 270 271 /** 272 * Get Bold. 273 * 274 * @return bool 275 */ 276 public function getBold() 277 { 278 if ($this->isSupervisor) { 279 return $this->getSharedComponent()->getBold(); 280 } 281 282 return $this->bold; 283 } 284 285 /** 286 * Set Bold. 287 * 288 * @param bool $pValue 289 * 290 * @return $this 291 */ 292 public function setBold($pValue) 293 { 294 if ($pValue == '') { 295 $pValue = false; 296 } 297 if ($this->isSupervisor) { 298 $styleArray = $this->getStyleArray(['bold' => $pValue]); 299 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 300 } else { 301 $this->bold = $pValue; 302 } 303 304 return $this; 305 } 306 307 /** 308 * Get Italic. 309 * 310 * @return bool 311 */ 312 public function getItalic() 313 { 314 if ($this->isSupervisor) { 315 return $this->getSharedComponent()->getItalic(); 316 } 317 318 return $this->italic; 319 } 320 321 /** 322 * Set Italic. 323 * 324 * @param bool $pValue 325 * 326 * @return $this 327 */ 328 public function setItalic($pValue) 329 { 330 if ($pValue == '') { 331 $pValue = false; 332 } 333 if ($this->isSupervisor) { 334 $styleArray = $this->getStyleArray(['italic' => $pValue]); 335 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 336 } else { 337 $this->italic = $pValue; 338 } 339 340 return $this; 341 } 342 343 /** 344 * Get Superscript. 345 * 346 * @return bool 347 */ 348 public function getSuperscript() 349 { 350 if ($this->isSupervisor) { 351 return $this->getSharedComponent()->getSuperscript(); 352 } 353 354 return $this->superscript; 355 } 356 357 /** 358 * Set Superscript. 359 * 360 * @return $this 361 */ 362 public function setSuperscript(bool $pValue) 363 { 364 if ($this->isSupervisor) { 365 $styleArray = $this->getStyleArray(['superscript' => $pValue]); 366 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 367 } else { 368 $this->superscript = $pValue; 369 if ($this->superscript) { 370 $this->subscript = false; 371 } 372 } 373 374 return $this; 375 } 376 377 /** 378 * Get Subscript. 379 * 380 * @return bool 381 */ 382 public function getSubscript() 383 { 384 if ($this->isSupervisor) { 385 return $this->getSharedComponent()->getSubscript(); 386 } 387 388 return $this->subscript; 389 } 390 391 /** 392 * Set Subscript. 393 * 394 * @return $this 395 */ 396 public function setSubscript(bool $pValue) 397 { 398 if ($this->isSupervisor) { 399 $styleArray = $this->getStyleArray(['subscript' => $pValue]); 400 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 401 } else { 402 $this->subscript = $pValue; 403 if ($this->subscript) { 404 $this->superscript = false; 405 } 406 } 407 408 return $this; 409 } 410 411 /** 412 * Get Underline. 413 * 414 * @return string 415 */ 416 public function getUnderline() 417 { 418 if ($this->isSupervisor) { 419 return $this->getSharedComponent()->getUnderline(); 420 } 421 422 return $this->underline; 423 } 424 425 /** 426 * Set Underline. 427 * 428 * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type 429 * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, 430 * false equates to UNDERLINE_NONE 431 * 432 * @return $this 433 */ 434 public function setUnderline($pValue) 435 { 436 if (is_bool($pValue)) { 437 $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; 438 } elseif ($pValue == '') { 439 $pValue = self::UNDERLINE_NONE; 440 } 441 if ($this->isSupervisor) { 442 $styleArray = $this->getStyleArray(['underline' => $pValue]); 443 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 444 } else { 445 $this->underline = $pValue; 446 } 447 448 return $this; 449 } 450 451 /** 452 * Get Strikethrough. 453 * 454 * @return bool 455 */ 456 public function getStrikethrough() 457 { 458 if ($this->isSupervisor) { 459 return $this->getSharedComponent()->getStrikethrough(); 460 } 461 462 return $this->strikethrough; 463 } 464 465 /** 466 * Set Strikethrough. 467 * 468 * @param bool $pValue 469 * 470 * @return $this 471 */ 472 public function setStrikethrough($pValue) 473 { 474 if ($pValue == '') { 475 $pValue = false; 476 } 477 478 if ($this->isSupervisor) { 479 $styleArray = $this->getStyleArray(['strikethrough' => $pValue]); 480 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 481 } else { 482 $this->strikethrough = $pValue; 483 } 484 485 return $this; 486 } 487 488 /** 489 * Get Color. 490 * 491 * @return Color 492 */ 493 public function getColor() 494 { 495 return $this->color; 496 } 497 498 /** 499 * Set Color. 500 * 501 * @return $this 502 */ 503 public function setColor(Color $pValue) 504 { 505 // make sure parameter is a real color and not a supervisor 506 $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; 507 508 if ($this->isSupervisor) { 509 $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); 510 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); 511 } else { 512 $this->color = $color; 513 } 514 515 return $this; 516 } 517 518 /** 519 * Get hash code. 520 * 521 * @return string Hash code 522 */ 523 public function getHashCode() 524 { 525 if ($this->isSupervisor) { 526 return $this->getSharedComponent()->getHashCode(); 527 } 528 529 return md5( 530 $this->name . 531 $this->size . 532 ($this->bold ? 't' : 'f') . 533 ($this->italic ? 't' : 'f') . 534 ($this->superscript ? 't' : 'f') . 535 ($this->subscript ? 't' : 'f') . 536 $this->underline . 537 ($this->strikethrough ? 't' : 'f') . 538 $this->color->getHashCode() . 539 __CLASS__ 540 ); 541 } 542 543 protected function exportArray1(): array 544 { 545 $exportedArray = []; 546 $this->exportArray2($exportedArray, 'bold', $this->getBold()); 547 $this->exportArray2($exportedArray, 'color', $this->getColor()); 548 $this->exportArray2($exportedArray, 'italic', $this->getItalic()); 549 $this->exportArray2($exportedArray, 'name', $this->getName()); 550 $this->exportArray2($exportedArray, 'size', $this->getSize()); 551 $this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough()); 552 $this->exportArray2($exportedArray, 'subscript', $this->getSubscript()); 553 $this->exportArray2($exportedArray, 'superscript', $this->getSuperscript()); 554 $this->exportArray2($exportedArray, 'underline', $this->getUnderline()); 555 556 return $exportedArray; 557 } 558 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body