stb_truetype: fix SDF handling of multiple move_to commands in a row
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 ca0e6d0 · Browse files · 2 files·+38 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+38 added−11 removed
- tests:+27 added−3 removed
- other:+11 added−8 removed
Provenance
- Commit
fc5b791- Tree
71f5213- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent
ca0e6d0
2 files changed+38 added−11 removed
modified:tests/test_truetype.c+27 added-3 removed
40 unmodified lines41 42 //debug();43 44 // @TODO: why is minglui.ttc failing? 45- fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/mingliu.ttc", "rb"));45+ //fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/mingliu.ttc", "rb"));46 47- //fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/x/DroidSansMono.ttf", "rb"));47+ fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/DejaVuSans.ttf", "rb"));48+49+ stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));50+51+#if 052 {53 static stbtt_pack_context pc;54 static stbtt_packedchar cd[256];55 static unsigned char atlas[1024*1024];1 unmodified line57 stbtt_PackBegin(&pc, atlas, 1024,1024,1024,1,NULL);58 stbtt_PackFontRange(&pc, ttf_buffer, 0, 32.0, 0, 256, cd);59 stbtt_PackEnd(&pc);60 }61+#endif62 63+ {64+ static stbtt_pack_context pc;65+ static stbtt_packedchar cd[256];66+ static unsigned char atlas[1024*1024];67+ unsigned char *data;68+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+73+ data = stbtt_GetCodepointSDF(&font, stbtt_ScaleForPixelHeight(&font,32.0), 'u', 4, 128, 128/4, &w,&h,&i,&j);74+ for (j=0; j < h; ++j) {75+ for (i=0; i < w; ++i) {76+ putchar(" .:ioVM@"[data[j*w+i]>>5]);77+ }78+ putchar('\n');79+ }80+ return 0;81+ }82+83 #if 084 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!85 stbi_write_png("fonttest1.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);86 29 unmodified lines116 }117 return 0;118 #endif119 95- stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));120 bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, (float)s), c, &w, &h, 0,0);121 122 for (j=0; j < h; ++j) {123 for (i=0; i < w; ++i)6 unmodified linesmodified:stb_truetype.h+11 added-8 removed
4585 unmodified lines4586 4587 for (i=0; i < num_verts; ++i) {4588 float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y;4589 4590- // check against every point here rather than inside line/curve primitives -- @TODO: wrong if multiple 'moves' in a row produce a garbage point, and given culling, probably more efficient to do within line/curve4591- float dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);4592- if (dist2 < min_dist*min_dist)4593- min_dist = (float) STBTT_sqrt(dist2);4594-4595- if (verts[i].type == STBTT_vline) {4590+ if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) {4591 float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y;4592 4593+ float dist,dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);4594+ if (dist2 < min_dist*min_dist)4595+ min_dist = (float) STBTT_sqrt(dist2);4596+4597 // coarse culling against bbox4598 //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist &&4599 // sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist)4601- float dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i];4600+ dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i];4601 STBTT_assert(i != 0);4602 if (dist < min_dist) {4603 // check position along line4604 // x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0)18 unmodified lines4623 int num=0;4624 float ax = x1-x0, ay = y1-y0;4625 float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2;4626 float mx = x0 - sx, my = y0 - sy;4628- float res[3],px,py,t,it;4627+ float res[3],px,py,t,it,dist2;4628 float a_inv = precompute[i];4629 if (a_inv == 0.0) { // if a_inv is 0, it's 2nd degree so use quadratic formula4630 float a = 3*(ax*bx + ay*by);4631 float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by);18 unmodified lines4650 float c = (2*(ax*ax + ay*ay) + (mx*bx+my*by)) * a_inv;4651 float d = (mx*ax+my*ay) * a_inv;4652 num = stbtt__solve_cubic(b, c, d, res);4653 }4654+ dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);4655+ if (dist2 < min_dist*min_dist)4656+ min_dist = (float) STBTT_sqrt(dist2);4657+4658 if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {4659 t = res[0], it = 1.0f - t;4660 px = it*it*x0 + 2*t*it*x1 + t*t*x2;4661 py = it*it*y0 + 2*t*it*y1 + t*t*y2;351 unmodified lines