Skip to content

Commit ac1cbaf

Browse files
Billy HewlettVictoria Lease
authored andcommitted
DO NOT MERGE Han Preference
Cherry-pick Ib5dd86950156c5a438f25c289acb839206bb455a from master. Data: label MTLmr3m with "ja" locale attribute, fallback_fonts-ja.xml removed, as we only need a single fallback font file Code: Add locale and variant to TextLayoutCache. Paint.java sets textLocale as the language (for example, "ja") rather than the language/locale concatenated (for example "ja_JP") This checkin, along with Change-Id: Id8c91ae0be6cad8a7ef77a0cd5803676290986c1, allows text view objects to set their locale dynamically and skia will use the correct font for the locale. Change-Id: Ieb60b0d7a39fcfef4f8ce90cd4f6065d33673710
1 parent bf5740e commit ac1cbaf

File tree

7 files changed

+74
-174
lines changed

7 files changed

+74
-174
lines changed

core/jni/android/graphics/Paint.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "jni.h"
2323
#include "GraphicsJNI.h"
2424
#include <android_runtime/AndroidRuntime.h>
25+
#include <ScopedUtfChars.h>
2526

2627
#include "SkBlurDrawLooper.h"
2728
#include "SkColorFilter.h"
@@ -30,6 +31,7 @@
3031
#include "SkShader.h"
3132
#include "SkTypeface.h"
3233
#include "SkXfermode.h"
34+
#include "unicode/uloc.h"
3335
#include "unicode/ushape.h"
3436
#include "TextLayout.h"
3537

@@ -254,11 +256,51 @@ class SkPaintGlue {
254256
obj->setTextAlign(align);
255257
}
256258

259+
// generate bcp47 identifier for the supplied locale
260+
static void toLanguageTag(char* output, size_t outSize,
261+
const char* locale) {
262+
if (output == NULL || outSize <= 0) {
263+
return;
264+
}
265+
if (locale == NULL) {
266+
output[0] = '\0';
267+
return;
268+
}
269+
char canonicalChars[ULOC_FULLNAME_CAPACITY];
270+
UErrorCode uErr = U_ZERO_ERROR;
271+
uloc_canonicalize(locale, canonicalChars, ULOC_FULLNAME_CAPACITY,
272+
&uErr);
273+
if (U_SUCCESS(uErr)) {
274+
char likelyChars[ULOC_FULLNAME_CAPACITY];
275+
uErr = U_ZERO_ERROR;
276+
uloc_addLikelySubtags(canonicalChars, likelyChars,
277+
ULOC_FULLNAME_CAPACITY, &uErr);
278+
if (U_SUCCESS(uErr)) {
279+
uErr = U_ZERO_ERROR;
280+
uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr);
281+
if (U_SUCCESS(uErr)) {
282+
return;
283+
} else {
284+
ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars,
285+
u_errorName(uErr));
286+
}
287+
} else {
288+
ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s",
289+
canonicalChars, u_errorName(uErr));
290+
}
291+
} else {
292+
ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale,
293+
u_errorName(uErr));
294+
}
295+
// unable to build a proper language identifier
296+
output[0] = '\0';
297+
}
298+
257299
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);
300+
ScopedUtfChars localeChars(env, locale);
301+
char langTag[ULOC_FULLNAME_CAPACITY];
302+
toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str());
303+
obj->setLanguage(SkLanguage(langTag));
262304
}
263305

264306
static jfloat getTextSize(JNIEnv* env, jobject paint) {

core/jni/android/graphics/TextLayoutCache.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "TextLayoutCache.h"
2020
#include "TextLayout.h"
2121
#include "SkFontHost.h"
22+
#include "SkTypeface_android.h"
2223
#include <unicode/unistr.h>
2324
#include <unicode/normlzr.h>
2425
#include <unicode/uchar.h>
@@ -224,7 +225,7 @@ void TextLayoutCache::dumpCacheStats() {
224225
*/
225226
TextLayoutCacheKey::TextLayoutCacheKey(): text(NULL), start(0), count(0), contextCount(0),
226227
dirFlags(0), typeface(NULL), textSize(0), textSkewX(0), textScaleX(0), flags(0),
227-
hinting(SkPaint::kNo_Hinting) {
228+
hinting(SkPaint::kNo_Hinting), variant(SkPaint::kDefault_Variant), language() {
228229
}
229230

230231
TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint, const UChar* text,
@@ -237,6 +238,8 @@ TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint, const UChar* text,
237238
textScaleX = paint->getTextScaleX();
238239
flags = paint->getFlags();
239240
hinting = paint->getHinting();
241+
variant = paint->getFontVariant();
242+
language = paint->getLanguage();
240243
}
241244

242245
TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) :
@@ -251,7 +254,9 @@ TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) :
251254
textSkewX(other.textSkewX),
252255
textScaleX(other.textScaleX),
253256
flags(other.flags),
254-
hinting(other.hinting) {
257+
hinting(other.hinting),
258+
variant(other.variant),
259+
language(other.language) {
255260
if (other.text) {
256261
textCopy.setTo(other.text, other.contextCount);
257262
}
@@ -288,6 +293,12 @@ int TextLayoutCacheKey::compare(const TextLayoutCacheKey& lhs, const TextLayoutC
288293
deltaInt = lhs.dirFlags - rhs.dirFlags;
289294
if (deltaInt) return (deltaInt);
290295

296+
deltaInt = lhs.variant - rhs.variant;
297+
if (deltaInt) return (deltaInt);
298+
299+
if (lhs.language < rhs.language) return -1;
300+
if (lhs.language > rhs.language) return +1;
301+
291302
return memcmp(lhs.getText(), rhs.getText(), lhs.contextCount * sizeof(UChar));
292303
}
293304

@@ -615,6 +626,8 @@ void TextLayoutShaper::computeRunValues(const SkPaint* paint, const UChar* chars
615626
mShapingPaint.setTextScaleX(paint->getTextScaleX());
616627
mShapingPaint.setFlags(paint->getFlags());
617628
mShapingPaint.setHinting(paint->getHinting());
629+
mShapingPaint.setFontVariant(paint->getFontVariant());
630+
mShapingPaint.setLanguage(paint->getLanguage());
618631

619632
// Split the BiDi run into Script runs. Harfbuzz will populate the pos, length and script
620633
// into the shaperItem

core/jni/android/graphics/TextLayoutCache.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <SkTemplates.h>
3333
#include <SkUtils.h>
3434
#include <SkAutoKern.h>
35-
#include "SkTypeface_android.h"
35+
#include <SkLanguage.h>
3636

3737
#include <unicode/ubidi.h>
3838
#include <unicode/ushape.h>
@@ -102,6 +102,8 @@ class TextLayoutCacheKey {
102102
SkScalar textScaleX;
103103
uint32_t flags;
104104
SkPaint::Hinting hinting;
105+
SkPaint::FontVariant variant;
106+
SkLanguage language;
105107

106108
inline const UChar* getText() const { return text ? text : textCopy.string(); }
107109

data/fonts/Android.mk

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,14 @@ LOCAL_MODULE_TAGS := optional
7676
LOCAL_MODULE_PATH := $(TARGET_OUT)/fonts
7777
include $(BUILD_PREBUILT)
7878

79-
include $(CLEAR_VARS)
80-
LOCAL_MODULE := fallback_fonts-ja.xml
81-
LOCAL_SRC_FILES := $(LOCAL_MODULE)
82-
LOCAL_MODULE_CLASS := ETC
83-
LOCAL_MODULE_TAGS := optional
84-
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
85-
include $(BUILD_PREBUILT)
86-
8779
droidsans_fallback_src := DroidSansFallbackFull.ttf
8880
extra_font_files := \
8981
DroidSans.ttf \
9082
DroidSans-Bold.ttf \
9183
DroidSansEthiopic-Regular.ttf \
9284
DroidSansTamil-Regular.ttf \
9385
DroidSansTamil-Bold.ttf \
94-
MTLmr3m.ttf \
95-
fallback_fonts-ja.xml
86+
MTLmr3m.ttf
9687
endif # SMALLER_FONT_FOOTPRINT
9788

9889
################################

data/fonts/fallback_fonts-ja.xml

Lines changed: 0 additions & 121 deletions
This file was deleted.

data/fonts/fallback_fonts.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
2525
Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters;
2626
their ordering in the fallback or vendor files gives priority to the first in the list.
27-
Locale-specific ordering can be configured by adding language and region codes to the end
28-
of the filename (e.g. /system/etc/fallback_fonts-ja.xml). When no region code is used,
29-
as with this example, all regions are matched. Use separate files for each supported locale.
30-
The standard fallback file (fallback_fonts.xml) is used when a locale does not have its own
31-
file. All fallback files must contain the same complete set of fonts; only their ordering
32-
can differ.
27+
Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to
28+
a "file" element; fonts matching the language of text being drawn will be prioritised over
29+
all others.
3330
-->
3431
<familyset>
3532
<family>
@@ -106,7 +103,7 @@
106103
</family>
107104
<family>
108105
<fileset>
109-
<file>MTLmr3m.ttf</file>
106+
<file lang="ja">MTLmr3m.ttf</file>
110107
</fileset>
111108
</family>
112109
<!--

data/fonts/vendor_fonts.xml

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
that in your makefile, this directory should be referenced as $(TARGET_COPY_OUT_VENDOR)/etc/:
88
99
PRODUCT_COPY_FILES += \
10-
frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml \
11-
frameworks/base/data/fonts/vendor_fonts-ja.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts-ja.xml
10+
frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml
1211
1312
For example, vendors might want to build configurations for locales that are
1413
better served by fonts which either handle glyphs not supported in the default fonts or which
@@ -32,32 +31,9 @@
3231
3332
Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters;
3433
their ordering in the fallback or vendor files gives priority to the first in the list.
35-
Locale-specific ordering can be configured by adding language and region codes to the end
36-
of the filename (e.g. /vendor/etc/fallback_fonts-ja.xml). When no region code is used,
37-
as with this example, all regions are matched. Use separate files for each supported locale.
38-
The standard fallback file (fallback_fonts.xml) is used when a locale does not have its own
39-
file. All fallback files must contain the same complete set of fonts; only their ordering
40-
can differ. For example, on a device supporting Japanese, but with English as the default,
41-
/vendor/etc/fallback_fonts.xml might contain:
42-
43-
<familyset>
44-
<family>
45-
<fileset>
46-
<file>DroidSansJapanese.ttf</file>
47-
</fileset>
48-
</family>
49-
</familyset>
50-
51-
placing the Japanese font at the end of the fallback sequence for English, with a corresponding
52-
/system/vendor/etc/fallback_fonts-ja.xml, placing it at the front of the list.
53-
54-
<familyset>
55-
<family order="0">
56-
<fileset>
57-
<file>DroidSansJapanese.ttf</file>
58-
</fileset>
59-
</family>
60-
</familyset>
34+
Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to
35+
a "file" element; fonts matching the language of text being drawn will be prioritised over
36+
all others.
6137
6238
The sample configuration below is an example of how one might provide two families of fonts
6339
that get inserted at the first and second (0 and 1) position in the overall fallback fonts.
@@ -82,4 +58,4 @@
8258
</fileset>
8359
</family>
8460
</familyset>
85-
-->
61+
--->

0 commit comments

Comments
 (0)