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

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Font.php

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