UNPKG

@compositive/foundation

Version:

Compositive framework foundation package.

73 lines (70 loc) 2.31 kB
import { titleize } from '@compositive/commons-util'; import { FontStyle } from './FontStyle.js'; import { FontWeight } from './FontWeight.js'; const fontDesignationMap = new Map(); class FontDesignation { constructor(weight, style) { this.weight = weight; this.style = style; } static fromFvd(fvd) { var _a; const style = (_a = { n: FontStyle.Normal, i: FontStyle.Italic, o: FontStyle.Oblique, }[fvd.charAt(0)]) !== null && _a !== void 0 ? _a : FontStyle.Normal; const weight = FontWeight.for(Number.parseInt(fvd.charAt(1))); return this.for(weight, style); } static for(weight, style) { const key = toKey(weight, style); if (!fontDesignationMap.has(key)) { fontDesignationMap.set(key, new FontDesignation(weight, style)); } return fontDesignationMap.get(key); } get fallbacks() { return this.weight.fallbacks.flatMap((weight) => fontStyleAlternatives(this.style).map((style) => FontDesignation.for(weight, style))); } get fvd() { const weight = this.weight.value / 100; const style = { [FontStyle.Normal]: "n", [FontStyle.Italic]: "i", [FontStyle.Oblique]: "o", }[this.style]; return `${style}${weight}`; } get googleFontsDescription() { const weight = this.weight.value; const style = { [FontStyle.Normal]: "", [FontStyle.Italic]: "i", [FontStyle.Oblique]: "o", }[this.style]; return `${weight}${style}`; } toString() { return toKey(this.weight, this.style); } toHumanString() { return titleize(this.toString()); } } const toKey = (weight, style) => weight.name + (style === FontStyle.Normal ? "" : titleize(style)); const fontStyleAlternatives = (style) => { switch (style) { case FontStyle.Normal: { return [FontStyle.Normal]; } case FontStyle.Italic: { return [FontStyle.Italic, FontStyle.Oblique]; } case FontStyle.Oblique: { return [FontStyle.Oblique, FontStyle.Italic]; } } }; export { FontDesignation }; //# sourceMappingURL=FontDesignation.js.map