diff --git a/index.js b/index.js index b452b7f..4e2f604 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,9 @@ export default class TinySDF { gridInner[j] = INF; } else { // aliased pixels - const d = 0.5 - a; + // gamma correction + const aLin = Math.pow(a, 1.0 / 2.2); + const d = 0.5 - aLin; gridOuter[j] = d > 0 ? d * d : 0; gridInner[j] = d < 0 ? d * d : 0; } diff --git a/package.json b/package.json index cedbd9e..f538a1a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "scripts": { "pretest": "eslint index.js index.html test", "test": "node --test", + "bench": "node ./test/bench.js", "start": "st --no-cache --localhost --index index.html ." }, "repository": { diff --git a/test/bench.js b/test/bench.js new file mode 100644 index 0000000..c953d49 --- /dev/null +++ b/test/bench.js @@ -0,0 +1,31 @@ +import TinySDF from '../index.js'; +import nodeCanvas from 'canvas'; + + +class MockTinySDF extends TinySDF { + _createCanvas(size) { + return nodeCanvas.createCanvas(size, size); + } +} + +function bench(iterationsCount) { + const sdf = new MockTinySDF({ + fontSize: 48, + buffer: 3 + }); + for (let i = 0; i < iterationsCount; ++i) { + sdf.draw('@'); + } +} + +function getCurrentTimestamp() { + return performance.now(); +} + +const ITERATIONS_COUNT = 1e5; +const tStart = getCurrentTimestamp(); +bench(ITERATIONS_COUNT); +const tEnd = getCurrentTimestamp(); +const dtSec = (tEnd - tStart) * 0.001; +console.log(`Durations for ${ITERATIONS_COUNT} iterations: ${dtSec} seconds`); + diff --git a/test/fixtures/1-metrics.json b/test/fixtures/1-metrics.json index 279c5a0..150a6cc 100644 --- a/test/fixtures/1-metrics.json +++ b/test/fixtures/1-metrics.json @@ -1,10 +1,10 @@ { - "width": 48, - "actualBoundingBoxLeft": 0, - "actualBoundingBoxRight": 48, - "actualBoundingBoxAscent": 39.2158203125, - "actualBoundingBoxDescent": 4.6083984375, - "emHeightAscent": 50.8798828125, - "emHeightDescent": 16.3203125, - "alphabeticBaseline": 13.9189453125 + "width": 48.2197265625, + "actualBoundingBoxLeft": 1.8720703125, + "actualBoundingBoxRight": 45.263671875, + "actualBoundingBoxAscent": 39.16796875, + "actualBoundingBoxDescent": 4.65625, + "emHeightAscent": 51, + "emHeightDescent": 17, + "alphabeticBaseline": 14.0390625 } \ No newline at end of file diff --git a/test/fixtures/1-out.json b/test/fixtures/1-out.json index 97c1b3b..19f4936 100644 --- a/test/fixtures/1-out.json +++ b/test/fixtures/1-out.json @@ -1,9 +1,9 @@ { - "width": 54, + "width": 50, "height": 51, - "glyphWidth": 48, + "glyphWidth": 44, "glyphHeight": 45, "glyphTop": 40, "glyphLeft": 0, - "glyphAdvance": 48 + "glyphAdvance": 48.2197265625 } \ No newline at end of file diff --git a/test/fixtures/1-raw.png b/test/fixtures/1-raw.png index d5bcf12..8a6f9c9 100644 Binary files a/test/fixtures/1-raw.png and b/test/fixtures/1-raw.png differ diff --git a/test/fixtures/1-sdf.png b/test/fixtures/1-sdf.png index 3b50280..d3d958f 100644 Binary files a/test/fixtures/1-sdf.png and b/test/fixtures/1-sdf.png differ