|
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_Excel_Writer: A library for generating Excel Spreadsheets 00018 * Copyright (c) 2002-2003 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 00035 require_once 'PEAR.php'; 00036 00045 class Spreadsheet_Excel_Writer_Format extends PEAR 00046 { 00051 var $_xf_index; 00052 00057 var $font_index; 00058 00063 var $_font_name; 00064 00069 var $_size; 00070 00075 var $_bold; 00076 00081 var $_italic; 00082 00087 var $_color; 00088 00093 var $_underline; 00094 00099 var $_font_strikeout; 00100 00105 var $_font_outline; 00106 00111 var $_font_shadow; 00112 00117 var $_font_script; 00118 00123 var $_font_family; 00124 00129 var $_font_charset; 00130 00135 var $_num_format; 00136 00141 var $_hidden; 00142 00147 var $_locked; 00148 00153 var $_text_h_align; 00154 00159 var $_text_wrap; 00160 00165 var $_text_v_align; 00166 00171 var $_text_justlast; 00172 00177 var $_rotation; 00178 00183 var $_fg_color; 00184 00189 var $_bg_color; 00190 00195 var $_pattern; 00196 00201 var $_bottom; 00202 00207 var $_bottom_color; 00208 00213 var $_top; 00214 00219 var $_top_color; 00220 00225 var $_left; 00226 00231 var $_left_color; 00232 00237 var $_right; 00238 00243 var $_right_color; 00244 00252 function Spreadsheet_Excel_Writer_Format($BIFF_version, $index = 0, $properties = array()) 00253 { 00254 $this->_xf_index = $index; 00255 $this->_BIFF_version = $BIFF_version; 00256 $this->font_index = 0; 00257 $this->_font_name = 'Arial'; 00258 $this->_size = 10; 00259 $this->_bold = 0x0190; 00260 $this->_italic = 0; 00261 $this->_color = 0x7FFF; 00262 $this->_underline = 0; 00263 $this->_font_strikeout = 0; 00264 $this->_font_outline = 0; 00265 $this->_font_shadow = 0; 00266 $this->_font_script = 0; 00267 $this->_font_family = 0; 00268 $this->_font_charset = 0; 00269 00270 $this->_num_format = 0; 00271 00272 $this->_hidden = 0; 00273 $this->_locked = 0; 00274 00275 $this->_text_h_align = 0; 00276 $this->_text_wrap = 0; 00277 $this->_text_v_align = 2; 00278 $this->_text_justlast = 0; 00279 $this->_rotation = 0; 00280 00281 $this->_fg_color = 0x40; 00282 $this->_bg_color = 0x41; 00283 00284 $this->_pattern = 0; 00285 00286 $this->_bottom = 0; 00287 $this->_top = 0; 00288 $this->_left = 0; 00289 $this->_right = 0; 00290 $this->_diag = 0; 00291 00292 $this->_bottom_color = 0x40; 00293 $this->_top_color = 0x40; 00294 $this->_left_color = 0x40; 00295 $this->_right_color = 0x40; 00296 $this->_diag_color = 0x40; 00297 00298 // Set properties passed to Spreadsheet_Excel_Writer_Workbook::addFormat() 00299 foreach ($properties as $property => $value) 00300 { 00301 if (method_exists($this, 'set'.ucwords($property))) { 00302 $method_name = 'set'.ucwords($property); 00303 $this->$method_name($value); 00304 } 00305 } 00306 } 00307 00308 00315 function getXf($style) 00316 { 00317 // Set the type of the XF record and some of the attributes. 00318 if ($style == 'style') { 00319 $style = 0xFFF5; 00320 } else { 00321 $style = $this->_locked; 00322 $style |= $this->_hidden << 1; 00323 } 00324 00325 // Flags to indicate if attributes have been set. 00326 $atr_num = ($this->_num_format != 0)?1:0; 00327 $atr_fnt = ($this->font_index != 0)?1:0; 00328 $atr_alc = ($this->_text_wrap)?1:0; 00329 $atr_bdr = ($this->_bottom || 00330 $this->_top || 00331 $this->_left || 00332 $this->_right)?1:0; 00333 $atr_pat = (($this->_fg_color != 0x40) || 00334 ($this->_bg_color != 0x41) || 00335 $this->_pattern)?1:0; 00336 $atr_prot = $this->_locked | $this->_hidden; 00337 00338 // Zero the default border colour if the border has not been set. 00339 if ($this->_bottom == 0) { 00340 $this->_bottom_color = 0; 00341 } 00342 if ($this->_top == 0) { 00343 $this->_top_color = 0; 00344 } 00345 if ($this->_right == 0) { 00346 $this->_right_color = 0; 00347 } 00348 if ($this->_left == 0) { 00349 $this->_left_color = 0; 00350 } 00351 if ($this->_diag == 0) { 00352 $this->_diag_color = 0; 00353 } 00354 00355 $record = 0x00E0; // Record identifier 00356 if ($this->_BIFF_version == 0x0500) { 00357 $length = 0x0010; // Number of bytes to follow 00358 } 00359 if ($this->_BIFF_version == 0x0600) { 00360 $length = 0x0014; 00361 } 00362 00363 $ifnt = $this->font_index; // Index to FONT record 00364 $ifmt = $this->_num_format; // Index to FORMAT record 00365 if ($this->_BIFF_version == 0x0500) { 00366 $align = $this->_text_h_align; // Alignment 00367 $align |= $this->_text_wrap << 3; 00368 $align |= $this->_text_v_align << 4; 00369 $align |= $this->_text_justlast << 7; 00370 $align |= $this->_rotation << 8; 00371 $align |= $atr_num << 10; 00372 $align |= $atr_fnt << 11; 00373 $align |= $atr_alc << 12; 00374 $align |= $atr_bdr << 13; 00375 $align |= $atr_pat << 14; 00376 $align |= $atr_prot << 15; 00377 00378 $icv = $this->_fg_color; // fg and bg pattern colors 00379 $icv |= $this->_bg_color << 7; 00380 00381 $fill = $this->_pattern; // Fill and border line style 00382 $fill |= $this->_bottom << 6; 00383 $fill |= $this->_bottom_color << 9; 00384 00385 $border1 = $this->_top; // Border line style and color 00386 $border1 |= $this->_left << 3; 00387 $border1 |= $this->_right << 6; 00388 $border1 |= $this->_top_color << 9; 00389 00390 $border2 = $this->_left_color; // Border color 00391 $border2 |= $this->_right_color << 7; 00392 00393 $header = pack("vv", $record, $length); 00394 $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align, 00395 $icv, $fill, 00396 $border1, $border2); 00397 } elseif ($this->_BIFF_version == 0x0600) { 00398 $align = $this->_text_h_align; // Alignment 00399 $align |= $this->_text_wrap << 3; 00400 $align |= $this->_text_v_align << 4; 00401 $align |= $this->_text_justlast << 7; 00402 00403 $used_attrib = $atr_num << 2; 00404 $used_attrib |= $atr_fnt << 3; 00405 $used_attrib |= $atr_alc << 4; 00406 $used_attrib |= $atr_bdr << 5; 00407 $used_attrib |= $atr_pat << 6; 00408 $used_attrib |= $atr_prot << 7; 00409 00410 $icv = $this->_fg_color; // fg and bg pattern colors 00411 $icv |= $this->_bg_color << 7; 00412 00413 $border1 = $this->_left; // Border line style and color 00414 $border1 |= $this->_right << 4; 00415 $border1 |= $this->_top << 8; 00416 $border1 |= $this->_bottom << 12; 00417 $border1 |= $this->_left_color << 16; 00418 $border1 |= $this->_right_color << 23; 00419 $diag_tl_to_rb = 0; // FIXME: add method 00420 $diag_tr_to_lb = 0; // FIXME: add method 00421 $border1 |= $diag_tl_to_rb << 30; 00422 $border1 |= $diag_tr_to_lb << 31; 00423 00424 $border2 = $this->_top_color; // Border color 00425 $border2 |= $this->_bottom_color << 7; 00426 $border2 |= $this->_diag_color << 14; 00427 $border2 |= $this->_diag << 21; 00428 $border2 |= $this->_pattern << 26; 00429 00430 $header = pack("vv", $record, $length); 00431 00432 $rotation = 0x00; 00433 $biff8_options = 0x00; 00434 $data = pack("vvvC", $ifnt, $ifmt, $style, $align); 00435 $data .= pack("CCC", $rotation, $biff8_options, $used_attrib); 00436 $data .= pack("VVv", $border1, $border2, $icv); 00437 } 00438 00439 return($header . $data); 00440 } 00441 00447 function getFont() 00448 { 00449 $dyHeight = $this->_size * 20; // Height of font (1/20 of a point) 00450 $icv = $this->_color; // Index to color palette 00451 $bls = $this->_bold; // Bold style 00452 $sss = $this->_font_script; // Superscript/subscript 00453 $uls = $this->_underline; // Underline 00454 $bFamily = $this->_font_family; // Font family 00455 $bCharSet = $this->_font_charset; // Character set 00456 $encoding = 0; // TODO: Unicode support 00457 00458 $cch = strlen($this->_font_name); // Length of font name 00459 $record = 0x31; // Record identifier 00460 if ($this->_BIFF_version == 0x0500) { 00461 $length = 0x0F + $cch; // Record length 00462 } elseif ($this->_BIFF_version == 0x0600) { 00463 $length = 0x10 + $cch; 00464 } 00465 $reserved = 0x00; // Reserved 00466 $grbit = 0x00; // Font attributes 00467 if ($this->_italic) { 00468 $grbit |= 0x02; 00469 } 00470 if ($this->_font_strikeout) { 00471 $grbit |= 0x08; 00472 } 00473 if ($this->_font_outline) { 00474 $grbit |= 0x10; 00475 } 00476 if ($this->_font_shadow) { 00477 $grbit |= 0x20; 00478 } 00479 00480 $header = pack("vv", $record, $length); 00481 if ($this->_BIFF_version == 0x0500) { 00482 $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls, 00483 $sss, $uls, $bFamily, 00484 $bCharSet, $reserved, $cch); 00485 } elseif ($this->_BIFF_version == 0x0600) { 00486 $data = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls, 00487 $sss, $uls, $bFamily, 00488 $bCharSet, $reserved, $cch, $encoding); 00489 } 00490 return($header . $data . $this->_font_name); 00491 } 00492 00503 function getFontKey() 00504 { 00505 $key = "$this->_font_name$this->_size"; 00506 $key .= "$this->_font_script$this->_underline"; 00507 $key .= "$this->_font_strikeout$this->_bold$this->_font_outline"; 00508 $key .= "$this->_font_family$this->_font_charset"; 00509 $key .= "$this->_font_shadow$this->_color$this->_italic"; 00510 $key = str_replace(' ', '_', $key); 00511 return ($key); 00512 } 00513 00519 function getXfIndex() 00520 { 00521 return($this->_xf_index); 00522 } 00523 00533 function _getColor($name_color = '') 00534 { 00535 $colors = array( 00536 'aqua' => 0x0F, 00537 'cyan' => 0x0F, 00538 'black' => 0x08, 00539 'blue' => 0x0C, 00540 'brown' => 0x10, 00541 'magenta' => 0x0E, 00542 'fuchsia' => 0x0E, 00543 'gray' => 0x17, 00544 'grey' => 0x17, 00545 'green' => 0x11, 00546 'lime' => 0x0B, 00547 'navy' => 0x12, 00548 'orange' => 0x35, 00549 'purple' => 0x14, 00550 'red' => 0x0A, 00551 'silver' => 0x16, 00552 'white' => 0x09, 00553 'yellow' => 0x0D 00554 ); 00555 00556 // Return the default color, 0x7FFF, if undef, 00557 if ($name_color == '') { 00558 return(0x7FFF); 00559 } 00560 00561 // or the color string converted to an integer, 00562 if (isset($colors[$name_color])) { 00563 return($colors[$name_color]); 00564 } 00565 00566 // or the default color if string is unrecognised, 00567 if (preg_match("/\D/",$name_color)) { 00568 return(0x7FFF); 00569 } 00570 00571 // or an index < 8 mapped into the correct range, 00572 if ($name_color < 8) { 00573 return($name_color + 8); 00574 } 00575 00576 // or the default color if arg is outside range, 00577 if ($name_color > 63) { 00578 return(0x7FFF); 00579 } 00580 00581 // or an integer in the valid range 00582 return($name_color); 00583 } 00584 00591 function setAlign($location) 00592 { 00593 if (preg_match("/\d/",$location)) { 00594 return; // Ignore numbers 00595 } 00596 00597 $location = strtolower($location); 00598 00599 if ($location == 'left') { 00600 $this->_text_h_align = 1; 00601 } 00602 if ($location == 'centre') { 00603 $this->_text_h_align = 2; 00604 } 00605 if ($location == 'center') { 00606 $this->_text_h_align = 2; 00607 } 00608 if ($location == 'right') { 00609 $this->_text_h_align = 3; 00610 } 00611 if ($location == 'fill') { 00612 $this->_text_h_align = 4; 00613 } 00614 if ($location == 'justify') { 00615 $this->_text_h_align = 5; 00616 } 00617 if ($location == 'merge') { 00618 $this->_text_h_align = 6; 00619 } 00620 if ($location == 'equal_space') { // For T.K. 00621 $this->_text_h_align = 7; 00622 } 00623 if ($location == 'top') { 00624 $this->_text_v_align = 0; 00625 } 00626 if ($location == 'vcentre') { 00627 $this->_text_v_align = 1; 00628 } 00629 if ($location == 'vcenter') { 00630 $this->_text_v_align = 1; 00631 } 00632 if ($location == 'bottom') { 00633 $this->_text_v_align = 2; 00634 } 00635 if ($location == 'vjustify') { 00636 $this->_text_v_align = 3; 00637 } 00638 if ($location == 'vequal_space') { // For T.K. 00639 $this->_text_v_align = 4; 00640 } 00641 } 00642 00649 function setHAlign($location) 00650 { 00651 if (preg_match("/\d/",$location)) { 00652 return; // Ignore numbers 00653 } 00654 00655 $location = strtolower($location); 00656 00657 if ($location == 'left') { 00658 $this->_text_h_align = 1; 00659 } 00660 if ($location == 'centre') { 00661 $this->_text_h_align = 2; 00662 } 00663 if ($location == 'center') { 00664 $this->_text_h_align = 2; 00665 } 00666 if ($location == 'right') { 00667 $this->_text_h_align = 3; 00668 } 00669 if ($location == 'fill') { 00670 $this->_text_h_align = 4; 00671 } 00672 if ($location == 'justify') { 00673 $this->_text_h_align = 5; 00674 } 00675 if ($location == 'merge') { 00676 $this->_text_h_align = 6; 00677 } 00678 if ($location == 'equal_space') { // For T.K. 00679 $this->_text_h_align = 7; 00680 } 00681 } 00682 00689 function setVAlign($location) 00690 { 00691 if (preg_match("/\d/",$location)) { 00692 return; // Ignore numbers 00693 } 00694 00695 $location = strtolower($location); 00696 00697 if ($location == 'top') { 00698 $this->_text_v_align = 0; 00699 } 00700 if ($location == 'vcentre') { 00701 $this->_text_v_align = 1; 00702 } 00703 if ($location == 'vcenter') { 00704 $this->_text_v_align = 1; 00705 } 00706 if ($location == 'bottom') { 00707 $this->_text_v_align = 2; 00708 } 00709 if ($location == 'vjustify') { 00710 $this->_text_v_align = 3; 00711 } 00712 if ($location == 'vequal_space') { // For T.K. 00713 $this->_text_v_align = 4; 00714 } 00715 } 00716 00722 function setMerge() 00723 { 00724 $this->setAlign('merge'); 00725 } 00726 00737 function setBold($weight = 1) 00738 { 00739 if ($weight == 1) { 00740 $weight = 0x2BC; // Bold text 00741 } 00742 if ($weight == 0) { 00743 $weight = 0x190; // Normal text 00744 } 00745 if ($weight < 0x064) { 00746 $weight = 0x190; // Lower bound 00747 } 00748 if ($weight > 0x3E8) { 00749 $weight = 0x190; // Upper bound 00750 } 00751 $this->_bold = $weight; 00752 } 00753 00754 00755 /************************************ 00756 * FUNCTIONS FOR SETTING CELLS BORDERS 00757 */ 00758 00765 function setBottom($style) 00766 { 00767 $this->_bottom = $style; 00768 } 00769 00776 function setTop($style) 00777 { 00778 $this->_top = $style; 00779 } 00780 00787 function setLeft($style) 00788 { 00789 $this->_left = $style; 00790 } 00791 00798 function setRight($style) 00799 { 00800 $this->_right = $style; 00801 } 00802 00803 00810 function setBorder($style) 00811 { 00812 $this->setBottom($style); 00813 $this->setTop($style); 00814 $this->setLeft($style); 00815 $this->setRight($style); 00816 } 00817 00818 00819 /******************************************* 00820 * FUNCTIONS FOR SETTING CELLS BORDERS COLORS 00821 */ 00822 00830 function setBorderColor($color) 00831 { 00832 $this->setBottomColor($color); 00833 $this->setTopColor($color); 00834 $this->setLeftColor($color); 00835 $this->setRightColor($color); 00836 } 00837 00844 function setBottomColor($color) 00845 { 00846 $value = $this->_getColor($color); 00847 $this->_bottom_color = $value; 00848 } 00849 00856 function setTopColor($color) 00857 { 00858 $value = $this->_getColor($color); 00859 $this->_top_color = $value; 00860 } 00861 00868 function setLeftColor($color) 00869 { 00870 $value = $this->_getColor($color); 00871 $this->_left_color = $value; 00872 } 00873 00880 function setRightColor($color) 00881 { 00882 $value = $this->_getColor($color); 00883 $this->_right_color = $value; 00884 } 00885 00886 00893 function setFgColor($color) 00894 { 00895 $value = $this->_getColor($color); 00896 $this->_fg_color = $value; 00897 if ($this->_pattern == 0) { // force color to be seen 00898 $this->_pattern = 1; 00899 } 00900 } 00901 00908 function setBgColor($color) 00909 { 00910 $value = $this->_getColor($color); 00911 $this->_bg_color = $value; 00912 if ($this->_pattern == 0) { // force color to be seen 00913 $this->_pattern = 1; 00914 } 00915 } 00916 00923 function setColor($color) 00924 { 00925 $value = $this->_getColor($color); 00926 $this->_color = $value; 00927 } 00928 00936 function setPattern($arg = 1) 00937 { 00938 $this->_pattern = $arg; 00939 } 00940 00948 function setUnderline($underline) 00949 { 00950 $this->_underline = $underline; 00951 } 00952 00958 function setItalic() 00959 { 00960 $this->_italic = 1; 00961 } 00962 00969 function setSize($size) 00970 { 00971 $this->_size = $size; 00972 } 00973 00979 function setTextWrap() 00980 { 00981 $this->_text_wrap = 1; 00982 } 00983 00991 function setTextRotation($angle) 00992 { 00993 switch ($angle) 00994 { 00995 case 0: 00996 $this->_rotation = 0; 00997 break; 00998 case 90: 00999 $this->_rotation = 3; 01000 break; 01001 case 270: 01002 $this->_rotation = 2; 01003 break; 01004 case -1: 01005 $this->_rotation = 1; 01006 break; 01007 default : 01008 return $this->raiseError("Invalid value for angle.". 01009 " Possible values are: 0, 90, 270 and -1 ". 01010 "for stacking top-to-bottom."); 01011 $this->_rotation = 0; 01012 break; 01013 } 01014 } 01015 01023 function setNumFormat($num_format) 01024 { 01025 $this->_num_format = $num_format; 01026 } 01027 01033 function setStrikeOut() 01034 { 01035 $this->_font_strikeout = 1; 01036 } 01037 01043 function setOutLine() 01044 { 01045 $this->_font_outline = 1; 01046 } 01047 01053 function setShadow() 01054 { 01055 $this->_font_shadow = 1; 01056 } 01057 01065 function setScript($script) 01066 { 01067 $this->_font_script = $script; 01068 } 01069 01075 function setLocked() 01076 { 01077 $this->_locked = 1; 01078 } 01079 01085 function setUnLocked() 01086 { 01087 $this->_locked = 0; 01088 } 01089 01097 function setFontFamily($font_family) 01098 { 01099 $this->_font_name = $font_family; 01100 } 01101 } 01102 ?>