Skip to content

Commit

Permalink
fix(android): fix the use of weak reference in font loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyunong committed Oct 31, 2024
1 parent a83e7ce commit 479af1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public static String getFontPath(String fontFileName) {
// Convert "hpfile://" to "file://" or "assets://"
private String convertToLocalPathIfNeeded(String fontUrl) {
if (fontUrl != null && fontUrl.startsWith("hpfile://")) {
String bundlePath = mNativeRenderRef.get().getBundlePath();
final NativeRender nativeRender = mNativeRenderRef.get();
String bundlePath = null;
if (nativeRender != null) {
bundlePath = mNativeRenderRef.get().getBundlePath();
}
String relativePath = fontUrl.replace("hpfile://./", "");
fontUrl = bundlePath == null ? null
: bundlePath.subSequence(0, bundlePath.lastIndexOf(File.separator) + 1)
Expand Down Expand Up @@ -230,13 +234,14 @@ public void loadAndRefresh(final String fontFamily, final String fontUrl, int ro
}
mConcurrentFontLoadStateMap.put(fontFamily, FontLoadState.FONT_LOADING);
String convertFontUrl = convertToLocalPathIfNeeded(fontUrl);
if (mVfsManager.get() == null) {
final VfsManager vfsManager = mVfsManager.get();
if (vfsManager == null) {
if (promise != null) {
promise.reject("Get vfsManager failed!");
}
return;
}
mVfsManager.get().fetchResourceAsync(convertFontUrl, null, null,
vfsManager.fetchResourceAsync(convertFontUrl, null, null,
new FetchResourceCallback() {
@Override
public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
Expand Down Expand Up @@ -282,7 +287,7 @@ public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
saveMapFile(mUrlFontMapFile, mConcurrentUrlFontMap);
saveMapFile(mLocalFontPathMapFile, mConcurrentLocalFontPathMap);
TypeFaceUtil.clearFontCache(fontFamily);
NativeRender nativeRender = mNativeRenderRef.get();
final NativeRender nativeRender = mNativeRenderRef.get();
if (nativeRender != null && needRefresh) {
nativeRender.onFontLoaded(rootId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public void setFontFamily(String family) {
if (!Objects.equals(mFontFamily, family)) {
mFontFamily = family;
markDirty();
if (mFontLoaderRef.get() != null && !mFontLoaderRef.get().isFontLoaded(mFontFamily)) {
final FontLoader fontLoader = mFontLoaderRef.get();
if (fontLoader != null && !fontLoader.isFontLoaded(mFontFamily)) {
mFontLoadState = FontLoader.FontLoadState.FONT_UNLOAD;
}
}
Expand Down Expand Up @@ -496,12 +497,14 @@ protected void createSpanOperationImpl(@NonNull List<SpanOperation> ops,
if (mFontAdapter != null && mEnableScale) {
size = (int) (size * mFontAdapter.getFontScale());
}
if (!TextUtils.isEmpty(mFontUrl) && mFontLoaderRef.get() != null) {
if (mNativeRenderRef.get() != null) {
Executor executor = mNativeRenderRef.get().getBackgroundExecutor();
final FontLoader fontLoader = mFontLoaderRef.get();
if (!TextUtils.isEmpty(mFontUrl) && fontLoader != null) {
final NativeRender nativeRender = mNativeRenderRef.get();
if (nativeRender != null) {
Executor executor = nativeRender.getBackgroundExecutor();
if (executor != null) {
executor.execute(() -> {
mFontLoaderRef.get().loadIfNeeded(mFontFamily, mFontUrl, getRootId());
fontLoader.loadIfNeeded(mFontFamily, mFontUrl, getRootId());
});
}
}
Expand Down Expand Up @@ -572,7 +575,7 @@ protected Layout createLayout() {

@NonNull
protected Layout createLayout(final float width, final FlexMeasureMode widthMode) {
FontLoader fontLoader = mFontLoaderRef.get();
final FontLoader fontLoader = mFontLoaderRef.get();
if (mFontLoadState == FontLoader.FontLoadState.FONT_UNLOAD && fontLoader != null &&
fontLoader.isFontLoaded(mFontFamily)) {
mDirty = true;
Expand Down

0 comments on commit 479af1f

Please sign in to comment.