PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 334 lines | 136 code | 29 blank | 169 comment | 23 complexity | e476ff7a6b334ad80b48e285c949a2d9 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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.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 - 2008 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. * Underline
  66. *
  67. * @var string
  68. */
  69. private $_underline;
  70. /**
  71. * Striketrough
  72. *
  73. * @var boolean
  74. */
  75. private $_striketrough;
  76. /**
  77. * Foreground color
  78. *
  79. * @var PHPExcel_Style_Color
  80. */
  81. private $_color;
  82. /**
  83. * Create a new PHPExcel_Style_Font
  84. */
  85. public function __construct()
  86. {
  87. // Initialise values
  88. $this->_name = 'Calibri';
  89. $this->_size = 10;
  90. $this->_bold = false;
  91. $this->_italic = false;
  92. $this->_underline = PHPExcel_Style_Font::UNDERLINE_NONE;
  93. $this->_striketrough = false;
  94. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  95. }
  96. /**
  97. * Apply styles from array
  98. *
  99. * <code>
  100. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  101. * array(
  102. * 'name' => 'Arial',
  103. * 'bold' => true,
  104. * 'italic' => false,
  105. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  106. * 'strike' => false,
  107. * 'color' => array(
  108. * 'rgb' => '808080'
  109. * )
  110. * )
  111. * );
  112. * </code>
  113. *
  114. * @param array $pStyles Array containing style information
  115. * @throws Exception
  116. */
  117. public function applyFromArray($pStyles = null) {
  118. if (is_array($pStyles)) {
  119. if (array_key_exists('name', $pStyles)) {
  120. $this->setName($pStyles['name']);
  121. }
  122. if (array_key_exists('bold', $pStyles)) {
  123. $this->setBold($pStyles['bold']);
  124. }
  125. if (array_key_exists('italic', $pStyles)) {
  126. $this->setItalic($pStyles['italic']);
  127. }
  128. if (array_key_exists('underline', $pStyles)) {
  129. $this->setUnderline($pStyles['underline']);
  130. }
  131. if (array_key_exists('strike', $pStyles)) {
  132. $this->setStriketrough($pStyles['strike']);
  133. }
  134. if (array_key_exists('color', $pStyles)) {
  135. $this->getColor()->applyFromArray($pStyles['color']);
  136. }
  137. if (array_key_exists('size', $pStyles)) {
  138. $this->setSize($pStyles['size']);
  139. }
  140. } else {
  141. throw new Exception("Invalid style array passed.");
  142. }
  143. }
  144. /**
  145. * Get Name
  146. *
  147. * @return string
  148. */
  149. public function getName() {
  150. return $this->_name;
  151. }
  152. /**
  153. * Set Name
  154. *
  155. * @param string $pValue
  156. */
  157. public function setName($pValue = 'Calibri') {
  158. if ($pValue == '') {
  159. $pValue = 'Calibri';
  160. }
  161. $this->_name = $pValue;
  162. }
  163. /**
  164. * Get Size
  165. *
  166. * @return double
  167. */
  168. public function getSize() {
  169. return $this->_size;
  170. }
  171. /**
  172. * Set Size
  173. *
  174. * @param double $pValue
  175. */
  176. public function setSize($pValue = 10) {
  177. if ($pValue == '') {
  178. $pValue = 10;
  179. }
  180. $this->_size = $pValue;
  181. }
  182. /**
  183. * Get Bold
  184. *
  185. * @return boolean
  186. */
  187. public function getBold() {
  188. return $this->_bold;
  189. }
  190. /**
  191. * Set Bold
  192. *
  193. * @param boolean $pValue
  194. */
  195. public function setBold($pValue = false) {
  196. if ($pValue == '') {
  197. $pValue = false;
  198. }
  199. $this->_bold = $pValue;
  200. }
  201. /**
  202. * Get Italic
  203. *
  204. * @return boolean
  205. */
  206. public function getItalic() {
  207. return $this->_italic;
  208. }
  209. /**
  210. * Set Italic
  211. *
  212. * @param boolean $pValue
  213. */
  214. public function setItalic($pValue = false) {
  215. if ($pValue == '') {
  216. $pValue = false;
  217. }
  218. $this->_italic = $pValue;
  219. }
  220. /**
  221. * Get Underline
  222. *
  223. * @return string
  224. */
  225. public function getUnderline() {
  226. return $this->_underline;
  227. }
  228. /**
  229. * Set Underline
  230. *
  231. * @param string $pValue PHPExcel_Style_Font underline type
  232. */
  233. public function setUnderline($pValue = PHPExcel_Style_Font::UNDERLINE_NONE) {
  234. if ($pValue == '') {
  235. $pValue = PHPExcel_Style_Font::UNDERLINE_NONE;
  236. }
  237. $this->_underline = $pValue;
  238. }
  239. /**
  240. * Set Striketrough
  241. *
  242. * @param boolean $pValue
  243. */
  244. public function setStriketrough($pValue = false) {
  245. if ($pValue == '') {
  246. $pValue = false;
  247. }
  248. $this->_striketrough = $pValue;
  249. }
  250. /**
  251. * Get Striketrough
  252. *
  253. * @return boolean
  254. */
  255. public function getStriketrough() {
  256. return $this->_striketrough;
  257. }
  258. /**
  259. * Get Color
  260. *
  261. * @return PHPExcel_Style_Color
  262. */
  263. public function getColor() {
  264. return $this->_color;
  265. }
  266. /**
  267. * Set Color
  268. *
  269. * @param PHPExcel_Style_Color $pValue
  270. * @throws Exception
  271. */
  272. public function setColor(PHPExcel_Style_Color $pValue = null) {
  273. $this->_color = $pValue;
  274. }
  275. /**
  276. * Get hash code
  277. *
  278. * @return string Hash code
  279. */
  280. public function getHashCode() {
  281. return md5(
  282. $this->_name
  283. . $this->_size
  284. . ($this->_bold ? 't' : 'f')
  285. . ($this->_italic ? 't' : 'f')
  286. . $this->_underline
  287. . ($this->_striketrough ? 't' : 'f')
  288. . $this->_color->getHashCode()
  289. . __CLASS__
  290. );
  291. }
  292. /**
  293. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  294. */
  295. public function __clone() {
  296. $vars = get_object_vars($this);
  297. foreach ($vars as $key => $value) {
  298. if (is_object($value)) {
  299. $this->$key = clone $value;
  300. } else {
  301. $this->$key = $value;
  302. }
  303. }
  304. }
  305. }