Search in sources :

Example 6 with Typeface

use of com.mta.tehreer.graphics.Typeface in project Tehreer-Android by mta452.

the class ShapeResolver method resolveTypefaces.

private static void resolveTypefaces(@NonNull String text, @NonNull Spanned spanned, @NonNull List<TextRun> runs, @NonNull ShapingRunLocator locator, @NonNull ShapingEngine engine, byte bidiLevel) {
    Paint paint = null;
    Paint.FontMetricsInt metrics = null;
    while (locator.moveNext()) {
        int runStart = locator.getRunStart();
        int runEnd = locator.getRunEnd();
        Typeface typeface = locator.getTypeface();
        checkArgument(typeface != null, "No typeface is specified for range [" + runStart + ", " + runEnd + ')');
        float typeSize = locator.getTypeSize();
        float sizeByEm = typeSize / typeface.getUnitsPerEm();
        float ascent = typeface.getAscent() * sizeByEm;
        float descent = typeface.getDescent() * sizeByEm;
        float leading = typeface.getLeading() * sizeByEm;
        ReplacementSpan replacement = locator.getReplacement();
        TextRun textRun;
        if (replacement == null) {
            engine.setTypeface(typeface);
            engine.setTypeSize(typeSize);
            ShapingResult shapingResult = null;
            try {
                shapingResult = engine.shapeText(text, runStart, runEnd);
                WritingDirection writingDirection = engine.getWritingDirection();
                boolean isBackward = shapingResult.isBackward();
                int[] glyphIds = shapingResult.getGlyphIds().toArray();
                float[] offsets = shapingResult.getGlyphOffsets().toArray();
                float[] advances = shapingResult.getGlyphAdvances().toArray();
                int[] clusterMap = shapingResult.getClusterMap().toArray();
                FloatList caretEdges = shapingResult.getCaretEdges(null);
                float scaleX = locator.getScaleX();
                if (Float.compare(scaleX, 1.0f) != 0) {
                    for (int i = 0; i < glyphIds.length; i++) {
                        offsets[i * 2] *= scaleX;
                        advances[i] *= scaleX;
                    }
                }
                float baselineShift = locator.getBaselineShift();
                if (Float.compare(baselineShift, 0.0f) != 0) {
                    for (int i = 0; i < glyphIds.length; i++) {
                        offsets[(i * 2) + 1] += baselineShift;
                    }
                }
                textRun = new IntrinsicRun(runStart, runEnd, isBackward, bidiLevel, writingDirection, typeface, typeSize, ascent, descent, leading, glyphIds, offsets, advances, clusterMap, caretEdges);
            } finally {
                if (shapingResult != null) {
                    shapingResult.dispose();
                }
            }
        } else {
            if (paint == null) {
                paint = new Paint();
            }
            if (metrics == null) {
                metrics = new Paint.FontMetricsInt();
            }
            metrics.ascent = (int) -(ascent + 0.5f);
            metrics.descent = (int) (descent + 0.5f);
            metrics.leading = (int) (leading + 0.5f);
            int extent = replacement.getSize(paint, spanned, runStart, runEnd, metrics);
            int runLength = runEnd - runStart;
            float[] caretEdges = new float[runLength + 1];
            if ((bidiLevel & 1) == 0) {
                caretEdges[runLength] = extent;
            } else {
                caretEdges[0] = extent;
            }
            textRun = new ReplacementRun(spanned, runStart, runEnd, bidiLevel, replacement, paint, typeface, typeSize, metrics.ascent, metrics.descent, metrics.leading, extent, FloatList.of(caretEdges));
        }
        runs.add(textRun);
    }
}
Also used : Typeface(com.mta.tehreer.graphics.Typeface) Paint(android.graphics.Paint) ReplacementSpan(android.text.style.ReplacementSpan) ShapingResult(com.mta.tehreer.sfnt.ShapingResult) Paint(android.graphics.Paint) FloatList(com.mta.tehreer.collections.FloatList) WritingDirection(com.mta.tehreer.sfnt.WritingDirection)

Example 7 with Typeface

use of com.mta.tehreer.graphics.Typeface in project Tehreer-Android by mta452.

the class DemoApplication method registerTypeface.

private void registerTypeface(String fileName, int tag) {
    // It is better to copy the typeface into sdcard for performance reasons.
    try {
        File file = copyAsset(fileName);
        Typeface typeface = new Typeface(file);
        TypefaceManager.registerTypeface(typeface, tag);
    } catch (Exception e) {
        throw new RuntimeException("Unable to register typeface \"" + fileName + "\"");
    }
}
Also used : Typeface(com.mta.tehreer.graphics.Typeface) File(java.io.File) IOException(java.io.IOException)

Example 8 with Typeface

use of com.mta.tehreer.graphics.Typeface in project Tehreer-Android by mta452.

the class GlyphInfoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_glyph_info);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    Intent intent = getIntent();
    final Typeface typeface = TypefaceManager.getTypefaceByName(intent.getStringExtra(TYPEFACE_NAME));
    final int glyphId = intent.getIntExtra(GLYPH_ID, 0);
    final ImageView glyphInfo = (ImageView) findViewById(R.id.image_view_glyph_info);
    glyphInfo.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int width = findViewById(android.R.id.content).getWidth();
            int height = glyphInfo.getHeight();
            Drawable drawable = new GlyphDrawable(GlyphInfoActivity.this, typeface, glyphId, width, height);
            glyphInfo.setImageDrawable(drawable);
        }
    });
}
Also used : Typeface(com.mta.tehreer.graphics.Typeface) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) ImageView(android.widget.ImageView) ViewTreeObserver(android.view.ViewTreeObserver) ActionBar(androidx.appcompat.app.ActionBar) Paint(android.graphics.Paint)

Example 9 with Typeface

use of com.mta.tehreer.graphics.Typeface 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)

Example 10 with Typeface

use of com.mta.tehreer.graphics.Typeface in project Tehreer-Android by mta452.

the class TypefaceGlyphsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_typeface_glyphs);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mGlyphsGridView = findViewById(R.id.grid_view_glyphs);
    mGlyphsGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            displayGlyph(i);
        }
    });
    Spinner typefaceSpinner = findViewById(R.id.spinner_typeface);
    typefaceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            loadTypeface((Typeface) adapterView.getAdapter().getItem(i));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
    typefaceSpinner.setAdapter(new TypefaceAdapter(this));
    typefaceSpinner.setSelection(0);
}
Also used : Typeface(com.mta.tehreer.graphics.Typeface) Spinner(android.widget.Spinner) AdapterView(android.widget.AdapterView) GridView(android.widget.GridView) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ActionBar(androidx.appcompat.app.ActionBar)

Aggregations

Typeface (com.mta.tehreer.graphics.Typeface)12 ActionBar (androidx.appcompat.app.ActionBar)5 Paint (android.graphics.Paint)3 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 Spinner (android.widget.Spinner)3 Intent (android.content.Intent)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 Typesetter (com.mta.tehreer.layout.Typesetter)2 TypeSizeSpan (com.mta.tehreer.layout.style.TypeSizeSpan)2 TypefaceSpan (com.mta.tehreer.layout.style.TypefaceSpan)2 ShapingResult (com.mta.tehreer.sfnt.ShapingResult)2 WritingDirection (com.mta.tehreer.sfnt.WritingDirection)2 ArrayList (java.util.ArrayList)2 Drawable (android.graphics.drawable.Drawable)1 SpannableString (android.text.SpannableString)1 ReplacementSpan (android.text.style.ReplacementSpan)1 DisplayMetrics (android.util.DisplayMetrics)1 ViewTreeObserver (android.view.ViewTreeObserver)1