PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/phpmyadmin/libraries/PHPExcel/PHPExcel/Style/Font.php

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