Font.php 15 KB

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