0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-03-01 02:07:07 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/local/translateFontVariantId.ts
Alex Sánchez 58f7b0ab2c
Local Fonts (#81)
* moved validate font logic

* google fonts working

* fixes

* minor improvements

* fix linter

* local fonts

* fixes

* Changeset

* changeset

* refactor

* refactor

* update branch

* minor fix

* move files around

* try to refactor

* refactor

* add todo

* refactor

* refactor

* refactor

* refactor

* refactor

* refactor

* refactor move files to their concrete location

---------

Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
2024-04-30 08:00:11 +02:00

30 lines
987 B
TypeScript

import { LocalFont } from './localFont';
export const translateFontVariantId = (
localFont: LocalFont,
fontName: FontName,
fontWeight: number
): string | undefined => {
// check match by style and weight
const italic = fontName.style.toLowerCase().includes('italic');
const variantWithStyleWeight = localFont.variants?.find(
variant =>
variant.weight === fontWeight.toString() && variant.style === (italic ? 'italic' : 'normal')
);
if (variantWithStyleWeight !== undefined) return variantWithStyleWeight.id;
// check match directly by suffix if exists
const variant = localFont.variants?.find(
variant => variant.suffix === fontName.style.toLowerCase().replace(/\s/g, '')
);
if (variant !== undefined) return variant.id;
// check match directly by id
const variantById = localFont.variants?.find(
variant => variant.id === fontName.style.toLowerCase().replace(/\s/g, '')
);
if (variantById !== undefined) return variantById.id;
};