From 42c34d81c6a2b98e6d62a1b9b310237191f1456a Mon Sep 17 00:00:00 2001 From: Amio Date: Wed, 11 Jul 2018 00:52:33 +0800 Subject: [PATCH] Decrease float precision in width table --- lib/calc-text-width.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/calc-text-width.js b/lib/calc-text-width.js index 4499526..94de69c 100644 --- a/lib/calc-text-width.js +++ b/lib/calc-text-width.js @@ -1,13 +1,12 @@ // 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.333984375,3.333984375,4.259765625,6.673828125,6.673828125,10.669921875,8.00390625,2.291015625,3.99609375,3.99609375,4.669921875,7.0078125,3.333984375,3.99609375,3.333984375,3.333984375,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,6.673828125,3.333984375,3.333984375,7.0078125,7.0078125,7.0078125,6.673828125,12.181640625,8.00390625,8.00390625,8.666015625,8.666015625,8.00390625,7.330078125,9.333984375,8.666015625,3.333984375,6,8.00390625,6.673828125,9.99609375,8.666015625,9.333984375,8.00390625,9.333984375,8.666015625,8.00390625,7.330078125,8.666015625,8.00390625,11.326171875,8.00390625,8.00390625,7.330078125,3.333984375,3.333984375,3.333984375,5.630859375,6.673828125,3.99609375,6.673828125,6.673828125,6,6.673828125,6.673828125,3.333984375,6.673828125,6.673828125,2.666015625,2.666015625,6,2.666015625,9.99609375,6.673828125,6.673828125,6.673828125,6.673828125,3.99609375,6,3.333984375,6.673828125,6,8.666015625,6,6,6,4.0078125,3.1171875,4.0078125,7.0078125] -const Tahoma12Widths = [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.75,3.984375,4.81640625,8.73046875,6.55078125,11.71875,8.0859375,2.53125,4.59375,4.59375,6.55078125,8.73046875,3.6328125,4.359375,3.6328125,4.587890625,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,6.55078125,4.2421875,4.2421875,8.73046875,8.73046875,8.73046875,5.68359375,10.91015625,7.1953125,7.072265625,7.20703125,8.138671875,6.732421875,6.2578125,8.009765625,8.103515625,4.4765625,4.998046875,7.0546875,5.970703125,9.24609375,8.009765625,8.490234375,6.615234375,8.490234375,7.447265625,6.685546875,7.0078125,7.869140625,7.16015625,10.822265625,6.966796875,6.9140625,6.708984375,4.59375,4.587890625,4.59375,8.73046875,6.55078125,6.55078125,6.298828125,6.6328125,5.537109375,6.6328125,6.31640625,3.8203125,6.6328125,6.69140625,2.7421875,3.380859375,5.9765625,2.7421875,10.078125,6.69140625,6.515625,6.6328125,6.6328125,4.32421875,5.35546875,4.013671875,6.69140625,5.9765625,8.90625,5.94140625,5.9765625,5.33203125,5.765625,4.587890625,5.765625,8.73046875] -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.21875,4.72265625,5.5078125,9.8203125,7.62890625,12.9140625,8.71875,3.22265625,5.44921875,5.44921875,7.62890625,9.8203125,4.365234375,5.44921875,4.365234375,5.44921875,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,5.44921875,5.44921875,9.8203125,9.8203125,9.8203125,6.544921875,12,8.203125,8.2265625,8.37890625,9.24609375,7.587890625,6.896484375,9.3046875,9.017578125,5.05078125,5.455078125,8.314453125,6.6796875,10.11328125,8.9765625,9.4453125,7.236328125,9.4453125,8.34375,8.203125,7.39453125,8.783203125,8.203125,11.865234375,8.220703125,7.3828125,8.220703125,5.44921875,5.44921875,5.44921875,9.8203125,7.62890625,7.62890625,7.20703125,7.4765625,6.251953125,7.4765625,7.1484375,4.21875,7.4765625,7.59375,3.29296875,4.130859375,7.1015625,3.29296875,11.671875,7.59375,7.283203125,7.4765625,7.4765625,5.12109375,6.251953125,4.728515625,7.59375,7.1015625,9.8203125,7.1015625,7.1015625,6.3046875,7.6171875,5.44921875,7.6171875,9.8203125] +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] function calcWidth (charWidthTable) { - const SHIFT = 10 - const LETTER_SPACING = 0.4 * SHIFT - const widthTable = charWidthTable.map(w => Math.round(w * SHIFT + LETTER_SPACING)) + const SCALE = 100 + const LETTER_SPACING = 0.4 + const widthTable = charWidthTable.map(w => Math.round((w + LETTER_SPACING) * SCALE)) return function (text) { if (typeof text !== 'string') { return 0 @@ -19,13 +18,12 @@ function calcWidth (charWidthTable) { charCode = text[i].charCodeAt() total += widthTable[charCode < 127 ? charCode : 64] // "@" for overflows } - return total / SHIFT + return total / SCALE } } } module.exports = { Arial12: calcWidth(Arial12Widths), - Tahoma12: calcWidth(Tahoma12Widths), Verdana12: calcWidth(Verdana12Widths) }