Home | History | Annotate | Download | only in style
      1 /*
      2  * Copyright (C) 2008 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.os.Parcel;
     20 import android.text.ParcelableSpan;
     21 import android.text.TextPaint;
     22 import android.text.TextUtils;
     23 
     24 public class AbsoluteSizeSpan extends MetricAffectingSpan implements ParcelableSpan {
     25 
     26     private final int mSize;
     27     private boolean mDip;
     28 
     29     /**
     30      * Set the text size to <code>size</code> physical pixels.
     31      */
     32     public AbsoluteSizeSpan(int size) {
     33         mSize = size;
     34     }
     35 
     36     /**
     37      * Set the text size to <code>size</code> physical pixels,
     38      * or to <code>size</code> device-independent pixels if
     39      * <code>dip</code> is true.
     40      */
     41     public AbsoluteSizeSpan(int size, boolean dip) {
     42         mSize = size;
     43         mDip = dip;
     44     }
     45 
     46     public AbsoluteSizeSpan(Parcel src) {
     47         mSize = src.readInt();
     48         mDip = src.readInt() != 0;
     49     }
     50 
     51     public int getSpanTypeId() {
     52         return TextUtils.ABSOLUTE_SIZE_SPAN;
     53     }
     54 
     55     public int describeContents() {
     56         return 0;
     57     }
     58 
     59     public void writeToParcel(Parcel dest, int flags) {
     60         dest.writeInt(mSize);
     61         dest.writeInt(mDip ? 1 : 0);
     62     }
     63 
     64     public int getSize() {
     65         return mSize;
     66     }
     67 
     68     public boolean getDip() {
     69         return mDip;
     70     }
     71 
     72     @Override
     73     public void updateDrawState(TextPaint ds) {
     74         if (mDip) {
     75             ds.setTextSize(mSize * ds.density);
     76         } else {
     77             ds.setTextSize(mSize);
     78         }
     79     }
     80 
     81     @Override
     82     public void updateMeasureState(TextPaint ds) {
     83         if (mDip) {
     84             ds.setTextSize(mSize * ds.density);
     85         } else {
     86             ds.setTextSize(mSize);
     87         }
     88     }
     89 }
     90