From 7e2ade58ea2933797fe50c7177a62481b8a0e4f6 Mon Sep 17 00:00:00 2001 From: yakov Date: Tue, 23 Mar 2021 00:33:05 -0400 Subject: [PATCH 1/4] stb_truetype -- fix floating point comparison against zero by using a correct epsilon --- stb_truetype.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stb_truetype.h b/stb_truetype.h index 62595a1..ddc7778 100644 --- a/stb_truetype.h +++ b/stb_truetype.h @@ -4547,6 +4547,9 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc // invert for y-downwards bitmaps scale_y = -scale_y; + // distance from singular values (in the same units as the pixel grid) + const float eps = 1./128, eps2 = eps*eps; + { int x,y,i,j; float *precompute; @@ -4560,15 +4563,15 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y; float dist = (float) STBTT_sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)); - precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist; + precompute[i] = (dist < eps) ? 0.0f : 1.0f / dist; } else if (verts[i].type == STBTT_vcurve) { float x2 = verts[j].x *scale_x, y2 = verts[j].y *scale_y; float x1 = verts[i].cx*scale_x, y1 = verts[i].cy*scale_y; float x0 = verts[i].x *scale_x, y0 = verts[i].y *scale_y; float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; float len2 = bx*bx + by*by; - if (len2 != 0.0f) - precompute[i] = 1.0f / (bx*bx + by*by); + if (len2 >= eps2) + precompute[i] = 1.0f / len2; else precompute[i] = 0.0f; } else @@ -4633,8 +4636,8 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc float a = 3*(ax*bx + ay*by); float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by); float c = mx*ax+my*ay; - if (a == 0.0) { // if a is 0, it's linear - if (b != 0.0) { + if (fabs(a) < eps2) { // if a is 0, it's linear + if (fabs(b) >= eps2) { res[num++] = -c/b; } } else { From d7a44685a82ce1cb5f5d5dfdee453a445f316238 Mon Sep 17 00:00:00 2001 From: yakov Date: Tue, 23 Mar 2021 00:37:50 -0400 Subject: [PATCH 2/4] use STBTT_fabs --- stb_truetype.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_truetype.h b/stb_truetype.h index ddc7778..92d4725 100644 --- a/stb_truetype.h +++ b/stb_truetype.h @@ -4636,8 +4636,8 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc float a = 3*(ax*bx + ay*by); float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by); float c = mx*ax+my*ay; - if (fabs(a) < eps2) { // if a is 0, it's linear - if (fabs(b) >= eps2) { + if (STBTT_fabs(a) < eps2) { // if a is 0, it's linear + if (STBTT_fabs(b) >= eps2) { res[num++] = -c/b; } } else { From d84b174fd354a501656ae8b506c9ae9a1f91176b Mon Sep 17 00:00:00 2001 From: yakov Date: Tue, 23 Mar 2021 00:40:06 -0400 Subject: [PATCH 3/4] add self --- stb_truetype.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_truetype.h b/stb_truetype.h index 92d4725..deea073 100644 --- a/stb_truetype.h +++ b/stb_truetype.h @@ -54,7 +54,7 @@ // Hou Qiming Derek Vinyard // Rob Loach Cort Stratton // Kenney Phillis Jr. Brian Costabile -// Ken Voskuil (kaesve) +// Ken Voskuil (kaesve) Yakov Galka // // VERSION HISTORY // From b1947dd6cfb289d4c823bf5caedc81258457aee1 Mon Sep 17 00:00:00 2001 From: yakov Date: Tue, 23 Mar 2021 01:01:42 -0400 Subject: [PATCH 4/4] pre-C99; decrease epsilon --- stb_truetype.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stb_truetype.h b/stb_truetype.h index deea073..e47062e 100644 --- a/stb_truetype.h +++ b/stb_truetype.h @@ -4547,10 +4547,9 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc // invert for y-downwards bitmaps scale_y = -scale_y; - // distance from singular values (in the same units as the pixel grid) - const float eps = 1./128, eps2 = eps*eps; - { + // distance from singular values (in the same units as the pixel grid) + const float eps = 1./1024, eps2 = eps*eps; int x,y,i,j; float *precompute; stbtt_vertex *verts;