Files
shields/gh-badges/lib/badge-cli.js
T
Paul MelnikowandGitHub 51897b3c7e Precompute text width using a lookup table (#2311)
This simplifies and further optimizes text-width computation by computing the entire width table in advance, and serializing it in the style of QuickTextMeasurer (#1390). This entirely removes the need for PDFKit at runtime. This has the advantage of fixing #1305 – more generally: producing the same result everywhere – without having to deploy a copy of Verdana.

The lifting is delegated to these three libraries, which are housed in a monorepo: https://github.com/metabolize/anafanafo

I'd be happy to move it into the badges org if folks want to collaborate on maintaining them.

QuickTextMeasurer took kerning pairs into account, whereas this implementation does not. I was thinking kerning would be a necessary refinement, though this seems to work well enough.

I dropped in a binary-search package to traverse the data structure, in part to conserve space. This causes a moderate performance regression, though there is ample room for improving on that: https://github.com/badges/shields/pull/2311#issuecomment-439182704
2018-11-15 17:27:21 -05:00

88 lines
2.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict'
const makeBadge = require('./make-badge')
const svg2img = require('./svg-to-img')
const colorscheme = require('./colorscheme.json')
if (process.argv.length < 4) {
console.log('Usage: badge subject status [:colorscheme] [.output] [@style]')
console.log(
'Or: badge subject status right-color [left-color] [.output] [@style]'
)
console.log()
console.log(` colorscheme: one of ${Object.keys(colorscheme).join(', ')}.`)
console.log(' left-color, right-color:')
console.log(' #xxx (three hex digits)')
console.log(' #xxxxxx (six hex digits)')
console.log(' color (CSS color)')
console.log(' output:')
console.log(' svg, png, jpg, or gif')
console.log()
console.log('Eg: badge cactus grown :green @flat')
console.log()
process.exit()
}
// Find a format specifier.
let format = 'svg'
let style = ''
for (let i = 4; i < process.argv.length; i++) {
if (process.argv[i][0] === '.') {
format = process.argv[i].slice(1)
process.argv.splice(i, 1)
continue
}
if (process.argv[i][0] === '@') {
style = process.argv[i].slice(1)
process.argv.splice(i, 1)
continue
}
}
const subject = process.argv[2]
const status = process.argv[3]
let color = process.argv[4] || ':green'
const colorA = process.argv[5]
const badgeData = { text: [subject, status], format }
if (style) {
badgeData.template = style
}
if (color[0] === ':') {
color = color.slice(1)
if (colorscheme[color] == null) {
// Colorscheme not found.
console.error('Invalid color scheme.')
process.exit(1)
}
badgeData.colorscheme = color
} else {
badgeData.colorB = color
if (colorA) {
badgeData.colorA = colorA
}
}
async function main() {
const svg = makeBadge(badgeData)
if (/png|jpg|gif/.test(format)) {
const data = await svg2img(svg, format)
process.stdout.write(data)
} else {
console.log(svg)
}
}
;(async () => {
try {
await main()
} catch (e) {
console.error(e)
process.exit(1)
}
})()