| 1 | package Encode::JP::Mobile::Vodafone; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw(Encode::Encoding); |
|---|
| 4 | __PACKAGE__->Define(qw(x-sjis-vodafone-raw)); |
|---|
| 5 | |
|---|
| 6 | use Encode::Alias; |
|---|
| 7 | define_alias('x-sjis-softbank-raw' => 'x-sjis-vodafone-raw'); |
|---|
| 8 | |
|---|
| 9 | # G! => E001, G" => E002, G# => E003 ... |
|---|
| 10 | # E! => E101, F! => E201, O! => E301, P! => E401, Q! => E501 |
|---|
| 11 | my %HighCharToBit = (G => 0xE000, E => 0xE100, F => 0xE200, |
|---|
| 12 | O => 0xE300, P => 0xE400, Q => 0xE500); |
|---|
| 13 | my %HighBitToChar = reverse %HighCharToBit; |
|---|
| 14 | |
|---|
| 15 | my $range = '\x{E001}-\x{E05A}\x{E101}-\x{E15A}\x{E201}-\x{E25A}\x{E301}-\x{E34D}\x{E401}-\x{E44C}\x{E501}-\x{E539}'; |
|---|
| 16 | my $InRange = "[$range]"; |
|---|
| 17 | my $OutRange = "[^$range]"; |
|---|
| 18 | |
|---|
| 19 | sub decode($$;$) { |
|---|
| 20 | my($self, $char, $check) = @_; |
|---|
| 21 | my $str = Encode::decode("cp932", $char, Encode::FB_PERLQQ); |
|---|
| 22 | $str =~ s{\x1b\x24([GEFOPQ])([\x20-\x7F]+)\x0f}{ |
|---|
| 23 | join '', map chr($HighCharToBit{$1} | ord($_) - 32), split //, $2; |
|---|
| 24 | }ge; |
|---|
| 25 | $_[1] = $str if $check; |
|---|
| 26 | $str; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub encode($$;$) { |
|---|
| 30 | my($self, $str, $check) = @_; |
|---|
| 31 | my $res = ''; |
|---|
| 32 | $str =~ tr/\x{301C}/\x{FF5E}/; # ad-hoc solution for FULLWIDTH TILDE Problem |
|---|
| 33 | $str =~ s{($InRange+)|($OutRange+)}{ |
|---|
| 34 | my $in = defined $1; |
|---|
| 35 | my $m = $in ? $1 : $2; |
|---|
| 36 | $res .= $in ? _encode_vodafone($m) |
|---|
| 37 | : Encode::encode("cp932", $m, $check); |
|---|
| 38 | '' |
|---|
| 39 | }egs; |
|---|
| 40 | $_[1] = $res if $check; |
|---|
| 41 | $res; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | sub _encode_vodafone { |
|---|
| 45 | my $str = shift; |
|---|
| 46 | my @str = split //, $str; |
|---|
| 47 | my $res = "\x1b\x24"; |
|---|
| 48 | my $buf = ''; |
|---|
| 49 | for my $str (@str) { |
|---|
| 50 | my $high = ord($str) & 0xEF00; |
|---|
| 51 | my $low = ord($str) & 0x00FF; |
|---|
| 52 | if ($buf ne $high) { |
|---|
| 53 | $res .= $HighBitToChar{$high}; |
|---|
| 54 | } |
|---|
| 55 | $res .= chr($low+32); |
|---|
| 56 | $buf = $high; |
|---|
| 57 | } |
|---|
| 58 | $res . "\x0f"; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | 1; |
|---|