summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/objpas/freebidi.pp
blob: df36376d5f4b8ff0e27b846515981509178db684 (plain)
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
{
Author Mazen NEIFER
Licence LGPL
}
unit FreeBIDI;

{$mode objfpc}{$H+}

interface

type
  TCharacter = WideChar;
  TString = WideString;
  TDirection=(
    drNONE,
    drRTL,
    drLTR
  );
  TVisualToLogical = Array[Byte]Of Byte;
  TFontInfoPtr = Pointer;
  TCharWidthRoutine = function(Character:TCharacter;FontInfo:TFontInfoPtr):Integer;

var
  FontInfoPtr:TFontInfoPtr;
  CharWidth:TCharWidthRoutine;

{****************************Logical aspects***********************************}
{Returns the number of logical characters}
function LLength(const Src:TString):Cardinal;
{Converts visual position to logical position}
function LPos(const Src:TString; vp:Integer; pDir:TDirection):Cardinal;
{****************************Visual aspects************************************}
{Returns the number of visual characters}
function VLength(const Src:TString; pDir:TDirection):Cardinal;
{Converts a logical position to a visual position}
function VPos(const Src:TString; lp:Integer; pDir, cDir:TDirection):Cardinal;
function VPos(UTF8Char:PChar; Len:integer; BytePos:integer):Cardinal;
{Returns character at a given visual position according to paragraph direction}
function VCharOf(Src:TString; vp:Integer; dir:TDirection):TCharacter;
{Inserts a string into another paying attention of RTL/LTR direction}
procedure VInsert(const Src:TString; var Dest:TString; vp:Integer; pDir:TDirection);
{Deletes a string into another paying attention of RTL/LTR direction}
procedure VDelete(var str:TString; vp, len:Integer; pDir:TDirection);
{Resturns a sub string of source string}
//function VCopy(const Src:TString; vStart, vWidth:Integer):TString;
{Resturns the visual image of current string}
function VStr(const Src:TString; pDir:TDirection):TString;
{****************************Helper routines***********************************}
{Returns direction of a character}
function DirectionOf(Character:TCharacter):TDirection;
{Returns contextual direction of caracter in a string}
function DirectionOf(Src:TString; 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:TCharacter; var Dest:TString; vp:Integer; pDir:TDirection):Integer;
{Returns a table mapping each visual position to its logical position in an UTF8*
string}
function VisualToLogical(const Src:TString; pDir:TDirection):TVisualToLogical;

implementation

function DefaultCharWidth(Character:TCharacter; FontInfoPtr:TFontInfoPtr):Integer;
begin
  case Character of
    #9:
      Result := 8;
  else
    Result := 1;
  end;
end;
function DumpStr(const Src:TString):String;
var
  i:Integer;
begin
  Result := '';
  for i:= 1 to Length(Src) do
    case Src[i] of
      #0..#127:
         Result := Result + Src[i];
    else
      Result := Result + '$' + HexStr(Ord(Src[i]),4);
    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;

{****************************Logical aspects***********************************}
function LLength(const Src:TString):Cardinal;
begin
  Result := Length(Src);
end;

function LPos(const Src:TString; vp:Integer; pDir:TDirection):Cardinal;
var
  v2l:TVisualToLogical;
  i:integer;
begin
  v2l := VisualToLogical(Src, pDir);
  if vp <= v2l[0]
  then
    Result := v2l[vp]
  else
    Result := Length(Src) + 1;
end;

{****************************Visual aspects************************************}
function VLength(const Src:TString; pDir:TDirection):Cardinal;
var
  Count:Integer;
begin
  Result := 0;
  Count := Length(Src);
  while (Count > 0) do
  begin
    Result := Result + CharWidth(Src[Count], FontInfoPtr);
    Count := Count - 1;
  end;
end;

function VPos(const Src:TString; lp:Integer; pDir, cDir:TDirection):Cardinal;
var
  v2l:TVisualToLogical;
  vp:Integer;
begin
  v2l := VisualToLogical(Src, 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(Src:TString; vp:Integer; dir:TDirection):TCharacter;
var
  CharLen: LongInt;
begin
  Result := Src[LPos(Src, vp, dir)];
end;

{****************************Helper routines***********************************}
function DirectionOf(Character:TCharacter):TDirection;
begin
  case Character of
    #9,#32,
    '/',
    '{','}',
    '[',']',
    '(',')':
      Result := drNONE;
    #$0590..#$05FF,      //Hebrew
    #$0600..#$06FF:      //Arabic
      Result := drRTL;
  else
    Result := drLTR;
  end;
end;

function DirectionOf(Src:TString; lp:Integer; pDir:TDirection):TDirection;
var
  c:TCharacter;
  lDir,rDir:TDirection;
  p:Integer;
begin
  if(lp <= 0)
  then
    lp := 1;
{Seek for proper character direction}
  c := Src[lp];
  lDir := DirectionOf(c);
{Seek for left character direction if it is neutral}
  p := lp;
  while(p > 1) and (lDir = drNONE)do
  begin
    c := Src[p - 1];
    lDir := DirectionOf(c);
    p := p - Length(c);
  end;
{Seek for right character direction if it is neutral}
  p := lp;
  repeat
    c := Src[p];
    rDir := DirectionOf(c);
    p := p + Length(c);
  until(p > Length(Src)) or (rDir <> drNONE);
  if(lDir = rDir)
  then
    Result := rDir
  else
    Result := pDir;
end;

function VisualToLogical(const Src:TString; 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:TCharacter;
i:Integer;
begin
  Result[0] := 0;
  lp := 1;
  vp := 1;
  lDir := drNONE;
  while lp <= Length(Src) do
  begin
    Character := Src[lp];
    cDir := DirectionOf(Src, 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);
    lp := lp + 1;
  end;
end;

function InsertChar(Src:TCharacter; var Dest:TString; vp:Integer; pDir:TDirection):Integer;
var
  vSrc,vDest:TString;
begin
  vSrc := VStr(Src,pDir);
  vDest := VStr(Dest,pDir);
  Insert(vSrc, vDest, vp);
  Dest := VStr(vDest, pDir);
  case DirectionOf(Src) of
  drRTL:
    Result := vp;
  drLTR:
    Result := vp + 1;
  else
    if(vp < Length(vDest)) and (DirectionOf(vDest[vp + 1]) = drRTL)
    then
      Result := vp
    else
      Result := vp + 1;
  end;
end;

procedure VInsert(const Src:TString;var Dest:TString; vp:Integer; pDir:TDirection);
var
  vSrc,vDest:TString;
begin
  vSrc := VStr(Src,pDir);
  vDest := VStr(Dest,pDir);
  Insert(vSrc, vDest, vp);
  Dest := VStr(vDest, pDir);
end;

procedure VDelete(var str:TString; vp, len:Integer; pDir:TDirection);
var
  v2l:TVisualToLogical;
  i:Integer;
begin
  v2l := VisualToLogical(str, pDir);
  for i := 1 to v2l[0] do
    if(v2l[i] >= vp) and (v2l[i] < vp + len)
    then
      Delete(str, v2l[i], 1);
end;

function VStr(const Src:TString; pDir:TDirection):TString;
var
  v2lSrc:TVisualToLogical;
  vp:Integer;
begin
  v2lSrc := VisualToLogical(Src,pDir);
  SetLength(Result, v2lSrc[0]);
  for vp := 1 to v2lSrc[0] do
    Result[vp] := Src[v2lSrc[vp]];
end;

initialization

  CharWidth := @DefaultCharWidth;

end.