UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

168 lines (143 loc) 4.21 kB
const R = require("ramda"); const logger = require("../../logger"); const EXT_FORMAT = { woff2: "woff2", woff: "woff", eot: "embedded-opentype", ttf: "truetype", otf: "opentype", }; const WEIGHT = { Thin: 100, ExtraLight: 200, UltraLight: 200, Light: 300, Normal: 400, Regular: 400, Medium: 500, SemiBold: 600, DemiBold: 600, Bold: 700, ExtraBold: 800, UltraBold: 800, Black: 900, Heavy: 900, }; const STYLE = { italic: "italic", normal: "normal", oblique: "oblique", }; /** * This method will return a font style if it is found in the font name * For more info https://www.w3.org/TR/css-fonts-3/#descdef-font-style * @param {String} familyName - accepts full family name "montserrat-latin-300italic" * @returns {?STYLE} returns a style string or nil */ function getFontStyle(familyName) { const name = R.toLower(familyName); const style = R.find(R.contains(R.__, name), R.keys(STYLE)); return style; } /** * This method will strip the font style from the font name to help parse weight * @param {STYLE} fontStyle - accepts font style string i.e. normal * @param {String} familyName - accepts full family name i.e. montserrat-latin-300italic * @returns {String} returns a font family name without style */ function stripStyle(fontStyle, familyName) { const name = R.toLower(familyName); return R.replace(fontStyle, "", name); } /** * This method will return the font weight if it is found in the font name * For more info https://www.w3.org/TR/css-fonts-3/#descdef-font-weight * @param {String} familyName - accepts full family name "montserrat-latin-300italic" * @returns {?STYLE} returns a style string or nil */ function getFontWeight(familyName) { const name = R.split(/[-._ ]/, R.toLower(familyName)); const weightKey = R.compose( R.find(R.compose((key) => R.any(R.equals(R.__, key), name), R.toLower)), R.keys )(WEIGHT); const weightValue = R.compose( R.find(R.curry((value) => R.any(R.equals(R.__, R.toString(value)), name))), R.values )(WEIGHT); return R.propOr(null, weightKey, WEIGHT) || weightValue; } /** * This method will parse the font string and split name, ext, style, and weight * Returns an object that has all of the data needed to create a font face declaration * For more info https://www.w3.org/TR/css-fonts-3/#descdef-src * https://www.w3.org/TR/css-fonts-3/#font-resources * @param {String} name - accepts unformatted font name "montserrat-latin-300italic.woff2" * @returns {?STYLE} returns a style string or nil */ function parseFont(name) { const splitName = R.split(".", name); const extension = R.toLower(R.last(splitName)); const family = R.join(".", R.init(splitName)); const style = getFontStyle(family); const weight = style ? getFontWeight(stripStyle(style, family)) : getFontWeight(family); if (EXT_FORMAT[extension] === EXT_FORMAT.eot) { logger.log(`Embedded Open Type fonts are not supported, removed: ${name}`); return null; } return ( family && { name, format: EXT_FORMAT[extension], family, weight, style, } ); } const commonFonts = [ "Ubuntu-Bold.ttf", "Ubuntu-BoldItalic.ttf", "Ubuntu-Italic.ttf", "Ubuntu-Light.ttf", "Ubuntu-LightItalic.ttf", "Ubuntu-Medium.ttf", "Ubuntu-MediumItalic.ttf", "Ubuntu-Regular.ttf", ]; const defaultFonts = { lg_tv: [ "Miso-Light.ttf", "Miso-Regular.ttf", "Miso-Bold.ttf", ...commonFonts, ], samsung_tv: [ "SamsungOne-200.ttf", "SamsungOne-300.ttf", "SamsungOne-400.ttf", "SamsungOne-600.ttf", "SamsungOne-700.ttf", "SamsungOne-800.ttf", ...commonFonts, ], }; function parseFonts(fonts = [], platform) { const platformFonts = defaultFonts[platform] || []; const appFonts = [...platformFonts, ...fonts]; const parsedFonts = []; try { if (appFonts && appFonts.length) { appFonts.forEach((font) => { const parsed = parseFont(font); parsed && parsedFonts.push(parsed); }); return parsedFonts; } } catch (error) { logger.log("Could not parse fonts at appBootstrapper", { error }); } } module.exports = { parseFonts };