diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java index 21d4c175380cd5..48ae279dc060d4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java @@ -7,6 +7,7 @@ package com.facebook.react.views.text; +import java.io.File; import java.util.HashMap; import java.util.Map; @@ -42,10 +43,12 @@ public class ReactFontManager { final private Map mFontCache; final private Map mCustomTypefaceCache; + final private Map mFontFileNameTypefaceCache; private ReactFontManager() { mFontCache = new HashMap<>(); mCustomTypefaceCache = new HashMap<>(); + mFontFileNameTypefaceCache = new HashMap<>(); } public static ReactFontManager getInstance() { @@ -62,6 +65,21 @@ public static ReactFontManager getInstance() { return getTypeface(fontFamilyName, style, 0, assetManager); } + public @Nullable Typeface getTypeface( + String fontPath, + String fontFamilyName, + int style) { + String fileName = fontPath.substring(fontPath.lastIndexOf(File.separator) + 1); + Typeface typeface = mFontFileNameTypefaceCache.get(fileName); + if (typeface == null) { + typeface = createTypeface(fontPath, fontFamilyName, style); + if (typeface != null) { + mFontFileNameTypefaceCache.put(fileName, typeface); + } + } + return typeface; + } + public @Nullable Typeface getTypeface( String fontFamilyName, int style, @@ -149,6 +167,20 @@ public void setTypeface(String fontFamilyName, int style, Typeface typeface) { return Typeface.create(fontFamilyName, style); } + private static + @Nullable Typeface createTypeface( + String fontPath, + String fontFamilyName, + int style) { + try { + return Typeface.createFromFile(fontPath); + } catch (RuntimeException e) { + // unfortunately Typeface.createFromFile throws an exception instead of returning null + // if the typeface doesn't exist + } + return Typeface.create(fontFamilyName, style); + } + private static class FontFamily { private SparseArray mTypefaceSparseArray; diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index 2cc5250f717d6d..25899d54a33bc9 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -49,7 +49,7 @@ void Instance::initializeBridge( std::shared_ptr moduleRegistry) { callback_ = std::move(callback); moduleRegistry_ = std::move(moduleRegistry); - + std::shared_ptr delegate; if (edf) { delegate = edf->createExecutorDelegate(moduleRegistry_, callback_); @@ -57,7 +57,7 @@ void Instance::initializeBridge( jsQueue->runOnQueueSync([this, delegate, &jsef, jsQueue]() mutable { nativeToJsBridge_ = folly::make_unique( - jsef.get(), delegate, moduleRegistry_, jsQueue, callback_); + jsef.get(), delegate, moduleRegistry_, jsQueue, callback_, jseConfigParams_); std::lock_guard lock(m_syncMutex); m_syncReady = true; @@ -183,7 +183,7 @@ void *Instance::getJavaScriptContext() { bool Instance::isInspectable() { return nativeToJsBridge_ ? nativeToJsBridge_->isInspectable() : false; } - + bool Instance::isBatchActive() { return nativeToJsBridge_ ? nativeToJsBridge_->isBatchActive() : false; } @@ -201,6 +201,10 @@ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic &¶ms) { nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params)); } +void Instance::setJSEConfigParams(std::shared_ptr&& jseConfigParams) { + jseConfigParams_ = std::move(jseConfigParams); +} + void Instance::registerBundle(uint32_t bundleId, const std::string& bundlePath) { nativeToJsBridge_->registerBundle(bundleId, bundlePath); } diff --git a/ReactCommon/cxxreact/Instance.h b/ReactCommon/cxxreact/Instance.h index 4ff8d7f9129395..ad8473b125ae71 100644 --- a/ReactCommon/cxxreact/Instance.h +++ b/ReactCommon/cxxreact/Instance.h @@ -83,6 +83,7 @@ class RN_EXPORT Instance { void callJSFunction(std::string &&module, std::string &&method, folly::dynamic &¶ms); void callJSCallback(uint64_t callbackId, folly::dynamic &¶ms); + virtual void setJSEConfigParams(std::shared_ptr&& jseConfigParams); // This method is experimental, and may be modified or removed. void registerBundle(uint32_t bundleId, const std::string& bundlePath); @@ -117,6 +118,7 @@ class RN_EXPORT Instance { std::shared_ptr callback_; std::unique_ptr nativeToJsBridge_; std::shared_ptr moduleRegistry_; + std::shared_ptr jseConfigParams_; std::mutex m_syncMutex; std::condition_variable m_syncCV; diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index b9f80f9525dee7..7f858bf6b55b58 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -37,7 +37,7 @@ class JsToNativeBridge : public react::ExecutorDelegate { std::shared_ptr getModuleRegistry() override { return m_registry; } - + virtual bool isBatchActive() override { return m_batchHadNativeModuleCalls; } @@ -88,12 +88,12 @@ NativeToJsBridge::NativeToJsBridge( std::shared_ptr delegate, // TODO(OSS Candidate ISS#2710739) std::shared_ptr registry, std::shared_ptr jsQueue, - std::shared_ptr callback) + std::shared_ptr callback, + std::shared_ptr jseConfigParams) : m_destroyed(std::make_shared(false)), m_delegate(delegate ? delegate : (std::make_shared(registry, callback))), - m_executor(jsExecutorFactory->createJSExecutor(m_delegate, jsQueue)), - m_executorMessageQueueThread(std::move(jsQueue)), - m_inspectable(m_executor->isInspectable()) {} + m_executor(jsExecutorFactory->createJSExecutor(m_delegate, jsQueue, std::move(jseConfigParams))), + m_executorMessageQueueThread(std::move(jsQueue)) {} // This must be called on the same thread on which the constructor was called. NativeToJsBridge::~NativeToJsBridge() { @@ -239,7 +239,7 @@ void* NativeToJsBridge::getJavaScriptContext() { bool NativeToJsBridge::isInspectable() { return m_inspectable; } - + bool NativeToJsBridge::isBatchActive() { return m_delegate->isBatchActive(); } diff --git a/ReactCommon/cxxreact/NativeToJsBridge.h b/ReactCommon/cxxreact/NativeToJsBridge.h index 8a7dfb02b9e35a..e02cf192889d4d 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.h +++ b/ReactCommon/cxxreact/NativeToJsBridge.h @@ -44,7 +44,8 @@ class NativeToJsBridge { std::shared_ptr delegate, // TODO(OSS Candidate ISS#2710739) std::shared_ptr registry, std::shared_ptr jsQueue, - std::shared_ptr callback); + std::shared_ptr callback, + std::shared_ptr jseConfigParams); virtual ~NativeToJsBridge(); /**