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