Limit widthTable precision to 2

pull/3/head
Amio 2018-07-11 13:36:30 +08:00
rodzic f3b7b77426
commit aeadb70a85
2 zmienionych plików z 6 dodań i 6 usunięć

Wyświetl plik

@ -1,10 +1,10 @@
// Generate on https://codesandbox.io/s/lr4ynm652m
/* eslint-disable comma-spacing */
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 Verdana11Widths = [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.87,4.33,5.05,9,6.99,11.8,7.99,2.95,5,5,6.99,9,4,5,4,5,6.99,6.99,6.99,6.99,6.99,6.99,6.99,6.99,6.99,6.99,5,5,9,9,9,6,11,7.52,7.54,7.68,8.48,6.96,6.32,8.53,8.27,4.63,5,7.62,6.12,9.27,8.23,8.66,6.63,8.66,7.65,7.52,6.78,8.05,7.52,10.9,7.54,6.77,7.54,5,5,5,9,6.99,6.99,6.61,6.85,5.73,6.85,6.55,3.87,6.85,6.96,3.02,3.79,6.51,3.02,10.7,6.96,6.68,6.85,6.85,4.69,5.73,4.33,6.96,6.51,9,6.51,6.51,5.78,6.98,5,6.98,9]
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.2,4.7,5.5,9.8,7.6,13,8.7,3.2,5.4,5.4,7.6,9.8,4.4,5.4,4.4,5.4,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,5.4,5.4,9.8,9.8,9.8,6.5,12,8.2,8.2,8.4,9.2,7.6,6.9,9.3,9,5.1,5.5,8.3,6.7,10,9,9.4,7.2,9.4,8.3,8.2,7.4,8.8,8.2,12,8.2,7.4,8.2,5.4,5.4,5.4,9.8,7.6,7.6,7.2,7.5,6.3,7.5,7.1,4.2,7.5,7.6,3.3,4.1,7.1,3.3,12,7.6,7.3,7.5,7.5,5.1,6.3,4.7,7.6,7.1,9.8,7.1,7.1,6.3,7.6,5.4,7.6,9.8]
const Verdana11Widths = [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.9,4.3,5,9,7,12,8,3,5,5,7,9,4,5,4,5,7,7,7,7,7,7,7,7,7,7,5,5,9,9,9,6,11,7.5,7.5,7.7,8.5,7,6.3,8.5,8.3,4.6,5,7.6,6.1,9.3,8.2,8.7,6.6,8.7,7.6,7.5,6.8,8.1,7.5,11,7.5,6.8,7.5,5,5,5,9,7,7,6.6,6.9,5.7,6.9,6.6,3.9,6.9,7,3,3.8,6.5,3,11,7,6.7,6.9,6.9,4.7,5.7,4.3,7,6.5,9,6.5,6.5,5.8,7,5,7,9]
function calcWidth (charWidthTable) {
const SCALE = 100
const SCALE = 10
const widthTable = charWidthTable.map(w => Math.round(w * SCALE))
return function (text) {
if (typeof text !== 'string') {
@ -23,6 +23,6 @@ function calcWidth (charWidthTable) {
}
module.exports = {
Arial12: calcWidth(Arial12Widths),
Verdana12: calcWidth(Verdana12Widths),
Verdana11: calcWidth(Verdana11Widths)
}

Wyświetl plik

@ -2,8 +2,8 @@ const tap = require('tap')
const calcWidth = require('../lib/calc-text-width.js').Verdana11
tap.test('calc width for "npm"', t => {
t.ok(typeof calcWidth === 'function', 'export function: Verdana12')
t.ok(typeof calcWidth === 'function', 'export calcWidth function')
t.ok(Number.isFinite(calcWidth('npm')), 'result is a number')
t.is(calcWidth('npm'), 24.51, 'result is correct value')
t.is(calcWidth('npm'), 24.9, 'result is correct value')
t.end()
})