|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * Module written/ported by Xavier Noguer <xnoguer@rezebra.com> 00004 * 00005 * The majority of this is _NOT_ my code. I simply ported it from the 00006 * PERL Spreadsheet::WriteExcel module. 00007 * 00008 * The author of the Spreadsheet::WriteExcel module is John McNamara 00009 * <jmcnamara@cpan.org> 00010 * 00011 * I _DO_ maintain this code, and John McNamara has nothing to do with the 00012 * porting of this code to PHP. Any questions directly related to this 00013 * class library should be directed to me. 00014 * 00015 * License Information: 00016 * 00017 * Spreadsheet::WriteExcel: A library for generating Excel Spreadsheets 00018 * Copyright (C) 2002 Xavier Noguer xnoguer@rezebra.com 00019 * 00020 * This library is free software; you can redistribute it and/or 00021 * modify it under the terms of the GNU Lesser General Public 00022 * License as published by the Free Software Foundation; either 00023 * version 2.1 of the License, or (at your option) any later version. 00024 * 00025 * This library is distributed in the hope that it will be useful, 00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00028 * Lesser General Public License for more details. 00029 * 00030 * You should have received a copy of the GNU Lesser General Public 00031 * License along with this library; if not, write to the Free Software 00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00033 */ 00034 00042 class Format 00043 { 00051 function Format($index = 0,$properties = array()) 00052 { 00053 $this->xf_index = $index; 00054 00055 $this->font_index = 0; 00056 $this->font = 'Arial'; 00057 $this->size = 10; 00058 $this->bold = 0x0190; 00059 $this->_italic = 0; 00060 $this->color = 0x7FFF; 00061 $this->_underline = 0; 00062 $this->font_strikeout = 0; 00063 $this->font_outline = 0; 00064 $this->font_shadow = 0; 00065 $this->font_script = 0; 00066 $this->font_family = 0; 00067 $this->font_charset = 0; 00068 00069 $this->_num_format = 0; 00070 00071 $this->hidden = 0; 00072 $this->locked = 1; 00073 00074 $this->_text_h_align = 0; 00075 $this->_text_wrap = 0; 00076 $this->text_v_align = 2; 00077 $this->text_justlast = 0; 00078 $this->rotation = 0; 00079 00080 $this->fg_color = 0x40; 00081 $this->bg_color = 0x41; 00082 00083 $this->pattern = 0; 00084 00085 $this->bottom = 0; 00086 $this->top = 0; 00087 $this->left = 0; 00088 $this->right = 0; 00089 00090 $this->bottom_color = 0x40; 00091 $this->top_color = 0x40; 00092 $this->left_color = 0x40; 00093 $this->right_color = 0x40; 00094 00095 // Set properties passed to Workbook::add_format() 00096 foreach($properties as $property => $value) 00097 { 00098 if(method_exists($this,"set_$property")) 00099 { 00100 $aux = 'set_'.$property; 00101 $this->$aux($value); 00102 } 00103 } 00104 } 00105 00112 function get_xf($style) 00113 { 00114 // Set the type of the XF record and some of the attributes. 00115 if ($style == "style") { 00116 $style = 0xFFF5; 00117 } 00118 else { 00119 $style = $this->locked; 00120 $style |= $this->hidden << 1; 00121 } 00122 00123 // Flags to indicate if attributes have been set. 00124 $atr_num = ($this->_num_format != 0)?1:0; 00125 $atr_fnt = ($this->font_index != 0)?1:0; 00126 $atr_alc = ($this->_text_wrap)?1:0; 00127 $atr_bdr = ($this->bottom || 00128 $this->top || 00129 $this->left || 00130 $this->right)?1:0; 00131 $atr_pat = (($this->fg_color != 0x40) || 00132 ($this->bg_color != 0x41) || 00133 $this->pattern)?1:0; 00134 $atr_prot = 0; 00135 00136 // Zero the default border colour if the border has not been set. 00137 if ($this->bottom == 0) { 00138 $this->bottom_color = 0; 00139 } 00140 if ($this->top == 0) { 00141 $this->top_color = 0; 00142 } 00143 if ($this->right == 0) { 00144 $this->right_color = 0; 00145 } 00146 if ($this->left == 0) { 00147 $this->left_color = 0; 00148 } 00149 00150 $record = 0x00E0; // Record identifier 00151 $length = 0x0010; // Number of bytes to follow 00152 00153 $ifnt = $this->font_index; // Index to FONT record 00154 $ifmt = $this->_num_format; // Index to FORMAT record 00155 00156 $align = $this->_text_h_align; // Alignment 00157 $align |= $this->_text_wrap << 3; 00158 $align |= $this->text_v_align << 4; 00159 $align |= $this->text_justlast << 7; 00160 $align |= $this->rotation << 8; 00161 $align |= $atr_num << 10; 00162 $align |= $atr_fnt << 11; 00163 $align |= $atr_alc << 12; 00164 $align |= $atr_bdr << 13; 00165 $align |= $atr_pat << 14; 00166 $align |= $atr_prot << 15; 00167 00168 $icv = $this->fg_color; // fg and bg pattern colors 00169 $icv |= $this->bg_color << 7; 00170 00171 $fill = $this->pattern; // Fill and border line style 00172 $fill |= $this->bottom << 6; 00173 $fill |= $this->bottom_color << 9; 00174 00175 $border1 = $this->top; // Border line style and color 00176 $border1 |= $this->left << 3; 00177 $border1 |= $this->right << 6; 00178 $border1 |= $this->top_color << 9; 00179 00180 $border2 = $this->left_color; // Border color 00181 $border2 |= $this->right_color << 7; 00182 00183 $header = pack("vv", $record, $length); 00184 $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align, 00185 $icv, $fill, 00186 $border1, $border2); 00187 return($header.$data); 00188 } 00189 00196 function get_font() 00197 { 00198 $dyHeight = $this->size * 20; // Height of font (1/20 of a point) 00199 $icv = $this->color; // Index to color palette 00200 $bls = $this->bold; // Bold style 00201 $sss = $this->font_script; // Superscript/subscript 00202 $uls = $this->_underline; // Underline 00203 $bFamily = $this->font_family; // Font family 00204 $bCharSet = $this->font_charset; // Character set 00205 $rgch = $this->font; // Font name 00206 00207 $cch = strlen($rgch); // Length of font name 00208 $record = 0x31; // Record identifier 00209 $length = 0x0F + $cch; // Record length 00210 $reserved = 0x00; // Reserved 00211 $grbit = 0x00; // Font attributes 00212 if ($this->_italic) { 00213 $grbit |= 0x02; 00214 } 00215 if ($this->font_strikeout) { 00216 $grbit |= 0x08; 00217 } 00218 if ($this->font_outline) { 00219 $grbit |= 0x10; 00220 } 00221 if ($this->font_shadow) { 00222 $grbit |= 0x20; 00223 } 00224 00225 $header = pack("vv", $record, $length); 00226 $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls, 00227 $sss, $uls, $bFamily, 00228 $bCharSet, $reserved, $cch); 00229 return($header . $data. $this->font); 00230 } 00231 00241 function get_font_key() 00242 { 00243 $key = "$this->font$this->size"; 00244 $key .= "$this->font_script$this->_underline"; 00245 $key .= "$this->font_strikeout$this->bold$this->font_outline"; 00246 $key .= "$this->font_family$this->font_charset"; 00247 $key .= "$this->font_shadow$this->color$this->_italic"; 00248 $key = str_replace(" ","_",$key); 00249 return ($key); 00250 } 00251 00257 function get_xf_index() 00258 { 00259 return($this->xf_index); 00260 } 00261 00270 function _get_color($name_color = '') 00271 { 00272 $colors = array( 00273 'aqua' => 0x0F, 00274 'cyan' => 0x0F, 00275 'black' => 0x08, 00276 'blue' => 0x0C, 00277 'brown' => 0x10, 00278 'magenta' => 0x0E, 00279 'fuchsia' => 0x0E, 00280 'gray' => 0x17, 00281 'grey' => 0x17, 00282 'green' => 0x11, 00283 'lime' => 0x0B, 00284 'navy' => 0x12, 00285 'orange' => 0x35, 00286 'purple' => 0x14, 00287 'red' => 0x0A, 00288 'silver' => 0x16, 00289 'white' => 0x09, 00290 'yellow' => 0x0D 00291 ); 00292 00293 // Return the default color, 0x7FFF, if undef, 00294 if($name_color == '') { 00295 return(0x7FFF); 00296 } 00297 00298 // or the color string converted to an integer, 00299 if(isset($colors[$name_color])) { 00300 return($colors[$name_color]); 00301 } 00302 00303 // or the default color if string is unrecognised, 00304 if(preg_match("/\D/",$name_color)) { 00305 return(0x7FFF); 00306 } 00307 00308 // or an index < 8 mapped into the correct range, 00309 if($name_color < 8) { 00310 return($name_color + 8); 00311 } 00312 00313 // or the default color if arg is outside range, 00314 if($name_color > 63) { 00315 return(0x7FFF); 00316 } 00317 00318 // or an integer in the valid range 00319 return($name_color); 00320 } 00321 00328 function set_align($location) 00329 { 00330 if (preg_match("/\d/",$location)) { 00331 return; // Ignore numbers 00332 } 00333 00334 $location = strtolower($location); 00335 00336 if ($location == 'left') 00337 $this->_text_h_align = 1; 00338 if ($location == 'centre') 00339 $this->_text_h_align = 2; 00340 if ($location == 'center') 00341 $this->_text_h_align = 2; 00342 if ($location == 'right') 00343 $this->_text_h_align = 3; 00344 if ($location == 'fill') 00345 $this->_text_h_align = 4; 00346 if ($location == 'justify') 00347 $this->_text_h_align = 5; 00348 if ($location == 'merge') 00349 $this->_text_h_align = 6; 00350 if ($location == 'equal_space') // For T.K. 00351 $this->_text_h_align = 7; 00352 if ($location == 'top') 00353 $this->text_v_align = 0; 00354 if ($location == 'vcentre') 00355 $this->text_v_align = 1; 00356 if ($location == 'vcenter') 00357 $this->text_v_align = 1; 00358 if ($location == 'bottom') 00359 $this->text_v_align = 2; 00360 if ($location == 'vjustify') 00361 $this->text_v_align = 3; 00362 if ($location == 'vequal_space') // For T.K. 00363 $this->text_v_align = 4; 00364 } 00365 00371 function set_merge() 00372 { 00373 $this->set_align('merge'); 00374 } 00375 00384 function set_bold($weight = 1) 00385 { 00386 if($weight == 1) { 00387 $weight = 0x2BC; // Bold text 00388 } 00389 if($weight == 0) { 00390 $weight = 0x190; // Normal text 00391 } 00392 if($weight < 0x064) { 00393 $weight = 0x190; // Lower bound 00394 } 00395 if($weight > 0x3E8) { 00396 $weight = 0x190; // Upper bound 00397 } 00398 $this->bold = $weight; 00399 } 00400 00401 00402 /************************************ 00403 * FUNCTIONS FOR SETTING CELLS BORDERS 00404 */ 00405 00412 function set_bottom($style) 00413 { 00414 $this->bottom = $style; 00415 } 00416 00423 function set_top($style) 00424 { 00425 $this->top = $style; 00426 } 00427 00434 function set_left($style) 00435 { 00436 $this->left = $style; 00437 } 00438 00445 function set_right($style) 00446 { 00447 $this->right = $style; 00448 } 00449 00450 00457 function set_border($style) 00458 { 00459 $this->set_bottom($style); 00460 $this->set_top($style); 00461 $this->set_left($style); 00462 $this->set_right($style); 00463 } 00464 00465 00466 /******************************************* 00467 * FUNCTIONS FOR SETTING CELLS BORDERS COLORS 00468 */ 00469 00477 function set_border_color($color) 00478 { 00479 $this->set_bottom_color($color); 00480 $this->set_top_color($color); 00481 $this->set_left_color($color); 00482 $this->set_right_color($color); 00483 } 00484 00491 function set_bottom_color($color) 00492 { 00493 $value = $this->_get_color($color); 00494 $this->bottom_color = $value; 00495 } 00496 00503 function set_top_color($color) 00504 { 00505 $value = $this->_get_color($color); 00506 $this->top_color = $value; 00507 } 00508 00515 function set_left_color($color) 00516 { 00517 $value = $this->_get_color($color); 00518 $this->left_color = $value; 00519 } 00520 00527 function set_right_color($color) 00528 { 00529 $value = $this->_get_color($color); 00530 $this->right_color = $value; 00531 } 00532 00533 00540 function set_fg_color($color) 00541 { 00542 $value = $this->_get_color($color); 00543 $this->fg_color = $value; 00544 } 00545 00552 function set_bg_color($color) 00553 { 00554 $value = $this->_get_color($color); 00555 $this->bg_color = $value; 00556 } 00557 00564 function set_color($color) 00565 { 00566 $value = $this->_get_color($color); 00567 $this->color = $value; 00568 } 00569 00576 function set_pattern($arg = 1) 00577 { 00578 $this->pattern = $arg; 00579 } 00580 00588 function set_underline($underline) 00589 { 00590 $this->_underline = $underline; 00591 } 00592 00598 function set_italic() 00599 { 00600 $this->_italic = 1; 00601 } 00602 00609 function set_size($size) 00610 { 00611 $this->size = $size; 00612 } 00613 00620 function set_num_format($num_format) 00621 { 00622 $this->_num_format = $num_format; 00623 } 00624 00632 function set_text_wrap($text_wrap = 1) 00633 { 00634 $this->_text_wrap = $text_wrap; 00635 } 00636 } 00637 ?>