Skip to content

Commit df8ef4b

Browse files
author
Victoria Lease
committed
DO NOT MERGE LocaleSpan makes Han disambiguation easy!
Cherry-pick of I7f1b0d49a2ece957a7b9b5d65d48385bf2c2a668 from master. I've also provided TextView.setTextLocale() for use in single-language TextViews. Change-Id: I5692859bfd2aafc284172454d943afc250b22535
1 parent 6fb73ab commit df8ef4b

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

api/current.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22503,6 +22503,17 @@ package android.text.style {
2250322503
method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt, android.text.TextPaint);
2250422504
}
2250522505

22506+
public class LocaleSpan extends android.text.style.MetricAffectingSpan implements android.text.ParcelableSpan {
22507+
ctor public LocaleSpan(java.util.Locale);
22508+
ctor public LocaleSpan(android.os.Parcel);
22509+
method public int describeContents();
22510+
method public java.util.Locale getLocale();
22511+
method public int getSpanTypeId();
22512+
method public void updateDrawState(android.text.TextPaint);
22513+
method public void updateMeasureState(android.text.TextPaint);
22514+
method public void writeToParcel(android.os.Parcel, int);
22515+
}
22516+
2250622517
public class MaskFilterSpan extends android.text.style.CharacterStyle implements android.text.style.UpdateAppearance {
2250722518
ctor public MaskFilterSpan(android.graphics.MaskFilter);
2250822519
method public android.graphics.MaskFilter getMaskFilter();
@@ -29303,6 +29314,7 @@ package android.widget {
2930329314
method public static int getTextColor(android.content.Context, android.content.res.TypedArray, int);
2930429315
method public final android.content.res.ColorStateList getTextColors();
2930529316
method public static android.content.res.ColorStateList getTextColors(android.content.Context, android.content.res.TypedArray);
29317+
method public java.util.Locale getTextLocale();
2930629318
method public float getTextScaleX();
2930729319
method public float getTextSize();
2930829320
method public int getTotalPaddingBottom();
@@ -29405,6 +29417,7 @@ package android.widget {
2940529417
method public void setTextIsSelectable(boolean);
2940629418
method public final void setTextKeepState(java.lang.CharSequence);
2940729419
method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType);
29420+
method public void setTextLocale(java.util.Locale);
2940829421
method public void setTextScaleX(float);
2940929422
method public void setTextSize(float);
2941029423
method public void setTextSize(int, float);

core/java/android/text/TextUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.text.style.EasyEditSpan;
2828
import android.text.style.ForegroundColorSpan;
2929
import android.text.style.LeadingMarginSpan;
30+
import android.text.style.LocaleSpan;
3031
import android.text.style.MetricAffectingSpan;
3132
import android.text.style.QuoteSpan;
3233
import android.text.style.RelativeSizeSpan;
@@ -587,6 +588,8 @@ public void getChars(int start, int end, char[] dest, int destoff) {
587588
public static final int SUGGESTION_RANGE_SPAN = 21;
588589
/** @hide */
589590
public static final int EASY_EDIT_SPAN = 22;
591+
/** @hide */
592+
public static final int LOCALE_SPAN = 23;
590593

591594
/**
592595
* Flatten a CharSequence and whatever styles can be copied across processes
@@ -754,6 +757,10 @@ public CharSequence createFromParcel(Parcel p) {
754757
readSpan(p, sp, new EasyEditSpan());
755758
break;
756759

760+
case LOCALE_SPAN:
761+
readSpan(p, sp, new LocaleSpan(p));
762+
break;
763+
757764
default:
758765
throw new RuntimeException("bogus span encoding " + kind);
759766
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2012 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.os.Parcel;
21+
import android.text.ParcelableSpan;
22+
import android.text.TextPaint;
23+
import android.text.TextUtils;
24+
import java.util.Locale;
25+
26+
/**
27+
* Changes the {@link Locale} of the text to which the span is attached.
28+
*/
29+
public class LocaleSpan extends MetricAffectingSpan implements ParcelableSpan {
30+
private final Locale mLocale;
31+
32+
/**
33+
* Creates a LocaleSpan.
34+
* @param locale The {@link Locale} of the text to which the span is
35+
* attached.
36+
*/
37+
public LocaleSpan(Locale locale) {
38+
mLocale = locale;
39+
}
40+
41+
public LocaleSpan(Parcel src) {
42+
mLocale = new Locale(src.readString(), src.readString(), src.readString());
43+
}
44+
45+
@Override
46+
public int getSpanTypeId() {
47+
return TextUtils.LOCALE_SPAN;
48+
}
49+
50+
@Override
51+
public int describeContents() {
52+
return 0;
53+
}
54+
55+
@Override
56+
public void writeToParcel(Parcel dest, int flags) {
57+
dest.writeString(mLocale.getLanguage());
58+
dest.writeString(mLocale.getCountry());
59+
dest.writeString(mLocale.getVariant());
60+
}
61+
62+
/**
63+
* Returns the {@link Locale}.
64+
*
65+
* @return The {@link Locale} for this span.
66+
*/
67+
public Locale getLocale() {
68+
return mLocale;
69+
}
70+
71+
@Override
72+
public void updateDrawState(TextPaint ds) {
73+
apply(ds, mLocale);
74+
}
75+
76+
@Override
77+
public void updateMeasureState(TextPaint paint) {
78+
apply(paint, mLocale);
79+
}
80+
81+
private static void apply(Paint paint, Locale locale) {
82+
paint.setTextLocale(locale);
83+
}
84+
}

core/java/android/widget/TextView.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,27 @@ public void setTextAppearance(Context context, int resid) {
22022202
appearance.recycle();
22032203
}
22042204

2205+
/**
2206+
* Get the default {@link Locale} of the text in this TextView.
2207+
* @return the default {@link Locale} of the text in this TextView.
2208+
*/
2209+
public Locale getTextLocale() {
2210+
return mTextPaint.getTextLocale();
2211+
}
2212+
2213+
/**
2214+
* Set the default {@link Locale} of the text in this TextView to the given value. This value
2215+
* is used to choose appropriate typefaces for ambiguous characters. Typically used for CJK
2216+
* locales to disambiguate Hanzi/Kanji/Hanja characters.
2217+
*
2218+
* @param locale the {@link Locale} for drawing text, must not be null.
2219+
*
2220+
* @see Paint#setTextLocale
2221+
*/
2222+
public void setTextLocale(Locale locale) {
2223+
mTextPaint.setTextLocale(locale);
2224+
}
2225+
22052226
/**
22062227
* @return the size (in pixels) of the default text size in this TextView.
22072228
*/

0 commit comments

Comments
 (0)