WriteExcel/Format.pm

filter: Statementstmt 72.5% 203 / 280 Branchbran 37.0% 57 / 154 Conditioncond 41.5% 27 / 65 MC/DCmcdc 12.1% 4 / 33 Subroutinesub 69.2% 18 / 26 Podpod 0.0% 0 / 16 totaltotal 53.8% 309 / 574 CC 100 SCAR 47.3
CC
100
Cov
58%
set_align
462.0
get_xf
89.9
set_rotation
42.0
CRAP 113.5
↑ Summary WriteExcel/Workbook.pm »
1 package Spreadsheet::WriteExcel::Format;
2  
3 ###############################################################################
4 #
5 # Format - A class for defining Excel formatting.
6 #
7 #
8 # Used in conjunction with Spreadsheet::WriteExcel
9 #
10 # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
11 #
12 # Documentation after __END__
13 #
14  
15 33 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21  
22  
23  
24 33 ▶use vars qw($AUTOLOAD $VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 33 @ISA = qw(Exporter);
26  
27 33 $VERSION = '2.40';
28  
29 ###############################################################################
30 #
31 # new()
32 #
33 # Constructor
34 #
35 1653 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
36  
37 1653 my $class = shift;
38  
39 1653 ▶ my $self = {
Condition100%2/2
shift() || 0
l!l
1285368
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
40 _xf_index => shift || 0,
41  
42 _type => 0,
43 _font_index => 0,
44 _font => 'Arial',
45 _size => 10,
46 _bold => 0x0190,
47 _italic => 0,
48 _color => 0x7FFF,
49 _underline => 0,
50 _font_strikeout => 0,
51 _font_outline => 0,
52 _font_shadow => 0,
53 _font_script => 0,
54 _font_family => 0,
55 _font_charset => 0,
56 _font_encoding => 0,
57  
58 _num_format => 0,
59 _num_format_enc => 0,
60  
61 _hidden => 0,
62 _locked => 1,
63  
64 _text_h_align => 0,
65 _text_wrap => 0,
66 _text_v_align => 2,
67 _text_justlast => 0,
68 _rotation => 0,
69  
70 _fg_color => 0x40,
71 _bg_color => 0x41,
72  
73 _pattern => 0,
74  
75 _bottom => 0,
76 _top => 0,
77 _left => 0,
78 _right => 0,
79  
80 _bottom_color => 0x40,
81 _top_color => 0x40,
82 _left_color => 0x40,
83 _right_color => 0x40,
84  
85 _indent => 0,
86 _shrink => 0,
87 _merge_range => 0,
88 _reading_order => 0,
89  
90 _diag_type => 0,
91 _diag_color => 0x40,
92 _diag_border => 0,
93  
94 _font_only => 0,
95  
96 # Temp code to prevent merged formats in non-merged cells.
97 _used_merge => 0,
98  
99 };
100  
101 1653 bless $self, $class;
102  
103 # Set properties passed to Workbook::add_format()
104 1653 ▶ $self->set_format_properties(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1589 64 1653
105  
106 1653 return $self;
107 }
108  
109  
110 ###############################################################################
111 #
112 # copy($format)
113 #
114 # Copy the attributes of another Spreadsheet::WriteExcel::Format object.
115 #
116 0 ▶sub copy {
Subroutine copy: not called CC=3 SCAR=24.8
Pod undocumented
117 0 my $self = shift;
118 0 my $other = $_[0];
119  
120 0 ▶ return unless defined $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 0 0
121 0 ▶ return unless (ref($self) eq ref($other));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq ref $other 0 0 0
122  
123 # Store the properties that we don't want overwritten.
124 0 my $xf = $self->{_xf_index};
125 0 my $merge_range = $self->{_merge_range};
126 0 my $used_merge = $self->{_used_merge};
127  
128 0 %$self = %$other; # Copy properties
129  
130 # Restore saved properties.
131 0 $self->{_xf_index} = $xf;
132 0 $self->{_merge_range} = $merge_range;
133 0 $self->{_used_merge} = $used_merge;
134 }
135  
136  
137 ###############################################################################
138 #
139 # get_xf($style)
140 #
141 # Generate an Excel BIFF XF record.
142 #
143 1346 ▶sub get_xf {
Subroutine get_xf: called CC=43 SCAR=45.0
Pod undocumented
144  
145 33 ▶ use integer; # Avoid << shift bug in Perl 5.6.0 on HP-UX
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 1346 my $self = shift;
148  
149 1346 my $record; # Record identifier
150 my $length; # Number of bytes to follow
151  
152 1346 my $ifnt; # Index to FONT record
153 1346 my $ifmt; # Index to FORMAT record
154 1346 my $style; # Style and other options
155 1346 my $align; # Alignment
156 1346 my $indent; #
157 1346 my $icv; # fg and bg pattern colors
158 1346 my $border1; # Border line options
159 1346 my $border2; # Border line options
160 1346 my $border3; # Border line options
161  
162  
163 # Set the type of the XF record and some of the attributes.
164 1346 ▶ if ($self->{_type} == 0xFFF5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_type'} == 65525) { } 1220 126 1346
165 1220 $style = 0xFFF5;
166 }
167 else {
168 126 $style = $self->{_locked};
169 126 $style |= $self->{_hidden} << 1;
170 }
171  
172  
173 # Flags to indicate if attributes have been set.
174 1346 my $atr_num = ($self->{_num_format} != 0);
175  
176 1346 my $atr_fnt = ($self->{_font_index} != 0);
177  
178 my $atr_alc = ($self->{_text_h_align} != 0 ||
179 $self->{_text_v_align} != 2 ||
180 $self->{_shrink} != 0 ||
181 $self->{_merge_range} != 0 ||
182 $self->{_text_wrap} != 0 ||
183 1346 ▶ $self->{_indent} != 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_text_h_align'} != 0 || $self->{'_text_v_align'} != 2 || $self->{'_shrink'} != 0 || $self->{'_merge_range'} != 0 || $self->{'_text_wrap'} != 0 || $self->{'_indent'} != 0 ? : 2 1344 1346
Condition53%8/15
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2 or $self->{"_shrink"} != 0 or $self->{"_merge_range"} != 0 or $self->{"_text_wrap"} != 0 or $self->{"_indent"} != 0
l!l&&r!l&&!r
201344
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2 or $self->{"_shrink"} != 0 or $self->{"_merge_range"} != 0 or $self->{"_text_wrap"} != 0
l!l&&r!l&&!r
201344
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2 or $self->{"_shrink"} != 0 or $self->{"_merge_range"} != 0
l!l&&r!l&&!r
021344
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2 or $self->{"_shrink"} != 0
l!l&&r!l&&!r
001346
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2
l!l&&r!l&&!r
001346
MC/DC16%1/6
$self->{"_text_h_align"} != 0 or $self->{"_text_v_align"} != 2 or $self->{"_shrink"} != 0 or $self->{"_merge_range"} != 0 or $self->{"_text_wrap"} != 0 or $self->{"_indent"} != 0
$self->{"_text_h_align"} != 0 $self->{"_text_v_align"} != 2 $self->{"_shrink"} != 0 $self->{"_merge_range"} != 0 $self->{"_text_wrap"} != 0 $self->{"_indent"} != 0
Truth table28%2/7
A or B or C or D or E or F
A= $self->{"_text_h_align"} != 0B= $self->{"_text_v_align"} != 2C= $self->{"_shrink"} != 0D= $self->{"_merge_range"} != 0E= $self->{"_text_wrap"} != 0F= $self->{"_indent"} != 0
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
184  
185 my $atr_bdr = ($self->{_bottom} != 0 ||
186 $self->{_top} != 0 ||
187 $self->{_left} != 0 ||
188 $self->{_right} != 0 ||
189 1346 ▶ $self->{_diag_type} != 0) ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_bottom'} != 0 || $self->{'_top'} != 0 || $self->{'_left'} != 0 || $self->{'_right'} != 0 || $self->{'_diag_type'} != 0 ? : 0 1346 1346
Condition33%4/12
$self->{"_bottom"} != 0 or $self->{"_top"} != 0 or $self->{"_left"} != 0 or $self->{"_right"} != 0 or $self->{"_diag_type"} != 0
l!l&&r!l&&!r
001346
$self->{"_bottom"} != 0 or $self->{"_top"} != 0 or $self->{"_left"} != 0 or $self->{"_right"} != 0
l!l&&r!l&&!r
001346
$self->{"_bottom"} != 0 or $self->{"_top"} != 0 or $self->{"_left"} != 0
l!l&&r!l&&!r
001346
$self->{"_bottom"} != 0 or $self->{"_top"} != 0
l!l&&r!l&&!r
001346
MC/DC0%0/5
$self->{"_bottom"} != 0 or $self->{"_top"} != 0 or $self->{"_left"} != 0 or $self->{"_right"} != 0 or $self->{"_diag_type"} != 0
$self->{"_bottom"} != 0 $self->{"_top"} != 0 $self->{"_left"} != 0 $self->{"_right"} != 0 $self->{"_diag_type"} != 0
Truth table16%1/6
A or B or C or D or E
A= $self->{"_bottom"} != 0B= $self->{"_top"} != 0C= $self->{"_left"} != 0D= $self->{"_right"} != 0E= $self->{"_diag_type"} != 0
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
190  
191 my $atr_pat = ($self->{_fg_color} != 0x40 ||
192 $self->{_bg_color} != 0x41 ||
193 1346 ▶ $self->{_pattern} != 0x00) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_fg_color'} != 64 || $self->{'_bg_color'} != 65 || $self->{'_pattern'} != 0 ? : 0 1346 1346
Condition33%2/6
$self->{"_fg_color"} != 64 or $self->{"_bg_color"} != 65 or $self->{"_pattern"} != 0
l!l&&r!l&&!r
001346
$self->{"_fg_color"} != 64 or $self->{"_bg_color"} != 65
l!l&&r!l&&!r
001346
MC/DC0%0/3
$self->{"_fg_color"} != 64 or $self->{"_bg_color"} != 65 or $self->{"_pattern"} != 0
$self->{"_fg_color"} != 64 $self->{"_bg_color"} != 65 $self->{"_pattern"} != 0
Truth table25%1/4
A or B or C
A= $self->{"_fg_color"} != 64B= $self->{"_bg_color"} != 65C= $self->{"_pattern"} != 0
ABCresult
1XX1
01X1
0011
0000
194  
195 my $atr_prot = ($self->{_hidden} != 0 ||
196 1346 ▶ $self->{_locked} != 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_hidden'} != 0 || $self->{'_locked'} != 1 ? : 0 1346 1346
Condition33%1/3
$self->{"_hidden"} != 0 or $self->{"_locked"} != 1
l!l&&r!l&&!r
001346
MC/DC0%0/2
$self->{"_hidden"} != 0 or $self->{"_locked"} != 1
$self->{"_hidden"} != 0 $self->{"_locked"} != 1
Truth table33%1/3
A or B
A= $self->{"_hidden"} != 0B= $self->{"_locked"} != 1
ABresult
1X1
011
000
197  
198  
199 # Set attribute changed flags for the style formats.
200 1346 ▶ if ($self->{_xf_index} != 0 and $self->{_type} == 0xFFF5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_xf_index"} != 0 and $self->{"_type"} == 65525) 1159 187 1346
Condition100%3/3
$self->{"_xf_index"} != 0 and $self->{"_type"} == 65525
!ll&&!rl&&r
611261159
MC/DC100%2/2
$self->{"_xf_index"} != 0 and $self->{"_type"} == 65525
$self->{"_xf_index"} != 0 $self->{"_type"} == 65525
Truth table100%3/3
A and B
A= $self->{"_xf_index"} != 0B= $self->{"_type"} == 65525
ABresult
0X0
100
111
201  
202 1159 ▶ if ($self->{_xf_index} >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_xf_index'} >= 16) { } 305 854 1159
203 305 $atr_num = 0;
204 305 $atr_fnt = 1;
205 }
206 else {
207 854 $atr_num = 1;
208 854 $atr_fnt = 0;
209 }
210  
211 1159 $atr_alc = 1;
212 1159 $atr_bdr = 1;
213 1159 $atr_pat = 1;
214 1159 $atr_prot = 1;
215 }
216  
217  
218 # Set a default diagonal border style if none was specified.
219 1346 ▶ $self->{_diag_border} = 1 if !$self->{_diag_border} and $self->{_diag_type};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_diag_border"} and $self->{"_diag_type"} 0 1346 1346
Condition33%1/3
not $self->{"_diag_border"} and $self->{"_diag_type"}
!ll&&!rl&&r
013460
MC/DC0%0/2
not $self->{"_diag_border"} and $self->{"_diag_type"}
not $self->{"_diag_border"} $self->{"_diag_type"}
Truth table33%1/3
not(A) and B
A= not $self->{"_diag_border"}B= $self->{"_diag_type"}
ABresult
0X0
100
111
220  
221  
222 # Reset the default colours for the non-font properties
223 1346 ▶ $self->{_fg_color} = 0x40 if $self->{_fg_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fg_color"} == 32767 0 1346 1346
224 1346 ▶ $self->{_bg_color} = 0x41 if $self->{_bg_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_bg_color"} == 32767 0 1346 1346
225 1346 ▶ $self->{_bottom_color} = 0x40 if $self->{_bottom_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_bottom_color"} == 32767 0 1346 1346
226 1346 ▶ $self->{_top_color} = 0x40 if $self->{_top_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_top_color"} == 32767 0 1346 1346
227 1346 ▶ $self->{_left_color} = 0x40 if $self->{_left_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_left_color"} == 32767 0 1346 1346
228 1346 ▶ $self->{_right_color} = 0x40 if $self->{_right_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_right_color"} == 32767 0 1346 1346
229 1346 ▶ $self->{_diag_color} = 0x40 if $self->{_diag_color} == 0x7FFF;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_diag_color"} == 32767 0 1346 1346
230  
231  
232 # Zero the default border colour if the border has not been set.
233 1346 ▶ $self->{_bottom_color} = 0 if $self->{_bottom} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_bottom"} == 0 1346 0 1346
234 1346 ▶ $self->{_top_color} = 0 if $self->{_top} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_top"} == 0 1346 0 1346
235 1346 ▶ $self->{_right_color} = 0 if $self->{_right} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_right"} == 0 1346 0 1346
236 1346 ▶ $self->{_left_color} = 0 if $self->{_left} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_left"} == 0 1346 0 1346
237 1346 ▶ $self->{_diag_color} = 0 if $self->{_diag_type} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_diag_type"} == 0 1346 0 1346
238  
239  
240 # The following 2 logical statements take care of special cases in relation
241 # to cell colours and patterns:
242 # 1. For a solid fill (_pattern == 1) Excel reverses the role of foreground
243 # and background colours.
244 # 2. If the user specifies a foreground or background colour without a
245 # pattern they probably wanted a solid fill, so we fill in the defaults.
246 #
247 1346 ▶ if ($self->{_pattern} <= 0x01 and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pattern"} <= 1 and $self->{"_bg_color"} != 65 and $self->{"_fg_color"} == 64) 0 1346 1346
Condition33%2/6
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} != 65 and $self->{"_fg_color"} == 64
!ll&&!rl&&r
134600
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} != 65
!ll&&!rl&&r
013460
MC/DC0%0/3
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} != 65 and $self->{"_fg_color"} == 64
$self->{"_pattern"} <= 1 $self->{"_bg_color"} != 65 $self->{"_fg_color"} == 64
Truth table25%1/4
A and B and C
A= $self->{"_pattern"} <= 1B= $self->{"_bg_color"} != 65C= $self->{"_fg_color"} == 64
ABCresult
0XX0
10X0
1100
1111
248 $self->{_bg_color} != 0x41 and
249 $self->{_fg_color} == 0x40 )
250 {
251 0 $self->{_fg_color} = $self->{_bg_color};
252 0 $self->{_bg_color} = 0x40;
253 0 $self->{_pattern} = 1;
254 }
255  
256 1346 ▶ if ($self->{_pattern} <= 0x01 and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pattern"} <= 1 and $self->{"_bg_color"} == 65 and $self->{"_fg_color"} != 64) 0 1346 1346
Condition33%2/6
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} == 65 and $self->{"_fg_color"} != 64
!ll&&!rl&&r
013460
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} == 65
!ll&&!rl&&r
001346
MC/DC0%0/3
$self->{"_pattern"} <= 1 and $self->{"_bg_color"} == 65 and $self->{"_fg_color"} != 64
$self->{"_pattern"} <= 1 $self->{"_bg_color"} == 65 $self->{"_fg_color"} != 64
Truth table25%1/4
A and B and C
A= $self->{"_pattern"} <= 1B= $self->{"_bg_color"} == 65C= $self->{"_fg_color"} != 64
ABCresult
0XX0
10X0
1100
1111
257 $self->{_bg_color} == 0x41 and
258 $self->{_fg_color} != 0x40 )
259 {
260 0 $self->{_bg_color} = 0x40;
261 0 $self->{_pattern} = 1;
262 }
263  
264  
265 # Set default alignment if indent is set.
266 $self->{_text_h_align} = 1 if $self->{_indent} and
267 1346 ▶ $self->{_text_h_align} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_indent"} and $self->{"_text_h_align"} == 0 0 1346 1346
Condition33%1/3
$self->{"_indent"} and $self->{"_text_h_align"} == 0
!ll&&!rl&&r
134600
MC/DC0%0/2
$self->{"_indent"} and $self->{"_text_h_align"} == 0
$self->{"_indent"} $self->{"_text_h_align"} == 0
Truth table33%1/3
A and B
A= $self->{"_indent"}B= $self->{"_text_h_align"} == 0
ABresult
0X0
100
111
268  
269  
270 1346 $record = 0x00E0;
271 1346 $length = 0x0014;
272  
273 1346 $ifnt = $self->{_font_index};
274 1346 $ifmt = $self->{_num_format};
275  
276  
277 1346 $align = $self->{_text_h_align};
278 1346 $align |= $self->{_text_wrap} << 3;
279 1346 $align |= $self->{_text_v_align} << 4;
280 1346 $align |= $self->{_text_justlast} << 7;
281 1346 $align |= $self->{_rotation} << 8;
282  
283  
284  
285 1346 $indent = $self->{_indent};
286 1346 $indent |= $self->{_shrink} << 4;
287 1346 $indent |= $self->{_merge_range} << 5;
288 1346 $indent |= $self->{_reading_order} << 6;
289 1346 $indent |= $atr_num << 10;
290 1346 $indent |= $atr_fnt << 11;
291 1346 $indent |= $atr_alc << 12;
292 1346 $indent |= $atr_bdr << 13;
293 1346 $indent |= $atr_pat << 14;
294 1346 $indent |= $atr_prot << 15;
295  
296  
297 1346 $border1 = $self->{_left};
298 1346 $border1 |= $self->{_right} << 4;
299 1346 $border1 |= $self->{_top} << 8;
300 1346 $border1 |= $self->{_bottom} << 12;
301  
302 1346 $border2 = $self->{_left_color};
303 1346 $border2 |= $self->{_right_color} << 7;
304 1346 $border2 |= $self->{_diag_type} << 14;
305  
306  
307 1346 $border3 = $self->{_top_color};
308 1346 $border3 |= $self->{_bottom_color} << 7;
309 1346 $border3 |= $self->{_diag_color} << 14;
310 1346 $border3 |= $self->{_diag_border} << 21;
311 1346 $border3 |= $self->{_pattern} << 26;
312  
313 1346 $icv = $self->{_fg_color};
314 1346 $icv |= $self->{_bg_color} << 7;
315  
316  
317  
318 1346 my $header = pack("vv", $record, $length);
319 1346 my $data = pack("vvvvvvvVv", $ifnt, $ifmt, $style,
320 $align, $indent,
321 $border1, $border2, $border3,
322 $icv);
323  
324 1346 return($header . $data);
325 }
326  
327  
328 ###############################################################################
329 #
330 # Note to porters. The majority of the set_property() methods are created
331 # dynamically via Perl' AUTOLOAD sub, see below. You may prefer/have to specify
332 # them explicitly in other implementation languages.
333 #
334  
335  
336 ###############################################################################
337 #
338 # get_font()
339 #
340 # Generate an Excel BIFF FONT record.
341 #
342 428 ▶sub get_font {
Subroutine get_font: called CC=10 SCAR=24.6
Pod undocumented
343  
344 428 my $self = shift;
345  
346 428 my $record; # Record identifier
347 my $length; # Record length
348  
349 428 my $dyHeight; # Height of font (1/20 of a point)
350 428 my $grbit; # Font attributes
351 428 my $icv; # Index to color palette
352 428 my $bls; # Bold style
353 428 my $sss; # Superscript/subscript
354 428 my $uls; # Underline
355 428 my $bFamily; # Font family
356 428 my $bCharSet; # Character set
357 428 my $reserved; # Reserved
358 428 my $cch; # Length of font name
359 428 my $rgch; # Font name
360 428 my $encoding; # Font name character encoding
361  
362  
363 428 $dyHeight = $self->{_size} * 20;
364 428 $icv = $self->{_color};
365 428 $bls = $self->{_bold};
366 428 $sss = $self->{_font_script};
367 428 $uls = $self->{_underline};
368 428 $bFamily = $self->{_font_family};
369 428 $bCharSet = $self->{_font_charset};
370 428 $rgch = $self->{_font};
371 428 $encoding = $self->{_font_encoding};
372  
373 # Handle utf8 strings in perl 5.8.
374 428 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 428 0 428
375 428 require Encode;
376  
377 428 ▶ if (Encode::is_utf8($rgch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($rgch)) 0 428 428
378 0 $rgch = Encode::encode("UTF-16BE", $rgch);
379 0 $encoding = 1;
380 }
381 }
382  
383 428 $cch = length $rgch;
384  
385 # Handle Unicode font names.
386 428 ▶ if ($encoding == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 1) 0 428 428
387 0 ▶ croak "Uneven number of bytes in Unicode font name" if $cch % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $cch % 2 0 0 0
388 0 ▶ $cch /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 0 0
389 0 $rgch = pack 'v*', unpack 'n*', $rgch;
390 }
391  
392 428 $record = 0x31;
393 428 $length = 0x10 + length $rgch;
394 428 $reserved = 0x00;
395  
396 428 $grbit = 0x00;
397 428 ▶ $grbit |= 0x02 if $self->{_italic};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_italic"} 0 428 428
398 428 ▶ $grbit |= 0x08 if $self->{_font_strikeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_font_strikeout"} 0 428 428
399 428 ▶ $grbit |= 0x10 if $self->{_font_outline};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_font_outline"} 0 428 428
400 428 ▶ $grbit |= 0x20 if $self->{_font_shadow};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_font_shadow"} 0 428 428
401  
402  
403 428 my $header = pack("vv", $record, $length);
404 428 my $data = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls,
405 $sss, $uls, $bFamily,
406 $bCharSet, $reserved, $cch, $encoding);
407  
408 428 return($header . $data . $rgch);
409 }
410  
411 ###############################################################################
412 #
413 # get_font_key()
414 #
415 # Returns a unique hash key for a font. Used by Workbook->_store_all_fonts()
416 #
417 1407 ▶sub get_font_key {
Subroutine get_font_key: called CC=1 SCAR=0.0
Pod undocumented
418  
419 1407 my $self = shift;
420  
421 # The following elements are arranged to increase the probability of
422 # generating a unique key. Elements that hold a large range of numbers
423 # e.g. _color are placed between two binary elements such as _italic
424 #
425 1407 my $key = "$self->{_font}$self->{_size}";
426 1407 $key .= "$self->{_font_script}$self->{_underline}";
427 1407 $key .= "$self->{_font_strikeout}$self->{_bold}$self->{_font_outline}";
428 1407 $key .= "$self->{_font_family}$self->{_font_charset}";
429 1407 $key .= "$self->{_font_shadow}$self->{_color}$self->{_italic}";
430 1407 $key .= "$self->{_font_encoding}";
431 1407 $key =~ s/ /_/g; # Convert the key to a single word
432  
433 1407 return $key;
434 }
435  
436  
437 ###############################################################################
438 #
439 # get_xf_index()
440 #
441 # Returns the index used by Worksheet->_XF()
442 #
443 23 ▶sub get_xf_index {
Subroutine get_xf_index: called CC=1 SCAR=0.0
Pod undocumented
444 23 my $self = shift;
445  
446 23 return $self->{_xf_index};
447 }
448  
449  
450 ###############################################################################
451 #
452 # _get_color()
453 #
454 # Used in conjunction with the set_xxx_color methods to convert a color
455 # string into a number. Color range is 0..63 but we will restrict it
456 # to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
457 #
458 22623 ▶sub _get_color {
Subroutine _get_color: called CC=6 SCAR=27.6
459  
460 22623 my %colors = (
461 aqua => 0x0F,
462 cyan => 0x0F,
463 black => 0x08,
464 blue => 0x0C,
465 brown => 0x10,
466 magenta => 0x0E,
467 fuchsia => 0x0E,
468 gray => 0x17,
469 grey => 0x17,
470 green => 0x11,
471 lime => 0x0B,
472 navy => 0x12,
473 orange => 0x35,
474 pink => 0x21,
475 purple => 0x14,
476 red => 0x0A,
477 silver => 0x16,
478 white => 0x09,
479 yellow => 0x0D,
480 );
481  
482 # Return the default color, 0x7FFF, if undef,
483 22623 ▶ return 0x7FFF unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 22562 61 22623
484  
485 # or the color string converted to an integer,
486 61 ▶ return $colors{lc($_[0])} if exists $colors{lc($_[0])};
Branch: 1 branch
BranchTrueFalseTotal
if exists $colors{lc $_[0]} 61 0 61
487  
488 # or the default color if string is unrecognised,
489 0 ▶ return 0x7FFF if ($_[0] =~ m/\D/);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\D/ 0 0 0
490  
491 # or an index < 8 mapped into the correct range,
492 0 ▶ return $_[0] + 8 if $_[0] < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 8 0 0 0
493  
494 # or the default color if arg is outside range,
495 0 ▶ return 0x7FFF if $_[0] > 63;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] > 63 0 0 0
496  
497 # or an integer in the valid range
498 0 return $_[0];
499 }
500  
501  
502 ###############################################################################
503 #
504 # set_type()
505 #
506 # Set the XF object type as 0 = cell XF or 0xFFF5 = style XF.
507 #
508 1220 ▶sub set_type {
Subroutine set_type: called CC=3 SCAR=12.7
Pod undocumented
509  
510 1220 my $self = shift;
511 1220 my $type = $_[0];
512  
513 1220 ▶ if (defined $_[0] and $_[0] eq 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq 0) { } 0 1220 1220
Condition33%1/3
defined $_[0] and $_[0] eq 0
!ll&&!rl&&r
012200
MC/DC0%0/2
defined $_[0] and $_[0] eq 0
defined $_[0] $_[0] eq 0
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq 0
ABresult
0X0
100
111
514 0 $self->{_type} = 0x0000;
515 }
516 else {
517 1220 $self->{_type} = 0xFFF5;
518 }
519 }
520  
521  
522 ###############################################################################
523 #
524 # set_align()
525 #
526 # Set cell alignment.
527 #
528 0 ▶sub set_align {
Subroutine set_align: not called CC=21 SCAR=61.4
Pod undocumented
529  
530 0 my $self = shift;
531 0 my $location = $_[0];
532  
533 0 ▶ return if not defined $location; # No default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location 0 0 0
534 0 ▶ return if $location =~ m/\d/; # Ignore numbers
Branch: 1 branch
BranchTrueFalseTotal
if $location =~ /\d/ 0 0 0
535  
536 0 $location = lc($location);
537  
538 0 ▶ $self->set_text_h_align(1) if ($location eq 'left');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "left" 0 0 0
539 0 ▶ $self->set_text_h_align(2) if ($location eq 'centre');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "centre" 0 0 0
540 0 ▶ $self->set_text_h_align(2) if ($location eq 'center');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "center" 0 0 0
541 0 ▶ $self->set_text_h_align(3) if ($location eq 'right');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "right" 0 0 0
542 0 ▶ $self->set_text_h_align(4) if ($location eq 'fill');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "fill" 0 0 0
543 0 ▶ $self->set_text_h_align(5) if ($location eq 'justify');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "justify" 0 0 0
544 0 ▶ $self->set_text_h_align(6) if ($location eq 'center_across');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "center_across" 0 0 0
545 0 ▶ $self->set_text_h_align(6) if ($location eq 'centre_across');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "centre_across" 0 0 0
546 0 ▶ $self->set_text_h_align(6) if ($location eq 'merge'); # S:WE name
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "merge" 0 0 0
547 0 ▶ $self->set_text_h_align(7) if ($location eq 'distributed');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "distributed" 0 0 0
548 0 ▶ $self->set_text_h_align(7) if ($location eq 'equal_space'); # ParseExcel
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "equal_space" 0 0 0
549  
550  
551 0 ▶ $self->set_text_v_align(0) if ($location eq 'top');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "top" 0 0 0
552 0 ▶ $self->set_text_v_align(1) if ($location eq 'vcentre');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "vcentre" 0 0 0
553 0 ▶ $self->set_text_v_align(1) if ($location eq 'vcenter');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "vcenter" 0 0 0
554 0 ▶ $self->set_text_v_align(2) if ($location eq 'bottom');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "bottom" 0 0 0
555 0 ▶ $self->set_text_v_align(3) if ($location eq 'vjustify');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "vjustify" 0 0 0
556 0 ▶ $self->set_text_v_align(4) if ($location eq 'vdistributed');
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "vdistributed" 0 0 0
557 0 ▶ $self->set_text_v_align(4) if ($location eq 'vequal_space'); # ParseExcel
Branch: 1 branch
BranchTrueFalseTotal
if $location eq "vequal_space" 0 0 0
558 }
559  
560  
561 ###############################################################################
562 #
563 # set_valign()
564 #
565 # Set vertical cell alignment. This is required by the set_format_properties()
566 # method to differentiate between the vertical and horizontal properties.
567 #
568 0 ▶sub set_valign {
Subroutine set_valign: not called CC=1 SCAR=6.9
Pod undocumented
569  
570 0 my $self = shift;
571 0 $self->set_align(@_);
572 }
573  
574  
575 ###############################################################################
576 #
577 # set_center_across()
578 #
579 # Implements the Excel5 style "merge".
580 #
581 0 ▶sub set_center_across {
Subroutine set_center_across: not called CC=1 SCAR=6.9
Pod undocumented
582  
583 0 my $self = shift;
584  
585 0 $self->set_text_h_align(6);
586 }
587  
588  
589 ###############################################################################
590 #
591 # set_merge()
592 #
593 # This was the way to implement a merge in Excel5. However it should have been
594 # called "center_across" and not "merge".
595 # This is now deprecated. Use set_center_across() or better merge_range().
596 #
597 #
598 0 ▶sub set_merge {
Subroutine set_merge: not called CC=1 SCAR=6.9
Pod undocumented
599  
600 0 my $self = shift;
601  
602 0 $self->set_text_h_align(6);
603 }
604  
605  
606 ###############################################################################
607 #
608 # set_bold()
609 #
610 # Bold has a range 0x64..0x3E8.
611 # 0x190 is normal. 0x2BC is bold. So is an excessive use of AUTOLOAD.
612 #
613 124 ▶sub set_bold {
Subroutine set_bold: called CC=6 SCAR=19.1
Pod undocumented
614  
615 124 my $self = shift;
616 124 my $weight = $_[0];
617  
618 124 ▶ $weight = 0x2BC if not defined $weight; # Bold text
Branch: 1 branch
BranchTrueFalseTotal
unless defined $weight 0 124 124
619 124 ▶ $weight = 0x2BC if $weight == 1; # Bold text
Branch: 1 branch
BranchTrueFalseTotal
if $weight == 1 124 0 124
620 124 ▶ $weight = 0x190 if $weight == 0; # Normal text
Branch: 1 branch
BranchTrueFalseTotal
if $weight == 0 0 124 124
621 124 ▶ $weight = 0x190 if $weight < 0x064; # Lower bound
Branch: 1 branch
BranchTrueFalseTotal
if $weight < 100 0 124 124
622 124 ▶ $weight = 0x190 if $weight > 0x3E8; # Upper bound
Branch: 1 branch
BranchTrueFalseTotal
if $weight > 1000 0 124 124
623  
624 124 $self->{_bold} = $weight;
625 }
626  
627  
628 ###############################################################################
629 #
630 # set_border($style)
631 #
632 # Set cells borders to the same style
633 #
634 0 ▶sub set_border {
Subroutine set_border: not called CC=1 SCAR=6.9
Pod undocumented
635  
636 0 my $self = shift;
637 0 my $style = $_[0];
638  
639 0 $self->set_bottom($style);
640 0 $self->set_top($style);
641 0 $self->set_left($style);
642 0 $self->set_right($style);
643 }
644  
645  
646 ###############################################################################
647 #
648 # set_border_color($color)
649 #
650 # Set cells border to the same color
651 #
652 0 ▶sub set_border_color {
Subroutine set_border_color: not called CC=1 SCAR=6.9
Pod undocumented
653  
654 0 my $self = shift;
655 0 my $color = $_[0];
656  
657 0 $self->set_bottom_color($color);
658 0 $self->set_top_color($color);
659 0 $self->set_left_color($color);
660 0 $self->set_right_color($color);
661 }
662  
663  
664 ###############################################################################
665 #
666 # set_rotation($angle)
667 #
668 # Set the rotation angle of the text. An alignment property.
669 #
670 0 ▶sub set_rotation {
Subroutine set_rotation: not called CC=6 SCAR=37.4
Pod undocumented
671  
672 0 my $self = shift;
673 0 my $rotation = $_[0];
674  
675 # Argument should be a number
676 0 ▶ return if $rotation !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rotation =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ 0 0 0
677  
678 # The arg type can be a double but the Excel dialog only allows integers.
679 0 $rotation = int $rotation;
680  
681 0 ▶ if ($rotation == 270) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rotation == 270) { } 0 0 0
elsif ($rotation >= -90 or $rotation <= 90) { } 0 0 0
682 0 $rotation = 255;
683 }
684 ▶ elsif ($rotation >= -90 or $rotation <= 90) {
Condition0%0/3
$rotation >= -90 or $rotation <= 90
l!l&&r!l&&!r
000
MC/DC0%0/2
$rotation >= -90 or $rotation <= 90
$rotation >= -90 $rotation <= 90
Truth table0%0/3
A or B
A= $rotation >= -90B= $rotation <= 90
ABresult
1X1
011
000
685 0 ▶ $rotation = -$rotation +90 if $rotation < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation < 0 0 0 0
686 }
687 else {
688 0 carp "Rotation $rotation outside range: -90 <= angle <= 90";
689 0 $rotation = 0;
690 }
691  
692 0 $self->{_rotation} = $rotation;
693 }
694  
695  
696 ###############################################################################
697 #
698 # set_format_properties()
699 #
700 # Convert hashes of properties to method calls.
701 #
702 1590 ▶sub set_format_properties {
Subroutine set_format_properties: called CC=2 SCAR=6.9
Pod undocumented
703  
704 1590 my $self = shift;
705  
706 1590 my %properties = @_; # Merge multiple hashes into one
707  
708 1590 ▶ while (my($key, $value) = each(%properties)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %properties) 2200 1590 3790
709  
710 # Strip leading "-" from Tk style properties e.g. -color => 'red'.
711 2200 $key =~ s/^-//;
712  
713 # Create a sub to set the property.
714 2200 my $sub = \&{"set_$key"};
715 2200 $sub->($self, $value);
716 }
717 }
718  
719 # Renamed rarely used set_properties() to set_format_properties() to avoid
720 # confusion with Workbook method of the same name. The following acts as an
721 # alias for any code that uses the old name.
722 33 *set_properties = *set_format_properties;
723  
724  
725 ###############################################################################
726 #
727 # AUTOLOAD. Deus ex machina.
728 #
729 # Dynamically create set methods that aren't already defined.
730 #
731 1824 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=17.9
732  
733 1824 my $self = shift;
734  
735 # Ignore calls to DESTROY
736 1824 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Spreadsheet::WriteExcel::Format::AUTOLOAD =~ /::DESTROY$/ 1652 172 1824
737  
738 # Check for a valid method names, i.e. "set_xxx_yyy".
739 172 ▶ $AUTOLOAD =~ /.*::set(\w+)/ or die "Unknown method: $AUTOLOAD\n";
Branch: 1 branch
BranchTrueFalseTotal
$Spreadsheet::WriteExcel::Format::AUTOLOAD =~ /.*::set(\w+)/ or die "Unknown method: $Spreadsheet::WriteExcel::Format::AUTOLOAD\n" 172 0 172
740  
741 # Match the attribute, i.e. "_xxx_yyy".
742 172 my $attribute = $1;
743  
744 # Check that the attribute exists
745 172 ▶ exists $self->{$attribute} or die "Unknown method: $AUTOLOAD\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$attribute} or die "Unknown method: $Spreadsheet::WriteExcel::Format::AUTOLOAD\n" 172 0 172
746  
747 # The attribute value
748 172 my $value;
749  
750  
751 # There are two types of set methods: set_property() and
752 # set_property_color(). When a method is AUTOLOADED we store a new anonymous
753 # sub in the appropriate slot in the symbol table. The speeds up subsequent
754 # calls to the same method.
755 #
756 33 ▶ no strict 'refs'; # To allow symbol table hackery
Subroutine BEGIN: called CC=1 SCAR=0.0
757  
758 172 ▶ if ($AUTOLOAD =~ /.*::set\w+color$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Spreadsheet::WriteExcel::Format::AUTOLOAD =~ /.*::set\w+color$/) { } 28 144 172
759 # For "set_property_color" methods
760 28 $value = _get_color($_[0]);
761  
762 28 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
763 33 my $self = shift;
764  
765 33 $self->{$attribute} = _get_color($_[0]);
766 28 };
767 }
768 else {
769  
770 144 $value = $_[0];
771 144 ▶ $value = 1 if not defined $value; # The default value is always 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 142 144
772  
773 144 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
774 654 my $self = shift;
775 654 my $value = shift;
776  
777 654 ▶ $value = 1 if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 653 654
778 654 $self->{$attribute} = $value;
779 144 };
780 }
781  
782  
783 172 $self->{$attribute} = $value;
784 }
785  
786  
787 33 1;
788  
789  
790 __END__
↑ Summary WriteExcel/Workbook.pm »