| 1 | package IO::SWF::Type::SHAPE; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Type'; | ||
| 7 | |||
| 8 | use List::Util; | ||
| 9 | use IO::SWF::Bit; | ||
| 10 | use IO::SWF::Type::FILLSTYLEARRAY; | ||
| 11 | use IO::SWF::Type::LINESTYLEARRAY; | ||
| 12 | |||
| 13 | sub parse { | ||
| 14 | my ($reader, $opts_href) = @_; | ||
| 15 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 16 | my $tagCode = $opts{'tagCode'}; | ||
| 17 | my @shapeRecords = (); | ||
| 18 | |||
| 19 | $reader->byteAlign(); | ||
| 20 | my $numFillBits = $reader->getUIBits(4); | ||
| 21 | my $numLineBits = $reader->getUIBits(4); | ||
| 22 | |||
| 23 | my $currentDrawingPositionX = 0; | ||
| 24 | my $currentDrawingPositionY = 0; | ||
| 25 | my $currentFillStyle0 = 0; | ||
| 26 | my $currentFillStyle1 = 0; | ||
| 27 | my $currentLineStyle = 0; | ||
| 28 | my $done = 0; | ||
| 29 | |||
| 30 | # ShapeRecords | ||
| 31 | while (!$done) { | ||
| 32 | my %shapeRecord = (); | ||
| 33 | my $typeFlag = $reader->getUIBit(); | ||
| 34 | $shapeRecord{'TypeFlag'} = $typeFlag; | ||
| 35 | if ($typeFlag == 0) { | ||
| 36 | my $endOfShape = $reader->getUIBits(5); | ||
| 37 | if ($endOfShape == 0) { | ||
| 38 | # EndShapeRecord | ||
| 39 | $shapeRecord{'EndOfShape'} = $endOfShape; | ||
| 40 | $done = 1; | ||
| 41 | } else { | ||
| 42 | # StyleChangeRecord | ||
| 43 | $reader->incrementOffset(0, -5); | ||
| 44 | my $stateNewStyles = $reader->getUIBit(); | ||
| 45 | my $stateLineStyle = $reader->getUIBit(); | ||
| 46 | my $stateFillStyle1 = $reader->getUIBit(); | ||
| 47 | my $stateFillStyle0 = $reader->getUIBit(); | ||
| 48 | |||
| 49 | my $stateMoveTo = $reader->getUIBit(); | ||
| 50 | if ($stateMoveTo) { | ||
| 51 | my $moveBits = $reader->getUIBits(5); | ||
| 52 | # $shapeRecord{'(MoveBits)'} = $moveBits; | ||
| 53 | my $moveDeltaX = $reader->getSIBits($moveBits); | ||
| 54 | my $moveDeltaY = $reader->getSIBits($moveBits); | ||
| 55 | # $currentDrawingPositionX += $moveDeltaX; | ||
| 56 | # $currentDrawingPositionY += $moveDeltaY; | ||
| 57 | $currentDrawingPositionX = $moveDeltaX; | ||
| 58 | $currentDrawingPositionY = $moveDeltaY; | ||
| 59 | $shapeRecord{'MoveX'} = $currentDrawingPositionX; | ||
| 60 | $shapeRecord{'MoveY'} = $currentDrawingPositionY; | ||
| 61 | } | ||
| 62 | $shapeRecord{'MoveX'} = $currentDrawingPositionX; | ||
| 63 | $shapeRecord{'MoveY'} = $currentDrawingPositionY; | ||
| 64 | |||
| 65 | if ($stateFillStyle0) { | ||
| 66 | $currentFillStyle0 = $reader->getUIBits($numFillBits); | ||
| 67 | } | ||
| 68 | if ($stateFillStyle1) { | ||
| 69 | $currentFillStyle1 = $reader->getUIBits($numFillBits); | ||
| 70 | } | ||
| 71 | if ($stateLineStyle) { | ||
| 72 | $currentLineStyle = $reader->getUIBits($numLineBits); | ||
| 73 | } | ||
| 74 | $shapeRecord{'FillStyle0'} = $currentFillStyle0; | ||
| 75 | $shapeRecord{'FillStyle1'} = $currentFillStyle1; | ||
| 76 | $shapeRecord{'LineStyle'} = $currentLineStyle; | ||
| 77 | if ($stateNewStyles) { | ||
| 78 | my %opts = ('tagCode' => $tagCode); | ||
| 79 | $shapeRecord{'FillStyles'} = IO::SWF::Type::FILLSTYLEARRAY::parse($reader, \%opts); | ||
| 80 | $shapeRecord{'LineStyles'} = IO::SWF::Type::LINESTYLEARRAY::parse($reader, \%opts); | ||
| 81 | $reader->byteAlign(); | ||
| 82 | $numFillBits = $reader->getUIBits(4); | ||
| 83 | $numLineBits = $reader->getUIBits(4); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } else { # Edge records | ||
| 87 | $shapeRecord{'StraightFlag'} = $reader->getUIBit(); | ||
| 88 | if ($shapeRecord{'StraightFlag'}) { | ||
| 89 | # StraightEdgeRecord | ||
| 90 | my $numBits = $reader->getUIBits(4); | ||
| 91 | # $shapeRecord{'(NumBits)'} = $numBits; | ||
| 92 | my $generalLineFlag = $reader->getUIBit(); | ||
| 93 | my $vertLineFlag = 0; | ||
| 94 | if ($generalLineFlag == 0) { | ||
| 95 | $vertLineFlag = $reader->getUIBit(); | ||
| 96 | } | ||
| 97 | if ($generalLineFlag || ($vertLineFlag == 0)) { | ||
| 98 | my $deltaX = $reader->getSIBits($numBits + 2); | ||
| 99 | $currentDrawingPositionX += $deltaX; | ||
| 100 | } | ||
| 101 | if ($generalLineFlag || $vertLineFlag) { | ||
| 102 | my $deltaY = $reader->getSIBits($numBits + 2); | ||
| 103 | $currentDrawingPositionY += $deltaY; | ||
| 104 | } | ||
| 105 | $shapeRecord{'X'} = $currentDrawingPositionX; | ||
| 106 | $shapeRecord{'Y'} = $currentDrawingPositionY; | ||
| 107 | } else { | ||
| 108 | # CurvedEdgeRecord | ||
| 109 | my $numBits = $reader->getUIBits(4); | ||
| 110 | # $shapeRecord{'(NumBits)'} = $numBits; | ||
| 111 | |||
| 112 | my $controlDeltaX = $reader->getSIBits($numBits + 2); | ||
| 113 | my $controlDeltaY = $reader->getSIBits($numBits + 2); | ||
| 114 | my $anchorDeltaX = $reader->getSIBits($numBits + 2); | ||
| 115 | my $anchorDeltaY = $reader->getSIBits($numBits + 2); | ||
| 116 | |||
| 117 | $currentDrawingPositionX += $controlDeltaX; | ||
| 118 | $currentDrawingPositionY += $controlDeltaY; | ||
| 119 | $shapeRecord{'ControlX'} = $currentDrawingPositionX; | ||
| 120 | $shapeRecord{'ControlY'} = $currentDrawingPositionY; | ||
| 121 | |||
| 122 | $currentDrawingPositionX += $anchorDeltaX; | ||
| 123 | $currentDrawingPositionY += $anchorDeltaY; | ||
| 124 | $shapeRecord{'AnchorX'} = $currentDrawingPositionX; | ||
| 125 | $shapeRecord{'AnchorY'} = $currentDrawingPositionY; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | push @shapeRecords, \%shapeRecord; | ||
| 129 | } | ||
| 130 | return \@shapeRecords; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub build { | ||
| 134 | my ($writer, $shapeRecords_aref, $opts_href) = @_; | ||
| 135 | my @shapeRecords = ref($shapeRecords_aref) ? @{$shapeRecords_aref} : (); | ||
| 136 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 137 | |||
| 138 | my $tagCode = $opts{'tagCode'}; | ||
| 139 | my $fillStyleCount = $opts{'fillStyleCount'}; | ||
| 140 | my $lineStyleCount = $opts{'lineStyleCount'}; | ||
| 141 | my ($numFillBits, $numLineBits); | ||
| 142 | if ($fillStyleCount == 0) { | ||
| 143 | $numFillBits = 0; | ||
| 144 | } else { | ||
| 145 | # $fillStyleCount == fillStyle MaxValue because 'undefined' use 0 | ||
| 146 | $numFillBits = $writer->need_bits_unsigned($fillStyleCount); | ||
| 147 | } | ||
| 148 | if ($lineStyleCount == 0) { | ||
| 149 | $numLineBits = 0; | ||
| 150 | } else { | ||
| 151 | # $lineStyleCount == lineStyle MaxValue because 'undefined' use 0 | ||
| 152 | $numLineBits = $writer->need_bits_unsigned($lineStyleCount); | ||
| 153 | } | ||
| 154 | |||
| 155 | $writer->byteAlign(); | ||
| 156 | $writer->putUIBits($numFillBits, 4); | ||
| 157 | $writer->putUIBits($numLineBits, 4); | ||
| 158 | my $currentDrawingPositionX = 0; | ||
| 159 | my $currentDrawingPositionY = 0; | ||
| 160 | my $currentFillStyle0 = 0; | ||
| 161 | my $currentFillStyle1 = 0; | ||
| 162 | my $currentLineStyle = 0; | ||
| 163 | my $counter = 0; | ||
| 164 | foreach my $shapeRecord (@shapeRecords) { | ||
| 165 | $counter++; | ||
| 166 | my $typeFlag = $shapeRecord->{'TypeFlag'}; | ||
| 167 | $writer->putUIBit($typeFlag); | ||
| 168 | if($typeFlag == 0) { | ||
| 169 | if (defined ($shapeRecord->{'EndOfShape'}) && ($shapeRecord->{'EndOfShape'}) == 0) { | ||
| 170 | # EndShapeRecord | ||
| 171 | $writer->putUIBits(0, 5); | ||
| 172 | } else { | ||
| 173 | # StyleChangeRecord | ||
| 174 | my $stateNewStyles = defined $shapeRecord->{'FillStyles'} ? 1 : 0; | ||
| 175 | my $stateLineStyle = ($shapeRecord->{'LineStyle'} != $currentLineStyle) ? 1 : 0; | ||
| 176 | my $stateFillStyle1 = ($shapeRecord->{'FillStyle1'} != $currentFillStyle1) ? 1 : 0; | ||
| 177 | my $stateFillStyle0 = ($shapeRecord->{'FillStyle0'} != $currentFillStyle0) ? 1 : 0; | ||
| 178 | |||
| 179 | $writer->putUIBit($stateNewStyles); | ||
| 180 | $writer->putUIBit($stateLineStyle); | ||
| 181 | $writer->putUIBit($stateFillStyle1); | ||
| 182 | $writer->putUIBit($stateFillStyle0); | ||
| 183 | |||
| 184 | my $stateMoveTo; | ||
| 185 | if (($shapeRecord->{'MoveX'} != $currentDrawingPositionX) || ($shapeRecord->{'MoveY'} != $currentDrawingPositionY)) { | ||
| 186 | $stateMoveTo = 1; | ||
| 187 | } else { | ||
| 188 | $stateMoveTo = 0; | ||
| 189 | } | ||
| 190 | $writer->putUIBit($stateMoveTo); | ||
| 191 | if ($stateMoveTo) { | ||
| 192 | my $moveX = $shapeRecord->{'MoveX'}; | ||
| 193 | my $moveY = $shapeRecord->{'MoveY'}; | ||
| 194 | $currentDrawingPositionX = $moveX; | ||
| 195 | $currentDrawingPositionY = $moveY; | ||
| 196 | my $moveBits; | ||
| 197 | if ($moveX | $moveY) { | ||
| 198 | my $XmoveBits = $writer->need_bits_signed($moveX); | ||
| 199 | my $YmoveBits = $writer->need_bits_signed($moveY); | ||
| 200 | $moveBits = List::Util::max($XmoveBits, $YmoveBits); | ||
| 201 | } else { | ||
| 202 | $moveBits = 0; | ||
| 203 | } | ||
| 204 | $writer->putUIBits($moveBits, 5); | ||
| 205 | $writer->putSIBits($moveX, $moveBits); | ||
| 206 | $writer->putSIBits($moveY, $moveBits); | ||
| 207 | } | ||
| 208 | if ($stateFillStyle0) { | ||
| 209 | $currentFillStyle0 = $shapeRecord->{'FillStyle0'}; | ||
| 210 | $writer->putUIBits($currentFillStyle0, $numFillBits); | ||
| 211 | } | ||
| 212 | if ($stateFillStyle1) { | ||
| 213 | $currentFillStyle1 = $shapeRecord->{'FillStyle1'}; | ||
| 214 | $writer->putUIBits($currentFillStyle1, $numFillBits); | ||
| 215 | } | ||
| 216 | if ($stateLineStyle) { | ||
| 217 | $currentLineStyle = $shapeRecord->{'LineStyle'}; | ||
| 218 | $writer->putUIBits($currentLineStyle, $numLineBits); | ||
| 219 | } | ||
| 220 | if ($stateNewStyles) { | ||
| 221 | my %opts = ('tagCode' => $tagCode); | ||
| 222 | IO::SWF::Type::FILLSTYLEARRAY::build($writer, $shapeRecord->{'FillStyles'}, \%opts); | ||
| 223 | IO::SWF::Type::LINESTYLEARRAY::build($writer, $shapeRecord->{'LineStyles'}, \%opts); | ||
| 224 | $fillStyleCount = scalar(@{$shapeRecord->{'FillStyles'}}); | ||
| 225 | if ($fillStyleCount == 0) { | ||
| 226 | $numFillBits = 0; | ||
| 227 | } else { | ||
| 228 | # $fillStyleCount == fillStyle MaxValue because 'undefined' use 0 | ||
| 229 | $numFillBits = $writer->need_bits_unsigned($fillStyleCount); | ||
| 230 | } | ||
| 231 | if ($lineStyleCount == 0) { | ||
| 232 | $numLineBits = 0; | ||
| 233 | } else { | ||
| 234 | # $lineStyleCount == lineStyle MaxValue because 'undefined' use 0 | ||
| 235 | $numLineBits = $writer->need_bits_unsigned($lineStyleCount); | ||
| 236 | } | ||
| 237 | $writer->byteAlign(); | ||
| 238 | $writer->putUIBits($numFillBits, 4); | ||
| 239 | $writer->putUIBits($numLineBits, 4); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } else { | ||
| 243 | my $straightFlag = $shapeRecord->{'StraightFlag'}; | ||
| 244 | $writer->putUIBit($straightFlag); | ||
| 245 | if ($straightFlag) { | ||
| 246 | my $deltaX = $shapeRecord->{'X'} - $currentDrawingPositionX; | ||
| 247 | my $deltaY = $shapeRecord->{'Y'} - $currentDrawingPositionY; | ||
| 248 | my $numBits; | ||
| 249 | if ($deltaX | $deltaY) { | ||
| 250 | my $XNumBits = $writer->need_bits_signed($deltaX); | ||
| 251 | my $YNumBits = $writer->need_bits_signed($deltaY); | ||
| 252 | $numBits = List::Util::max($XNumBits, $YNumBits); | ||
| 253 | } else { | ||
| 254 | $numBits = 0; | ||
| 255 | } | ||
| 256 | if ($numBits < 2) { | ||
| 257 | $numBits = 2; | ||
| 258 | } | ||
| 259 | $writer->putUIBits($numBits - 2, 4); | ||
| 260 | if ($deltaX && $deltaY) { | ||
| 261 | $writer->putUIBit(1); # GeneralLineFlag | ||
| 262 | $writer->putSIBits($deltaX, $numBits); | ||
| 263 | $writer->putSIBits($deltaY, $numBits); | ||
| 264 | } else { | ||
| 265 | $writer->putUIBit(0); # GeneralLineFlag | ||
| 266 | if ($deltaX) { | ||
| 267 | $writer->putUIBit(0); # VertLineFlag | ||
| 268 | $writer->putSIBits($deltaX, $numBits); | ||
| 269 | } else { | ||
| 270 | $writer->putUIBit(1); # VertLineFlag | ||
| 271 | $writer->putSIBits($deltaY, $numBits); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 275 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 276 | } else { | ||
| 277 | my $controlDeltaX = $shapeRecord->{'ControlX'} - $currentDrawingPositionX; | ||
| 278 | my $controlDeltaY = $shapeRecord->{'ControlY'} - $currentDrawingPositionY; | ||
| 279 | $currentDrawingPositionX = $shapeRecord->{'ControlX'}; | ||
| 280 | $currentDrawingPositionY = $shapeRecord->{'ControlY'}; | ||
| 281 | my $anchorDeltaX = $shapeRecord->{'AnchorX'} - $currentDrawingPositionX; | ||
| 282 | my $anchorDeltaY = $shapeRecord->{'AnchorY'} - $currentDrawingPositionY; | ||
| 283 | $currentDrawingPositionX = $shapeRecord->{'AnchorX'}; | ||
| 284 | $currentDrawingPositionY = $shapeRecord->{'AnchorY'}; | ||
| 285 | |||
| 286 | my $numBitsControlDeltaX = $writer->need_bits_signed($controlDeltaX); | ||
| 287 | my $numBitsControlDeltaY = $writer->need_bits_signed($controlDeltaY); | ||
| 288 | my $numBitsAnchorDeltaX = $writer->need_bits_signed($anchorDeltaX); | ||
| 289 | my $numBitsAnchorDeltaY = $writer->need_bits_signed($anchorDeltaY); | ||
| 290 | my $numBits = List::Util::max($numBitsControlDeltaX, $numBitsControlDeltaY, $numBitsAnchorDeltaX, $numBitsAnchorDeltaY); | ||
| 291 | if ($numBits < 2) { | ||
| 292 | $numBits = 2; | ||
| 293 | } | ||
| 294 | $writer->putUIBits($numBits - 2, 4); | ||
| 295 | $writer->putSIBits($controlDeltaX, $numBits); | ||
| 296 | $writer->putSIBits($controlDeltaY, $numBits); | ||
| 297 | $writer->putSIBits($anchorDeltaX, $numBits); | ||
| 298 | $writer->putSIBits($anchorDeltaY, $numBits); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | return 1; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub string { | ||
| 306 | my ($shapeRecords_aref, $opts_href) = @_; | ||
| 307 | my @shapeRecords = ref($shapeRecords_aref) ? @{$shapeRecords_aref} : (); | ||
| 308 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 309 | |||
| 310 | my $tagCode = $opts{'tagCode'}; | ||
| 311 | foreach my $row (@shapeRecords) { | ||
| 312 | my %shapeRecord = %{$row}; | ||
| 313 | my $typeFlag = $shapeRecord{'TypeFlag'}; | ||
| 314 | if ($typeFlag == 0) { | ||
| 315 | if (defined ($shapeRecord{'EndOfShape'})) { | ||
| 316 | last; | ||
| 317 | } else { | ||
| 318 | my $moveX = $shapeRecord{'MoveX'} / 20; | ||
| 319 | my $moveY = $shapeRecord{'MoveY'} / 20; | ||
| 320 | print "\tChangeStyle: MoveTo: ($moveX, $moveY)"; | ||
| 321 | my @style_list = ('FillStyle0', 'FillStyle1', 'LineStyle'); | ||
| 322 | print " FillStyle: ".$shapeRecord{'FillStyle0'}."|".$shapeRecord{'FillStyle1'}; | ||
| 323 | print " LineStyle: ".$shapeRecord{'LineStyle'}."\n"; | ||
| 324 | if (defined ($shapeRecord{'FillStyles'})) { | ||
| 325 | print " FillStyles:\n"; | ||
| 326 | print IO::SWF::Type::FILLSTYLEARRAY::string($shapeRecord{'FillStyles'}, \%opts); | ||
| 327 | } | ||
| 328 | if (defined ($shapeRecord{'LineStyles'})) { | ||
| 329 | print " LineStyles:\n"; | ||
| 330 | print IO::SWF::Type::LINESTYLEARRAY::string($shapeRecord{'LineStyles'}, \%opts); | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } else { | ||
| 334 | my $straightFlag = $shapeRecord{'StraightFlag'}; | ||
| 335 | if ($straightFlag) { | ||
| 336 | my $x = $shapeRecord{'X'} / 20; | ||
| 337 | my $y = $shapeRecord{'Y'} / 20; | ||
| 338 | print "\tStraightEdge: MoveTo: ($x, $y)\n"; | ||
| 339 | } else { | ||
| 340 | my $controlX = $shapeRecord{'ControlX'} / 20; | ||
| 341 | my $controlY = $shapeRecord{'ControlY'} / 20; | ||
| 342 | my $anchorX = $shapeRecord{'AnchorX'} / 20; | ||
| 343 | my $anchorY = $shapeRecord{'AnchorY'} / 20; | ||
| 344 | print "\tCurvedEdge: MoveTo: Control($controlX, $controlY) Anchor($anchorX, $anchorY)\n"; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | 1; |