KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > ArabicShaping


1 /*
2 *******************************************************************************
3 * Copyright (C) 2001-2005, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 */

7
8 package com.ibm.icu.text;
9
10 import java.io.IOException JavaDoc;
11 import java.util.MissingResourceException JavaDoc;
12
13 import com.ibm.icu.impl.UBiDiProps;
14
15 import com.ibm.icu.lang.*;
16
17 /**
18  * Shape Arabic text on a character basis.
19  *
20  * <p>ArabicShaping performs basic operations for "shaping" Arabic text. It is most
21  * useful for use with legacy data formats and legacy display technology
22  * (simple terminals). All operations are performed on Unicode characters.</p>
23  *
24  * <p>Text-based shaping means that some character code points in the text are
25  * replaced by others depending on the context. It transforms one kind of text
26  * into another. In comparison, modern displays for Arabic text select
27  * appropriate, context-dependent font glyphs for each text element, which means
28  * that they transform text into a glyph vector.</p>
29  *
30  * <p>Text transformations are necessary when modern display technology is not
31  * available or when text needs to be transformed to or from legacy formats that
32  * use "shaped" characters. Since the Arabic script is cursive, connecting
33  * adjacent letters to each other, computers select images for each letter based
34  * on the surrounding letters. This usually results in four images per Arabic
35  * letter: initial, middle, final, and isolated forms. In Unicode, on the other
36  * hand, letters are normally stored abstract, and a display system is expected
37  * to select the necessary glyphs. (This makes searching and other text
38  * processing easier because the same letter has only one code.) It is possible
39  * to mimic this with text transformations because there are characters in
40  * Unicode that are rendered as letters with a specific shape
41  * (or cursive connectivity). They were included for interoperability with
42  * legacy systems and codepages, and for unsophisticated display systems.</p>
43  *
44  * <p>A second kind of text transformations is supported for Arabic digits:
45  * For compatibility with legacy codepages that only include European digits,
46  * it is possible to replace one set of digits by another, changing the
47  * character code points. These operations can be performed for either
48  * Arabic-Indic Digits (U+0660...U+0669) or Eastern (Extended) Arabic-Indic
49  * digits (U+06f0...U+06f9).</p>
50  *
51  * <p>Some replacements may result in more or fewer characters (code points).
52  * By default, this means that the destination buffer may receive text with a
53  * length different from the source length. Some legacy systems rely on the
54  * length of the text to be constant. They expect extra spaces to be added
55  * or consumed either next to the affected character or at the end of the
56  * text.</p>
57  * @stable ICU 2.0
58  */

59 public final class ArabicShaping {
60     private final int options;
61     private boolean isLogical; // convenience
62

63     /**
64      * Convert a range of text in the source array, putting the result
65      * into a range of text in the destination array, and return the number
66      * of characters written.
67      *
68      * @param source An array containing the input text
69      * @param sourceStart The start of the range of text to convert
70      * @param sourceLength The length of the range of text to convert
71      * @param dest The destination array that will receive the result.
72      * It may be <code>NULL</code> only if <code>destSize</code> is 0.
73      * @param destStart The start of the range of the destination buffer to use.
74      * @param destSize The size (capacity) of the destination buffer.
75      * If <code>destSize</code> is 0, then no output is produced,
76      * but the necessary buffer size is returned ("preflighting"). This
77      * does not validate the text against the options, for example,
78      * if letters are being unshaped, and spaces are being consumed
79      * following lamalef, this will not detect a lamalef without a
80      * corresponding space. An error will be thrown when the actual
81      * conversion is attempted.
82      * @return The number of chars written to the destination buffer.
83      * If an error occurs, then no output was written, or it may be
84      * incomplete.
85      * @throws ArabicShapingException if the text cannot be converted according to the options.
86      * @stable ICU 2.0
87      */

88     public int shape(char[] source, int sourceStart, int sourceLength,
89                      char[] dest, int destStart, int destSize) throws ArabicShapingException {
90         if (source == null) {
91             throw new IllegalArgumentException JavaDoc("source can not be null");
92         }
93         if (sourceStart < 0 || sourceLength < 0 || sourceStart + sourceLength > source.length) {
94             throw new IllegalArgumentException JavaDoc("bad source start (" + sourceStart +
95                                                ") or length (" + sourceLength +
96                                                ") for buffer of length " + source.length);
97         }
98         if (dest == null && destSize != 0) {
99             throw new IllegalArgumentException JavaDoc("null dest requires destSize == 0");
100         }
101         if ((destSize != 0) &&
102             (destStart < 0 || destSize < 0 || destStart + destSize > dest.length)) {
103             throw new IllegalArgumentException JavaDoc("bad dest start (" + destStart +
104                                                ") or size (" + destSize +
105                                                ") for buffer of length " + dest.length);
106         }
107
108         return internalShape(source, sourceStart, sourceLength, dest, destStart, destSize);
109     }
110
111     /**
112      * Convert a range of text in place. This may only be used if the Length option
113      * does not grow or shrink the text.
114      *
115      * @param source An array containing the input text
116      * @param start The start of the range of text to convert
117      * @param length The length of the range of text to convert
118      * @throws ArabicShapingException if the text cannot be converted according to the options.
119      * @stable ICU 2.0
120      */

121     public void shape(char[] source, int start, int length) throws ArabicShapingException {
122         if ((options & LENGTH_MASK) == LENGTH_GROW_SHRINK) {
123             throw new ArabicShapingException("Cannot shape in place with length option grow/shrink.");
124         }
125         shape(source, start, length, source, start, length);
126     }
127
128     /**
129      * Convert a string, returning the new string.
130      *
131      * @param text the string to convert
132      * @return the converted string
133      * @throws ArabicShapingException if the string cannot be converted according to the options.
134      * @stable ICU 2.0
135      */

136     public String JavaDoc shape(String JavaDoc text) throws ArabicShapingException {
137         char[] src = text.toCharArray();
138         char[] dest = src;
139         if (((options & LENGTH_MASK) == LENGTH_GROW_SHRINK) &&
140             ((options & LETTERS_MASK) == LETTERS_UNSHAPE)) {
141
142             dest = new char[src.length * 2]; // max
143
}
144         int len = shape(src, 0, src.length, dest, 0, dest.length);
145
146         return new String JavaDoc(dest, 0, len);
147     }
148
149     /**
150      * Construct ArabicShaping using the options flags.
151      * The flags are as follows:<br>
152      * 'LENGTH' flags control whether the text can change size, and if not,
153      * how to maintain the size of the text when LamAlef ligatures are
154      * formed or broken.<br>
155      * 'TEXT_DIRECTION' flags control whether the text is read and written
156      * in visual order or in logical order.<br>
157      * 'LETTERS_SHAPE' flags control whether conversion is to or from
158      * presentation forms.<br>
159      * 'DIGITS' flags control whether digits are shaped, and whether from
160      * European to Arabic-Indic or vice-versa.<br>
161      * 'DIGIT_TYPE' flags control whether standard or extended Arabic-Indic
162      * digits are used when performing digit conversion.
163      * @stable ICU 2.0
164      */

165     public ArabicShaping(int options) {
166         this.options = options;
167         if ((options & DIGITS_MASK) > 0x80) {
168             throw new IllegalArgumentException JavaDoc("bad DIGITS options");
169         }
170         isLogical = (options & TEXT_DIRECTION_MASK) == TEXT_DIRECTION_LOGICAL;
171     }
172
173     /**
174      * Memory option: allow the result to have a different length than the source.
175      * @stable ICU 2.0
176      */

177     public static final int LENGTH_GROW_SHRINK = 0;
178
179     /**
180      * Memory option: the result must have the same length as the source.
181      * If more room is necessary, then try to consume spaces next to modified characters.
182      * @stable ICU 2.0
183      */

184     public static final int LENGTH_FIXED_SPACES_NEAR = 1;
185
186     /**
187      * Memory option: the result must have the same length as the source.
188      * If more room is necessary, then try to consume spaces at the end of the text.
189      * @stable ICU 2.0
190      */

191     public static final int LENGTH_FIXED_SPACES_AT_END = 2;
192
193     /**
194      * Memory option: the result must have the same length as the source.
195      * If more room is necessary, then try to consume spaces at the beginning of the text.
196      * @stable ICU 2.0
197      */

198     public static final int LENGTH_FIXED_SPACES_AT_BEGINNING = 3;
199
200     /**
201      * Bit mask for memory options.
202      * @stable ICU 2.0
203      */

204     public static final int LENGTH_MASK = 3;
205
206
207     /**
208      * Direction indicator: the source is in logical (keyboard) order.
209      * @stable ICU 2.0
210      */

211     public static final int TEXT_DIRECTION_LOGICAL = 0;
212
213     /**
214      * Direction indicator: the source is in visual (display) order, that is,
215      * the leftmost displayed character is stored first.
216      * @stable ICU 2.0
217      */

218     public static final int TEXT_DIRECTION_VISUAL_LTR = 4;
219
220     /**
221      * Bit mask for direction indicators.
222      * @stable ICU 2.0
223      */

224     public static final int TEXT_DIRECTION_MASK = 4;
225
226
227     /**
228      * Letter shaping option: do not perform letter shaping.
229      * @stable ICU 2.0
230      */

231     public static final int LETTERS_NOOP = 0;
232
233     /**
234      * Letter shaping option: replace normative letter characters in the U+0600 (Arabic) block,
235      * by shaped ones in the U+FE70 (Presentation Forms B) block. Performs Lam-Alef ligature
236      * substitution.
237      * @stable ICU 2.0
238      */

239     public static final int LETTERS_SHAPE = 8;
240
241     /**
242      * Letter shaping option: replace shaped letter characters in the U+FE70 (Presentation Forms B) block
243      * by normative ones in the U+0600 (Arabic) block. Converts Lam-Alef ligatures to pairs of Lam and
244      * Alef characters, consuming spaces if required.
245      * @stable ICU 2.0
246      */

247     public static final int LETTERS_UNSHAPE = 0x10;
248
249     /**
250      * Letter shaping option: replace normative letter characters in the U+0600 (Arabic) block,
251      * except for the TASHKEEL characters at U+064B...U+0652, by shaped ones in the U+Fe70
252      * (Presentation Forms B) block. The TASHKEEL characters will always be converted to
253      * the isolated forms rather than to their correct shape.
254      * @stable ICU 2.0
255      */

256     public static final int LETTERS_SHAPE_TASHKEEL_ISOLATED = 0x18;
257
258     /**
259      * Bit mask for letter shaping options.
260      * @stable ICU 2.0
261      */

262     public static final int LETTERS_MASK = 0x18;
263
264
265     /**
266      * Digit shaping option: do not perform digit shaping.
267      * @stable ICU 2.0
268      */

269     public static final int DIGITS_NOOP = 0;
270
271     /**
272      * Digit shaping option: Replace European digits (U+0030...U+0039) by Arabic-Indic digits.
273      * @stable ICU 2.0
274      */

275     public static final int DIGITS_EN2AN = 0x20;
276
277     /**
278      * Digit shaping option: Replace Arabic-Indic digits by European digits (U+0030...U+0039).
279      * @stable ICU 2.0
280      */

281     public static final int DIGITS_AN2EN = 0x40;
282
283     /**
284      * Digit shaping option:
285      * Replace European digits (U+0030...U+0039) by Arabic-Indic digits
286      * if the most recent strongly directional character
287      * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC).
288      * The initial state at the start of the text is assumed to be not an Arabic,
289      * letter, so European digits at the start of the text will not change.
290      * Compare to DIGITS_ALEN2AN_INIT_AL.
291      * @stable ICU 2.0
292      */

293     public static final int DIGITS_EN2AN_INIT_LR = 0x60;
294
295     /**
296      * Digit shaping option:
297      * Replace European digits (U+0030...U+0039) by Arabic-Indic digits
298      * if the most recent strongly directional character
299      * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC).
300      * The initial state at the start of the text is assumed to be an Arabic,
301      * letter, so European digits at the start of the text will change.
302      * Compare to DIGITS_ALEN2AN_INT_LR.
303      * @stable ICU 2.0
304      */

305     public static final int DIGITS_EN2AN_INIT_AL = 0x80;
306
307     /** Not a valid option value. */
308     private static final int DIGITS_RESERVED = 0xa0;
309
310     /**
311      * Bit mask for digit shaping options.
312      * @stable ICU 2.0
313      */

314     public static final int DIGITS_MASK = 0xe0;
315
316     /**
317      * Digit type option: Use Arabic-Indic digits (U+0660...U+0669).
318      * @stable ICU 2.0
319      */

320     public static final int DIGIT_TYPE_AN = 0;
321
322     /**
323      * Digit type option: Use Eastern (Extended) Arabic-Indic digits (U+06f0...U+06f9).
324      * @stable ICU 2.0
325      */

326     public static final int DIGIT_TYPE_AN_EXTENDED = 0x100;
327
328     /**
329      * Bit mask for digit type options.
330      * @stable ICU 2.0
331      */

332     public static final int DIGIT_TYPE_MASK = 0x0100; // 0x3f00?
333

334     /**
335      * @stable ICU 2.0
336      */

337     public boolean equals(Object JavaDoc rhs) {
338         return rhs != null &&
339             rhs.getClass() == ArabicShaping.class &&
340             options == ((ArabicShaping)rhs).options;
341     }
342
343     /**
344      * @stable ICU 2.0
345      */

346      ///CLOVER:OFF
347
public int hashCode() {
348         return options;
349     }
350
351     /**
352      * @stable ICU 2.0
353      */

354     public String JavaDoc toString() {
355         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(super.toString());
356         buf.append('[');
357         switch (options & LENGTH_MASK) {
358         case LENGTH_GROW_SHRINK: buf.append("grow/shrink"); break;
359         case LENGTH_FIXED_SPACES_NEAR: buf.append("spaces near"); break;
360         case LENGTH_FIXED_SPACES_AT_END: buf.append("spaces at end"); break;
361         case LENGTH_FIXED_SPACES_AT_BEGINNING: buf.append("spaces at beginning"); break;
362         }
363         switch (options & TEXT_DIRECTION_MASK) {
364         case TEXT_DIRECTION_LOGICAL: buf.append(", logical"); break;
365         case TEXT_DIRECTION_VISUAL_LTR: buf.append(", visual"); break;
366         }
367         switch (options & LETTERS_MASK) {
368         case LETTERS_NOOP: buf.append(", no letter shaping"); break;
369         case LETTERS_SHAPE: buf.append(", shape letters"); break;
370         case LETTERS_SHAPE_TASHKEEL_ISOLATED: buf.append(", shape letters tashkeel isolated"); break;
371         case LETTERS_UNSHAPE: buf.append(", unshape letters"); break;
372         }
373         switch (options & DIGITS_MASK) {
374         case DIGITS_NOOP: buf.append(", no digit shaping"); break;
375         case DIGITS_EN2AN: buf.append(", shape digits to AN"); break;
376         case DIGITS_AN2EN: buf.append(", shape digits to EN"); break;
377         case DIGITS_EN2AN_INIT_LR: buf.append(", shape digits to AN contextually: default EN"); break;
378         case DIGITS_EN2AN_INIT_AL: buf.append(", shape digits to AN contextually: default AL"); break;
379         }
380         switch (options & DIGIT_TYPE_MASK) {
381         case DIGIT_TYPE_AN: buf.append(", standard Arabic-Indic digits"); break;
382         case DIGIT_TYPE_AN_EXTENDED: buf.append(", extended Arabic-Indic digits"); break;
383         }
384         buf.append("]");
385
386         return buf.toString();
387     }
388     ///CLOVER:ON
389

390     //
391
// ported api
392
//
393

394     private static final int IRRELEVANT = 4;
395     private static final int LAMTYPE = 16;
396     private static final int ALEFTYPE = 32;
397
398     private static final int LINKR = 1;
399     private static final int LINKL = 2;
400     private static final int LINK_MASK = 3;
401
402     private static final int irrelevantPos[] = {
403         0x0, 0x2, 0x4, 0x6, 0x8, 0xA, 0xC, 0xE
404     };
405
406     private static final char convertLamAlef[] = {
407         '\u0622', // FEF5
408
'\u0622', // FEF6
409
'\u0623', // FEF7
410
'\u0623', // FEF8
411
'\u0625', // FEF9
412
'\u0625', // FEFA
413
'\u0627', // FEFB
414
'\u0627' // FEFC
415
};
416
417     private static final char convertNormalizedLamAlef[] = {
418         '\u0622', // 065C
419
'\u0623', // 065D
420
'\u0625', // 065E
421
'\u0627', // 065F
422
};
423
424     private static final int[] araLink = {
425         1 + 32 + 256 * 0x11, /*0x0622*/
426         1 + 32 + 256 * 0x13, /*0x0623*/
427         1 + 256 * 0x15, /*0x0624*/
428         1 + 32 + 256 * 0x17, /*0x0625*/
429         1 + 2 + 256 * 0x19, /*0x0626*/
430         1 + 32 + 256 * 0x1D, /*0x0627*/
431         1 + 2 + 256 * 0x1F, /*0x0628*/
432         1 + 256 * 0x23, /*0x0629*/
433         1 + 2 + 256 * 0x25, /*0x062A*/
434         1 + 2 + 256 * 0x29, /*0x062B*/
435         1 + 2 + 256 * 0x2D, /*0x062C*/
436         1 + 2 + 256 * 0x31, /*0x062D*/
437         1 + 2 + 256 * 0x35, /*0x062E*/
438         1 + 256 * 0x39, /*0x062F*/
439         1 + 256 * 0x3B, /*0x0630*/
440         1 + 256 * 0x3D, /*0x0631*/
441         1 + 256 * 0x3F, /*0x0632*/
442         1 + 2 + 256 * 0x41, /*0x0633*/
443         1 + 2 + 256 * 0x45, /*0x0634*/
444         1 + 2 + 256 * 0x49, /*0x0635*/
445         1 + 2 + 256 * 0x4D, /*0x0636*/
446         1 + 2 + 256 * 0x51, /*0x0637*/
447         1 + 2 + 256 * 0x55, /*0x0638*/
448         1 + 2 + 256 * 0x59, /*0x0639*/
449         1 + 2 + 256 * 0x5D, /*0x063A*/
450         0, 0, 0, 0, 0, /*0x063B-0x063F*/
451         1 + 2, /*0x0640*/
452         1 + 2 + 256 * 0x61, /*0x0641*/
453         1 + 2 + 256 * 0x65, /*0x0642*/
454         1 + 2 + 256 * 0x69, /*0x0643*/
455         1 + 2 + 16 + 256 * 0x6D, /*0x0644*/
456         1 + 2 + 256 * 0x71, /*0x0645*/
457         1 + 2 + 256 * 0x75, /*0x0646*/
458         1 + 2 + 256 * 0x79, /*0x0647*/
459         1 + 256 * 0x7D, /*0x0648*/
460         1 + 256 * 0x7F, /*0x0649*/
461         1 + 2 + 256 * 0x81, /*0x064A*/
462         4, 4, 4, 4, /*0x064B-0x064E*/
463         4, 4, 4, 4, /*0x064F-0x0652*/
464         4, 4, 4, 0, 0, /*0x0653-0x0657*/
465         0, 0, 0, 0, /*0x0658-0x065B*/
466         1 + 256 * 0x85, /*0x065C*/
467         1 + 256 * 0x87, /*0x065D*/
468         1 + 256 * 0x89, /*0x065E*/
469         1 + 256 * 0x8B, /*0x065F*/
470         0, 0, 0, 0, 0, /*0x0660-0x0664*/
471         0, 0, 0, 0, 0, /*0x0665-0x0669*/
472         0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
473         4, /*0x0670*/
474         0, /*0x0671*/
475         1 + 32, /*0x0672*/
476         1 + 32, /*0x0673*/
477         0, /*0x0674*/
478         1 + 32, /*0x0675*/
479         1, 1, /*0x0676-0x0677*/
480         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/
481         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
482         1+2, 1+2, 1+2, 1+2, /*0x0684-0x0687*/
483         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/
484         1, 1, 1, 1, 1, 1, 1, 1, /*0x0692-0x0699*/
485         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
486         1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
487         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
488         1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
489         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
490         1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
491         1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/
492         1+2, 1+2, /*0x06B8-0x06BF*/
493         1, /*0x06C0*/
494         1+2, /*0x06C1*/
495         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
496         1+2, /*0x06CC*/
497         1, /*0x06CD*/
498         1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
499         1, 1 /*0x06D2-0x06D3*/
500     };
501
502     private static final int[] presLink = {
503         1 + 2, /*0xFE70*/
504         1 + 2, /*0xFE71*/
505         1 + 2, 0, 1+ 2, 0, 1+ 2, /*0xFE72-0xFE76*/
506         1 + 2, /*0xFE77*/
507         1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE78-0xFE81*/
508         1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE82-0xFE85*/
509         0, 0 + 32, 1 + 32, 0 + 32, /*0xFE86-0xFE89*/
510         1 + 32, 0, 1, 0 + 32, /*0xFE8A-0xFE8D*/
511         1 + 32, 0, 2, 1 + 2, /*0xFE8E-0xFE91*/
512         1, 0 + 32, 1 + 32, 0, /*0xFE92-0xFE95*/
513         2, 1 + 2, 1, 0, /*0xFE96-0xFE99*/
514         1, 0, 2, 1 + 2, /*0xFE9A-0xFE9D*/
515         1, 0, 2, 1 + 2, /*0xFE9E-0xFEA1*/
516         1, 0, 2, 1 + 2, /*0xFEA2-0xFEA5*/
517         1, 0, 2, 1 + 2, /*0xFEA6-0xFEA9*/
518         1, 0, 2, 1 + 2, /*0xFEAA-0xFEAD*/
519         1, 0, 1, 0, /*0xFEAE-0xFEB1*/
520         1, 0, 1, 0, /*0xFEB2-0xFEB5*/
521         1, 0, 2, 1+2, /*0xFEB6-0xFEB9*/
522         1, 0, 2, 1+2, /*0xFEBA-0xFEBD*/
523         1, 0, 2, 1+2, /*0xFEBE-0xFEC1*/
524         1, 0, 2, 1+2, /*0xFEC2-0xFEC5*/
525         1, 0, 2, 1+2, /*0xFEC6-0xFEC9*/
526         1, 0, 2, 1+2, /*0xFECA-0xFECD*/
527         1, 0, 2, 1+2, /*0xFECE-0xFED1*/
528         1, 0, 2, 1+2, /*0xFED2-0xFED5*/
529         1, 0, 2, 1+2, /*0xFED6-0xFED9*/
530         1, 0, 2, 1+2, /*0xFEDA-0xFEDD*/
531         1, 0, 2, 1+2, /*0xFEDE-0xFEE1*/
532         1, 0 + 16, 2 + 16, 1 + 2 +16, /*0xFEE2-0xFEE5*/
533         1 + 16, 0, 2, 1+2, /*0xFEE6-0xFEE9*/
534         1, 0, 2, 1+2, /*0xFEEA-0xFEED*/
535         1, 0, 2, 1+2, /*0xFEEE-0xFEF1*/
536         1, 0, 1, 0, /*0xFEF2-0xFEF5*/
537         1, 0, 2, 1+2, /*0xFEF6-0xFEF9*/
538         1, 0, 1, 0, /*0xFEFA-0xFEFD*/
539         1, 0, 1, 0,
540         1
541     };
542
543     private static int[] convertFEto06 = {
544         /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
545         /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
546         /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
547         /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
548         /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
549         /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
550         /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
551         /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
552         /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
553         /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
554     };
555
556     private static final int shapeTable[][][] = {
557         { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
558         { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
559         { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
560         { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
561     };
562
563     /*
564      * This function shapes European digits to Arabic-Indic digits
565      * in-place, writing over the input characters. Data is in visual
566      * order.
567      */

568     private void shapeToArabicDigitsWithContext(char[] dest,
569                                                 int start,
570                                                 int length,
571                                                 char digitBase,
572                                                 boolean lastStrongWasAL) {
573         UBiDiProps bdp;
574         try {
575             bdp=UBiDiProps.getSingleton();
576         } catch (IOException JavaDoc e) {
577             throw new MissingResourceException JavaDoc(e.getMessage(), "(BidiProps)", "");
578         }
579         digitBase -= '0'; // move common adjustment out of loop
580

581         for(int i = start + length; --i >= start;) {
582             char ch = dest[i];
583             switch (bdp.getClass(ch)) {
584             case UCharacterDirection.LEFT_TO_RIGHT:
585             case UCharacterDirection.RIGHT_TO_LEFT:
586                 lastStrongWasAL = false;
587                 break;
588             case UCharacterDirection.RIGHT_TO_LEFT_ARABIC:
589                 lastStrongWasAL = true;
590                 break;
591             case UCharacterDirection.EUROPEAN_NUMBER:
592                 if (lastStrongWasAL && ch <= '\u0039') {
593                     dest[i] = (char)(ch + digitBase);
594                 }
595                 break;
596             default:
597                 break;
598             }
599         }
600     }
601
602     /*
603      * Name : invertBuffer
604      * Function: This function inverts the buffer, it's used
605      * in case the user specifies the buffer to be
606      * TEXT_DIRECTION_LOGICAL
607      */

608     private static void invertBuffer(char[] buffer,
609                                      int start,
610                                      int length) {
611
612         for(int i = start, j = start + length - 1; i < j; i++, --j) {
613             char temp = buffer[i];
614             buffer[i] = buffer[j];
615             buffer[j] = temp;
616         }
617     }
618
619     /*
620      * Name : changeLamAlef
621      * Function: Converts the Alef characters into an equivalent
622      * LamAlef location in the 0x06xx Range, this is an
623      * intermediate stage in the operation of the program
624      * later it'll be converted into the 0xFExx LamAlefs
625      * in the shaping function.
626      */

627     private static char changeLamAlef(char ch) {
628         switch(ch) {
629         case '\u0622': return '\u065C';
630         case '\u0623': return '\u065D';
631         case '\u0625': return '\u065E';
632         case '\u0627': return '\u065F';
633         default: return '\u0000'; // not a lamalef
634
}
635     }
636
637     /*
638      * Name : specialChar
639      * Function: Special Arabic characters need special handling in the shapeUnicode
640      * function, this function returns 1 or 2 for these special characters
641      */

642     private static int specialChar(char ch) {
643         if ((ch > '\u0621' && ch < '\u0626') ||
644             (ch == '\u0627') ||
645             (ch > '\u062E' && ch < '\u0633') ||
646             (ch > '\u0647' && ch < '\u064A') ||
647             (ch == '\u0629')) {
648             return 1;
649         } else if (ch >= '\u064B' && ch<= '\u0652') {
650             return 2;
651         } else if (ch >= 0x0653 && ch <= 0x0655 ||
652                    ch == 0x0670 ||
653                    ch >= 0xFE70 && ch <= 0xFE7F) {
654             return 3;
655         } else {
656             return 0;
657         }
658     }
659     
660     /*
661      * Name : getLink
662      * Function: Resolves the link between the characters as
663      * Arabic characters have four forms :
664      * Isolated, Initial, Middle and Final Form
665      */

666     private static int getLink(char ch) {
667         if (ch >= '\u0622' && ch <= '\u06D3') {
668             return araLink[ch - '\u0622'];
669         } else if (ch == '\u200D') {
670             return 3;
671         } else if (ch >= '\u206D' && ch <= '\u206F') {
672             return 4;
673         } else if (ch >= '\uFE70' && ch <= '\uFEFC') {
674             return presLink[ch - '\uFE70'];
675         } else {
676             return 0;
677         }
678     }
679
680     /*
681      * Name : countSpaces
682      * Function: Counts the number of spaces
683      * at each end of the logical buffer
684      */

685     private static int countSpacesLeft(char[] dest,
686                                        int start,
687                                        int count) {
688         for (int i = start, e = start + count; i < e; ++i) {
689             if (dest[i] != '\u0020') {
690                 return i - start;
691             }
692         }
693         return count;
694     }
695
696     private static int countSpacesRight(char[] dest,
697                                         int start,
698                                         int count) {
699
700         for (int i = start + count; --i >= start;) {
701             if (dest[i] != '\u0020') {
702                 return start + count - 1 - i;
703             }
704         }
705         return count;
706     }
707
708     /*
709      * Name : isTashkeelChar
710      * Function: Returns 1 for Tashkeel characters else return 0
711      */

712     private static boolean isTashkeelChar(char ch) {
713         return ch >='\u064B' && ch <= '\u0652';
714     }
715
716     /*
717      * Name : isAlefChar
718      * Function: Returns 1 for Alef characters else return 0
719      */

720     private static boolean isAlefChar(char ch) {
721         return ch == '\u0622' || ch == '\u0623' || ch == '\u0625' || ch == '\u0627';
722     }
723
724     /*
725      * Name : isLamAlefChar
726      * Function: Returns 1 for LamAlef characters else return 0
727      */

728     private static boolean isLamAlefChar(char ch) {
729         return ch >= '\uFEF5' && ch <= '\uFEFC';
730     }
731
732     private static boolean isNormalizedLamAlefChar(char ch) {
733         return ch >= '\u065C' && ch <= '\u065F';
734     }
735
736     /*
737      * Name : calculateSize
738      * Function: This function calculates the destSize to be used in preflighting
739      * when the destSize is equal to 0
740      */

741     private int calculateSize(char[] source,
742                               int sourceStart,
743                               int sourceLength) {
744     
745         int destSize = sourceLength;
746
747         switch (options & LETTERS_MASK) {
748         case LETTERS_SHAPE:
749         case LETTERS_SHAPE_TASHKEEL_ISOLATED:
750             if (isLogical) {
751                 for (int i = sourceStart, e = sourceStart + sourceLength - 1; i < e; ++i) {
752                     if (source[i] == '\u0644' && isAlefChar(source[i+1])) {
753                         --destSize;
754                     }
755                 }
756             } else { // visual
757
for(int i = sourceStart + 1, e = sourceStart + sourceLength; i < e; ++i) {
758                     if (source[i] == '\u0644' && isAlefChar(source[i-1])) {
759                         --destSize;
760                     }
761                 }
762             }
763             break;
764
765         case LETTERS_UNSHAPE:
766             for(int i = sourceStart, e = sourceStart + sourceLength; i < e; ++i) {
767                 if (isLamAlefChar(source[i])) {
768                     destSize++;
769                 }
770             }
771             break;
772
773         default:
774             break;
775         }
776
777         return destSize;
778     }
779
780     /*
781      * Name : removeLamAlefSpaces
782      * Function: The shapeUnicode function converts Lam + Alef into LamAlef + space,
783      * this function removes the spaces behind the LamAlefs according to
784      * the options the user specifies, the spaces are removed to the end
785      * of the buffer, or shrink the buffer and remove spaces for good
786      * or leave the buffer as it is LamAlef + space.
787      */

788     private int removeLamAlefSpaces(char[] dest,
789                                     int start,
790                                     int length) {
791     
792         int lenOptions = options & LENGTH_MASK;
793         if (!isLogical) {
794             switch (lenOptions) {
795             case LENGTH_FIXED_SPACES_AT_BEGINNING: lenOptions = LENGTH_FIXED_SPACES_AT_END; break;
796             case LENGTH_FIXED_SPACES_AT_END: lenOptions = LENGTH_FIXED_SPACES_AT_BEGINNING; break;
797             default: break;
798             }
799         }
800
801         if (lenOptions == LENGTH_FIXED_SPACES_NEAR) {
802             for (int i = start, e = i + length; i < e; ++i) {
803                 if (dest[i] == '\uffff') {
804                     dest[i] = '\u0020';
805                 }
806             }
807         } else {
808             final int e = start + length;
809             int w = e;
810             int r = e;
811             while (--r >= start) {
812                 char ch = dest[r];
813                 if (ch != '\uffff') {
814                     --w;
815                     if (w != r) {
816                         dest[w] = ch;
817                     }
818                 }
819             }
820
821             if (lenOptions == LENGTH_FIXED_SPACES_AT_END) {
822                 while (w > start) {
823                     dest[--w] = '\u0020';
824                 }
825             } else {
826                 if (w > start) {
827                     // shift, assume small buffer size so don't use arraycopy
828
r = w;
829                     w = start;
830                     while (r < e) {
831                         dest[w++] = dest[r++];
832                     }
833                 } else {
834                     w = e;
835                 }
836                 if (lenOptions == LENGTH_GROW_SHRINK) {
837                     length = w - start;
838                 } else { // spaces at beginning
839
while (w < e) {
840                         dest[w++] = '\u0020';
841                     }
842                 }
843             }
844         }
845         return length;
846     }
847
848     /*
849      * Name : expandLamAlef
850      * Function: LamAlef needs special handling as the LamAlef is
851      * one character while expanding it will give two
852      * characters Lam + Alef, so we need to expand the LamAlef
853      * in near or far spaces according to the options the user
854      * specifies or increase the buffer size.
855      * Dest has enough room for the expansion if we are growing.
856      * lamalef are normalized to the 'special characters'
857      */

858     private int expandLamAlef(char[] dest,
859                               int start,
860                               int length,
861                               int lacount) throws ArabicShapingException {
862
863         int lenOptions = options & LENGTH_MASK;
864         if (!isLogical) {
865             switch (lenOptions) {
866             case LENGTH_FIXED_SPACES_AT_BEGINNING: lenOptions = LENGTH_FIXED_SPACES_AT_END; break;
867             case LENGTH_FIXED_SPACES_AT_END: lenOptions = LENGTH_FIXED_SPACES_AT_BEGINNING; break;
868             default: break;
869             }
870         }
871
872         switch (lenOptions) {
873         case LENGTH_GROW_SHRINK:
874             {
875                 for (int r = start + length, w = r + lacount; --r >= start;) {
876                     char ch = dest[r];
877                     if (isNormalizedLamAlefChar(ch)) {
878                         dest[--w] = '\u0644';
879                         dest[--w] = convertNormalizedLamAlef[ch - '\u065C'];
880                     } else {
881                         dest[--w] = ch;
882                     }
883                 }
884             }
885             length += lacount;
886             break;
887
888         case LENGTH_FIXED_SPACES_NEAR:
889             {
890                 if (isNormalizedLamAlefChar(dest[start])) {
891                     throw new ArabicShapingException("no space for lamalef");
892                 }
893                 for (int i = start + length; --i > start;) { // don't check start, already checked
894
char ch = dest[i];
895                     if (isNormalizedLamAlefChar(ch)) {
896                         if (dest[i-1] == '\u0020') {
897                             dest[i] = '\u0644';
898                             dest[--i] = convertNormalizedLamAlef[ch - '\u065C'];
899                         } else {
900                             throw new ArabicShapingException("no space for lamalef");
901                         }
902                     }
903                 }
904             }
905             break;
906
907         case LENGTH_FIXED_SPACES_AT_END:
908             {
909                 if (lacount > countSpacesLeft(dest, start, length)) {
910                     throw new ArabicShapingException("no space for lamalef");
911                 }
912                 for (int r = start + lacount, w = start, e = start + length; r < e; ++r) {
913                     char ch = dest[r];
914                     if (isNormalizedLamAlefChar(ch)) {
915                         dest[w++] = convertNormalizedLamAlef[ch - '\u065C'];
916                         dest[w++] = '\u0644';
917                     } else {
918                         dest[w++] = ch;
919                     }
920                 }
921             }
922             break;
923                 
924         case LENGTH_FIXED_SPACES_AT_BEGINNING:
925             {
926                 if (lacount > countSpacesRight(dest, start, length)) {
927                     throw new ArabicShapingException("no space for lamalef");
928                 }
929                 for (int r = start + length - lacount, w = start + length; --r >= start;) {
930                     char ch = dest[r];
931                     if (isNormalizedLamAlefChar(ch)) {
932                         dest[--w] = '\u0644';
933                         dest[--w] = convertNormalizedLamAlef[ch - '\u065C'];
934                     } else {
935                         dest[--w] = ch;
936                     }
937                 }
938             }
939             break;
940         }
941
942         return length;
943     }
944
945     /* Convert the input buffer from FExx Range into 06xx Range
946      * to put all characters into the 06xx range
947      * even the lamalef is converted to the special region in
948      * the 06xx range. Return the number of lamalef chars found.
949      */

950     private int normalize(char[] dest, int start, int length) {
951         int lacount = 0;
952         for (int i = start, e = i + length; i < e; ++i) {
953             char ch = dest[i];
954             if (ch >= '\uFE70' && ch <= '\uFEFC') {
955                 if (isLamAlefChar(ch)) {
956                     ++lacount;
957                 }
958                 dest[i] = (char)convertFEto06[ch - '\uFE70'];
959             }
960         }
961         return lacount;
962     }
963
964     /*
965      * Name : shapeUnicode
966      * Function: Converts an Arabic Unicode buffer in 06xx Range into a shaped
967      * arabic Unicode buffer in FExx Range
968      */

969     private int shapeUnicode(char[] dest,
970                              int start,
971                              int length,
972                              int destSize,
973                              int tashkeelFlag) {
974
975
976         normalize(dest, start, length);
977
978         // resolve the link between the characters.
979
// Arabic characters have four forms: Isolated, Initial, Medial and Final.
980
// Tashkeel characters have two, isolated or medial, and sometimes only isolated.
981
// tashkeelFlag == 0: shape normally, 1: shape isolated, 2: don't shape
982

983         boolean lamalef_found = false;
984         int i = start + length - 1;
985         int currLink = getLink(dest[i]);
986         int nextLink = 0;
987         int prevLink = 0;
988         int lastLink = 0;
989         int prevPos = i;
990         int lastPos = i;
991         int nx = -2;
992         int nw = 0;
993
994         while (i >= 0) {
995             // If high byte of currLink > 0 then there might be more than one shape
996
if ((currLink & '\uFF00') > 0 || isTashkeelChar(dest[i])) {
997                 nw = i - 1;
998                 nx = -2;
999                 while (nx < 0) { // we need to know about next char
1000
if (nw == -1) {
1001                        nextLink = 0;
1002                        nx = Integer.MAX_VALUE;
1003                    } else {
1004                        nextLink = getLink(dest[nw]);
1005                        if ((nextLink & IRRELEVANT) == 0) {
1006                            nx = nw;
1007                        } else {
1008                            --nw;
1009                        }
1010                    }
1011                }
1012
1013                if (((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0)) {
1014                    lamalef_found = true;
1015                    char wLamalef = changeLamAlef(dest[i]); // get from 0x065C-0x065f
1016
if (wLamalef != '\u0000') {
1017                        // replace alef by marker, it will be removed later
1018
dest[i] = '\uffff';
1019                        dest[lastPos] = wLamalef;
1020                        i = lastPos;
1021                    }
1022
1023                    lastLink = prevLink;
1024                    currLink = getLink(wLamalef); // requires '\u0000', unfortunately
1025
}
1026
1027                // get the proper shape according to link ability of neighbors
1028
// and of character; depends on the order of the shapes
1029
// (isolated, initial, middle, final) in the compatibility area
1030

1031                int flag = specialChar(dest[i]);
1032
1033                int shape = shapeTable[nextLink & LINK_MASK]
1034                    [lastLink & LINK_MASK]
1035                    [currLink & LINK_MASK];
1036
1037                if (flag == 1) {
1038                    shape &= 0x1;
1039                } else if (flag == 2) {
1040                    if (tashkeelFlag == 0 &&
1041                        ((lastLink & LINKL) != 0) &&
1042                        ((nextLink & LINKR) != 0) &&
1043                        dest[i] != '\u064C' &&
1044                        dest[i] != '\u064D' &&
1045                        !((nextLink & ALEFTYPE) == ALEFTYPE &&
1046                          (lastLink & LAMTYPE) == LAMTYPE)) {
1047        
1048                        shape = 1;
1049                    } else {
1050                        shape = 0;
1051                    }
1052                }
1053
1054                if (flag == 2) {
1055                    if (tashkeelFlag < 2) {
1056                        dest[i] = (char)('\uFE70' + irrelevantPos[dest[i] - '\u064B'] + shape);
1057                    } // else leave tashkeel alone
1058
} else {
1059                    dest[i] = (char)('\uFE70' + (currLink >> 8) + shape);
1060                }
1061            }
1062
1063            // move one notch forward
1064
if ((currLink & IRRELEVANT) == 0) {
1065                prevLink = lastLink;
1066                lastLink = currLink;
1067                prevPos = lastPos;
1068                lastPos = i;
1069            }
1070
1071            --i;
1072            if (i == nx) {
1073                currLink = nextLink;
1074                nx = -2;
1075            } else if (i != -1) {
1076                currLink = getLink(dest[i]);
1077            }
1078        }
1079
1080        // If we found a lam/alef pair in the buffer
1081
// call removeLamAlefSpaces to remove the spaces that were added
1082

1083        if (lamalef_found) {
1084            destSize = removeLamAlefSpaces(dest, start, length);
1085        } else {
1086            destSize = length;
1087        }
1088        
1089        return destSize;
1090    }
1091
1092    /*
1093     * Name : deShapeUnicode
1094     * Function: Converts an Arabic Unicode buffer in FExx Range into unshaped
1095     * arabic Unicode buffer in 06xx Range
1096     */

1097    private int deShapeUnicode(char[] dest,
1098                               int start,
1099                               int length,
1100                               int destSize) throws ArabicShapingException {
1101
1102        int lamalef_count = normalize(dest, start, length);
1103
1104        // If there was a lamalef in the buffer call expandLamAlef
1105
if (lamalef_count != 0) {
1106            // need to adjust dest to fit expanded buffer... !!!
1107
destSize = expandLamAlef(dest, start, length, lamalef_count);
1108        } else {
1109            destSize = length;
1110        }
1111
1112        return destSize;
1113    }
1114
1115    private int internalShape(char[] source,
1116                              int sourceStart,
1117                              int sourceLength,
1118                              char[] dest,
1119                              int destStart,
1120                              int destSize) throws ArabicShapingException {
1121
1122        if (sourceLength == 0) {
1123            return 0;
1124        }
1125
1126        if (destSize == 0) {
1127            if (((options & LETTERS_MASK) != LETTERS_NOOP) &&
1128                ((options & LENGTH_MASK) == LENGTH_GROW_SHRINK)) {
1129    
1130                return calculateSize(source, sourceStart, sourceLength);
1131            } else {
1132                return sourceLength; // by definition
1133
}
1134        }
1135
1136        // always use temp buffer
1137
char[] temp = new char[sourceLength * 2]; // all lamalefs requiring expansion
1138
System.arraycopy(source, sourceStart, temp, 0, sourceLength);
1139
1140        if (isLogical) {
1141            invertBuffer(temp, 0, sourceLength);
1142        }
1143
1144        int outputSize = sourceLength;
1145
1146        switch (options & LETTERS_MASK) {
1147        case LETTERS_SHAPE_TASHKEEL_ISOLATED:
1148            outputSize = shapeUnicode(temp, 0, sourceLength, destSize, 1);
1149            break;
1150
1151        case LETTERS_SHAPE:
1152            outputSize = shapeUnicode(temp, 0, sourceLength, destSize, 0);
1153            break;
1154
1155        case LETTERS_UNSHAPE:
1156            outputSize = deShapeUnicode(temp, 0, sourceLength, destSize);
1157            break;
1158
1159        default:
1160            break;
1161        }
1162                
1163        if (outputSize > destSize) {
1164            throw new ArabicShapingException("not enough room for result data");
1165        }
1166
1167        if ((options & DIGITS_MASK) != DIGITS_NOOP) {
1168            char digitBase = '\u0030'; // European digits
1169
switch (options & DIGIT_TYPE_MASK) {
1170            case DIGIT_TYPE_AN:
1171                digitBase = '\u0660'; // Arabic-Indic digits
1172
break;
1173
1174            case DIGIT_TYPE_AN_EXTENDED:
1175                digitBase = '\u06f0'; // Eastern Arabic-Indic digits (Persian and Urdu)
1176
break;
1177
1178            default:
1179                break;
1180            }
1181
1182            switch (options & DIGITS_MASK) {
1183            case DIGITS_EN2AN:
1184                {
1185                    int digitDelta = digitBase - '\u0030';
1186                    for (int i = 0; i < outputSize; ++i) {
1187                        char ch = temp[i];
1188                        if (ch <= '\u0039' && ch >= '\u0030') {
1189                            temp[i] += digitDelta;
1190                        }
1191                    }
1192                }
1193                break;
1194
1195            case DIGITS_AN2EN:
1196                {
1197                    char digitTop = (char)(digitBase + 9);
1198                    int digitDelta = '\u0030' - digitBase;
1199                    for (int i = 0; i < outputSize; ++i) {
1200                        char ch = temp[i];
1201                        if (ch <= digitTop && ch >= digitBase) {
1202                            temp[i] += digitDelta;
1203                        }
1204                    }
1205                }
1206                break;
1207
1208            case DIGITS_EN2AN_INIT_LR:
1209                shapeToArabicDigitsWithContext(temp, 0, outputSize, digitBase, false);
1210                break;
1211
1212            case DIGITS_EN2AN_INIT_AL:
1213                shapeToArabicDigitsWithContext(temp, 0, outputSize, digitBase, true);
1214                break;
1215
1216            default:
1217                break;
1218            }
1219        }
1220
1221        if (isLogical) {
1222            invertBuffer(temp, 0, outputSize);
1223        }
1224      
1225        System.arraycopy(temp, 0, dest, destStart, outputSize);
1226      
1227        return outputSize;
1228    }
1229}
1230
Popular Tags