50c61d1e0dd35826a0eeb19d733d2cf7d776e512.svn-base 12 KB

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