source: sourcecode/application/libraries/PHPExcel/Style/Font.php @ 1

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