PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Font.php

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