PageRenderTime 40ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

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