blib/lib/App/Brl2Brl.pm

filter: Statementstmt 91.5% 87 / 95 Branchbran 71.0% 27 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 81.6% 129 / 158 CC 24 SCAR 25.8
CC
24
Cov
82%
Conv
25.7
parse_dis
11.1
new
3.0
CRAP 13.2
↑ Summary
1 package App::Brl2Brl;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our @EXPORT_OK = qw(parse_dis Conv switch_brl_char_map new);
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 App::Brl2Brl - Convert between braille display tables defined in Liblouis.
18  
19 =head1 VERSION
20  
21 Version 0.05
22  
23 =cut
24  
25 2 our $VERSION = '0.05';
26  
27  
28 =head1 SYNOPSIS
29  
30 This module is useful if you have a text coded for one braille character set and need to convert it to another, e.g. you have a text in North American ASCII or Eurobraille and you need it in Unicode braille.
31  
32 use App::Brl2Brl;
33  
34 my $brl_obj = App::Brl2Brl->new({ # to read in the specified files and store the characters/dots in hashes
35 from_table_file => 'en-us-brf.dis', # or another display table
36 to_table_file => 'unicode.dis', # or another display table
37 warn => 1, # if you want to be warned if a char isn't defined in table
38 });
39 my $out = $brl_obj->switch_brl_char_map('ABC123'); # switch from BRF to Unicode braille
40 print "$out\n";
41  
42 Or you may do:
43  
44 use App::Brl2Brl;
45  
46 my $from_table_file = 'en-us-brf.dis';
47 my $to_table_file = 'unicode.dis';
48  
49 my %from_table = parse_dis( "$from_table_file" );
50 my %to_table = parse_dis( "$to_table_file" );
51 while( <> ){
52 my $out = Conv( \%from_table, \%to_table, $_);
53 print "$out\n";
54 };
55  
56  
57 =head1 EXPORT
58  
59 parse_dis - Parses a given display table
60  
61 Conv - Convert from one display table to another.
62  
63 =head1 SUBROUTINES/METHODS
64  
65 =head2 new
66  
67 Takes the following parameters:
68  
69 path => '/usr/share/liblouis/tables', # path to liblouis tables
70 from_table_file => 'en-us-brf.dis', # or another display table
71 to_table_file => 'unicode.dis', # or another display table
72 warn => 1, # if you want to be warned if a char isn't defined in table
73  
74 The path is optional. App::Brl2Brl comes with a copy of the data files
75 and knows where to find them. Only provide this if you want to use a
76 different set of data files, perhaps a more recent one. As with most
77 liblouis software you can also set C<LOUIS_TABLEPATH> in your environment.
78  
79 The order of precedence is that the value in a C<path> argument will be used,
80 falling back to C<LOUIS_TABLEPATH>, falling back to using the data bundled with
81 the module.
82  
83 =cut
84  
85 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
86 3 my ($class,$args) = @_;
87  
88 # figure out which path to use
89 3 ▶ if(!exists($args->{path})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"path"}) 2 1 3
90 2 ▶ if(exists($ENV{LOUIS_TABLEPATH})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'LOUIS_TABLEPATH'}) { } 1 1 2
91 1 $args->{path} = $ENV{LOUIS_TABLEPATH};
92 } else {
93 1 $args->{path} = dist_dir('App-Brl2Brl');
94 }
95 }
96  
97 my $self = {
98 path => $args->{path},
99 from_table_file => $args->{from_table_file},
100 to_table_file => $args->{to_table_file},
101 warn => $args->{warn},
102 3 }; # $self
103 3 my $complete_from_filename = "$self->{path}/"."$self->{from_table_file}";
104 3 my $complete_to_filename = "$self->{path}/"."$self->{to_table_file}";
105 3 $self->{from_table} = { parse_dis( $complete_from_filename ) };
106 1 $self->{to_table} = { parse_dis( $complete_to_filename ) };
107
108 1 bless( $self, $class );
109 1 return $self;
110 } # new
111  
112 =head2 switch_brl_char_map
113  
114 Switch a character or string of characters from one character set
115 to another, defined by from_table and to_table set in the new function.
116  
117 =cut
118  
119 1 ▶sub switch_brl_char_map {
Subroutine switch_brl_char_map: called CC=1 SCAR=0.0
Pod documented
120 1 my $self = shift;
121 1 my $inputstr = shift;
122 1 my $warn = $self->{warn};
123 1 my $outputstr = Conv( $self->{from_table}, $self->{to_table}, $warn, $inputstr );
124 1 return $outputstr;
125 } # switch_brl_char_map
126  
127 =head2 parse_dis
128  
129 Parses a liblouis display table file (.dis) and return a hash with the
130 characters and dots respectively.
131  
132 =cut
133  
134 4 ▶sub parse_dis {
Subroutine parse_dis: called CC=11 SCAR=24.0
Pod documented
135 4 my $fileName = shift;
136 4 my ($char, $dots, %table);
137 4 ▶ open( DIS, "<", $fileName) || croak "Error opening file $fileName;";
Branch: 1 branch
BranchTrueFalseTotal
open DIS, "<", $fileName or croak("Error opening file $fileName;") 2 2 4
138 2 ▶ while( my $line = <DIS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DIS)) 380 2 382
139 380 $char = '';
140 380 $dots = 0;
141 380 ▶ next unless( $line =~ /^display/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^display/i 60 320 380
142 320 ($char, $dots) = $line =~ /display\s+(\S+)\s+(\S+)/i;
143 320 ▶ if( $char =~ /\\s/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\\s/) 1 319 320
144 1 $char = " ";
145 }
146 320 ▶ if( length($char) >=4 ){ # $char is a hex value, not a char.
Branch: 1 branch
BranchTrueFalseTotal
if (length $char >= 4) 256 64 320
147 #$charhex = "u";
148 #$charhex = sprintf '%2.2x', unpack('U0U*', $char);
149 #$charhex .= sprintf "%04x", ord Encode::decode("UTF-8", $char);
150 256 $char =~ s/\\x//i;
151 256 $char =~ s/(....)/ pack( 'U*', hex($1))/ie;
152 }
153 320 ▶ if( !defined($table{$char})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table{$char}) 320 0 320
154 320 ▶ if( $dots =~ /^$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($dots =~ /^$/) 0 320 320
155 0 $dots = 0;
156 }
157 320 $char =~ s/^\\\\$/\\/;
158 320 $table{$char} = $dots;
159 }
160 }
161 2 close( DIS );
162  
163 2 my( $chr, $dts );
164 2 ▶ while( ($chr, $dts) = each (%table) ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %table) 15 0 15
165 15 $dts = $table{$chr};
166 15 ▶ next unless( $dts == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $dts == 1 13 2 15
167 2 last;
168 } # while
169 2 ▶ if( $chr =~ /⠁/ ){ # if dot 1 is x2801
Branch: 1 branch
BranchTrueFalseTotal
if ($chr =~ /\x{2801}/) { } 1 1 2
170 1 $table{"⠀"} = 0; # inject unicode brl space
171 } else {
172 1 $table{" "} = "0";
173 } # if
174  
175 2 return( %table );
176 } # parse_dis
177  
178 =head2 Conv
179  
180 Converts a string, character by character, from %from_table to %to_table.
181  
182 =cut
183  
184 1 ▶sub Conv {
Subroutine Conv: called CC=12 SCAR=32.5
Pod documented
185 1 my %from_tab = %{shift()};
186 1 my %to_tab = %{shift()};
187 1 ▶ my $warn = shift unless $#_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 0 1 0 1
188 1 my $inputstr = shift;
189  
190 1 my( $dots, $outC, $outstr);
191 1 foreach my $inC (split( //, $inputstr )){
192 4 ▶ if( $inC =~ /([\r\n\f])/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($inC =~ /([\r\n\f])/) 0 4 4
193 0 $outstr .= $inC;
194 0 next;
195 } # if
196 4 ▶ if( !exists $from_tab{$inC} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $from_tab{$inC}) 0 4 4
197 0 $outstr .= $inC;
198 0 ▶ carp "Warning: Character $inC isn't defined in input table!\n" if( defined $warn && $warn != 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $warn and $warn != 0 0 0 0
Condition0%0/3
defined $warn and $warn != 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $warn and $warn != 0
defined $warn $warn != 0
Truth table0%0/3
A and B
A= defined $warnB= $warn != 0
ABresult
0X0
100
111
199 0 next;
200 }
201 4 $dots = 0;
202 4 $outC = '';
203 4 $dots = $from_tab{$inC};
204 4 for my $outkey (keys %to_tab) {
205 256 ▶ if( $to_tab{$outkey} =~ /^$dots$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($to_tab{$outkey} =~ /^$dots$/) 4 252 256
206 4 $outC = $outkey;
207 4 $outstr .= $outC;
208 }
209 }
210 4 ▶ if( $outC =~ /^$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($outC =~ /^$/) 0 4 4
211 0 $outstr .= $inC;
212 0 ▶ carp "Warning: Dots $dots isn't defined in output table!\n" if( defined $warn && $warn != 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $warn and $warn != 0 0 0 0
Condition0%0/3
defined $warn and $warn != 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $warn and $warn != 0
defined $warn $warn != 0
Truth table0%0/3
A and B
A= defined $warnB= $warn != 0
ABresult
0X0
100
111
213 }
214 }
215 1 return $outstr;
216 } # Conv
217  
218 =head1 AUTHOR
219  
220 Lars Bjørndal, C<< <lars at lamasti.net> >>
221  
222 =head1 BUGS
223  
224 Please report any bugs or feature requests to C<bug-app-brl2brl at rt.cpan.org>, or through
225 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-Brl2Brl>. I will be notified, and then you'll
226 automatically be notified of progress on your bug as I make changes.
227  
228  
229  
230  
231 =head1 SUPPORT
232  
233 You can find documentation for this module with the perldoc command.
234  
235 perldoc App::Brl2Brl
236  
237 and
238  
239 perldoc brl2brl
240  
241 You can also look for information at:
242  
243 =over 4
244  
245 =item * RT: CPAN's request tracker (report bugs here)
246  
247 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Brl2Brl>
248  
249 =item * CPAN Ratings
250  
251 L<https://cpanratings.perl.org/d/App-Brl2Brl>
252  
253 =item * Search CPAN
254  
255 L<https://metacpan.org/release/App-Brl2Brl>
256  
257 =back
258  
259  
260 =head1 ACKNOWLEDGEMENTS
261  
262  
263 =head1 LICENSE AND COPYRIGHT
264  
265 This software is Copyright (c) 2023 by Lars Bjørndal.
266  
267 This is free software, licensed under:
268  
269 The Artistic License 2.0 (GPL Compatible)
270  
271 It includes data files in the C<share> directory copied from
272 v3.26.0 of
273 L<liblouis|https://github.com/liblouis/liblouis/tree/v3.26.0/tables>.
274 Liblouis is free software licensed under the
275 L<GNU LGPLv2.1+|https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>
276 (see the file COPYING.LESSER).
277  
278 =cut
279  
280 2 1; # End of App::Brl2Brl
↑ Summary