PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
PHP | 504 lines | 199 code | 49 blank | 256 comment | 33 complexity | 54b1acc200cce8372facdc251ab0683f 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 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->_striketrough = 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->setStriketrough($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. * @return boolean
  360. */
  361. public function getStriketrough() {
  362. return $this->propertyGetBound()->_striketrough;
  363. }
  364. /**
  365. * Set Striketrough
  366. *
  367. * @param boolean $pValue
  368. */
  369. public function setStriketrough($pValue = false) {
  370. if ($pValue == '') {
  371. $pValue = false;
  372. }
  373. $this->propertyBeginBind()->_striketrough = $pValue;
  374. }
  375. /**
  376. * Get Color
  377. *
  378. * @return PHPExcel_Style_Color
  379. */
  380. public function getColor() {
  381. // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  382. // So bind as an assurance.
  383. return $this->propertyBeginBind()->_color;
  384. }
  385. /**
  386. * Set Color
  387. *
  388. * @param PHPExcel_Style_Color $pValue
  389. * @throws Exception
  390. */
  391. public function setColor(PHPExcel_Style_Color $pValue = null) {
  392. $this->propertyBeginBind()->_color = $pValue;
  393. }
  394. /**
  395. * Get hash code
  396. *
  397. * @return string Hash code
  398. */
  399. public function getHashCode() {
  400. $property = $this->propertyGetBound();
  401. return md5(
  402. $property->_name
  403. . $property->_size
  404. . ($property->_bold ? 't' : 'f')
  405. . ($property->_italic ? 't' : 'f')
  406. . ($property->_superScript ? 't' : 'f')
  407. . ($property->_subScript ? 't' : 'f')
  408. . $property->_underline
  409. . ($property->_striketrough ? 't' : 'f')
  410. . $property->_color->getHashCode()
  411. . __CLASS__
  412. );
  413. }
  414. /**
  415. * Hash index
  416. *
  417. * @var string
  418. */
  419. private $_hashIndex;
  420. /**
  421. * Get hash index
  422. *
  423. * Note that this index may vary during script execution! Only reliable moment is
  424. * while doing a write of a workbook and when changes are not allowed.
  425. *
  426. * @return string Hash index
  427. */
  428. public function getHashIndex() {
  429. return $this->_hashIndex;
  430. }
  431. /**
  432. * Set hash index
  433. *
  434. * Note that this index may vary during script execution! Only reliable moment is
  435. * while doing a write of a workbook and when changes are not allowed.
  436. *
  437. * @param string $value Hash index
  438. */
  439. public function setHashIndex($value) {
  440. $this->_hashIndex = $value;
  441. }
  442. /**
  443. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  444. */
  445. public function __clone() {
  446. $vars = get_object_vars($this);
  447. foreach ($vars as $key => $value) {
  448. if (is_object($value)) {
  449. $this->$key = clone $value;
  450. } else {
  451. $this->$key = $value;
  452. }
  453. }
  454. }
  455. }