From 88455fa13f8683dfdbf5e6a823da4a44f373da4e Mon Sep 17 00:00:00 2001 From: Billy Hewlett Date: Wed, 18 Jul 2012 09:51:45 -0700 Subject: [PATCH] Han Preference 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: Ib5dd86950156c5a438f25c289acb839206bb455a --- core/jni/android/graphics/Paint.cpp | 50 +++++++- core/jni/android/graphics/TextLayoutCache.cpp | 17 ++- core/jni/android/graphics/TextLayoutCache.h | 4 +- data/fonts/Android.mk | 11 +- data/fonts/fallback_fonts-ja.xml | 121 ------------------ data/fonts/fallback_fonts.xml | 11 +- data/fonts/vendor_fonts.xml | 34 +---- 7 files changed, 74 insertions(+), 174 deletions(-) delete mode 100644 data/fonts/fallback_fonts-ja.xml diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp index a65262c0cb79f..edc9732202972 100644 --- a/core/jni/android/graphics/Paint.cpp +++ b/core/jni/android/graphics/Paint.cpp @@ -22,6 +22,7 @@ #include "jni.h" #include "GraphicsJNI.h" #include +#include #include "SkBlurDrawLooper.h" #include "SkColorFilter.h" @@ -30,6 +31,7 @@ #include "SkShader.h" #include "SkTypeface.h" #include "SkXfermode.h" +#include "unicode/uloc.h" #include "unicode/ushape.h" #include "TextLayout.h" @@ -254,11 +256,51 @@ public: obj->setTextAlign(align); } + // generate bcp47 identifier for the supplied locale + static void toLanguageTag(char* output, size_t outSize, + const char* locale) { + if (output == NULL || outSize <= 0) { + return; + } + if (locale == NULL) { + output[0] = '\0'; + return; + } + char canonicalChars[ULOC_FULLNAME_CAPACITY]; + UErrorCode uErr = U_ZERO_ERROR; + uloc_canonicalize(locale, canonicalChars, ULOC_FULLNAME_CAPACITY, + &uErr); + if (U_SUCCESS(uErr)) { + char likelyChars[ULOC_FULLNAME_CAPACITY]; + uErr = U_ZERO_ERROR; + uloc_addLikelySubtags(canonicalChars, likelyChars, + ULOC_FULLNAME_CAPACITY, &uErr); + if (U_SUCCESS(uErr)) { + uErr = U_ZERO_ERROR; + uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr); + if (U_SUCCESS(uErr)) { + return; + } else { + ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, + u_errorName(uErr)); + } + } else { + ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", + canonicalChars, u_errorName(uErr)); + } + } else { + ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale, + u_errorName(uErr)); + } + // unable to build a proper language identifier + output[0] = '\0'; + } + static void setTextLocale(JNIEnv* env, jobject clazz, SkPaint* obj, jstring locale) { - const char* localeArray = env->GetStringUTFChars(locale, NULL); - SkString skLocale(localeArray); - obj->setTextLocale(skLocale); - env->ReleaseStringUTFChars(locale, localeArray); + ScopedUtfChars localeChars(env, locale); + char langTag[ULOC_FULLNAME_CAPACITY]; + toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str()); + obj->setLanguage(SkLanguage(langTag)); } static jfloat getTextSize(JNIEnv* env, jobject paint) { diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp index 9b9b9919f5f46..7abfcf1efaa5f 100644 --- a/core/jni/android/graphics/TextLayoutCache.cpp +++ b/core/jni/android/graphics/TextLayoutCache.cpp @@ -19,6 +19,7 @@ #include "TextLayoutCache.h" #include "TextLayout.h" #include "SkFontHost.h" +#include "SkTypeface_android.h" #include #include #include @@ -224,7 +225,7 @@ void TextLayoutCache::dumpCacheStats() { */ TextLayoutCacheKey::TextLayoutCacheKey(): text(NULL), start(0), count(0), contextCount(0), dirFlags(0), typeface(NULL), textSize(0), textSkewX(0), textScaleX(0), flags(0), - hinting(SkPaint::kNo_Hinting) { + hinting(SkPaint::kNo_Hinting), variant(SkPaint::kDefault_Variant), language() { } TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint, const UChar* text, @@ -237,6 +238,8 @@ TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint, const UChar* text, textScaleX = paint->getTextScaleX(); flags = paint->getFlags(); hinting = paint->getHinting(); + variant = paint->getFontVariant(); + language = paint->getLanguage(); } TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) : @@ -251,7 +254,9 @@ TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) : textSkewX(other.textSkewX), textScaleX(other.textScaleX), flags(other.flags), - hinting(other.hinting) { + hinting(other.hinting), + variant(other.variant), + language(other.language) { if (other.text) { textCopy.setTo(other.text, other.contextCount); } @@ -288,6 +293,12 @@ int TextLayoutCacheKey::compare(const TextLayoutCacheKey& lhs, const TextLayoutC deltaInt = lhs.dirFlags - rhs.dirFlags; if (deltaInt) return (deltaInt); + deltaInt = lhs.variant - rhs.variant; + if (deltaInt) return (deltaInt); + + if (lhs.language < rhs.language) return -1; + if (lhs.language > rhs.language) return +1; + return memcmp(lhs.getText(), rhs.getText(), lhs.contextCount * sizeof(UChar)); } @@ -615,6 +626,8 @@ void TextLayoutShaper::computeRunValues(const SkPaint* paint, const UChar* chars mShapingPaint.setTextScaleX(paint->getTextScaleX()); mShapingPaint.setFlags(paint->getFlags()); mShapingPaint.setHinting(paint->getHinting()); + mShapingPaint.setFontVariant(paint->getFontVariant()); + mShapingPaint.setLanguage(paint->getLanguage()); // Split the BiDi run into Script runs. Harfbuzz will populate the pos, length and script // into the shaperItem diff --git a/core/jni/android/graphics/TextLayoutCache.h b/core/jni/android/graphics/TextLayoutCache.h index f007f9a34f081..64b33a0a2c3d2 100644 --- a/core/jni/android/graphics/TextLayoutCache.h +++ b/core/jni/android/graphics/TextLayoutCache.h @@ -32,7 +32,7 @@ #include #include #include -#include "SkTypeface_android.h" +#include #include #include @@ -102,6 +102,8 @@ private: SkScalar textScaleX; uint32_t flags; SkPaint::Hinting hinting; + SkPaint::FontVariant variant; + SkLanguage language; inline const UChar* getText() const { return text ? text : textCopy.string(); } diff --git a/data/fonts/Android.mk b/data/fonts/Android.mk index 3d6c9d3ff8ade..7bc172c8d0e69 100644 --- a/data/fonts/Android.mk +++ b/data/fonts/Android.mk @@ -76,14 +76,6 @@ LOCAL_MODULE_TAGS := optional LOCAL_MODULE_PATH := $(TARGET_OUT)/fonts include $(BUILD_PREBUILT) -include $(CLEAR_VARS) -LOCAL_MODULE := fallback_fonts-ja.xml -LOCAL_SRC_FILES := $(LOCAL_MODULE) -LOCAL_MODULE_CLASS := ETC -LOCAL_MODULE_TAGS := optional -LOCAL_MODULE_PATH := $(TARGET_OUT_ETC) -include $(BUILD_PREBUILT) - droidsans_fallback_src := DroidSansFallbackFull.ttf extra_font_files := \ DroidSans.ttf \ @@ -91,8 +83,7 @@ extra_font_files := \ DroidSansEthiopic-Regular.ttf \ DroidSansTamil-Regular.ttf \ DroidSansTamil-Bold.ttf \ - MTLmr3m.ttf \ - fallback_fonts-ja.xml + MTLmr3m.ttf endif # SMALLER_FONT_FOOTPRINT ################################ diff --git a/data/fonts/fallback_fonts-ja.xml b/data/fonts/fallback_fonts-ja.xml deleted file mode 100644 index 82e3a381a56b2..0000000000000 --- a/data/fonts/fallback_fonts-ja.xml +++ /dev/null @@ -1,121 +0,0 @@ - - - - - - DroidNaskh-Regular.ttf - - - - - DroidNaskh-Regular-SystemUI.ttf - - - - - DroidSansEthiopic-Regular.ttf - - - - - DroidSansHebrew-Regular.ttf - DroidSansHebrew-Bold.ttf - - - - - DroidSansThai.ttf - - - - - DroidSansArmenian.ttf - - - - - DroidSansGeorgian.ttf - - - - - DroidSansDevanagari-Regular.ttf - - - - - DroidSansTamil-Regular.ttf - DroidSansTamil-Bold.ttf - - - - - AnjaliNewLipi-light.ttf - - - - - Lohit-Bengali.ttf - - - - - Lohit-Kannada.ttf - - - - - AndroidEmoji.ttf - - - - - MTLmr3m.ttf - - - - - DroidSansFallback.ttf - - - - - - Lohit-Telugu.ttf - - - diff --git a/data/fonts/fallback_fonts.xml b/data/fonts/fallback_fonts.xml index 758adb5e62a99..2c9a0c806c11d 100644 --- a/data/fonts/fallback_fonts.xml +++ b/data/fonts/fallback_fonts.xml @@ -24,12 +24,9 @@ Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters; their ordering in the fallback or vendor files gives priority to the first in the list. - Locale-specific ordering can be configured by adding language and region codes to the end - of the filename (e.g. /system/etc/fallback_fonts-ja.xml). When no region code is used, - as with this example, all regions are matched. Use separate files for each supported locale. - The standard fallback file (fallback_fonts.xml) is used when a locale does not have its own - file. All fallback files must contain the same complete set of fonts; only their ordering - can differ. + Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to + a "file" element; fonts matching the language of text being drawn will be prioritised over + all others. --> @@ -106,7 +103,7 @@ - MTLmr3m.ttf + MTLmr3m.ttf \ No newline at end of file +--->