Java Code Examples for android.text.TextPaint#getTypeface()

The following examples show how to use android.text.TextPaint#getTypeface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuggestionStripLayoutHelper.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }
    final int length = text.length();
    final float[] widths = new float[length];
    final int count;
    final Typeface savedTypeface = paint.getTypeface();
    try {
        paint.setTypeface(getTextTypeface(text));
        count = paint.getTextWidths(text, 0, length, widths);
    } finally {
        paint.setTypeface(savedTypeface);
    }
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    return width;
}
 
Example 2
Source File: MongolTypefaceSpan.java    From mongol-library with MIT License 6 votes vote down vote up
private void apply(TextPaint paint) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }
    final int fakeStyle = oldStyle & ~typeface.getStyle();

    if ((fakeStyle & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fakeStyle & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(typeface);
}
 
Example 3
Source File: SuggestionStripLayoutHelper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }
    final int length = text.length();
    final float[] widths = new float[length];
    final int count;
    final Typeface savedTypeface = paint.getTypeface();
    try {
        paint.setTypeface(getTextTypeface(text));
        count = paint.getTextWidths(text, 0, length, widths);
    } finally {
        paint.setTypeface(savedTypeface);
    }
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    return width;
}
 
Example 4
Source File: CustomTypefaceSpan.java    From tns-core-modules-widgets with Apache License 2.0 6 votes vote down vote up
private void applyCustomTypeFace(TextPaint paint) {
    final Typeface old = paint.getTypeface();
    final int oldStyle = (old == null) ? 0 : old.getStyle();

    Typeface typeface = this.typeface;
    int fake = oldStyle & ~typeface.getStyle();
    if ((fake & android.graphics.Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & android.graphics.Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(typeface);
}
 
Example 5
Source File: SpanFormatter.java    From nfcard with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateDrawState(TextPaint ds) {
	ds.setTextSize(size);
	ds.setColor(color);

	if (face != null) {
		ds.setTypeface(face);
	} else if (bold) {
		Typeface tf = ds.getTypeface();

		if (tf != null) {
			int style = tf.getStyle() | Typeface.BOLD;
			tf = Typeface.create(tf, style);
			ds.setTypeface(tf);

			style &= ~tf.getStyle();

			if ((style & Typeface.BOLD) != 0) {
				ds.setFakeBoldText(true);
			}
		}
	}
}
 
Example 6
Source File: SpanFormatter.java    From NFCard with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateDrawState(TextPaint ds) {
	ds.setTextSize(size);
	ds.setColor(color);

	if (face != null) {
		ds.setTypeface(face);
	} else if (bold) {
		Typeface tf = ds.getTypeface();

		if (tf != null) {
			int style = tf.getStyle() | Typeface.BOLD;
			tf = Typeface.create(tf, style);
			ds.setTypeface(tf);

			style &= ~tf.getStyle();

			if ((style & Typeface.BOLD) != 0) {
				ds.setFakeBoldText(true);
			}
		}
	}
}
 
Example 7
Source File: SuggestionStripLayoutHelper.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }
    final int length = text.length();
    final float[] widths = new float[length];
    final int count;
    final Typeface savedTypeface = paint.getTypeface();
    try {
        paint.setTypeface(getTextTypeface(text));
        count = paint.getTextWidths(text, 0, length, widths);
    } finally {
        paint.setTypeface(savedTypeface);
    }
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    return width;
}
 
Example 8
Source File: FontSpan.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
@SuppressLint("WrongConstant")
private void update(TextPaint textPaint) {
    Typeface old = textPaint.getTypeface();
    int oldStyle = old != null ? old.getStyle() : 0;

    // Typeface is already cached at the system level
    // keep the style set before
    Typeface font = Typeface.create(this.font, oldStyle);
    textPaint.setTypeface(font);
}
 
Example 9
Source File: TextAppearanceSpan.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void updateMeasureState(TextPaint ds) {
    if (mTypeface != null || mStyle != 0) {
        Typeface tf = ds.getTypeface();
        int style = 0;

        if (tf != null) {
            style = tf.getStyle();
        }

        style |= mStyle;

        if (mTypeface != null) {
            tf = Typeface.create(mTypeface, style);
        } else if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }

        int fake = style & ~tf.getStyle();

        if ((fake & Typeface.BOLD) != 0) {
            ds.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            ds.setTextSkewX(-0.25f);
        }

        ds.setTypeface(tf);
    }

    if (mTextSize > 0) {
        ds.setTextSize(mTextSize);
    }
}
 
Example 10
Source File: TypefaceResourceSpan.java    From md2tv with MIT License 5 votes vote down vote up
@Override
public void updateMeasureState(TextPaint p) {
    Typeface old=p.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        p.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        p.setTextSkewX(-0.25f);
    }
    p.setTypeface(tf_);
}
 
Example 11
Source File: TypefaceResourceSpan.java    From md2tv with MIT License 5 votes vote down vote up
@Override
public void updateDrawState(TextPaint tp) {
    Typeface old=tp.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        tp.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        tp.setTextSkewX(-0.25f);
    }
    tp.setTypeface(tf_);
}
 
Example 12
Source File: TextAppearanceSpan.java    From JotaTextEditor with Apache License 2.0 5 votes vote down vote up
@Override
public void updateMeasureState(TextPaint ds) {
    if (mTypeface != null || mStyle != 0) {
        Typeface tf = ds.getTypeface();
        int style = 0;

        if (tf != null) {
            style = tf.getStyle();
        }

        style |= mStyle;

        if (mTypeface != null) {
            tf = Typeface.create(mTypeface, style);
        } else if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }

        int fake = style & ~tf.getStyle();

        if ((fake & Typeface.BOLD) != 0) {
            ds.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            ds.setTextSkewX(-0.25f);
        }

        ds.setTypeface(tf);
    }

    if (mTextSize > 0) {
        ds.setTextSize(mTextSize);
    }
}
 
Example 13
Source File: TextAppearanceSpan.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void updateMeasureState(TextPaint ds) {
    final Typeface styledTypeface;
    int style = 0;

    if (mTypeface != null) {
        style = mStyle;
        styledTypeface = Typeface.create(mTypeface, style);
    } else if (mFamilyName != null || mStyle != 0) {
        Typeface tf = ds.getTypeface();

        if (tf != null) {
            style = tf.getStyle();
        }

        style |= mStyle;

        if (mFamilyName != null) {
            styledTypeface = Typeface.create(mFamilyName, style);
        } else if (tf == null) {
            styledTypeface = Typeface.defaultFromStyle(style);
        } else {
            styledTypeface = Typeface.create(tf, style);
        }
    } else {
        styledTypeface = null;
    }

    if (styledTypeface != null) {
        int fake = style & ~styledTypeface.getStyle();

        if ((fake & Typeface.BOLD) != 0) {
            ds.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            ds.setTextSkewX(-0.25f);
        }

        ds.setTypeface(styledTypeface);
    }

    if (mTextSize > 0) {
        ds.setTextSize(mTextSize);
    }
}