File: Styles.pm

package info (click to toggle)
libexcel-writer-xlsx-perl 0.79-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,716 kB
  • ctags: 930
  • sloc: perl: 18,380; makefile: 40
file content (1133 lines) | stat: -rw-r--r-- 26,114 bytes parent folder | download
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
package Excel::Writer::XLSX::Package::Styles;

###############################################################################
#
# Styles - A class for writing the Excel XLSX styles file.
#
# Used in conjunction with Excel::Writer::XLSX
#
# Copyright 2000-2014, John McNamara, jmcnamara@cpan.org
#
# Documentation after __END__
#

# perltidy with the following options: -mbl=2 -pt=0 -nola

use 5.008002;
use strict;
use warnings;
use Carp;
use Excel::Writer::XLSX::Package::XMLwriter;

our @ISA     = qw(Excel::Writer::XLSX::Package::XMLwriter);
our $VERSION = '0.79';


###############################################################################
#
# Public and private API methods.
#
###############################################################################


###############################################################################
#
# new()
#
# Constructor.
#
sub new {

    my $class = shift;
    my $fh    = shift;
    my $self  = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );

    $self->{_xf_formats}       = undef;
    $self->{_palette}          = [];
    $self->{_font_count}       = 0;
    $self->{_num_format_count} = 0;
    $self->{_border_count}     = 0;
    $self->{_fill_count}       = 0;
    $self->{_custom_colors}    = [];
    $self->{_dxf_formats}      = [];

    bless $self, $class;

    return $self;
}


###############################################################################
#
# _assemble_xml_file()
#
# Assemble and write the XML file.
#
sub _assemble_xml_file {

    my $self = shift;

    $self->xml_declaration;

    # Add the style sheet.
    $self->_write_style_sheet();

    # Write the number formats.
    $self->_write_num_fmts();

    # Write the fonts.
    $self->_write_fonts();

    # Write the fills.
    $self->_write_fills();

    # Write the borders element.
    $self->_write_borders();

    # Write the cellStyleXfs element.
    $self->_write_cell_style_xfs();

    # Write the cellXfs element.
    $self->_write_cell_xfs();

    # Write the cellStyles element.
    $self->_write_cell_styles();

    # Write the dxfs element.
    $self->_write_dxfs();

    # Write the tableStyles element.
    $self->_write_table_styles();

    # Write the colors element.
    $self->_write_colors();

    # Close the style sheet tag.
    $self->xml_end_tag( 'styleSheet' );

    # Close the XML writer filehandle.
    $self->xml_get_fh()->close();
}


###############################################################################
#
# _set_style_properties()
#
# Pass in the Format objects and other properties used to set the styles.
#
sub _set_style_properties {

    my $self = shift;

    $self->{_xf_formats}       = shift;
    $self->{_palette}          = shift;
    $self->{_font_count}       = shift;
    $self->{_num_format_count} = shift;
    $self->{_border_count}     = shift;
    $self->{_fill_count}       = shift;
    $self->{_custom_colors}    = shift;
    $self->{_dxf_formats}      = shift;
}


###############################################################################
#
# Internal methods.
#
###############################################################################


###############################################################################
#
# _get_palette_color()
#
# Convert from an Excel internal colour index to a XML style #RRGGBB index
# based on the default or user defined values in the Workbook palette.
#
sub _get_palette_color {

    my $self    = shift;
    my $index   = shift;
    my $palette = $self->{_palette};

    # Handle colours in #XXXXXX RGB format.
    if ( $index =~ m/^#([0-9A-F]{6})$/i ) {
        return "FF" . uc( $1 );
    }

    # Adjust the colour index.
    $index -= 8;

    # Palette is passed in from the Workbook class.
    my @rgb = @{ $palette->[$index] };

    return sprintf "FF%02X%02X%02X", @rgb;
}


###############################################################################
#
# XML writing methods.
#
###############################################################################


##############################################################################
#
# _write_style_sheet()
#
# Write the <styleSheet> element.
#
sub _write_style_sheet {

    my $self  = shift;
    my $xmlns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';

    my @attributes = ( 'xmlns' => $xmlns );

    $self->xml_start_tag( 'styleSheet', @attributes );
}


##############################################################################
#
# _write_num_fmts()
#
# Write the <numFmts> element.
#
sub _write_num_fmts {

    my $self  = shift;
    my $count = $self->{_num_format_count};

    return unless $count;

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'numFmts', @attributes );

    # Write the numFmts elements.
    for my $format ( @{ $self->{_xf_formats} } ) {

        # Ignore built-in number formats, i.e., < 164.
        next unless $format->{_num_format_index} >= 164;
        $self->_write_num_fmt( $format->{_num_format_index},
            $format->{_num_format} );
    }

    $self->xml_end_tag( 'numFmts' );
}


##############################################################################
#
# _write_num_fmt()
#
# Write the <numFmt> element.
#
sub _write_num_fmt {

    my $self        = shift;
    my $num_fmt_id  = shift;
    my $format_code = shift;

    my %format_codes = (
        0  => 'General',
        1  => '0',
        2  => '0.00',
        3  => '#,##0',
        4  => '#,##0.00',
        5  => '($#,##0_);($#,##0)',
        6  => '($#,##0_);[Red]($#,##0)',
        7  => '($#,##0.00_);($#,##0.00)',
        8  => '($#,##0.00_);[Red]($#,##0.00)',
        9  => '0%',
        10 => '0.00%',
        11 => '0.00E+00',
        12 => '# ?/?',
        13 => '# ??/??',
        14 => 'm/d/yy',
        15 => 'd-mmm-yy',
        16 => 'd-mmm',
        17 => 'mmm-yy',
        18 => 'h:mm AM/PM',
        19 => 'h:mm:ss AM/PM',
        20 => 'h:mm',
        21 => 'h:mm:ss',
        22 => 'm/d/yy h:mm',
        37 => '(#,##0_);(#,##0)',
        38 => '(#,##0_);[Red](#,##0)',
        39 => '(#,##0.00_);(#,##0.00)',
        40 => '(#,##0.00_);[Red](#,##0.00)',
        41 => '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)',
        42 => '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)',
        43 => '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)',
        44 => '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)',
        45 => 'mm:ss',
        46 => '[h]:mm:ss',
        47 => 'mm:ss.0',
        48 => '##0.0E+0',
        49 => '@',
    );

    # Set the format code for built-in number formats.
    if ( $num_fmt_id < 164 ) {
        if ( exists $format_codes{$num_fmt_id} ) {
            $format_code = $format_codes{$num_fmt_id};
        }
        else {
            $format_code = 'General';
        }
    }

    my @attributes = (
        'numFmtId'   => $num_fmt_id,
        'formatCode' => $format_code,
    );

    $self->xml_empty_tag( 'numFmt', @attributes );
}


##############################################################################
#
# _write_fonts()
#
# Write the <fonts> element.
#
sub _write_fonts {

    my $self  = shift;
    my $count = $self->{_font_count};

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'fonts', @attributes );

    # Write the font elements for format objects that have them.
    for my $format ( @{ $self->{_xf_formats} } ) {
        $self->_write_font( $format ) if $format->{_has_font};
    }

    $self->xml_end_tag( 'fonts' );
}


##############################################################################
#
# _write_font()
#
# Write the <font> element.
#
sub _write_font {

    my $self       = shift;
    my $format     = shift;
    my $dxf_format = shift;

    $self->xml_start_tag( 'font' );

    # The condense and extend elements are mainly used in dxf formats.
    $self->_write_condense() if $format->{_font_condense};
    $self->_write_extend()   if $format->{_font_extend};

    $self->xml_empty_tag( 'b' )       if $format->{_bold};
    $self->xml_empty_tag( 'i' )       if $format->{_italic};
    $self->xml_empty_tag( 'strike' )  if $format->{_font_strikeout};
    $self->xml_empty_tag( 'outline' ) if $format->{_font_outline};
    $self->xml_empty_tag( 'shadow' )  if $format->{_font_shadow};

    # Handle the underline variants.
    $self->_write_underline( $format->{_underline} ) if $format->{_underline};

    $self->_write_vert_align( 'superscript' ) if $format->{_font_script} == 1;
    $self->_write_vert_align( 'subscript' )   if $format->{_font_script} == 2;

    if ( !$dxf_format ) {
        $self->xml_empty_tag( 'sz', 'val', $format->{_size} );
    }

    my $theme = $format->{_theme};


    if ( $theme == -1 ) {
        # Ignore for excel2003_style.
    }
    elsif ( $theme ) {
        $self->_write_color( 'theme' => $theme );
    }
    elsif ( my $index = $format->{_color_indexed} ) {
        $self->_write_color( 'indexed' => $index );
    }
    elsif ( my $color = $format->{_color} ) {
        $color = $self->_get_palette_color( $color );

        $self->_write_color( 'rgb' => $color );
    }
    elsif ( !$dxf_format ) {
        $self->_write_color( 'theme' => 1 );
    }

    if ( !$dxf_format ) {
        $self->xml_empty_tag( 'name',   'val', $format->{_font} );

        if ($format->{_font_family}) {
            $self->xml_empty_tag( 'family', 'val', $format->{_font_family} );
        }

        if ( $format->{_font} eq 'Calibri' && !$format->{_hyperlink} ) {
            $self->xml_empty_tag(

                'scheme',
                'val' => $format->{_font_scheme}
            );
        }
    }

    $self->xml_end_tag( 'font' );
}


###############################################################################
#
# _write_underline()
#
# Write the underline font element.
#
sub _write_underline {

    my $self      = shift;
    my $underline = shift;
    my @attributes;

    # Handle the underline variants.
    if ( $underline == 2 ) {
        @attributes = ( val => 'double' );
    }
    elsif ( $underline == 33 ) {
        @attributes = ( val => 'singleAccounting' );
    }
    elsif ( $underline == 34 ) {
        @attributes = ( val => 'doubleAccounting' );
    }
    else {
        @attributes = ();    # Default to single underline.
    }

    $self->xml_empty_tag( 'u', @attributes );

}


##############################################################################
#
# _write_vert_align()
#
# Write the <vertAlign> font sub-element.
#
sub _write_vert_align {

    my $self = shift;
    my $val  = shift;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'vertAlign', @attributes );
}


##############################################################################
#
# _write_color()
#
# Write the <color> element.
#
sub _write_color {

    my $self  = shift;
    my $name  = shift;
    my $value = shift;

    my @attributes = ( $name => $value );

    $self->xml_empty_tag( 'color', @attributes );
}


##############################################################################
#
# _write_fills()
#
# Write the <fills> element.
#
sub _write_fills {

    my $self  = shift;
    my $count = $self->{_fill_count};

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'fills', @attributes );

    # Write the default fill element.
    $self->_write_default_fill( 'none' );
    $self->_write_default_fill( 'gray125' );

    # Write the fill elements for format objects that have them.
    for my $format ( @{ $self->{_xf_formats} } ) {
        $self->_write_fill( $format ) if $format->{_has_fill};
    }

    $self->xml_end_tag( 'fills' );
}


##############################################################################
#
# _write_default_fill()
#
# Write the <fill> element for the default fills.
#
sub _write_default_fill {

    my $self         = shift;
    my $pattern_type = shift;

    $self->xml_start_tag( 'fill' );

    $self->xml_empty_tag( 'patternFill', 'patternType', $pattern_type );

    $self->xml_end_tag( 'fill' );
}


##############################################################################
#
# _write_fill()
#
# Write the <fill> element.
#
sub _write_fill {

    my $self       = shift;
    my $format     = shift;
    my $dxf_format = shift;
    my $pattern    = $format->{_pattern};
    my $bg_color   = $format->{_bg_color};
    my $fg_color   = $format->{_fg_color};

    # Colors for dxf formats are handled differently from normal formats since
    # the normal format reverses the meaning of BG and FG for solid fills.
    if ( $dxf_format ) {
        $bg_color = $format->{_dxf_bg_color};
        $fg_color = $format->{_dxf_fg_color};
    }


    my @patterns = qw(
      none
      solid
      mediumGray
      darkGray
      lightGray
      darkHorizontal
      darkVertical
      darkDown
      darkUp
      darkGrid
      darkTrellis
      lightHorizontal
      lightVertical
      lightDown
      lightUp
      lightGrid
      lightTrellis
      gray125
      gray0625

    );


    $self->xml_start_tag( 'fill' );

    # The "none" pattern is handled differently for dxf formats.
    if ( $dxf_format && $format->{_pattern} <= 1 ) {
        $self->xml_start_tag( 'patternFill' );
    }
    else {
        $self->xml_start_tag(
            'patternFill',
            'patternType',
            $patterns[ $format->{_pattern} ]

        );
    }

    if ( $fg_color ) {
        $fg_color = $self->_get_palette_color( $fg_color );
        $self->xml_empty_tag( 'fgColor', 'rgb' => $fg_color );
    }

    if ( $bg_color ) {
        $bg_color = $self->_get_palette_color( $bg_color );
        $self->xml_empty_tag( 'bgColor', 'rgb' => $bg_color );
    }
    else {
        if ( !$dxf_format ) {
            $self->xml_empty_tag( 'bgColor', 'indexed' => 64 );
        }
    }

    $self->xml_end_tag( 'patternFill' );
    $self->xml_end_tag( 'fill' );
}


##############################################################################
#
# _write_borders()
#
# Write the <borders> element.
#
sub _write_borders {

    my $self  = shift;
    my $count = $self->{_border_count};

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'borders', @attributes );

    # Write the border elements for format objects that have them.
    for my $format ( @{ $self->{_xf_formats} } ) {
        $self->_write_border( $format ) if $format->{_has_border};
    }

    $self->xml_end_tag( 'borders' );
}


##############################################################################
#
# _write_border()
#
# Write the <border> element.
#
sub _write_border {

    my $self       = shift;
    my $format     = shift;
    my $dxf_format = shift;
    my @attributes = ();


    # Diagonal borders add attributes to the <border> element.
    if ( $format->{_diag_type} == 1 ) {
        push @attributes, ( diagonalUp => 1 );
    }
    elsif ( $format->{_diag_type} == 2 ) {
        push @attributes, ( diagonalDown => 1 );
    }
    elsif ( $format->{_diag_type} == 3 ) {
        push @attributes, ( diagonalUp   => 1 );
        push @attributes, ( diagonalDown => 1 );
    }

    # Ensure that a default diag border is set if the diag type is set.
    if ( $format->{_diag_type} && !$format->{_diag_border} ) {
        $format->{_diag_border} = 1;
    }

    # Write the start border tag.
    $self->xml_start_tag( 'border', @attributes );

    # Write the <border> sub elements.
    $self->_write_sub_border(
        'left',
        $format->{_left},
        $format->{_left_color}

    );

    $self->_write_sub_border(
        'right',
        $format->{_right},
        $format->{_right_color}

    );

    $self->_write_sub_border(
        'top',
        $format->{_top},
        $format->{_top_color}

    );

    $self->_write_sub_border(
        'bottom',
        $format->{_bottom},
        $format->{_bottom_color}

    );

    # Condition DXF formats don't allow diagonal borders
    if ( !$dxf_format ) {
        $self->_write_sub_border(
            'diagonal',
            $format->{_diag_border},
            $format->{_diag_color}

        );
    }

    if ( $dxf_format ) {
        $self->_write_sub_border( 'vertical' );
        $self->_write_sub_border( 'horizontal' );
    }

    $self->xml_end_tag( 'border' );
}


##############################################################################
#
# _write_sub_border()
#
# Write the <border> sub elements such as <right>, <top>, etc.
#
sub _write_sub_border {

    my $self  = shift;
    my $type  = shift;
    my $style = shift;
    my $color = shift;
    my @attributes;

    if ( !$style ) {
        $self->xml_empty_tag( $type );
        return;
    }

    my @border_styles = qw(
      none
      thin
      medium
      dashed
      dotted
      thick
      double
      hair
      mediumDashed
      dashDot
      mediumDashDot
      dashDotDot
      mediumDashDotDot
      slantDashDot

    );


    push @attributes, ( style => $border_styles[$style] );

    $self->xml_start_tag( $type, @attributes );

    if ( $color ) {
        $color = $self->_get_palette_color( $color );
        $self->xml_empty_tag( 'color', 'rgb' => $color );
    }
    else {
        $self->xml_empty_tag( 'color', 'auto' => 1 );
    }

    $self->xml_end_tag( $type );
}


##############################################################################
#
# _write_cell_style_xfs()
#
# Write the <cellStyleXfs> element.
#
sub _write_cell_style_xfs {

    my $self  = shift;
    my $count = 1;

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'cellStyleXfs', @attributes );

    # Write the style_xf element.
    $self->_write_style_xf();

    $self->xml_end_tag( 'cellStyleXfs' );
}


##############################################################################
#
# _write_cell_xfs()
#
# Write the <cellXfs> element.
#
sub _write_cell_xfs {

    my $self    = shift;
    my @formats = @{ $self->{_xf_formats} };

    # Workaround for when the last format is used for the comment font
    # and shouldn't be used for cellXfs.
    my $last_format = $formats[-1];

    if ( $last_format->{_font_only} ) {
        pop @formats;
    }

    my $count = scalar @formats;
    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'cellXfs', @attributes );

    # Write the xf elements.
    for my $format ( @formats ) {
        $self->_write_xf( $format );
    }

    $self->xml_end_tag( 'cellXfs' );
}


##############################################################################
#
# _write_style_xf()
#
# Write the style <xf> element.
#
sub _write_style_xf {

    my $self       = shift;
    my $num_fmt_id = 0;
    my $font_id    = 0;
    my $fill_id    = 0;
    my $border_id  = 0;

    my @attributes = (
        'numFmtId' => $num_fmt_id,
        'fontId'   => $font_id,
        'fillId'   => $fill_id,
        'borderId' => $border_id,
    );

    $self->xml_empty_tag( 'xf', @attributes );
}


##############################################################################
#
# _write_xf()
#
# Write the <xf> element.
#
sub _write_xf {

    my $self        = shift;
    my $format      = shift;
    my $num_fmt_id  = $format->{_num_format_index};
    my $font_id     = $format->{_font_index};
    my $fill_id     = $format->{_fill_index};
    my $border_id   = $format->{_border_index};
    my $xf_id       = 0;
    my $has_align   = 0;
    my $has_protect = 0;

    my @attributes = (
        'numFmtId' => $num_fmt_id,
        'fontId'   => $font_id,
        'fillId'   => $fill_id,
        'borderId' => $border_id,
        'xfId'     => $xf_id,
    );


    if ( $format->{_num_format_index} > 0 ) {
        push @attributes, ( 'applyNumberFormat' => 1 );
    }

    # Add applyFont attribute if XF format uses a font element.
    if ( $format->{_font_index} > 0 ) {
        push @attributes, ( 'applyFont' => 1 );
    }

    # Add applyFill attribute if XF format uses a fill element.
    if ( $format->{_fill_index} > 0 ) {
        push @attributes, ( 'applyFill' => 1 );
    }

    # Add applyBorder attribute if XF format uses a border element.
    if ( $format->{_border_index} > 0 ) {
        push @attributes, ( 'applyBorder' => 1 );
    }

    # Check if XF format has alignment properties set.
    my ( $apply_align, @align ) = $format->get_align_properties();

    # Check if an alignment sub-element should be written.
    $has_align = 1 if $apply_align && @align;

    # We can also have applyAlignment without a sub-element.
    if ( $apply_align ) {
        push @attributes, ( 'applyAlignment' => 1 );
    }

    # Check for cell protection properties.
    my @protection = $format->get_protection_properties();

    if ( @protection ) {
        push @attributes, ( 'applyProtection' => 1 );
        $has_protect = 1;
    }

    # Write XF with sub-elements if required.
    if ( $has_align || $has_protect ) {
        $self->xml_start_tag( 'xf', @attributes );
        $self->xml_empty_tag( 'alignment',  @align )      if $has_align;
        $self->xml_empty_tag( 'protection', @protection ) if $has_protect;
        $self->xml_end_tag( 'xf' );
    }
    else {
        $self->xml_empty_tag( 'xf', @attributes );
    }
}


##############################################################################
#
# _write_cell_styles()
#
# Write the <cellStyles> element.
#
sub _write_cell_styles {

    my $self  = shift;
    my $count = 1;

    my @attributes = ( 'count' => $count );

    $self->xml_start_tag( 'cellStyles', @attributes );

    # Write the cellStyle element.
    $self->_write_cell_style();

    $self->xml_end_tag( 'cellStyles' );
}


##############################################################################
#
# _write_cell_style()
#
# Write the <cellStyle> element.
#
sub _write_cell_style {

    my $self       = shift;
    my $name       = 'Normal';
    my $xf_id      = 0;
    my $builtin_id = 0;

    my @attributes = (
        'name'      => $name,
        'xfId'      => $xf_id,
        'builtinId' => $builtin_id,
    );

    $self->xml_empty_tag( 'cellStyle', @attributes );
}


##############################################################################
#
# _write_dxfs()
#
# Write the <dxfs> element.
#
sub _write_dxfs {

    my $self    = shift;
    my $formats = $self->{_dxf_formats};

    my $count = scalar @{$formats};

    my @attributes = ( 'count' => $count );

    if ( $count ) {
        $self->xml_start_tag( 'dxfs', @attributes );

        # Write the font elements for format objects that have them.
        for my $format ( @{ $self->{_dxf_formats} } ) {
            $self->xml_start_tag( 'dxf' );
            $self->_write_font( $format, 1 ) if $format->{_has_dxf_font};

            if ( $format->{_num_format_index} ) {
                $self->_write_num_fmt( $format->{_num_format_index},
                    $format->{_num_format} );
            }

            $self->_write_fill( $format, 1 ) if $format->{_has_dxf_fill};
            $self->_write_border( $format, 1 ) if $format->{_has_dxf_border};
            $self->xml_end_tag( 'dxf' );
        }

        $self->xml_end_tag( 'dxfs' );
    }
    else {
        $self->xml_empty_tag( 'dxfs', @attributes );
    }

}


##############################################################################
#
# _write_table_styles()
#
# Write the <tableStyles> element.
#
sub _write_table_styles {

    my $self                = shift;
    my $count               = 0;
    my $default_table_style = 'TableStyleMedium9';
    my $default_pivot_style = 'PivotStyleLight16';

    my @attributes = (
        'count'             => $count,
        'defaultTableStyle' => $default_table_style,
        'defaultPivotStyle' => $default_pivot_style,
    );

    $self->xml_empty_tag( 'tableStyles', @attributes );
}


##############################################################################
#
# _write_colors()
#
# Write the <colors> element.
#
sub _write_colors {

    my $self          = shift;
    my @custom_colors = @{ $self->{_custom_colors} };

    return unless @custom_colors;

    $self->xml_start_tag( 'colors' );
    $self->_write_mru_colors( @custom_colors );
    $self->xml_end_tag( 'colors' );
}


##############################################################################
#
# _write_mru_colors()
#
# Write the <mruColors> element for the most recently used colours.
#
sub _write_mru_colors {

    my $self          = shift;
    my @custom_colors = @_;

    # Limit the mruColors to the last 10.
    my $count = @custom_colors;
    if ( $count > 10 ) {
        splice @custom_colors, 0, ( $count - 10 );
    }

    $self->xml_start_tag( 'mruColors' );

    # Write the custom colors in reverse order.
    for my $color ( reverse @custom_colors ) {
        $self->_write_color( 'rgb' => $color );
    }

    $self->xml_end_tag( 'mruColors' );
}


##############################################################################
#
# _write_condense()
#
# Write the <condense> element.
#
sub _write_condense {

    my $self = shift;
    my $val  = 0;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'condense', @attributes );
}


##############################################################################
#
# _write_extend()
#
# Write the <extend> element.
#
sub _write_extend {

    my $self = shift;
    my $val  = 0;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'extend', @attributes );
}


1;


__END__

=pod

=head1 NAME

Styles - A class for writing the Excel XLSX styles file.

=head1 SYNOPSIS

See the documentation for L<Excel::Writer::XLSX>.

=head1 DESCRIPTION

This module is used in conjunction with L<Excel::Writer::XLSX>.

=head1 AUTHOR

John McNamara jmcnamara@cpan.org

=head1 COPYRIGHT

(c) MM-MMXIIII, John McNamara.

All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.

=head1 LICENSE

Either the Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html> or the GPL L<http://www.opensource.org/licenses/gpl-license.php>.

=head1 DISCLAIMER OF WARRANTY

See the documentation for L<Excel::Writer::XLSX>.

=cut