summaryrefslogtreecommitdiff
path: root/modules/arabic/arabic-ot.c
blob: ba8e9bad68cd804b889de8d3b6db85d851b34423 (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
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
/* This file is taken from the FreeType (1) tree. It's been reindented
 * to roughly match Pango guidelines (in anticipation of future changes),
 * but not otherwise much altered.
 */

/****************************************************************************/
/*                                                                          */
/*  The FreeType project -- a free and portable quality TrueType renderer.  */
/*                                                                          */
/*  Copyright 1996-2000 by                                                  */
/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
/*                                                                          */
/*  arabic -- An implementation of the contextual algorithm given in the    */
/*  Unicode 2.0 book to assign the `isolated', `initial', `medial', and     */
/*  `final' properties to an input string of character codes for the Arabic */
/*  script.                                                                 */
/*                                                                          */
/*  This file is part of the FreeType project, and may only be used         */
/*  modified and distributed under the terms of the FreeType project        */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute      */
/*  this file you indicate that you have read the license and               */
/*  understand and accept it fully.                                         */
/*                                                                          */
/* The code, like the FreeType code it is derived from is dual-licensed     */
/* under the GNU General Public License and the FreeType license. See       */
/* pango/opentype/COPYING for full details of this licensing scheme.        */
/****************************************************************************/

#include <config.h>

#include "arabic-ot.h"


/*
 *
 *     Here a table of the joining classes for characters in the range
 *     U+0620 - U+06FF and U+0750 - U+077F.
 *
 *    The following character also has a joining class:
 *
 *     U+200D  ZERO WIDTH JOINER  -> causing
 *
 *    All other characters are given the joining class `none'.
 *
 */

static const joining_class  arabic[] =
{
  /* U+0620 */
  none, none, right, right,
  right, right, dual, right,
  dual, right, dual, dual,
  dual, dual, dual, right,

  /* U+0630 */
  right, right, right, dual,
  dual, dual, dual, dual,
  dual, dual, dual, none,
  none, none, none, none,

  /* U+0640 */
  causing, dual, dual, dual,
  dual, dual, dual, dual,
  right, dual, dual, transparent,
  transparent, transparent, transparent, transparent,

  /* U+0650 */
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, none,

  /* U+0660 */
  none, none, none, none,
  none, none, none, none,
  none, none, none, none,
  none, none, dual, dual,

  /* U+0670 */
  transparent, right, right, right,
  none, right, right, right,
  dual, dual, dual, dual,
  dual, dual, dual, dual,

  /* U+0680 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  right, right, right, right,
  right, right, right, right,

  /* U+0690 */
  right, right, right, right,
  right, right, right, right,
  right, right, dual, dual,
  dual, dual, dual, dual,

  /* U+06A0 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,

  /* U+06B0 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,

  /* U+06C0 */
  right, dual, right, right,
  right, right, right, right,
  right, right, right, right,
  dual, right, dual, right,

  /* U+06D0 */
  dual, dual, right, right,
  none, right, none, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,

  /* U+06E0 */
  transparent, transparent, transparent, transparent,
  transparent, none, none, transparent,
  transparent, none, transparent, transparent,
  transparent, transparent, right, right,

  /* U+06F0 */
  none, none, none, none,
  none, none, none, none,
  none, none, dual, dual,
  dual, none, none, dual
};

static const joining_class  arabic_supplement[] =
{
  /* U+0750 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, right, right, right,
  dual, dual, dual, dual,

  /* U+0760 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, right,
  right, dual, none, none,

  /* U+0770 */
  none, none, none, none,
  none, none, none, none,
  none, none, none, none,
  none, none, none, none
};

/*     Here a table of the joining classes for characters in the range
 *     U+07C0 - U+07FF.
 *
 *     The following character also has a joining class:
 *
 *     U+200C  ZERO WIDTH NON-JOINER  -> causing
 *
 *     All other characters are given the joining class `none'.
 */
static const joining_class nko[] =
{
  /* U+07C0 */
  none, none, none, none,
  none, none, none, none,
  none, none, dual, dual,
  dual, dual, dual, dual,

  /* U+07D0 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, dual,

  /* U+07E0 */
  dual, dual, dual, dual,
  dual, dual, dual, dual,
  dual, dual, dual, transparent,
  transparent, transparent, transparent, transparent,

  /* U+07F0 */
  transparent, transparent, transparent, transparent,
  none, none, none, none,
  none, none, causing, none,
  none, none, none, none,
};

#if 0
struct cgc_
{
  FT_UShort  char_code;
  FT_UShort  glyph_index;
  FT_UShort  class;
};

typedef struct cgc_  cgc;

int  compare_cgc (const void*  a,
		  const void*  b)
{
  return (((cgc*)a)->glyph_index > ((cgc*)b)->glyph_index) ?
    1 : ((((cgc*)a)->glyph_index == ((cgc*)b)->glyph_index) ?
	  0 : -1);
}


TT_Error  Build_Arabic_Glyph_Properties (TT_CharMap        char_map,
					 TT_UShort         max_glyphs,
					 TTO_GDEFHeader**  gdef)
{
  TT_UShort  i, j, num_glyphs;

  cgc        Arabic[0x0700 - 0x0620];

  TT_UShort  glyph_indices[0x700 - 0x0620];
  TT_UShort  classes[0x700 - 0x0620];

  if (!gdef)
    return TT_Err_Invalid_Argument;

  j = 0;

  for (i = 0x0620; i < 0x0700; i++)
    {
      Arabic[j].char_code   = i;
      Arabic[j].class       = (arabic[i - 0x0620] == transparent) ?
	MARK_GLYPH : SIMPLE_GLYPH;
      Arabic[j].glyph_index = TT_Char_Index (char_map, i);
      if (Arabic[j].glyph_index)
	j++;
    }
  num_glyphs = j;

  if (!num_glyphs)
    {
      /* no Arabic font */
      *gdef = NULL;
      return TT_Err_Ok;
    }

  /* sort it */

  qsort (Arabic, num_glyphs, sizeof (cgc), compare_cgc);

  /* write it to the arrays, removing duplicates */

  glyph_indices[0] = Arabic[0].glyph_index;
  classes[0]       = Arabic[0].class;

  j = 1;

  for (i = 1; i < num_glyphs; i++)
    {
      glyph_indices[j] = Arabic[i].glyph_index;
      classes[j]       = Arabic[i].class;

      if (glyph_indices[j - 1] != glyph_indices[j])
	j++;
    }
  num_glyphs = j;

  TT_GDEF_Build_ClassDefinition (*gdef, max_glyphs, num_glyphs,
				 glyph_indices, classes);

  return TT_Err_Ok;
}
#endif

/* The joining rules as given in the Unicode 2.0 book (characters are
 * here specified as appearing in the byte stream, i.e. *not* in
 * visual order).  Joining classes are given in angle brackets, glyph
 * forms in square brackets.  Glyphs affected by a specific rule are
 * enclosed with vertical bars.
 *
 * Note: The description of the joining algorithm in the book is
 *       severely broken.  You can get a corrected version from
 *       www.unicode.org (as of 29-Jun-1999, this hasn't appeared).
 *
 *   R1: <anything1> <transparent> <anything2>
 *
 *         apply joining rules for
 *           <anything1> <anything2> -> [shape1] [shape2]
 *
 *       -> [shape1] [isolated] [shape2]
 *
 *   R2: <causing|left|dual> |<right>|
 *
 *       -> [final]
 *
 *   R3: |<left>| <causing|right|dual>
 *
 *       -> [initial]
 *
 *   R4: <causing|left|dual> |<dual>| <causing|right|dual>
 *
 *       -> [medial]
 *
 *   R5: <causing|left|dual> |<dual>| <!(causing|right|dual)>
 *
 *       -> [final]
 *
 *   R6: <!(causing|left|dual)> |<dual>| <causing|right|dual>
 *
 *       -> [initial]
 *
 *   R7: If R1-R6 fail:
 *
 *       <anything> -> [isolated]
 */

/* `direction' can be -1, 0, or 1 to indicate the last non-transparent
 * glyph, the current glyph, and the next non-transparent glyph,
 * respectively.
 */

static joining_class  Get_Joining_Class (gunichar*   string,
					 int         pos,
					 int         length,
					 int         direction,
					 gboolean    reverse)
{
  joining_class  j;


  while (1)
    {
      if (pos == 0 && direction < 0)
	return none;

      pos += direction;

      if (pos >= length)
	return none;

      if (string[pos] >= 0x0620 &&
	  string[pos] < 0x0700)
	j = arabic[string[pos] - 0x0620];
      else if (string[pos] >= 0x0750 &&
	  string[pos] < 0x0780)
	j = arabic_supplement[string[pos] - 0x0750];
      else if (string[pos] >= 0x07C0 &&
	  string[pos] < 0x0800)
	j = nko[string[pos] - 0x07C0];
      else if (string[pos] == 0x200D)
	return causing;
      else
	return none;

      if (!direction || j != transparent)
        {
	  if (G_UNLIKELY (reverse))
	    return j == right ? left : j == left ? right : j;
	  else
	    return j;
	}
    }
}


FT_Error  Arabic_Assign_Properties (gunichar    *string,
				    gulong      *properties,
				    int          length,
				    gboolean     reverse)
{
  joining_class  previous, current, next;
  int      i;

  if (!string || !properties || length == 0)
    return FT_Err_Invalid_Argument;

  for (i = 0; i < length; i++)
    {
      previous = Get_Joining_Class (string, i, length, -1, reverse);
      current  = Get_Joining_Class (string, i, length,  0, reverse);
      next     = Get_Joining_Class (string, i, length,  1, reverse);

      /* R1 */

      if (current == transparent)
	{
	  properties[i] |= isolated_p;
	  continue;
	}

      /* R2 */

      if (previous == causing ||
	  previous == left    ||
	  previous == dual   )
	if (current == right)
	  {
	    properties[i] |= reverse ? initial_p : final_p;
	    continue;
	  }

      /* R3 */

      if (current == left)
	if (next == causing ||
	    next == right   ||
	    next == dual   )
	  {
	    properties[i] |= reverse ? final_p : initial_p;
	    continue;
	  }

      /* R4 */

      if (previous == causing ||
	  previous == left    ||
	  previous == dual   )
	if (current == dual)
	  if (next == causing ||
	      next == right   ||
	      next == dual   )
	    {
	      properties[i] |= medial_p;
	      continue;
	    }

      /* R5 */

      if (previous == causing ||
	  previous == left    ||
	  previous == dual   )
	if (current == dual)
	  if (!(next == causing ||
		next == right   ||
		next == dual   ))
	    {
	      properties[i] |= reverse ? initial_p : final_p;
	      continue;
	    }

      /* R6 */

      if (!(previous == causing ||
	    previous == left    ||
	    previous == dual   ))
	if (current == dual)
	  if (next == causing ||
	      next == right   ||
	      next == dual   )
	    {
	      properties[i] |= reverse ? final_p : initial_p;
	      continue;
	    }

      /* R7 */

      properties[i] |= isolated_p;
    }

  return FT_Err_Ok;
}


/* End */