Search in sources :

Example 1 with BidiRun

use of com.ibm.icu.text.BidiRun in project Osmand by osmandapp.

the class Reshaper method reshape.

public static String reshape(String s) {
    // }
    try {
        ArabicShaping as = new ArabicShaping(ArabicShaping.LETTERS_SHAPE | ArabicShaping.LENGTH_GROW_SHRINK);
        try {
            s = as.shape(s);
        } catch (ArabicShapingException e) {
            LOG.error(e.getMessage(), e);
        }
        Bidi line = new Bidi(s.length(), s.length());
        line.setPara(s, Bidi.LEVEL_DEFAULT_LTR, null);
        byte direction = line.getDirection();
        if (direction != Bidi.MIXED) {
            // unidirectional
            if (line.isLeftToRight()) {
                return s;
            } else {
                char[] chs = new char[s.length()];
                for (int i = 0; i < chs.length; i++) {
                    chs[i] = s.charAt(chs.length - i - 1);
                }
                return new String(chs);
            }
        } else {
            // // mixed-directional
            int count = line.countRuns();
            // if (styleRunCount <= 1) {
            // int style = styleRuns[0].style;
            // // iterate over directional runs
            // for (i = 0; i < count; ++i) {
            // run = line.getVisualRun(i);
            // renderRun(text, run.getStart(), run.getLimit(),
            // run.getDirection(), style);
            // }
            // }
            StringBuilder res = new StringBuilder();
            // iterate over both directional and style runs
            for (int i = 0; i < count; ++i) {
                BidiRun run = line.getVisualRun(i);
                int st = run.getStart();
                int e = run.getLimit();
                int j = run.getDirection() == Bidi.LTR ? st : e - 1;
                int l = run.getDirection() == Bidi.LTR ? e : st - 1;
                boolean plus = run.getDirection() == Bidi.LTR;
                while (j != l) {
                    res.append(s.charAt(j));
                    if (plus) {
                        j++;
                    } else {
                        j--;
                    }
                }
            }
            return res.toString();
        }
    } catch (RuntimeException e) {
        LOG.error(e.getMessage(), e);
        return s;
    }
}
Also used : ArabicShaping(com.ibm.icu.text.ArabicShaping) ArabicShapingException(com.ibm.icu.text.ArabicShapingException) Bidi(com.ibm.icu.text.Bidi) BidiRun(com.ibm.icu.text.BidiRun)

Aggregations

ArabicShaping (com.ibm.icu.text.ArabicShaping)1 ArabicShapingException (com.ibm.icu.text.ArabicShapingException)1 Bidi (com.ibm.icu.text.Bidi)1 BidiRun (com.ibm.icu.text.BidiRun)1