Skip to content

Commit

Permalink
feat(ios, android): support loading font dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyunong committed Oct 15, 2024
1 parent 53c8a09 commit 6a19551
Show file tree
Hide file tree
Showing 32 changed files with 793 additions and 22 deletions.
2 changes: 2 additions & 0 deletions driver/js/packages/hippy-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const {
Device,
HippyRegister,
ImageLoader: ImageLoaderModule,
FontLoader: FontLoaderModule,
NetworkInfo: NetInfo,
UIManager: UIManagerModule,
flushSync,
Expand Down Expand Up @@ -118,6 +119,7 @@ export {
Clipboard,
ConsoleModule,
ImageLoaderModule,
FontLoaderModule,
Platform,
BackAndroid,
Animation,
Expand Down
35 changes: 35 additions & 0 deletions driver/js/packages/hippy-react/src/modules/font-loader-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Bridge } from '../global';

/**
* Load font from remote url.
*
* @param {string} url - Remote font url.
* @param {string} fontFamily - Remote fontFamily name.
*/
function load(fontFamily: string, url: string) {
return Bridge.callNativeWithPromise('FontLoaderModule', 'load', fontFamily, url);
}

export {
load,
};
2 changes: 2 additions & 0 deletions driver/js/packages/hippy-react/src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as HippyGlobal from './global';
import * as Clipboard from './modules/clipboard';
import * as Cookie from './modules/cookie-module';
import * as ImageLoader from './modules/image-loader-module';
import * as FontLoader from './modules/font-loader-module';
import * as NetworkInfo from './modules/network-info';
import * as UIManager from './modules/ui-manager-module';
import BackAndroid from './modules/back-android';
Expand Down Expand Up @@ -52,6 +53,7 @@ export {
Device,
HippyRegister,
ImageLoader,
FontLoader,
NetworkInfo,
UIManager,
flushSync,
Expand Down
22 changes: 22 additions & 0 deletions driver/js/packages/hippy-vue-next/src/runtime/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export interface NativeApiType {
prefetch: (url: string) => void;
};

FontLoader: {
load: (fontFamily: string, url: string) => Promise<undefined>;
};

// include window and screen info
Dimensions: Dimensions;

Expand Down Expand Up @@ -517,6 +521,24 @@ export const Native: NativeApiType = {
},
},

FontLoader: {
/**
* get image size before image rendering
*
* @param fontFamily
* @param url - image url
*/
load(fontFamily, url): Promise<undefined> {
return Native.callNativeWithPromise.call(
this,
'FontLoaderModule',
'load',
fontFamily,
url,
);
},
},

/**
* Get the screen or view size.
*/
Expand Down
2 changes: 2 additions & 0 deletions driver/js/packages/hippy-vue-next/src/types/native-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { ClipboardModule } from './native-modules/clip-board-module';
import type { DeviceEventModule } from './native-modules/device-event-module';
import type { Http } from './native-modules/http';
import type { ImageLoaderModule } from './native-modules/image-loader-module';
import type { FontLoaderModule } from './native-modules/font-loader-module';
import type { NetInfo } from './native-modules/net-info';
import type { Network } from './native-modules/network';
import type { TestModule } from './native-modules/test-module';
Expand All @@ -32,6 +33,7 @@ export interface NativeInterfaceMap {
// The key here is the module name set by the native and cannot be changed at will.
UIManagerModule: UiManagerModule;
ImageLoaderModule: ImageLoaderModule;
FontLoaderModule: FontLoaderModule;
websocket: Websocket;
NetInfo: NetInfo;
ClipboardModule: ClipboardModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2022 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface FontLoaderModule {
load: (fontFamily: string, url: string) => undefined;
}
14 changes: 14 additions & 0 deletions driver/js/packages/hippy-vue/src/runtime/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ const Native: NeedToTyped = {
callNative.call(this, 'ImageLoaderModule', 'prefetch', url);
},
},
/**
* operations for font
*/
FontLoader: {
/**
* Download the font from the url.
*
* @param {string} fontFamily - The font family to download,
* @param {string} url - The url where to download the font.
*/
load(fontFamily: NeedToTyped, url: NeedToTyped) {
return callNativeWithPromise.call(this, 'FontLoaderModule', 'load', fontFamily, url);
},
},
/**
* Network operations
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.tencent.mtt.hippy.modules.nativemodules.console.ConsoleModule;
import com.tencent.mtt.hippy.modules.nativemodules.deviceevent.DeviceEventModule;
import com.tencent.mtt.hippy.modules.nativemodules.exception.ExceptionModule;
import com.tencent.mtt.hippy.modules.nativemodules.font.FontLoaderModule;
import com.tencent.mtt.hippy.modules.nativemodules.image.ImageLoaderModule;
import com.tencent.mtt.hippy.modules.nativemodules.netinfo.NetInfoModule;
import com.tencent.mtt.hippy.modules.nativemodules.network.NetworkModule;
Expand Down Expand Up @@ -84,6 +85,12 @@ public HippyNativeModuleBase get() {
return new ImageLoaderModule(context);
}
});
modules.put(FontLoaderModule.class, new Provider<HippyNativeModuleBase>() {
@Override
public HippyNativeModuleBase get() {
return new FontLoaderModule(context);
}
});
modules.put(NetworkModule.class, new Provider<HippyNativeModuleBase>() {
@Override
public HippyNativeModuleBase get() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Tencent is pleased to support the open source community by making Hippy available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

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);
mFontLoader = new FontLoader(context.getVfsManager());
mNativeRender = NativeRendererManager.getNativeRenderer(context.getRootView().getContext());
rootId = context.getRootView().getId();
}

@HippyMethod(name = "load")
public void load(final String fontFamily, final String fontUrl, final Promise promise) {
mFontLoader.loadAndFresh(fontFamily, fontUrl, mNativeRender, rootId, promise);
}
}
36 changes: 36 additions & 0 deletions framework/ios/module/fontLoader/HippyFontLoaderModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import "HippyBridgeModule.h"

static NSString *const HippyLoadFontNotification = @"HippyLoadFontNotification";

@interface HippyFontLoaderModule : NSObject<HippyBridgeModule>

@property (nonatomic, readonly) NSString *fontDir;
@property (nonatomic, readonly) NSString *fontUrlCachePath;

- (NSString *)getFontPath:(NSString *)url;
- (BOOL)registerFontFromURL:(NSString *)urlString error:(NSError *)error;

@end
Loading

0 comments on commit 6a19551

Please sign in to comment.