badgen/src/calc-text-width.ts

21 wiersze
585 B
TypeScript

2019-10-03 04:08:16 +00:00
// import widthsVerdana110 from './widths-verdana-110.json'
// @ts-ignore
2022-04-30 08:10:24 +00:00
const widthsVerdana110: number[] = require('./widths-verdana-110.json')
2022-04-30 08:10:24 +00:00
const calcWidth = (charWidthTable: number[]) => {
const fallbackWidth = charWidthTable[64] // Width as "@" for overflows
2018-07-18 16:35:09 +00:00
return ([...text]) => {
2018-07-18 05:59:40 +00:00
let total = 0
let charWidth = 0
2018-07-18 05:59:40 +00:00
let i = text.length
while (i--) {
charWidth = charWidthTable[text[i].charCodeAt()]
total += charWidth === undefined ? fallbackWidth : charWidth
2018-06-22 15:49:01 +00:00
}
return total
2018-05-30 02:43:23 +00:00
}
}
2019-10-03 04:08:16 +00:00
export const Verdana110 = calcWidth(widthsVerdana110)