1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
{
Author Mazen NEIFER
Licence LGPL
}
unit UTF8BIDI;
{$mode objfpc}{$H+}
interface
uses
FreeBIDI;
type
TUCS32Char = Cardinal;
TUCS16Char = Word;
TUTF8Char = String[4];
TUTF8String = UTF8String;
{****************************Conversion routines*******************************}
{Converts an UCS 16/32 bits charcater to UTF8 character}
function UnicodeToUTF8(aChar:TUCS32Char):TUTF8Char;
{Converts a wide char UCS 16 bits chcarcter to UTF8 character}
function UnicodeToUTF8(aChar:WideChar):TUTF8Char;
{Converts a wide char UCS 16 bits string to UTF8 character}
function UnicodeToUTF8(const Src:TString):TUTF8String;
{Converts an UTF8 character to UCS 32 bits character}
function UTF8ToUCS32(const UTF8Char:TUTF8Char):TUCS32Char;
{Converts an UTF8 character to UCS 16 bits character}
function UTF8ToUCS16(const UTF8Char:TUTF8Char):TUCS16Char;
{Converts an UTF8 string to UCS 16 bits string}
function UTF8ToUnicode(const Src:TUTF8String):TString;
{Converts an UTF8 string to a double byte string}
function UTF8ToDoubleByteString(const UTF8Str:TUTF8String):String;
function UTF8ToDoubleByte(UTF8Str:PChar; Len:Cardinal; DBStr:PByte):Cardinal;
{****************************Logical aspects***********************************}
{Returns the number of logical characters}
function LLength(const UTF8Str:TUTF8String):Cardinal;
{Converts visual position to logical position}
function LPos(const UTF8Str:TUTF8String; vp:Integer; pDir:TDirection):Cardinal;
{Returns character at a given logical position according to paragraph direction}
function LCharOf(UTF8String:TUTF8String; lp:Integer):TUTF8Char;
{****************************Visual aspects************************************}
{Returns the number of visual characters}
function VLength(const Src:TUTF8String; pDir:TDirection):Cardinal;
{Converts a logical position to a visual position}
function VPos(const UTF8Str:TUTF8String; lp:Integer; pDir, cDir:TDirection):Cardinal;
{Returns character at a given visual position according to paragraph direction}
function VCharOf(UTF8Str:TUTF8String; vp:Integer; dir:TDirection):TUTF8Char;
{Inserts a string into another paying attention of RTL/LTR direction}
procedure VInsert(const Src:TUTF8String; var Dest:TUTF8String; vp:Integer; pDir:TDirection);
{Deletes a string into another paying attention of RTL/LTR direction}
procedure VDelete(var str:TUTF8String; vp, len:Integer; pDir:TDirection);
{****************************Helper routines***********************************}
{Returns direction of a character}
function DirectionOf(Character:TUTF8Char):TDirection;
{Returns contextual direction of caracter in a string}
function DirectionOf(UTF8String:TUTF8String; lp:Integer; pDir:TDirection):TDirection;
{Inserts a char as if it was typed using keyboard in the most user friendly way.
Returns the new cursor position after insersion depending on the new visual text}
function InsertChar(Src:TUTF8Char; var Dest:TUTF8String; vp:Integer; pDir:TDirection):Integer;
{Returns a table mapping each visual position to its logical position in an UTF8*
string}
function VisualToLogical(const UTF8String:TUTF8String; pDir:TDirection):TVisualToLogical;
implementation
function DumpStr(const s:TUTF8String):String;
var
i:Integer;
begin
Result := '';
for i:= 1 to Length(s) do
case s[i] of
#0..#127:
Result := Result + s[i];
else
Result := Result + '$' + HexStr(Ord(s[i]),2);
end;
end;
function ComputeCharLength(p:PChar):Cardinal;
begin
if ord(p^)<%11000000
then
{regular single byte character (#0 is a normal char, this is UTF8Charascal ;)}
Result:=1
else if ((ord(p^) and %11100000) = %11000000)
then
if (ord(p[1]) and %11000000) = %10000000 then
Result:=2
else
Result:=1
else if ((ord(p^) and %11110000) = %11100000)
then
if ((ord(p[1]) and %11000000) = %10000000)
and ((ord(p[2]) and %11000000) = %10000000)
then
Result:=3
else
Result:=1
else if ((ord(p^) and %11111000) = %11110000)
then
if ((ord(p[1]) and %11000000) = %10000000)
and ((ord(p[2]) and %11000000) = %10000000)
and ((ord(p[3]) and %11000000) = %10000000)
then
Result:=4
else
Result:=1
else
Result:=1
end;
{****************************Conversion routines*******************************}
function UnicodeToUTF8(aChar:TUCS32Char):TUTF8Char;
begin
case aChar of
0..$7f:
begin
Result[1]:=char(aChar);
SetLength(UnicodeToUTF8,1);
end;
$80..$7ff:
begin
Result[1]:=char($c0 or (aChar shr 6));
Result[2]:=char($80 or (aChar and $3f));
SetLength(UnicodeToUTF8,2);
end;
$800..$ffff:
begin
SetLength(Result,3);
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));
end;
$10000..$1fffff:
begin
SetLength(UnicodeToUTF8,4);
Result[1]:=char($f0 or (aChar shr 18));
Result[2]:=char($80 or ((aChar shr 12) and $3f));
Result[3]:=char($80 or ((aChar shr 6) and $3f));
Result[4]:=char($80 or (aChar and $3f));
end;
else
SetLength(UnicodeToUTF8, 0);
end;
end;
function UnicodeToUTF8(aChar:WideChar):TUTF8Char;
var
c:TUCS16Char absolute aChar;
begin
case c of
0..$7f:
begin
Result[1]:=char(c);
SetLength(UnicodeToUTF8,1);
end;
$80..$7ff:
begin
Result[1]:=char($c0 or (c shr 6));
Result[2]:=char($80 or (c and $3f));
SetLength(UnicodeToUTF8,2);
end;
else
SetLength(UnicodeToUTF8, 0);
end;
end;
function UnicodeToUTF8(const Src:TString):TUTF8String;
var
vp:Integer;
begin
vp := 1;
Result := '';
for vp := 1 to Length(Src) do
Result := Result + UnicodeToUTF8(Src[vp]);
end;
function UTF8ToUCS32(const UTF8Char:TUTF8Char):TUCS32Char;
begin
case ComputeCharLength(@UTF8Char[1]) of
1:{regular single byte character (#0 is a normal char, this is UTF8Charascal ;)}
Result := ord(UTF8Char[1]);
2:
Result := ((ord(UTF8Char[1]) and %00011111) shl 6)
or (ord(UTF8Char[2]) and %00111111);
3:
Result := ((ord(UTF8Char[1]) and %00011111) shl 12)
or ((ord(UTF8Char[1]) and %00111111) shl 6)
or (ord(UTF8Char[2]) and %00111111);
4:
Result := ((ord(UTF8Char[1]) and %00011111) shl 18)
or ((ord(UTF8Char[2]) and %00111111) shl 12)
or ((ord(UTF8Char[3]) and %00111111) shl 6)
or (ord(UTF8Char[4]) and %00111111);
else
Result := $FFFFFFFF;
end
end;
function UTF8ToUCS16(const UTF8Char:TUTF8Char):TUCS16Char;
begin
case Length(UTF8Char) of
1:{regular single byte character (#0 is a normal char, this is UTF8Charascal ;)}
Result := ord(UTF8Char[1]);
2:
Result := ((ord(UTF8Char[1]) and %00011111) shl 6)
or (ord(UTF8Char[2]) and %00111111);
else
Result := $FFFF;
end;
end;
function UTF8ToUnicode(const Src:TUTF8String):TString;
var
lp, vp:Integer;
c:TUTF8Char;
begin
lp := 1;
vp := 0;
SetLength(Result, Length(Src));
while lp <= Length(Src) do
begin
vp := vp + 1;
c := LCharOf(Src, lp);
Result[vp] := WideChar(UTF8ToUCS16(c));
lp := lp + Length(c);
end;
SetLength(Result, vp);
end;
function UTF8ToDoubleByteString(const UTF8Str: TUTF8String): string;
var
Len: Integer;
begin
Len:=VLength(UTF8Str, drLTR);
SetLength(Result,Len*2);
if Len=0 then exit;
UTF8ToDoubleByte(PChar(UTF8Str),length(UTF8Str),PByte(Result));
end;
function UTF8ToDoubleByte(UTF8Str: PChar; Len:Cardinal; DBStr: PByte):Cardinal;
var
SrcPos: PChar;
CharLen: LongInt;
DestPos: PByte;
u: Cardinal;
begin
SrcPos:=UTF8Str;
DestPos:=DBStr;
Result:=0;
while Len>0 do begin
u:=UTF8ToUCS32(SrcPos);
DestPos^:=byte((u shr 8) and $ff);
inc(DestPos);
DestPos^:=byte(u and $ff);
inc(DestPos);
inc(SrcPos,CharLen);
dec(Len,CharLen);
inc(Result);
end;
end;
{****************************Logical aspects***********************************}
function LLength(const UTF8Str:TUTF8String):Cardinal;
begin
Result := Length(UTF8Str);
end;
function LPos(const UTF8Str:TUTF8String; vp:Integer; pDir:TDirection):Cardinal;
var
v2l:TVisualToLogical;
i:integer;
begin
v2l := VisualToLogical(UTF8Str, pDir);
if vp <= v2l[0]
then
Result := v2l[vp]
else
Result := Length(UTF8Str) + 1;
end;
function LCharOf(UTF8String:TUTF8String; lp:Integer):TUTF8Char;
begin
if lp > Length(UTF8String)
then
Exit('');
while(lp > 0) and ((Ord(UTF8String[lp]) and $F0) in [$80..$B0]) do
begin
Dec(lp);
end;
if lp = 0
then
Exit('');
Move(UTF8String[lp], Result[1], SizeOf(TUTF8Char) - 1);
SetLength(Result, ComputeCharLength(@Result[1]));
end;
{****************************Visual aspects************************************}
function VLength(const Src:TUTF8String; pDir:TDirection):Cardinal;
begin
Result := FreeBIDI.VLength(UTF8ToUnicode(Src), pDir);
end;
function VPos(const UTF8Str:TUTF8String; lp:Integer; pDir, cDir:TDirection):Cardinal;
var
v2l:TVisualToLogical;
vp:Integer;
begin
v2l := VisualToLogical(UTF8Str, pDir);
for vp := 1 to v2l[0] do
if lp = v2l[vp]
then
begin
Exit(vp);
end;
Result := v2l[0];
end;
function VPos(UTF8Char:PChar; Len:integer; BytePos:integer):Cardinal;
begin
end;
function VCharOf(UTF8Str:TUTF8String; vp:Integer; dir:TDirection):TUTF8Char;
var
CharLen: LongInt;
begin
Result:=LCharOf(UTF8Str,LPos(UTF8Str, vp, dir));
end;
{****************************Helper routines***********************************}
function DirectionOf(Character:TUTF8Char):TDirection;
begin
case Character[1] of
#9,#32,
'/',
'{','}',
'[',']',
'(',')':
Result := drNONE;
#$D8,#$D9:
Result := drRTL;
else
Result := drLTR;
end;
end;
function DirectionOf(UTF8String:TUTF8String; lp:Integer; pDir:TDirection):TDirection;
var
c:TUTF8Char;
lDir,rDir:TDirection;
p:Integer;
begin
if(lp <= 0)
then
lp := 1;
{Seek for proper character direction}
c := LCharOf(UTF8String, lp);
lDir := DirectionOf(c);
{Seek for left character direction if it is neutral}
p := lp;
while(p > 1) and (lDir = drNONE)do
begin
c := LCharOf(UTF8String, p - 1);
lDir := DirectionOf(c);
p := p - Length(c);
end;
{Seek for right character direction if it is neutral}
p := lp;
repeat
c := LCharOf(UTF8String, p);
rDir := DirectionOf(c);
p := p + Length(c);
until(p > Length(UTF8String)) or (rDir <> drNONE);
if(lDir = rDir)
then
Result := rDir
else
Result := pDir;
end;
function VisualToLogical(const UTF8String:TUTF8String; pDir:TDirection):TVisualToLogical;
procedure Insert(value:Byte; var v2l:TVisualToLogical; InsPos:Byte);
var
l:Byte;
begin
if v2l[0] < 255
then
Inc(InsPos);
if InsPos > v2l[0]
then
InsPos := v2l[0];
for l := v2l[0] downto InsPos do
v2l[l] := v2l[l-1];
v2l[InsPos] := Value;
end;
var
lp, vp : Integer;
cDir,lDir:TDirection;
Character:TUTF8Char;
i:Integer;
begin
Result[0] := 0;
lp := 1;
vp := 1;
lDir := drNONE;
while lp <= Length(UTF8String) do
begin
Character := LCharOf(UTF8String, lp);
cDir := DirectionOf(UTF8String, lp, pDir);
Inc(Result[0]);
case cDir of
drRTL:
begin
lDir := drRTL;
end;
drLTR:
begin
lDir := drLTR;
vp := Result[0];
end;
else
vp := Result[0];
end;
Insert(lp, Result, vp);
Inc(lp, Length(Character));
end;
end;
function InsertChar(Src:TUTF8Char; var Dest:TUTF8String; vp:Integer; pDir:TDirection):Integer;
var
temp:TString;
c:TCharacter;
begin
temp := UTF8ToUnicode(Dest);
c := WideChar(UTF8ToUCS16(Src));
Result := FreeBIDI.InsertChar(c, temp, vp, pDir);
Dest := UnicodeToUTF8(temp);
end;
procedure VInsert(const Src:TUTF8String;var Dest:TUTF8String; vp:Integer; pDir:TDirection);
function VStr(const Src:TUTF8String; pDir:TDirection):TUTF8String;
var
v2lSrc:TVisualToLogical;
i:Integer;
begin
v2lSrc := VisualToLogical(Src,pDir);
Result := '';
for i := 1 to v2lSrc[0] do
Result := Result + LCharOf(Src,v2lSrc[i]);
end;
var
vSrc,vDest:TUTF8String;
begin
vSrc := VStr(Src,pDir);
vDest := VStr(Dest,pDir);
Insert(vSrc, vDest, vp);
Dest := VStr(vDest, pDir);
end;
procedure VDelete(var str:TUTF8String; vp, len:Integer; pDir:TDirection);
var
temp:TString;
begin
temp := UTF8ToUnicode(str);
FreeBIDI.VDelete(temp, vp, len, pDir);
str := UnicodeToUTF8(temp);
end;
end.
|