-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
164 lines (123 loc) · 4.9 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { NativeModules } from 'react-native';
import RNFS from 'react-native-fs';
import SvgEditor from './components';
// native module import
const SvgEditorNativeModule = NativeModules.SvgEditor;
export default SvgEditor;
export * from './components/SvgItem';
export {default as MemorisedPan} from './components/SvgItemControlLayer/MemorisedPan';
const folderSafeCheck = async path => {
let folderExists = await RNFS.exists(path);
if (!folderExists) {
console.log('Folder doesn\'t exist. Creating now...')
await RNFS.mkdir(path);
}
}
const documentDirectoryPath = `file://${RNFS.DocumentDirectoryPath}`;
export const fontsFolderRelativePath = `fonts`;
export const fontsFolderAbsolutePath = `${documentDirectoryPath}/${fontsFolderRelativePath}`;
const remoteFontFetchOrigin = "https://gwfh.mranftl.com";
class SvgEditorManagerObject {
remoteFonts = null;
/**
* @typedef LoadedFontFamily
* @type {Object.<string, string>} An object with the font weight as key and path to local font file as value
*/
/**
* @type {Object.<string, LoadedFontFamily>} An object with the font id as key and LoadedFontFamily as value
*/
loadedFonts = {};
constructor() {
console.log("SvgEditorNativeModule: ", SvgEditorNativeModule.createFontWithUrl);
if (!SvgEditorNativeModule) {
this._nativeModuleNotAvailable = true;
} else {
this.textToPath = SvgEditorNativeModule.textToPath;
}
}
async listRemoteFontsFromGoogle() {
const url = `${remoteFontFetchOrigin}/api/fonts`;
if (!this.remoteFonts) {
const fonts = await fetch(url)
.then((response) => response.json());
this.remoteFonts = fonts;
}
return this.remoteFonts;
}
getFilePath(filePath) {
return SvgEditorNativeModule.getFilePath(filePath);
}
/**
* @typedef DownloadGoogleRemoteFontResult
* @property {string} fontFamily The Registered Font Family Name
* @property {Array} fontFile Array of local font files of different weights
*/
/**
* Download Font from google fonts. Throws Error if failed
* @param {string} fontId Font Id of google-webfonts-helper
* @returns {Promise<Object.<string, DownloadGoogleRemoteFontResult>} Promise that resolves to
* an object with the font weight as key and DownloadGoogleRemoteFontResult as value
*/
async downloadRemoteFontFromGoogle(fontId) {
if (this.loadedFonts[fontId]) {
return this.loadedFonts[fontId];
}
const url = `${remoteFontFetchOrigin}/api/fonts/${fontId}?subsets=latin,latin-ext`;
const { variants=[] } = await fetch(url).then((response) => response.json());
const font = {};
const fonts = variants.filter(variant =>
variant.fontStyle == 'normal' && (variant.fontWeight == 400 || variant.fontWeight == 100 || variant.fontWeight == 700));
for (let fontVariant of fonts) {
const { ttf, woff, fontWeight } = fontVariant, file = ttf || woff;
if (!file) continue;
const fileExtension = file.split('.').pop();
const fontFamily = await this.loadFont(file, fontId, fontWeight);
// save font file locally
const fontFileRelativePath = `${fontsFolderRelativePath}/${fontFamily}.${fileExtension}`;
const fontFileAbsolutePath = `${documentDirectoryPath}/${fontFileRelativePath}`;
await folderSafeCheck(fontsFolderAbsolutePath);
await RNFS.downloadFile({
fromUrl: file,
toFile: fontFileAbsolutePath
});
font[fontWeight] = {fontFamily, fontFile: fontFileRelativePath};
}
return font;
}
/**
* Load Font from remote url and save font file locally
* @param {string} url Url to remote font
* @param {string} key Custom key used to map to font family
* @param {string} weight Font weight of the remote font
* @returns {Promise} Promise that resolves to font family of installed font
*/
async loadFont(url, key, weight="default") {
if (this._nativeModuleNotAvailable) return;
key = key || url;
if (this.loadedFonts[key]?.[weight]) {
return this.loadedFonts[key][weight];
}
const fontFamily = await SvgEditorNativeModule.createFontWithUrl(url).catch(e => null);
if (!this.loadedFonts[key]) this.loadedFonts[key] = {};
this.loadedFonts[key][weight] = fontFamily;
return fontFamily;
}
/**
* Load Font stored locally
* @param {string} file Relative Url to font file
*/
async loadLocalFont(file, key, weight) {
if (this._nativeModuleNotAvailable) return;
if (!file) return;
key = key || file;
console.log('loadLocalFont: ', file, key, weight)
if (this.loadedFonts[key]?.[weight]) {
return this.loadedFonts[key][weight];
}
const fontFamily = await SvgEditorNativeModule.createFontWithLocalFile(file);
if (!this.loadedFonts[key]) this.loadedFonts[key] = {};
this.loadedFonts[key][weight] = fontFamily;
return fontFamily;
}
}
export const SvgEditorManager = new SvgEditorManagerObject();