Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9a629e24 authored by Jerome Gaillard's avatar Jerome Gaillard
Browse files

Add android_text_Hyphenator to libandroid_runtime for host

android_text_Hyphenator relies on mmap to do memory mapping of the
hyphenation pattern files. This is not supported on all host platforms.
Instead, this uses the MappedFile class from libbase which is supported
on all platforms.
In addition, this allows for customization of the location of the
pattern files by using a system property instead of a hardcoded value.

Flag: NONE host-only change
Bug: 353457304
Test: build libandroid_runtime on host
Change-Id: I2c8f345fb6172c0f719732517b8c84a61109de27
parent dd5840f5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ cc_library_shared_for_libandroid_runtime {
        "android_os_SystemClock.cpp",
        "android_os_SystemProperties.cpp",
        "android_text_AndroidCharacter.cpp",
        "android_text_Hyphenator.cpp",
        "android_util_AssetManager.cpp",
        "android_util_EventLog.cpp",
        "android_util_Log.cpp",
@@ -166,7 +167,6 @@ cc_library_shared_for_libandroid_runtime {
                "android_view_SurfaceSession.cpp",
                "android_view_TextureView.cpp",
                "android_view_TunnelModeEnabledListener.cpp",
                "android_text_Hyphenator.cpp",
                "android_os_Debug.cpp",
                "android_os_GraphicsEnvironment.cpp",
                "android_os_HidlMemory.cpp",
+27 −0
Original line number Diff line number Diff line
@@ -18,10 +18,17 @@
#include <cutils/trace.h>
#include <fcntl.h>
#include <minikin/Hyphenator.h>
#ifdef __ANDROID__
#include <sys/mman.h>
#else
#include <android-base/mapped_file.h>
#include <android-base/properties.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __ANDROID__
#include <tracing_perfetto.h>
#endif
#include <unicode/uloc.h>
#include <unistd.h>

@@ -30,7 +37,12 @@
namespace android {

static std::string buildFileName(const std::string& locale) {
#ifdef __ANDROID__
    constexpr char SYSTEM_HYPHENATOR_PREFIX[] = "/system/usr/hyphen-data/hyph-";
#else
    std::string hyphenPath = base::GetProperty("ro.hyphen.data.dir", "/system/usr/hyphen-data");
    std::string SYSTEM_HYPHENATOR_PREFIX = hyphenPath + "/hyph-";
#endif
    constexpr char SYSTEM_HYPHENATOR_SUFFIX[] = ".hyb";
    std::string lowerLocale;
    lowerLocale.reserve(locale.size());
@@ -51,11 +63,22 @@ static std::pair<const uint8_t*, size_t> mmapPatternFile(const std::string& loca
        return std::make_pair(nullptr, 0);
    }

#ifdef __ANDROID__
    void* ptr = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0 /* offset */);
    close(fd);
    if (ptr == MAP_FAILED) {
        return std::make_pair(nullptr, 0);
    }
#else
    std::unique_ptr<base::MappedFile> patternFile =
            base::MappedFile::FromFd(fd, 0, st.st_size, PROT_READ);
    close(fd);
    if (patternFile == nullptr) {
        return std::make_pair(nullptr, 0);
    }
    auto* mappedPtr = new base::MappedFile(std::move(*patternFile));
    char* ptr = mappedPtr->data();
#endif
    return std::make_pair(reinterpret_cast<const uint8_t*>(ptr), st.st_size);
}

@@ -210,9 +233,13 @@ static void init() {
    addHyphenatorAlias("und-Taml", "ta");  // Tamil
    addHyphenatorAlias("und-Telu", "te");  // Telugu

#ifdef __ANDROID__
    tracing_perfetto::traceBegin(ATRACE_TAG_VIEW, "CacheUnicodeExtensionSubtagsKeyMap");
#endif
    cacheUnicodeExtensionSubtagsKeyMap();
#ifdef __ANDROID__
    tracing_perfetto::traceEnd(ATRACE_TAG_VIEW); // CacheUnicodeExtensionSubtagsKeyMap
#endif
}

static const JNINativeMethod gMethods[] = {
+2 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ extern int register_android_os_Parcel(JNIEnv* env);
extern int register_android_os_SystemClock(JNIEnv* env);
extern int register_android_os_SystemProperties(JNIEnv* env);
extern int register_android_text_AndroidCharacter(JNIEnv* env);
extern int register_android_text_Hyphenator(JNIEnv* env);
extern int register_android_util_EventLog(JNIEnv* env);
extern int register_android_util_Log(JNIEnv* env);
extern int register_android_util_jar_StrictJarFile(JNIEnv* env);
@@ -133,6 +134,7 @@ static const std::unordered_map<std::string, RegJNIRec> gRegJNIMap = {
        {"android.os.SystemClock", REG_JNI(register_android_os_SystemClock)},
        {"android.os.SystemProperties", REG_JNI(register_android_os_SystemProperties)},
        {"android.text.AndroidCharacter", REG_JNI(register_android_text_AndroidCharacter)},
        {"android.text.Hyphenator", REG_JNI(register_android_text_Hyphenator)},
        {"android.util.EventLog", REG_JNI(register_android_util_EventLog)},
        {"android.util.Log", REG_JNI(register_android_util_Log)},
        {"android.util.jar.StrictJarFile", REG_JNI(register_android_util_jar_StrictJarFile)},