dcd2a1ffc6f14db98fdbcf2aa9c583d12d2f6f2a
[p5sagit/Excel-Template.git] / lib / Excel / Template / Format.pm
1 package Excel::Template::Format;
2
3 use 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
12 {
13     # %_Parameters is a hash with the key being the format name and the value
14     # being the index/length of the format in the bit-vector.
15     my %_Formats = ( 
16         bold   => [ 0, 1 ],
17         italic => [ 1, 1 ],
18         locked => [ 2, 1 ],
19         hidden => [ 3, 1 ],
20         font_outline   => [ 4, 1 ],
21         font_shadow    => [ 5, 1 ],
22         font_strikeout => [ 6, 1 ],
23     );
24  
25     sub _params_to_vec
26     {
27         my %params = @_;
28         $params{lc $_} = delete $params{$_} for keys %params;
29  
30         my $vec = '';
31         vec( $vec, $_Formats{$_}[0], $_Formats{$_}[1] ) = ($params{$_} && 1)  
32             for grep { exists $_Formats{$_} } 
33                 map { lc } keys %params;
34  
35         $vec;
36     }
37  
38     sub _vec_to_params
39     {
40         my ($vec) = @_;
41  
42         my %params;
43         while (my ($k, $v) = each %_Formats) 
44         {
45             next unless vec( $vec, $v->[0], $v->[1] );
46             $params{$k} = 1;
47         }
48  
49         %params;
50     }
51 }
52
53 {
54     my %_Formats;
55
56     sub _assign {
57         $_Formats{$_[0]} = $_[1] unless exists $_Formats{$_[0]};
58         $_Formats{$_[1]} = $_[0] unless exists $_Formats{$_[1]};
59     }
60
61     sub _retrieve_vec    { ref($_[0]) ? ($_Formats{$_[0]}) : ($_[0]); }
62     sub _retrieve_format { ref($_[0]) ? ($_[0]) : ($_Formats{$_[0]}); }
63 }
64
65 sub blank_format
66 {
67     shift;
68     my ($context) = @_;
69
70     my $blank_vec = _params_to_vec();
71
72     my $format = _retrieve_format($blank_vec);
73     return $format if $format;
74
75     $format = $context->{XLS}->add_format;
76     _assign($blank_vec, $format);
77     $format;
78 }
79
80 sub copy
81 {
82     shift;
83     my ($context, $old_format, %properties) = @_;
84
85     defined(my $vec = _retrieve_vec($old_format))
86         || die "Internal Error: Cannot find vector for format '$old_format'!\n";
87
88     my $new_vec = _params_to_vec(%properties);
89
90     $new_vec |= $vec;
91
92     my $format = _retrieve_format($new_vec);
93     return $format if $format;
94
95     $format = $context->{XLS}->add_format(_vec_to_params($new_vec));
96     _assign($new_vec, $format);
97     $format;
98 }
99
100 1;
101 __END__
102
103 Category   Description       Property        Method Name          Implemented
104 --------   -----------       --------        -----------          -----------
105 Font       Font type         font            set_font()
106            Font size         size            set_size()
107            Font color        color           set_color()
108            Bold              bold            set_bold()              YES
109            Italic            italic          set_italic()            YES
110            Underline         underline       set_underline()
111            Strikeout         font_strikeout  set_font_strikeout()    YES
112            Super/Subscript   font_script     set_font_script()
113            Outline           font_outline    set_font_outline()      YES
114            Shadow            font_shadow     set_font_shadow()       YES
115
116 Number     Numeric format    num_format      set_num_format()
117
118 Protection Lock cells        locked          set_locked()            YES
119            Hide formulas     hidden          set_hidden()            YES
120
121 Alignment  Horizontal align  align           set_align()
122            Vertical align    valign          set_align()
123            Rotation          rotation        set_rotation()
124            Text wrap         text_wrap       set_text_wrap()
125            Justify last      text_justlast   set_text_justlast()
126            Merge             merge           set_merge()
127
128 Pattern    Cell pattern      pattern         set_pattern()
129            Background color  bg_color        set_bg_color()
130            Foreground color  fg_color        set_fg_color()
131
132 Border     Cell border       border          set_border()
133            Bottom border     bottom          set_bottom()
134            Top border        top             set_top()
135            Left border       left            set_left()
136            Right border      right           set_right()
137            Border color      border_color    set_border_color()
138            Bottom color      bottom_color    set_bottom_color()
139            Top color         top_color       set_top_color()
140            Left color        left_color      set_left_color()
141            Right color       right_color     set_right_color()