Font.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 null|string
  15. */
  16. protected $name = 'Calibri';
  17. /**
  18. * Font Size.
  19. *
  20. * @var null|float
  21. */
  22. protected $size = 11;
  23. /**
  24. * Bold.
  25. *
  26. * @var null|bool
  27. */
  28. protected $bold = false;
  29. /**
  30. * Italic.
  31. *
  32. * @var null|bool
  33. */
  34. protected $italic = false;
  35. /**
  36. * Superscript.
  37. *
  38. * @var null|bool
  39. */
  40. protected $superscript = false;
  41. /**
  42. * Subscript.
  43. *
  44. * @var null|bool
  45. */
  46. protected $subscript = false;
  47. /**
  48. * Underline.
  49. *
  50. * @var null|string
  51. */
  52. protected $underline = self::UNDERLINE_NONE;
  53. /**
  54. * Strikethrough.
  55. *
  56. * @var null|bool
  57. */
  58. protected $strikethrough = false;
  59. /**
  60. * Foreground color.
  61. *
  62. * @var Color
  63. */
  64. protected $color;
  65. /**
  66. * @var null|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 null|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 $fontname
  196. *
  197. * @return $this
  198. */
  199. public function setName($fontname)
  200. {
  201. if ($fontname == '') {
  202. $fontname = 'Calibri';
  203. }
  204. if ($this->isSupervisor) {
  205. $styleArray = $this->getStyleArray(['name' => $fontname]);
  206. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  207. } else {
  208. $this->name = $fontname;
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Get Size.
  214. *
  215. * @return null|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 mixed $sizeInPoints A float representing the value of a positive measurement in points (1/72 of an inch)
  228. *
  229. * @return $this
  230. */
  231. public function setSize($sizeInPoints)
  232. {
  233. if (is_string($sizeInPoints) || is_int($sizeInPoints)) {
  234. $sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric
  235. }
  236. // Size must be a positive floating point number
  237. // ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536
  238. if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) {
  239. $sizeInPoints = 10.0;
  240. }
  241. if ($this->isSupervisor) {
  242. $styleArray = $this->getStyleArray(['size' => $sizeInPoints]);
  243. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  244. } else {
  245. $this->size = $sizeInPoints;
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Get Bold.
  251. *
  252. * @return null|bool
  253. */
  254. public function getBold()
  255. {
  256. if ($this->isSupervisor) {
  257. return $this->getSharedComponent()->getBold();
  258. }
  259. return $this->bold;
  260. }
  261. /**
  262. * Set Bold.
  263. *
  264. * @param bool $pValue
  265. *
  266. * @return $this
  267. */
  268. public function setBold($pValue)
  269. {
  270. if ($pValue == '') {
  271. $pValue = false;
  272. }
  273. if ($this->isSupervisor) {
  274. $styleArray = $this->getStyleArray(['bold' => $pValue]);
  275. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  276. } else {
  277. $this->bold = $pValue;
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Get Italic.
  283. *
  284. * @return null|bool
  285. */
  286. public function getItalic()
  287. {
  288. if ($this->isSupervisor) {
  289. return $this->getSharedComponent()->getItalic();
  290. }
  291. return $this->italic;
  292. }
  293. /**
  294. * Set Italic.
  295. *
  296. * @param bool $pValue
  297. *
  298. * @return $this
  299. */
  300. public function setItalic($pValue)
  301. {
  302. if ($pValue == '') {
  303. $pValue = false;
  304. }
  305. if ($this->isSupervisor) {
  306. $styleArray = $this->getStyleArray(['italic' => $pValue]);
  307. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  308. } else {
  309. $this->italic = $pValue;
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Get Superscript.
  315. *
  316. * @return null|bool
  317. */
  318. public function getSuperscript()
  319. {
  320. if ($this->isSupervisor) {
  321. return $this->getSharedComponent()->getSuperscript();
  322. }
  323. return $this->superscript;
  324. }
  325. /**
  326. * Set Superscript.
  327. *
  328. * @return $this
  329. */
  330. public function setSuperscript(bool $pValue)
  331. {
  332. if ($this->isSupervisor) {
  333. $styleArray = $this->getStyleArray(['superscript' => $pValue]);
  334. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  335. } else {
  336. $this->superscript = $pValue;
  337. if ($this->superscript) {
  338. $this->subscript = false;
  339. }
  340. }
  341. return $this;
  342. }
  343. /**
  344. * Get Subscript.
  345. *
  346. * @return null|bool
  347. */
  348. public function getSubscript()
  349. {
  350. if ($this->isSupervisor) {
  351. return $this->getSharedComponent()->getSubscript();
  352. }
  353. return $this->subscript;
  354. }
  355. /**
  356. * Set Subscript.
  357. *
  358. * @return $this
  359. */
  360. public function setSubscript(bool $pValue)
  361. {
  362. if ($this->isSupervisor) {
  363. $styleArray = $this->getStyleArray(['subscript' => $pValue]);
  364. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  365. } else {
  366. $this->subscript = $pValue;
  367. if ($this->subscript) {
  368. $this->superscript = false;
  369. }
  370. }
  371. return $this;
  372. }
  373. /**
  374. * Get Underline.
  375. *
  376. * @return null|string
  377. */
  378. public function getUnderline()
  379. {
  380. if ($this->isSupervisor) {
  381. return $this->getSharedComponent()->getUnderline();
  382. }
  383. return $this->underline;
  384. }
  385. /**
  386. * Set Underline.
  387. *
  388. * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type
  389. * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
  390. * false equates to UNDERLINE_NONE
  391. *
  392. * @return $this
  393. */
  394. public function setUnderline($pValue)
  395. {
  396. if (is_bool($pValue)) {
  397. $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
  398. } elseif ($pValue == '') {
  399. $pValue = self::UNDERLINE_NONE;
  400. }
  401. if ($this->isSupervisor) {
  402. $styleArray = $this->getStyleArray(['underline' => $pValue]);
  403. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  404. } else {
  405. $this->underline = $pValue;
  406. }
  407. return $this;
  408. }
  409. /**
  410. * Get Strikethrough.
  411. *
  412. * @return null|bool
  413. */
  414. public function getStrikethrough()
  415. {
  416. if ($this->isSupervisor) {
  417. return $this->getSharedComponent()->getStrikethrough();
  418. }
  419. return $this->strikethrough;
  420. }
  421. /**
  422. * Set Strikethrough.
  423. *
  424. * @param bool $pValue
  425. *
  426. * @return $this
  427. */
  428. public function setStrikethrough($pValue)
  429. {
  430. if ($pValue == '') {
  431. $pValue = false;
  432. }
  433. if ($this->isSupervisor) {
  434. $styleArray = $this->getStyleArray(['strikethrough' => $pValue]);
  435. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  436. } else {
  437. $this->strikethrough = $pValue;
  438. }
  439. return $this;
  440. }
  441. /**
  442. * Get Color.
  443. *
  444. * @return Color
  445. */
  446. public function getColor()
  447. {
  448. return $this->color;
  449. }
  450. /**
  451. * Set Color.
  452. *
  453. * @return $this
  454. */
  455. public function setColor(Color $pValue)
  456. {
  457. // make sure parameter is a real color and not a supervisor
  458. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  459. if ($this->isSupervisor) {
  460. $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
  461. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  462. } else {
  463. $this->color = $color;
  464. }
  465. return $this;
  466. }
  467. /**
  468. * Get hash code.
  469. *
  470. * @return string Hash code
  471. */
  472. public function getHashCode()
  473. {
  474. if ($this->isSupervisor) {
  475. return $this->getSharedComponent()->getHashCode();
  476. }
  477. return md5(
  478. $this->name .
  479. $this->size .
  480. ($this->bold ? 't' : 'f') .
  481. ($this->italic ? 't' : 'f') .
  482. ($this->superscript ? 't' : 'f') .
  483. ($this->subscript ? 't' : 'f') .
  484. $this->underline .
  485. ($this->strikethrough ? 't' : 'f') .
  486. $this->color->getHashCode() .
  487. __CLASS__
  488. );
  489. }
  490. protected function exportArray1(): array
  491. {
  492. $exportedArray = [];
  493. $this->exportArray2($exportedArray, 'bold', $this->getBold());
  494. $this->exportArray2($exportedArray, 'color', $this->getColor());
  495. $this->exportArray2($exportedArray, 'italic', $this->getItalic());
  496. $this->exportArray2($exportedArray, 'name', $this->getName());
  497. $this->exportArray2($exportedArray, 'size', $this->getSize());
  498. $this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough());
  499. $this->exportArray2($exportedArray, 'subscript', $this->getSubscript());
  500. $this->exportArray2($exportedArray, 'superscript', $this->getSuperscript());
  501. $this->exportArray2($exportedArray, 'underline', $this->getUnderline());
  502. return $exportedArray;
  503. }
  504. }