"Fossies" - the Fresh Open Source Software Archive 
Member "php_writeexcel-0.3.0/class.writeexcel_format.inc.php" (1 Nov 2005, 19502 Bytes) of package /linux/www/old/php_writeexcel-0.3.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 <?php
2
3 /*
4 * Copyleft 2002 Johann Hanne
5 *
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this software; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*
23 * This is the Spreadsheet::WriteExcel Perl package ported to PHP
24 * Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
25 */
26
27 class writeexcel_format {
28
29 var $_xf_index;
30 var $_font_index;
31 var $_font;
32 var $_size;
33 var $_bold;
34 var $_italic;
35 var $_color;
36 var $_underline;
37 var $_font_strikeout;
38 var $_font_outline;
39 var $_font_shadow;
40 var $_font_script;
41 var $_font_family;
42 var $_font_charset;
43 var $_num_format;
44 var $_hidden;
45 var $_locked;
46 var $_text_h_align;
47 var $_text_wrap;
48 var $_text_v_align;
49 var $_text_justlast;
50 var $_rotation;
51 var $_fg_color;
52 var $_bg_color;
53 var $_pattern;
54 var $_bottom;
55 var $_top;
56 var $_left;
57 var $_right;
58 var $_bottom_color;
59 var $_top_color;
60 var $_left_color;
61 var $_right_color;
62
63 /*
64 * Constructor
65 */
66 function writeexcel_format() {
67 $_=func_get_args();
68
69 $this->_xf_index = (sizeof($_)>0) ? array_shift($_) : 0;
70
71 $this->_font_index = 0;
72 $this->_font = 'Arial';
73 $this->_size = 10;
74 $this->_bold = 0x0190;
75 $this->_italic = 0;
76 $this->_color = 0x7FFF;
77 $this->_underline = 0;
78 $this->_font_strikeout = 0;
79 $this->_font_outline = 0;
80 $this->_font_shadow = 0;
81 $this->_font_script = 0;
82 $this->_font_family = 0;
83 $this->_font_charset = 0;
84
85 $this->_num_format = 0;
86
87 $this->_hidden = 0;
88 $this->_locked = 1;
89
90 $this->_text_h_align = 0;
91 $this->_text_wrap = 0;
92 $this->_text_v_align = 2;
93 $this->_text_justlast = 0;
94 $this->_rotation = 0;
95
96 $this->_fg_color = 0x40;
97 $this->_bg_color = 0x41;
98
99 $this->_pattern = 0;
100
101 $this->_bottom = 0;
102 $this->_top = 0;
103 $this->_left = 0;
104 $this->_right = 0;
105
106 $this->_bottom_color = 0x40;
107 $this->_top_color = 0x40;
108 $this->_left_color = 0x40;
109 $this->_right_color = 0x40;
110
111 // Set properties passed to writeexcel_workbook::addformat()
112 if (sizeof($_)>0) {
113 call_user_func_array(array(&$this, 'set_properties'), $_);
114 }
115 }
116
117 /*
118 * Copy the attributes of another writeexcel_format object.
119 */
120 function copy($other) {
121 $xf = $this->_xf_index; // Backup XF index
122 foreach ($other as $key->$value) {
123 $this->{$key} = $value;
124 }
125 $this->_xf_index = $xf; // Restore XF index
126 }
127
128 /*
129 * Generate an Excel BIFF XF record.
130 */
131 function get_xf() {
132
133 $_=func_get_args();
134
135 // $record Record identifier
136 // $length Number of bytes to follow
137
138 // $ifnt Index to FONT record
139 // $ifmt Index to FORMAT record
140 // $style Style and other options
141 // $align Alignment
142 // $icv fg and bg pattern colors
143 // $fill Fill and border line style
144 // $border1 Border line style and color
145 // $border2 Border color
146
147 // Set the type of the XF record and some of the attributes.
148 if ($_[0] == "style") {
149 $style = 0xFFF5;
150 } else {
151 $style = $this->_locked;
152 $style |= $this->_hidden << 1;
153 }
154
155 // Flags to indicate if attributes have been set.
156 $atr_num = ($this->_num_format != 0) ? 1 : 0;
157 $atr_fnt = ($this->_font_index != 0) ? 1 : 0;
158 $atr_alc = $this->_text_wrap ? 1 : 0;
159 $atr_bdr = ($this->_bottom ||
160 $this->_top ||
161 $this->_left ||
162 $this->_right) ? 1 : 0;
163 $atr_pat = ($this->_fg_color != 0x41 ||
164 $this->_bg_color != 0x41 ||
165 $this->_pattern != 0x00) ? 1 : 0;
166 $atr_prot = 0;
167
168 // Reset the default colors for the non-font properties
169 if ($this->_fg_color == 0x7FFF) $this->_fg_color = 0x40;
170 if ($this->_bg_color == 0x7FFF) $this->_bg_color = 0x41;
171 if ($this->_bottom_color == 0x7FFF) $this->_bottom_color = 0x41;
172 if ($this->_top_color == 0x7FFF) $this->_top_color = 0x41;
173 if ($this->_left_color == 0x7FFF) $this->_left_color = 0x41;
174 if ($this->_right_color == 0x7FFF) $this->_right_color = 0x41;
175
176 // Zero the default border colour if the border has not been set.
177 if ($this->_bottom == 0) {
178 $this->_bottom_color = 0;
179 }
180 if ($this->_top == 0) {
181 $this->_top_color = 0;
182 }
183 if ($this->_right == 0) {
184 $this->_right_color = 0;
185 }
186 if ($this->_left == 0) {
187 $this->_left_color = 0;
188 }
189
190 // The following 2 logical statements take care of special cases in
191 // relation to cell colors and patterns:
192 // 1. For a solid fill (_pattern == 1) Excel reverses the role of
193 // foreground and background colors
194 // 2. If the user specifies a foreground or background color
195 // without a pattern they probably wanted a solid fill, so we
196 // fill in the defaults.
197 if ($this->_pattern <= 0x01 &&
198 $this->_bg_color != 0x41 &&
199 $this->_fg_color == 0x40 )
200 {
201 $this->_fg_color = $this->_bg_color;
202 $this->_bg_color = 0x40;
203 $this->_pattern = 1;
204 }
205
206 if ($this->_pattern <= 0x01 &&
207 $this->_bg_color == 0x41 &&
208 $this->_fg_color != 0x40 )
209 {
210 $this->_bg_color = 0x40;
211 $this->_pattern = 1;
212 }
213
214 $record = 0x00E0;
215 $length = 0x0010;
216
217 $ifnt = $this->_font_index;
218 $ifmt = $this->_num_format;
219
220 $align = $this->_text_h_align;
221 $align |= $this->_text_wrap << 3;
222 $align |= $this->_text_v_align << 4;
223 $align |= $this->_text_justlast << 7;
224 $align |= $this->_rotation << 8;
225 $align |= $atr_num << 10;
226 $align |= $atr_fnt << 11;
227 $align |= $atr_alc << 12;
228 $align |= $atr_bdr << 13;
229 $align |= $atr_pat << 14;
230 $align |= $atr_prot << 15;
231
232 $icv = $this->_fg_color;
233 $icv |= $this->_bg_color << 7;
234
235 $fill = $this->_pattern;
236 $fill |= $this->_bottom << 6;
237 $fill |= $this->_bottom_color << 9;
238
239 $border1 = $this->_top;
240 $border1 |= $this->_left << 3;
241 $border1 |= $this->_right << 6;
242 $border1 |= $this->_top_color << 9;
243
244 $border2 = $this->_left_color;
245 $border2 |= $this->_right_color << 7;
246
247 $header = pack("vv", $record, $length);
248 $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
249 $icv, $fill,
250 $border1, $border2);
251
252 return($header . $data);
253 }
254
255 /*
256 * Generate an Excel BIFF FONT record.
257 */
258 function get_font() {
259
260 // $record Record identifier
261 // $length Record length
262
263 // $dyHeight Height of font (1/20 of a point)
264 // $grbit Font attributes
265 // $icv Index to color palette
266 // $bls Bold style
267 // $sss Superscript/subscript
268 // $uls Underline
269 // $bFamily Font family
270 // $bCharSet Character set
271 // $reserved Reserved
272 // $cch Length of font name
273 // $rgch Font name
274
275 $dyHeight = $this->_size * 20;
276 $icv = $this->_color;
277 $bls = $this->_bold;
278 $sss = $this->_font_script;
279 $uls = $this->_underline;
280 $bFamily = $this->_font_family;
281 $bCharSet = $this->_font_charset;
282 $rgch = $this->_font;
283
284 $cch = strlen($rgch);
285 $record = 0x31;
286 $length = 0x0F + $cch;
287 $reserved = 0x00;
288
289 $grbit = 0x00;
290
291 if ($this->_italic) {
292 $grbit |= 0x02;
293 }
294
295 if ($this->_font_strikeout) {
296 $grbit |= 0x08;
297 }
298
299 if ($this->_font_outline) {
300 $grbit |= 0x10;
301 }
302
303 if ($this->_font_shadow) {
304 $grbit |= 0x20;
305 }
306
307 $header = pack("vv", $record, $length);
308 $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
309 $sss, $uls, $bFamily,
310 $bCharSet, $reserved, $cch);
311
312 return($header . $data . $this->_font);
313 }
314
315 /*
316 * Returns a unique hash key for a font.
317 * Used by writeexcel_workbook::_store_all_fonts()
318 */
319 function get_font_key() {
320
321 # The following elements are arranged to increase the probability of
322 # generating a unique key. Elements that hold a large range of numbers
323 # eg. _color are placed between two binary elements such as _italic
324 #
325 $key = $this->_font.$this->_size.
326 $this->_font_script.$this->_underline.
327 $this->_font_strikeout.$this->_bold.$this->_font_outline.
328 $this->_font_family.$this->_font_charset.
329 $this->_font_shadow.$this->_color.$this->_italic;
330
331 $key = preg_replace('/ /', '_', $key); # Convert the key to a single word
332
333 return $key;
334 }
335
336 /*
337 * Returns the used by Worksheet->_XF()
338 */
339 function get_xf_index() {
340 return $this->_xf_index;
341 }
342
343 /*
344 * Used in conjunction with the set_xxx_color methods to convert a color
345 * string into a number. Color range is 0..63 but we will restrict it
346 * to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
347 */
348 function _get_color($color=false) {
349
350 $colors = array(
351 'aqua' => 0x0F,
352 'cyan' => 0x0F,
353 'black' => 0x08,
354 'blue' => 0x0C,
355 'brown' => 0x10,
356 'magenta' => 0x0E,
357 'fuchsia' => 0x0E,
358 'gray' => 0x17,
359 'grey' => 0x17,
360 'green' => 0x11,
361 'lime' => 0x0B,
362 'navy' => 0x12,
363 'orange' => 0x35,
364 'purple' => 0x14,
365 'red' => 0x0A,
366 'silver' => 0x16,
367 'white' => 0x09,
368 'yellow' => 0x0D
369 );
370
371 // Return the default color, 0x7FFF, if undef,
372 if ($color===false) {
373 return 0x7FFF;
374 }
375
376 // or the color string converted to an integer,
377 if (isset($colors[strtolower($color)])) {
378 return $colors[strtolower($color)];
379 }
380
381 // or the default color if string is unrecognised,
382 if (preg_match('/\D/', $color)) {
383 return 0x7FFF;
384 }
385
386 // or an index < 8 mapped into the correct range,
387 if ($color<8) {
388 return $color + 8;
389 }
390
391 // or the default color if arg is outside range,
392 if ($color>63) {
393 return 0x7FFF;
394 }
395
396 // or an integer in the valid range
397 return $color;
398 }
399
400 /*
401 * Set cell alignment.
402 */
403 function set_align($location) {
404
405 // Ignore numbers
406 if (preg_match('/\d/', $location)) {
407 return;
408 }
409
410 $location = strtolower($location);
411
412 switch ($location) {
413
414 case 'left':
415 $this->set_text_h_align(1);
416 break;
417
418 case 'centre':
419 case 'center':
420 $this->set_text_h_align(2);
421 break;
422
423 case 'right':
424 $this->set_text_h_align(3);
425 break;
426
427 case 'fill':
428 $this->set_text_h_align(4);
429 break;
430
431 case 'justify':
432 $this->set_text_h_align(5);
433 break;
434
435 case 'merge':
436 $this->set_text_h_align(6);
437 break;
438
439 case 'equal_space':
440 $this->set_text_h_align(7);
441 break;
442
443 case 'top':
444 $this->set_text_v_align(0);
445 break;
446
447 case 'vcentre':
448 case 'vcenter':
449 $this->set_text_v_align(1);
450 break;
451 break;
452
453 case 'bottom':
454 $this->set_text_v_align(2);
455 break;
456
457 case 'vjustify':
458 $this->set_text_v_align(3);
459 break;
460
461 case 'vequal_space':
462 $this->set_text_v_align(4);
463 break;
464 }
465 }
466
467 /*
468 * Set vertical cell alignment. This is required by the set_properties()
469 * method to differentiate between the vertical and horizontal properties.
470 */
471 function set_valign($location) {
472 $this->set_align($location);
473 }
474
475 /*
476 * This is an alias for the unintuitive set_align('merge')
477 */
478 function set_merge() {
479 $this->set_text_h_align(6);
480 }
481
482 /*
483 * Bold has a range 0x64..0x3E8.
484 * 0x190 is normal. 0x2BC is bold.
485 */
486 function set_bold($weight=1) {
487
488 if ($weight == 1) {
489 // Bold text
490 $weight = 0x2BC;
491 }
492
493 if ($weight == 0) {
494 // Normal text
495 $weight = 0x190;
496 }
497
498 if ($weight < 0x064) {
499 // Lower bound
500 $weight = 0x190;
501 }
502
503 if ($weight > 0x3E8) {
504 // Upper bound
505 $weight = 0x190;
506 }
507
508 $this->_bold = $weight;
509 }
510
511 /*
512 * Set all cell borders (bottom, top, left, right) to the same style
513 */
514 function set_border($style) {
515 $this->set_bottom($style);
516 $this->set_top($style);
517 $this->set_left($style);
518 $this->set_right($style);
519 }
520
521 /*
522 * Set all cell borders (bottom, top, left, right) to the same color
523 */
524 function set_border_color($color) {
525 $this->set_bottom_color($color);
526 $this->set_top_color($color);
527 $this->set_left_color($color);
528 $this->set_right_color($color);
529 }
530
531 /*
532 * Convert hashes of properties to method calls.
533 */
534 function set_properties() {
535
536 $_=func_get_args();
537
538 $properties=array();
539 foreach($_ as $props) {
540 if (is_array($props)) {
541 $properties=array_merge($properties, $props);
542 } else {
543 $properties[]=$props;
544 }
545 }
546
547 foreach ($properties as $key=>$value) {
548
549 // Strip leading "-" from Tk style properties eg. -color => 'red'.
550 $key = preg_replace('/^-/', '', $key);
551
552 /* Make sure method names are alphanumeric characters only, in
553 case tainted data is passed to the eval(). */
554 if (preg_match('/\W/', $key)) {
555 trigger_error("Unknown property: $key.",
556 E_USER_ERROR);
557 }
558
559 /* Evaling all $values as a strings gets around the problem of
560 some numerical format strings being evaluated as numbers, for
561 example "00000" for a zip code. */
562 if (is_int($key)) {
563 eval("\$this->set_$value();");
564 } else {
565 eval("\$this->set_$key('$value');");
566 }
567
568 }
569 }
570
571 function set_font($font) {
572 $this->_font=$font;
573 }
574
575 function set_size($size) {
576 $this->_size=$size;
577 }
578
579 function set_italic($italic=1) {
580 $this->_italic=$italic;
581 }
582
583 function set_color($color) {
584 $this->_color=$this->_get_color($color);
585 }
586
587 function set_underline($underline=1) {
588 $this->_underline=$underline;
589 }
590
591 function set_font_strikeout($font_strikeout=1) {
592 $this->_font_strikeout=$font_strikeout;
593 }
594
595 function set_font_outline($font_outline=1) {
596 $this->_font_outline=$font_outline;
597 }
598
599 function set_font_shadow($font_shadow=1) {
600 $this->_font_shadow=$font_shadow;
601 }
602
603 function set_font_script($font_script=1) {
604 $this->_font_script=$font_script;
605 }
606
607 /* Undocumented */
608 function set_font_family($font_family=1) {
609 $this->_font_family=$font_family;
610 }
611
612 /* Undocumented */
613 function set_font_charset($font_charset=1) {
614 $this->_font_charset=$font_charset;
615 }
616
617 function set_num_format($num_format=1) {
618 $this->_num_format=$num_format;
619 }
620
621 function set_hidden($hidden=1) {
622 $this->_hidden=$hidden;
623 }
624
625 function set_locked($locked=1) {
626 $this->_locked=$locked;
627 }
628
629 function set_text_h_align($align) {
630 $this->_text_h_align=$align;
631 }
632
633 function set_text_wrap($wrap=1) {
634 $this->_text_wrap=$wrap;
635 }
636
637 function set_text_v_align($align) {
638 $this->_text_v_align=$align;
639 }
640
641 function set_text_justlast($text_justlast=1) {
642 $this->_text_justlast=$text_justlast;
643 }
644
645 function set_rotation($rotation=1) {
646 $this->_rotation=$rotation;
647 }
648
649 function set_fg_color($color) {
650 $this->_fg_color=$this->_get_color($color);
651 }
652
653 function set_bg_color($color) {
654 $this->_bg_color=$this->_get_color($color);
655 }
656
657 function set_pattern($pattern=1) {
658 $this->_pattern=$pattern;
659 }
660
661 function set_bottom($bottom=1) {
662 $this->_bottom=$bottom;
663 }
664
665 function set_top($top=1) {
666 $this->_top=$top;
667 }
668
669 function set_left($left=1) {
670 $this->_left=$left;
671 }
672
673 function set_right($right=1) {
674 $this->_right=$right;
675 }
676
677 function set_bottom_color($color) {
678 $this->_bottom_color=$this->_get_color($color);
679 }
680
681 function set_top_color($color) {
682 $this->_top_color=$this->_get_color($color);
683 }
684
685 function set_left_color($color) {
686 $this->_left_color=$this->_get_color($color);
687 }
688
689 function set_right_color($color) {
690 $this->_right_color=$this->_get_color($color);
691 }
692
693 }
694
695 ?>