Skip to content

Commit

Permalink
fix(android): improve the implemention of loading font dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyunong committed Oct 21, 2024
1 parent 9b9c83d commit ce1f0d6
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.tencent.mtt.hippy.common.Callback;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.renderer.FrameworkProxy;
import com.tencent.renderer.RenderProxy;
import java.util.List;
Expand Down Expand Up @@ -143,6 +144,17 @@ public void destroy() {
mRenderer = null;
}

@Override
public void loadFontAndRefreshWindow(@NonNull String fontFamily, @NonNull String fontUrl,
int rootId, Object promise) {
if (!(promise instanceof Promise)) {
promise = null;
}
if (mRenderer != null) {
mRenderer.loadFontAndRefreshWindow(fontFamily, fontUrl, rootId, (Promise) promise);
}
}

@Override
public int getInstanceId() {
return mInstanceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.util.Map;

Expand All @@ -33,6 +35,8 @@ public interface RenderConnector extends Connector {

void onRuntimeInitialized(int rootId);

void loadFontAndRefreshWindow(@NonNull String fontFamily, @NonNull String fontUrl, int rootId, Object promise);

void recordSnapshot(int rootId, @NonNull Object callback);

View replaySnapshot(@NonNull Context context, @NonNull byte[] buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.openhippy.connector.JsDriver;
import com.openhippy.connector.RenderConnector;
import com.tencent.devtools.DevtoolsManager;
import com.tencent.mtt.hippy.HippyEngine.ModuleLoadStatus;
import com.tencent.mtt.hippy.bridge.HippyBridgeManager;
Expand Down Expand Up @@ -65,6 +66,8 @@ public interface HippyEngineContext extends BaseEngineContext {

ThreadExecutor getThreadExecutor();

RenderConnector getRenderer();

ViewGroup getRootView();

View getRootView(int rootId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ DomManager getDomManager() {
}

@NonNull
RenderConnector getRenderer() {
public RenderConnector getRenderer() {
return mRenderer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,26 @@

package com.tencent.mtt.hippy.modules.nativemodules.font;

import com.openhippy.connector.RenderConnector;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.annotation.HippyMethod;
import com.tencent.mtt.hippy.annotation.HippyNativeModule;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.renderer.NativeRender;
import com.tencent.renderer.NativeRendererManager;
import com.tencent.renderer.component.text.FontLoader;

@HippyNativeModule(name = "FontLoaderModule")
public class FontLoaderModule extends HippyNativeModuleBase {

private final FontLoader mFontLoader;
private final NativeRender mNativeRender;
private final int rootId;

public FontLoaderModule(HippyEngineContext context) {
super(context);
mNativeRender = NativeRendererManager.getNativeRenderer(context.getRootView().getContext());
mFontLoader = mNativeRender.getFontLoader();
rootId = context.getRootView().getId();
}

@HippyMethod(name = "load")
public void load(final String fontFamily, final String fontUrl, final Promise promise) {
mFontLoader.loadAndRefresh(fontFamily, fontUrl, mNativeRender, rootId, promise);
RenderConnector renderer = mContext.getRenderer();
renderer.loadFontAndRefreshWindow(fontFamily, fontUrl, rootId, promise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ jobject GetNativeRendererInstance(JNIEnv* j_env,
jobject j_object,
jint j_render_manager_id);

void MarkTextNodeDirty(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id);

void RefreshWindow(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id);
void RefreshTextWindow(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id);

void UpdateRootSize(JNIEnv* j_env, jobject j_obj, jint j_render_manager_id, jint j_root_id,
jfloat width, jfloat height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,9 @@ REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
UpdateRootSize)

REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
"refreshWindow",
"refreshTextWindow",
"(II)V",
RefreshWindow)

REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
"markTextNodeDirty",
"(II)V",
MarkTextNodeDirty)
RefreshTextWindow)

static jint JNI_OnLoad(__unused JavaVM* j_vm, __unused void* reserved) {
auto j_env = JNIEnvironment::GetInstance()->AttachCurrentThread();
Expand Down Expand Up @@ -173,58 +168,31 @@ void MarkTextNodeDirtyRecursive(const std::shared_ptr<DomNode>& node) {
}
}

void MarkTextNodeDirty(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id) {
auto& map = NativeRenderManager::PersistentMap();
std::shared_ptr<NativeRenderManager> render_manager;
bool ret = map.Find(static_cast<uint32_t>(j_render_manager_id), render_manager);
if (!ret) {
FOOTSTONE_DLOG(WARNING) << "MarkTextNodeDirty j_render_manager_id invalid";
return;
}
std::shared_ptr<DomManager> dom_manager = render_manager->GetDomManager();
if (dom_manager == nullptr) {
FOOTSTONE_DLOG(WARNING) << "MarkTextNodeDirty dom_manager is nullptr";
return;
}
auto& root_map = RootNode::PersistentMap();
std::shared_ptr<RootNode> root_node;
uint32_t root_id = footstone::check::checked_numeric_cast<jint, uint32_t>(j_root_id);
ret = root_map.Find(root_id, root_node);
if (!ret) {
FOOTSTONE_DLOG(WARNING) << "MarkTextNodeDirty root_node is nullptr";
return;
}

std::vector<std::function<void()>> ops = {[root_node] {
MarkTextNodeDirtyRecursive(root_node);
}};
dom_manager->PostTask(Scene(std::move(ops)));
}

void RefreshWindow(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id) {
void RefreshTextWindow(JNIEnv *j_env, jobject j_object, jint j_render_manager_id, jint j_root_id) {
auto& map = NativeRenderManager::PersistentMap();
std::shared_ptr<NativeRenderManager> render_manager;
bool ret = map.Find(static_cast<uint32_t>(j_render_manager_id), render_manager);
if (!ret) {
FOOTSTONE_DLOG(WARNING) << "RefreshWindow j_render_manager_id invalid";
FOOTSTONE_DLOG(WARNING) << "RefreshTextWindow j_render_manager_id invalid";
return;
}
std::shared_ptr<DomManager> dom_manager = render_manager->GetDomManager();
if (dom_manager == nullptr) {
FOOTSTONE_DLOG(WARNING) << "RefreshWindow dom_manager is nullptr";
FOOTSTONE_DLOG(WARNING) << "RefreshTextWindow dom_manager is nullptr";
return;
}
auto& root_map = RootNode::PersistentMap();
std::shared_ptr<RootNode> root_node;
uint32_t root_id = footstone::check::checked_numeric_cast<jint, uint32_t>(j_root_id);
ret = root_map.Find(root_id, root_node);
if (!ret) {
FOOTSTONE_DLOG(WARNING) << "RefreshWindow root_node is nullptr";
FOOTSTONE_DLOG(WARNING) << "RefreshTextWindow root_node is nullptr";
return;
}

std::vector<std::function<void()>> ops;
ops.emplace_back([dom_manager, root_node]{
MarkTextNodeDirtyRecursive(root_node);
dom_manager->DoLayout(root_node);
dom_manager->EndBatch(root_node);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,13 @@ public class HippyTextInput extends AppCompatEditText implements HippyViewBase,
private String mFontFamily;
private String mFontUrl;
private Paint mTextPaint;
protected FontLoader mFontLoader;
protected boolean mFromFontLoader = false;

public HippyTextInput(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);

NativeRender nativeRenderer = NativeRendererManager.getNativeRenderer(context);
if (nativeRenderer != null) {
mFontLoader = nativeRenderer.getFontLoader();
}
mDefaultGravityHorizontal =
getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK
| Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
Expand Down Expand Up @@ -215,7 +209,12 @@ public void setTextLineHeight(int lineHeight) {
}

public void onBatchComplete() {
if (!mFromFontLoader && mFontLoader != null && mFontLoader.isFontLoaded(mFontFamily)) {
NativeRender nativeRenderer = NativeRendererManager.getNativeRenderer(this.getContext());
FontLoader fontLoader = null;
if (nativeRenderer != null) {
fontLoader = nativeRenderer.getFontLoader();
}
if (!mFromFontLoader && fontLoader != null && fontLoader.isFontLoaded(mFontFamily)) {
mShouldUpdateTypeface = true;
mFromFontLoader = true;
}
Expand Down Expand Up @@ -772,7 +771,12 @@ public void setFontFamily(String family) {
if (!Objects.equals(mFontFamily, family)) {
mFontFamily = family;
mShouldUpdateTypeface = true;
if (mFromFontLoader && mFontLoader != null && !mFontLoader.isFontLoaded(mFontFamily)) {
NativeRender nativeRenderer = NativeRendererManager.getNativeRenderer(this.getContext());
FontLoader fontLoader = null;
if (nativeRenderer != null) {
fontLoader = nativeRenderer.getFontLoader();
}
if (mFromFontLoader && fontLoader != null && !fontLoader.isFontLoaded(mFontFamily)) {
mFromFontLoader = false;
}
}
Expand Down Expand Up @@ -803,7 +807,7 @@ private void updateTypeface() {
FontLoader loader = nativeRenderer == null ? null : nativeRenderer.getFontLoader();
if (loader != null) {
int rootId = nativeRenderer.getRootView(this).getId();
loader.loadIfNeeded(mFontFamily, mFontUrl, nativeRenderer, rootId);
loader.loadIfNeeded(mFontFamily, mFontUrl, rootId);
}
}
FontAdapter fontAdapter = nativeRenderer == null ? null : nativeRenderer.getFontAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ VirtualNode createVirtualNode(int rootId, int id, int pid, int index, @NonNull S

void onSizeChanged(int rootId, int nodeId, int width, int height, boolean isSync);

void markTextNodeDirty(int rootId);

void refreshWindow(int rootId);
void refreshTextWindow(int rootId);

void updateDimension(int width, int height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,8 @@ public void onSizeChanged(int rootId, int width, int height) {
updateRootSize(mInstanceId, rootId, PixelUtil.px2dp(width), PixelUtil.px2dp(height));
}

public void refreshWindow(int rootId) {
refreshWindow(mInstanceId, rootId);
}

public void markTextNodeDirty(int rootId) {
markTextNodeDirty(mInstanceId, rootId);
public void refreshTextWindow(int rootId) {
refreshTextWindow(mInstanceId, rootId);
}

public void onSizeChanged(int rootId, int nodeId, int width, int height, boolean isSync) {
Expand Down Expand Up @@ -449,16 +445,7 @@ private void dispatchEventImpl(int rootId, int nodeId, @NonNull String eventName
* @param instanceId the unique id of native (C++) render manager
*/
@SuppressWarnings("JavaJniMissingFunction")
private native void refreshWindow(int instanceId, int rootId);

/**
* Call back from Android system when size changed, just like horizontal and vertical screen
* switching, call this jni interface to invoke dom tree relayout.
*
* @param rootId the root node id
*/
@SuppressWarnings("JavaJniMissingFunction")
public native void markTextNodeDirty(int instanceId, int rootId);
private native void refreshTextWindow(int instanceId, int rootId);

/**
* Updates the size to the specified node, such as modal node, should set new window size before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.tencent.mtt.hippy.common.BaseEngineContext;
import com.tencent.mtt.hippy.common.Callback;
import com.tencent.mtt.hippy.common.LogAdapter;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.serialization.nio.reader.BinaryReader;
import com.tencent.mtt.hippy.serialization.nio.reader.SafeHeapReader;
import com.tencent.mtt.hippy.serialization.nio.writer.SafeHeapWriter;
Expand Down Expand Up @@ -227,7 +228,7 @@ public ImageLoaderAdapter getImageLoader() {
@Nullable
public FontLoader getFontLoader() {
if (mFontLoader == null && getVfsManager() != null) {
mFontLoader = new FontLoader(getVfsManager());
mFontLoader = new FontLoader(getVfsManager(), this);
}
return mFontLoader;
}
Expand Down Expand Up @@ -415,12 +416,8 @@ private void onSizeChanged(int rootId, int w, int h) {
mRenderProvider.onSizeChanged(rootId, w, h);
}

public void markTextNodeDirty(int rootId) {
mRenderProvider.markTextNodeDirty(rootId);
}

public void refreshWindow(int rootId) {
mRenderProvider.refreshWindow(rootId);
public void refreshTextWindow(int rootId) {
mRenderProvider.refreshTextWindow(rootId);
}

@Override
Expand Down Expand Up @@ -1144,6 +1141,15 @@ private boolean collectNodeInfo(@NonNull RenderNode child, int pid, int outerLef
return true;
}

@Override
public void loadFontAndRefreshWindow(@NonNull String fontFamily, @NonNull String fontUrl,
int rootId, final Promise promise) {
if (mFontLoader == null && getVfsManager() != null) {
mFontLoader = new FontLoader(getVfsManager(), this);
}
mFontLoader.loadAndRefresh(fontFamily, fontUrl, rootId, promise);
}

private interface UITaskExecutor {

void exec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.tencent.mtt.hippy.common.Callback;
import com.tencent.mtt.hippy.modules.Promise;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -131,4 +133,9 @@ public interface RenderProxy {
*/
void removeSnapshotView();

/**
* Notify renderer to load font from url and refresh text window.
*/
void loadFontAndRefreshWindow(@NonNull String fontFamily, @NonNull String fontUrl, int rootId, final Promise promise);

}
Loading

0 comments on commit ce1f0d6

Please sign in to comment.