2018-06-22 15:49:01 +00:00
|
|
|
// Generate on https://codesandbox.io/s/lr4ynm652m
|
2018-06-22 16:35:24 +00:00
|
|
|
/* eslint-disable comma-spacing */
|
2018-07-10 16:52:33 +00:00
|
|
|
const Arial12Widths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3.33,3.33,4.26,6.67,6.67,10.7,8,2.29,4,4,4.67,7.01,3.33,4,3.33,3.33,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,3.33,3.33,7.01,7.01,7.01,6.67,12.2,8,8,8.67,8.67,8,7.33,9.33,8.67,3.33,6,8,6.67,10,8.67,9.33,8,9.33,8.67,8,7.33,8.67,8,11.3,8,8,7.33,3.33,3.33,3.33,5.63,6.67,4,6.67,6.67,6,6.67,6.67,3.33,6.67,6.67,2.67,2.67,6,2.67,10,6.67,6.67,6.67,6.67,4,6,3.33,6.67,6,8.67,6,6,6,4.01,3.12,4.01,7.01]
|
|
|
|
const Verdana12Widths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4.22,4.72,5.51,9.82,7.63,12.9,8.72,3.22,5.45,5.45,7.63,9.82,4.37,5.45,4.37,5.45,7.63,7.63,7.63,7.63,7.63,7.63,7.63,7.63,7.63,7.63,5.45,5.45,9.82,9.82,9.82,6.54,12,8.2,8.23,8.38,9.25,7.59,6.9,9.3,9.02,5.05,5.46,8.31,6.68,10.1,8.98,9.45,7.24,9.45,8.34,8.2,7.39,8.78,8.2,11.9,8.22,7.38,8.22,5.45,5.45,5.45,9.82,7.63,7.63,7.21,7.48,6.25,7.48,7.15,4.22,7.48,7.59,3.29,4.13,7.1,3.29,11.7,7.59,7.28,7.48,7.48,5.12,6.25,4.73,7.59,7.1,9.82,7.1,7.1,6.3,7.62,5.45,7.62,9.82]
|
2018-05-30 02:43:23 +00:00
|
|
|
|
2018-06-22 15:49:01 +00:00
|
|
|
function calcWidth (charWidthTable) {
|
2018-07-10 16:52:33 +00:00
|
|
|
const SCALE = 100
|
|
|
|
const LETTER_SPACING = 0.4
|
|
|
|
const widthTable = charWidthTable.map(w => Math.round((w + LETTER_SPACING) * SCALE))
|
2018-06-22 15:49:01 +00:00
|
|
|
return function (text) {
|
|
|
|
if (typeof text !== 'string') {
|
2018-06-23 08:27:21 +00:00
|
|
|
return 0
|
2018-06-22 15:49:01 +00:00
|
|
|
} else {
|
2018-06-29 10:25:00 +00:00
|
|
|
let total = 0
|
|
|
|
let i = text.length
|
|
|
|
let charCode = 0
|
|
|
|
while (i--) {
|
|
|
|
charCode = text[i].charCodeAt()
|
|
|
|
total += widthTable[charCode < 127 ? charCode : 64] // "@" for overflows
|
|
|
|
}
|
2018-07-10 16:52:33 +00:00
|
|
|
return total / SCALE
|
2018-06-22 15:49:01 +00:00
|
|
|
}
|
2018-05-30 02:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-06-22 15:49:01 +00:00
|
|
|
Arial12: calcWidth(Arial12Widths),
|
|
|
|
Verdana12: calcWidth(Verdana12Widths)
|
2018-05-30 02:43:23 +00:00
|
|
|
}
|