|
@@ -8,11 +8,8 @@ unit UTF8BIDI;
|
|
|
|
|
|
interface
|
|
interface
|
|
|
|
|
|
-uses
|
|
|
|
- Classes, SysUtils;
|
|
|
|
-
|
|
|
|
type
|
|
type
|
|
- TChar = WideChar;
|
|
|
|
|
|
+ TUCS32Char = Cardinal;
|
|
TUTF8Char = String[3];
|
|
TUTF8Char = String[3];
|
|
TUTF8Str = UTF8String;
|
|
TUTF8Str = UTF8String;
|
|
TDirection=(
|
|
TDirection=(
|
|
@@ -21,49 +18,51 @@ type
|
|
drLTR
|
|
drLTR
|
|
);
|
|
);
|
|
|
|
|
|
-procedure insert(aChar:TChar;var uString:TUTF8Str; var CursorPos:Integer);
|
|
|
|
|
|
+function UnicodeToUtf8(aChar:TUCS32Char):TUTF8Char;
|
|
|
|
+function UnicodeToUtf8(aChar:WideChar):TUTF8Char;
|
|
|
|
+procedure insert(CharToInsert:TUTF8Char;var uString:TUTF8Str; var CursorPos:Integer);
|
|
|
|
|
|
implementation
|
|
implementation
|
|
|
|
|
|
-function UnicodeToUtf8(aChar:TChar):TUTF8Char;
|
|
|
|
-var
|
|
|
|
- w:Word;
|
|
|
|
|
|
+function UnicodeToUtf8(aChar:TUCS32Char):TUTF8Char;
|
|
begin
|
|
begin
|
|
- w:= Word(aChar);
|
|
|
|
- case w of
|
|
|
|
|
|
+ case aChar of
|
|
0..$7f:
|
|
0..$7f:
|
|
begin
|
|
begin
|
|
- Result[1]:=char(w);
|
|
|
|
|
|
+ Result[1]:=char(aChar);
|
|
SetLength(Result,1);
|
|
SetLength(Result,1);
|
|
end;
|
|
end;
|
|
$80..$7ff:
|
|
$80..$7ff:
|
|
begin
|
|
begin
|
|
- Result[1]:=char($c0 or (w shr 6));
|
|
|
|
- Result[2]:=char($80 or (w and $3f));
|
|
|
|
|
|
+ Result[1]:=char($c0 or (aChar shr 6));
|
|
|
|
+ Result[2]:=char($80 or (aChar and $3f));
|
|
SetLength(Result,2);
|
|
SetLength(Result,2);
|
|
end;
|
|
end;
|
|
else
|
|
else
|
|
begin
|
|
begin
|
|
- Result[1]:=char($e0 or (w shr 12));
|
|
|
|
- Result[2]:=char($80 or ((w shr 6)and $3f));
|
|
|
|
- Result[3]:=char($80 or (w and $3f));
|
|
|
|
|
|
+ Result[1]:=char($e0 or (aChar shr 12));
|
|
|
|
+ Result[2]:=char($80 or ((aChar shr 6)and $3f));
|
|
|
|
+ Result[3]:=char($80 or (aChar and $3f));
|
|
SetLength(Result,3);
|
|
SetLength(Result,3);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure insert(aChar:TChar;var uString:TUTF8Str; var CursorPos:Integer);
|
|
|
|
|
|
+function UnicodeToUtf8(aChar:WideChar):TUTF8Char;
|
|
|
|
+begin
|
|
|
|
+ UnicodeToUtf8(Word(aChar));
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+procedure insert(CharToInsert:TUTF8Char;var uString:TUTF8Str; var CursorPos:Integer);
|
|
var
|
|
var
|
|
{At beginning of the line we don't know which direction, thus the first
|
|
{At beginning of the line we don't know which direction, thus the first
|
|
character usually decides of paragrph direction}
|
|
character usually decides of paragrph direction}
|
|
dir:TDirection;
|
|
dir:TDirection;
|
|
LeftCursorPos, RightCursorPos, InsertPos:Integer;
|
|
LeftCursorPos, RightCursorPos, InsertPos:Integer;
|
|
- CharToInsert:TUTF8Char;
|
|
|
|
uLen:Integer;
|
|
uLen:Integer;
|
|
begin
|
|
begin
|
|
dir := drNONE;
|
|
dir := drNONE;
|
|
uLen := Length(uString);
|
|
uLen := Length(uString);
|
|
- CharToInsert := UnicodeToUTF8(aChar);
|
|
|
|
LeftCursorPos := 1;
|
|
LeftCursorPos := 1;
|
|
RightCursorPos := 1;
|
|
RightCursorPos := 1;
|
|
InsertPos := 1;
|
|
InsertPos := 1;
|