| 1 | package IO::SWF::Type::FILLSTYLE; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Type'; | ||
| 7 | |||
| 8 | use IO::SWF::Bit; | ||
| 9 | use IO::SWF::Type::MATRIX; | ||
| 10 | use IO::SWF::Type::RGB; | ||
| 11 | use IO::SWF::Type::RGBA; | ||
| 12 | |||
| 13 | sub parse { | ||
| 14 | my ($reader, $opts_href) = @_; | ||
| 15 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 16 | my $tagCode = $opts{'tagCode'}; | ||
| 17 | my $isMorph = ($tagCode == 46) || ($tagCode == 84) ? 1 : 0; | ||
| 18 | |||
| 19 | my %fillStyle; | ||
| 20 | my $fillStyleType = $reader->getUI8(); | ||
| 21 | $fillStyle{'FillStyleType'} = $fillStyleType; | ||
| 22 | if ($fillStyleType == 0x00) { | ||
| 23 | # 0x00: // solid fill | ||
| 24 | if (!$isMorph) { | ||
| 25 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 26 | $fillStyle{'Color'} = IO::SWF::Type::RGB::parse($reader); | ||
| 27 | } else { | ||
| 28 | $fillStyle{'Color'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 29 | } | ||
| 30 | } else { | ||
| 31 | $fillStyle{'StartColor'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 32 | $fillStyle{'EndColor'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | elsif ($fillStyleType == 0x10 || $fillStyleType == 0x12) { | ||
| 36 | # 0x10: // linear gradient fill | ||
| 37 | # 0x12: // radial gradient fill | ||
| 38 | if (!$isMorph) { | ||
| 39 | $fillStyle{'GradientMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 40 | } else { | ||
| 41 | $fillStyle{'StartGradientMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 42 | $fillStyle{'EndGradientMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 43 | } | ||
| 44 | $reader->byteAlign(); | ||
| 45 | my $numGradients; | ||
| 46 | if (!$isMorph) { | ||
| 47 | $fillStyle{'SpreadMode'} = $reader->getUIBits(2); | ||
| 48 | $fillStyle{'InterpolationMode'} = $reader->getUIBits(2); | ||
| 49 | $numGradients = $reader->getUIBits(4); | ||
| 50 | } else { | ||
| 51 | $numGradients = $reader->getUI8(); | ||
| 52 | } | ||
| 53 | my @GradientRecords = (); | ||
| 54 | for (my $j = 0 ; $j < $numGradients ; $j++) { | ||
| 55 | my %gradientRecord ; | ||
| 56 | if (!$isMorph) { | ||
| 57 | $gradientRecord{'Ratio'} = $reader->getUI8(); | ||
| 58 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 59 | $gradientRecord{'Color'} = IO::SWF::Type::RGB::parse($reader); | ||
| 60 | } else { | ||
| 61 | $gradientRecord{'Color'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 62 | } | ||
| 63 | } else { # Morph | ||
| 64 | $gradientRecord{'StartRatio'} = $reader->getUI8(); | ||
| 65 | $gradientRecord{'EndRatio'} = $reader->getUI8(); | ||
| 66 | $gradientRecord{'StartColor'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 67 | $gradientRecord{'EndColor'} = IO::SWF::Type::RGBA::parse($reader); | ||
| 68 | } | ||
| 69 | push @GradientRecords, \%gradientRecord; | ||
| 70 | } | ||
| 71 | $fillStyle{'GradientRecords'} = \@GradientRecords; | ||
| 72 | } | ||
| 73 | # case 0x13: // focal gradient fill // 8 and later | ||
| 74 | elsif ($fillStyleType == 0x40 || $fillStyleType == 0x41 || $fillStyleType == 0x42 || $fillStyleType == 0x43) { | ||
| 75 | # 0x40: // repeating bitmap fill | ||
| 76 | # 0x41: // clipped bitmap fill | ||
| 77 | # 0x42: // non-smoothed repeating bitmap fill | ||
| 78 | # 0x43: // non-smoothed clipped bitmap fill | ||
| 79 | $fillStyle{'BitmapId'} = $reader->getUI16LE(); | ||
| 80 | if (!$isMorph) { | ||
| 81 | $fillStyle{'BitmapMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 82 | } else { | ||
| 83 | $fillStyle{'StartBitmapMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 84 | $fillStyle{'EndBitmapMatrix'} = IO::SWF::Type::MATRIX::parse($reader); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | die "Unknown FillStyleType=$fillStyleType tagCode=$tagCode"; | ||
| 89 | } | ||
| 90 | return \%fillStyle; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub build { | ||
| 94 | my ($writer, $fillStyle_href, $opts_href) = @_; | ||
| 95 | my %fillStyle = ref($fillStyle_href) ? %{$fillStyle_href} : (); | ||
| 96 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 97 | |||
| 98 | my $tagCode = $opts{'tagCode'}; | ||
| 99 | my $isMorph = ($tagCode == 46) || ($tagCode == 84) ? 1 : 0; | ||
| 100 | |||
| 101 | my $fillStyleType = $fillStyle{'FillStyleType'}; | ||
| 102 | $writer->putUI8($fillStyleType); | ||
| 103 | if ($fillStyleType == 0x00) { | ||
| 104 | # 0x00: // solid fill | ||
| 105 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 106 | IO::SWF::Type::RGB::build($writer, $fillStyle{'Color'}); | ||
| 107 | } else { | ||
| 108 | IO::SWF::Type::RGBA::build($writer, $fillStyle{'Color'}); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | elsif ($fillStyleType == 0x10 || $fillStyleType == 0x12) { | ||
| 112 | # 0x10: // linear gradient fill | ||
| 113 | # 0x12: // radial gradient fill | ||
| 114 | IO::SWF::Type::MATRIX::build($writer, $fillStyle{'GradientMatrix'}); | ||
| 115 | $writer->byteAlign(); | ||
| 116 | $writer->putUIBits($fillStyle{'SpreadMode'}, 2); | ||
| 117 | $writer->putUIBits($fillStyle{'InterpolationMode'}, 2); | ||
| 118 | my $numGradients = scalar(@{$fillStyle{'GradientRecords'}}); | ||
| 119 | $writer->putUIBits($numGradients , 4); | ||
| 120 | foreach my $gradientRecord (@{$fillStyle{'GradientRecords'}}) { | ||
| 121 | $writer->putUI8($gradientRecord->{'Ratio'}); | ||
| 122 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 123 | IO::SWF::Type::RGB::build($writer, $gradientRecord->{'Color'}); | ||
| 124 | } else { | ||
| 125 | IO::SWF::Type::RGBA::build($writer, $gradientRecord->{'Color'}); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | # case 0x13: // focal gradient fill // 8 and later | ||
| 130 | elsif ($fillStyleType == 0x40 || $fillStyleType == 0x41 || $fillStyleType == 0x42 || $fillStyleType == 0x43) { | ||
| 131 | # 0x40: // repeating bitmap fill | ||
| 132 | # 0x41: // clipped bitmap fill | ||
| 133 | # 0x42: // non-smoothed repeating bitmap fill | ||
| 134 | # 0x43: // non-smoothed clipped bitmap fill | ||
| 135 | $writer->putUI16LE($fillStyle{'BitmapId'}); | ||
| 136 | IO::SWF::Type::MATRIX::build($writer, $fillStyle{'BitmapMatrix'}); | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | # default: | ||
| 140 | # throw new IO_SWF_Exception("Unknown FillStyleType=$fillStyleType tagCode=$tagCode"); | ||
| 141 | } | ||
| 142 | return 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub string { | ||
| 146 | my ($fillStyle_href, $opts_href) = @_; | ||
| 147 | my %fillStyle = ref($fillStyle_href) ? %{$fillStyle_href} : (); | ||
| 148 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 149 | |||
| 150 | my $tagCode = $opts{'tagCode'}; | ||
| 151 | my $isMorph = ($tagCode == 46) || ($tagCode == 84) ? 1 : 0; | ||
| 152 | |||
| 153 | my $text = ''; | ||
| 154 | my $fillStyleType = $fillStyle{'FillStyleType'}; | ||
| 155 | if ($fillStyleType == 0x00) { | ||
| 156 | # 0x00: // solid fill | ||
| 157 | my $color = $fillStyle{'Color'}; | ||
| 158 | my $color_str; | ||
| 159 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 160 | $color_str = IO::SWF::Type::RGB::string($color); | ||
| 161 | } else { | ||
| 162 | $color_str = IO::SWF::Type::RGBA::string($color); | ||
| 163 | } | ||
| 164 | $text .= "\tsolid fill: $color_str\n"; | ||
| 165 | } | ||
| 166 | elsif ($fillStyleType == 0x10 || $fillStyleType == 0x12) { | ||
| 167 | # 0x10: // linear gradient fill | ||
| 168 | # 0x12: // radial gradient fill | ||
| 169 | if ($fillStyleType == 0x10) { | ||
| 170 | $text .= "\tlinear gradient fill\n"; | ||
| 171 | } else { | ||
| 172 | $text .= "\tradial gradient fill\n"; | ||
| 173 | } | ||
| 174 | my %opts = ('indent' => 2); | ||
| 175 | if (!$isMorph) { | ||
| 176 | my $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'GradientMatrix'}, \%opts); | ||
| 177 | $text .= $matrix_str . "\n"; | ||
| 178 | my $spreadMode = $fillStyle{'SpreadMode'}; | ||
| 179 | my $interpolationMode = $fillStyle{'InterpolationMode'}; | ||
| 180 | } else { | ||
| 181 | # my $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'StartGradientMatrix'}, $opts); | ||
| 182 | my $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'EndGradientMatrix'}, \%opts); | ||
| 183 | $text .= $matrix_str . "\n"; | ||
| 184 | } | ||
| 185 | |||
| 186 | foreach my $gradientRecord (@{$fillStyle{'GradientRecords'}}) { | ||
| 187 | if (!$isMorph) { | ||
| 188 | my $ratio = $gradientRecord->{'Ratio'}; | ||
| 189 | my $color = $gradientRecord->{'Color'}; | ||
| 190 | my $color_str; | ||
| 191 | if ($tagCode < 32 ) { # 32:DefineShape3 | ||
| 192 | $color_str = IO::SWF::Type::RGB::string($color); | ||
| 193 | } else { | ||
| 194 | $color_str = IO::SWF::Type::RGBA::string($color); | ||
| 195 | } | ||
| 196 | $text .= "\t\tRatio: $ratio Color:$color_str\n"; | ||
| 197 | } else { | ||
| 198 | my $startRatio = $gradientRecord->{'StartRatio'}; | ||
| 199 | my $endRatio = $gradientRecord->{'EndRatio'}; | ||
| 200 | my $startColorStr = IO::SWF::Type::RGBA::string($gradientRecord->{'StartColor'}); | ||
| 201 | my $endColorStr = IO::SWF::Type::RGBA::string($gradientRecord->{'EndColor'}); | ||
| 202 | $text .= "\t\tRatio: $startRatio => $endRatio Color:$startColorStr => $endColorStr\n"; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | elsif ($fillStyleType == 0x40 || $fillStyleType == 0x41 || $fillStyleType == 0x42 || $fillStyleType == 0x43) { | ||
| 207 | # 0x40: // repeating bitmap fill | ||
| 208 | # 0x41: // clipped bitmap fill | ||
| 209 | # 0x42: // non-smoothed repeating bitmap fill | ||
| 210 | # 0x43: // non-smoothed clipped bitmap fill | ||
| 211 | $text .= "\tBigmap($fillStyleType): "; | ||
| 212 | $text .= " BitmapId: ".$fillStyle{'BitmapId'}."\n"; | ||
| 213 | my $matrix_str; | ||
| 214 | if (!$isMorph) { | ||
| 215 | $text .= "\tBitmapMatrix:\n"; | ||
| 216 | my %opts = ('indent' => 2); | ||
| 217 | $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'BitmapMatrix'}, \%opts); | ||
| 218 | $text .= $matrix_str . "\n"; | ||
| 219 | } else { | ||
| 220 | my %opts = ('indent' => 2); | ||
| 221 | $text .= "\tStartBitmapMatrix:\n"; | ||
| 222 | $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'StartBitmapMatrix'}, \%opts); | ||
| 223 | $text .= $matrix_str . "\n"; | ||
| 224 | $text .= "\tEndBitmapMatrix:\n"; | ||
| 225 | $matrix_str = IO::SWF::Type::MATRIX::string($fillStyle{'EndBitmapMatrix'}, \%opts); | ||
| 226 | $text .= $matrix_str . "\n"; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | # default: | ||
| 231 | $text .= "Unknown FillStyleType($fillStyleType)\n"; | ||
| 232 | } | ||
| 233 | return $text; | ||
| 234 | } | ||
| 235 | |||
| 236 | 1; |