| 1 | package IO::SWF::Type::MATRIX; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Type'; | ||
| 7 | |||
| 8 | use IO::SWF::Bit; | ||
| 9 | use List::Util; | ||
| 10 | |||
| 11 | sub parse { | ||
| 12 | my ($reader, $opts_href) = @_; | ||
| 13 | my %matrix = (); | ||
| 14 | |||
| 15 | $reader->byteAlign(); | ||
| 16 | my $hasScale = $reader->getUIBit(); | ||
| 17 | if ($hasScale) { | ||
| 18 | my $nScaleBits = $reader->getUIBits(5); | ||
| 19 | # $matrix{'(NScaleBits)'} = $nScaleBits; | ||
| 20 | $matrix{'ScaleX'} = $reader->getSIBits($nScaleBits); | ||
| 21 | $matrix{'ScaleY'} = $reader->getSIBits($nScaleBits); | ||
| 22 | } else { | ||
| 23 | $matrix{'ScaleX'} = 0x10000; | ||
| 24 | $matrix{'ScaleY'} = 0x10000; | ||
| 25 | } | ||
| 26 | my $hasRotate = $reader->getUIBit(); | ||
| 27 | if ($hasRotate) { | ||
| 28 | my $nRotateBits = $reader->getUIBits(5); | ||
| 29 | # $matrix{'(NRotateBits)'} = $nRotateBits; | ||
| 30 | $matrix{'RotateSkew0'} = $reader->getSIBits($nRotateBits); | ||
| 31 | $matrix{'RotateSkew1'} = $reader->getSIBits($nRotateBits); | ||
| 32 | } else { | ||
| 33 | $matrix{'RotateSkew0'} = 0; | ||
| 34 | $matrix{'RotateSkew1'} = 0; | ||
| 35 | } | ||
| 36 | my $nTranslateBits = $reader->getUIBits(5); | ||
| 37 | $matrix{'TranslateX'} = $reader->getSIBits($nTranslateBits); | ||
| 38 | $matrix{'TranslateY'} = $reader->getSIBits($nTranslateBits); | ||
| 39 | return \%matrix; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub build { | ||
| 43 | my ($writer, $matrix_href, $opts_href) = @_; | ||
| 44 | my %matrix = ref($matrix_href) ? %{$matrix_href} : (); | ||
| 45 | $writer->byteAlign(); | ||
| 46 | if (($matrix{'ScaleX'} != 0x10000) || ($matrix{'ScaleY'} != 0x10000)) { | ||
| 47 | $writer->putUIBit(1); # HasScale; | ||
| 48 | my $nScaleBits; | ||
| 49 | if ($matrix{'ScaleX'} | $matrix{'ScaleY'}) { | ||
| 50 | my $xNBits = $writer->need_bits_signed($matrix{'ScaleX'}); | ||
| 51 | my $yNBits = $writer->need_bits_signed($matrix{'ScaleY'}); | ||
| 52 | $nScaleBits = List::Util::max($xNBits, $yNBits); | ||
| 53 | } else { | ||
| 54 | $nScaleBits = 0; | ||
| 55 | } | ||
| 56 | $writer->putUIBits($nScaleBits, 5); | ||
| 57 | $writer->putSIBits($matrix{'ScaleX'}, $nScaleBits); | ||
| 58 | $writer->putSIBits($matrix{'ScaleY'}, $nScaleBits); | ||
| 59 | } else { | ||
| 60 | $writer->putUIBit(0); # HasScale; | ||
| 61 | } | ||
| 62 | if ($matrix{'RotateSkew0'} | $matrix{'RotateSkew1'}) { | ||
| 63 | $writer->putUIBit(1); # HasRotate | ||
| 64 | my $nRotateBits; | ||
| 65 | if ($matrix{'RotateSkew0'} | $matrix{'RotateSkew1'}) { | ||
| 66 | my $rs0NBits = $writer->need_bits_signed($matrix{'RotateSkew0'}); | ||
| 67 | my $rs1NBits = $writer->need_bits_signed($matrix{'RotateSkew1'}); | ||
| 68 | $nRotateBits = List::Util::max($rs0NBits, $rs1NBits); | ||
| 69 | } else { | ||
| 70 | $nRotateBits = 0; | ||
| 71 | } | ||
| 72 | $writer->putUIBits($nRotateBits, 5); | ||
| 73 | $writer->putSIBits($matrix{'RotateSkew0'}, $nRotateBits); | ||
| 74 | $writer->putSIBits($matrix{'RotateSkew1'}, $nRotateBits); | ||
| 75 | } else { | ||
| 76 | $writer->putUIBit(0); # HasRotate | ||
| 77 | } | ||
| 78 | my $nTranslateBits; | ||
| 79 | if ($matrix{'TranslateX'} | $matrix{'TranslateY'}) { | ||
| 80 | my $xNTranslateBits = $writer->need_bits_signed($matrix{'TranslateX'}); | ||
| 81 | my $yNTranslateBits = $writer->need_bits_signed($matrix{'TranslateY'}); | ||
| 82 | $nTranslateBits = List::Util::max($xNTranslateBits, $yNTranslateBits); | ||
| 83 | } else { | ||
| 84 | $nTranslateBits = 0; | ||
| 85 | } | ||
| 86 | $writer->putUIBits($nTranslateBits, 5); | ||
| 87 | $writer->putSIBits($matrix{'TranslateX'}, $nTranslateBits); | ||
| 88 | $writer->putSIBits($matrix{'TranslateY'}, $nTranslateBits); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub string { | ||
| 92 | my ($matrix_href, $opts_href) = @_; | ||
| 93 | my %matrix = ref($matrix_href) ? %{$matrix_href} : (); | ||
| 94 | my %opts = ref($opts_href) ? %{$opts_href} : (); | ||
| 95 | |||
| 96 | my $indent = 0; | ||
| 97 | if (defined $opts{'indent'}) { | ||
| 98 | $indent = $opts{'indent'}; | ||
| 99 | } | ||
| 100 | my $text_fmt = << 'EOS'; | ||
| 101 | %s| %3.3f %3.3f | %3.2f | ||
| 102 | %s| %3.3f %3.3f | %3.2f | ||
| 103 | EOS | ||
| 104 | return sprintf($text_fmt, | ||
| 105 | "\t" x $indent, | ||
| 106 | $matrix{'ScaleX'} / 0x10000, | ||
| 107 | $matrix{'RotateSkew0'} / 0x10000, | ||
| 108 | $matrix{'TranslateX'} / 20, | ||
| 109 | "\t" x $indent, | ||
| 110 | $matrix{'RotateSkew1'} / 0x10000, | ||
| 111 | $matrix{'ScaleY'} / 0x10000, | ||
| 112 | $matrix{'TranslateY'} / 20); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |