Home | History | Annotate | Download | only in style
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      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 
     17 package android.text.style;
     18 
     19 import android.graphics.Paint;
     20 import android.graphics.Typeface;
     21 import android.os.Parcel;
     22 import android.text.ParcelableSpan;
     23 import android.text.TextPaint;
     24 import android.text.TextUtils;
     25 
     26 /**
     27  * Changes the typeface family of the text to which the span is attached.
     28  */
     29 public class TypefaceSpan extends MetricAffectingSpan implements ParcelableSpan {
     30     private final String mFamily;
     31 
     32     /**
     33      * @param family The font family for this typeface.  Examples include
     34      * "monospace", "serif", and "sans-serif".
     35      */
     36     public TypefaceSpan(String family) {
     37         mFamily = family;
     38     }
     39 
     40     public TypefaceSpan(Parcel src) {
     41         mFamily = src.readString();
     42     }
     43 
     44     public int getSpanTypeId() {
     45         return TextUtils.TYPEFACE_SPAN;
     46     }
     47 
     48     public int describeContents() {
     49         return 0;
     50     }
     51 
     52     public void writeToParcel(Parcel dest, int flags) {
     53         dest.writeString(mFamily);
     54     }
     55 
     56     /**
     57      * Returns the font family name.
     58      */
     59     public String getFamily() {
     60         return mFamily;
     61     }
     62 
     63     @Override
     64     public void updateDrawState(TextPaint ds) {
     65         apply(ds, mFamily);
     66     }
     67 
     68     @Override
     69     public void updateMeasureState(TextPaint paint) {
     70         apply(paint, mFamily);
     71     }
     72 
     73     private static void apply(Paint paint, String family) {
     74         int oldStyle;
     75 
     76         Typeface old = paint.getTypeface();
     77         if (old == null) {
     78             oldStyle = 0;
     79         } else {
     80             oldStyle = old.getStyle();
     81         }
     82 
     83         Typeface tf = Typeface.create(family, oldStyle);
     84         int fake = oldStyle & ~tf.getStyle();
     85 
     86         if ((fake & Typeface.BOLD) != 0) {
     87             paint.setFakeBoldText(true);
     88         }
     89 
     90         if ((fake & Typeface.ITALIC) != 0) {
     91             paint.setTextSkewX(-0.25f);
     92         }
     93 
     94         paint.setTypeface(tf);
     95     }
     96 }
     97