2011-09-15 01:20:50 +00:00
( * The function to_braille from this unit translates a string from UTF8 chars to Braille chars.
The dictionaries were taken from http: //www.ibc.gov.br/?catid=110&blogid=1&itemid=479 and from http://www.braillevirtual.fe.usp.br/pt/Portugues/braille.html
* )
Unit utf8_braille;
interface
Function Braille ( Line: string ) : string ;
Type
dictionary = array [ 1 .. 3 2 ] of string ;
dictionary_pointer = ^ dictionary;
Const
number_signal = chr( $e2 ) + chr( $a0 ) + chr( $bc ) ;
caps_signal = chr( $e2 ) + chr( $a0 ) + chr( $a8 ) ;
d1: dictionary = ( {!} chr( $96 ) , {"} chr( $a6 ) , {#} 'TODO' , {(*$*)} chr( $b0 ) , {%} chr( $b8 ) + chr( $e2 ) + chr( $a0 ) + chr( $b4 ) , {&} chr( $af ) , {'} chr( $84 ) , {(} chr( $a3 ) + chr( $e2 ) + chr( $a0 ) + chr( $84 ) , {)} chr( $a0 ) + chr( $e2 ) + chr( $a0 ) + chr( $9c ) , {*} chr( $94 ) , {+} chr( $96 ) , {,} chr( $82 ) , {-} chr( $a4 ) , {.} chr( $84 ) , {/} chr( $90 ) + chr( $e2 ) + chr( $a0 ) + chr( $b2 ) , {0} chr( $9a ) , {1} chr( $81 ) , {2} chr( $83 ) , {3} chr( $89 ) , {4} chr( $99 ) , {5} chr( $91 ) , {6} chr( $8b ) , {7} chr( $9b ) , {8} chr( $93 ) , {9} chr( $8a ) , {:} chr( $92 ) , {;} chr( $86 ) , {<} chr( $aa ) , {=} chr( $b6 ) , {>} chr( $95 ) , {?} chr( $a2 ) , {@} 'TODO' ) ;
d2: dictionary = ( {a} chr( $81 ) , {b} chr( $83 ) , {c} chr( $89 ) , {d} chr( $99 ) , {e} chr( $91 ) , {f} chr( $8b ) , {g} chr( $9b ) , {h} chr( $93 ) , {i} chr( $8a ) , {j} chr( $9a ) , {k} chr( $85 ) , {l} chr( $87 ) , {m} chr( $8d ) , {n} chr( $9d ) , {o} chr( $95 ) , {p} chr( $8f ) , {q} chr( $9f ) , {r} chr( $97 ) , {s} chr( $8e ) , {t} chr( $9e ) , {u} chr( $a5 ) , {v} chr( $a7 ) , {w} chr( $ba ) , {x} chr( $ad ) , {y} chr( $bd ) , {z} chr( $b5 ) , '' , '' , '' , '' , '' , '' ) ;
d3: dictionary = ( {a + grave} chr( $ab ) , {a + acute} chr( $b7 ) , {a + circumflex} chr( $a1 ) , {a + tilde} chr( $9c ) , {a + diaeresis} 'TODO' , {a + ring above} 'TODO' , {ae} 'TODO' , {c + cedilla} chr( $af ) , {e + grave} chr( $ae ) , {e + acute} chr( $bf ) , {e + circumflex} chr( $a3 ) , {e + diaeresis} 'TODO' , {i + grave} chr( $a9 ) , {i + acute} chr( $8c ) , {i + circumflex} 'TODO' , {i + diaeresis} chr( $bb ) , {eth} 'TODO' , {n + tilde} 'TODO' , {o + grave} chr( $ba ) , {o + acute} chr( $ac ) , {o + circumflex} chr( $b9 ) , {o + tilde} chr( $aa ) , {o + diaeresis} 'TODO' , {division sign} 'TODO' , {o + stroke} 'TODO' , {u + grave} chr( $b1 ) , {u + acute} chr( $be ) , {u + circumflex} 'TODO' , {u + diaeresis} chr( $b3 ) , {y + acute} 'TODO' , {thorn} 'TODO' , {y + diaeresis} 'TODO' ) ;
implementation
Function Braille ( Line: string ) : string ;
Var
count, count_aux, n, n_aux, decr: integer ;
Braille_string, string_aux: string ;
num, accented: boolean ;
dict_p: dictionary_pointer;
begin
Braille_string : = '' ;
num : = False ;
accented : = False ;
New( dict_p) ;
decr : = 0 ;
count : = 1 ;
n_aux : = 0 ;
if Line = '' then
Braille : = '' ;
while count < = length( Line) do
begin
if Line[ count] = ' ' then {reproduce the empty space and go to next iteration}
begin
Braille_string : = Braille_string + chr( $e2 ) + chr( $a0 ) + chr( $80 ) ;
count : = count + 1 ;
continue
end ;
if accented then {if the last iteration found n = 195, it means we have an accented character. Print it and go to next iteration}
begin
dict_p^ : = d3;
decr : = 1 5 9 ;
n : = ord( Line[ count] ) ;
if n < 1 6 0 then {if it's a capital letter, put the caps signal and change to minuscule}
begin
Braille_string : = Braille_string + caps_signal;
n : = n + 3 2
end ;
Braille_string : = Braille_string + chr( $e2 ) + chr( $a0 ) + ( dict_p^ ) [ n- decr] ;
accented : = False ;
count : = count + 1 ;
continue
end ;
n : = ord( Line[ count] ) ;
if ( ( n > = 9 7 ) and ( n < = 1 2 2 ) ) then {if the char is a letter, choose d2}
begin
dict_p^ : = d2;
decr : = 9 6
end
else
if n = 1 9 5 then {if the char is an accented letter, flag it and go to next iteration}
begin
accented : = True ;
count : = count + 1 ;
continue
end
else
if ( ( n > = 6 5 ) and ( n < = 9 0 ) ) then {if it's a capital letter, choose d2 and see if it's only the first letter or if there are more capital letters}
begin
dict_p^ : = d2;
decr : = 9 6 - 3 2 ;
string_aux : = chr( $e2 ) + chr( $a0 ) + ( dict_p^ ) [ n- decr] ;
count_aux : = count + 1 ;
if count_aux > length( Line) then
begin
Braille_string : = Braille_string + caps_signal + string_aux;
break
end ;
n_aux : = ord( Line[ count_aux] ) ;
while ( ( n_aux > = 6 5 ) and ( n_aux < = 9 0 ) ) do
begin
string_aux : = string_aux + chr( $e2 ) + chr( $a0 ) + ( dict_p^ ) [ n_aux- decr] ;
count_aux : = count_aux + 1 ;
if count_aux > length( Line) then
break;
n_aux : = ord( Line[ count_aux] ) ;
end ;
if length( string_aux) > 3 then
Braille_string : = Braille_string + caps_signal + caps_signal + string_aux
else
Braille_string : = Braille_string + caps_signal + string_aux;
count : = count_aux;
continue
end
else {or else, choose d1}
begin
dict_p^ : = d1;
decr : = 3 2
end ;
if num and not ( ( ( n > = 4 8 ) and ( n < = 5 7 ) ) or ( n = 4 6 ) or ( n = 4 4 ) ) then {if the whole number has already been printed, change num to False again}
num: = False ;
if not num and ( ( n > = 4 8 ) and ( n < = 5 7 ) ) then {if it's the first number to appear, print the number signal first}
begin
Braille_string : = Braille_string + number_signal;
num : = True ;
end ;
Braille_string : = Braille_string + chr( $e2 ) + chr( $a0 ) + ( dict_p^ ) [ n- decr] ;
count : = count + 1 ;
end ;
Braille : = Braille_string;
end ;
End .