-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple fonts support #58
Changes from all commits
62f3eb5
a6a1cf9
50d9e06
8ab35b0
a9bca53
7da126f
89c47fc
5a8de1b
7135e39
7d2b4c8
54606e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,37 +18,43 @@ const path = require("path"); | |
const { | ||
parseFontFile, | ||
buildStylesheet, | ||
buildFontJs | ||
buildFontJs, | ||
getSelector | ||
} = require("specimen-skeleton-support"); | ||
|
||
const srcDirectory = path.resolve(__dirname, "../", "src"); | ||
const fontsDirectory = path.resolve(srcDirectory, "fonts"); | ||
const dataDirectory = path.resolve(srcDirectory, "_data"); | ||
const fontsStylesheetPath = path.resolve(srcDirectory, "css", "font.css"); | ||
const fontJsPath = path.resolve(srcDirectory, "js", "font.js"); | ||
const dataDirectory = path.resolve(srcDirectory, "_data/fonts"); | ||
const fontsStylesheetPath = path.resolve(srcDirectory, "css", "fonts.css"); | ||
const fontJsPath = path.resolve(srcDirectory, "js", "fonts.js"); | ||
|
||
const assert = (condition, message) => { | ||
if (!condition) { | ||
throw new Error(message); | ||
} | ||
}; | ||
|
||
const _appendFile = util.promisify(fs.appendFile); | ||
const _writeFile = util.promisify(fs.writeFile); | ||
const writeFile = (path, contents) => { | ||
const writeFile = (path, contents, append) => { | ||
console.info("Writing", path); | ||
if (append) { | ||
return _appendFile(path, contents); | ||
} | ||
return _writeFile(path, contents); | ||
}; | ||
|
||
const writeDataFile = async (filename, data) => { | ||
const dataFilePath = path.join(dataDirectory, filename); | ||
const fileContents = JSON.stringify(data, null, 4); | ||
|
||
return writeFile(dataFilePath, fileContents); | ||
const writeDataFile = async (filename, fontName, data) => { | ||
fs.mkdir(path.join(dataDirectory, fontName), { recursive: true }, () => { | ||
const dataFilePath = path.join(dataDirectory, fontName, filename); | ||
const fileContents = JSON.stringify(data, null, 4); | ||
return writeFile(dataFilePath, fileContents); | ||
}); | ||
}; | ||
|
||
const writeDataFiles = async fontData => { | ||
const promises = Object.entries(fontData).map(([type, data]) => { | ||
return writeDataFile(`${type}.json`, data); | ||
const promises = Object.entries(fontData.data).map(([type, data]) => { | ||
return writeDataFile(`${type}.json`, getSelector(fontData, true), data); | ||
}); | ||
|
||
return Promise.all(promises); | ||
|
@@ -59,46 +65,58 @@ const writeStylesheet = async (fontData, fontFilePath) => { | |
path.dirname(fontsStylesheetPath), | ||
fontFilePath | ||
); | ||
const stylesheet = buildStylesheet(fontData, fontUrl).toString(); | ||
return writeFile(fontsStylesheetPath, stylesheet); | ||
let stylesheet = buildStylesheet(fontData, fontUrl).toString(); | ||
stylesheet += "\n\n"; | ||
return writeFile(fontsStylesheetPath, stylesheet, true); | ||
}; | ||
|
||
const writeFontJs = async fontData => { | ||
const js = buildFontJs(fontData); | ||
return writeFile(fontJsPath, js); | ||
return writeFile(fontJsPath, js, true); | ||
}; | ||
|
||
const findFirstFontFile = async directory => { | ||
const findFontFile = async directory => { | ||
const fontFiles = (await util.promisify(fs.readdir)(directory)).filter( | ||
f => path.extname(f) == ".woff2" | ||
); | ||
|
||
assert( | ||
fontFiles.length > 0, | ||
`No font file found. Place your font in ${path.relative( | ||
`No WOFF2 font files found. Place your WOFF2 fonts in ${path.relative( | ||
process.cwd(), | ||
directory | ||
)}.` | ||
); | ||
|
||
assert( | ||
fontFiles.length == 1, | ||
"Multiple font files found. Please specify the path to your font file explicitly." | ||
); | ||
const paths = fontFiles.map(fontFile => ({ | ||
RoelN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: path.basename(fontFile, path.extname(fontFile)), | ||
path: path.resolve(fontsDirectory, fontFile) | ||
})); | ||
|
||
return path.resolve(fontsDirectory, fontFiles[0]); | ||
return paths; | ||
}; | ||
|
||
const main = async () => { | ||
const fontFilePath = | ||
process.argv[2] || (await findFirstFontFile(fontsDirectory)); | ||
const fontData = await parseFontFile(fontFilePath); | ||
|
||
await Promise.all([ | ||
writeDataFiles(fontData.data), | ||
writeStylesheet(fontData, fontFilePath), | ||
writeFontJs(fontData) | ||
]); | ||
const fontFiles = process.argv[2] || (await findFontFile(fontsDirectory)); | ||
|
||
// Initialise files | ||
writeFile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, will do. |
||
fontsStylesheetPath, | ||
`/* Generated by the Specimen Skeleton */\n` | ||
); | ||
writeFile( | ||
fontJsPath, | ||
`/* Generated by the Specimen Skeleton */\nexport const fontNames = [];\n` | ||
); | ||
|
||
for (const fontFile of fontFiles) { | ||
const fontData = await parseFontFile(fontFile.path); | ||
await Promise.all([ | ||
writeDataFiles(fontData, fontFile.name), | ||
writeStylesheet(fontData, fontFile.path), | ||
writeFontJs(fontData, fontFile.name) | ||
]); | ||
} | ||
}; | ||
|
||
main().catch(e => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<div class="interactive-controls"> | ||
<h2>Axes & Instances</h2> | ||
<div class="interactive-controls {{ font[0] }}"> | ||
<h2>Axes & Instances for {{ font[0] }}</h2> | ||
<ul class="interactive-controls-sliders"> | ||
<li> | ||
<label for="interactive-controls-fontsize">Size</label> | ||
|
@@ -14,7 +14,7 @@ <h2>Axes & Instances</h2> | |
class="interactive-controls-slider" | ||
/> | ||
</li> | ||
{% for axis in axes %} | ||
{% for axis in font[1].axes %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this loop over all fonts? And why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the way 11ty returns data in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah clear! |
||
<li> | ||
<label for="axis-{{ axis.axis }}">{{ axis.name }}</label> | ||
<input | ||
|
@@ -37,7 +37,7 @@ <h2>Axes & Instances</h2> | |
id="interactive-controls-instances-select" | ||
class="interactive-controls-instances" | ||
> | ||
{% for instance in instances %} | ||
{% for instance in font[1].instances %} | ||
<option value="{{ instance.axes | json_stringify | escape }}" | ||
>{{ instance.name }}</option | ||
> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Put your WOFF2 font files here! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of mutating things. I'd rather build up the whole result and write it at once, instead of appending things. Many things can go wrong with appending, you need to handle order etc. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I'll see if I can refactor this.