Convert-Braille
view release on metacpan or search on metacpan
lib/Convert/Braille.pm view on Meta::CPAN
'^' => 'â ',
'>' => 'â ',
'\'' => 'â ',
'*' => 'â ¡',
'<' => 'â £',
'-' => 'â ¤',
'.' => 'â ¨',
'%' => 'â ©',
'[' => 'â ª',
'$' => 'â «',
'+' => 'â ¬',
'!' => 'â ®',
'&' => 'â ¯',
';' => 'â °',
':' => 'â ±',
'\\' => 'â ³',
'(' => 'â ·',
'_' => 'â ¸',
'?' => 'â ¹',
']' => 'â »',
'#' => 'â ¼',
')' => 'â ¾',
'=' => 'â ¿'
);
foreach ( keys %BrailleAscii_To_Unicode ) {
$BrailleUnicode_To_Ascii{$BrailleAscii_To_Unicode{$_}} = $_;
}
$dot_separator = "";
}
sub _convert
{
return unless ( defined($_[0]) );
my ( $token, $hash ) = @_;
( exists($hash->{$token}) ) ? $hash->{$token} : $token ;
}
sub brailleAscii_To_Unicode
{
return unless ( defined($_[0]) );
my $ascii = uc($_[0]);
$ascii =~ s/(.)/_convert ( $1, \%BrailleAscii_To_Unicode )/ge;
$ascii;
}
sub brailleUnicode_To_Ascii
{
return unless ( defined($_[0]) );
my $unicode = $_[0];
#
# first strip off dots 7 and 8:
#
if ( $unicode =~ /â¡-⣿/ ) {
$unicode =~ tr/â¢-⣿/â -â¡¿/; # fold upper half
$unicode =~ tr/â¡-â¡¿/â -â ¿/; # fold upper quarter
}
$unicode =~ s/(.)/_convert ( $1, \%BrailleUnicode_To_Ascii )/ge;
$unicode;
}
sub brailleUnicode_To_DotNumbers
{
my $string = shift; # no || "" because fail for '0'
return "" if !defined $string || $string eq "";
my $braced = ( @_ ) ? shift : 0 ;
my @chars = split ( //, $string );
my ($trans, $dots);
foreach ( @chars ) {
if ( /[â -⣿]/ ) { # assume UTF8
my $char = ord ( $_ ) - 0x2800;
$trans .= $dot_separator if ( $dots );
$dots = undef;
$dots = "1" if ( $char & 0x1 );
$dots .= "2" if ( $char & 0x2 );
$dots .= "3" if ( $char & 0x4 );
$dots .= "4" if ( $char & 0x8 );
$dots .= "5" if ( $char & 0x10 );
$dots .= "6" if ( $char & 0x20 );
$dots .= "7" if ( $char & 0x40 );
$dots .= "8" if ( $char & 0x80 );
$trans .= ($braced) ? "[$dots]" : $dots;
}
else {
$trans .= "$_";
$dots = undef;
}
}
$trans;
}
sub brailleDotNumbers_To_Unicode
{
my $string = shift;
return "" if !defined $string || $string eq "";
$string =~ s/$dot_separator//g if( $dot_separator );
my @bits = split ( //, $string );
my ($char, $lastBit, $trans) = (0,0,"");
foreach ( @bits ) {
my $bit = $_;
if ( $bit =~ /[1-8]/ ) {
if ( $bit > $lastBit ) {
# bit continues sequence
$char += 2**($bit-1);
}
else {
( run in 0.727 second using v1.01-cache-2.11-cpan-39bf76dae61 )