Skip to content

Commit 517825f

Browse files
author
Fabrice Di Meglio
committed
Add Paint.setTextLocale()
- will be used for better shaping CJK and other goodies Change-Id: If64945a337edd915f5ebb88f04b6fd18e92ca587
1 parent 7405b90 commit 517825f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

api/current.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8495,6 +8495,7 @@ package android.graphics {
84958495
method public android.graphics.Paint.Align getTextAlign();
84968496
method public void getTextBounds(java.lang.String, int, int, android.graphics.Rect);
84978497
method public void getTextBounds(char[], int, int, android.graphics.Rect);
8498+
method public java.util.Locale getTextLocale();
84988499
method public void getTextPath(char[], int, int, float, float, android.graphics.Path);
84998500
method public void getTextPath(java.lang.String, int, int, float, float, android.graphics.Path);
85008501
method public float getTextScaleX();
@@ -8544,6 +8545,7 @@ package android.graphics {
85448545
method public void setStyle(android.graphics.Paint.Style);
85458546
method public void setSubpixelText(boolean);
85468547
method public void setTextAlign(android.graphics.Paint.Align);
8548+
method public void setTextLocale(java.util.Locale);
85478549
method public void setTextScaleX(float);
85488550
method public void setTextSize(float);
85498551
method public void setTextSkewX(float);

core/jni/android/graphics/Paint.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ class SkPaintGlue {
254254
obj->setTextAlign(align);
255255
}
256256

257+
static void setTextLocale(JNIEnv* env, jobject clazz, SkPaint* obj, jstring locale) {
258+
const char* localeArray = env->GetStringUTFChars(locale, NULL);
259+
SkString skLocale(localeArray);
260+
obj->setTextLocale(skLocale);
261+
env->ReleaseStringUTFChars(locale, localeArray);
262+
}
263+
257264
static jfloat getTextSize(JNIEnv* env, jobject paint) {
258265
NPE_CHECK_RETURN_ZERO(env, paint);
259266
return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize());
@@ -817,6 +824,7 @@ static JNINativeMethod methods[] = {
817824
{"native_setRasterizer","(II)I", (void*) SkPaintGlue::setRasterizer},
818825
{"native_getTextAlign","(I)I", (void*) SkPaintGlue::getTextAlign},
819826
{"native_setTextAlign","(II)V", (void*) SkPaintGlue::setTextAlign},
827+
{"native_setTextLocale","(ILjava/lang/String;)V", (void*) SkPaintGlue::setTextLocale},
820828
{"getTextSize","()F", (void*) SkPaintGlue::getTextSize},
821829
{"setTextSize","(F)V", (void*) SkPaintGlue::setTextSize},
822830
{"getTextScaleX","()F", (void*) SkPaintGlue::getTextScaleX},

graphics/java/android/graphics/Paint.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.text.SpannedString;
2222
import android.text.TextUtils;
2323

24+
import java.util.Locale;
25+
2426
/**
2527
* The Paint class holds the style and color information about how to draw
2628
* geometries, text and bitmaps.
@@ -44,6 +46,8 @@ public class Paint {
4446
private float mCompatScaling;
4547
private float mInvCompatScaling;
4648

49+
private Locale mLocale;
50+
4751
/**
4852
* @hide
4953
*/
@@ -348,6 +352,7 @@ public Paint(int flags) {
348352
// setHinting(DisplayMetrics.DENSITY_DEVICE >= DisplayMetrics.DENSITY_TV
349353
// ? HINTING_OFF : HINTING_ON);
350354
mCompatScaling = mInvCompatScaling = 1;
355+
mLocale = Locale.getDefault();
351356
}
352357

353358
/**
@@ -360,6 +365,7 @@ public Paint(int flags) {
360365
public Paint(Paint paint) {
361366
mNativePaint = native_initWithPaint(paint.mNativePaint);
362367
setClassVariablesFrom(paint);
368+
mLocale = paint.mLocale;
363369
}
364370

365371
/** Restores the paint to its default settings. */
@@ -373,6 +379,7 @@ public void reset() {
373379
mHasCompatScaling = false;
374380
mCompatScaling = mInvCompatScaling = 1;
375381
mBidiFlags = BIDI_DEFAULT_LTR;
382+
mLocale = Locale.getDefault();
376383
}
377384

378385
/**
@@ -412,6 +419,7 @@ private void setClassVariablesFrom(Paint paint) {
412419
shadowColor = paint.shadowColor;
413420

414421
mBidiFlags = paint.mBidiFlags;
422+
mLocale = paint.mLocale;
415423
}
416424

417425
/** @hide */
@@ -1044,6 +1052,36 @@ public void setTextAlign(Align align) {
10441052
native_setTextAlign(mNativePaint, align.nativeInt);
10451053
}
10461054

1055+
/**
1056+
* Get the text Locale.
1057+
*
1058+
* @return the paint's Locale used for drawing text, never null.
1059+
*/
1060+
public Locale getTextLocale() {
1061+
return mLocale;
1062+
}
1063+
1064+
/**
1065+
* Set the text locale.
1066+
*
1067+
* This controls how the text will be drawn. Providing the ROOT Locale (default case)
1068+
* means that the text will be drawn with the font corresponding to its script.
1069+
*
1070+
* Using the CHINESE or CHINA Locale means that the text will be drawn with a Chinese font.
1071+
* Using the JAPANESE or JAPAN Locale means that the text will be drawn with a Japanese font.
1072+
* Using the KOREAN or KOREA Locale means that the text will be drawn with a Korean font.
1073+
*
1074+
* @param locale the paint's locale value for drawing text, must not be null.
1075+
*/
1076+
public void setTextLocale(Locale locale) {
1077+
if (locale == null) {
1078+
throw new IllegalArgumentException("locale cannot be null");
1079+
}
1080+
if (locale.equals(mLocale)) return;
1081+
mLocale = locale;
1082+
native_setTextLocale(mNativePaint, locale.toString());
1083+
}
1084+
10471085
/**
10481086
* Return the paint's text size.
10491087
*
@@ -2144,6 +2182,9 @@ private static native int native_setRasterizer(int native_object,
21442182
private static native void native_setTextAlign(int native_object,
21452183
int align);
21462184

2185+
private static native void native_setTextLocale(int native_object,
2186+
String locale);
2187+
21472188
private static native int native_getTextWidths(int native_object,
21482189
char[] text, int index, int count, float[] widths);
21492190
private static native int native_getTextWidths(int native_object,

0 commit comments

Comments
 (0)