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.annotation.FloatRange;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.text.ParcelableSpan;
23 import android.text.TextPaint;
24 import android.text.TextUtils;
25 
26 /**
27  * Scales horizontally the size of the text to which it's attached by a certain factor.
28  * <p>
29  * Values > 1.0 will stretch the text wider. Values < 1.0 will stretch the text narrower.
30  * <p>
31  * For example, a <code>ScaleXSpan</code> that stretches the text size by 100% can be
32  * constructed like this:
33  * <pre>{@code
34  * SpannableString string = new SpannableString("Text with ScaleX span");
35  *string.setSpan(new ScaleXSpan(2f), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre>
36  * <img src="{@docRoot}reference/android/images/text/style/scalexspan.png" />
37  * <figcaption>Text scaled by 100% with <code>ScaleXSpan</code>.</figcaption>
38  */
39 public class ScaleXSpan extends MetricAffectingSpan implements ParcelableSpan {
40 
41     private final float mProportion;
42 
43     /**
44      * Creates a {@link ScaleXSpan} based on a proportion. Values > 1.0 will stretch the text wider.
45      * Values < 1.0 will stretch the text narrower.
46      *
47      * @param proportion the horizontal scale factor.
48      */
ScaleXSpan(@loatRangefrom = 0) float proportion)49     public ScaleXSpan(@FloatRange(from = 0) float proportion) {
50         mProportion = proportion;
51     }
52 
53     /**
54      * Creates a {@link ScaleXSpan} from a parcel.
55      */
ScaleXSpan(@onNull Parcel src)56     public ScaleXSpan(@NonNull Parcel src) {
57         mProportion = src.readFloat();
58     }
59 
60     @Override
getSpanTypeId()61     public int getSpanTypeId() {
62         return getSpanTypeIdInternal();
63     }
64 
65     /** @hide */
66     @Override
getSpanTypeIdInternal()67     public int getSpanTypeIdInternal() {
68         return TextUtils.SCALE_X_SPAN;
69     }
70 
71     @Override
describeContents()72     public int describeContents() {
73         return 0;
74     }
75 
76     @Override
writeToParcel(@onNull Parcel dest, int flags)77     public void writeToParcel(@NonNull Parcel dest, int flags) {
78         writeToParcelInternal(dest, flags);
79     }
80 
81     /** @hide */
82     @Override
writeToParcelInternal(@onNull Parcel dest, int flags)83     public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
84         dest.writeFloat(mProportion);
85     }
86 
87     /**
88      * Get the horizontal scale factor for the text.
89      *
90      * @return the horizontal scale factor.
91      */
getScaleX()92     public float getScaleX() {
93         return mProportion;
94     }
95 
96     @Override
updateDrawState(TextPaint ds)97     public void updateDrawState(TextPaint ds) {
98         ds.setTextScaleX(ds.getTextScaleX() * mProportion);
99     }
100 
101     @Override
updateMeasureState(TextPaint ds)102     public void updateMeasureState(TextPaint ds) {
103         ds.setTextScaleX(ds.getTextScaleX() * mProportion);
104     }
105 
106     @Override
toString()107     public String toString() {
108         return "ScaleXSpan{scaleX=" + getScaleX() + '}';
109     }
110 }
111