Skip to content

Commit

Permalink
feat: remove normal CSS files (#112)
Browse files Browse the repository at this point in the history
* feat: remove normal generation for v1

* feat: remove normal css files for v2

* feat: remove normal CSS files for variable fonts

Co-authored-by: Lotus <DecliningLotus@users.noreply.github.com>
  • Loading branch information
ayuhito and Lotus committed Dec 14, 2020
1 parent 371db8d commit 5d155de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
18 changes: 11 additions & 7 deletions scripts/google/packager-v1.js
Expand Up @@ -60,7 +60,6 @@ module.exports = function (id) {
font.subsets.forEach(subset => {
const cssSubset = []
font.weights.forEach(weight => {
const cssWeight = []
font.styles.forEach(style => {
// Some fonts may have variants 400, 400i, 700 but not 700i.
if (style in font.variants[weight]) {
Expand All @@ -74,15 +73,20 @@ module.exports = function (id) {
woff2Path: makeFontFilePath(subset, weight, style, "woff2"),
woffPath: makeFontFilePath(subset, weight, style, "woff"),
})
cssWeight.push(css)
cssSubset.push(css)

const cssStylePath = `${fontDir}/${subset}-${weight}-${style}.css`
fs.writeFileSync(cssStylePath, css)
if (style === "normal") {
const cssPath = `${fontDir}/${subset}-${weight}.css`
fs.writeFileSync(cssPath, css)

// Should only push normal variants into subset
cssSubset.push(css)
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/${subset}-${weight}-${style}.css`
fs.writeFileSync(cssStylePath, css)
}
}
})
const cssWeightPath = `${fontDir}/${subset}-${weight}.css`
fs.writeFileSync(cssWeightPath, cssWeight.join(""))
})
const cssSubsetPath = `${fontDir}/${subset}.css`
fs.writeFileSync(cssSubsetPath, cssSubset.join(""))
Expand Down
23 changes: 14 additions & 9 deletions scripts/google/packager-v2.js
Expand Up @@ -86,7 +86,6 @@ module.exports = function (id) {
const unicodeKeys = Object.keys(font.unicodeRange)

font.weights.forEach(weight => {
const cssWeight = []
font.styles.forEach(style => {
const cssStyle = []
unicodeKeys.forEach(subset => {
Expand All @@ -109,18 +108,24 @@ module.exports = function (id) {
unicodeRange: font.unicodeRange[subset],
})
cssStyle.push(css)
cssWeight.push(css)
}
})
// Write down CSS
if (style in font.variants[weight]) {
const cssStylePath = `${fontDir}/${weight}-${style}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
if (style === "normal") {
const cssPath = `${fontDir}/${weight}.css`
fs.writeFileSync(cssPath, cssStyle.join(""))

// Generate index CSS
if (weight === "400") {
fs.writeFileSync(`${fontDir}/index.css`, cssStyle.join(""))
}
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/${weight}-${style}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
}
}
})
const cssWeightPath = `${fontDir}/${weight}.css`
fs.writeFileSync(cssWeightPath, cssWeight.join(""))
if (weight === "400") {
fs.writeFileSync(`${fontDir}/index.css`, cssWeight.join(""))
}
})
}
49 changes: 31 additions & 18 deletions scripts/google/variable.js
Expand Up @@ -53,7 +53,6 @@ module.exports = function (id) {
const variableName = `${font.family}Variable`

// wghtOnly CSS Generation
const css = []
font.styles.forEach(style => {
const cssStyle = []
font.subsets.forEach(subset => {
Expand All @@ -69,20 +68,25 @@ module.exports = function (id) {
unicodeRange: font.unicodeRange[subset],
})
cssStyle.push(cssWght)
css.push(cssWght)
})
const cssStylePath = `${fontDir}/variable-${style}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))

// Write down CSS
if (style === "normal") {
const cssPath = `${fontDir}/variable.css`
fs.writeFileSync(cssPath, cssStyle.join(""))
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/variable-${style}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
}
})
const cssPath = `${fontDir}/variable.css`
fs.writeFileSync(cssPath, css.join(""))

// full CSS Generation
if ("full" in fontVariable.variants) {
// Wdth requires a different CSS template (font-stretch)
if ("wdth" in fontVariable.axes) {
const css = []
font.styles.forEach(style => {
// Preserve the 'normal' style as a flag
const origStyle = style
if ("slnt" in fontVariable.axes && style === "normal") {
// SLNT has a different style linked to it.
Expand All @@ -105,15 +109,19 @@ module.exports = function (id) {
unicodeRange: font.unicodeRange[subset],
})
cssStyle.push(cssWght)
css.push(cssWght)
})
const cssStylePath = `${fontDir}/variable-full-${origStyle}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))

// Write down CSS
if (origStyle === "normal") {
const cssPath = `${fontDir}/variable-full.css`
fs.writeFileSync(cssPath, cssStyle.join(""))
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/variable-full-${origStyle}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
}
})
const cssPath = `${fontDir}/variable-full.css`
fs.writeFileSync(cssPath, css.join(""))
} else {
const css = []
font.styles.forEach(style => {
const origStyle = style
if ("slnt" in fontVariable.axes && style === "normal") {
Expand All @@ -136,13 +144,18 @@ module.exports = function (id) {
unicodeRange: font.unicodeRange[subset],
})
cssStyle.push(cssWght)
css.push(cssWght)
})
const cssStylePath = `${fontDir}/variable-full-${origStyle}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))

// Write down CSS
if (origStyle === "normal") {
const cssPath = `${fontDir}/variable-full.css`
fs.writeFileSync(cssPath, cssStyle.join(""))
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/variable-full-${origStyle}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
}
})
const cssPath = `${fontDir}/variable-full.css`
fs.writeFileSync(cssPath, css.join(""))
}
}
}

0 comments on commit 5d155de

Please sign in to comment.