Add verification to the computed widths

This commit is contained in:
Frank A. Krueger 2018-02-01 22:19:47 -08:00
parent f05f232d5b
commit 0bbb3e95f5
1 changed files with 21 additions and 1 deletions

View File

@ -14,13 +14,16 @@ var ctx = canvas.getContext('2d');
var r = "static readonly double[] CharacterProportions = {\n "; var r = "static readonly double[] CharacterProportions = {\n ";
var head = ""; var head = "";
let size = 16; let size = 24;
ctx.font = "bold " + size + "px \"Helvetica Neue\""; ctx.font = "bold " + size + "px \"Helvetica Neue\"";
var mmm = ctx.measureText("MM"); var mmm = ctx.measureText("MM");
let sp = 0; let sp = 0;
let np = 0; let np = 0;
let mw = 0; let mw = 0;
let tx = 0;
let ty = size;
let widths = {};
for (let i = 0; i < 128; i++) { for (let i = 0; i < 128; i++) {
if (i > 0 && i % 8 == 0) { if (i > 0 && i % 8 == 0) {
head = ",\n "; head = ",\n ";
@ -32,6 +35,13 @@ for (let i = 0; i < 128; i++) {
let s = "M" + c + "M"; let s = "M" + c + "M";
let m = ctx.measureText(s); let m = ctx.measureText(s);
let w = m.width - mmm.width; let w = m.width - mmm.width;
if (tx + w > 320) {
tx = 0;
ty += size;
}
ctx.fillText(c, tx, ty);
ctx.strokeRect(tx, ty - size, w, size);
tx += w;
let p = w / size; let p = w / size;
if (p > 1e-4) { if (p > 1e-4) {
sp += p; sp += p;
@ -41,9 +51,19 @@ for (let i = 0; i < 128; i++) {
mw = w; mw = w;
} }
r += head + p; r += head + p;
widths[c] = w;
console.log (c + " = " + w); console.log (c + " = " + w);
} }
let test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
let testLen = 0;
for (const i in test) {
const w = widths[test[i]];
testLen += w;
}
console.log("TEST COMP LEN = " + testLen);
console.log("TEST REAL LEN = " + ctx.measureText(test).width);
let ap = sp / np; let ap = sp / np;
let padding = (mmm.width - mw*2)/size; let padding = (mmm.width - mw*2)/size;
r += "\n};\nconst double AverageCharProportion = " + ap + ";"; r += "\n};\nconst double AverageCharProportion = " + ap + ";";