Font.php 13 KB

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