File: SBiDi.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (359 lines) | stat: -rw-r--r-- 8,108 bytes parent folder | download
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
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/**
 * I collected most of the Unicode Implicit bidi 
 * algorithm in this class, so it will be easier
 * to throw it away.
 */

#include "stoolkit/SBiDi.h"

/**
 * This is the segment, that can independently resolve types.
 */
SBiDiSegment::SBiDiSegment (SD_BiDiClass* _classes)
{
  classes = _classes;
  from = 0;
  till = 0;
  sor = true;
  eor = true;
}
SBiDiSegment::~SBiDiSegment()
{
}

static bool debugFlag = false;

/**
 * This will resolve the weak and the neutral types.
 * FIXME: This is the only thing that is not yet finished.
 */
void
SBiDiSegment::resolveWeakNeutral()
{
  unsigned int i;
  unsigned int j;

  SD_BiDiClass first = sor ? SD_BC_L : SD_BC_R;
  SD_BiDiClass last = eor ? SD_BC_L : SD_BC_R;

  if (debugFlag)
  {
    fprintf (stderr, "RES from=%u till=%u sor=%d eor=%d level=%u\n",
       from, till, (int)sor, (int)eor, level); 
  }

  /* W1 */
  SD_BiDiClass curr = first;
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_NSM)
    {
      classes[i] = curr;
    }
    else
    {
      curr = classes[i];
    }
  }
  /* W2 */
  /* Search backwards from each instance of a European number until the
   * first strong type (R,L AL or sor is found. If an AL is found, change
   * the type of the European number to Arabic number.
   * I think this is strange: in Arabic context, at the beginng of the line:
   * TSET CIBARA -10% 
   * TSET %10- CIBARA 
   * is the output. Isn't that strange? sor will never be AL....
   */
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_EN)
    {
      for (j=i; j>0; j--)
      {
        SD_BiDiClass c = classes[j-1];
        if (c==SD_BC_L || c==SD_BC_R || c==SD_BC_AL)
        {
          if (c==SD_BC_AL) classes[i] = SD_BC_AN;
          break;
        }
      }
      // first is never AL, it is either L or R
      //if (j==0) classes[i] = first;
    }
  }
  /* W3 */
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_AL) classes[i] = SD_BC_R;
  }
  /* W4 */
  for (i=from+1; i+1<till; i++)
  {
    if (classes[i] == SD_BC_ES)
    {
      if (classes[i-1] == SD_BC_EN && classes[i+1] == SD_BC_EN)
      {
        classes[i] = SD_BC_EN;
      }
      i++;
    }
    if (classes[i] == SD_BC_CS && i+1<till)
    {
      if (classes[i-1] == SD_BC_EN && classes[i+1] == SD_BC_EN)
      {
        classes[i] = SD_BC_EN;
      }
      if (classes[i-1] == SD_BC_AN && classes[i+1] == SD_BC_AN)
      {
        classes[i] = SD_BC_AN;
      }
      i++;
    }
  }
  /* W5 */
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_ET)
    {
      bool ok = false;
      /* find an EN. only ET is allowed. */
      for (j=i+1; j<till; j++)
      {
        if (classes[j] == SD_BC_EN)
        {
          ok = true;
          break;
        }
        if (classes[j] != SD_BC_ET) break;
      }
      if (ok)
      {
        while (i<j) classes[i++] = SD_BC_EN;
      }
      /* the routine below will continue if ok */
    }
    if (classes[i] == SD_BC_EN)
    {
      i++;
      while (i<till && classes[i] == SD_BC_ET)
      {
        classes[i] = SD_BC_EN;
      }
      /* increment in upper cycle */
      i--;
    }
  }

  /* W6 */
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_ET) classes[i] = SD_BC_ON;
    if (classes[i] == SD_BC_ES) classes[i] = SD_BC_ON;
    if (classes[i] == SD_BC_CS) classes[i] = SD_BC_ON;
  }
  /* W7 */
  for (i=from; i<till; i++)
  {
    if (classes[i] == SD_BC_EN)
    {
      for (j=i; j>0; j--)
      {
        SD_BiDiClass c = classes[j-1];
        if (c==SD_BC_L || c==SD_BC_R)
        {
          if (c==SD_BC_L) classes[i] = SD_BC_L;
          break;
        }
      }
      if (j==0 && first==SD_BC_L) classes[i] = first;
    }
  }
  /* Resolving Neutral Types */

  /* Neutral is non: R L AN EN */
  /* N1 */
  curr = first;
  for (i=from; i<till; i++)
  {
    SD_BiDiClass c = classes[i];
    if (c == SD_BC_EN || c==SD_BC_AN)
    {
      c = SD_BC_R;
    } 
    if (c==SD_BC_R ||  c==SD_BC_L)
    {
      curr = c; 
      continue;
    }
    /* This is the beginning of neutral sequence */
    SD_BiDiClass found = last; 
    for (j=i+1; j<till; j++)
    {
      SD_BiDiClass c0 = classes[j];
      if (c0 == SD_BC_EN || c0==SD_BC_AN)
      {
        c0 = SD_BC_R;
      }
      if (c0==SD_BC_R || c0==SD_BC_L)
      {
        found = c0; 
        break;
      }
    }
    if (found == curr)
    {
      while (i<j) classes[i++] = found;
      curr = found; 
    }
  }
  /* N2 */
  for (i=from; i<till; i++)
  {
    SD_BiDiClass c = classes[i];
    if (c == SD_BC_EN || c==SD_BC_AN) c = SD_BC_R;
    if (c!=SD_BC_R && c!=SD_BC_L)
    {
      classes[i] = ((level%2)==0) ? SD_BC_L : SD_BC_R;
    }
  }
}


/**
 * Create a new holder for the bidi algorithm.
 */
SBiDi::SBiDi (unsigned int _initLevel, unsigned int size)
{
  initLevel = _initLevel;
  lastLevel = _initLevel;

  /* To make things a bit faster we use arrays */
  classes = new SD_BiDiClass[size];
  CHECK_NEW (classes);

  lastSegment = new SBiDiSegment(classes);
  CHECK_NEW (lastSegment);
  lastSegment->sor = (initLevel%2) == 0;
  lastSegment->level = lastLevel;
  lastSize = 0;
}

SBiDi::~SBiDi ()
{
  delete classes;
  for (unsigned int i=0; i<segments.size(); i++)
  {
    delete segments[i];
  }
  if (lastSegment) delete lastSegment;
}

void
SBiDi::append (unsigned int embedLevel, SD_BiDiClass bidiType)
{
  classes[lastSize] = bidiType;
  lastSize++;

  if (embedLevel == lastLevel)
  {
    lastSegment->till = lastSize;
    lastLevel = embedLevel;
    return;
  }
  /* change of level at the beginning */
  if (lastSegment->till==0)
  {
    lastSegment->sor = (lastLevel>embedLevel) 
       ? ((lastLevel %2)==0) : ((embedLevel%2)==0);
    lastSegment->till = lastSize;
    lastSegment->level = lastLevel;

    lastLevel = embedLevel;
    return;
  }
  /* finish old segment */
  lastSegment->eor = (lastLevel>embedLevel) 
     ? ((lastLevel %2)==0) : ((embedLevel%2)==0);
  lastSegment->till = lastSize-1; /* sorry last one does not belong to us */
  segments.append (lastSegment);


  /* new segment needed */
  bool sor = (lastLevel>embedLevel)
       ? ((lastLevel %2)==0) : ((embedLevel%2)==0);
  lastLevel = embedLevel;

  lastSegment = new SBiDiSegment(classes);
  CHECK_NEW (lastSegment);

  lastSegment->sor = sor;
  /* we incremented lastSize at the beginning */
  lastSegment->from = lastSize-1;
  lastSegment->till = lastSize;
  lastSegment->level = embedLevel;
}

void
SBiDi::resolveWeakNeutral()
{
  if (lastSize == 0) return;
  if (lastSegment)
  {
    lastSegment->eor = (lastLevel>initLevel) 
       ? ((lastLevel %2)==0) : ((initLevel%2)==0);
    segments.append (lastSegment);
    /* This is how I debug */
    if (segments.size()>1)
    {
      // debugFlag = true;
    }
    lastSegment = 0;
  }
  for (unsigned int i=0; i<segments.size(); i++)
  {
    segments[i]->resolveWeakNeutral();
  }
  if (segments.size()>1) debugFlag = false;
}

void
SBiDi::insertBN (unsigned int at)
{
  /* move up */
  for (unsigned int i=lastSize; i>at; i--)
  {
    classes[i] = classes[i-1];
  }
  lastSize++;
  //classes[at] = SD_BC_BN;

  /* make this stupid BN the type of the previous char */
  /* The standard does not require any particular placement */
  if (at == 0)
  {
    classes[at] = ((initLevel%2) == 0) ? SD_BC_L : SD_BC_R;
    return;
  }
  /* be the previous. */
  classes[at] = classes[at-1];
}