summaryrefslogtreecommitdiff
path: root/modules/syriac/syriac-ot.c
blob: b5bb48a9d1fd382156c377671e4452c901c5bce5 (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
/* Pango
 * syriac-ot.h: Determine what OpenType features to apply to characters based
 *              on the rules for Syriac from the OpenType standard.
 *
 * Copyright (C) 2004 Emil Soleyman-Zomalan
 * Author: Emil Soleyman-Zomalan <emil@soleyman.com>
 *
 * This file is based on the Arabic shaping code from FreeType 1 tree; original
 * copyright notice:
 *                                                                         
 *  The FreeType project -- a free and portable quality TrueType renderer.
 *
 *  Copyright 1996-2000 by
 *  D. Turner, R.Wilhelm, and W. Lemberg
 *
 * The code, like the FreeType code it is derived from is dual-licensed
 * under the  GNU Public License and the FreeType license. See see
 * pango/opentype/FT-license.txt for full details of the FreeType 
 * license.
 */
#include <config.h>
#include "syriac-ot.h"

/*     Here a table of the joining classes for characters in the range
 *     U+0700 - U+074F.
 *
 *     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 JoiningClass syriac[] =
{
  /* U+0700 */
  none, none, none, none,
  none, none, none, none,
  none, none, none, none,
  none, none, none, transparent,
	
  /* U+0710 */
  right, none, dual, dual,
  dual, right, right, right,
  right, right, dual, dual,
  dual, dual, right, dual,

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

  /* U+0730 */
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,

  /* U+0740 */
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, transparent,
  transparent, transparent, transparent, none,
  none, right, dual, dual
};

/* `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 JoiningClass
Get_Joining_Class (gunichar*   string,
		   int         pos,
		   int         length,
		   int         direction)
{
  JoiningClass  j;

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

      pos += direction;

      if (pos >= length)
        return none;
      
      if (string[pos] < 0x0700 ||
	  string[pos] >= 0x074F)
	{
	  if (string[pos] == 0x200C)
	    return causing;
	  else
	    return none;
	}   
      else
        j =  syriac[string[pos] - 0x0700];
      
      if (!direction || j != transparent)
        return j;
    }
}


/* The rules here are roughly based on the Arabic rules from the Unicode
 * 2.0 standard (which differ from the Unicode-4.0 rules), augmented
 * with the Syriac rules from the Unicode-4.0 standard. The numbers
 * R1...R11  below do not correspond to either the Arabic or the Syriac
 * rule numbering from the Unicode standard.
 * 
 * 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.
 *
 *
 * Glyphs: 0x0715 (Dalath), 0x0716 (Dalath Rish), 0x072A (Rish),
 *         0x0722 (Nun), 0x071F (Kaph)
 *   
 *
 *   R1: <anything1> <transparent> <anything2>
 *    
 *       apply joining rules for
 *       <anything1> <anything2> -> [shape1] [shape2]
 *       -> [shape1] [isolated] [shape2]
 *
 *
 *   R2: <causing|right> <0x0722|0x071F> <!(causing|right|dual)>
 *       -> [isolated]
 *
 *       The Nun and Kaph characters each have 3 different glyphs
 *       with two of those glyphs coming at the final position.
 *       However, one of those final glyphs should really be of the
 *       isolated glyph form where the preceding character cannot be
 *       joined to and there is no next character.
 *
 *       This rule exists to combine similar exception for both
 *       characters without increasing the complexity in the other
 *       rules.
 *
 *
 *   R3: <causing|right|dual> && <!(0x0715|0x0716|0x072A)> |<alaph>|
 *   
 *   	 -> [final2]
 *
 *   	 If the preceding glyph cannot be joined to the current 
 *   	 glyph and the preceding character is not a Dalath, Rish, 
 *   	 or Dotless Dalath Rish, then the Alaph takes this contextual 
 *   	 position.
 *
 *   	 The [final2] joining rule is placed ahead of the [final] to
 *   	 give it greater precedence when choosing the correct glyph.
 *   	 If it comes after the [final] rule, the incorrect glyph is
 *   	 inserted into position.
 *   	 
 *   	 
 *   R4: <0x0715|0x0715|0x072A> |<alaph>|
 *
 *   	 -> [final3]
 *
 *   	 If the previous glyph is a Dalath, Rish, or Dotless Dalath 
 *   	 Rish, then the Alaph takes this contextual position.
 *
 *   	 The [final3] joining rule is placed ahead of the [final] to
 *   	 give it greater precedence when choosing the correct glyph.
 *   	 If it comes after the [final] rule, the incorrect glyph is
 *   	 inserted into position.
 *
 *
 *   R5: <causing|right|dual> |<right>|
 *
 *   	 -> [final]
 * 
 * 
 *   R6: <causing|right|dual> |<dual>| <!(causing|right|dual)>
 *
 *   	 -> [final]
 *   	 
 * 
 *   R7: <causing|left|dual> |<dual>| <causing|right|dual>
 *
 *   	 -> [medial]
 *
 *
 *   R8: <causing|right> |<alaph>| <causing|right|dual>
 *
 *   	 -> [medial2]
 *
 *       If the Alaph glyph falls in the middle of a Syriac word and 
 *       the preceding character cannot be joined to, then the Alaph 
 *       takes this contextual position.
 *   	 
 *
 *   R9: |<left>| <causing|right|dual>
 *
 *   	 -> [initial]
 *
 *
 *   R10: <!(causing|left|dual)> |<dual>| <causing|right|dual>
 *
 *       -> [initial]
 *
 *
 *   R11: <anything> -> [isolated]
 *
 *        This joining rule is placed at the end of these features
 *        because when it is placed at the beginning of all of them
 *        it tends to break the cursive nature of Syriac writing --
 *        it inserts the isolated glyph of each character into that
 *        position with no joining occurring all throughout a text
 *        document.
 */

FT_Error
syriac_assign_properties (gunichar    *string,
			  gulong      *properties,
			  int          length)
{
  JoiningClass 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);
      current  = Get_Joining_Class (string, i, length,  0);
      next     = Get_Joining_Class (string, i, length,  1);

      /* R1 */

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

      /* R2 */

      if (string[i] == 0x0722 ||
	  string[i] == 0x071F)  
        if (previous == causing ||
            previous == right)
          if (!(next == causing ||
		next == right ||
		next == dual))
          {
            properties[i] |= isolated_p;
            continue;
          }

      /* R3 */

      if (string[i] == 0x0710)
        if (previous == causing ||
            previous == right)
          if (!(string[i - 1] == 0x0715 ||
		string[i - 1] == 0x0716 ||
		string[i - 1] == 0x072A))
          {
            properties[i] |= final2_p;
            continue;
          }

      /* R4 */

      if (string[i] == 0x0710)
        if (previous == causing ||
            previous == right)
         if (string[i - 1] == 0x0715 ||
              string[i - 1] == 0x0716 ||
              string[i - 1] == 0x072A)
          {    
            properties[i] |= final3_p;
            continue;
          }

      /* R5 */

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

      /* R6 */

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

      /* R7 */

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

      /* R8 */

      if (string[i] == 0x0710)
        if (previous == causing ||
            previous == right)
            if (next == causing ||
                next == right ||
                next == dual)
            {
              properties[i] |= medial2_p;
              continue;
            }

      /* R9 */

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

      /* R10 */

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

      /* R11 */    

      properties[i] |= isolated_p;
    }
    
  return FT_Err_Ok;
}