Skip to content
Myr

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

3 files+52 added−16 removed

modified:tests/stb.dsp+1 added-1 removed
64 unmodified lines
65# 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"73BSC32=bscmake.exe
179 unmodified lines
modified:tests/test_truetype.c+32 added-7 removed
35 unmodified lines
36int 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);4142 //debug();4344 // @TODO: why is minglui.ttc failing?
4 unmodified lines
49 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));5051#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#endif6970#if 171 {72 static stbtt_pack_context pc;73 static stbtt_packedchar cd[256];74 static unsigned char atlas[1024*1024];75
2 unmodified lines
78 stbtt_PackEnd(&pc);79 }80#endif818283#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;8969 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);7290 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#endif99100#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 lines
133 }134 return 0;135#endif136120 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);145146 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 lines
modified:stb_truetype.h+19 added-8 removed
3118 unmodified lines
3119 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 lines
3132 dx = -dx;3133 dy = -dy;3134 t = x0, x0 = xb, xb = t;3135 }3136 assert(dy >= 0);3137 assert(dx >= 0);31383139 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;31463147 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;31523148 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 }31593160 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 down31663155 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);31693157 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);31583170 scanline_fill[x2] += sign * (sy1-sy0);3171 }3172 } else {3173 // if edge goes outside of box we're drawing, we require
1837 unmodified lines