mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
feat: optimized font importing
This commit is contained in:
1 parent
2dbf754572
commit
50369e9225
1 file changed
+20
-7
+20
-7
@@ -1,19 +1,32 @@
|
|||||||
import { Resvg } from '@resvg/resvg-js';
|
import { Resvg } from '@resvg/resvg-js';
|
||||||
import { readFileSync } from 'fs';
|
import { readFile } from 'fs';
|
||||||
import { ReactNode } from 'react';
|
import { ReactNode } from 'react';
|
||||||
import satori from 'satori';
|
import satori from 'satori';
|
||||||
|
|
||||||
const robotoFlex = readFileSync('src/assets/fonts/Roboto.ttf');
|
const FONT_NAME = 'Roboto';
|
||||||
const robotoFlexBold = readFileSync('src/assets/fonts/Roboto-Bold.ttf');
|
const FONT_FILES = ['Roboto', 'Roboto-Bold', 'Roboto-Black'];
|
||||||
const robotoFlexBlack = readFileSync('src/assets/fonts/Roboto-Black.ttf');
|
|
||||||
|
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) {
|
export default async function render(element: ReactNode) {
|
||||||
const svg = await satori(element, {
|
const svg = await satori(element, {
|
||||||
width: 640,
|
width: 640,
|
||||||
fonts: [
|
fonts: [
|
||||||
{ data: robotoFlex, name: 'Roboto Flex', weight: 400 },
|
{ data: robotoFlex, name: FONT_NAME, weight: 400 },
|
||||||
{ data: robotoFlexBold, name: 'Roboto Flex', weight: 700 },
|
{ data: robotoFlexBold, name: FONT_NAME, weight: 700 },
|
||||||
{ data: robotoFlexBlack, name: 'Roboto Flex', weight: 900 },
|
{ data: robotoFlexBlack, name: FONT_NAME, weight: 900 },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const png = new Resvg(svg).render().asPng();
|
const png = new Resvg(svg).render().asPng();
|
||||||
|
|||||||
Reference in new issue
Block a user