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