From f0dd23a154537afa6fe21f9b7bc0c4ab9b329336 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Mon, 22 Feb 2021 16:56:40 +0200 Subject: [PATCH] fix glyph clipping at the bottom --- index.js | 6 ++---- test/fixtures/1-out.json | 4 ++-- test/fixtures/1-sdf.png | Bin 3527 -> 3595 bytes 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 9d3ea19..33f66c3 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ export default class TinySDF { // If the glyph overflows the canvas size, it will be clipped at the bottom/right const glyphWidth = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft)); - const glyphHeight = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxAscent + actualBoundingBoxDescent)); + const glyphHeight = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxAscent) + Math.ceil(actualBoundingBoxDescent)); const width = glyphWidth + 2 * this.buffer; const height = glyphHeight + 2 * this.buffer; @@ -80,14 +80,12 @@ export default class TinySDF { gridOuter.fill(INF, 0, len); gridInner.fill(0, 0, len); - const offset = (width - glyphWidth) >> 1; - for (let y = 0; y < glyphHeight; y++) { for (let x = 0; x < glyphWidth; x++) { const a = imgData.data[4 * (y * glyphWidth + x) + 3] / 255; // alpha value if (a === 0) continue; // empty pixels - const j = (y + offset) * width + x + offset; + const j = (y + buffer) * width + x + buffer; if (a === 1) { // fully drawn pixels gridOuter[j] = 0; diff --git a/test/fixtures/1-out.json b/test/fixtures/1-out.json index 2a73176..c2fcc48 100644 --- a/test/fixtures/1-out.json +++ b/test/fixtures/1-out.json @@ -1,8 +1,8 @@ { "width": 54, - "height": 50, + "height": 51, "glyphWidth": 48, - "glyphHeight": 44, + "glyphHeight": 45, "glyphTop": 39, "glyphLeft": 0, "glyphAdvance": 48 diff --git a/test/fixtures/1-sdf.png b/test/fixtures/1-sdf.png index 079a9bdbc3b018da9a2cc263ead6f1d5e5136a2f..3b502808fcd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