Skip to content

Commit

Permalink
fix(ios, android): make the code more standardized for font loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyunong committed Nov 12, 2024
1 parent ca9e7c4 commit 3569002
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/api/style/appearance.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

# fontUrl

动态加载字体的url,会在渲染时异步动态加载,可以是远程字体地址或者是使用“hpfile://./”为前缀的JSBundle包中字体路径
动态加载字体的url,会在渲染时异步动态加载,可以是远程字体地址或者是使用JSBundle包中字体路径的相对地址。

| 类型 | 必需 | 支持平台 |
| ------------------------ | ---- | ------------ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export default class LoadFontExample extends React.Component {
}

fillExample() {
this.setState({ inputFontFamily: 'HYHuaXianZi J' });
this.setState({ fontUrl: 'https://zf.sc.chinaz.com/Files/DownLoad/upload/2024/1009/hanyihuaxianzijianti.ttf' });
this.setState({ inputFontFamily: 'TencentSans W7' });
this.setState({ fontUrl: 'https://infra-packages.openhippy.com/test/resources/TencentSans_W7.ttf' });
// this.setState({ fontUrl: 'hpfile://./assets/hanyihuaxianzijianti.ttf' }
}

Expand All @@ -93,13 +93,13 @@ export default class LoadFontExample extends React.Component {
<Text>通过组件fontUrl属性动态下载并使用字体</Text>
</View>
<Text style={styles.text_style}
fontFamily='HYHuaXianZi F'
fontUrl='https://zf.sc.chinaz.com/Files/DownLoad/upload/2024/1009/hanyihuaxianzifanti.ttf'>
This sentence will use font 'HYHuaXianZi F' downloaded dynamically according to 'fontUrl' property.
fontFamily='TencentSans W3'
fontUrl='https://infra-packages.openhippy.com/test/resources/TencentSans_W3.otf'>
This sentence will use font 'TencentSans W3' downloaded dynamically according to 'fontUrl' property.
</Text>
<Text style={styles.text_style}
fontFamily='HYHuaXianZi F'>
这句话将使用通过fontUrl属性下载的汉仪花仙子繁体字体.
fontFamily='TencentSans W3'>
这句话将使用通过fontUrl属性下载的'TencentSans W3'字体.
</Text>
<View style={styles.itemTitle}>
<Text>下载并使用字体</Text>
Expand Down
11 changes: 10 additions & 1 deletion framework/ios/module/fontLoader/HippyFontLoaderModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ NS_ASSUME_NONNULL_BEGIN
HIPPY_EXTERN NSString *const HippyLoadFontNotification;

typedef NS_ENUM(NSInteger, HippyFontUrlState) {
HippyFontUrlPending = 0,
HippyFontUrlLoading = 1,
HippyFontUrlLoaded = 2,
HippyFontUrlFailed = 3,
};

/**
* @class HippyFontLoaderModule
* @brief This class is responsible for loading and registering fonts.
*
* This class is a hippy module, providing a load method for the front end to download and register fonts.
* It also provides method for native side to register font.
*/
@interface HippyFontLoaderModule : NSObject<HippyBridgeModule>

/**
Expand Down Expand Up @@ -72,7 +80,8 @@ typedef NS_ENUM(NSInteger, HippyFontUrlState) {
* @param resolve - The callback block for downloading successful.
* @param reject - The callback block for downloading failed.
*/
- (void)load:(NSString *)fontFamily from:(NSString *)urlString resolver:(nullable HippyPromiseResolveBlock)resolve rejecter:(nullable HippyPromiseRejectBlock)reject;
- (void)load:(NSString *)fontFamily from:(NSString *)urlString resolver:(nullable HippyPromiseResolveBlock)resolve
rejecter:(nullable HippyPromiseRejectBlock)reject;

@end

Expand Down
13 changes: 4 additions & 9 deletions framework/ios/module/fontLoader/HippyFontLoaderModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ + (BOOL)registerFontIfNeeded:(NSString *)fontFamily {
[fontRegistered addObject:fontFile];
isFontRegistered = YES;
HippyLogInfo(@"register font \"%@\" success!", fontFile);
}
else {
} else {
if (error.domain == kFontLoaderModuleErrorDomain && error.code == FontLoaderErrorRegisterError) {
[fileNotExist addObject:fontFile];
}
Expand Down Expand Up @@ -180,8 +179,7 @@ - (void)saveFontfamily:(NSString *)fontFamily url:(NSString *)url filePath:(NSSt
if (!fontFiles) {
fontFiles = [NSMutableArray arrayWithObject:filePath];
[fontFamilyToFiles setObject:fontFiles forKey:fontFamily];
}
else {
} else {
[fontFiles addObject:filePath];
}
[urlToFilePath writeToFile:fontUrlSavePath atomically:YES];
Expand Down Expand Up @@ -236,9 +234,7 @@ - (void)saveFontfamily:(NSString *)fontFamily url:(NSString *)url filePath:(NSSt
resolve([NSString stringWithFormat:@"load local font file \"%@\" success!", fontFilePath]);
}
[HippyFontLoaderModule setUrl:urlString state:HippyFontUrlLoaded];
}
// is http url
else {
} else { // is http url
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fontDirPath]) {
NSError *error;
Expand All @@ -263,8 +259,7 @@ - (void)saveFontfamily:(NSString *)fontFamily url:(NSString *)url filePath:(NSSt
resolve([NSString stringWithFormat:@"download font file \"%@\" success!", fileName]);
}
[HippyFontLoaderModule setUrl:urlString state:HippyFontUrlLoaded];
}
else {
} else {
if (reject) {
NSString *errorKey = [NSString stringWithFormat:@"%lu", FontLoaderErrorWriteFileError];
reject(errorKey, @"font request error", error);
Expand Down
3 changes: 2 additions & 1 deletion modules/vfs/ios/HippyVFSDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ enum HippyVFSRscType {
};

enum HippyVFSURLType {
HippyVFSURLTypeHTTP = 1,
HippyVFSURLTypeUnknown = 0,
HippyVFSURLTypeHTTP,
HippyVFSURLTypeFile,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,18 @@ public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
String fileName = fontFamily + getFileExtension(fontUrl);
if (convertFontUrl.equals(mConcurrentLocalFontPathMap.get(fileName))) {
needRefresh = false;
}
else {
} else {
mConcurrentLocalFontPathMap.put(fileName, convertFontUrl);
}
if (promise != null) {
promise.resolve(String.format("Load local font %s success", convertFontUrl));
}
}
else {
} else {
String fileName = fontFamily + getFileExtension(fontUrl);
if (!saveFontFile(bytes, fileName, promise)) {
mConcurrentFontLoadStateMap.remove(fontFamily);
return;
}
else {
} else {
File fontFile = new File(mFontDir, fileName);
mConcurrentUrlFontMap.put(fontUrl, fontFile.getAbsolutePath());
if (promise != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ private static Typeface createExactTypeFace(String fileName) {
} catch (Exception e) {
LogUtils.w(TAG, e.getMessage());
}
}
else {
} else {
if (bundleFontPath.startsWith(UrlUtils.PREFIX_FILE)) {
bundleFontPath = bundleFontPath.substring(UrlUtils.PREFIX_FILE.length());
}
Expand Down
3 changes: 1 addition & 2 deletions renderer/native/ios/renderer/HippyFont.mm
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ + (UIFont *)updateFont:(UIFont *)font
font = [UIFont fontWithName:familyName size:fontSize];
if (font) {
didFindFont = YES;
}
else {
} else {
fontWeight = weight ? fontWeight : weightOfFont(font);
isItalic = style ? isItalic : isItalicFont(font);
isCondensed = isCondensedFont(font);
Expand Down
40 changes: 36 additions & 4 deletions renderer/native/ios/renderer/component/view/HippyViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ - (void)measureInAppWindow:(NSNumber *)componentTag
}];
}

HIPPY_EXPORT_METHOD(getViewTagByLocation:(nonnull NSNumber *)componentTag
params:(NSDictionary *__nonnull)params
callback:(HippyPromiseResolveBlock)callback) {
[self.bridge.uiManager addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[componentTag];
if (view == nil) {
callback(@[]);
return;
}
UIView *rootView = viewRegistry[view.rootTag];
if (rootView == nil) {
callback(@[]);
return;
}
double locationX = [params[@"xOnScreen"] doubleValue];
double locationY = [params[@"yOnScreen"] doubleValue];
UIView* hitView = [rootView hitTest:{locationX, locationY} withEvent:nil];

NSDictionary *locationDict = @{
@"hippyTag": hitView.hippyTag,
};
callback(@[locationDict]);
}];
}

HIPPY_EXPORT_METHOD(getLocationOnScreen:(nonnull NSNumber *)componentTag
params:(NSDictionary *__nonnull)params
callback:(HippyPromiseResolveBlock)callback) {
Expand All @@ -187,12 +212,19 @@ - (void)measureInAppWindow:(NSNumber *)componentTag
callback(@[]);
return;
}
UIView *rootView = viewRegistry[view.rootTag];
CGFloat rootX = 0, rootY = 0;
if (rootView) {
CGRect windowFrame = [rootView.window convertRect:rootView.frame fromView:rootView.superview];
rootX = windowFrame.origin.x;
rootY = windowFrame.origin.y;
}
CGRect windowFrame = [view.window convertRect:view.frame fromView:view.superview];
NSDictionary *locationDict = @{
@"xOnScreen": @(static_cast<int>(windowFrame.origin.x)),
@"yOnScreen": @(static_cast<int>(windowFrame.origin.y)),
@"viewWidth": @(static_cast<int>(CGRectGetHeight(windowFrame))),
@"viewHeight": @(static_cast<int>(CGRectGetWidth(windowFrame)))
@"xOnScreen": @(static_cast<int>(windowFrame.origin.x - rootX)),
@"yOnScreen": @(static_cast<int>(windowFrame.origin.y - rootY)),
@"viewWidth": @(static_cast<int>(CGRectGetWidth(windowFrame))),
@"viewHeight": @(static_cast<int>(CGRectGetHeight(windowFrame)))
};
callback(@[locationDict]);
}];
Expand Down

0 comments on commit 3569002

Please sign in to comment.