Search in sources :

Example 1 with IntList

use of com.mta.tehreer.collections.IntList in project Tehreer-Android by mta452.

the class ShapingResult method getCaretAdvances.

@NonNull
private float[] getCaretAdvances(@Nullable boolean[] caretStops) {
    int codeUnitCount = getCharCount();
    float[] caretAdvances = new float[codeUnitCount + 1];
    boolean isBackward = isBackward();
    FloatList glyphAdvances = getGlyphAdvances();
    IntList clusterMap = getClusterMap();
    int glyphIndex = clusterMap.get(0) + 1;
    int refIndex = glyphIndex;
    int totalStops = 0;
    int clusterStart = 0;
    for (int codeUnitIndex = 1; codeUnitIndex <= codeUnitCount; codeUnitIndex++) {
        int oldIndex = glyphIndex;
        if (codeUnitIndex != codeUnitCount) {
            glyphIndex = clusterMap.get(codeUnitIndex) + 1;
            if (caretStops != null && !caretStops[codeUnitIndex - 1]) {
                continue;
            }
            totalStops += 1;
        } else {
            totalStops += 1;
            glyphIndex = (isBackward ? 0 : getGlyphCount() + 1);
        }
        if (glyphIndex != oldIndex) {
            float clusterAdvance = 0;
            float distance = 0;
            int counter = 1;
            /* Find the advance of current cluster. */
            if (isBackward) {
                while (refIndex > glyphIndex) {
                    clusterAdvance += glyphAdvances.get(refIndex - 1);
                    refIndex -= 1;
                }
            } else {
                while (refIndex < glyphIndex) {
                    clusterAdvance += glyphAdvances.get(refIndex - 1);
                    refIndex += 1;
                }
            }
            /* Divide the advance evenly between cluster length. */
            while (clusterStart < codeUnitIndex) {
                float advance = 0;
                if (caretStops == null || caretStops[clusterStart] || clusterStart == codeUnitCount - 1) {
                    float previous = distance;
                    distance = (clusterAdvance * counter) / totalStops;
                    advance = distance - previous;
                    counter += 1;
                }
                caretAdvances[clusterStart] = advance;
                clusterStart += 1;
            }
            totalStops = 0;
        }
    }
    return caretAdvances;
}
Also used : FloatList(com.mta.tehreer.collections.FloatList) IntList(com.mta.tehreer.collections.IntList) NonNull(androidx.annotation.NonNull)

Example 2 with IntList

use of com.mta.tehreer.collections.IntList in project Tehreer-Android by mta452.

the class OpenTypeInfoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opentype_info);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    Intent intent = getIntent();
    String typefaceName = intent.getStringExtra(TYPEFACE_NAME);
    int typeSize = intent.getIntExtra(TYPE_SIZE, 0);
    int scriptTag = intent.getIntExtra(SCRIPT_TAG, 0);
    int languageTag = intent.getIntExtra(LANGUAGE_TAG, 0);
    String sourceText = intent.getCharSequenceExtra(SOURCE_TEXT).toString();
    WritingDirection writingDirection = ShapingEngine.getScriptDirection(scriptTag);
    Typeface typeface = TypefaceManager.getTypefaceByName(typefaceName);
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    float displaySize = 18.0f * displayMetrics.scaledDensity;
    float sizeScale = displaySize / typeSize;
    Renderer renderer = new Renderer();
    renderer.setTypeface(typeface);
    renderer.setTypeSize(typeSize);
    renderer.setScaleX(sizeScale);
    renderer.setScaleY(sizeScale);
    ShapingEngine shapingEngine = ShapingEngine.finalizable(new ShapingEngine());
    shapingEngine.setTypeface(typeface);
    shapingEngine.setTypeSize(typeSize);
    shapingEngine.setScriptTag(scriptTag);
    shapingEngine.setLanguageTag(languageTag);
    shapingEngine.setWritingDirection(writingDirection);
    ShapingResult shapingResult = ShapingResult.finalizable(shapingEngine.shapeText(sourceText, 0, sourceText.length()));
    IntList clusterMap = shapingResult.getClusterMap();
    int length = clusterMap.size();
    int[] initials = new int[length + 1];
    int cluster = -1;
    int previous = -1;
    for (int i = 0; i < length; i++) {
        int value = clusterMap.get(i);
        if (value != previous) {
            initials[++cluster] = i;
        }
        previous = value;
    }
    initials[++cluster] = length;
    ListView infoListView = findViewById(R.id.list_view_info);
    infoListView.setAdapter(new ClusterAdapter(this, renderer, sourceText, shapingResult, IntList.of(initials).subList(0, cluster + 1)));
}
Also used : ShapingEngine(com.mta.tehreer.sfnt.ShapingEngine) Typeface(com.mta.tehreer.graphics.Typeface) Intent(android.content.Intent) SpannableString(android.text.SpannableString) DisplayMetrics(android.util.DisplayMetrics) ShapingResult(com.mta.tehreer.sfnt.ShapingResult) Paint(android.graphics.Paint) IntList(com.mta.tehreer.collections.IntList) ListView(android.widget.ListView) Renderer(com.mta.tehreer.graphics.Renderer) WritingDirection(com.mta.tehreer.sfnt.WritingDirection) ActionBar(androidx.appcompat.app.ActionBar)

Aggregations

IntList (com.mta.tehreer.collections.IntList)2 Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 SpannableString (android.text.SpannableString)1 DisplayMetrics (android.util.DisplayMetrics)1 ListView (android.widget.ListView)1 NonNull (androidx.annotation.NonNull)1 ActionBar (androidx.appcompat.app.ActionBar)1 FloatList (com.mta.tehreer.collections.FloatList)1 Renderer (com.mta.tehreer.graphics.Renderer)1 Typeface (com.mta.tehreer.graphics.Typeface)1 ShapingEngine (com.mta.tehreer.sfnt.ShapingEngine)1 ShapingResult (com.mta.tehreer.sfnt.ShapingResult)1 WritingDirection (com.mta.tehreer.sfnt.WritingDirection)1