UNPKG

@npio/internals

Version:

A free visual website editor, powered with your own SolidJS components.

73 lines (63 loc) 1.99 kB
import { NitroFont } from "@prisma/client"; import { urlJoin } from "../../string"; const weightMappings: { weight: number; name: string }[] = [ { weight: 100, name: "thin" }, { weight: 100, name: "hairline" }, { weight: 200, name: "extra light" }, { weight: 300, name: "light" }, { weight: 400, name: "normal" }, { weight: 400, name: "regular" }, { weight: 500, name: "medium" }, { weight: 500, name: "dark" }, { weight: 600, name: "semi bold" }, { weight: 600, name: "demi bold" }, { weight: 800, name: "extra bold" }, { weight: 800, name: "ultra bold" }, { weight: 700, name: "bold" }, { weight: 900, name: "heavy" }, { weight: 950, name: "extra black" }, { weight: 950, name: "ultra black" }, { weight: 900, name: "black" }, { weight: 950, name: "super" }, ]; export const faceKey = function (weight = 400, style = "") { return `w${weight}--s${style}`; }; export const extractFontMeta = function (name: string) { const lowerName = name.toLowerCase(); let weight: number | undefined = undefined; let style = ""; for (const mapping of weightMappings) { if (lowerName.includes(mapping.weight + "")) { weight = mapping.weight; break; } } if (weight == null) { mappingLoop: for (const mapping of weightMappings) { const variants = [ mapping.name, mapping.name.replace(" ", "-"), mapping.name.replace(" ", "_"), mapping.name.replace(" ", ""), ]; for (const variant of variants) { if (lowerName.includes(variant)) { weight = mapping.weight; break mappingLoop; } } } } weight = weight ?? 400; if ( lowerName.includes("slanted") || lowerName.includes("italic") || lowerName.includes("oblique") ) { style = "italic"; } return { weight, style, key: faceKey(weight, style) }; }; export const fontPath = (font: NitroFont, ...parts: string[]) => urlJoin("fonts", font.publicId, ...parts);