1/** 2 * Copyright (c) 2013, Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.mail.ui; 18 19import android.graphics.Paint; 20import android.graphics.Typeface; 21import android.text.TextPaint; 22import android.text.style.TypefaceSpan; 23 24/** 25 * CustomTypefaceSpan allows for the use of a non-framework font supplied in the 26 * assets/fonts directory of the application. Use this class whenever the 27 * framework does not contain a needed font. 28 */ 29public class CustomTypefaceSpan extends TypefaceSpan { 30 private final Typeface newType; 31 private int newColor; 32 private int newSize; 33 34 /** 35 * @param family Ignored since this uses a completely custom included font. 36 * @param type Typeface, specified as: Typeface.createFromAsset( 37 * context.getAssets(), "fonts/Roboto-Medium.ttf"), 38 * @param size Desired font size; this should have already been converted 39 * from a dimension. 40 * @param color Desired font color; this should have already been converted 41 * to an integer representation of a color. 42 */ 43 public CustomTypefaceSpan(String family, Typeface type, int size, int color) { 44 super(family); 45 newType = type; 46 newSize = size; 47 newColor = color; 48 } 49 50 @Override 51 public void updateDrawState(TextPaint ds) { 52 applyCustomTypeFace(ds, newType, newSize, newColor); 53 } 54 55 @Override 56 public void updateMeasureState(TextPaint paint) { 57 applyCustomTypeFace(paint, newType, newSize, newColor); 58 } 59 60 private static void applyCustomTypeFace(Paint paint, Typeface tf, int newSize, int newColor) { 61 int oldStyle; 62 Typeface old = paint.getTypeface(); 63 if (old == null) { 64 oldStyle = 0; 65 } else { 66 oldStyle = old.getStyle(); 67 } 68 69 int fake = oldStyle & ~tf.getStyle(); 70 if ((fake & Typeface.BOLD) != 0) { 71 paint.setFakeBoldText(true); 72 } 73 74 if ((fake & Typeface.ITALIC) != 0) { 75 paint.setTextSkewX(-0.25f); 76 } 77 78 paint.setTextSize(newSize); 79 paint.setColor(newColor); 80 paint.setTypeface(tf); 81 } 82}