2018-07-14 18:14:24 +00:00
|
|
|
const widthsVerdana11 = require('./widths-verdana-11.json')
|
2018-07-19 06:54:54 +00:00
|
|
|
const astralRegex = require('unicode-astral-regex')
|
2018-05-30 02:43:23 +00:00
|
|
|
|
2018-06-22 15:49:01 +00:00
|
|
|
function calcWidth (charWidthTable) {
|
2018-07-18 16:35:09 +00:00
|
|
|
const SCALE = 10 // Prevent results like 60.599999999999994
|
2018-07-10 17:28:12 +00:00
|
|
|
const widthTable = charWidthTable.map(w => Math.round(w * SCALE))
|
2018-07-25 07:58:22 +00:00
|
|
|
widthTable[64] = widthTable[64] + 6 // Slightly increase width of "@" by 0.6px
|
2018-07-18 16:35:09 +00:00
|
|
|
|
2018-07-18 16:50:28 +00:00
|
|
|
return function (text, astral) {
|
|
|
|
if (astral) text = text.match(astralRegex)
|
2018-07-18 05:59:40 +00:00
|
|
|
|
|
|
|
let total = 0
|
2018-07-18 16:35:09 +00:00
|
|
|
let code = 0
|
2018-07-18 05:59:40 +00:00
|
|
|
let i = text.length
|
|
|
|
while (i--) {
|
2018-07-18 16:35:09 +00:00
|
|
|
code = text[i].charCodeAt()
|
|
|
|
total += widthTable[code < 127 ? code : 64] // Width as "@" for overflows
|
2018-06-22 15:49:01 +00:00
|
|
|
}
|
2018-07-18 05:59:40 +00:00
|
|
|
return total / SCALE
|
2018-05-30 02:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-07-14 18:14:24 +00:00
|
|
|
Verdana11: calcWidth(widthsVerdana11)
|
2018-05-30 02:43:23 +00:00
|
|
|
}
|