forked from alibaba/GCanvas
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
69 lines (61 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {Platform} from 'react-native';
import GCanvasView from './components/GCanvasComponent';
import GImage from './packages/gcanvas/src/env/image';
import {ReactNativeBridge} from './packages/gcanvas';
const createCanvas = (width, height) => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
};
const getFontNames = () => {
return ReactNativeBridge.GCanvasModule ? ReactNativeBridge.GCanvasModule.getFontNames() : [];
}
let extraFontLocation = null;
const setExtraFontLocation = (location) => {
if (Platform.OS === 'android') {
const fontLocation = location.endsWith('/') ? location : `${location}/`;
if ('/system/fonts/' === fontLocation) {
// SYSTEM_FONT_LOCATION = "/system/fonts/" in
// android/gcanvas_library/src/main/java/com/taobao/gcanvas/GFontConfigParser.java
// so just return here if e.g. registerFont('/system/fonts/NotoSansThai-Regular.ttf')
return;
}
if (extraFontLocation !== fontLocation) {
ReactNativeBridge.GCanvasModule.setExtraFontLocation(fontLocation);
extraFontLocation = fontLocation;
}
}
};
/**
* Register a custom font on Android, so that can `ctx.font = `, and can be included in the return of getFontNames().
*
* Ref to <https://mehrankhandev.medium.com/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4>
* and <https://github.com/callstack/react-native-paper/blob/291d9a90ea2bf2d9a3416170a9a3a1791cf051b0/docs/docs/guides/04-fonts.md?plain=1#L32>
* to also reuse custom fonts in React `<Text/>`, need rename font file name, maybe
* <https://gist.github.com/keighl/5434540> can do the rename work.
*
* @param src {string} Path to font file, e.g. `${RNFS.ExternalDirectoryPath}/fonts/KaiTi.ttf`
* @param fontFace {{
* family?: string, // fontName that getFontNames() will return, if undefined, fontName will be font file name e.g. `KaiTi`
* weight?: string, // useless
* style?: string, // useless
* }} Object, can be undefined
*/
const registerFont = (src, fontFace) => {
if (Platform.OS === 'android') {
const fontFile = src.substring(src.lastIndexOf('/') + 1);
const fontLocation = src.substring(0, src.lastIndexOf('/') + 1);
setExtraFontLocation(fontLocation);
const fontFileName = fontFile.includes('.') ? fontFile.substring(0, fontFile.lastIndexOf('.')) : fontFile;
const fontName = fontFace && fontFace.family ? fontFace.family : fontFileName;
ReactNativeBridge.GCanvasModule.addFontFamily([fontName], [fontFile]);
}
}
export {
GCanvasView,
GImage,
createCanvas,
getFontNames,
registerFont,
}