Skip to content
Myr

sdf_test.c

This file

Path

tests/sdf/sdf_test.c

152 lines · 4.5 KiB · C

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1–152why, one step back
1#define STB_DEFINE
2#include "stb.h"34#define STB_TRUETYPE_IMPLEMENTATION5#include "stb_truetype.h"67#define STB_IMAGE_WRITE_IMPLEMENTATION8#include "stb_image_write.h"910// used both to compute SDF and in 'shader'11float sdf_size = 32.0; // the larger this is, the better large font sizes look12float pixel_dist_scale = 64.0; // trades off precision w/ ability to handle *smaller* sizes13int onedge_value = 128;14int padding = 3; // not used in shader1516typedef struct17{18 float advance;19 signed char xoff;20 signed char yoff;21 unsigned char w,h;22 unsigned char *data;23} fontchar;2425fontchar fdata[128];2627#define BITMAP_W 120028#define BITMAP_H 80029unsigned char bitmap[BITMAP_H][BITMAP_W][3];3031char *sample = "This is goofy text, size %d!";32char *small_sample = "This is goofy text, size %d! Really needs in-shader supersampling to look good.";3334void blend_pixel(int x, int y, int color, float alpha)35{36 int i;37 for (i=0; i < 3; ++i)38 bitmap[y][x][i] = (unsigned char) (stb_lerp(alpha, bitmap[y][x][i], color)+0.5); // round39}4041void draw_char(float px, float py, char c, float relative_scale)42{43 int x,y;44 fontchar *fc = &fdata[c];45 float fx0 = px + fc->xoff*relative_scale;46 float fy0 = py + fc->yoff*relative_scale;47 float fx1 = fx0 + fc->w*relative_scale;48 float fy1 = fy0 + fc->h*relative_scale;49 int ix0 = (int) floor(fx0);50 int iy0 = (int) floor(fy0);51 int ix1 = (int) ceil(fx1);52 int iy1 = (int) ceil(fy1);53 // clamp to viewport54 if (ix0 < 0) ix0 = 0;55 if (iy0 < 0) iy0 = 0;56 if (ix1 > BITMAP_W) ix1 = BITMAP_W;57 if (iy1 > BITMAP_H) iy1 = BITMAP_H;5859 for (y=iy0; y < iy1; ++y) {60 for (x=ix0; x < ix1; ++x) {61 float sdf_dist, pix_dist;62 float bmx = stb_linear_remap(x, fx0, fx1, 0, fc->w);63 float bmy = stb_linear_remap(y, fy0, fy1, 0, fc->h);64 int v00,v01,v10,v11;65 float v0,v1,v;66 int sx0 = (int) bmx;67 int sx1 = sx0+1;68 int sy0 = (int) bmy;69 int sy1 = sy0+1;70 // compute lerp weights71 bmx = bmx - sx0;72 bmy = bmy - sy0;73 // clamp to edge74 sx0 = stb_clamp(sx0, 0, fc->w-1);75 sx1 = stb_clamp(sx1, 0, fc->w-1);76 sy0 = stb_clamp(sy0, 0, fc->h-1);77 sy1 = stb_clamp(sy1, 0, fc->h-1);78 // bilinear texture sample79 v00 = fc->data[sy0*fc->w+sx0];80 v01 = fc->data[sy0*fc->w+sx1];81 v10 = fc->data[sy1*fc->w+sx0];82 v11 = fc->data[sy1*fc->w+sx1];83 v0 = stb_lerp(bmx,v00,v01);84 v1 = stb_lerp(bmx,v10,v11);85 v = stb_lerp(bmy,v0 ,v1 );86 #if 087 // non-anti-aliased88 if (v > onedge_value)89 blend_pixel(x,y,0,1.0);90 #else91 // Following math can be greatly simplified9293 // convert distance in SDF value to distance in SDF bitmap94 sdf_dist = stb_linear_remap(v, onedge_value, onedge_value+pixel_dist_scale, 0, 1);95 // convert distance in SDF bitmap to distance in output bitmap96 pix_dist = sdf_dist * relative_scale;97 // anti-alias by mapping 1/2 pixel around contour from 0..1 alpha98 v = stb_linear_remap(pix_dist, -0.5f, 0.5f, 0, 1);99 if (v > 1) v = 1;100 if (v > 0)101 blend_pixel(x,y,0,v);102 #endif103 }104 }105}106107108void print_text(float x, float y, char *text, float scale)109{110 int i;111 for (i=0; text[i]; ++i) {112 if (fdata[text[i]].data)113 draw_char(x,y,text[i],scale);114 x += fdata[text[i]].advance * scale;115 }116}117118int main(int argc, char **argv)119{120 int ch;121 float scale, ypos;122 stbtt_fontinfo font;123 void *data = stb_file("c:/windows/fonts/times.ttf", NULL);124 stbtt_InitFont(&font, data, 0);125126 scale = stbtt_ScaleForPixelHeight(&font, sdf_size);127128 for (ch=32; ch < 127; ++ch) {129 fontchar fc;130 int xoff,yoff,w,h, advance;131 fc.data = stbtt_GetCodepointSDF(&font, scale, ch, padding, onedge_value, pixel_dist_scale, &w, &h, &xoff, &yoff);132 fc.xoff = xoff;133 fc.yoff = yoff;134 fc.w = w;135 fc.h = h;136 stbtt_GetCodepointHMetrics(&font, ch, &advance, NULL);137 fc.advance = advance * scale;138 fdata[ch] = fc;139 }140141 ypos = 60;142 memset(bitmap, 255, sizeof(bitmap));143 print_text(400, ypos+30, stb_sprintf("sdf bitmap height %d", (int) sdf_size), 30/sdf_size);144 ypos += 80;145 for (scale = 8.0; scale < 120.0; scale *= 1.33f) {146 print_text(80, ypos+scale, stb_sprintf(scale == 8.0 ? small_sample : sample, (int) scale), scale / sdf_size);147 ypos += scale*1.05f + 20;148 }149150 stbi_write_png("sdf_test.png", BITMAP_W, BITMAP_H, 3, bitmap, 0);151 return 0;152}