feat: optimized font importing

This commit is contained in:
TresAbhi committed 2023-05-29 18:42:18 +00:00
1 parent 2dbf754572
commit 50369e9225
1 file changed
+20 -7
+20 -7
View File
@@ -1,19 +1,32 @@
import { Resvg } from '@resvg/resvg-js';
import { readFileSync } from 'fs';
import { readFile } from 'fs';
import { ReactNode } from 'react';
import satori from 'satori';
const robotoFlex = readFileSync('src/assets/fonts/Roboto.ttf');
const robotoFlexBold = readFileSync('src/assets/fonts/Roboto-Bold.ttf');
const robotoFlexBlack = readFileSync('src/assets/fonts/Roboto-Black.ttf');
const FONT_NAME = 'Roboto';
const FONT_FILES = ['Roboto', 'Roboto-Bold', 'Roboto-Black'];
console.log('Importing fonts...');
const [robotoFlex, robotoFlexBold, robotoFlexBlack] = await Promise.all(
FONT_FILES.map(
(file) =>
new Promise<Buffer>((resolve, reject) => {
readFile(`src/assets/fonts/${file}.ttf`, (error, data) => {
if (error) reject(error);
resolve(data);
});
}),
),
);
console.log('Fonts imported');
export default async function render(element: ReactNode) {
const svg = await satori(element, {
width: 640,
fonts: [
{ data: robotoFlex, name: 'Roboto Flex', weight: 400 },
{ data: robotoFlexBold, name: 'Roboto Flex', weight: 700 },
{ data: robotoFlexBlack, name: 'Roboto Flex', weight: 900 },
{ data: robotoFlex, name: FONT_NAME, weight: 400 },
{ data: robotoFlexBold, name: FONT_NAME, weight: 700 },
{ data: robotoFlexBlack, name: FONT_NAME, weight: 900 },
],
});
const png = new Resvg(svg).render().asPng();