1/*
2*******************************************************************************
3*   Copyright (C) 2001-2009, International Business Machines
4*   Corporation and others.  All Rights Reserved.
5*******************************************************************************
6*/
7
8/*
9 * Ported with minor modifications from ICU4J 4.2's
10 * com.ibm.icu.text.ArabicShaping class.
11 */
12
13package android.icu.text;
14
15
16/**
17 * Shape Arabic text on a character basis.
18 *
19 * <p>ArabicShaping performs basic operations for "shaping" Arabic text. It is most
20 * useful for use with legacy data formats and legacy display technology
21 * (simple terminals). All operations are performed on Unicode characters.</p>
22 *
23 * <p>Text-based shaping means that some character code points in the text are
24 * replaced by others depending on the context. It transforms one kind of text
25 * into another. In comparison, modern displays for Arabic text select
26 * appropriate, context-dependent font glyphs for each text element, which means
27 * that they transform text into a glyph vector.</p>
28 *
29 * <p>Text transformations are necessary when modern display technology is not
30 * available or when text needs to be transformed to or from legacy formats that
31 * use "shaped" characters. Since the Arabic script is cursive, connecting
32 * adjacent letters to each other, computers select images for each letter based
33 * on the surrounding letters. This usually results in four images per Arabic
34 * letter: initial, middle, final, and isolated forms. In Unicode, on the other
35 * hand, letters are normally stored abstract, and a display system is expected
36 * to select the necessary glyphs. (This makes searching and other text
37 * processing easier because the same letter has only one code.) It is possible
38 * to mimic this with text transformations because there are characters in
39 * Unicode that are rendered as letters with a specific shape
40 * (or cursive connectivity). They were included for interoperability with
41 * legacy systems and codepages, and for unsophisticated display systems.</p>
42 *
43 * <p>A second kind of text transformations is supported for Arabic digits:
44 * For compatibility with legacy codepages that only include European digits,
45 * it is possible to replace one set of digits by another, changing the
46 * character code points. These operations can be performed for either
47 * Arabic-Indic Digits (U+0660...U+0669) or Eastern (Extended) Arabic-Indic
48 * digits (U+06f0...U+06f9).</p>
49 *
50 * <p>Some replacements may result in more or fewer characters (code points).
51 * By default, this means that the destination buffer may receive text with a
52 * length different from the source length. Some legacy systems rely on the
53 * length of the text to be constant. They expect extra spaces to be added
54 * or consumed either next to the affected character or at the end of the
55 * text.</p>
56 * @stable ICU 2.0
57 *
58 * @hide
59 */
60public class ArabicShaping {
61    private final int options;
62    private boolean isLogical; // convenience
63    private boolean spacesRelativeToTextBeginEnd;
64    private char tailChar;
65
66    public static final ArabicShaping SHAPER = new ArabicShaping(
67            ArabicShaping.TEXT_DIRECTION_LOGICAL |
68            ArabicShaping.LENGTH_FIXED_SPACES_NEAR |
69            ArabicShaping.LETTERS_SHAPE |
70            ArabicShaping.DIGITS_NOOP);
71
72    /**
73     * Convert a range of text in the source array, putting the result
74     * into a range of text in the destination array, and return the number
75     * of characters written.
76     *
77     * @param source An array containing the input text
78     * @param sourceStart The start of the range of text to convert
79     * @param sourceLength The length of the range of text to convert
80     * @param dest The destination array that will receive the result.
81     *   It may be <code>NULL</code> only if  <code>destSize</code> is 0.
82     * @param destStart The start of the range of the destination buffer to use.
83     * @param destSize The size (capacity) of the destination buffer.
84     *   If <code>destSize</code> is 0, then no output is produced,
85     *   but the necessary buffer size is returned ("preflighting").  This
86     *   does not validate the text against the options, for example,
87     *   if letters are being unshaped, and spaces are being consumed
88     *   following lamalef, this will not detect a lamalef without a
89     *   corresponding space.  An error will be thrown when the actual
90     *   conversion is attempted.
91     * @return The number of chars written to the destination buffer.
92     *   If an error occurs, then no output was written, or it may be
93     *   incomplete.
94     * @throws ArabicShapingException if the text cannot be converted according to the options.
95     * @stable ICU 2.0
96     */
97    public int shape(char[] source, int sourceStart, int sourceLength,
98                     char[] dest, int destStart, int destSize) throws ArabicShapingException {
99        if (source == null) {
100            throw new IllegalArgumentException("source can not be null");
101        }
102        if (sourceStart < 0 || sourceLength < 0 || sourceStart + sourceLength > source.length) {
103            throw new IllegalArgumentException("bad source start (" + sourceStart +
104                                               ") or length (" + sourceLength +
105                                               ") for buffer of length " + source.length);
106        }
107        if (dest == null && destSize != 0) {
108            throw new IllegalArgumentException("null dest requires destSize == 0");
109        }
110        if ((destSize != 0) &&
111            (destStart < 0 || destSize < 0 || destStart + destSize > dest.length)) {
112            throw new IllegalArgumentException("bad dest start (" + destStart +
113                                               ") or size (" + destSize +
114                                               ") for buffer of length " + dest.length);
115        }
116        /* Validate input options */
117        if ( ((options&TASHKEEL_MASK) > 0) &&
118             !(((options & TASHKEEL_MASK)==TASHKEEL_BEGIN)  ||
119               ((options & TASHKEEL_MASK)==TASHKEEL_END )   ||
120               ((options & TASHKEEL_MASK)==TASHKEEL_RESIZE )||
121               ((options & TASHKEEL_MASK)==TASHKEEL_REPLACE_BY_TATWEEL)) ){
122            throw new IllegalArgumentException("Wrong Tashkeel argument");
123        }
124
125       ///CLOVER:OFF
126       //According to Steven Loomis, the code is unreachable when you OR all the constants within the if statements
127       if(((options&LAMALEF_MASK) > 0)&&
128              !(((options & LAMALEF_MASK)==LAMALEF_BEGIN)  ||
129                ((options & LAMALEF_MASK)==LAMALEF_END )   ||
130                ((options & LAMALEF_MASK)==LAMALEF_RESIZE )||
131                 ((options & LAMALEF_MASK)==LAMALEF_AUTO)  ||
132                 ((options & LAMALEF_MASK)==LAMALEF_NEAR))){
133           throw new IllegalArgumentException("Wrong Lam Alef argument");
134       }
135       ///CLOVER:ON
136
137       /* Validate Tashkeel (Tashkeel replacement options should be enabled in shaping mode only)*/
138       if(((options&TASHKEEL_MASK) > 0) && (options&LETTERS_MASK) == LETTERS_UNSHAPE) {
139            throw new IllegalArgumentException("Tashkeel replacement should not be enabled in deshaping mode ");
140       }
141       return internalShape(source, sourceStart, sourceLength, dest, destStart, destSize);
142    }
143
144    /**
145     * Convert a range of text in place.  This may only be used if the Length option
146     * does not grow or shrink the text.
147     *
148     * @param source An array containing the input text
149     * @param start The start of the range of text to convert
150     * @param length The length of the range of text to convert
151     * @throws ArabicShapingException if the text cannot be converted according to the options.
152     * @stable ICU 2.0
153     */
154    public void shape(char[] source, int start, int length) throws ArabicShapingException {
155        if ((options & LAMALEF_MASK) == LAMALEF_RESIZE) {
156            throw new ArabicShapingException("Cannot shape in place with length option resize.");
157        }
158        shape(source, start, length, source, start, length);
159    }
160
161    /**
162     * Convert a string, returning the new string.
163     *
164     * @param text the string to convert
165     * @return the converted string
166     * @throws ArabicShapingException if the string cannot be converted according to the options.
167     * @stable ICU 2.0
168     */
169    public String shape(String text) throws ArabicShapingException {
170        char[] src = text.toCharArray();
171        char[] dest = src;
172        if (((options & LAMALEF_MASK) == LAMALEF_RESIZE) &&
173            ((options & LETTERS_MASK) == LETTERS_UNSHAPE)) {
174
175            dest = new char[src.length * 2]; // max
176        }
177        int len = shape(src, 0, src.length, dest, 0, dest.length);
178
179        return new String(dest, 0, len);
180    }
181
182    /**
183     * Construct ArabicShaping using the options flags.
184     * The flags are as follows:<br>
185     * 'LENGTH' flags control whether the text can change size, and if not,
186     * how to maintain the size of the text when LamAlef ligatures are
187     * formed or broken.<br>
188     * 'TEXT_DIRECTION' flags control whether the text is read and written
189     * in visual order or in logical order.<br>
190     * 'LETTERS_SHAPE' flags control whether conversion is to or from
191     * presentation forms.<br>
192     * 'DIGITS' flags control whether digits are shaped, and whether from
193     * European to Arabic-Indic or vice-versa.<br>
194     * 'DIGIT_TYPE' flags control whether standard or extended Arabic-Indic
195     * digits are used when performing digit conversion.
196     * @stable ICU 2.0
197     */
198    public ArabicShaping(int options) {
199        this.options = options;
200        if ((options & DIGITS_MASK) > 0x80) {
201            throw new IllegalArgumentException("bad DIGITS options");
202        }
203
204        isLogical = ( (options & TEXT_DIRECTION_MASK) == TEXT_DIRECTION_LOGICAL );
205        /* Validate options */
206        spacesRelativeToTextBeginEnd = ( (options & SPACES_RELATIVE_TO_TEXT_MASK) == SPACES_RELATIVE_TO_TEXT_BEGIN_END );
207        if ( (options&SHAPE_TAIL_TYPE_MASK) == SHAPE_TAIL_NEW_UNICODE){
208            tailChar = NEW_TAIL_CHAR;
209        } else {
210            tailChar = OLD_TAIL_CHAR;
211        }
212    }
213
214    /* Seen Tail options */
215    /**
216     * Memory option: the result must have the same length as the source.
217     * Shaping mode: The SEEN family character will expand into two characters using space near
218     *               the SEEN family character(i.e. the space after the character).
219     *               if there are no spaces found, ArabicShapingException will be thrown
220     *
221     * De-shaping mode: Any Seen character followed by Tail character will be
222     *                  replaced by one cell Seen and a space will replace the Tail.
223     * Affects: Seen options
224     */
225    public static final int SEEN_TWOCELL_NEAR = 0x200000;
226
227    /** Bit mask for Seen memory options. */
228    public static final int SEEN_MASK = 0x700000;
229
230    /* YehHamza options */
231    /**
232     * Memory option: the result must have the same length as the source.
233     * Shaping mode: The YEHHAMZA character will expand into two characters using space near it
234     *              (i.e. the space after the character)
235     *               if there are no spaces found, ArabicShapingException will be thrown
236     *
237     * De-shaping mode: Any Yeh (final or isolated) character followed by Hamza character will be
238     *                  replaced by one cell YehHamza and space will replace the Hamza.
239     * Affects: YehHamza options
240     */
241    public static final int YEHHAMZA_TWOCELL_NEAR  = 0x1000000;
242
243
244    /** Bit mask for YehHamza memory options. */
245    public static final int YEHHAMZA_MASK = 0x3800000;
246
247    /* New Tashkeel options */
248    /**
249     * Memory option: the result must have the same length as the source.
250     * Shaping mode: Tashkeel characters will be replaced by spaces.
251     *               Spaces will be placed at beginning of the buffer
252     *
253     * De-shaping mode: N/A
254     * Affects: Tashkeel options
255     */
256    public static final int TASHKEEL_BEGIN = 0x40000;
257
258    /**
259     * Memory option: the result must have the same length as the source.
260     * Shaping mode: Tashkeel characters will be replaced by spaces.
261     *               Spaces will be placed at end of the buffer
262     *
263     * De-shaping mode: N/A
264     * Affects: Tashkeel options
265     */
266    public static final int TASHKEEL_END = 0x60000;
267
268    /**
269     * Memory option: allow the result to have a different length than the source.
270     * Shaping mode: Tashkeel characters will be removed, buffer length will shrink.
271     * De-shaping mode: N/A
272     *
273     * Affects: Tashkeel options
274     */
275    public static final int TASHKEEL_RESIZE = 0x80000;
276
277    /**
278     * Memory option: the result must have the same length as the source.
279     * Shaping mode: Tashkeel characters will be replaced by Tatweel if it is connected to adjacent
280     *               characters (i.e. shaped on Tatweel) or replaced by space if it is not connected.
281     *
282     * De-shaping mode: N/A
283     * Affects: YehHamza options
284     */
285    public static final int TASHKEEL_REPLACE_BY_TATWEEL = 0xC0000;
286
287    /** Bit mask for Tashkeel replacement with Space or Tatweel memory options. */
288    public static final int TASHKEEL_MASK  = 0xE0000;
289
290    /* Space location Control options */
291    /**
292     * This option effects the meaning of BEGIN and END options. if this option is not used the default
293     * for BEGIN and END will be as following:
294     * The Default (for both Visual LTR, Visual RTL and Logical Text)
295     *           1. BEGIN always refers to the start address of physical memory.
296     *           2. END always refers to the end address of physical memory.
297     *
298     * If this option is used it will swap the meaning of BEGIN and END only for Visual LTR text.
299     *
300     * The affect on BEGIN and END Memory Options will be as following:
301     *    A. BEGIN For Visual LTR text: This will be the beginning (right side) of the visual text
302     *       (corresponding to the physical memory address end, same as END in default behavior)
303     *    B. BEGIN For Logical text: Same as BEGIN in default behavior.
304     *    C. END For Visual LTR text: This will be the end (left side) of the visual text. (corresponding to
305     *      the physical memory address beginning, same as BEGIN in default behavior)
306     *    D. END For Logical text: Same as END in default behavior.
307     * Affects: All LamAlef BEGIN, END and AUTO options.
308     */
309    public static final int SPACES_RELATIVE_TO_TEXT_BEGIN_END = 0x4000000;
310
311    /** Bit mask for swapping BEGIN and END for Visual LTR text */
312    public static final int SPACES_RELATIVE_TO_TEXT_MASK = 0x4000000;
313
314    /**
315     * If this option is used, shaping will use the new Unicode code point for TAIL (i.e. 0xFE73).
316     * If this option is not specified (Default), old unofficial Unicode TAIL code point is used (i.e. 0x200B)
317     * De-shaping will not use this option as it will always search for both the new Unicode code point for the
318     * TAIL (i.e. 0xFE73) or the old unofficial Unicode TAIL code point (i.e. 0x200B) and de-shape the
319     * Seen-Family letter accordingly.
320     *
321     * Shaping Mode: Only shaping.
322     * De-shaping Mode: N/A.
323     * Affects: All Seen options
324     */
325    public static final int SHAPE_TAIL_NEW_UNICODE = 0x8000000;
326
327    /** Bit mask for new Unicode Tail option */
328    public static final int SHAPE_TAIL_TYPE_MASK = 0x8000000;
329
330    /**
331     * Memory option: allow the result to have a different length than the source.
332     * @stable ICU 2.0
333     */
334    public static final int LENGTH_GROW_SHRINK = 0;
335
336    /**
337     * Memory option: allow the result to have a different length than the source.
338     * Affects: LamAlef options
339     * This option is an alias to LENGTH_GROW_SHRINK
340     */
341    public static final int LAMALEF_RESIZE   = 0;
342
343    /**
344     * Memory option: the result must have the same length as the source.
345     * If more room is necessary, then try to consume spaces next to modified characters.
346     * @stable ICU 2.0
347     */
348    public static final int LENGTH_FIXED_SPACES_NEAR = 1;
349
350    /**
351     * Memory option: the result must have the same length as the source.
352     * If more room is necessary, then try to consume spaces next to modified characters.
353     * Affects: LamAlef options
354     * This option is an alias to LENGTH_FIXED_SPACES_NEAR
355     */
356    public static final int LAMALEF_NEAR = 1 ;
357
358    /**
359     * Memory option: the result must have the same length as the source.
360     * If more room is necessary, then try to consume spaces at the end of the text.
361     * @stable ICU 2.0
362     */
363    public static final int LENGTH_FIXED_SPACES_AT_END = 2;
364
365
366    /**
367     * Memory option: the result must have the same length as the source.
368     * If more room is necessary, then try to consume spaces at the end of the text.
369     * Affects: LamAlef options
370     * This option is an alias to LENGTH_FIXED_SPACES_AT_END
371     */
372    public static final int LAMALEF_END = 2;
373
374    /**
375     * Memory option: the result must have the same length as the source.
376     * If more room is necessary, then try to consume spaces at the beginning of the text.
377     * @stable ICU 2.0
378     */
379    public static final int LENGTH_FIXED_SPACES_AT_BEGINNING = 3;
380
381    /**
382     * Memory option: the result must have the same length as the source.
383     * If more room is necessary, then try to consume spaces at the beginning of the text.
384     * Affects: LamAlef options
385     * This option is an alias to LENGTH_FIXED_SPACES_AT_BEGINNING
386     */
387    public static final int LAMALEF_BEGIN = 3;
388
389    /**
390     * Memory option: the result must have the same length as the source.
391     * Shaping Mode: For each LAMALEF character found, expand LAMALEF using space at end.
392     *               If there is no space at end, use spaces at beginning of the buffer. If there
393     *               is no space at beginning of the buffer, use spaces at the near (i.e. the space
394     *               after the LAMALEF character).
395     *
396     * Deshaping Mode: Perform the same function as the flag equals LAMALEF_END.
397     * Affects: LamAlef options
398     */
399    public static final int LAMALEF_AUTO  = 0x10000;
400
401    /**
402     * Bit mask for memory options.
403     * @stable ICU 2.0
404     */
405    public static final int LENGTH_MASK = 0x10003;
406
407    /** Bit mask for LamAlef memory options. */
408
409    public static final int LAMALEF_MASK  = 0x10003;
410
411    /**
412     * Direction indicator: the source is in logical (keyboard) order.
413     * @stable ICU 2.0
414     */
415    public static final int TEXT_DIRECTION_LOGICAL = 0;
416
417    /**
418     * Direction indicator:the source is in visual RTL order,
419     * the rightmost displayed character stored first.
420     * This option is an alias to U_SHAPE_TEXT_DIRECTION_LOGICAL
421     */
422    public static final int TEXT_DIRECTION_VISUAL_RTL = 0;
423
424    /**
425     * Direction indicator: the source is in visual (display) order, that is,
426     * the leftmost displayed character is stored first.
427     * @stable ICU 2.0
428     */
429    public static final int TEXT_DIRECTION_VISUAL_LTR = 4;
430
431    /**
432     * Bit mask for direction indicators.
433     * @stable ICU 2.0
434     */
435    public static final int TEXT_DIRECTION_MASK = 4;
436
437
438    /**
439     * Letter shaping option: do not perform letter shaping.
440     * @stable ICU 2.0
441     */
442    public static final int LETTERS_NOOP = 0;
443
444    /**
445     * Letter shaping option: replace normative letter characters in the U+0600 (Arabic) block,
446     * by shaped ones in the U+FE70 (Presentation Forms B) block. Performs Lam-Alef ligature
447     * substitution.
448     * @stable ICU 2.0
449     */
450    public static final int LETTERS_SHAPE = 8;
451
452    /**
453     * Letter shaping option: replace shaped letter characters in the U+FE70 (Presentation Forms B) block
454     * by normative ones in the U+0600 (Arabic) block.  Converts Lam-Alef ligatures to pairs of Lam and
455     * Alef characters, consuming spaces if required.
456     * @stable ICU 2.0
457     */
458    public static final int LETTERS_UNSHAPE = 0x10;
459
460    /**
461     * Letter shaping option: replace normative letter characters in the U+0600 (Arabic) block,
462     * except for the TASHKEEL characters at U+064B...U+0652, by shaped ones in the U+Fe70
463     * (Presentation Forms B) block.  The TASHKEEL characters will always be converted to
464     * the isolated forms rather than to their correct shape.
465     * @stable ICU 2.0
466     */
467    public static final int LETTERS_SHAPE_TASHKEEL_ISOLATED = 0x18;
468
469    /**
470     * Bit mask for letter shaping options.
471     * @stable ICU 2.0
472     */
473    public static final int LETTERS_MASK = 0x18;
474
475
476    /**
477     * Digit shaping option: do not perform digit shaping.
478     * @stable ICU 2.0
479     */
480    public static final int DIGITS_NOOP = 0;
481
482    /**
483     * Digit shaping option: Replace European digits (U+0030...U+0039) by Arabic-Indic digits.
484     * @stable ICU 2.0
485     */
486    public static final int DIGITS_EN2AN = 0x20;
487
488    /**
489     * Digit shaping option: Replace Arabic-Indic digits by European digits (U+0030...U+0039).
490     * @stable ICU 2.0
491     */
492    public static final int DIGITS_AN2EN = 0x40;
493
494    /**
495     * Digit shaping option:
496     * Replace European digits (U+0030...U+0039) by Arabic-Indic digits
497     * if the most recent strongly directional character
498     * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC).
499     * The initial state at the start of the text is assumed to be not an Arabic,
500     * letter, so European digits at the start of the text will not change.
501     * Compare to DIGITS_ALEN2AN_INIT_AL.
502     * @stable ICU 2.0
503     */
504    public static final int DIGITS_EN2AN_INIT_LR = 0x60;
505
506    /**
507     * Digit shaping option:
508     * Replace European digits (U+0030...U+0039) by Arabic-Indic digits
509     * if the most recent strongly directional character
510     * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC).
511     * The initial state at the start of the text is assumed to be an Arabic,
512     * letter, so European digits at the start of the text will change.
513     * Compare to DIGITS_ALEN2AN_INT_LR.
514     * @stable ICU 2.0
515     */
516    public static final int DIGITS_EN2AN_INIT_AL = 0x80;
517
518    /** Not a valid option value. */
519    //private static final int DIGITS_RESERVED = 0xa0;
520
521    /**
522     * Bit mask for digit shaping options.
523     * @stable ICU 2.0
524     */
525    public static final int DIGITS_MASK = 0xe0;
526
527    /**
528     * Digit type option: Use Arabic-Indic digits (U+0660...U+0669).
529     * @stable ICU 2.0
530     */
531    public static final int DIGIT_TYPE_AN = 0;
532
533    /**
534     * Digit type option: Use Eastern (Extended) Arabic-Indic digits (U+06f0...U+06f9).
535     * @stable ICU 2.0
536     */
537    public static final int DIGIT_TYPE_AN_EXTENDED = 0x100;
538
539    /**
540     * Bit mask for digit type options.
541     * @stable ICU 2.0
542     */
543    public static final int DIGIT_TYPE_MASK = 0x0100; // 0x3f00?
544
545    /**
546     * some constants
547     */
548    private static final char HAMZAFE_CHAR       = '\ufe80';
549    private static final char HAMZA06_CHAR       = '\u0621';
550    private static final char YEH_HAMZA_CHAR     = '\u0626';
551    private static final char YEH_HAMZAFE_CHAR   = '\uFE89';
552    private static final char LAMALEF_SPACE_SUB  = '\uffff';
553    private static final char TASHKEEL_SPACE_SUB = '\ufffe';
554    private static final char LAM_CHAR      = '\u0644';
555    private static final char SPACE_CHAR    = '\u0020';
556    private static final char SPACE_CHAR_FOR_LAMALEF = '\ufeff'; // XXX: tweak for TextLine use
557    private static final char SHADDA_CHAR   = '\uFE7C';
558    private static final char TATWEEL_CHAR  = '\u0640';
559    private static final char SHADDA_TATWEEL_CHAR = '\uFE7D';
560    private static final char NEW_TAIL_CHAR = '\uFE73';
561    private static final char OLD_TAIL_CHAR = '\u200B';
562    private static final int SHAPE_MODE      = 0;
563    private static final int DESHAPE_MODE    = 1;
564
565    /**
566     * @stable ICU 2.0
567     */
568    public boolean equals(Object rhs) {
569        return rhs != null &&
570            rhs.getClass() == ArabicShaping.class &&
571            options == ((ArabicShaping)rhs).options;
572    }
573
574    /**
575     * @stable ICU 2.0
576     */
577     ///CLOVER:OFF
578    public int hashCode() {
579        return options;
580    }
581
582    /**
583     * @stable ICU 2.0
584     */
585    public String toString() {
586        StringBuffer buf = new StringBuffer(super.toString());
587        buf.append('[');
588
589        switch (options & LAMALEF_MASK) {
590        case LAMALEF_RESIZE: buf.append("LamAlef resize"); break;
591        case LAMALEF_NEAR: buf.append("LamAlef spaces at near"); break;
592        case LAMALEF_BEGIN: buf.append("LamAlef spaces at begin"); break;
593        case LAMALEF_END: buf.append("LamAlef spaces at end"); break;
594        case LAMALEF_AUTO: buf.append("lamAlef auto"); break;
595        }
596        switch (options & TEXT_DIRECTION_MASK) {
597        case TEXT_DIRECTION_LOGICAL: buf.append(", logical"); break;
598        case TEXT_DIRECTION_VISUAL_LTR: buf.append(", visual"); break;
599        }
600        switch (options & LETTERS_MASK) {
601        case LETTERS_NOOP: buf.append(", no letter shaping"); break;
602        case LETTERS_SHAPE: buf.append(", shape letters"); break;
603        case LETTERS_SHAPE_TASHKEEL_ISOLATED: buf.append(", shape letters tashkeel isolated"); break;
604        case LETTERS_UNSHAPE: buf.append(", unshape letters"); break;
605        }
606        switch (options & SEEN_MASK) {
607        case SEEN_TWOCELL_NEAR: buf.append(", Seen at near"); break;
608        }
609        switch (options & YEHHAMZA_MASK) {
610        case YEHHAMZA_TWOCELL_NEAR: buf.append(", Yeh Hamza at near"); break;
611        }
612        switch (options & TASHKEEL_MASK) {
613        case TASHKEEL_BEGIN: buf.append(", Tashkeel at begin"); break;
614        case TASHKEEL_END: buf.append(", Tashkeel at end"); break;
615        case TASHKEEL_REPLACE_BY_TATWEEL: buf.append(", Tashkeel replace with tatweel"); break;
616        case TASHKEEL_RESIZE: buf.append(", Tashkeel resize"); break;
617        }
618
619        switch (options & DIGITS_MASK) {
620        case DIGITS_NOOP: buf.append(", no digit shaping"); break;
621        case DIGITS_EN2AN: buf.append(", shape digits to AN"); break;
622        case DIGITS_AN2EN: buf.append(", shape digits to EN"); break;
623        case DIGITS_EN2AN_INIT_LR: buf.append(", shape digits to AN contextually: default EN"); break;
624        case DIGITS_EN2AN_INIT_AL: buf.append(", shape digits to AN contextually: default AL"); break;
625        }
626        switch (options & DIGIT_TYPE_MASK) {
627        case DIGIT_TYPE_AN: buf.append(", standard Arabic-Indic digits"); break;
628        case DIGIT_TYPE_AN_EXTENDED: buf.append(", extended Arabic-Indic digits"); break;
629        }
630        buf.append("]");
631
632        return buf.toString();
633    }
634    ///CLOVER:ON
635
636    //
637    // ported api
638    //
639
640    private static final int IRRELEVANT = 4;
641    private static final int LAMTYPE = 16;
642    private static final int ALEFTYPE = 32;
643
644    private static final int LINKR = 1;
645    private static final int LINKL = 2;
646    private static final int LINK_MASK = 3;
647
648    private static final int irrelevantPos[] = {
649        0x0, 0x2, 0x4, 0x6, 0x8, 0xA, 0xC, 0xE
650    };
651
652/*
653    private static final char convertLamAlef[] =  {
654        '\u0622', // FEF5
655        '\u0622', // FEF6
656        '\u0623', // FEF7
657        '\u0623', // FEF8
658        '\u0625', // FEF9
659        '\u0625', // FEFA
660        '\u0627', // FEFB
661        '\u0627'  // FEFC
662    };
663*/
664
665    private static final int tailFamilyIsolatedFinal[] = {
666        /* FEB1 */ 1,
667        /* FEB2 */ 1,
668        /* FEB3 */ 0,
669        /* FEB4 */ 0,
670        /* FEB5 */ 1,
671        /* FEB6 */ 1,
672        /* FEB7 */ 0,
673        /* FEB8 */ 0,
674        /* FEB9 */ 1,
675        /* FEBA */ 1,
676        /* FEBB */ 0,
677        /* FEBC */ 0,
678        /* FEBD */ 1,
679        /* FEBE */ 1
680    };
681
682    private static final int tashkeelMedial[] = {
683        /* FE70 */ 0,
684        /* FE71 */ 1,
685        /* FE72 */ 0,
686        /* FE73 */ 0,
687        /* FE74 */ 0,
688        /* FE75 */ 0,
689        /* FE76 */ 0,
690        /* FE77 */ 1,
691        /* FE78 */ 0,
692        /* FE79 */ 1,
693        /* FE7A */ 0,
694        /* FE7B */ 1,
695        /* FE7C */ 0,
696        /* FE7D */ 1,
697        /* FE7E */ 0,
698        /* FE7F */ 1
699    };
700
701    private static final char yehHamzaToYeh[] =
702    {
703    /* isolated*/ 0xFEEF,
704    /* final   */ 0xFEF0
705    };
706
707    private static final char convertNormalizedLamAlef[] = {
708        '\u0622', // 065C
709        '\u0623', // 065D
710        '\u0625', // 065E
711        '\u0627', // 065F
712    };
713
714    private static final int[] araLink = {
715        1           + 32 + 256 * 0x11,  /*0x0622*/
716        1           + 32 + 256 * 0x13,  /*0x0623*/
717        1                + 256 * 0x15,  /*0x0624*/
718        1           + 32 + 256 * 0x17,  /*0x0625*/
719        1 + 2            + 256 * 0x19,  /*0x0626*/
720        1           + 32 + 256 * 0x1D,  /*0x0627*/
721        1 + 2            + 256 * 0x1F,  /*0x0628*/
722        1                + 256 * 0x23,  /*0x0629*/
723        1 + 2            + 256 * 0x25,  /*0x062A*/
724        1 + 2            + 256 * 0x29,  /*0x062B*/
725        1 + 2            + 256 * 0x2D,  /*0x062C*/
726        1 + 2            + 256 * 0x31,  /*0x062D*/
727        1 + 2            + 256 * 0x35,  /*0x062E*/
728        1                + 256 * 0x39,  /*0x062F*/
729        1                + 256 * 0x3B,  /*0x0630*/
730        1                + 256 * 0x3D,  /*0x0631*/
731        1                + 256 * 0x3F,  /*0x0632*/
732        1 + 2            + 256 * 0x41,  /*0x0633*/
733        1 + 2            + 256 * 0x45,  /*0x0634*/
734        1 + 2            + 256 * 0x49,  /*0x0635*/
735        1 + 2            + 256 * 0x4D,  /*0x0636*/
736        1 + 2            + 256 * 0x51,  /*0x0637*/
737        1 + 2            + 256 * 0x55,  /*0x0638*/
738        1 + 2            + 256 * 0x59,  /*0x0639*/
739        1 + 2            + 256 * 0x5D,  /*0x063A*/
740        0, 0, 0, 0, 0,                  /*0x063B-0x063F*/
741        1 + 2,                          /*0x0640*/
742        1 + 2            + 256 * 0x61,  /*0x0641*/
743        1 + 2            + 256 * 0x65,  /*0x0642*/
744        1 + 2            + 256 * 0x69,  /*0x0643*/
745        1 + 2       + 16 + 256 * 0x6D,  /*0x0644*/
746        1 + 2            + 256 * 0x71,  /*0x0645*/
747        1 + 2            + 256 * 0x75,  /*0x0646*/
748        1 + 2            + 256 * 0x79,  /*0x0647*/
749        1                + 256 * 0x7D,  /*0x0648*/
750        1                + 256 * 0x7F,  /*0x0649*/
751        1 + 2            + 256 * 0x81,  /*0x064A*/
752        4, 4, 4, 4,                     /*0x064B-0x064E*/
753        4, 4, 4, 4,                     /*0x064F-0x0652*/
754        4, 4, 4, 0, 0,                  /*0x0653-0x0657*/
755        0, 0, 0, 0,                     /*0x0658-0x065B*/
756        1                + 256 * 0x85,  /*0x065C*/
757        1                + 256 * 0x87,  /*0x065D*/
758        1                + 256 * 0x89,  /*0x065E*/
759        1                + 256 * 0x8B,  /*0x065F*/
760        0, 0, 0, 0, 0,                  /*0x0660-0x0664*/
761        0, 0, 0, 0, 0,                  /*0x0665-0x0669*/
762        0, 0, 0, 0, 0, 0,               /*0x066A-0x066F*/
763        4,                              /*0x0670*/
764        0,                              /*0x0671*/
765        1           + 32,               /*0x0672*/
766        1           + 32,               /*0x0673*/
767        0,                              /*0x0674*/
768        1           + 32,               /*0x0675*/
769        1, 1,                           /*0x0676-0x0677*/
770        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x0678-0x067D*/
771        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x067E-0x0683*/
772        1+2, 1+2, 1+2, 1+2,             /*0x0684-0x0687*/
773        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   /*0x0688-0x0691*/
774        1, 1, 1, 1, 1, 1, 1, 1,         /*0x0692-0x0699*/
775        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x069A-0x06A3*/
776        1+2, 1+2, 1+2, 1+2,             /*0x069A-0x06A3*/
777        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x06A4-0x06AD*/
778        1+2, 1+2, 1+2, 1+2,             /*0x06A4-0x06AD*/
779        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x06AE-0x06B7*/
780        1+2, 1+2, 1+2, 1+2,             /*0x06AE-0x06B7*/
781        1+2, 1+2, 1+2, 1+2, 1+2, 1+2,   /*0x06B8-0x06BF*/
782        1+2, 1+2,                       /*0x06B8-0x06BF*/
783        1,                              /*0x06C0*/
784        1+2,                            /*0x06C1*/
785        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   /*0x06C2-0x06CB*/
786        1+2,                            /*0x06CC*/
787        1,                              /*0x06CD*/
788        1+2, 1+2, 1+2, 1+2,             /*0x06CE-0x06D1*/
789        1, 1                            /*0x06D2-0x06D3*/
790    };
791
792    private static final int[] presLink = {
793        1 + 2,                        /*0xFE70*/
794        1 + 2,                        /*0xFE71*/
795        1 + 2, 0, 1+ 2, 0, 1+ 2,      /*0xFE72-0xFE76*/
796        1 + 2,                        /*0xFE77*/
797        1+ 2, 1 + 2, 1+2, 1 + 2,      /*0xFE78-0xFE81*/
798        1+ 2, 1 + 2, 1+2, 1 + 2,      /*0xFE82-0xFE85*/
799        0, 0 + 32, 1 + 32, 0 + 32,    /*0xFE86-0xFE89*/
800        1 + 32, 0, 1,  0 + 32,        /*0xFE8A-0xFE8D*/
801        1 + 32, 0, 2,  1 + 2,         /*0xFE8E-0xFE91*/
802        1, 0 + 32, 1 + 32, 0,         /*0xFE92-0xFE95*/
803        2, 1 + 2, 1, 0,               /*0xFE96-0xFE99*/
804        1, 0, 2, 1 + 2,               /*0xFE9A-0xFE9D*/
805        1, 0, 2, 1 + 2,               /*0xFE9E-0xFEA1*/
806        1, 0, 2, 1 + 2,               /*0xFEA2-0xFEA5*/
807        1, 0, 2, 1 + 2,               /*0xFEA6-0xFEA9*/
808        1, 0, 2, 1 + 2,               /*0xFEAA-0xFEAD*/
809        1, 0, 1, 0,                   /*0xFEAE-0xFEB1*/
810        1, 0, 1, 0,                   /*0xFEB2-0xFEB5*/
811        1, 0, 2, 1+2,                 /*0xFEB6-0xFEB9*/
812        1, 0, 2, 1+2,                 /*0xFEBA-0xFEBD*/
813        1, 0, 2, 1+2,                 /*0xFEBE-0xFEC1*/
814        1, 0, 2, 1+2,                 /*0xFEC2-0xFEC5*/
815        1, 0, 2, 1+2,                 /*0xFEC6-0xFEC9*/
816        1, 0, 2, 1+2,                 /*0xFECA-0xFECD*/
817        1, 0, 2, 1+2,                 /*0xFECE-0xFED1*/
818        1, 0, 2, 1+2,                 /*0xFED2-0xFED5*/
819        1, 0, 2, 1+2,                 /*0xFED6-0xFED9*/
820        1, 0, 2, 1+2,                 /*0xFEDA-0xFEDD*/
821        1, 0, 2, 1+2,                 /*0xFEDE-0xFEE1*/
822        1, 0 + 16, 2 + 16, 1 + 2 +16, /*0xFEE2-0xFEE5*/
823        1 + 16, 0, 2, 1+2,            /*0xFEE6-0xFEE9*/
824        1, 0, 2, 1+2,                 /*0xFEEA-0xFEED*/
825        1, 0, 2, 1+2,                 /*0xFEEE-0xFEF1*/
826        1, 0, 1, 0,                   /*0xFEF2-0xFEF5*/
827        1, 0, 2, 1+2,                 /*0xFEF6-0xFEF9*/
828        1, 0, 1, 0,                   /*0xFEFA-0xFEFD*/
829        1, 0, 1, 0,
830        1
831    };
832
833    private static int[] convertFEto06 = {
834        /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
835        /*FE7*/   0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
836        /*FE8*/   0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
837        /*FE9*/   0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
838        /*FEA*/   0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
839        /*FEB*/   0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
840        /*FEC*/   0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
841        /*FED*/   0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
842        /*FEE*/   0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
843        /*FEF*/   0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
844    };
845
846    private static final int shapeTable[][][] = {
847        { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
848        { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
849        { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
850        { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
851    };
852
853    /*
854     * This function shapes European digits to Arabic-Indic digits
855     * in-place, writing over the input characters.  Data is in visual
856     * order.
857     */
858    private void shapeToArabicDigitsWithContext(char[] dest,
859                                                int start,
860                                                int length,
861                                                char digitBase,
862                                                boolean lastStrongWasAL) {
863        digitBase -= '0'; // move common adjustment out of loop
864
865        for(int i = start + length; --i >= start;) {
866            char ch = dest[i];
867            switch (Character.getDirectionality(ch)) {
868            case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
869            case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
870                lastStrongWasAL = false;
871                break;
872            case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
873                lastStrongWasAL = true;
874                break;
875            case Character.DIRECTIONALITY_EUROPEAN_NUMBER:
876                if (lastStrongWasAL && ch <= '\u0039') {
877                    dest[i] = (char)(ch + digitBase);
878                }
879                break;
880            default:
881                break;
882            }
883        }
884    }
885
886    /*
887     * Name    : invertBuffer
888     * Function: This function inverts the buffer, it's used
889     *           in case the user specifies the buffer to be
890     *           TEXT_DIRECTION_LOGICAL
891     */
892    private static void invertBuffer(char[] buffer,
893                                     int start,
894                                     int length) {
895
896        for(int i = start, j = start + length - 1; i < j; i++, --j) {
897            char temp = buffer[i];
898            buffer[i] = buffer[j];
899            buffer[j] = temp;
900        }
901    }
902
903    /*
904     * Name    : changeLamAlef
905     * Function: Converts the Alef characters into an equivalent
906     *           LamAlef location in the 0x06xx Range, this is an
907     *           intermediate stage in the operation of the program
908     *           later it'll be converted into the 0xFExx LamAlefs
909     *           in the shaping function.
910     */
911    private static char changeLamAlef(char ch) {
912        switch(ch) {
913        case '\u0622': return '\u065C';
914        case '\u0623': return '\u065D';
915        case '\u0625': return '\u065E';
916        case '\u0627': return '\u065F';
917        default:  return '\u0000'; // not a lamalef
918        }
919    }
920
921    /*
922     * Name    : specialChar
923     * Function: Special Arabic characters need special handling in the shapeUnicode
924     *           function, this function returns 1 or 2 for these special characters
925     */
926    private static int specialChar(char ch) {
927        if ((ch > '\u0621' && ch < '\u0626') ||
928            (ch == '\u0627') ||
929            (ch > '\u062E' && ch < '\u0633') ||
930            (ch > '\u0647' && ch < '\u064A') ||
931            (ch == '\u0629')) {
932            return 1;
933        } else if (ch >= '\u064B' && ch<= '\u0652') {
934            return 2;
935        } else if (ch >= 0x0653 && ch <= 0x0655 ||
936                   ch == 0x0670 ||
937                   ch >= 0xFE70 && ch <= 0xFE7F) {
938            return 3;
939        } else {
940            return 0;
941        }
942    }
943
944    /*
945     * Name    : getLink
946     * Function: Resolves the link between the characters as
947     *           Arabic characters have four forms :
948     *           Isolated, Initial, Middle and Final Form
949     */
950    private static int getLink(char ch) {
951        if (ch >= '\u0622' && ch <= '\u06D3') {
952            return araLink[ch - '\u0622'];
953        } else if (ch == '\u200D') {
954            return 3;
955        } else if (ch >= '\u206D' && ch <= '\u206F') {
956            return 4;
957        } else if (ch >= '\uFE70' && ch <= '\uFEFC') {
958            return presLink[ch - '\uFE70'];
959        } else {
960            return 0;
961        }
962    }
963
964    /*
965     * Name    : countSpaces
966     * Function: Counts the number of spaces
967     *           at each end of the logical buffer
968     */
969    private static int countSpacesLeft(char[] dest,
970                                       int start,
971                                       int count) {
972        for (int i = start, e = start + count; i < e; ++i) {
973            if (dest[i] != SPACE_CHAR) {
974                return i - start;
975            }
976        }
977        return count;
978    }
979
980    private static int countSpacesRight(char[] dest,
981                                        int start,
982                                        int count) {
983
984        for (int i = start + count; --i >= start;) {
985            if (dest[i] != SPACE_CHAR) {
986                return start + count - 1 - i;
987            }
988        }
989        return count;
990    }
991
992    /*
993     * Name    : isTashkeelChar
994     * Function: Returns true for Tashkeel characters else return false
995     */
996    private static boolean isTashkeelChar(char ch) {
997        return ( ch >='\u064B' && ch <= '\u0652' );
998    }
999
1000    /*
1001     *Name     : isSeenTailFamilyChar
1002     *Function : returns 1 if the character is a seen family isolated character
1003     *           in the FE range otherwise returns 0
1004     */
1005
1006    private static int isSeenTailFamilyChar(char ch) {
1007        if (ch >= 0xfeb1 && ch < 0xfebf){
1008             return tailFamilyIsolatedFinal [ch - 0xFEB1];
1009        } else {
1010             return 0;
1011        }
1012    }
1013
1014     /* Name     : isSeenFamilyChar
1015      * Function : returns 1 if the character is a seen family character in the Unicode
1016      *            06 range otherwise returns 0
1017     */
1018
1019    private static int isSeenFamilyChar(char  ch){
1020        if (ch >= 0x633 && ch <= 0x636){
1021            return 1;
1022        }else {
1023            return 0;
1024        }
1025    }
1026
1027    /*
1028     *Name     : isTailChar
1029     *Function : returns true if the character matches one of the tail characters
1030     *           (0xfe73 or 0x200b) otherwise returns false
1031     */
1032
1033    private static boolean isTailChar(char ch) {
1034        if(ch == OLD_TAIL_CHAR || ch == NEW_TAIL_CHAR){
1035                return true;
1036        }else{
1037                return false;
1038        }
1039    }
1040
1041    /*
1042     *Name     : isAlefMaksouraChar
1043     *Function : returns true if the character is a Alef Maksoura Final or isolated
1044     *           otherwise returns false
1045     */
1046    private static boolean isAlefMaksouraChar(char ch) {
1047        return ( (ch == 0xFEEF) || ( ch == 0xFEF0) || (ch == 0x0649));
1048    }
1049
1050    /*
1051     * Name     : isYehHamzaChar
1052     * Function : returns true if the character is a yehHamza isolated or yehhamza
1053     *            final is found otherwise returns false
1054     */
1055    private static boolean isYehHamzaChar(char ch) {
1056        if((ch==0xFE89)||(ch==0xFE8A)){
1057            return true;
1058        }else{
1059            return false;
1060        }
1061    }
1062
1063    /*
1064     *Name     : isTashkeelCharFE
1065     *Function : Returns true for Tashkeel characters in FE range else return false
1066     */
1067
1068    private static boolean isTashkeelCharFE(char ch) {
1069        return ( ch!=0xFE75 &&(ch>=0xFE70 && ch<= 0xFE7F) );
1070    }
1071
1072    /*
1073     * Name: isTashkeelOnTatweelChar
1074     * Function: Checks if the Tashkeel Character is on Tatweel or not,if the
1075     *           Tashkeel on tatweel (FE range), it returns 1 else if the
1076     *           Tashkeel with shadda on tatweel (FC range)return 2 otherwise
1077     *           returns 0
1078     */
1079    private static int isTashkeelOnTatweelChar(char ch){
1080        if (ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75 && ch != SHADDA_TATWEEL_CHAR)
1081        {
1082            return tashkeelMedial [ch - 0xFE70];
1083        } else if( (ch >= 0xfcf2 && ch <= 0xfcf4) || (ch == SHADDA_TATWEEL_CHAR)) {
1084            return 2;
1085        } else {
1086            return 0;
1087        }
1088    }
1089
1090    /*
1091     * Name: isIsolatedTashkeelChar
1092     * Function: Checks if the Tashkeel Character is in the isolated form
1093     *           (i.e. Unicode FE range) returns 1 else if the Tashkeel
1094     *           with shadda is in the isolated form (i.e. Unicode FC range)
1095     *           returns 1 otherwise returns 0
1096     */
1097    private static int isIsolatedTashkeelChar(char ch){
1098        if (ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75){
1099            return (1 - tashkeelMedial [ch - 0xFE70]);
1100        } else if(ch >= 0xfc5e && ch <= 0xfc63){
1101            return 1;
1102        } else{
1103            return 0;
1104        }
1105    }
1106
1107    /*
1108     * Name    : isAlefChar
1109     * Function: Returns 1 for Alef characters else return 0
1110     */
1111    private static boolean isAlefChar(char ch) {
1112        return ch == '\u0622' || ch == '\u0623' || ch == '\u0625' || ch == '\u0627';
1113    }
1114
1115    /*
1116     * Name    : isLamAlefChar
1117     * Function: Returns true for LamAlef characters else return false
1118     */
1119    private static boolean isLamAlefChar(char ch) {
1120        return ch >= '\uFEF5' && ch <= '\uFEFC';
1121    }
1122
1123    private static boolean isNormalizedLamAlefChar(char ch) {
1124        return ch >= '\u065C' && ch <= '\u065F';
1125    }
1126
1127    /*
1128     * Name    : calculateSize
1129     * Function: This function calculates the destSize to be used in preflighting
1130     *           when the destSize is equal to 0
1131     */
1132    private int calculateSize(char[] source,
1133                              int sourceStart,
1134                              int sourceLength) {
1135
1136        int destSize = sourceLength;
1137
1138        switch (options & LETTERS_MASK) {
1139        case LETTERS_SHAPE:
1140        case LETTERS_SHAPE_TASHKEEL_ISOLATED:
1141            if (isLogical) {
1142                for (int i = sourceStart, e = sourceStart + sourceLength - 1; i < e; ++i) {
1143                    if ((source[i] == LAM_CHAR && isAlefChar(source[i+1])) || isTashkeelCharFE(source[i])){
1144                        --destSize;
1145                    }
1146                }
1147            } else { // visual
1148                for(int i = sourceStart + 1, e = sourceStart + sourceLength; i < e; ++i) {
1149                    if ((source[i] == LAM_CHAR && isAlefChar(source[i-1])) || isTashkeelCharFE(source[i])) {
1150                        --destSize;
1151                    }
1152                }
1153            }
1154            break;
1155
1156        case LETTERS_UNSHAPE:
1157            for(int i = sourceStart, e = sourceStart + sourceLength; i < e; ++i) {
1158                if (isLamAlefChar(source[i])) {
1159                    destSize++;
1160                }
1161            }
1162            break;
1163
1164        default:
1165            break;
1166        }
1167
1168        return destSize;
1169    }
1170
1171
1172    /*
1173     * Name    : countSpaceSub
1174     * Function: Counts number of times the subChar appears in the array
1175     */
1176    public static int countSpaceSub(char [] dest,int length, char subChar){
1177        int i = 0;
1178        int count = 0;
1179        while (i < length) {
1180          if (dest[i] == subChar) {
1181              count++;
1182              }
1183          i++;
1184        }
1185        return count;
1186    }
1187
1188    /*
1189     * Name    : shiftArray
1190     * Function: Shifts characters to replace space sub characters
1191     */
1192    public static void shiftArray(char [] dest,int start, int e, char subChar){
1193        int w = e;
1194        int r = e;
1195        while (--r >= start) {
1196          char ch = dest[r];
1197          if (ch != subChar) {
1198            --w;
1199            if (w != r) {
1200              dest[w] = ch;
1201            }
1202          }
1203        }
1204   }
1205
1206    /*
1207     * Name    : flipArray
1208     * Function: inverts array, so that start becomes end and vice versa
1209     */
1210      public static int flipArray(char [] dest, int start, int e, int w){
1211        int r;
1212        if (w > start) {
1213        // shift, assume small buffer size so don't use arraycopy
1214          r = w;
1215          w = start;
1216          while (r < e) {
1217            dest[w++] = dest[r++];
1218           }
1219         } else {
1220             w = e;
1221         }
1222        return w;
1223      }
1224
1225    /*
1226     * Name     : handleTashkeelWithTatweel
1227     * Function : Replaces Tashkeel as following:
1228     *            Case 1 :if the Tashkeel on tatweel, replace it with Tatweel.
1229     *            Case 2 :if the Tashkeel aggregated with Shadda on Tatweel, replace
1230     *                   it with Shadda on Tatweel.
1231     *            Case 3: if the Tashkeel is isolated replace it with Space.
1232     *
1233     */
1234    private static int handleTashkeelWithTatweel(char[] dest, int sourceLength) {
1235                     int i;
1236                     for(i = 0; i < sourceLength; i++){
1237                         if((isTashkeelOnTatweelChar(dest[i]) == 1)){
1238                             dest[i] = TATWEEL_CHAR;
1239                        }else if((isTashkeelOnTatweelChar(dest[i]) == 2)){
1240                             dest[i] = SHADDA_TATWEEL_CHAR;
1241                        }else if((isIsolatedTashkeelChar(dest[i])==1) && dest[i] != SHADDA_CHAR){
1242                             dest[i] = SPACE_CHAR;
1243                        }
1244                     }
1245                     return sourceLength;
1246    }
1247
1248    /*
1249     *Name     : handleGeneratedSpaces
1250     *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
1251     *           and Tashkeel to space.
1252     *           handleGeneratedSpaces function puts these generated spaces
1253     *           according to the options the user specifies. LamAlef and Tashkeel
1254     *           spaces can be replaced at begin, at end, at near or decrease the
1255     *           buffer size.
1256     *
1257     *           There is also Auto option for LamAlef and tashkeel, which will put
1258     *           the spaces at end of the buffer (or end of text if the user used
1259     *           the option SPACES_RELATIVE_TO_TEXT_BEGIN_END).
1260     *
1261     *           If the text type was visual_LTR and the option
1262     *           SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected the END
1263     *           option will place the space at the beginning of the buffer and
1264     *           BEGIN will place the space at the end of the buffer.
1265     */
1266  private int handleGeneratedSpaces(char[] dest,
1267            int start,
1268            int length) {
1269
1270      int lenOptionsLamAlef = options & LAMALEF_MASK;
1271      int lenOptionsTashkeel = options & TASHKEEL_MASK;
1272      boolean lamAlefOn = false;
1273      boolean tashkeelOn = false;
1274
1275      if (!isLogical & !spacesRelativeToTextBeginEnd) {
1276          switch (lenOptionsLamAlef) {
1277          case LAMALEF_BEGIN: lenOptionsLamAlef = LAMALEF_END; break;
1278          case LAMALEF_END: lenOptionsLamAlef = LAMALEF_BEGIN; break;
1279          default: break;
1280         }
1281          switch (lenOptionsTashkeel){
1282          case TASHKEEL_BEGIN: lenOptionsTashkeel = TASHKEEL_END; break;
1283          case TASHKEEL_END: lenOptionsTashkeel = TASHKEEL_BEGIN; break;
1284          default: break;
1285          }
1286        }
1287
1288
1289      if (lenOptionsLamAlef == LAMALEF_NEAR) {
1290          for (int i = start, e = i + length; i < e; ++i) {
1291              if (dest[i] == LAMALEF_SPACE_SUB) {
1292                  dest[i] = SPACE_CHAR_FOR_LAMALEF;
1293              }
1294          }
1295
1296      } else {
1297
1298          final int e = start + length;
1299          int wL = countSpaceSub(dest, length, LAMALEF_SPACE_SUB);
1300          int wT = countSpaceSub(dest, length, TASHKEEL_SPACE_SUB);
1301
1302          if (lenOptionsLamAlef == LAMALEF_END){
1303            lamAlefOn = true;
1304          }
1305          if (lenOptionsTashkeel == TASHKEEL_END){
1306            tashkeelOn = true;
1307          }
1308
1309
1310          if (lamAlefOn && (lenOptionsLamAlef == LAMALEF_END)) {
1311            shiftArray(dest, start, e, LAMALEF_SPACE_SUB);
1312            while (wL > start) {
1313                dest[--wL] = SPACE_CHAR;
1314            }
1315          }
1316
1317          if (tashkeelOn && (lenOptionsTashkeel == TASHKEEL_END)){
1318            shiftArray(dest, start, e, TASHKEEL_SPACE_SUB);
1319            while (wT > start) {
1320                 dest[--wT] = SPACE_CHAR;
1321            }
1322          }
1323
1324          lamAlefOn = false;
1325          tashkeelOn = false;
1326
1327          if (lenOptionsLamAlef == LAMALEF_RESIZE){
1328            lamAlefOn = true;
1329          }
1330          if (lenOptionsTashkeel == TASHKEEL_RESIZE){
1331            tashkeelOn = true;
1332          }
1333
1334          if (lamAlefOn && (lenOptionsLamAlef == LAMALEF_RESIZE)){
1335              shiftArray(dest, start, e, LAMALEF_SPACE_SUB);
1336              wL = flipArray(dest,start,e, wL);
1337              length = wL - start;
1338          }
1339          if (tashkeelOn && (lenOptionsTashkeel == TASHKEEL_RESIZE)) {
1340              shiftArray(dest, start, e, TASHKEEL_SPACE_SUB);
1341              wT = flipArray(dest,start,e, wT);
1342              length = wT - start;
1343          }
1344
1345          lamAlefOn = false;
1346          tashkeelOn = false;
1347
1348          if ((lenOptionsLamAlef == LAMALEF_BEGIN) ||
1349              (lenOptionsLamAlef == LAMALEF_AUTO)){
1350                lamAlefOn = true;
1351          }
1352          if (lenOptionsTashkeel == TASHKEEL_BEGIN){
1353                tashkeelOn = true;
1354          }
1355
1356          if (lamAlefOn && ((lenOptionsLamAlef == LAMALEF_BEGIN)||
1357                            (lenOptionsLamAlef == LAMALEF_AUTO))) { // spaces at beginning
1358              shiftArray(dest, start, e, LAMALEF_SPACE_SUB);
1359               wL = flipArray(dest,start,e, wL);
1360                  while (wL < e) {
1361                      dest[wL++] = SPACE_CHAR;
1362                  }
1363              }
1364              if(tashkeelOn && (lenOptionsTashkeel == TASHKEEL_BEGIN)){
1365               shiftArray(dest, start, e, TASHKEEL_SPACE_SUB);
1366               wT = flipArray(dest,start,e, wT);
1367                  while (wT < e) {
1368                      dest[wT++] = SPACE_CHAR;
1369                  }
1370              }
1371           }
1372
1373      return length;
1374  }
1375
1376
1377  /*
1378   *Name     :expandCompositCharAtBegin
1379   *Function :Expands the LamAlef character to Lam and Alef consuming the required
1380   *         space from beginning of the buffer. If the text type was visual_LTR
1381   *         and the option SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected
1382   *         the spaces will be located at end of buffer.
1383   *         If there are no spaces to expand the LamAlef, an exception is thrown.
1384*/
1385 private boolean expandCompositCharAtBegin(char[] dest,int start, int length,
1386                            int lacount) {
1387     boolean spaceNotFound = false;
1388
1389     if (lacount > countSpacesRight(dest, start, length)) {
1390         spaceNotFound = true;
1391         return spaceNotFound;
1392     }
1393     for (int r = start + length - lacount, w = start + length; --r >= start;) {
1394         char ch = dest[r];
1395         if (isNormalizedLamAlefChar(ch)) {
1396             dest[--w] = LAM_CHAR;
1397             dest[--w] = convertNormalizedLamAlef[ch - '\u065C'];
1398         } else {
1399             dest[--w] = ch;
1400         }
1401     }
1402     return spaceNotFound;
1403
1404  }
1405
1406  /*
1407   *Name     : expandCompositCharAtEnd
1408   *Function : Expands the LamAlef character to Lam and Alef consuming the
1409   *           required space from end of the buffer. If the text type was
1410   *           Visual LTR and the option SPACES_RELATIVE_TO_TEXT_BEGIN_END
1411   *           was used, the spaces will be consumed from begin of buffer. If
1412   *           there are no spaces to expand the LamAlef, an exception is thrown.
1413   */
1414
1415  private boolean  expandCompositCharAtEnd(char[] dest,int start, int length,
1416                          int lacount){
1417      boolean spaceNotFound = false;
1418
1419      if (lacount > countSpacesLeft(dest, start, length)) {
1420          spaceNotFound = true;
1421          return spaceNotFound;
1422      }
1423      for (int r = start + lacount, w = start, e = start + length; r < e; ++r) {
1424          char ch = dest[r];
1425          if (isNormalizedLamAlefChar(ch)) {
1426              dest[w++] = convertNormalizedLamAlef[ch - '\u065C'];
1427              dest[w++] = LAM_CHAR;
1428          } else {
1429              dest[w++] = ch;
1430          }
1431      }
1432      return spaceNotFound;
1433  }
1434
1435  /*
1436   *Name     : expandCompositCharAtNear
1437   *Function : Expands the LamAlef character into Lam + Alef, YehHamza character
1438   *           into Yeh + Hamza, SeenFamily character into SeenFamily character
1439   *           + Tail, while consuming the space next to the character.
1440   */
1441
1442  private boolean expandCompositCharAtNear(char[] dest,int start, int length,
1443                                       int yehHamzaOption, int seenTailOption, int lamAlefOption){
1444
1445      boolean spaceNotFound = false;
1446
1447
1448
1449      if (isNormalizedLamAlefChar(dest[start])) {
1450          spaceNotFound = true;
1451          return spaceNotFound;
1452      }
1453      for (int i = start + length; --i >=start;) {
1454          char ch = dest[i];
1455          if (lamAlefOption == 1 && isNormalizedLamAlefChar(ch)) {
1456              if (i>start &&dest[i-1] == SPACE_CHAR) {
1457                  dest[i] = LAM_CHAR;
1458                  dest[--i] = convertNormalizedLamAlef[ch - '\u065C'];
1459              } else {
1460                  spaceNotFound = true;
1461                  return spaceNotFound;
1462              }
1463          }else if(seenTailOption == 1 && isSeenTailFamilyChar(ch) == 1){
1464              if(i>start &&dest[i-1] == SPACE_CHAR){
1465                  dest[i-1] = tailChar;
1466              } else{
1467                  spaceNotFound = true;
1468                  return spaceNotFound;
1469              }
1470          }else if(yehHamzaOption == 1 && isYehHamzaChar(ch)){
1471
1472               if(i>start &&dest[i-1] == SPACE_CHAR){
1473                  dest[i] = yehHamzaToYeh[ch - YEH_HAMZAFE_CHAR];
1474                  dest[i-1] = HAMZAFE_CHAR;
1475              }else{
1476                  spaceNotFound = true;
1477                  return spaceNotFound;
1478                }
1479
1480
1481          }
1482      }
1483      return false;
1484
1485  }
1486
1487    /*
1488     * Name    : expandCompositChar
1489     * Function: LamAlef needs special handling as the LamAlef is
1490     *           one character while expanding it will give two
1491     *           characters Lam + Alef, so we need to expand the LamAlef
1492     *           in near or far spaces according to the options the user
1493     *           specifies or increase the buffer size.
1494     *           Dest has enough room for the expansion if we are growing.
1495     *           lamalef are normalized to the 'special characters'
1496     */
1497    private int expandCompositChar(char[] dest,
1498                              int start,
1499                              int length,
1500                              int lacount,
1501                              int shapingMode) throws ArabicShapingException {
1502
1503        int lenOptionsLamAlef = options & LAMALEF_MASK;
1504        int lenOptionsSeen = options & SEEN_MASK;
1505        int lenOptionsYehHamza = options & YEHHAMZA_MASK;
1506        boolean spaceNotFound = false;
1507
1508        if (!isLogical && !spacesRelativeToTextBeginEnd) {
1509            switch (lenOptionsLamAlef) {
1510            case LAMALEF_BEGIN: lenOptionsLamAlef = LAMALEF_END; break;
1511            case LAMALEF_END: lenOptionsLamAlef = LAMALEF_BEGIN; break;
1512            default: break;
1513            }
1514        }
1515
1516        if(shapingMode == 1){
1517            if(lenOptionsLamAlef == LAMALEF_AUTO){
1518                if(isLogical){
1519                    spaceNotFound = expandCompositCharAtEnd(dest, start, length, lacount);
1520                    if(spaceNotFound){
1521                        spaceNotFound = expandCompositCharAtBegin(dest, start, length, lacount);
1522                    }
1523                    if(spaceNotFound){
1524                        spaceNotFound = expandCompositCharAtNear(dest, start, length,0,0,1);
1525                    }
1526                    if(spaceNotFound){
1527                        throw new ArabicShapingException("No spacefor lamalef");
1528                    }
1529                }else{
1530                    spaceNotFound = expandCompositCharAtBegin(dest, start, length, lacount);
1531                    if(spaceNotFound){
1532                        spaceNotFound = expandCompositCharAtEnd(dest, start, length, lacount);
1533                    }
1534                    if(spaceNotFound){
1535                        spaceNotFound = expandCompositCharAtNear(dest, start, length,0,0,1);
1536                    }
1537                    if(spaceNotFound){
1538                        throw new ArabicShapingException("No spacefor lamalef");
1539                    }
1540                }
1541            }else if(lenOptionsLamAlef == LAMALEF_END){
1542                spaceNotFound = expandCompositCharAtEnd(dest, start, length, lacount);
1543                if(spaceNotFound){
1544                    throw new ArabicShapingException("No spacefor lamalef");
1545                }
1546            }else if(lenOptionsLamAlef == LAMALEF_BEGIN){
1547                spaceNotFound = expandCompositCharAtBegin(dest, start, length, lacount);
1548                if(spaceNotFound){
1549                    throw new ArabicShapingException("No spacefor lamalef");
1550                }
1551            }else if(lenOptionsLamAlef == LAMALEF_NEAR){
1552                spaceNotFound = expandCompositCharAtNear(dest, start, length,0,0,1);
1553                if(spaceNotFound){
1554                    throw new ArabicShapingException("No spacefor lamalef");
1555            }
1556            }else if(lenOptionsLamAlef == LAMALEF_RESIZE){
1557                for (int r = start + length, w = r + lacount; --r >= start;) {
1558                    char ch = dest[r];
1559                    if (isNormalizedLamAlefChar(ch)) {
1560                        dest[--w] = '\u0644';
1561                        dest[--w] = convertNormalizedLamAlef[ch - '\u065C'];
1562                    } else {
1563                        dest[--w] = ch;
1564                    }
1565                }
1566                length += lacount;
1567            }
1568            }else{
1569                if(lenOptionsSeen == SEEN_TWOCELL_NEAR){
1570                spaceNotFound = expandCompositCharAtNear(dest, start, length,0,1,0);
1571                if(spaceNotFound){
1572                    throw new ArabicShapingException("No space for Seen tail expansion");
1573                }
1574            }
1575            if(lenOptionsYehHamza == YEHHAMZA_TWOCELL_NEAR){
1576                spaceNotFound = expandCompositCharAtNear(dest, start, length,1,0,0);
1577                if(spaceNotFound){
1578                    throw new ArabicShapingException("No space for YehHamza expansion");
1579                }
1580            }
1581            }
1582        return length;
1583    }
1584
1585
1586    /* Convert the input buffer from FExx Range into 06xx Range
1587     * to put all characters into the 06xx range
1588     * even the lamalef is converted to the special region in
1589     * the 06xx range.  Return the number of lamalef chars found.
1590     */
1591    private int normalize(char[] dest, int start, int length) {
1592        int lacount = 0;
1593        for (int i = start, e = i + length; i < e; ++i) {
1594            char ch = dest[i];
1595            if (ch >= '\uFE70' && ch <= '\uFEFC') {
1596                if (isLamAlefChar(ch)) {
1597                    ++lacount;
1598                }
1599                dest[i] = (char)convertFEto06[ch - '\uFE70'];
1600            }
1601        }
1602        return lacount;
1603    }
1604
1605    /*
1606     * Name    : deshapeNormalize
1607     * Function: Convert the input buffer from FExx Range into 06xx Range
1608     *           even the lamalef is converted to the special region in the 06xx range.
1609     *           According to the options the user enters, all seen family characters
1610     *           followed by a tail character are merged to seen tail family character and
1611     *           any yeh followed by a hamza character are merged to yehhamza character.
1612     *           Method returns the number of lamalef chars found.
1613     */
1614    private int deshapeNormalize(char[] dest, int start, int length) {
1615        int lacount = 0;
1616        int yehHamzaComposeEnabled = 0;
1617        int seenComposeEnabled = 0;
1618
1619        yehHamzaComposeEnabled = ((options&YEHHAMZA_MASK) == YEHHAMZA_TWOCELL_NEAR) ? 1 : 0;
1620        seenComposeEnabled = ((options&SEEN_MASK) == SEEN_TWOCELL_NEAR)? 1 : 0;
1621
1622        for (int i = start, e = i + length; i < e; ++i) {
1623            char ch = dest[i];
1624
1625        if( (yehHamzaComposeEnabled == 1) && ((ch == HAMZA06_CHAR) || (ch == HAMZAFE_CHAR))
1626               && (i < (length - 1)) && isAlefMaksouraChar(dest[i+1] )) {
1627                dest[i] = SPACE_CHAR;
1628                dest[i+1] = YEH_HAMZA_CHAR;
1629       } else if ( (seenComposeEnabled == 1) && (isTailChar(ch)) && (i< (length - 1))
1630                       && (isSeenTailFamilyChar(dest[i+1])==1) ) {
1631               dest[i] = SPACE_CHAR;
1632       }
1633       else if (ch >= '\uFE70' && ch <= '\uFEFC') {
1634                if (isLamAlefChar(ch)) {
1635                    ++lacount;
1636                }
1637                dest[i] = (char)convertFEto06[ch - '\uFE70'];
1638            }
1639        }
1640        return lacount;
1641    }
1642
1643    /*
1644     * Name    : shapeUnicode
1645     * Function: Converts an Arabic Unicode buffer in 06xx Range into a shaped
1646     *           arabic Unicode buffer in FExx Range
1647     */
1648    private int shapeUnicode(char[] dest,
1649                             int start,
1650                             int length,
1651                             int destSize,
1652                             int tashkeelFlag)throws ArabicShapingException {
1653
1654        int lamalef_count = normalize(dest, start, length);
1655
1656        // resolve the link between the characters.
1657        // Arabic characters have four forms: Isolated, Initial, Medial and Final.
1658        // Tashkeel characters have two, isolated or medial, and sometimes only isolated.
1659        // tashkeelFlag == 0: shape normally, 1: shape isolated, 2: don't shape
1660
1661        boolean lamalef_found = false, seenfam_found = false;
1662        boolean yehhamza_found = false, tashkeel_found = false;
1663        int i = start + length - 1;
1664        int currLink = getLink(dest[i]);
1665        int nextLink = 0;
1666        int prevLink = 0;
1667        int lastLink = 0;
1668        //int prevPos = i;
1669        int lastPos = i;
1670        int nx = -2;
1671        int nw = 0;
1672
1673        while (i >= 0) {
1674            // If high byte of currLink > 0 then there might be more than one shape
1675            if ((currLink & '\uFF00') > 0 || isTashkeelChar(dest[i])) {
1676                nw = i - 1;
1677                nx = -2;
1678                while (nx < 0) { // we need to know about next char
1679                    if (nw == -1) {
1680                        nextLink = 0;
1681                        nx = Integer.MAX_VALUE;
1682                    } else {
1683                        nextLink = getLink(dest[nw]);
1684                        if ((nextLink & IRRELEVANT) == 0) {
1685                            nx = nw;
1686                        } else {
1687                            --nw;
1688                        }
1689                    }
1690                }
1691
1692                if (((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0)) {
1693                    lamalef_found = true;
1694                    char wLamalef = changeLamAlef(dest[i]); // get from 0x065C-0x065f
1695                    if (wLamalef != '\u0000') {
1696                        // replace alef by marker, it will be removed later
1697                        dest[i] = '\uffff';
1698                        dest[lastPos] = wLamalef;
1699                        i = lastPos;
1700                    }
1701
1702                    lastLink = prevLink;
1703                    currLink = getLink(wLamalef); // requires '\u0000', unfortunately
1704                }
1705                if ((i > 0) && (dest[i-1] == SPACE_CHAR))
1706                {
1707                    if ( isSeenFamilyChar(dest[i]) == 1){
1708                        seenfam_found = true;
1709                    } else if (dest[i] == YEH_HAMZA_CHAR) {
1710                        yehhamza_found = true;
1711                    }
1712                }
1713                else if(i==0){
1714                    if ( isSeenFamilyChar(dest[i]) == 1){
1715                        seenfam_found = true;
1716                    } else if (dest[i] == YEH_HAMZA_CHAR) {
1717                        yehhamza_found = true;
1718                    }
1719                }
1720
1721
1722                // get the proper shape according to link ability of neighbors
1723                // and of character; depends on the order of the shapes
1724                // (isolated, initial, middle, final) in the compatibility area
1725
1726                int flag = specialChar(dest[i]);
1727
1728                int shape = shapeTable[nextLink & LINK_MASK]
1729                    [lastLink & LINK_MASK]
1730                    [currLink & LINK_MASK];
1731
1732                if (flag == 1) {
1733                    shape &= 0x1;
1734                } else if (flag == 2) {
1735                    if (tashkeelFlag == 0 &&
1736                        ((lastLink & LINKL) != 0) &&
1737                        ((nextLink & LINKR) != 0) &&
1738                        dest[i] != '\u064C' &&
1739                        dest[i] != '\u064D' &&
1740                        !((nextLink & ALEFTYPE) == ALEFTYPE &&
1741                          (lastLink & LAMTYPE) == LAMTYPE)) {
1742
1743                        shape = 1;
1744                    } else {
1745                        shape = 0;
1746                    }
1747                }
1748                if (flag == 2) {
1749                    if (tashkeelFlag == 2) {
1750                        dest[i] = TASHKEEL_SPACE_SUB;
1751                        tashkeel_found = true;
1752                    }
1753                    else{
1754                        dest[i] = (char)('\uFE70' + irrelevantPos[dest[i] - '\u064B'] + shape);
1755                    }
1756                    // else leave tashkeel alone
1757                } else {
1758                    dest[i] = (char)('\uFE70' + (currLink >> 8) + shape);
1759                }
1760            }
1761
1762            // move one notch forward
1763            if ((currLink & IRRELEVANT) == 0) {
1764                prevLink = lastLink;
1765                lastLink = currLink;
1766                //prevPos = lastPos;
1767                lastPos = i;
1768            }
1769
1770            --i;
1771            if (i == nx) {
1772                currLink = nextLink;
1773                nx = -2;
1774            } else if (i != -1) {
1775                currLink = getLink(dest[i]);
1776            }
1777        }
1778
1779        // If we found a lam/alef pair in the buffer
1780        // call handleGeneratedSpaces to remove the spaces that were added
1781
1782        destSize = length;
1783        if (lamalef_found || tashkeel_found) {
1784            destSize = handleGeneratedSpaces(dest, start, length);
1785        }
1786        if (seenfam_found || yehhamza_found){
1787            destSize = expandCompositChar(dest, start, destSize, lamalef_count, SHAPE_MODE);
1788        }
1789        return destSize;
1790    }
1791
1792    /*
1793     * Name    : deShapeUnicode
1794     * Function: Converts an Arabic Unicode buffer in FExx Range into unshaped
1795     *           arabic Unicode buffer in 06xx Range
1796     */
1797    private int deShapeUnicode(char[] dest,
1798                               int start,
1799                               int length,
1800                               int destSize) throws ArabicShapingException {
1801
1802        int lamalef_count = deshapeNormalize(dest, start, length);
1803
1804        // If there was a lamalef in the buffer call expandLamAlef
1805        if (lamalef_count != 0) {
1806            // need to adjust dest to fit expanded buffer... !!!
1807            destSize = expandCompositChar(dest, start, length, lamalef_count,DESHAPE_MODE);
1808        } else {
1809            destSize = length;
1810        }
1811
1812        return destSize;
1813    }
1814
1815    private int internalShape(char[] source,
1816                              int sourceStart,
1817                              int sourceLength,
1818                              char[] dest,
1819                              int destStart,
1820                              int destSize) throws ArabicShapingException {
1821
1822        if (sourceLength == 0) {
1823            return 0;
1824        }
1825
1826        if (destSize == 0) {
1827            if (((options & LETTERS_MASK) != LETTERS_NOOP) &&
1828                ((options & LAMALEF_MASK) == LAMALEF_RESIZE)) {
1829
1830                return calculateSize(source, sourceStart, sourceLength);
1831            } else {
1832                return sourceLength; // by definition
1833            }
1834        }
1835
1836        // always use temp buffer
1837        char[] temp = new char[sourceLength * 2]; // all lamalefs requiring expansion
1838        System.arraycopy(source, sourceStart, temp, 0, sourceLength);
1839
1840        if (isLogical) {
1841            invertBuffer(temp, 0, sourceLength);
1842        }
1843
1844        int outputSize = sourceLength;
1845
1846        switch (options & LETTERS_MASK) {
1847        case LETTERS_SHAPE_TASHKEEL_ISOLATED:
1848            outputSize = shapeUnicode(temp, 0, sourceLength, destSize, 1);
1849            break;
1850
1851        case LETTERS_SHAPE:
1852            if( ((options&TASHKEEL_MASK)> 0) &&
1853                ((options&TASHKEEL_MASK) !=TASHKEEL_REPLACE_BY_TATWEEL)) {
1854                   /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */
1855                outputSize = shapeUnicode(temp, 0, sourceLength, destSize, 2);
1856                }else {
1857                   //default Call the shaping function with tashkeel flag == 1 */
1858                    outputSize = shapeUnicode(temp, 0, sourceLength, destSize, 0);
1859
1860                   /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/
1861                   if( (options&TASHKEEL_MASK) == TASHKEEL_REPLACE_BY_TATWEEL){
1862                       outputSize = handleTashkeelWithTatweel(temp,sourceLength);
1863                   }
1864               }
1865            break;
1866
1867        case LETTERS_UNSHAPE:
1868            outputSize = deShapeUnicode(temp, 0, sourceLength, destSize);
1869            break;
1870
1871        default:
1872            break;
1873        }
1874
1875        if (outputSize > destSize) {
1876            throw new ArabicShapingException("not enough room for result data");
1877        }
1878
1879        if ((options & DIGITS_MASK) != DIGITS_NOOP) {
1880            char digitBase = '\u0030'; // European digits
1881            switch (options & DIGIT_TYPE_MASK) {
1882            case DIGIT_TYPE_AN:
1883                digitBase = '\u0660';  // Arabic-Indic digits
1884                break;
1885
1886            case DIGIT_TYPE_AN_EXTENDED:
1887                digitBase = '\u06f0';  // Eastern Arabic-Indic digits (Persian and Urdu)
1888                break;
1889
1890            default:
1891                break;
1892            }
1893
1894            switch (options & DIGITS_MASK) {
1895            case DIGITS_EN2AN:
1896                {
1897                    int digitDelta = digitBase - '\u0030';
1898                    for (int i = 0; i < outputSize; ++i) {
1899                        char ch = temp[i];
1900                        if (ch <= '\u0039' && ch >= '\u0030') {
1901                            temp[i] += digitDelta;
1902                        }
1903                    }
1904                }
1905                break;
1906
1907            case DIGITS_AN2EN:
1908                {
1909                    char digitTop = (char)(digitBase + 9);
1910                    int digitDelta = '\u0030' - digitBase;
1911                    for (int i = 0; i < outputSize; ++i) {
1912                        char ch = temp[i];
1913                        if (ch <= digitTop && ch >= digitBase) {
1914                            temp[i] += digitDelta;
1915                        }
1916                    }
1917                }
1918                break;
1919
1920            case DIGITS_EN2AN_INIT_LR:
1921                shapeToArabicDigitsWithContext(temp, 0, outputSize, digitBase, false);
1922                break;
1923
1924            case DIGITS_EN2AN_INIT_AL:
1925                shapeToArabicDigitsWithContext(temp, 0, outputSize, digitBase, true);
1926                break;
1927
1928            default:
1929                break;
1930            }
1931        }
1932
1933        if (isLogical) {
1934            invertBuffer(temp, 0, outputSize);
1935        }
1936
1937        System.arraycopy(temp, 0, dest, destStart, outputSize);
1938
1939        return outputSize;
1940    }
1941
1942    private static class ArabicShapingException extends RuntimeException {
1943        ArabicShapingException(String msg) {
1944            super(msg);
1945        }
1946    }
1947}
1948