stb_truetype: fix incorrect antialiasing computation in v2 rasterizer, and handle certain cases where math blew up
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent e05ecc0 · Browse files · 3 files·+52 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−16 removed
- tests:+33 added−8 removed
- other:+19 added−8 removed
Provenance
- Commit
0be82e4- Tree
78be6a6- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent
e05ecc0
3 files changed+52 added−16 removed
modified:tests/stb.dsp+1 added-1 removed
64 unmodified lines65 # PROP Intermediate_Dir "Debug\stb"66 # PROP Ignore_Export_Lib 067 # PROP Target_Dir ""68 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c69-# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DS_TEST" /FR /FD /GZ /c69+# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "TT_TEST" /FR /FD /GZ /c70 # SUBTRACT CPP /YX71 # ADD BASE RSC /l 0x409 /d "_DEBUG"72 # ADD RSC /l 0x409 /d "_DEBUG"73 BSC32=bscmake.exe179 unmodified linesmodified:tests/test_truetype.c+32 added-7 removed
35 unmodified lines36 int main(int argc, char **argv)37 {38 stbtt_fontinfo font;39 unsigned char *bitmap;40- int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 34807), s = (argc > 2 ? atoi(argv[2]) : 32);40+ int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : '@'), s = (argc > 2 ? atoi(argv[2]) : 32);41 42 //debug();43 44 // @TODO: why is minglui.ttc failing? 4 unmodified lines49 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));50 51 #if 052 {53+ stbtt__bitmap b;54+ stbtt__point p[2];55+ int wcount[2] = { 2,0 };56+ p[0].x = 0.2f;57+ p[0].y = 0.3f;58+ p[1].x = 3.8f;59+ p[1].y = 0.8f;60+ b.w = 16;61+ b.h = 2;62+ b.stride = 16;63+ b.pixels = malloc(b.w*b.h);64+ stbtt__rasterize(&b, p, wcount, 1, 1, 1, 0, 0, 0, 0, 0, NULL);65+ for (i=0; i < 8; ++i)66+ printf("%f\n", b.pixels[i]/255.0);67+ }68+#endif69+70+#if 171+ {72 static stbtt_pack_context pc;73 static stbtt_packedchar cd[256];74 static unsigned char atlas[1024*1024];75 2 unmodified lines78 stbtt_PackEnd(&pc);79 }80 #endif81 82+83+#if 184 {85 static stbtt_pack_context pc;86 static stbtt_packedchar cd[256];87 static unsigned char atlas[1024*1024];88 unsigned char *data;89 69- stbtt_PackBegin(&pc, atlas, 1024,1024,1024,1,NULL);70- stbtt_PackFontRange(&pc, ttf_buffer, 0, 32.0, 'u', 1, cd);71- stbtt_PackEnd(&pc);72-90 data = stbtt_GetCodepointSDF(&font, stbtt_ScaleForPixelHeight(&font,32.0), 'u', 4, 128, 128/4, &w,&h,&i,&j);91 for (j=0; j < h; ++j) {92 for (i=0; i < w; ++i) {93 putchar(" .:ioVM@"[data[j*w+i]>>5]);94 }95 putchar('\n');96 }80- return 0;97 }98+#endif99 100 #if 0101 stbtt_BakeFontBitmap(ttf_buffer,stbtt_GetFontOffsetForIndex(ttf_buffer,0), 40.0, temp_bitmap[0],BITMAP_W,BITMAP_H, 32,96, cdata); // no guarantee this fits!102 stbi_write_png("fonttest1.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);30 unmodified lines133 }134 return 0;135 #endif136 120- bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, (float)s), c, &w, &h, 0,0);137+ (void)stbtt_GetCodepointBitmapSubpixel(&font,138+ 0.4972374737262726f,139+ 0.4986416995525360f,140+ 0.2391788959503174f,141+ 0.1752119064331055f,142+ 'd',143+ &w, &h,144+ 0,0);145 146+ bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, (float)s), c, &w, &h, 0,0);147 for (j=0; j < h; ++j) {148 for (i=0; i < w; ++i)149 putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);150 putchar('\n');4 unmodified linesmodified:stb_truetype.h+19 added-8 removed
3118 unmodified lines3119 scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height;3120 scanline_fill[x] += e->direction * height; // everything right of this pixel is filled3121 } else {3122 int x,x1,x2;3123- float y_crossing, step, sign, area;3123+ float y_crossing, y_final, step, sign, area;3124 // covers 2+ pixels3125 if (x_top > x_bottom) {3126 // flip scanline vertically; signed area is the same3127 float t;4 unmodified lines3132 dx = -dx;3133 dy = -dy;3134 t = x0, x0 = xb, xb = t;3135 }3136+ assert(dy >= 0);3137+ assert(dx >= 0);3138 3139 x1 = (int) x_top;3140 x2 = (int) x_bottom;3141 // compute intersection with y axis at x1+13142 y_crossing = (x1+1 - x0) * dy + y_top;3143+ // if x2 is right at the right edge of x1, y_crossing can blow up, github #10573144+ if (y_crossing > y_bottom)3145+ y_crossing = y_bottom;3146 3147 sign = e->direction;3148 // area of the rectangle covered from y0..y_crossing3149 area = sign * (y_crossing-sy0);3150 // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing)3146- scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);3151+ scanline[x1] += area * (x1+1 - x_top)/2;3152 3148- step = sign * dy;3153+ // check if final y_crossing is blown up; no test case for this3154+ y_final = y_crossing + dy * (x2 - (x1+1)); // advance y by number of steps taken below3155+ if (y_final > y_bottom) {3156+ y_final = y_bottom;3157+ dy = (y_final - y_crossing ) / (x2 - (x1+1)); // if denom=0, y_final = y_crossing, so y_final <= y_bottom3158+ }3159+3160+ step = sign * dy * 1; // dy is dy/dx, change in y for every 1 change in x, which is also how much pixel area changes for each step in x3161 for (x = x1+1; x < x2; ++x) {3150- scanline[x] += area + step/2;3162+ scanline[x] += area + step/2; // area of parallelogram is step/23163 area += step;3164 }3153- y_crossing += dy * (x2 - (x1+1));3165+ STBTT_assert(STBTT_fabs(area) <= 1.01f); // accumulated error from area += step unless we round step down3166 3155- STBTT_assert(STBTT_fabs(area) <= 1.01f);3167+ // area of the triangle (x2,y_crossing), (x_bottom,y1), (x2,y1)3168+ scanline[x2] += area + sign * (x_bottom - x2)/2 * (sy1-y_crossing);3169 3157- scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);3158-3170 scanline_fill[x2] += sign * (sy1-sy0);3171 }3172 } else {3173 // if edge goes outside of box we're drawing, we require1837 unmodified lines