/lib/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/Font.php

https://github.com/abgreeve/moodle · PHP · 558 lines · 292 code · 59 blank · 207 comment · 55 complexity · d5f0c7a1608bcc75469eadbccedf1dfd MD5 · raw file

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