1 #define STB_DEFINE
2 #include "stb.h"
3 
4 #define STB_TRUETYPE_IMPLEMENTATION
5 #include "stb_truetype.h"
6 
7 #define STB_IMAGE_WRITE_IMPLEMENTATION
8 #include "stb_image_write.h"
9 
10 // used both to compute SDF and in 'shader'
11 float sdf_size = 32.0;          // the larger this is, the better large font sizes look
12 float pixel_dist_scale = 64.0;  // trades off precision w/ ability to handle *smaller* sizes
13 int onedge_value = 128;
14 int padding = 3; // not used in shader
15 
16 typedef struct
17 {
18    float advance;
19    signed char xoff;
20    signed char yoff;
21    unsigned char w,h;
22    unsigned char *data;
23 } fontchar;
24 
25 fontchar fdata[128];
26 
27 #define BITMAP_W  1200
28 #define BITMAP_H  800
29 unsigned char bitmap[BITMAP_H][BITMAP_W][3];
30 
31 char *sample = "This is goofy text, size %d!";
32 char *small_sample = "This is goofy text, size %d! Really needs in-shader supersampling to look good.";
33 
blend_pixel(int x,int y,int color,float alpha)34 void 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); // round
39 }
40 
draw_char(float px,float py,char c,float relative_scale)41 void 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 viewport
54    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;
58 
59    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 weights
71          bmx = bmx - sx0;
72          bmy = bmy - sy0;
73          // clamp to edge
74          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 sample
79          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 0
87          // non-anti-aliased
88          if (v > onedge_value)
89             blend_pixel(x,y,0,1.0);
90          #else
91          // Following math can be greatly simplified
92 
93          // convert distance in SDF value to distance in SDF bitmap
94          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 bitmap
96          pix_dist = sdf_dist * relative_scale;
97          // anti-alias by mapping 1/2 pixel around contour from 0..1 alpha
98          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          #endif
103       }
104    }
105 }
106 
107 
print_text(float x,float y,char * text,float scale)108 void 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 }
117 
main(int argc,char ** argv)118 int 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);
125 
126    scale = stbtt_ScaleForPixelHeight(&font, sdf_size);
127 
128    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    }
140 
141    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    }
149 
150    stbi_write_png("sdf_test.png", BITMAP_W, BITMAP_H, 3, bitmap, 0);
151    return 0;
152 }
153