The function ConvertUTF8TexttoBraille has been corrected, and now can translate full texts to the Braille code, including those with accented characters. The function ConvertUTF8HtmlTexttoBraille was also modified, but there are still some bugs to be corrected.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2057 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
anavaleije 2011-10-01 19:25:25 +00:00
parent a8e23840e6
commit e43937240d
2 changed files with 177 additions and 118 deletions

View File

@ -132,6 +132,10 @@
</Checks> </Checks>
</CodeGeneration> </CodeGeneration>
<Linking> <Linking>
<Debugging>
<GenerateDebugInfo Value="True"/>
<DebugInfoType Value="dsAuto"/>
</Debugging>
<Options> <Options>
<Win32> <Win32>
<GraphicApplication Value="True"/> <GraphicApplication Value="True"/>

View File

@ -27,19 +27,19 @@ type
function ConvertUTF8TextToBraille(Line: string): string; function ConvertUTF8TextToBraille(Line: string): string;
function ConvertUTF8HtmlTextToBraille(AInput: string): string; function ConvertUTF8HtmlTextToBraille(AInput: string): string;
function ConvertUTF8TextToBrailleNew(AInput: string): string;
implementation implementation
type type
dictionary = array[1..32] of string; dictionary = array[1..33] of string;
dictionary_pointer = ^dictionary;
const const
number_signal = chr($e2) + chr($a0) + chr($bc); number_signal = chr($e2) + chr($a0) + chr($bc);
caps_signal = chr($e2) + chr($a0) + chr($a8); caps_signal = chr($e2) + chr($a0) + chr($a8);
d1: dictionary = ({!} chr($96), {"} chr($a6), {#} 'TODO', {(*$*)} chr($b0), d1: dictionary = ({<space>} chr($80), {!} chr($96), {"} chr($a6), {#} 'TODO', {(*$*)} chr($b0),
{%} chr($b8) + chr($e2) + chr($a0) + chr($b4), {&} chr($af), {'} chr($84), {%} 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($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($94), {+} chr($96), {,} chr($82), {-} chr($a4), {.} chr($84),
@ -53,7 +53,7 @@ const
{j} chr($9a), {k} chr($85), {l} chr($87), {m} chr($8d), {n} chr($9d), {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), {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), {t} chr($9e), {u} chr($a5), {v} chr($a7), {w} chr($ba), {x} chr($ad),
{y} chr($bd), {z} chr($b5), '', '', '', '', '', ''); {y} chr($bd), {z} chr($b5), '', '', '', '', '', '', '');
d3: dictionary = ({a + grave} chr($ab), {a + acute} chr($b7), d3: dictionary = ({a + grave} chr($ab), {a + acute} chr($b7),
{a + circumflex} chr($a1), {a + tilde} chr($9c), {a + diaeresis} 'TODO', {a + circumflex} chr($a1), {a + tilde} chr($9c), {a + diaeresis} 'TODO',
@ -65,11 +65,7 @@ const
{o + circumflex} chr($b9), {o + tilde} chr($aa), {o + diaeresis} 'TODO', {o + circumflex} chr($b9), {o + tilde} chr($aa), {o + diaeresis} 'TODO',
{division sign} 'TODO', {o + stroke} 'TODO', {u + grave} chr($b1), {division sign} 'TODO', {o + stroke} 'TODO', {u + grave} chr($b1),
{u + acute} chr($be), {u + circumflex} 'TODO', {u + diaeresis} chr($b3), {u + acute} chr($be), {u + circumflex} 'TODO', {u + diaeresis} chr($b3),
{y + acute} 'TODO', {thorn} 'TODO', {y + diaeresis} 'TODO'); {y + acute} 'TODO', {thorn} 'TODO', {y + diaeresis} 'TODO', '');
function ConvertUTF8CharacterToBraille(AInput: string): string;
begin
end;
function ConvertUTF8TextToBrailleNew(AInput: string): string; function ConvertUTF8TextToBrailleNew(AInput: string): string;
var var
@ -87,7 +83,7 @@ begin
SetLength(lCurChar, lCharSize); SetLength(lCurChar, lCharSize);
Move(lCurChar[1], lInput^, lCharSize); Move(lCurChar[1], lInput^, lCharSize);
Result := Result + ConvertUTF8CharacterToBraille(lCurChar); {Result := Result + ConvertUTF8CharacterToBraille(lCurChar);}
Inc(lInput, lCharSize); Inc(lInput, lCharSize);
Inc(lPos, lCharSize); Inc(lPos, lCharSize);
@ -96,159 +92,218 @@ end;
function ConvertUTF8TextToBraille(Line: string): string; function ConvertUTF8TextToBraille(Line: string): string;
var var
Count, count_aux, n, n_aux, decr: integer; lCharSize, count, n, next_n, len: integer;
Braille_string, string_aux: string; Braille_string: string;
num, accented: boolean; Pline: Pchar;
dict_p: dictionary_pointer; num, caps: boolean;
f: TextFile;
begin begin
Braille_string := ''; Braille_string := '';
num := False; num := False;
accented := False; caps := False;
New(dict_p); count := 1;
decr := 0; AssignFile(f, 'test');
Count := 1; Rewrite(f);
n_aux := 0;
if Line = '' then Result := ''; if Line = '' then Result := '';
while Count <= length(Line) do len := length(Line);
Pline := PChar(Line);
while count <= len do
begin 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 lCharSize := LCLProc.UTF8CharacterLength(PLine);
{if the last iteration found n = 195, it means we have an accented character. Print it and go to next iteration}
if (lCharSize = 1) then
begin begin
dict_p^ := d3;
decr := 159; n := Ord(Line[count]);
n := Ord(Line[Count]);
if n < 160 then if (n = 9) then Braille_string := Braille_string + #9
{if it's a capital letter, put the caps signal and change to minuscule}
else
if (n = 10) then Braille_string := Braille_string + #10
else
begin
if ((n >= 97) and (n <= 122)) then {a lower case letter}
Braille_string := Braille_string + chr($e2) + chr($a0) + d2[n - 96]
else
begin
if ((n >= 65) and (n <= 90)) then {an upper case letter}
begin
if not caps then
begin begin
Braille_string := Braille_string + caps_signal; Braille_string := Braille_string + caps_signal;
n := n + 32; if (count + 2 < length(Line)) then
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 >= 97) and (n <= 122)) then {if the char is a letter, choose d2}
begin begin
dict_p^ := d2; next_n := Ord(Line[count + 1]);
decr := 96; if ((next_n >= 65) and (next_n <= 90)) or ((next_n = 195) and ((Ord(Line[count + 2]) >= 128) and (Ord(Line[count + 2]) <= 159))) then {if the next char is also upper case, add another caps signal}
end
else
if n = 195 then {if the char is an accented letter, flag it and go to next iteration}
begin begin
accented := True; Braille_string := Braille_string + caps_signal;
Count := Count + 1; caps := True;
continue; end;
end;
end;
Braille_string := Braille_string + chr($e2) + chr($a0) + d1[n - 31];
if (count + 1 <= length(Line)) then
begin
next_n := Ord(Line[count + 1]);
if not(((next_n >= 65) and (next_n <= 90)) or ((next_n = 195) and ((Ord(Line[count + 2]) >= 128) and (Ord(Line[count + 2]) <= 159)))) then caps := False; {if the next char is not upper case, unflag <caps>}
end;
end end
else else
if ((n >= 65) and (n <= 90)) 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 begin
dict_p^ := d2;
decr := 96 - 32;
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 >= 65) and (n_aux <= 90)) 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 := 32;
end;
if num and not (((n >= 48) and (n <= 57)) or (n = 46) or (n = 44)) then if (n >= 48) and (n <= 57) then {a number}
{if the whole number has already been printed, change num to False again}
num := False;
if not num and ((n >= 48) and (n <= 57)) then
{if it's the first number to appear, print the number signal first}
begin begin
Braille_string := Braille_string + number_signal; if not num then Braille_string := Braille_string + number_signal; {first algarism of a number, add a number signal}
num := True; num := True;
Braille_string := Braille_string + chr($e2) + chr($a0) + d1[n - 31];
if (count + 1 <= length(Line)) then
begin
next_n := Ord(Line[count + 1]);
if not ((next_n >= 48) and (next_n <= 57) or (next_n = 44) or (next_n = 46)) then num := False; {the next char is not a number, nor ',', nor '.', then unflag <num>}
end; end;
end
Braille_string := Braille_string + chr($e2) + chr($a0) + (dict_p^)[n - decr]; else
begin
Count := Count + 1; if (n >= 32) and (n <= 64) then {a char from the first dictionary}
begin
Braille_string := Braille_string + chr($e2) + chr($a0) + d1[n - 31];
if (count + 1 <= length(Line)) then
begin
next_n := Ord(Line[count + 1]);
if ((n = 44) or (n = 46)) and not ((next_n >= 48) and (next_n <= 57)) then num := False; {if the char is a ',' or a '.' but the next is not a number, unflag <num>}
end; end;
end;
end;
end;
end;
end;
end
else
begin
if (lCharSize = 2) then
begin
n := Ord(Line[count]);
if (n = 195) then
begin
n := Ord(Line[count + 1]);
if (n >= 128) and (n <= 159) then {upper case accented char}
begin
if not caps then
begin
Braille_string := Braille_string + caps_signal;
if (count + 2 <= length(Line)) then
begin
next_n := Ord(Line[count + 2]);
if ((next_n >= 65) and (next_n <= 90)) or ((next_n = 195) and ((Ord(Line[count + 2]) >= 128) and (Ord(Line[count + 2]) <= 159))) then {if the next char is also upper case, add another caps signal}
begin
Braille_string := Braille_string + caps_signal;
caps := True;
end;
end;
end;
Braille_string := Braille_string + chr($e2) + chr($a0) + d3[n - 127];
if (count + 3 <= length(Line)) then
begin
next_n := Ord(Line[count + 2]);
if not(((next_n >= 65) and (next_n <= 90)) or ((next_n = 195) and ((Ord(Line[count + 3]) >= 128) and (Ord(Line[count + 3]) <= 159)))) then {if the next char is not upper case, unflag <caps>}
caps := False;
end;
end
else
if (n >= 160) and (n <= 191) then Braille_string := Braille_string + chr($e2) + chr($a0) + d3[n - 159]; {lower case accented letter}
end;
end;
end;
count := count + lCharSize;
Inc(Pline, lCharSize);
end;
Close(f);
Result := Braille_string; Result := Braille_string;
end; end;
function ConvertUTF8HtmlTextToBraille(AInput: string): string; function ConvertUTF8HtmlTextToBraille(AInput: string): string;
var var
i: integer; i, j, lCharSize: integer;
output, aux_string: string; output, aux_string, end_string: string;
is_text: boolean; is_text: boolean;
page: string; Pline : PChar;
begin begin
page := AInput;
i := 1; i := 1;
output := ''; output := '';
aux_string := ''; aux_string := '';
is_text := True; is_text := False;
Pline := PChar(AInput);
while i <= length(page) do while i <= length(AInput) do
begin begin
while is_text and (i <= length(page)) do end_string := '';
while is_text and (i <= length(AInput)) do
begin begin
if (page[i] = '<') or (page[i] = '/') then if (AInput[i] = '<') then
begin begin
is_text := False; is_text := False;
break
end;
aux_string := aux_string + page[i];
i := i + 1; i := i + 1;
Inc(Pline, 1);
end_string := '<';
break;
end
else
begin
if ((AInput[i] = '/') and (AInput[i+1] = '/') and (AInput[i+2] = '<')) then
begin
is_text := False;
i := i + 3;
Inc(Pline, 3);
end_string := '//<';
break
end
end; end;
output := output + ConvertUTF8TextToBraille(aux_string); lCharSize := LCLProc.UTF8CharacterLength(Pline);
for j := 0 to lCharSize - 1 do aux_string := aux_string + AInput[i + j];
while (not is_text) and (i <= length(page))do i := i + lCharSize;
Inc(Pline, lCharSize);
end;
output := output + ConvertUTF8TextToBraille(aux_string) + end_string;
aux_string := '';
while (not is_text) and (i <= length(AInput))do
begin begin
if (page[i] = '>') then if (AInput[i] = '>') then
begin begin
is_text := True; is_text := True;
i := i + 1; i := i + 1;
Inc(Pline, 1);
output := output + '>'; output := output + '>';
break break
end; end;
output := output + page[i]; lCharSize := LCLProc.UTF8CharacterLength(Pline);
i := i + 1; for j := 0 to lCharSize - 1 do
output := output + AInput[i + j];
i := i + lCharSize;
Inc(Pline, lCharSize);
end; end;
end; end;
ConvertUTF8HtmlTextToBraille := output; ConvertUTF8HtmlTextToBraille := output;
end; end;