0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-01-07 23:50:05 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/font/gfonts/translateGoogleFont.ts
Jordi Sala Morales 4edb964a96
Cache google fonts traversal (#176)
* wip

* revert

* extract as a class

* fix

* fix package.jsonm
2024-06-18 14:02:13 +02:00

32 lines
1,013 B
TypeScript

import slugify from 'slugify';
import { Cache } from '@plugin/Cache';
import { translateFontVariantId } from '@plugin/translators/text/font/gfonts';
import { FontId } from '@ui/lib/types/shapes/textShape';
import { items as gfonts } from './gfonts.json';
import { GoogleFont } from './googleFont';
const fontsCache = new Cache<string, GoogleFont>({ max: 30 });
export const translateGoogleFont = (fontName: FontName, fontWeight: number): FontId | undefined => {
const googleFont = getGoogleFont(fontName);
if (googleFont === undefined) return;
return {
fontId: `gfont-${slugify(fontName.family.toLowerCase())}`,
fontVariantId: translateFontVariantId(googleFont, fontName, fontWeight)
};
};
export const isGoogleFont = (fontName: FontName): boolean => {
return getGoogleFont(fontName) !== undefined;
};
const getGoogleFont = (fontName: FontName): GoogleFont | undefined => {
return fontsCache.get(fontName.family, () =>
gfonts.find(font => font.family === fontName.family)
);
};