| 1 | package Spreadsheet::ParseODS::Styles; | ||
| 2 | use 5.010; # for // | ||
| 3 | use Moo 2; | ||
| 4 | use Carp qw(croak); | ||
| 5 | use Filter::signatures; | ||
| 6 | use feature 'signatures'; | ||
| 7 | no warnings 'experimental::signatures'; | ||
| 8 | use PerlX::Maybe; | ||
| 9 | |||
| 10 | our $VERSION = '0.39'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Spreadsheet::ParseODS::Styles - styles / formatting of cells in a workbook | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | has 'styles' => ( | ||
| 19 | is => 'lazy', | ||
| 20 | default => sub { {} }, | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub part_to_format( $self, $part ) { | ||
| 24 | my $t = $part->tag; | ||
| 25 | my $res; | ||
| 26 | if( $t eq 'number:seconds' ) { | ||
| 27 | my $style = $part->att('number:style'); | ||
| 28 | if( $style and $style eq 'long') { | ||
| 29 | $res = 'SS'; | ||
| 30 | } else { | ||
| 31 | $res = 'S'; | ||
| 32 | }; | ||
| 33 | #warn $part->toString; | ||
| 34 | } elsif( $t eq 'number:minutes' ) { | ||
| 35 | my $style = $part->att('number:style'); | ||
| 36 | if( $style and $style eq 'long') { | ||
| 37 | $res = 'MM'; | ||
| 38 | } else { | ||
| 39 | $res = 'M'; | ||
| 40 | }; | ||
| 41 | #warn $part->toString; | ||
| 42 | } elsif( $t eq 'number:hours' ) { | ||
| 43 | my $style = $part->att('number:style'); | ||
| 44 | if( $style and $style eq 'long') { | ||
| 45 | $res = 'HH'; | ||
| 46 | } else { | ||
| 47 | $res = 'H'; | ||
| 48 | }; | ||
| 49 | #warn $part->toString; | ||
| 50 | } elsif( $t eq 'number:am-pm' ) { | ||
| 51 | $res = 'am'; | ||
| 52 | #warn $part->toString; | ||
| 53 | } elsif( $t eq 'number:day' ) { | ||
| 54 | my $style = $part->att('number:style'); | ||
| 55 | if( $style and $style eq 'long') { | ||
| 56 | $res = 'dd'; | ||
| 57 | } else { | ||
| 58 | $res = 'd'; | ||
| 59 | }; | ||
| 60 | } elsif( $t eq 'number:day-of-week' ) { | ||
| 61 | $res = 'ddd'; | ||
| 62 | #warn $part->toString; | ||
| 63 | } elsif( $t eq 'number:week-of-year' ) { | ||
| 64 | $res = 'ww'; | ||
| 65 | #warn $part->toString; | ||
| 66 | } elsif( $t eq 'number:month' ) { | ||
| 67 | my $style = $part->att('number:style'); | ||
| 68 | my $month_name = $part->att('number:textual'); | ||
| 69 | if( $month_name and $month_name eq 'true') { | ||
| 70 | $res = 'mmm'; | ||
| 71 | } elsif( $style and $style eq 'long') { | ||
| 72 | $res = 'mm'; | ||
| 73 | } else { | ||
| 74 | $res = 'm'; | ||
| 75 | }; | ||
| 76 | #warn $part->toString; | ||
| 77 | } elsif( $t eq 'number:year' ) { | ||
| 78 | my $style = $part->att('number:style'); | ||
| 79 | if( $style and $style eq 'long') { | ||
| 80 | $res = 'yyyy'; | ||
| 81 | } else { | ||
| 82 | $res = 'yy'; | ||
| 83 | }; | ||
| 84 | #warn $part->toString; | ||
| 85 | } elsif( $t eq 'number:number' ) { | ||
| 86 | $res = '#' x ($part->att('number:min-integer-digits') || 1); | ||
| 87 | |||
| 88 | if( defined( my $dec = $part->att('number:decimal-places'))) { | ||
| 89 | $res .= '.' . ('0' x $dec); | ||
| 90 | }; | ||
| 91 | #warn $part->toString; | ||
| 92 | } elsif( $t eq 'number:scientific-number' ) { | ||
| 93 | $res = '#' x $part->att('number:min-integer-digits'); | ||
| 94 | |||
| 95 | if( defined( my $dec = $part->att('number:decimal-places'))) { | ||
| 96 | $res .= '.' . ('0' x $dec); | ||
| 97 | }; | ||
| 98 | $res .= 'E+'; | ||
| 99 | if( defined( my $dec = $part->att('number:exponent-digits'))) { | ||
| 100 | $res .= '.' . ('#' x $dec); | ||
| 101 | }; | ||
| 102 | |||
| 103 | #warn $part->toString; | ||
| 104 | } elsif( $t eq 'number:text' ) { | ||
| 105 | $res = $part->text; | ||
| 106 | } elsif( $t eq 'number:text-content' or $t eq 'number:currency-symbol' ) { | ||
| 107 | $res = $part->text; | ||
| 108 | } elsif( $t eq 'number:fraction' ) { | ||
| 109 | $res = '#' x $part->att('number:min-integer-digits'); | ||
| 110 | |||
| 111 | if( defined( my $num = $part->att('number:min-numerator-digits'))) { | ||
| 112 | $res .= ' ' . ('#' x $num); | ||
| 113 | |||
| 114 | my $den = $part->att('number:min-denominator-digits'); | ||
| 115 | $res .= '/' . ('#' x $den); | ||
| 116 | }; | ||
| 117 | } elsif( $t eq 'loext:text' ) { | ||
| 118 | $res = $part->text; | ||
| 119 | } elsif( $t eq 'style:text-properties' or $t eq 'style:properties' ) { | ||
| 120 | # ignored | ||
| 121 | } elsif( $t eq 'loext:fill-character' ) { | ||
| 122 | # ignored | ||
| 123 | } elsif( $t eq 'number:fill-character' ) { | ||
| 124 | # ignored | ||
| 125 | } elsif( $t eq 'style:map' ) { | ||
| 126 | # ignored | ||
| 127 | } elsif( $t eq '#PCDATA' ) { | ||
| 128 | # ignored | ||
| 129 | } else { | ||
| 130 | warn "Unknown tag name '$t'"; | ||
| 131 | warn $part->toString; | ||
| 132 | }; | ||
| 133 | return $res | ||
| 134 | } | ||
| 135 | |||
| 136 | sub to_format( $self, $style ) { | ||
| 137 | return join "", map { my $res = $self->part_to_format( $_ ) // '' } $style->children | ||
| 138 | } | ||
| 139 | |||
| 140 | sub read_from_twig( $self, $elt ) { | ||
| 141 | my $styles = $self->styles; | ||
| 142 | |||
| 143 | for my $style ($elt->findnodes(join " | ", | ||
| 144 | '//style:default-style', | ||
| 145 | '//number:date-style', | ||
| 146 | '//number:number-style', | ||
| 147 | '//number:text-style', | ||
| 148 | '//number:time-style', | ||
| 149 | '//number:currency-style', | ||
| 150 | )) { | ||
| 151 | my $name = $style->att('style:data-style-name') | ||
| 152 | || $style->att('style:name'); | ||
| 153 | |||
| 154 | # Currently we simply ignore the default style... | ||
| 155 | next unless defined $name; | ||
| 156 | |||
| 157 | # ignore language and country | ||
| 158 | # This is not ideal, but oh well | ||
| 159 | my $format = $self->to_format( $style ); | ||
| 160 | my ($font) = map { $_->att('style:font-name') } $style->findnodes('style:text-properties'); | ||
| 161 | |||
| 162 | #warn "Defined '$name' as '$format'"; | ||
| 163 | #warn $style->toString unless $format; | ||
| 164 | $styles->{ $name } = { | ||
| 165 | format => $format, | ||
| 166 | font_face => $font, | ||
| 167 | }; | ||
| 168 | }; | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | 1; |