MDL-33399 files: Fixed up handling of empty sort in File API methods.
[moodle.git] / lib / excel / Format.php
blob76331ba490b302d38c4382aee16e258d07edc223
1 <?php
2 /*
3 * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
5 * The majority of this is _NOT_ my code. I simply ported it from the
6 * PERL Spreadsheet::WriteExcel module.
8 * The author of the Spreadsheet::WriteExcel module is John McNamara
9 * <jmcnamara@cpan.org>
11 * I _DO_ maintain this code, and John McNamara has nothing to do with the
12 * porting of this code to PHP. Any questions directly related to this
13 * class library should be directed to me.
15 * License Information:
17 * Spreadsheet::WriteExcel: A library for generating Excel Spreadsheets
18 * Copyright (C) 2002 Xavier Noguer xnoguer@rezebra.com
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 /**
36 * Class for generating Excel XF records (formats)
38 * @author Xavier Noguer <xnoguer@rezebra.com>
39 * @package Spreadsheet_WriteExcel
42 class Format
44 /**
45 * Constructor
47 * @access public
48 * @param integer $index the XF index for the format.
49 * @param array $properties array with properties to be set on initialization.
51 function Format($index = 0,$properties = array())
53 $this->xf_index = $index;
55 $this->font_index = 0;
56 $this->font = 'Arial';
57 $this->size = 10;
58 $this->bold = 0x0190;
59 $this->_italic = 0;
60 $this->color = 0x7FFF;
61 $this->_underline = 0;
62 $this->font_strikeout = 0;
63 $this->font_outline = 0;
64 $this->font_shadow = 0;
65 $this->font_script = 0;
66 $this->font_family = 0;
67 $this->font_charset = 0;
69 $this->_num_format = 0;
71 $this->hidden = 0;
72 $this->locked = 1;
74 $this->_text_h_align = 0;
75 $this->_text_wrap = 0;
76 $this->text_v_align = 2;
77 $this->text_justlast = 0;
78 $this->rotation = 0;
80 $this->fg_color = 0x40;
81 $this->bg_color = 0x41;
83 $this->pattern = 0;
85 $this->bottom = 0;
86 $this->top = 0;
87 $this->left = 0;
88 $this->right = 0;
90 $this->bottom_color = 0x40;
91 $this->top_color = 0x40;
92 $this->left_color = 0x40;
93 $this->right_color = 0x40;
95 // Set properties passed to Workbook::add_format()
96 foreach($properties as $property => $value)
98 if(method_exists($this,"set_$property"))
100 $aux = 'set_'.$property;
101 $this->$aux($value);
107 * Generate an Excel BIFF XF record (style or cell).
109 * @param string $style The type of the XF record ('style' or 'cell').
110 * @return string The XF record
112 function get_xf($style)
114 // Set the type of the XF record and some of the attributes.
115 if ($style == "style") {
116 $style = 0xFFF5;
118 else {
119 $style = $this->locked;
120 $style |= $this->hidden << 1;
123 // Flags to indicate if attributes have been set.
124 $atr_num = ($this->_num_format != 0)?1:0;
125 $atr_fnt = ($this->font_index != 0)?1:0;
126 $atr_alc = ($this->_text_wrap)?1:0;
127 $atr_bdr = ($this->bottom ||
128 $this->top ||
129 $this->left ||
130 $this->right)?1:0;
131 $atr_pat = (($this->fg_color != 0x40) ||
132 ($this->bg_color != 0x41) ||
133 $this->pattern)?1:0;
134 $atr_prot = 0;
136 // Zero the default border colour if the border has not been set.
137 if ($this->bottom == 0) {
138 $this->bottom_color = 0;
140 if ($this->top == 0) {
141 $this->top_color = 0;
143 if ($this->right == 0) {
144 $this->right_color = 0;
146 if ($this->left == 0) {
147 $this->left_color = 0;
150 $record = 0x00E0; // Record identifier
151 $length = 0x0010; // Number of bytes to follow
153 $ifnt = $this->font_index; // Index to FONT record
154 $ifmt = $this->_num_format; // Index to FORMAT record
156 $align = $this->_text_h_align; // Alignment
157 $align |= $this->_text_wrap << 3;
158 $align |= $this->text_v_align << 4;
159 $align |= $this->text_justlast << 7;
160 $align |= $this->rotation << 8;
161 $align |= $atr_num << 10;
162 $align |= $atr_fnt << 11;
163 $align |= $atr_alc << 12;
164 $align |= $atr_bdr << 13;
165 $align |= $atr_pat << 14;
166 $align |= $atr_prot << 15;
168 $icv = $this->fg_color; // fg and bg pattern colors
169 $icv |= $this->bg_color << 7;
171 $fill = $this->pattern; // Fill and border line style
172 $fill |= $this->bottom << 6;
173 $fill |= $this->bottom_color << 9;
175 $border1 = $this->top; // Border line style and color
176 $border1 |= $this->left << 3;
177 $border1 |= $this->right << 6;
178 $border1 |= $this->top_color << 9;
180 $border2 = $this->left_color; // Border color
181 $border2 |= $this->right_color << 7;
183 $header = pack("vv", $record, $length);
184 $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
185 $icv, $fill,
186 $border1, $border2);
187 return($header.$data);
191 * Generate an Excel BIFF FONT record.
193 * @see Workbook::_store_all_fonts()
194 * @return string The FONT record
196 function get_font()
198 $dyHeight = $this->size * 20; // Height of font (1/20 of a point)
199 $icv = $this->color; // Index to color palette
200 $bls = $this->bold; // Bold style
201 $sss = $this->font_script; // Superscript/subscript
202 $uls = $this->_underline; // Underline
203 $bFamily = $this->font_family; // Font family
204 $bCharSet = $this->font_charset; // Character set
205 $rgch = $this->font; // Font name
207 $cch = strlen($rgch); // Length of font name
208 $record = 0x31; // Record identifier
209 $length = 0x0F + $cch; // Record length
210 $reserved = 0x00; // Reserved
211 $grbit = 0x00; // Font attributes
212 if ($this->_italic) {
213 $grbit |= 0x02;
215 if ($this->font_strikeout) {
216 $grbit |= 0x08;
218 if ($this->font_outline) {
219 $grbit |= 0x10;
221 if ($this->font_shadow) {
222 $grbit |= 0x20;
225 $header = pack("vv", $record, $length);
226 $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
227 $sss, $uls, $bFamily,
228 $bCharSet, $reserved, $cch);
229 return($header . $data. $this->font);
233 * Returns a unique hash key for a font. Used by Workbook->_store_all_fonts()
235 * The elements that form the key are arranged to increase the probability of
236 * generating a unique key. Elements that hold a large range of numbers
237 * (eg. _color) are placed between two binary elements such as _italic
239 * @return string A key for this font
241 function get_font_key()
243 $key = "$this->font$this->size";
244 $key .= "$this->font_script$this->_underline";
245 $key .= "$this->font_strikeout$this->bold$this->font_outline";
246 $key .= "$this->font_family$this->font_charset";
247 $key .= "$this->font_shadow$this->color$this->_italic";
248 $key = str_replace(" ","_",$key);
249 return ($key);
253 * Returns the index used by Worksheet->_XF()
255 * @return integer The index for the XF record
257 function get_xf_index()
259 return($this->xf_index);
263 * Used in conjunction with the set_xxx_color methods to convert a color
264 * string into a number. Color range is 0..63 but we will restrict it
265 * to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
267 * @param string $name_color name of the color (i.e.: 'blue', 'red', etc..). Optional.
268 * @return integer The color index
270 function _get_color($name_color = '')
272 $colors = array(
273 'aqua' => 0x0F,
274 'cyan' => 0x0F,
275 'black' => 0x08,
276 'blue' => 0x0C,
277 'brown' => 0x10,
278 'magenta' => 0x0E,
279 'fuchsia' => 0x0E,
280 'gray' => 0x17,
281 'grey' => 0x17,
282 'green' => 0x11,
283 'lime' => 0x0B,
284 'navy' => 0x12,
285 'orange' => 0x35,
286 'purple' => 0x14,
287 'red' => 0x0A,
288 'silver' => 0x16,
289 'white' => 0x09,
290 'yellow' => 0x0D
293 // Return the default color, 0x7FFF, if undef,
294 if($name_color == '') {
295 return(0x7FFF);
298 // or the color string converted to an integer,
299 if(isset($colors[$name_color])) {
300 return($colors[$name_color]);
303 // or the default color if string is unrecognised,
304 if(preg_match("/\D/",$name_color)) {
305 return(0x7FFF);
308 // or an index < 8 mapped into the correct range,
309 if($name_color < 8) {
310 return($name_color + 8);
313 // or the default color if arg is outside range,
314 if($name_color > 63) {
315 return(0x7FFF);
318 // or an integer in the valid range
319 return($name_color);
323 * Set cell alignment.
325 * @access public
326 * @param string $location alignment for the cell ('left', 'right', etc...).
328 function set_align($location)
330 if (preg_match("/\d/",$location)) {
331 return; // Ignore numbers
334 $location = strtolower($location);
336 if ($location == 'left')
337 $this->_text_h_align = 1;
338 if ($location == 'centre')
339 $this->_text_h_align = 2;
340 if ($location == 'center')
341 $this->_text_h_align = 2;
342 if ($location == 'right')
343 $this->_text_h_align = 3;
344 if ($location == 'fill')
345 $this->_text_h_align = 4;
346 if ($location == 'justify')
347 $this->_text_h_align = 5;
348 if ($location == 'merge')
349 $this->_text_h_align = 6;
350 if ($location == 'equal_space') // For T.K.
351 $this->_text_h_align = 7;
352 if ($location == 'top')
353 $this->text_v_align = 0;
354 if ($location == 'vcentre')
355 $this->text_v_align = 1;
356 if ($location == 'vcenter')
357 $this->text_v_align = 1;
358 if ($location == 'bottom')
359 $this->text_v_align = 2;
360 if ($location == 'vjustify')
361 $this->text_v_align = 3;
362 if ($location == 'vequal_space') // For T.K.
363 $this->text_v_align = 4;
367 * This is an alias for the unintuitive set_align('merge')
369 * @access public
371 function set_merge()
373 $this->set_align('merge');
377 * Bold has a range 0x64..0x3E8.
378 * 0x190 is normal. 0x2BC is bold.
380 * @access public
381 * @param integer $weight Weight for the text, 0 maps to 0x190, 1 maps to 0x2BC.
382 It's Optional, default is 1 (bold).
384 function set_bold($weight = 1)
386 if($weight == 1) {
387 $weight = 0x2BC; // Bold text
389 if($weight == 0) {
390 $weight = 0x190; // Normal text
392 if($weight < 0x064) {
393 $weight = 0x190; // Lower bound
395 if($weight > 0x3E8) {
396 $weight = 0x190; // Upper bound
398 $this->bold = $weight;
402 /************************************
403 * FUNCTIONS FOR SETTING CELLS BORDERS
407 * Sets the bottom border of the cell
409 * @access public
410 * @param integer $style style of the cell border. 1 => thin, 2 => thick.
412 function set_bottom($style)
414 $this->bottom = $style;
418 * Sets the top border of the cell
420 * @access public
421 * @param integer $style style of the cell top border. 1 => thin, 2 => thick.
423 function set_top($style)
425 $this->top = $style;
429 * Sets the left border of the cell
431 * @access public
432 * @param integer $style style of the cell left border. 1 => thin, 2 => thick.
434 function set_left($style)
436 $this->left = $style;
440 * Sets the right border of the cell
442 * @access public
443 * @param integer $style style of the cell right border. 1 => thin, 2 => thick.
445 function set_right($style)
447 $this->right = $style;
452 * Set cells borders to the same style
454 * @access public
455 * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
457 function set_border($style)
459 $this->set_bottom($style);
460 $this->set_top($style);
461 $this->set_left($style);
462 $this->set_right($style);
466 /*******************************************
467 * FUNCTIONS FOR SETTING CELLS BORDERS COLORS
471 * Sets all the cell's borders to the same color
473 * @access public
474 * @param mixed $color The color we are setting. Either a string (like 'blue'),
475 * or an integer (like 0x41).
477 function set_border_color($color)
479 $this->set_bottom_color($color);
480 $this->set_top_color($color);
481 $this->set_left_color($color);
482 $this->set_right_color($color);
486 * Sets the cell's bottom border color
488 * @access public
489 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
491 function set_bottom_color($color)
493 $value = $this->_get_color($color);
494 $this->bottom_color = $value;
498 * Sets the cell's top border color
500 * @access public
501 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
503 function set_top_color($color)
505 $value = $this->_get_color($color);
506 $this->top_color = $value;
510 * Sets the cell's left border color
512 * @access public
513 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
515 function set_left_color($color)
517 $value = $this->_get_color($color);
518 $this->left_color = $value;
522 * Sets the cell's right border color
524 * @access public
525 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
527 function set_right_color($color)
529 $value = $this->_get_color($color);
530 $this->right_color = $value;
535 * Sets the cell's foreground color
537 * @access public
538 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
540 function set_fg_color($color)
542 $value = $this->_get_color($color);
543 $this->fg_color = $value;
547 * Sets the cell's background color
549 * @access public
550 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
552 function set_bg_color($color)
554 $value = $this->_get_color($color);
555 $this->bg_color = $value;
559 * Sets the cell's color
561 * @access public
562 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
564 function set_color($color)
566 $value = $this->_get_color($color);
567 $this->color = $value;
571 * Sets the pattern attribute of a cell
573 * @access public
574 * @param integer $arg Optional. Defaults to 1.
576 function set_pattern($arg = 1)
578 $this->pattern = $arg;
582 * Sets the underline of the text
584 * @access public
585 * @param integer $underline The value for underline. Possible values are:
586 * 1 => underline, 2 => double underline.
588 function set_underline($underline)
590 $this->_underline = $underline;
594 * Sets the font style as italic
596 * @access public
598 function set_italic()
600 $this->_italic = 1;
604 * Sets the font size
606 * @access public
607 * @param integer $size The font size (in pixels I think).
609 function set_size($size)
611 $this->size = $size;
615 * Sets the num format
617 * @access public
618 * @param integer $num_format The num format.
620 function set_num_format($num_format)
622 $this->_num_format = $num_format;
626 * Sets text wrapping
628 * @access public
629 * @param integer $text_wrap Optional. 0 => no text wrapping, 1 => text wrapping.
630 * Defaults to 1.
632 function set_text_wrap($text_wrap = 1)
634 $this->_text_wrap = $text_wrap;