PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.7.5/Classes/PHPExcel/Style/Font.php

#
PHP | 618 lines | 287 code | 48 blank | 283 comment | 61 complexity | e1b81cb9c77f12602c4f0c5f302ca396 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Font 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. private $_name = 'Calibri';
  48. /**
  49. * Font Size
  50. *
  51. * @var float
  52. */
  53. private $_size = 11;
  54. /**
  55. * Bold
  56. *
  57. * @var boolean
  58. */
  59. private $_bold = false;
  60. /**
  61. * Italic
  62. *
  63. * @var boolean
  64. */
  65. private $_italic = false;
  66. /**
  67. * Superscript
  68. *
  69. * @var boolean
  70. */
  71. private $_superScript = false;
  72. /**
  73. * Subscript
  74. *
  75. * @var boolean
  76. */
  77. private $_subScript = false;
  78. /**
  79. * Underline
  80. *
  81. * @var string
  82. */
  83. private $_underline = PHPExcel_Style_Font::UNDERLINE_NONE;
  84. /**
  85. * Strikethrough
  86. *
  87. * @var boolean
  88. */
  89. private $_strikethrough = false;
  90. /**
  91. * Foreground color
  92. *
  93. * @var PHPExcel_Style_Color
  94. */
  95. private $_color;
  96. /**
  97. * Parent Borders
  98. *
  99. * @var _parentPropertyName string
  100. */
  101. private $_parentPropertyName;
  102. /**
  103. * Supervisor?
  104. *
  105. * @var boolean
  106. */
  107. private $_isSupervisor;
  108. /**
  109. * Parent. Only used for supervisor
  110. *
  111. * @var PHPExcel_Style
  112. */
  113. private $_parent;
  114. /**
  115. * Create a new PHPExcel_Style_Font
  116. */
  117. public function __construct($isSupervisor = false)
  118. {
  119. // Supervisor?
  120. $this->_isSupervisor = $isSupervisor;
  121. // Initialise values
  122. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  123. // bind parent if we are a supervisor
  124. if ($isSupervisor) {
  125. $this->_color->bindParent($this, '_color');
  126. }
  127. }
  128. /**
  129. * Bind parent. Only used for supervisor
  130. *
  131. * @param PHPExcel_Style $parent
  132. * @return PHPExcel_Style_Font
  133. */
  134. public function bindParent($parent)
  135. {
  136. $this->_parent = $parent;
  137. }
  138. /**
  139. * Is this a supervisor or a real style component?
  140. *
  141. * @return boolean
  142. */
  143. public function getIsSupervisor()
  144. {
  145. return $this->_isSupervisor;
  146. }
  147. /**
  148. * Get the shared style component for the currently active cell in currently active sheet.
  149. * Only used for style supervisor
  150. *
  151. * @return PHPExcel_Style_Font
  152. */
  153. public function getSharedComponent()
  154. {
  155. return $this->_parent->getSharedComponent()->getFont();
  156. }
  157. /**
  158. * Get the currently active sheet. Only used for supervisor
  159. *
  160. * @return PHPExcel_Worksheet
  161. */
  162. public function getActiveSheet()
  163. {
  164. return $this->_parent->getActiveSheet();
  165. }
  166. /**
  167. * Get the currently active cell coordinate in currently active sheet.
  168. * Only used for supervisor
  169. *
  170. * @return string E.g. 'A1'
  171. */
  172. public function getSelectedCells()
  173. {
  174. return $this->getActiveSheet()->getSelectedCells();
  175. }
  176. /**
  177. * Get the currently active cell coordinate in currently active sheet.
  178. * Only used for supervisor
  179. *
  180. * @return string E.g. 'A1'
  181. */
  182. public function getActiveCell()
  183. {
  184. return $this->getActiveSheet()->getActiveCell();
  185. }
  186. /**
  187. * Build style array from subcomponents
  188. *
  189. * @param array $array
  190. * @return array
  191. */
  192. public function getStyleArray($array)
  193. {
  194. return array('font' => $array);
  195. }
  196. /**
  197. * Apply styles from array
  198. *
  199. * <code>
  200. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  201. * array(
  202. * 'name' => 'Arial',
  203. * 'bold' => true,
  204. * 'italic' => false,
  205. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  206. * 'strike' => false,
  207. * 'color' => array(
  208. * 'rgb' => '808080'
  209. * )
  210. * )
  211. * );
  212. * </code>
  213. *
  214. * @param array $pStyles Array containing style information
  215. * @throws Exception
  216. * @return PHPExcel_Style_Font
  217. */
  218. public function applyFromArray($pStyles = null) {
  219. if (is_array($pStyles)) {
  220. if ($this->_isSupervisor) {
  221. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  222. } else {
  223. if (array_key_exists('name', $pStyles)) {
  224. $this->setName($pStyles['name']);
  225. }
  226. if (array_key_exists('bold', $pStyles)) {
  227. $this->setBold($pStyles['bold']);
  228. }
  229. if (array_key_exists('italic', $pStyles)) {
  230. $this->setItalic($pStyles['italic']);
  231. }
  232. if (array_key_exists('superScript', $pStyles)) {
  233. $this->setSuperScript($pStyles['superScript']);
  234. }
  235. if (array_key_exists('subScript', $pStyles)) {
  236. $this->setSubScript($pStyles['subScript']);
  237. }
  238. if (array_key_exists('underline', $pStyles)) {
  239. $this->setUnderline($pStyles['underline']);
  240. }
  241. if (array_key_exists('strike', $pStyles)) {
  242. $this->setStrikethrough($pStyles['strike']);
  243. }
  244. if (array_key_exists('color', $pStyles)) {
  245. $this->getColor()->applyFromArray($pStyles['color']);
  246. }
  247. if (array_key_exists('size', $pStyles)) {
  248. $this->setSize($pStyles['size']);
  249. }
  250. }
  251. } else {
  252. throw new Exception("Invalid style array passed.");
  253. }
  254. return $this;
  255. }
  256. /**
  257. * Get Name
  258. *
  259. * @return string
  260. */
  261. public function getName() {
  262. if ($this->_isSupervisor) {
  263. return $this->getSharedComponent()->getName();
  264. }
  265. return $this->_name;
  266. }
  267. /**
  268. * Set Name
  269. *
  270. * @param string $pValue
  271. * @return PHPExcel_Style_Font
  272. */
  273. public function setName($pValue = 'Calibri') {
  274. if ($pValue == '') {
  275. $pValue = 'Calibri';
  276. }
  277. if ($this->_isSupervisor) {
  278. $styleArray = $this->getStyleArray(array('name' => $pValue));
  279. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  280. } else {
  281. $this->_name = $pValue;
  282. }
  283. return $this;
  284. }
  285. /**
  286. * Get Size
  287. *
  288. * @return double
  289. */
  290. public function getSize() {
  291. if ($this->_isSupervisor) {
  292. return $this->getSharedComponent()->getSize();
  293. }
  294. return $this->_size;
  295. }
  296. /**
  297. * Set Size
  298. *
  299. * @param double $pValue
  300. * @return PHPExcel_Style_Font
  301. */
  302. public function setSize($pValue = 10) {
  303. if ($pValue == '') {
  304. $pValue = 10;
  305. }
  306. if ($this->_isSupervisor) {
  307. $styleArray = $this->getStyleArray(array('size' => $pValue));
  308. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  309. } else {
  310. $this->_size = $pValue;
  311. }
  312. return $this;
  313. }
  314. /**
  315. * Get Bold
  316. *
  317. * @return boolean
  318. */
  319. public function getBold() {
  320. if ($this->_isSupervisor) {
  321. return $this->getSharedComponent()->getBold();
  322. }
  323. return $this->_bold;
  324. }
  325. /**
  326. * Set Bold
  327. *
  328. * @param boolean $pValue
  329. * @return PHPExcel_Style_Font
  330. */
  331. public function setBold($pValue = false) {
  332. if ($pValue == '') {
  333. $pValue = false;
  334. }
  335. if ($this->_isSupervisor) {
  336. $styleArray = $this->getStyleArray(array('bold' => $pValue));
  337. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  338. } else {
  339. $this->_bold = $pValue;
  340. }
  341. return $this;
  342. }
  343. /**
  344. * Get Italic
  345. *
  346. * @return boolean
  347. */
  348. public function getItalic() {
  349. if ($this->_isSupervisor) {
  350. return $this->getSharedComponent()->getItalic();
  351. }
  352. return $this->_italic;
  353. }
  354. /**
  355. * Set Italic
  356. *
  357. * @param boolean $pValue
  358. * @return PHPExcel_Style_Font
  359. */
  360. public function setItalic($pValue = false) {
  361. if ($pValue == '') {
  362. $pValue = false;
  363. }
  364. if ($this->_isSupervisor) {
  365. $styleArray = $this->getStyleArray(array('italic' => $pValue));
  366. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  367. } else {
  368. $this->_italic = $pValue;
  369. }
  370. return $this;
  371. }
  372. /**
  373. * Get SuperScript
  374. *
  375. * @return boolean
  376. */
  377. public function getSuperScript() {
  378. if ($this->_isSupervisor) {
  379. return $this->getSharedComponent()->getSuperScript();
  380. }
  381. return $this->_superScript;
  382. }
  383. /**
  384. * Set SuperScript
  385. *
  386. * @param boolean $pValue
  387. * @return PHPExcel_Style_Font
  388. */
  389. public function setSuperScript($pValue = false) {
  390. if ($pValue == '') {
  391. $pValue = false;
  392. }
  393. if ($this->_isSupervisor) {
  394. $styleArray = $this->getStyleArray(array('superScript' => $pValue));
  395. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  396. } else {
  397. $this->_superScript = $pValue;
  398. $this->_subScript = !$pValue;
  399. }
  400. return $this;
  401. }
  402. /**
  403. * Get SubScript
  404. *
  405. * @return boolean
  406. */
  407. public function getSubScript() {
  408. if ($this->_isSupervisor) {
  409. return $this->getSharedComponent()->getSubScript();
  410. }
  411. return $this->_subScript;
  412. }
  413. /**
  414. * Set SubScript
  415. *
  416. * @param boolean $pValue
  417. * @return PHPExcel_Style_Font
  418. */
  419. public function setSubScript($pValue = false) {
  420. if ($pValue == '') {
  421. $pValue = false;
  422. }
  423. if ($this->_isSupervisor) {
  424. $styleArray = $this->getStyleArray(array('subScript' => $pValue));
  425. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  426. } else {
  427. $this->_subScript = $pValue;
  428. $this->_superScript = !$pValue;
  429. }
  430. return $this;
  431. }
  432. /**
  433. * Get Underline
  434. *
  435. * @return string
  436. */
  437. public function getUnderline() {
  438. if ($this->_isSupervisor) {
  439. return $this->getSharedComponent()->getUnderline();
  440. }
  441. return $this->_underline;
  442. }
  443. /**
  444. * Set Underline
  445. *
  446. * @param string $pValue PHPExcel_Style_Font underline type
  447. * @return PHPExcel_Style_Font
  448. */
  449. public function setUnderline($pValue = PHPExcel_Style_Font::UNDERLINE_NONE) {
  450. if ($pValue == '') {
  451. $pValue = PHPExcel_Style_Font::UNDERLINE_NONE;
  452. }
  453. if ($this->_isSupervisor) {
  454. $styleArray = $this->getStyleArray(array('underline' => $pValue));
  455. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  456. } else {
  457. $this->_underline = $pValue;
  458. }
  459. return $this;
  460. }
  461. /**
  462. * Get Striketrough
  463. *
  464. * @deprecated Use getStrikethrough() instead.
  465. * @return boolean
  466. */
  467. public function getStriketrough() {
  468. return $this->getStrikethrough();
  469. }
  470. /**
  471. * Set Striketrough
  472. *
  473. * @deprecated Use setStrikethrough() instead.
  474. * @param boolean $pValue
  475. * @return PHPExcel_Style_Font
  476. */
  477. public function setStriketrough($pValue = false) {
  478. return $this->setStrikethrough($pValue);
  479. }
  480. /**
  481. * Get Strikethrough
  482. *
  483. * @return boolean
  484. */
  485. public function getStrikethrough() {
  486. if ($this->_isSupervisor) {
  487. return $this->getSharedComponent()->getStrikethrough();
  488. }
  489. return $this->_strikethrough;
  490. }
  491. /**
  492. * Set Strikethrough
  493. *
  494. * @param boolean $pValue
  495. * @return PHPExcel_Style_Font
  496. */
  497. public function setStrikethrough($pValue = false) {
  498. if ($pValue == '') {
  499. $pValue = false;
  500. }
  501. if ($this->_isSupervisor) {
  502. $styleArray = $this->getStyleArray(array('strike' => $pValue));
  503. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  504. } else {
  505. $this->_strikethrough = $pValue;
  506. }
  507. return $this;
  508. }
  509. /**
  510. * Get Color
  511. *
  512. * @return PHPExcel_Style_Color
  513. */
  514. public function getColor() {
  515. return $this->_color;
  516. }
  517. /**
  518. * Set Color
  519. *
  520. * @param PHPExcel_Style_Color $pValue
  521. * @throws Exception
  522. * @return PHPExcel_Style_Font
  523. */
  524. public function setColor(PHPExcel_Style_Color $pValue = null) {
  525. // make sure parameter is a real color and not a supervisor
  526. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  527. if ($this->_isSupervisor) {
  528. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  529. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  530. } else {
  531. $this->_color = $color;
  532. }
  533. return $this;
  534. }
  535. /**
  536. * Get hash code
  537. *
  538. * @return string Hash code
  539. */
  540. public function getHashCode() {
  541. if ($this->_isSupervisor) {
  542. return $this->getSharedComponent()->getHashCode();
  543. }
  544. return md5(
  545. $this->_name
  546. . $this->_size
  547. . ($this->_bold ? 't' : 'f')
  548. . ($this->_italic ? 't' : 'f')
  549. . ($this->_superScript ? 't' : 'f')
  550. . ($this->_subScript ? 't' : 'f')
  551. . $this->_underline
  552. . ($this->_strikethrough ? 't' : 'f')
  553. . $this->_color->getHashCode()
  554. . __CLASS__
  555. );
  556. }
  557. /**
  558. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  559. */
  560. public function __clone() {
  561. $vars = get_object_vars($this);
  562. foreach ($vars as $key => $value) {
  563. if ((is_object($value)) && ($key != '_parent')) {
  564. $this->$key = clone $value;
  565. } else {
  566. $this->$key = $value;
  567. }
  568. }
  569. }
  570. }