Added merge_range as submitted by Stevan Little
[p5sagit/Excel-Template.git] / lib / Excel / Template / Format.pm
CommitLineData
d0eafc11 1package Excel::Template::Format;
2
3use strict;
4
5# This is the format repository. Spreadsheet::WriteExcel does not cache the
6# known formats. So, it is very possible to continually add the same format
7# over and over until you run out of RAM or addressability in the XLS file. In
8# real life, less than 10-20 formats are used, and they're re-used in various
9# places in the file. This provides a way of keeping track of already-allocated
10# formats and making new formats based on old ones.
11
9ee3aea0 12sub new { bless {}, shift }
13
14sub _assign { $_[0]{$_[1]} = $_[2]; $_[0]{$_[2]} = $_[1] }
15# my $self = shift;
16# my ($key, $format) = @_;
17# $self->{$key} = $format;
18# $self->{$format} = $key;
19#}
20
21sub _retrieve_key { $_[0]{ $_[1] } }
22# my $self = shift;
23# my ($format) = @_;
24# return $self->{$format};
25#}
26
27*_retrieve_format = \&_retrieve_key;
28#sub _retrieve_format {
29# my $self = shift;
30# my ($key) = @_;
31# return $self->{$key};
32#}
c09684ff 33
34{
35 my @_boolean_formats = qw(
36 bold italic locked hidden font_outline font_shadow font_strikeout
ddb9abcf 37 text_wrap text_justlast shrink is_merged
d0eafc11 38 );
c09684ff 39
40 my @_integer_formats = qw(
41 size num_format underline rotation indent pattern border
42 bottom top left right
43 );
44
45 my @_string_formats = qw(
46 font color align valign bg_color fg_color border_color
47 bottom_color top_color left_color right_color
48 );
49
ddb9abcf 50 my @_fake_slots = qw(
51 is_merged
52 );
53
c09684ff 54 sub _params_to_key
d0eafc11 55 {
56 my %params = @_;
57 $params{lc $_} = delete $params{$_} for keys %params;
c09684ff 58
59 my @parts = (
d5382a2d 60 (map { $params{$_} ? 1 : '' } @_boolean_formats),
c09684ff 61 (map { $params{$_} ? $params{$_} + 0 : '' } @_integer_formats),
62 (map { $params{$_} || '' } @_string_formats),
63 );
64
65 return join( "\n", @parts );
d0eafc11 66 }
c09684ff 67
68 sub _key_to_params
d0eafc11 69 {
c09684ff 70 my ($key) = @_;
71
72 my @key_parts = split /\n/, $key;
73
74 my @boolean_parts = splice @key_parts, 0, scalar( @_boolean_formats );
75 my @integer_parts = splice @key_parts, 0, scalar( @_integer_formats );
76 my @string_parts = splice @key_parts, 0, scalar( @_string_formats );
77
d0eafc11 78 my %params;
d5382a2d 79 $params{ $_boolean_formats[$_] } = ~~1
c09684ff 80 for grep { $boolean_parts[$_] } 0 .. $#_boolean_formats;
d0eafc11 81
c09684ff 82 $params{ $_integer_formats[$_] } = $integer_parts[$_]
d5382a2d 83 for grep { defined $integer_parts[$_] && length $integer_parts[$_] } 0 .. $#_integer_formats;
c09684ff 84
85 $params{ $_string_formats[$_] } = $string_parts[$_]
86 for grep { $string_parts[$_] } 0 .. $#_string_formats;
d0eafc11 87
c09684ff 88 return %params;
d0eafc11 89 }
90
c09684ff 91 sub copy
92 {
9ee3aea0 93 my $self = shift;
c09684ff 94 my ($context, $old_fmt, %properties) = @_;
d0eafc11 95
b6bc5a5d 96 # This is a key used for non-format book-keeping.
97 delete $properties{ ELEMENTS };
98
9ee3aea0 99 defined(my $key = _retrieve_key($self, $old_fmt))
c09684ff 100 || die "Internal Error: Cannot find key for format '$old_fmt'!\n";
101
102 my %params = _key_to_params($key);
103 PROPERTY:
104 while ( my ($prop, $value) = each %properties )
105 {
106 $prop = lc $prop;
107 foreach (@_boolean_formats)
108 {
109 if ($prop eq $_) {
110 $params{$_} = ($value && $value !~ /false/i);
111 next PROPERTY;
112 }
113 }
114 foreach (@_integer_formats, @_string_formats)
115 {
116 if ($prop eq $_) {
117 $params{$_} = $value;
118 next PROPERTY;
119 }
120 }
b6bc5a5d 121
dee1f239 122 warn "Property '$prop' is unrecognized\n" if $^W;
c09684ff 123 }
d0eafc11 124
c09684ff 125 my $new_key = _params_to_key(%params);
d0eafc11 126
9ee3aea0 127 my $format = _retrieve_format($self, $new_key);
c09684ff 128 return $format if $format;
d0eafc11 129
ddb9abcf 130 delete $params{$_} for @_fake_slots;
131
c09684ff 132 $format = $context->{XLS}->add_format(%params);
9ee3aea0 133 _assign($self, $new_key, $format);
c09684ff 134 return $format;
135 }
d0eafc11 136}
137
c09684ff 138sub blank_format
d0eafc11 139{
9ee3aea0 140 my $self = shift;
c09684ff 141 my ($context) = @_;
d0eafc11 142
c09684ff 143 my $blank_key = _params_to_key();
d0eafc11 144
9ee3aea0 145 my $format = _retrieve_format($self, $blank_key);
d0eafc11 146 return $format if $format;
147
c09684ff 148 $format = $context->{XLS}->add_format;
9ee3aea0 149 _assign($self, $blank_key, $format);
c09684ff 150 return $format;
d0eafc11 151}
152
1531;
154__END__
6dd4c89d 155
156=head1 NAME
157
158Excel::Template::Format - Excel::Template::Format
159
160=head1 PURPOSE
161
162Helper class for FORMAT
163
164=head1 NODE NAME
165
166None
167
168=head1 INHERITANCE
169
170None
171
172=head1 ATTRIBUTES
173
174None
175
176=head1 CHILDREN
177
178None
179
180=head1 EFFECTS
181
182None
183
184=head1 DEPENDENCIES
185
186None
187
188=head1 METHODS
189
190=head2 blank_format
191
192Provides a blank format for use
193
194=head2 copy
195
196Clones an existing format, so that a new format can be built from it
197
198=head1 AUTHOR
199
200Rob Kinyon (rob.kinyon@gmail.com)
201
202=head1 SEE ALSO
203
204FORMAT
205
206=cut