Font.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. *
  127. * <code>
  128. * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  129. * [
  130. * 'name' => 'Arial',
  131. * 'bold' => TRUE,
  132. * 'italic' => FALSE,
  133. * 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
  134. * 'strikethrough' => FALSE,
  135. * 'color' => [
  136. * 'rgb' => '808080'
  137. * ]
  138. * ]
  139. * );
  140. * </code>
  141. *
  142. * @param array $pStyles Array containing style information
  143. *
  144. * @throws PhpSpreadsheetException
  145. *
  146. * @return Font
  147. */
  148. public function applyFromArray(array $pStyles)
  149. {
  150. if ($this->isSupervisor) {
  151. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  152. } else {
  153. if (isset($pStyles['name'])) {
  154. $this->setName($pStyles['name']);
  155. }
  156. if (isset($pStyles['bold'])) {
  157. $this->setBold($pStyles['bold']);
  158. }
  159. if (isset($pStyles['italic'])) {
  160. $this->setItalic($pStyles['italic']);
  161. }
  162. if (isset($pStyles['superscript'])) {
  163. $this->setSuperscript($pStyles['superscript']);
  164. }
  165. if (isset($pStyles['subscript'])) {
  166. $this->setSubscript($pStyles['subscript']);
  167. }
  168. if (isset($pStyles['underline'])) {
  169. $this->setUnderline($pStyles['underline']);
  170. }
  171. if (isset($pStyles['strikethrough'])) {
  172. $this->setStrikethrough($pStyles['strikethrough']);
  173. }
  174. if (isset($pStyles['color'])) {
  175. $this->getColor()->applyFromArray($pStyles['color']);
  176. }
  177. if (isset($pStyles['size'])) {
  178. $this->setSize($pStyles['size']);
  179. }
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Get Name.
  185. *
  186. * @return string
  187. */
  188. public function getName()
  189. {
  190. if ($this->isSupervisor) {
  191. return $this->getSharedComponent()->getName();
  192. }
  193. return $this->name;
  194. }
  195. /**
  196. * Set Name.
  197. *
  198. * @param string $pValue
  199. *
  200. * @return Font
  201. */
  202. public function setName($pValue)
  203. {
  204. if ($pValue == '') {
  205. $pValue = 'Calibri';
  206. }
  207. if ($this->isSupervisor) {
  208. $styleArray = $this->getStyleArray(['name' => $pValue]);
  209. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  210. } else {
  211. $this->name = $pValue;
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Get Size.
  217. *
  218. * @return float
  219. */
  220. public function getSize()
  221. {
  222. if ($this->isSupervisor) {
  223. return $this->getSharedComponent()->getSize();
  224. }
  225. return $this->size;
  226. }
  227. /**
  228. * Set Size.
  229. *
  230. * @param float $pValue
  231. *
  232. * @return Font
  233. */
  234. public function setSize($pValue)
  235. {
  236. if ($pValue == '') {
  237. $pValue = 10;
  238. }
  239. if ($this->isSupervisor) {
  240. $styleArray = $this->getStyleArray(['size' => $pValue]);
  241. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  242. } else {
  243. $this->size = $pValue;
  244. }
  245. return $this;
  246. }
  247. /**
  248. * Get Bold.
  249. *
  250. * @return bool
  251. */
  252. public function getBold()
  253. {
  254. if ($this->isSupervisor) {
  255. return $this->getSharedComponent()->getBold();
  256. }
  257. return $this->bold;
  258. }
  259. /**
  260. * Set Bold.
  261. *
  262. * @param bool $pValue
  263. *
  264. * @return Font
  265. */
  266. public function setBold($pValue)
  267. {
  268. if ($pValue == '') {
  269. $pValue = false;
  270. }
  271. if ($this->isSupervisor) {
  272. $styleArray = $this->getStyleArray(['bold' => $pValue]);
  273. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  274. } else {
  275. $this->bold = $pValue;
  276. }
  277. return $this;
  278. }
  279. /**
  280. * Get Italic.
  281. *
  282. * @return bool
  283. */
  284. public function getItalic()
  285. {
  286. if ($this->isSupervisor) {
  287. return $this->getSharedComponent()->getItalic();
  288. }
  289. return $this->italic;
  290. }
  291. /**
  292. * Set Italic.
  293. *
  294. * @param bool $pValue
  295. *
  296. * @return Font
  297. */
  298. public function setItalic($pValue)
  299. {
  300. if ($pValue == '') {
  301. $pValue = false;
  302. }
  303. if ($this->isSupervisor) {
  304. $styleArray = $this->getStyleArray(['italic' => $pValue]);
  305. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  306. } else {
  307. $this->italic = $pValue;
  308. }
  309. return $this;
  310. }
  311. /**
  312. * Get Superscript.
  313. *
  314. * @return bool
  315. */
  316. public function getSuperscript()
  317. {
  318. if ($this->isSupervisor) {
  319. return $this->getSharedComponent()->getSuperscript();
  320. }
  321. return $this->superscript;
  322. }
  323. /**
  324. * Set Superscript.
  325. *
  326. * @param bool $pValue
  327. *
  328. * @return Font
  329. */
  330. public function setSuperscript($pValue)
  331. {
  332. if ($pValue == '') {
  333. $pValue = false;
  334. }
  335. if ($this->isSupervisor) {
  336. $styleArray = $this->getStyleArray(['superscript' => $pValue]);
  337. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  338. } else {
  339. $this->superscript = $pValue;
  340. $this->subscript = !$pValue;
  341. }
  342. return $this;
  343. }
  344. /**
  345. * Get Subscript.
  346. *
  347. * @return bool
  348. */
  349. public function getSubscript()
  350. {
  351. if ($this->isSupervisor) {
  352. return $this->getSharedComponent()->getSubscript();
  353. }
  354. return $this->subscript;
  355. }
  356. /**
  357. * Set Subscript.
  358. *
  359. * @param bool $pValue
  360. *
  361. * @return Font
  362. */
  363. public function setSubscript($pValue)
  364. {
  365. if ($pValue == '') {
  366. $pValue = false;
  367. }
  368. if ($this->isSupervisor) {
  369. $styleArray = $this->getStyleArray(['subscript' => $pValue]);
  370. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  371. } else {
  372. $this->subscript = $pValue;
  373. $this->superscript = !$pValue;
  374. }
  375. return $this;
  376. }
  377. /**
  378. * Get Underline.
  379. *
  380. * @return string
  381. */
  382. public function getUnderline()
  383. {
  384. if ($this->isSupervisor) {
  385. return $this->getSharedComponent()->getUnderline();
  386. }
  387. return $this->underline;
  388. }
  389. /**
  390. * Set Underline.
  391. *
  392. * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type
  393. * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
  394. * false equates to UNDERLINE_NONE
  395. *
  396. * @return Font
  397. */
  398. public function setUnderline($pValue)
  399. {
  400. if (is_bool($pValue)) {
  401. $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
  402. } elseif ($pValue == '') {
  403. $pValue = self::UNDERLINE_NONE;
  404. }
  405. if ($this->isSupervisor) {
  406. $styleArray = $this->getStyleArray(['underline' => $pValue]);
  407. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  408. } else {
  409. $this->underline = $pValue;
  410. }
  411. return $this;
  412. }
  413. /**
  414. * Get Strikethrough.
  415. *
  416. * @return bool
  417. */
  418. public function getStrikethrough()
  419. {
  420. if ($this->isSupervisor) {
  421. return $this->getSharedComponent()->getStrikethrough();
  422. }
  423. return $this->strikethrough;
  424. }
  425. /**
  426. * Set Strikethrough.
  427. *
  428. * @param bool $pValue
  429. *
  430. * @return Font
  431. */
  432. public function setStrikethrough($pValue)
  433. {
  434. if ($pValue == '') {
  435. $pValue = false;
  436. }
  437. if ($this->isSupervisor) {
  438. $styleArray = $this->getStyleArray(['strikethrough' => $pValue]);
  439. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  440. } else {
  441. $this->strikethrough = $pValue;
  442. }
  443. return $this;
  444. }
  445. /**
  446. * Get Color.
  447. *
  448. * @return Color
  449. */
  450. public function getColor()
  451. {
  452. return $this->color;
  453. }
  454. /**
  455. * Set Color.
  456. *
  457. * @param Color $pValue
  458. *
  459. * @throws PhpSpreadsheetException
  460. *
  461. * @return Font
  462. */
  463. public function setColor(Color $pValue)
  464. {
  465. // make sure parameter is a real color and not a supervisor
  466. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  467. if ($this->isSupervisor) {
  468. $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
  469. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  470. } else {
  471. $this->color = $color;
  472. }
  473. return $this;
  474. }
  475. /**
  476. * Get hash code.
  477. *
  478. * @return string Hash code
  479. */
  480. public function getHashCode()
  481. {
  482. if ($this->isSupervisor) {
  483. return $this->getSharedComponent()->getHashCode();
  484. }
  485. return md5(
  486. $this->name .
  487. $this->size .
  488. ($this->bold ? 't' : 'f') .
  489. ($this->italic ? 't' : 'f') .
  490. ($this->superscript ? 't' : 'f') .
  491. ($this->subscript ? 't' : 'f') .
  492. $this->underline .
  493. ($this->strikethrough ? 't' : 'f') .
  494. $this->color->getHashCode() .
  495. __CLASS__
  496. );
  497. }
  498. }