PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi16-graphs/Classes/PHPExcel/Style/Font.php

#
PHP | 524 lines | 205 code | 51 blank | 268 comment | 33 complexity | 571ecf14ab9d90988b92f54336353ced 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. * Strikethrough
  84. *
  85. * @var boolean
  86. */
  87. private $_strikethrough;
  88. /**
  89. * Foreground color
  90. *
  91. * @var PHPExcel_Style_Color
  92. */
  93. private $_color;
  94. /**
  95. * Parent Style
  96. *
  97. * @var PHPExcel_Style
  98. */
  99. private $_parent;
  100. /**
  101. * Parent Borders
  102. *
  103. * @var _parentPropertyName string
  104. */
  105. private $_parentPropertyName;
  106. /**
  107. * Create a new PHPExcel_Style_Font
  108. */
  109. public function __construct()
  110. {
  111. // Initialise values
  112. $this->_name = 'Calibri';
  113. $this->_size = 10;
  114. $this->_bold = false;
  115. $this->_italic = false;
  116. $this->_superScript = false;
  117. $this->_subScript = false;
  118. $this->_underline = PHPExcel_Style_Font::UNDERLINE_NONE;
  119. $this->_strikethrough = false;
  120. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  121. }
  122. /**
  123. * Property Prepare bind
  124. *
  125. * Configures this object for late binding as a property of a parent object
  126. *
  127. * @param $parent
  128. * @param $parentPropertyName
  129. */
  130. public function propertyPrepareBind($parent, $parentPropertyName)
  131. {
  132. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  133. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  134. $this->_parent = $parent;
  135. $this->_parentPropertyName = $parentPropertyName;
  136. }
  137. /**
  138. * Property Get Bound
  139. *
  140. * Returns the PHPExcel_Style_Font that is actual bound to PHPExcel_Style
  141. *
  142. * @return PHPExcel_Style_Font
  143. */
  144. private function propertyGetBound() {
  145. if(!isset($this->_parent))
  146. return $this; // I am bound
  147. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  148. return $this->_parent->getFont(); // Another one is bound
  149. return $this; // No one is bound yet
  150. }
  151. /**
  152. * Property Begin Bind
  153. *
  154. * If no PHPExcel_Style_Font has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  155. *
  156. * @return PHPExcel_Style_Font
  157. */
  158. private function propertyBeginBind() {
  159. if(!isset($this->_parent))
  160. return $this; // I am already bound
  161. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  162. return $this->_parent->getFont(); // Another one is already bound
  163. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  164. $this->_parent = null;
  165. return $this;
  166. }
  167. /**
  168. * Apply styles from array
  169. *
  170. * <code>
  171. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  172. * array(
  173. * 'name' => 'Arial',
  174. * 'bold' => true,
  175. * 'italic' => false,
  176. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  177. * 'strike' => false,
  178. * 'color' => array(
  179. * 'rgb' => '808080'
  180. * )
  181. * )
  182. * );
  183. * </code>
  184. *
  185. * @param array $pStyles Array containing style information
  186. * @throws Exception
  187. */
  188. public function applyFromArray($pStyles = null) {
  189. if (is_array($pStyles)) {
  190. if (array_key_exists('name', $pStyles)) {
  191. $this->setName($pStyles['name']);
  192. }
  193. if (array_key_exists('bold', $pStyles)) {
  194. $this->setBold($pStyles['bold']);
  195. }
  196. if (array_key_exists('italic', $pStyles)) {
  197. $this->setItalic($pStyles['italic']);
  198. }
  199. if (array_key_exists('superScript', $pStyles)) {
  200. $this->setSuperScript($pStyles['superScript']);
  201. }
  202. if (array_key_exists('subScript', $pStyles)) {
  203. $this->setSubScript($pStyles['subScript']);
  204. }
  205. if (array_key_exists('underline', $pStyles)) {
  206. $this->setUnderline($pStyles['underline']);
  207. }
  208. if (array_key_exists('strike', $pStyles)) {
  209. $this->setStrikethrough($pStyles['strike']);
  210. }
  211. if (array_key_exists('color', $pStyles)) {
  212. $this->getColor()->applyFromArray($pStyles['color']);
  213. }
  214. if (array_key_exists('size', $pStyles)) {
  215. $this->setSize($pStyles['size']);
  216. }
  217. } else {
  218. throw new Exception("Invalid style array passed.");
  219. }
  220. }
  221. /**
  222. * Get Name
  223. *
  224. * @return string
  225. */
  226. public function getName() {
  227. return $this->propertyGetBound()->_name;
  228. }
  229. /**
  230. * Set Name
  231. *
  232. * @param string $pValue
  233. */
  234. public function setName($pValue = 'Calibri') {
  235. if ($pValue == '') {
  236. $pValue = 'Calibri';
  237. }
  238. $this->propertyBeginBind()->_name = $pValue;
  239. }
  240. /**
  241. * Get Size
  242. *
  243. * @return double
  244. */
  245. public function getSize() {
  246. return $this->propertyGetBound()->_size;
  247. }
  248. /**
  249. * Set Size
  250. *
  251. * @param double $pValue
  252. */
  253. public function setSize($pValue = 10) {
  254. if ($pValue == '') {
  255. $pValue = 10;
  256. }
  257. $this->propertyBeginBind()->_size = $pValue;
  258. }
  259. /**
  260. * Get Bold
  261. *
  262. * @return boolean
  263. */
  264. public function getBold() {
  265. return $this->propertyGetBound()->_bold;
  266. }
  267. /**
  268. * Set Bold
  269. *
  270. * @param boolean $pValue
  271. */
  272. public function setBold($pValue = false) {
  273. if ($pValue == '') {
  274. $pValue = false;
  275. }
  276. $this->propertyBeginBind()->_bold = $pValue;
  277. }
  278. /**
  279. * Get Italic
  280. *
  281. * @return boolean
  282. */
  283. public function getItalic() {
  284. return $this->propertyGetBound()->_italic;
  285. }
  286. /**
  287. * Set Italic
  288. *
  289. * @param boolean $pValue
  290. */
  291. public function setItalic($pValue = false) {
  292. if ($pValue == '') {
  293. $pValue = false;
  294. }
  295. $this->propertyBeginBind()->_italic = $pValue;
  296. }
  297. /**
  298. * Get SuperScript
  299. *
  300. * @return boolean
  301. */
  302. public function getSuperScript() {
  303. return $this->propertyGetBound()->_superScript;
  304. }
  305. /**
  306. * Set SuperScript
  307. *
  308. * @param boolean $pValue
  309. */
  310. public function setSuperScript($pValue = false) {
  311. if ($pValue == '') {
  312. $pValue = false;
  313. }
  314. $this->propertyBeginBind()->_superScript = $pValue;
  315. $this->propertyBeginBind()->_subScript = !$pValue;
  316. }
  317. /**
  318. * Get SubScript
  319. *
  320. * @return boolean
  321. */
  322. public function getSubScript() {
  323. return $this->propertyGetBound()->_subScript;
  324. }
  325. /**
  326. * Set SubScript
  327. *
  328. * @param boolean $pValue
  329. */
  330. public function setSubScript($pValue = false) {
  331. if ($pValue == '') {
  332. $pValue = false;
  333. }
  334. $this->propertyBeginBind()->_subScript = $pValue;
  335. $this->propertyBeginBind()->_superScript = !$pValue;
  336. }
  337. /**
  338. * Get Underline
  339. *
  340. * @return string
  341. */
  342. public function getUnderline() {
  343. return $this->propertyGetBound()->_underline;
  344. }
  345. /**
  346. * Set Underline
  347. *
  348. * @param string $pValue PHPExcel_Style_Font underline type
  349. */
  350. public function setUnderline($pValue = PHPExcel_Style_Font::UNDERLINE_NONE) {
  351. if ($pValue == '') {
  352. $pValue = PHPExcel_Style_Font::UNDERLINE_NONE;
  353. }
  354. $this->propertyBeginBind()->_underline = $pValue;
  355. }
  356. /**
  357. * Get Striketrough
  358. *
  359. * @deprecated Use getStrikethrough() instead.
  360. * @return boolean
  361. */
  362. public function getStriketrough() {
  363. return $this->getStrikethrough();
  364. }
  365. /**
  366. * Set Striketrough
  367. *
  368. * @deprecated Use setStrikethrough() instead.
  369. * @param boolean $pValue
  370. */
  371. public function setStriketrough($pValue = false) {
  372. $this->setStrikethrough($pValue);
  373. }
  374. /**
  375. * Get Strikethrough
  376. *
  377. * @return boolean
  378. */
  379. public function getStrikethrough() {
  380. return $this->propertyGetBound()->_strikethrough;
  381. }
  382. /**
  383. * Set Strikethrough
  384. *
  385. * @param boolean $pValue
  386. */
  387. public function setStrikethrough($pValue = false) {
  388. if ($pValue == '') {
  389. $pValue = false;
  390. }
  391. $this->propertyBeginBind()->_strikethrough = $pValue;
  392. }
  393. /**
  394. * Get Color
  395. *
  396. * @return PHPExcel_Style_Color
  397. */
  398. public function getColor() {
  399. // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  400. // So bind as an assurance.
  401. return $this->propertyBeginBind()->_color;
  402. }
  403. /**
  404. * Set Color
  405. *
  406. * @param PHPExcel_Style_Color $pValue
  407. * @throws Exception
  408. */
  409. public function setColor(PHPExcel_Style_Color $pValue = null) {
  410. $this->propertyBeginBind()->_color = $pValue;
  411. }
  412. /**
  413. * Get hash code
  414. *
  415. * @return string Hash code
  416. */
  417. public function getHashCode() {
  418. $property = $this->propertyGetBound();
  419. return md5(
  420. $property->_name
  421. . $property->_size
  422. . ($property->_bold ? 't' : 'f')
  423. . ($property->_italic ? 't' : 'f')
  424. . ($property->_superScript ? 't' : 'f')
  425. . ($property->_subScript ? 't' : 'f')
  426. . $property->_underline
  427. . ($property->_strikethrough ? 't' : 'f')
  428. . $property->_color->getHashCode()
  429. . __CLASS__
  430. );
  431. }
  432. /**
  433. * Hash index
  434. *
  435. * @var string
  436. */
  437. private $_hashIndex;
  438. /**
  439. * Get hash index
  440. *
  441. * Note that this index may vary during script execution! Only reliable moment is
  442. * while doing a write of a workbook and when changes are not allowed.
  443. *
  444. * @return string Hash index
  445. */
  446. public function getHashIndex() {
  447. return $this->_hashIndex;
  448. }
  449. /**
  450. * Set hash index
  451. *
  452. * Note that this index may vary during script execution! Only reliable moment is
  453. * while doing a write of a workbook and when changes are not allowed.
  454. *
  455. * @param string $value Hash index
  456. */
  457. public function setHashIndex($value) {
  458. $this->_hashIndex = $value;
  459. }
  460. /**
  461. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  462. */
  463. public function __clone() {
  464. $vars = get_object_vars($this);
  465. foreach ($vars as $key => $value) {
  466. if (is_object($value)) {
  467. $this->$key = clone $value;
  468. } else {
  469. $this->$key = $value;
  470. }
  471. }
  472. }
  473. }