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  *
     28  * Describes a style in a span.
     29  * Note that styles are cumulative -- if both bold and italic are set in
     30  * separate spans, or if the base style is bold and a span calls for italic,
     31  * you get bold italic.  You can't turn off a style from the base style.
     32  *
     33  */
     34 public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan {
     35 
     36 	private final int mStyle;
     37 
     38 	/**
     39 	 *
     40 	 * @param style An integer constant describing the style for this span. Examples
     41 	 * include bold, italic, and normal. Values are constants defined
     42 	 * in {@link android.graphics.Typeface}.
     43 	 */
     44 	public StyleSpan(int style) {
     45 		mStyle = style;
     46 	}
     47 
     48     public StyleSpan(Parcel src) {
     49         mStyle = src.readInt();
     50     }
     51 
     52     public int getSpanTypeId() {
     53         return TextUtils.STYLE_SPAN;
     54     }
     55 
     56     public int describeContents() {
     57         return 0;
     58     }
     59 
     60     public void writeToParcel(Parcel dest, int flags) {
     61         dest.writeInt(mStyle);
     62     }
     63 
     64 	/**
     65 	 * Returns the style constant defined in {@link android.graphics.Typeface}.
     66 	 */
     67 	public int getStyle() {
     68 		return mStyle;
     69 	}
     70 
     71 	@Override
     72     public void updateDrawState(TextPaint ds) {
     73         apply(ds, mStyle);
     74     }
     75 
     76 	@Override
     77     public void updateMeasureState(TextPaint paint) {
     78         apply(paint, mStyle);
     79     }
     80 
     81     private static void apply(Paint paint, int style) {
     82         int oldStyle;
     83 
     84         Typeface old = paint.getTypeface();
     85         if (old == null) {
     86             oldStyle = 0;
     87         } else {
     88             oldStyle = old.getStyle();
     89         }
     90 
     91         int want = oldStyle | style;
     92 
     93         Typeface tf;
     94         if (old == null) {
     95             tf = Typeface.defaultFromStyle(want);
     96         } else {
     97             tf = Typeface.create(old, want);
     98         }
     99 
    100         int fake = want & ~tf.getStyle();
    101 
    102         if ((fake & Typeface.BOLD) != 0) {
    103             paint.setFakeBoldText(true);
    104         }
    105 
    106         if ((fake & Typeface.ITALIC) != 0) {
    107             paint.setTextSkewX(-0.25f);
    108         }
    109 
    110         paint.setTypeface(tf);
    111     }
    112 }
    113