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
17package android.text.style;
18
19import android.os.Parcel;
20import android.text.ParcelableSpan;
21import android.text.TextPaint;
22import android.text.TextUtils;
23
24public 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 getSpanTypeIdInternal();
53    }
54
55    /** @hide */
56    public int getSpanTypeIdInternal() {
57        return TextUtils.ABSOLUTE_SIZE_SPAN;
58    }
59
60    public int describeContents() {
61        return 0;
62    }
63
64    public void writeToParcel(Parcel dest, int flags) {
65        writeToParcelInternal(dest, flags);
66    }
67
68    /** @hide */
69    public void writeToParcelInternal(Parcel dest, int flags) {
70        dest.writeInt(mSize);
71        dest.writeInt(mDip ? 1 : 0);
72    }
73
74    public int getSize() {
75        return mSize;
76    }
77
78    public boolean getDip() {
79        return mDip;
80    }
81
82    @Override
83    public void updateDrawState(TextPaint ds) {
84        if (mDip) {
85            ds.setTextSize(mSize * ds.density);
86        } else {
87            ds.setTextSize(mSize);
88        }
89    }
90
91    @Override
92    public void updateMeasureState(TextPaint ds) {
93        if (mDip) {
94            ds.setTextSize(mSize * ds.density);
95        } else {
96            ds.setTextSize(mSize);
97        }
98    }
99}
100