Search in sources :

Example 1 with BidiAlgorithm

use of com.mta.tehreer.unicode.BidiAlgorithm in project Tehreer-Android by mta452.

the class ShapeResolver method fillRuns.

public static void fillRuns(@NonNull String text, @NonNull Spanned spanned, @NonNull List<Object> defaultSpans, @NonNull byte[] breaks, @NonNull List<BidiParagraph> paragraphs, @NonNull List<TextRun> runs) {
    BidiAlgorithm bidiAlgorithm = null;
    ShapingEngine shapingEngine = null;
    try {
        bidiAlgorithm = new BidiAlgorithm(text);
        shapingEngine = new ShapingEngine();
        ScriptClassifier scriptClassifier = new ScriptClassifier(text);
        ShapingRunLocator locator = new ShapingRunLocator(spanned, defaultSpans);
        BaseDirection baseDirection = BaseDirection.DEFAULT_LEFT_TO_RIGHT;
        byte forwardType = BreakResolver.typeMode(BreakResolver.PARAGRAPH, true);
        byte backwardType = BreakResolver.typeMode(BreakResolver.PARAGRAPH, false);
        int paragraphStart = 0;
        int suggestedEnd = text.length();
        while (paragraphStart != suggestedEnd) {
            BidiParagraph paragraph = bidiAlgorithm.createParagraph(paragraphStart, suggestedEnd, baseDirection);
            for (BidiRun bidiRun : paragraph.getLogicalRuns()) {
                for (ScriptRun scriptRun : scriptClassifier.getScriptRuns(bidiRun.charStart, bidiRun.charEnd)) {
                    int scriptTag = Script.getOpenTypeTag(scriptRun.script);
                    WritingDirection writingDirection = ShapingEngine.getScriptDirection(scriptTag);
                    boolean isOddLevel = ((bidiRun.embeddingLevel & 1) == 1);
                    boolean isBackward = (isOddLevel && writingDirection == WritingDirection.LEFT_TO_RIGHT) | (!isOddLevel && writingDirection == WritingDirection.RIGHT_TO_LEFT);
                    ShapingOrder shapingOrder = (isBackward ? ShapingOrder.BACKWARD : ShapingOrder.FORWARD);
                    locator.reset(scriptRun.charStart, scriptRun.charEnd);
                    shapingEngine.setScriptTag(scriptTag);
                    shapingEngine.setWritingDirection(writingDirection);
                    shapingEngine.setShapingOrder(shapingOrder);
                    resolveTypefaces(text, spanned, runs, locator, shapingEngine, bidiRun.embeddingLevel);
                }
            }
            paragraphs.add(paragraph);
            breaks[paragraph.getCharStart()] |= backwardType;
            breaks[paragraph.getCharEnd() - 1] |= forwardType;
            paragraphStart = paragraph.getCharEnd();
        }
    } finally {
        if (shapingEngine != null) {
            shapingEngine.dispose();
        }
        if (bidiAlgorithm != null) {
            bidiAlgorithm.dispose();
        }
    }
}
Also used : ShapingEngine(com.mta.tehreer.sfnt.ShapingEngine) BidiRun(com.mta.tehreer.unicode.BidiRun) ScriptClassifier(com.mta.tehreer.unicode.ScriptClassifier) Paint(android.graphics.Paint) ShapingOrder(com.mta.tehreer.sfnt.ShapingOrder) BaseDirection(com.mta.tehreer.unicode.BaseDirection) ScriptRun(com.mta.tehreer.unicode.ScriptRun) BidiParagraph(com.mta.tehreer.unicode.BidiParagraph) WritingDirection(com.mta.tehreer.sfnt.WritingDirection) BidiAlgorithm(com.mta.tehreer.unicode.BidiAlgorithm)

Example 2 with BidiAlgorithm

use of com.mta.tehreer.unicode.BidiAlgorithm in project Tehreer-Android by mta452.

the class Typesetter method resolveBidi.

private void resolveBidi() {
    // TODO: Analyze script runs.
    BidiAlgorithm bidiAlgorithm = null;
    ShapingEngine shapingEngine = null;
    try {
        bidiAlgorithm = new BidiAlgorithm(mText);
        shapingEngine = new ShapingEngine();
        BaseDirection baseDirection = BaseDirection.DEFAULT_LEFT_TO_RIGHT;
        byte forwardType = specializeBreakType(BREAK_TYPE_PARAGRAPH, true);
        byte backwardType = specializeBreakType(BREAK_TYPE_PARAGRAPH, false);
        int paragraphStart = 0;
        int suggestedEnd = mText.length();
        while (paragraphStart != suggestedEnd) {
            BidiParagraph paragraph = bidiAlgorithm.createParagraph(paragraphStart, suggestedEnd, baseDirection);
            for (BidiRun bidiRun : paragraph.getLogicalRuns()) {
                int scriptTag = SfntTag.make(bidiRun.isRightToLeft() ? "arab" : "latn");
                WritingDirection writingDirection = ShapingEngine.getScriptDirection(scriptTag);
                shapingEngine.setScriptTag(scriptTag);
                shapingEngine.setWritingDirection(writingDirection);
                resolveTypefaces(bidiRun.charStart, bidiRun.charEnd, bidiRun.embeddingLevel, shapingEngine);
            }
            mBidiParagraphs.add(paragraph);
            mBreakRecord[paragraph.getCharStart()] |= backwardType;
            mBreakRecord[paragraph.getCharEnd() - 1] |= forwardType;
            paragraphStart = paragraph.getCharEnd();
        }
    } finally {
        if (shapingEngine != null) {
            shapingEngine.dispose();
        }
        if (bidiAlgorithm != null) {
            bidiAlgorithm.dispose();
        }
    }
}
Also used : ShapingEngine(com.mta.tehreer.sfnt.ShapingEngine) BaseDirection(com.mta.tehreer.unicode.BaseDirection) BidiRun(com.mta.tehreer.unicode.BidiRun) BidiParagraph(com.mta.tehreer.unicode.BidiParagraph) WritingDirection(com.mta.tehreer.sfnt.WritingDirection) BidiAlgorithm(com.mta.tehreer.unicode.BidiAlgorithm)

Example 3 with BidiAlgorithm

use of com.mta.tehreer.unicode.BidiAlgorithm in project Tehreer-Android by mta452.

the class BidiInfoActivity method writeBidiText.

private void writeBidiText(SpannableStringBuilder builder) {
    builder.setSpan(new AbsoluteSizeSpan((int) (16.0f * mDensity + 0.5f)), 0, 0, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    builder.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 0, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    BidiAlgorithm algorithm = null;
    try {
        algorithm = new BidiAlgorithm(mBidiText);
        writeAlgorithmText(builder, algorithm);
    } finally {
        if (algorithm != null) {
            algorithm.dispose();
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) BidiAlgorithm(com.mta.tehreer.unicode.BidiAlgorithm)

Aggregations

BidiAlgorithm (com.mta.tehreer.unicode.BidiAlgorithm)3 ShapingEngine (com.mta.tehreer.sfnt.ShapingEngine)2 WritingDirection (com.mta.tehreer.sfnt.WritingDirection)2 BaseDirection (com.mta.tehreer.unicode.BaseDirection)2 BidiParagraph (com.mta.tehreer.unicode.BidiParagraph)2 BidiRun (com.mta.tehreer.unicode.BidiRun)2 Paint (android.graphics.Paint)1 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 ShapingOrder (com.mta.tehreer.sfnt.ShapingOrder)1 ScriptClassifier (com.mta.tehreer.unicode.ScriptClassifier)1 ScriptRun (com.mta.tehreer.unicode.ScriptRun)1