Font.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Style_Font extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  8. {
  9. /* Underline types */
  10. const UNDERLINE_NONE = 'none';
  11. const UNDERLINE_DOUBLE = 'double';
  12. const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
  13. const UNDERLINE_SINGLE = 'single';
  14. const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
  15. /**
  16. * Font Name
  17. *
  18. * @var string
  19. */
  20. protected $name = 'Calibri';
  21. /**
  22. * Font Size
  23. *
  24. * @var float
  25. */
  26. protected $size = 11;
  27. /**
  28. * Bold
  29. *
  30. * @var boolean
  31. */
  32. protected $bold = false;
  33. /**
  34. * Italic
  35. *
  36. * @var boolean
  37. */
  38. protected $italic = false;
  39. /**
  40. * Superscript
  41. *
  42. * @var boolean
  43. */
  44. protected $superScript = false;
  45. /**
  46. * Subscript
  47. *
  48. * @var boolean
  49. */
  50. protected $subScript = false;
  51. /**
  52. * Underline
  53. *
  54. * @var string
  55. */
  56. protected $underline = self::UNDERLINE_NONE;
  57. /**
  58. * Strikethrough
  59. *
  60. * @var boolean
  61. */
  62. protected $strikethrough = false;
  63. /**
  64. * Foreground color
  65. *
  66. * @var PHPExcel_Style_Color
  67. */
  68. protected $color;
  69. /**
  70. * Create a new PHPExcel_Style_Font
  71. *
  72. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  73. * Leave this value at default unless you understand exactly what
  74. * its ramifications are
  75. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  76. * Leave this value at default unless you understand exactly what
  77. * its ramifications are
  78. */
  79. public function __construct($isSupervisor = false, $isConditional = false)
  80. {
  81. // Supervisor?
  82. parent::__construct($isSupervisor);
  83. // Initialise values
  84. if ($isConditional) {
  85. $this->name = null;
  86. $this->size = null;
  87. $this->bold = null;
  88. $this->italic = null;
  89. $this->superScript = null;
  90. $this->subScript = null;
  91. $this->underline = null;
  92. $this->strikethrough = null;
  93. $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
  94. } else {
  95. $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  96. }
  97. // bind parent if we are a supervisor
  98. if ($isSupervisor) {
  99. $this->color->bindParent($this, 'color');
  100. }
  101. }
  102. /**
  103. * Get the shared style component for the currently active cell in currently active sheet.
  104. * Only used for style supervisor
  105. *
  106. * @return PHPExcel_Style_Font
  107. */
  108. public function getSharedComponent()
  109. {
  110. return $this->parent->getSharedComponent()->getFont();
  111. }
  112. /**
  113. * Build style array from subcomponents
  114. *
  115. * @param array $array
  116. * @return array
  117. */
  118. public function getStyleArray($array)
  119. {
  120. return array('font' => $array);
  121. }
  122. /**
  123. * Apply styles from array
  124. *
  125. * <code>
  126. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  127. * array(
  128. * 'name' => 'Arial',
  129. * 'bold' => TRUE,
  130. * 'italic' => FALSE,
  131. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  132. * 'strike' => FALSE,
  133. * 'color' => array(
  134. * 'rgb' => '808080'
  135. * )
  136. * )
  137. * );
  138. * </code>
  139. *
  140. * @param array $pStyles Array containing style information
  141. * @throws PHPExcel_Exception
  142. * @return PHPExcel_Style_Font
  143. */
  144. public function applyFromArray($pStyles = null)
  145. {
  146. if (is_array($pStyles)) {
  147. if ($this->isSupervisor) {
  148. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  149. } else {
  150. if (array_key_exists('name', $pStyles)) {
  151. $this->setName($pStyles['name']);
  152. }
  153. if (array_key_exists('bold', $pStyles)) {
  154. $this->setBold($pStyles['bold']);
  155. }
  156. if (array_key_exists('italic', $pStyles)) {
  157. $this->setItalic($pStyles['italic']);
  158. }
  159. if (array_key_exists('superScript', $pStyles)) {
  160. $this->setSuperScript($pStyles['superScript']);
  161. }
  162. if (array_key_exists('subScript', $pStyles)) {
  163. $this->setSubScript($pStyles['subScript']);
  164. }
  165. if (array_key_exists('underline', $pStyles)) {
  166. $this->setUnderline($pStyles['underline']);
  167. }
  168. if (array_key_exists('strike', $pStyles)) {
  169. $this->setStrikethrough($pStyles['strike']);
  170. }
  171. if (array_key_exists('color', $pStyles)) {
  172. $this->getColor()->applyFromArray($pStyles['color']);
  173. }
  174. if (array_key_exists('size', $pStyles)) {
  175. $this->setSize($pStyles['size']);
  176. }
  177. }
  178. } else {
  179. throw new PHPExcel_Exception("Invalid style array passed.");
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Get Name
  185. *
  186. * @return string
  187. */
  188. public function getName()
  189. {
  190. if ($this->isSupervisor) {
  191. return $this->getSharedComponent()->getName();
  192. }
  193. return $this->name;
  194. }
  195. /**
  196. * Set Name
  197. *
  198. * @param string $pValue
  199. * @return PHPExcel_Style_Font
  200. */
  201. public function setName($pValue = 'Calibri')
  202. {
  203. if ($pValue == '') {
  204. $pValue = 'Calibri';
  205. }
  206. if ($this->isSupervisor) {
  207. $styleArray = $this->getStyleArray(array('name' => $pValue));
  208. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  209. } else {
  210. $this->name = $pValue;
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Get Size
  216. *
  217. * @return double
  218. */
  219. public function getSize()
  220. {
  221. if ($this->isSupervisor) {
  222. return $this->getSharedComponent()->getSize();
  223. }
  224. return $this->size;
  225. }
  226. /**
  227. * Set Size
  228. *
  229. * @param double $pValue
  230. * @return PHPExcel_Style_Font
  231. */
  232. public function setSize($pValue = 10)
  233. {
  234. if ($pValue == '') {
  235. $pValue = 10;
  236. }
  237. if ($this->isSupervisor) {
  238. $styleArray = $this->getStyleArray(array('size' => $pValue));
  239. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  240. } else {
  241. $this->size = $pValue;
  242. }
  243. return $this;
  244. }
  245. /**
  246. * Get Bold
  247. *
  248. * @return boolean
  249. */
  250. public function getBold()
  251. {
  252. if ($this->isSupervisor) {
  253. return $this->getSharedComponent()->getBold();
  254. }
  255. return $this->bold;
  256. }
  257. /**
  258. * Set Bold
  259. *
  260. * @param boolean $pValue
  261. * @return PHPExcel_Style_Font
  262. */
  263. public function setBold($pValue = false)
  264. {
  265. if ($pValue == '') {
  266. $pValue = false;
  267. }
  268. if ($this->isSupervisor) {
  269. $styleArray = $this->getStyleArray(array('bold' => $pValue));
  270. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  271. } else {
  272. $this->bold = $pValue;
  273. }
  274. return $this;
  275. }
  276. /**
  277. * Get Italic
  278. *
  279. * @return boolean
  280. */
  281. public function getItalic()
  282. {
  283. if ($this->isSupervisor) {
  284. return $this->getSharedComponent()->getItalic();
  285. }
  286. return $this->italic;
  287. }
  288. /**
  289. * Set Italic
  290. *
  291. * @param boolean $pValue
  292. * @return PHPExcel_Style_Font
  293. */
  294. public function setItalic($pValue = false)
  295. {
  296. if ($pValue == '') {
  297. $pValue = false;
  298. }
  299. if ($this->isSupervisor) {
  300. $styleArray = $this->getStyleArray(array('italic' => $pValue));
  301. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  302. } else {
  303. $this->italic = $pValue;
  304. }
  305. return $this;
  306. }
  307. /**
  308. * Get SuperScript
  309. *
  310. * @return boolean
  311. */
  312. public function getSuperScript()
  313. {
  314. if ($this->isSupervisor) {
  315. return $this->getSharedComponent()->getSuperScript();
  316. }
  317. return $this->superScript;
  318. }
  319. /**
  320. * Set SuperScript
  321. *
  322. * @param boolean $pValue
  323. * @return PHPExcel_Style_Font
  324. */
  325. public function setSuperScript($pValue = false)
  326. {
  327. if ($pValue == '') {
  328. $pValue = false;
  329. }
  330. if ($this->isSupervisor) {
  331. $styleArray = $this->getStyleArray(array('superScript' => $pValue));
  332. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  333. } else {
  334. $this->superScript = $pValue;
  335. $this->subScript = !$pValue;
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Get SubScript
  341. *
  342. * @return boolean
  343. */
  344. public function getSubScript()
  345. {
  346. if ($this->isSupervisor) {
  347. return $this->getSharedComponent()->getSubScript();
  348. }
  349. return $this->subScript;
  350. }
  351. /**
  352. * Set SubScript
  353. *
  354. * @param boolean $pValue
  355. * @return PHPExcel_Style_Font
  356. */
  357. public function setSubScript($pValue = false)
  358. {
  359. if ($pValue == '') {
  360. $pValue = false;
  361. }
  362. if ($this->isSupervisor) {
  363. $styleArray = $this->getStyleArray(array('subScript' => $pValue));
  364. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  365. } else {
  366. $this->subScript = $pValue;
  367. $this->superScript = !$pValue;
  368. }
  369. return $this;
  370. }
  371. /**
  372. * Get Underline
  373. *
  374. * @return string
  375. */
  376. public function getUnderline()
  377. {
  378. if ($this->isSupervisor) {
  379. return $this->getSharedComponent()->getUnderline();
  380. }
  381. return $this->underline;
  382. }
  383. /**
  384. * Set Underline
  385. *
  386. * @param string|boolean $pValue PHPExcel_Style_Font underline type
  387. * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
  388. * false equates to UNDERLINE_NONE
  389. * @return PHPExcel_Style_Font
  390. */
  391. public function setUnderline($pValue = self::UNDERLINE_NONE)
  392. {
  393. if (is_bool($pValue)) {
  394. $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
  395. } elseif ($pValue == '') {
  396. $pValue = self::UNDERLINE_NONE;
  397. }
  398. if ($this->isSupervisor) {
  399. $styleArray = $this->getStyleArray(array('underline' => $pValue));
  400. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  401. } else {
  402. $this->underline = $pValue;
  403. }
  404. return $this;
  405. }
  406. /**
  407. * Get Strikethrough
  408. *
  409. * @return boolean
  410. */
  411. public function getStrikethrough()
  412. {
  413. if ($this->isSupervisor) {
  414. return $this->getSharedComponent()->getStrikethrough();
  415. }
  416. return $this->strikethrough;
  417. }
  418. /**
  419. * Set Strikethrough
  420. *
  421. * @param boolean $pValue
  422. * @return PHPExcel_Style_Font
  423. */
  424. public function setStrikethrough($pValue = false)
  425. {
  426. if ($pValue == '') {
  427. $pValue = false;
  428. }
  429. if ($this->isSupervisor) {
  430. $styleArray = $this->getStyleArray(array('strike' => $pValue));
  431. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  432. } else {
  433. $this->strikethrough = $pValue;
  434. }
  435. return $this;
  436. }
  437. /**
  438. * Get Color
  439. *
  440. * @return PHPExcel_Style_Color
  441. */
  442. public function getColor()
  443. {
  444. return $this->color;
  445. }
  446. /**
  447. * Set Color
  448. *
  449. * @param PHPExcel_Style_Color $pValue
  450. * @throws PHPExcel_Exception
  451. * @return PHPExcel_Style_Font
  452. */
  453. public function setColor(PHPExcel_Style_Color $pValue = null)
  454. {
  455. // make sure parameter is a real color and not a supervisor
  456. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  457. if ($this->isSupervisor) {
  458. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  459. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  460. } else {
  461. $this->color = $color;
  462. }
  463. return $this;
  464. }
  465. /**
  466. * Get hash code
  467. *
  468. * @return string Hash code
  469. */
  470. public function getHashCode()
  471. {
  472. if ($this->isSupervisor) {
  473. return $this->getSharedComponent()->getHashCode();
  474. }
  475. return md5(
  476. $this->name .
  477. $this->size .
  478. ($this->bold ? 't' : 'f') .
  479. ($this->italic ? 't' : 'f') .
  480. ($this->superScript ? 't' : 'f') .
  481. ($this->subScript ? 't' : 'f') .
  482. $this->underline .
  483. ($this->strikethrough ? 't' : 'f') .
  484. $this->color->getHashCode() .
  485. __CLASS__
  486. );
  487. }
  488. }