-
Notifications
You must be signed in to change notification settings - Fork 934
Expand file tree
/
Copy pathandroidFontAssetHelpers.ts
More file actions
367 lines (314 loc) · 9.4 KB
/
androidFontAssetHelpers.ts
File metadata and controls
367 lines (314 loc) · 9.4 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import {isProjectUsingKotlin} from '@react-native-community/cli-platform-android';
import {CLIError, logger} from '@react-native-community/cli-tools';
import {XMLBuilder, XMLParser} from 'fast-xml-parser';
import fs from 'fs-extra';
import glob from 'tinyglobby';
import OpenType from 'opentype.js';
import path from 'path';
type FontXMLEntry = {
'@_app:font': string;
'@_app:fontStyle': string;
'@_app:fontWeight': number;
};
type FontXMLObject = {
'?xml': {
'@_version': string;
'@_encoding': string;
};
'font-family': {
'@_xmlns:app': string;
font: FontXMLEntry[];
};
};
type FontFamilyFile = {
name: string;
path: string;
weight: number;
isItalic: boolean;
};
type FontFamilyEntry = {
id: string;
files: FontFamilyFile[];
};
type FontFamilyMap = Record<string, FontFamilyEntry>;
const REACT_FONT_MANAGER_IMPORT =
'com.facebook.react.common.assets.ReactFontManager';
function toArrayBuffer(buffer: Buffer) {
const arrayBuffer = new ArrayBuffer(buffer.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; i += 1) {
view[i] = buffer[i];
}
return arrayBuffer;
}
function convertToAndroidResourceName(str: string) {
// Extract the file name (without extension) and the extension
const extension = path.extname(str);
const baseName = path.basename(str, extension);
// Remove any leading numbers from the base name and replace invalid characters with underscores
let cleanedBaseName = baseName.replace(/[^a-zA-Z0-9_]/g, '_').toLowerCase();
// Ensure the cleaned base name does not start with a number (prepend with an underscore if it does)
if (cleanedBaseName.match(/^[0-9]/)) {
cleanedBaseName = '_' + cleanedBaseName;
}
// Clean the extension by removing any invalid characters and convert to lowercase
// Note: path.extname includes the dot (.) in the extension, we preserve that
const cleanedExtension = extension
.replace(/[^a-zA-Z0-9_.]/g, '')
.toLowerCase();
// Reconstruct the file path with the cleaned base name and cleaned extension
return cleanedBaseName + cleanedExtension;
}
function getProjectFilePath(rootPath: string, name: string) {
const isUsingKotlin = isProjectUsingKotlin(rootPath);
const ext = isUsingKotlin ? 'kt' : 'java';
const filePath = glob.globSync(
path.join(rootPath, `app/src/main/java/**/${name}.${ext}`),
{expandDirectories: false},
)[0];
return filePath;
}
function getFontFamily(
fontFamily: OpenType.LocalizedName,
preferredFontFamily?: OpenType.LocalizedName,
) {
const availableFontFamily = preferredFontFamily || fontFamily;
return availableFontFamily.en || Object.values(availableFontFamily)[0];
}
/**
* Calculate a fallback weight to ensure it is multiple of 100 and between 100 and 900.
*
* Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#fallback_weights
*
* @param weight the font's weight.
* @returns a fallback weight multiple of 100, between 100 and 900, inclusive.
*/
function getFontFallbackWeight(weight: number) {
if (weight <= 500) {
return Math.max(Math.floor(weight / 100) * 100, 100);
} else {
return Math.min(Math.ceil(weight / 100) * 100, 900);
}
}
function getFontResFolderPath(rootPath: string) {
return path.join(rootPath, 'app/src/main/res/font');
}
function getXMLFontId(fontFileName: string) {
return `@font/${path.basename(fontFileName, path.extname(fontFileName))}`;
}
function buildXMLFontObjectEntry(fontFile: FontFamilyFile): FontXMLEntry {
return {
'@_app:fontStyle': fontFile.isItalic ? 'italic' : 'normal',
'@_app:fontWeight': fontFile.weight,
'@_app:font': getXMLFontId(fontFile.name),
};
}
function buildXMLFontObject(fontFiles: FontFamilyFile[]): FontXMLObject {
const fonts: FontXMLEntry[] = [];
fontFiles.forEach((fontFile) => {
const xmlEntry = buildXMLFontObjectEntry(fontFile);
// We can't have style / weight duplicates.
const foundEntryIndex = fonts.findIndex(
(font) =>
font['@_app:fontStyle'] === xmlEntry['@_app:fontStyle'] &&
font['@_app:fontWeight'] === xmlEntry['@_app:fontWeight'],
);
if (foundEntryIndex === -1) {
fonts.push(xmlEntry);
} else {
fonts[foundEntryIndex] = xmlEntry;
}
});
return {
'?xml': {
'@_version': '1.0',
'@_encoding': 'utf-8',
},
'font-family': {
'@_xmlns:app': 'http://schemas.android.com/apk/res-auto',
font: fonts,
},
};
}
function getAddCustomFontMethodCall(
fontName: string,
fontId: string,
isKotlin: boolean,
) {
return `ReactFontManager.getInstance().addCustomFont(this, "${fontName}", R.font.${fontId})${
isKotlin ? '' : ';'
}`;
}
function addImportToFile(
fileData: string,
importToAdd: string,
isKotlin: boolean,
) {
const importRegex = new RegExp(
`import\\s+${importToAdd}${isKotlin ? '' : ';'}`,
'gm',
);
const existingImport = importRegex.exec(fileData);
if (existingImport) {
return fileData;
}
const packageRegex = isKotlin ? /package\s+[\w.]+/ : /package\s+[\w.]+;/;
const packageMatch = packageRegex.exec(fileData);
if (packageMatch) {
return fileData.replace(
packageMatch[0],
`${packageMatch[0]}\n\nimport ${importToAdd}${isKotlin ? '' : ';'}`,
);
}
return fileData;
}
function insertLineInClassMethod(
fileData: string,
targetClass: string,
targetMethod: string,
codeToInsert: string,
lineToInsertAfter: string | undefined,
isKotlin: boolean,
) {
const classRegex = new RegExp(
isKotlin
? `class\\s+${targetClass}\\s*:\\s*\\S+\\(\\)\\s*,?\\s*(\\S+\\s*)?\\{`
: `class\\s+${targetClass}(\\s+extends\\s+\\S+)?(\\s+implements\\s+\\S+)?\\s*\\{`,
'gm',
);
const classMatch = classRegex.exec(fileData);
if (!classMatch) {
logger.error(`Class ${targetClass} not found.`);
return fileData;
}
const methodRegex = new RegExp(
isKotlin
? `override\\s+fun\\s+${targetMethod}\\s*\\(\\)`
: `(public|protected|private)\\s+(static\\s+)?\\S+\\s+${targetMethod}\\s*\\(`,
'gm',
);
let methodMatch = methodRegex.exec(fileData);
while (methodMatch) {
if (methodMatch.index > classMatch.index) {
break;
}
methodMatch = methodRegex.exec(fileData);
}
if (!methodMatch) {
logger.error(`Method ${targetMethod} not found in class ${targetClass}.`);
return fileData;
}
const openingBraceIndex = fileData.indexOf('{', methodMatch.index);
let closingBraceIndex = -1;
let braceCount = 1;
for (let i = openingBraceIndex + 1; i < fileData.length; i += 1) {
if (fileData[i] === '{') {
braceCount += 1;
} else if (fileData[i] === '}') {
braceCount -= 1;
}
if (braceCount === 0) {
closingBraceIndex = i;
break;
}
}
if (closingBraceIndex === -1) {
logger.error(
`Could not find closing brace for method ${targetMethod} in class ${targetClass}.`,
);
return fileData;
}
const methodBody = fileData.slice(openingBraceIndex + 1, closingBraceIndex);
if (methodBody.includes(codeToInsert.trim())) {
return fileData;
}
let insertPosition = closingBraceIndex;
if (lineToInsertAfter) {
const lineIndex = methodBody.indexOf(lineToInsertAfter.trim());
if (lineIndex !== -1) {
insertPosition =
openingBraceIndex + 1 + lineIndex + lineToInsertAfter.trim().length;
} else {
logger.error(
`Line "${lineToInsertAfter}" not found in method ${targetMethod} of class ${targetClass}.`,
);
return fileData;
}
}
return `${fileData.slice(
0,
insertPosition,
)}\n ${codeToInsert}${fileData.slice(insertPosition)}`;
}
function removeLineFromFile(fileData: string, stringToRemove: string) {
const lines = fileData.split('\n');
const updatedLines = lines.filter((line) => !line.includes(stringToRemove));
return updatedLines.join('\n');
}
function readAndParseFontFile(filePath: string): OpenType.Font {
let buffer: Buffer;
try {
buffer = fs.readFileSync(filePath);
} catch (e) {
throw new CLIError(`Failed to read "${filePath}" font file.`, e as Error);
}
return OpenType.parse(toArrayBuffer(buffer));
}
function readAndParseFontXMLFile(xmlFilePath: string): FontXMLObject {
try {
return xmlParser.parse(fs.readFileSync(xmlFilePath));
} catch (e) {
throw new CLIError(
`Failed to read "${xmlFilePath}" XML font file.`,
e as Error,
);
}
}
function writeFontXMLFile(xmlFilePath: string, xmlData: string): void {
try {
fs.outputFileSync(xmlFilePath, xmlData);
} catch (e) {
throw new CLIError(
`Failed to write / update "${xmlFilePath}" XML font file.`,
e as Error,
);
}
}
function getXMLFontFilePath(
platformPath: string,
fontFamilyId: string,
): string {
return path.join(getFontResFolderPath(platformPath), `${fontFamilyId}.xml`);
}
const xmlParser = new XMLParser({
ignoreAttributes: false,
isArray: (tagName) => tagName === 'font',
});
const xmlBuilder = new XMLBuilder({
format: true,
ignoreAttributes: false,
suppressEmptyNode: true,
});
export {
FontFamilyEntry,
FontFamilyMap,
FontXMLObject,
REACT_FONT_MANAGER_IMPORT,
addImportToFile,
buildXMLFontObject,
buildXMLFontObjectEntry,
convertToAndroidResourceName,
getAddCustomFontMethodCall,
getFontFallbackWeight,
getFontFamily,
getFontResFolderPath,
getProjectFilePath,
getXMLFontFilePath,
getXMLFontId,
insertLineInClassMethod,
readAndParseFontFile,
readAndParseFontXMLFile,
removeLineFromFile,
writeFontXMLFile,
xmlBuilder,
};