| 1 | package IO::SWF::Tag::Shape; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Tag::Base'; | ||
| 7 | |||
| 8 | use IO::SWF::Bit; | ||
| 9 | use IO::SWF::Type::RECT; | ||
| 10 | use IO::SWF::Type::FILLSTYLEARRAY; | ||
| 11 | use IO::SWF::Type::LINESTYLEARRAY; | ||
| 12 | use IO::SWF::Type::SHAPE; | ||
| 13 | |||
| 14 | __PACKAGE__->mk_accessors( qw( | ||
| 15 | _shapeId | ||
| 16 | _shapeBounds | ||
| 17 | _fillStyles | ||
| 18 | _lineStyles | ||
| 19 | _shapeRecords | ||
| 20 | _startBounds | ||
| 21 | _endBounds | ||
| 22 | _offset | ||
| 23 | _morphFillStyles | ||
| 24 | _morphLineStyles | ||
| 25 | _startEdge | ||
| 26 | _endEdges | ||
| 27 | )); | ||
| 28 | |||
| 29 | sub parseContent { | ||
| 30 | my ($self, $tagCode, $content, $opts_href) = @_; | ||
| 31 | |||
| 32 | my $isMorph = ($tagCode == 46 || $tagCode == 84) ? 1 : 0; | ||
| 33 | my $reader = IO::SWF::Bit->new(); | ||
| 34 | $reader->input($content); | ||
| 35 | $self->_shapeId($reader->getUI16LE()); | ||
| 36 | |||
| 37 | my %opts = ('tagCode' => $tagCode, 'isMorph' => $isMorph); | ||
| 38 | |||
| 39 | if (!$isMorph) { | ||
| 40 | # style | ||
| 41 | $self->_shapeBounds(IO::SWF::Type::RECT::parse($reader)); | ||
| 42 | $self->_fillStyles(IO::SWF::Type::FILLSTYLEARRAY::parse($reader, \%opts)); | ||
| 43 | $self->_lineStyles(IO::SWF::Type::LINESTYLEARRAY::parse($reader, \%opts)); | ||
| 44 | # shape | ||
| 45 | $self->_shapeRecords(IO::SWF::Type::SHAPE::parse($reader, \%opts)); | ||
| 46 | } else { | ||
| 47 | $self->_startBounds(IO::SWF::Type::RECT::parse($reader)); | ||
| 48 | $self->_endBounds(IO::SWF::Type::RECT::parse($reader)); | ||
| 49 | $self->_offset($reader->getUI32LE()); | ||
| 50 | # style | ||
| 51 | $self->_morphFillStyles(IO::SWF::Type::FILLSTYLEARRAY::parse($reader, \%opts)); | ||
| 52 | $self->_morphLineStyles(IO::SWF::Type::LINESTYLEARRAY::parse($reader, \%opts)); | ||
| 53 | # shape | ||
| 54 | $self->_startEdge(IO::SWF::Type::SHAPE::parse($reader, \%opts)); | ||
| 55 | $self->_endEdge(IO::SWF::Type::SHAPE::parse($reader, \%opts)); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub dumpContent { | ||
| 60 | my ($self, $tagCode, $opts_href) = @_; | ||
| 61 | |||
| 62 | my $isMorph = ($tagCode == 46 || $tagCode == 84) ? 1 : 0; | ||
| 63 | if ($self->_shapeId) { | ||
| 64 | print " ShapeId: {" . $self->_shapeId . "}\n"; | ||
| 65 | } | ||
| 66 | my %opts = ('tagCode' => $tagCode, 'isMorph' => $isMorph); | ||
| 67 | |||
| 68 | if (!$isMorph) { | ||
| 69 | print " ShapeBounds: ". IO::SWF::Type::RECT::string($self->_shapeBounds)."\n"; | ||
| 70 | print " FillStyles:\n"; | ||
| 71 | print IO::SWF::Type::FILLSTYLEARRAY::string($self->_fillStyles, \%opts); | ||
| 72 | print " LineStyles:\n"; | ||
| 73 | print IO::SWF::Type::LINESTYLEARRAY::string($self->_lineStyles, \%opts); | ||
| 74 | |||
| 75 | print " ShapeRecords:\n"; | ||
| 76 | print IO::SWF::Type::SHAPE::string($self->_shapeRecords, \%opts); | ||
| 77 | } else { | ||
| 78 | print " StartBounds: ". IO::SWF::Type::RECT::string($self->_startBounds)."\n"; | ||
| 79 | print " EndBounds: ". IO::SWF::Type::RECT::string($self->_endBounds)."\n"; | ||
| 80 | print " FillStyles:\n"; | ||
| 81 | print IO::SWF::Type::FILLSTYLEARRAY::string($self->_morphFillStyles, \%opts); | ||
| 82 | print " LineStyles:\n"; | ||
| 83 | print IO::SWF::Type::LINESTYLEARRAY::string($self->_morphLineStyles, \%opts); | ||
| 84 | |||
| 85 | print " StartEdge:\n"; | ||
| 86 | print IO::SWF::Type::SHAPE::string($self->_startEdge, \%opts); | ||
| 87 | print " endEdge:\n"; | ||
| 88 | print IO::SWF::Type::SHAPE::string($self->_endEdge, \%opts); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub buildContent { | ||
| 93 | my ($self, $tagCode, $opts_href) = @_; | ||
| 94 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 95 | |||
| 96 | my $isMorph = ($tagCode == 46 || $tagCode == 84) ? 1 : 0; | ||
| 97 | my $writer = IO::SWF::Bit->new(); | ||
| 98 | if (!exists($opts{'noShapeId'})) { | ||
| 99 | $writer->putUI16LE($self->_shapeId); | ||
| 100 | } | ||
| 101 | %opts = ('tagCode' => $tagCode); | ||
| 102 | |||
| 103 | if (!$isMorph) { | ||
| 104 | IO::SWF::Type::RECT::build($writer, $self->_shapeBounds); | ||
| 105 | # style | ||
| 106 | IO::SWF::Type::FILLSTYLEARRAY::build($writer, $self->_fillStyles, \%opts); | ||
| 107 | IO::SWF::Type::LINESTYLEARRAY::build($writer, $self->_lineStyles, \%opts); | ||
| 108 | # shape | ||
| 109 | $opts{'fillStyleCount'} = scalar(@{$self->_fillStyles}); | ||
| 110 | $opts{'lineStyleCount'} = scalar(@{$self->_lineStyles}); | ||
| 111 | IO::SWF::Type::SHAPE::build($writer, $self->_shapeRecords, \%opts); | ||
| 112 | } else { | ||
| 113 | IO::SWF::Type::RECT::build($writer, $self->_startBounds); | ||
| 114 | IO::SWF::Type::RECT::build($writer, $self->_endBounds); | ||
| 115 | # style | ||
| 116 | IO::SWF::Type::FILLSTYLEARRAY::build($writer, $self->_morphFillStyles, \%opts); | ||
| 117 | IO::SWF::Type::LINESTYLEARRAY::build($writer, $self->_morphLineStyles, \%opts); | ||
| 118 | # shape | ||
| 119 | $opts{'fillStyleCount'} = scalar(@{$self->_morphFillStyles}); | ||
| 120 | $opts{'lineStyleCount'} = scalar(@{$self->_morphLineStyles}); | ||
| 121 | IO::SWF::Type::SHAPE::build($writer, $self->_startEdge, \%opts); | ||
| 122 | IO::SWF::Type::SHAPE::build($writer, $self->_endEdge, \%opts); | ||
| 123 | } | ||
| 124 | return $writer->output(); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub deforme { | ||
| 128 | my ($self, $threshold) = @_; | ||
| 129 | my $startIndex = undef(); | ||
| 130 | my $endIndex; | ||
| 131 | my @shapeRecords = @{$self->_shapeRecords}; | ||
| 132 | for (my $shapeRecordIndex = 0; $shapeRecordIndex < @shapeRecords; $shapeRecordIndex++) { | ||
| 133 | my %shapeRecord = %{$shapeRecords[$shapeRecordIndex]}; | ||
| 134 | if ($shapeRecord{'TypeFlag'} == 0 && !defined $shapeRecord{'EndOfShape'}) { | ||
| 135 | # StyleChangeRecord | ||
| 136 | $endIndex = $shapeRecordIndex - 1; | ||
| 137 | if ($startIndex) { | ||
| 138 | $self->deformeShapeRecordUnit($threshold, $startIndex, $endIndex); | ||
| 139 | } | ||
| 140 | $startIndex = $shapeRecordIndex; | ||
| 141 | } | ||
| 142 | if (defined $shapeRecord{'EndOfShape'} && $shapeRecord{'EndOfShape'} == 0) { | ||
| 143 | # EndShapeRecord | ||
| 144 | $endIndex = $shapeRecordIndex - 1; | ||
| 145 | $self->deformeShapeRecordUnit($threshold, $startIndex, $endIndex); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | # my @array_value = map{ $_ } values @{$self->_shapeRecords}; | ||
| 149 | # $self->_shapeRecords(\@array_value); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub deformeShapeRecordUnit { | ||
| 153 | my ($self, $threshold, $startIndex, $endIndex) = @_; | ||
| 154 | # return $self->deformeShapeRecordUnit_1($threshold, $startIndex, $endIndex); | ||
| 155 | return $self->deformeShapeRecordUnit_2($threshold, $startIndex, $endIndex); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub deformeShapeRecordUnit_1 { | ||
| 159 | my ($self, $threshold, $startIndex, $endIndex) = @_; | ||
| 160 | my $threshold_2 = $threshold * $threshold; | ||
| 161 | my $shapeRecord = @{$self->_shapeRecords}[$startIndex]; | ||
| 162 | my $prevIndex = undef(); | ||
| 163 | my $prevDrawingPositionX; | ||
| 164 | my $prevDrawingPositionY; | ||
| 165 | my $currentDrawingPositionX = $shapeRecord->{'MoveX'}; | ||
| 166 | my $currentDrawingPositionY = $shapeRecord->{'MoveY'}; | ||
| 167 | for (my $i = $startIndex + 1 ;$i <= $endIndex; $i++) { | ||
| 168 | $shapeRecord = @{$self->_shapeRecords}[$i]; | ||
| 169 | my ($diff_x, $diff_y, $distance_2, $distance_2_control, $distance_2_anchor); | ||
| 170 | if ($shapeRecord->{'StraightFlag'} == 0) { | ||
| 171 | # process for curve | ||
| 172 | $diff_x = $shapeRecord->{'ControlX'} - $currentDrawingPositionX; | ||
| 173 | $diff_y = $shapeRecord->{'ControlY'} - $currentDrawingPositionY; | ||
| 174 | $distance_2_control = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 175 | $diff_x = $shapeRecord->{'AnchorX'} - $currentDrawingPositionX; | ||
| 176 | $diff_y = $shapeRecord->{'AnchorY'} - $currentDrawingPositionY; | ||
| 177 | $distance_2_anchor = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 178 | if (($distance_2_control + $distance_2_anchor) > $threshold_2) { | ||
| 179 | # nothing | ||
| 180 | $prevIndex = $i; | ||
| 181 | $prevDrawingPositionX = $currentDrawingPositionX; | ||
| 182 | $prevDrawingPositionY = $currentDrawingPositionY; | ||
| 183 | $currentDrawingPositionX = $shapeRecord->{'AnchorX'}; | ||
| 184 | $currentDrawingPositionY = $shapeRecord->{'AnchorY'}; | ||
| 185 | next; # skip | ||
| 186 | } | ||
| 187 | # convert to straight | ||
| 188 | $shapeRecord->{'StraightFlag'} = 1; # to Straight | ||
| 189 | $shapeRecord->{'X'} = $shapeRecord->{'AnchorX'}; | ||
| 190 | $shapeRecord->{'Y'} = $shapeRecord->{'AnchorY'}; | ||
| 191 | delete $shapeRecord->{'ControlX'}; | ||
| 192 | delete $shapeRecord->{'ControlY'}; | ||
| 193 | delete $shapeRecord->{'AnchorX'}; | ||
| 194 | delete $shapeRecord->{'AnchorY'}; | ||
| 195 | } | ||
| 196 | if (!$prevIndex) { | ||
| 197 | # nothing | ||
| 198 | $prevIndex = $i; | ||
| 199 | $prevDrawingPositionX = $currentDrawingPositionX; | ||
| 200 | $prevDrawingPositionY = $currentDrawingPositionY; | ||
| 201 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 202 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 203 | next; # skip | ||
| 204 | } | ||
| 205 | $diff_x = $shapeRecord->{'X'} - $prevDrawingPositionX; | ||
| 206 | $diff_y = $shapeRecord->{'Y'} - $prevDrawingPositionY; | ||
| 207 | $distance_2 = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 208 | if ($distance_2 > $threshold_2) { | ||
| 209 | # nothing | ||
| 210 | $prevIndex = $i; | ||
| 211 | $prevDrawingPositionX = $currentDrawingPositionX; | ||
| 212 | $prevDrawingPositionY = $currentDrawingPositionY; | ||
| 213 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 214 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 215 | next; # skip | ||
| 216 | } | ||
| 217 | # joint to previous shape | ||
| 218 | my $prevShapeRecord = @{$self->_shapeRecords}[$prevIndex]; | ||
| 219 | $prevShapeRecord->{'X'} = $shapeRecord->{'X'}; | ||
| 220 | $prevShapeRecord->{'Y'} = $shapeRecord->{'Y'}; | ||
| 221 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 222 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 223 | @{$self->_shapeRecords}[$i] = undef(); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | sub deformeShapeRecordUnit_2 { | ||
| 228 | my ($self, $threshold, $startIndex, $endIndex) = @_; | ||
| 229 | $self->deformeShapeRecordUnit_2_curve($threshold, $startIndex, $endIndex); | ||
| 230 | while ($self->deformeShapeRecordUnit_2_line($threshold, $startIndex, $endIndex)) {}; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub deformeShapeRecordUnit_2_curve { | ||
| 234 | my ($self, $threshold, $startIndex, $endIndex) = @_; | ||
| 235 | my $threshold_2 = $threshold * $threshold; | ||
| 236 | my $shapeRecord = @{$self->_shapeRecords}[$startIndex]; | ||
| 237 | my $currentDrawingPositionX = $shapeRecord->{'MoveX'}; | ||
| 238 | my $currentDrawingPositionY = $shapeRecord->{'MoveY'}; | ||
| 239 | for (my $i = $startIndex + 1 ;$i <= $endIndex; $i++) { | ||
| 240 | $shapeRecord = @{$self->_shapeRecords}[$i]; | ||
| 241 | if ($shapeRecord->{'StraightFlag'} == 0) { | ||
| 242 | # process for curve | ||
| 243 | my $diff_x = $shapeRecord->{'ControlX'} - $currentDrawingPositionX; | ||
| 244 | my $diff_y = $shapeRecord->{'ControlY'} - $currentDrawingPositionY; | ||
| 245 | my $distance_2_control = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 246 | $diff_x = $shapeRecord->{'AnchorX'} - $currentDrawingPositionX; | ||
| 247 | $diff_y = $shapeRecord->{'AnchorY'} - $currentDrawingPositionY; | ||
| 248 | my $distance_2_anchor = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 249 | if (($distance_2_control + $distance_2_anchor) > $threshold_2) { | ||
| 250 | # nothing | ||
| 251 | $currentDrawingPositionX = $shapeRecord->{'AnchorX'}; | ||
| 252 | $currentDrawingPositionY = $shapeRecord->{'AnchorY'}; | ||
| 253 | next; # skip | ||
| 254 | } | ||
| 255 | # convert to straight | ||
| 256 | $shapeRecord->{'StraightFlag'} = 1; # to Straight | ||
| 257 | $shapeRecord->{'X'} = $shapeRecord->{'AnchorX'}; | ||
| 258 | $shapeRecord->{'Y'} = $shapeRecord->{'AnchorY'}; | ||
| 259 | delete $shapeRecord->{'ControlX'}; | ||
| 260 | delete $shapeRecord->{'ControlY'}; | ||
| 261 | delete $shapeRecord->{'AnchorX'}; | ||
| 262 | delete $shapeRecord->{'AnchorY'}; | ||
| 263 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 264 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | sub deformeShapeRecordUnit_2_line { | ||
| 270 | my ($self, $threshold, $startIndex, $endIndex) = @_; | ||
| 271 | my $threshold_2 = $threshold * $threshold; | ||
| 272 | my $shapeRecord = @{$self->_shapeRecords}[$startIndex]; | ||
| 273 | my $prevIndex = undef(); | ||
| 274 | my $currentDrawingPositionX = $shapeRecord->{'MoveX'}; | ||
| 275 | my $currentDrawingPositionY = $shapeRecord->{'MoveY'}; | ||
| 276 | my @distance_list_short = (); | ||
| 277 | my @distance_list_all = (); | ||
| 278 | my @distance_table_all = (); | ||
| 279 | for (my $i = $startIndex + 1 ;$i <= $endIndex; $i++) { | ||
| 280 | $shapeRecord = @{$self->_shapeRecords}[$i]; | ||
| 281 | my ($diff_x, $diff_y, $distance_2, $distance_2_control, $distance_2_anchor); | ||
| 282 | if ($shapeRecord->{'StraightFlag'} == 0) { | ||
| 283 | $diff_x = $shapeRecord->{'ControlX'} - $currentDrawingPositionX; | ||
| 284 | $diff_y = $shapeRecord->{'ControlY'} - $currentDrawingPositionY; | ||
| 285 | $distance_2_control = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 286 | $diff_x = $shapeRecord->{'AnchorX'} - $currentDrawingPositionX; | ||
| 287 | $diff_y = $shapeRecord->{'AnchorY'} - $currentDrawingPositionY; | ||
| 288 | $distance_2_anchor = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 289 | # $distance_list[$i] = $distance_2_control + $distance_2_anchor; | ||
| 290 | $distance_table_all[$i] = $distance_2_control + $distance_2_anchor; | ||
| 291 | $currentDrawingPositionX = $shapeRecord->{'AnchorX'}; | ||
| 292 | $currentDrawingPositionY = $shapeRecord->{'AnchorY'}; | ||
| 293 | } else { | ||
| 294 | $diff_x = $shapeRecord->{'X'} - $currentDrawingPositionX; | ||
| 295 | $diff_y = $shapeRecord->{'Y'} - $currentDrawingPositionY; | ||
| 296 | $distance_2 = $diff_x * $diff_x + $diff_y * $diff_y; | ||
| 297 | if ($distance_2 < $threshold_2) { | ||
| 298 | push @distance_list_short, $i; | ||
| 299 | } | ||
| 300 | $distance_table_all[$i] = $distance_2; | ||
| 301 | $currentDrawingPositionX = $shapeRecord->{'X'}; | ||
| 302 | $currentDrawingPositionY = $shapeRecord->{'Y'}; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | @distance_list_short = sort(@distance_list_short); | ||
| 306 | my $deforme_number = 0; | ||
| 307 | foreach my $i (@distance_list_short) { | ||
| 308 | if ($distance_table_all[$i] > $threshold_2) { | ||
| 309 | next; | ||
| 310 | } | ||
| 311 | if (!defined ($distance_list_all[$i-1]) && !defined ($distance_list_all[$i+1])) { | ||
| 312 | |||
| 313 | } | ||
| 314 | my $index_to_merge; | ||
| 315 | if (!defined ($distance_list_all[$i-1])) { | ||
| 316 | if (!defined ($distance_list_all[$i+1])) { | ||
| 317 | next; | ||
| 318 | } else { | ||
| 319 | $index_to_merge = $i+1; | ||
| 320 | } | ||
| 321 | } else { | ||
| 322 | if (!defined ($distance_list_all[$i+1])) { | ||
| 323 | $index_to_merge = $i-1; | ||
| 324 | } else { | ||
| 325 | $index_to_merge = $i-1; # XXX | ||
| 326 | } | ||
| 327 | } | ||
| 328 | $shapeRecord = @{$self->_shapeRecords}[$i]; | ||
| 329 | my $shapeRecord_toMerge = @{$self->_shapeRecords}[$index_to_merge]; | ||
| 330 | if ($i > $index_to_merge) { | ||
| 331 | if ($shapeRecord->{'StraightFlag'}) { | ||
| 332 | $shapeRecord_toMerge->{'X'} = $shapeRecord->{'X'}; | ||
| 333 | $shapeRecord_toMerge->{'Y'} = $shapeRecord->{'Y'}; | ||
| 334 | } else { | ||
| 335 | $shapeRecord_toMerge->{'AnchorX'} = $shapeRecord->{'X'}; | ||
| 336 | $shapeRecord_toMerge->{'AnchorY'} = $shapeRecord->{'Y'}; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | $distance_list_all[$index_to_merge] += $distance_list_all[$i]; | ||
| 340 | # unset($distance_list_all[$i]); | ||
| 341 | @{$self->_shapeRecords}[$i] = undef(); | ||
| 342 | $deforme_number += 1; | ||
| 343 | } | ||
| 344 | return $deforme_number; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub countEdges { | ||
| 348 | my $self = shift; | ||
| 349 | my $edges_count = 0; | ||
| 350 | my @shapeRecords; | ||
| 351 | if (defined ($self->_shapeRecords) && $self->_shapeRecords) { | ||
| 352 | @shapeRecords = @{$self->_shapeRecords}; | ||
| 353 | } | ||
| 354 | elsif (defined ($self->_startEdge) && $self->_startEdge) { | ||
| 355 | @shapeRecords = @{$self->_startEdge}; | ||
| 356 | } | ||
| 357 | else { | ||
| 358 | @shapeRecords = (); # nothing to do. | ||
| 359 | } | ||
| 360 | foreach my $shapeRecord (@shapeRecords) { | ||
| 361 | if (defined ($shapeRecord->{'StraightFlag'})) { # XXX | ||
| 362 | $edges_count++; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | return ($self->_shapeId, $edges_count); | ||
| 366 | } | ||
| 367 | |||
| 368 | 1; |