added stb_easy_font.h
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 0d840ab · Browse files · 5 files·+452 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+452 added−17 removed
- other:+452 added−17 removed
Provenance
- Commit
a3e84e2- Tree
2096739- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
0d840ab
5 files changed+452 added−17 removed
added:data/easy_font_raw.pngbinary
A binary file, added.
added:tools/easy_font_maker.c+211 added
1+// This program was used to encode the data for stb_simple_font.h2+3+#define STB_DEFINE4+#include "stb.h"5+#define STB_IMAGE_IMPLEMENTATION6+#include "stb_image.h"7+8+int w,h;9+uint8 *data;10+11+int last_x[2], last_y[2];12+int num_seg[2], non_empty;13+#if 014+typedef struct15+{16+ unsigned short first_segment;17+ unsigned char advance;18+} chardata;19+20+typedef struct21+{22+ unsigned char x:4;23+ unsigned char y:4;24+ unsigned char len:3;25+ unsigned char dir:1;26+} segment;27+28+segment *segments;29+30+void add_seg(int x, int y, int len, int horizontal)31+{32+ segment s;33+ s.x = x;34+ s.y = y;35+ s.len = len;36+ s.dir = horizontal;37+ assert(s.x == x);38+ assert(s.y == y);39+ assert(s.len == len);40+ stb_arr_push(segments, s);41+}42+#else43+typedef struct44+{45+ unsigned char first_segment:8;46+ unsigned char first_v_segment:8;47+ unsigned char advance:5;48+ unsigned char voff:1;49+} chardata;50+51+#define X_LIMIT 152+#define LEN_LIMIT 753+54+typedef struct55+{56+ unsigned char dx:1;57+ unsigned char y:4;58+ unsigned char len:3;59+} segment;60+61+segment *segments;62+segment *vsegments;63+64+void add_seg(int x, int y, int len, int horizontal)65+{66+ segment s;67+68+ while (x - last_x[horizontal] > X_LIMIT) {69+ add_seg(last_x[horizontal] + X_LIMIT, 0, 0, horizontal);70+ }71+ while (len > LEN_LIMIT) {72+ add_seg(x, y, LEN_LIMIT, horizontal);73+ len -= LEN_LIMIT;74+ x += LEN_LIMIT*horizontal;75+ y += LEN_LIMIT*!horizontal;76+ }77+78+ s.dx = x - last_x[horizontal];79+ s.y = y;80+ s.len = len;81+ non_empty += len != 0;82+ //assert(s.x == x);83+ assert(s.y == y);84+ assert(s.len == len);85+ ++num_seg[horizontal];86+ if (horizontal)87+ stb_arr_push(segments, s);88+ else89+ stb_arr_push(vsegments, s);90+ last_x[horizontal] = x;91+}92+93+void print_segments(segment *s)94+{95+ int i, hpos;96+ printf(" ");97+ hpos = 4;98+ for (i=0; i < stb_arr_len(s); ++i) {99+ // repack for portability100+ unsigned char seg = s[i].len + s[i].dx*8 + s[i].y*16;101+ hpos += printf("%d,", seg);102+ if (hpos > 72 && i+1 < stb_arr_len(s)) {103+ hpos = 4;104+ printf("\n ");105+ }106+ }107+ printf("\n");108+}109+110+#endif111+112+chardata charinfo[128];113+114+int parse_char(int x, chardata *c, int offset)115+{116+ int start_x = x, end_x, top_y = 0, y;117+118+ c->first_segment = stb_arr_len(segments);119+ c->first_v_segment = stb_arr_len(vsegments) - offset;120+ assert(c->first_segment == stb_arr_len(segments));121+ assert(c->first_v_segment + offset == stb_arr_len(vsegments));122+123+ // find advance distance124+ end_x = x+1;125+ while (data[end_x*3] == 255)126+ ++end_x;127+ c->advance = end_x - start_x + 1;128+129+ last_x[0] = last_x[1] = 0;130+ last_y[0] = last_y[1] = 0;131+132+ for (y=2; y < h; ++y) {133+ for (x=start_x; x < end_x; ++x) {134+ if (data[y*3*w+x*3+1] < 255) {135+ top_y = y;136+ break;137+ }138+ }139+ if (top_y)140+ break;141+ }142+ c->voff = top_y > 2;143+ if (top_y > 2) 144+ top_y = 3;145+146+ for (x=start_x; x < end_x; ++x) {147+ int y;148+ for (y=2; y < h; ++y) {149+ if (data[y*3*w+x*3+1] < 255) {150+ if (data[y*3*w+x*3+0] == 255) { // red151+ int len=0;152+ while (y+len < h && data[(y+len)*3*w+x*3+0] == 255 && data[(y+len)*3*w+x*3+1] == 0) {153+ data[(y+len)*3*w+x*3+0] = 0;154+ ++len;155+ }156+ add_seg(x-start_x,y-top_y,len,0);157+ }158+ if (data[y*3*w+x*3+2] == 255) { // blue159+ int len=0;160+ while (x+len < end_x && data[y*3*w+(x+len)*3+2] == 255 && data[y*3*w+(x+len)*3+1] == 0) {161+ data[y*3*w+(x+len)*3+2] = 0;162+ ++len;163+ }164+ add_seg(x-start_x,y-top_y,len,1);165+ }166+ }167+ }168+ }169+ return end_x;170+}171+172+173+int main(int argc, char **argv)174+{175+ int c, x=0;176+ data = stbi_load("easy_font_raw.png", &w, &h, 0, 3);177+ for (c=32; c < 127; ++c) {178+ x = parse_char(x, &charinfo[c], 0);179+ printf("%3d -- %3d %3d\n", c, charinfo[c].first_segment, charinfo[c].first_v_segment);180+ }181+ printf("===\n");182+ printf("%d %d %d\n", num_seg[0], num_seg[1], non_empty);183+ printf("%d\n", sizeof(segments[0]) * stb_arr_len(segments));184+ printf("%d\n", sizeof(segments[0]) * stb_arr_len(segments) + sizeof(segments[0]) * stb_arr_len(vsegments) + sizeof(charinfo[32])*95);185+186+ printf("struct {\n"187+ " unsigned char advance;\n"188+ " unsigned char h_seg;\n"189+ " unsigned char v_seg;\n"190+ "} stb_easy_font_charinfo[96] = {\n");191+ charinfo[c].first_segment = stb_arr_len(segments);192+ charinfo[c].first_v_segment = stb_arr_len(vsegments);193+ for (c=32; c < 128; ++c) {194+ if ((c & 3) == 0) printf(" ");195+ printf("{ %2d,%3d,%3d },",196+ charinfo[c].advance + 16*charinfo[c].voff,197+ charinfo[c].first_segment,198+ charinfo[c].first_v_segment);199+ if ((c & 3) == 3) printf("\n"); else printf(" ");200+ }201+ printf("};\n\n");202+203+ printf("unsigned char stb_easy_font_hseg[%d] = {\n", stb_arr_len(segments));204+ print_segments(segments);205+ printf("};\n\n");206+207+ printf("unsigned char stb_easy_font_vseg[%d] = {\n", stb_arr_len(vsegments));208+ print_segments(vsegments);209+ printf("};\n");210+ return 0;211+}modified:tools/README.list+17 added-15 removed
1-stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2-stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3-stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4-stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5-stb_image_resize.h | graphics | resize images larger/smaller with good quality6-stb_rect_pack.h | graphics | simple 2D rectangle packer with decent quality7-stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++8-stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch9-stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor10-stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)11-stb_tilemap_editor.h | games | embeddable tilemap editor12-stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator13-stb_c_lexer.h | parsing | simplify writing parsers for C-like languages14-stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"15-stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff1+stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2+stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3+stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4+stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5+stb_image_resize.h | graphics | resize images larger/smaller with good quality6+stb_rect_pack.h | graphics | simple 2D rectangle packer with decent quality7+stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++8+stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch9+stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor10+stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)11+stb_easy_font.h | 3D graphics | quick-and-dirty easy-to-deploy bitmap font for printing frame rate, etc12+stb_tilemap_editor.h | game development | embeddable tilemap editor13+stb_herringbone_wang_tile.h | game development | herringbone Wang tile map generator14+stb_c_lexer.h | parsing | simplify writing parsers for C-like languages15+stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"16+stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff17+stb_leakcheck.h | misc | quick-and-dirty malloc/free leak-checkingmodified:README.md+4 added-2 removed
13 unmodified lines141516171818191920212223242526272861 unmodified lines13 unmodified lines**stretchy_buffer.h** | 1.01 | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++**stb_textedit.h** | 1.5 | UI | guts of a text editor for games etc implementing them from scratch**stb_dxt.h** | 1.04 | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor**stb_perlin.h** | 0.2 | 3D graphics | revised Perlin noise (3D input, 1D output)**stb_easy_font.h** | 0.5 | 3D graphics | quick-and-dirty easy-to-deploy bitmap font for printing frame rate, etc**stb_tilemap_editor.h** | 0.30 | games | embeddable tilemap editor**stb_herringbone_wang_tile.h** | 0.6 | games | herringbone Wang tile map generator**stb_tilemap_editor.h** | 0.30 | game development | embeddable tilemap editor**stb_herringbone_wang_tile.h** | 0.6 | game development | herringbone Wang tile map generator**stb_c_lexer.h** | 0.06 | parsing | simplify writing parsers for C-like languages**stb_divide.h** | 0.91 | math | more useful 32-bit modulus e.g. "euclidean divide"**stb.h** | 2.24 | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff**stb_leakcheck.h** | 0.1 | misc | quick-and-dirty malloc/free leak-checking
FAQ---
61 unmodified linesadded:stb_easy_font.h+220 added
1+// stb_easy_font.h - v0.5 - bitmap font for 3D rendering - public domain2+// Sean Barrett, Feb 20153+//4+// Easy-to-deploy,5+// reasonably compact,6+// extremely inefficient performance-wise,7+// crappy-looking,8+// ASCII-only,9+// bitmap font for use in 3D APIs.10+//11+// Intended for when you just want to get some text displaying12+// in a 3D app as quickly as possible.13+//14+// Doesn't use any textures, instead builds characters out of quads.15+//16+// DOCUMENTATION:17+//18+// int stb_easy_font_width(char *text)19+//20+// Takes a string without newlines and returns the horizontal size.21+//22+// int stb_easy_font_print(float x, float y,23+// char *text, unsigned char color[4],24+// void *vertex_buffer, int vbuf_size)25+//26+// Takes a string (which can contain '\n') and fills out a27+// vertex buffer with renderable data to draw the string.28+// Output data assumes increasing x is rightwards, increasing y29+// is downwards.30+//31+// The vertex data is divided into quads, i.e. there are four32+// vertices in the vertex buffer for each quad.33+//34+// The vertices are stored in an interleaved format:35+//36+// x:float37+// y:float38+// z:float39+// color:uint8[4]40+//41+// You can ignore z and color if you get them from elsewhere42+// This format was chosen in the hopes it would make it43+// easier for you to reuse existing buffer-drawing code.44+//45+// If you pass in NULL for color, it becomes 255,255,255,255.46+//47+// Returns the number of quads.48+//49+// If the buffer isn't large enough, it will truncate.50+// Expect it to use an average of ~270 bytes per character.51+//52+// If your API doesn't draw quads, build a reusable index53+// list that allows you to render quads as indexed triangles.54+//55+// void stb_easy_font_spacing(float spacing)56+//57+// Use positive values to expand the space between characters,58+// and small negative values (no smaller than -1.5) to contract59+// the space between characters. 60+//61+// E.g. spacing = 1 adds one "pixel" of spacing between the62+// characters. spacing = -1 is reasonable but feels a bit too63+// compact to me; -0.5 is a reasonable compromise as long as64+// you're scaling the font up.65+//66+// SAMPLE CODE:67+//68+// Here's sample code for old OpenGL; it's a lot more complicated69+// to make work on modern APIs, and that's your problem.70+//71+#if 072+void print_string(float x, float y, char *text, float r, float g, float b)73+{74+ static char buffer[99999]; // ~500 chars75+ int num_quads;76+77+ num_quads = stb_easy_font_print(x, y, text, NULL, buffer, sizeof(buffer));78+79+ glColor3f(r,g,b);80+ glEnableClientState(GL_VERTEX_ARRAY);81+ glVertexPointer(2, GL_FLOAT, 16, buffer);82+ glDrawArrays(GL_QUADS, 0, num_quads*4);83+ glDisableClientState(GL_VERTEX_ARRAY);84+}85+#endif86+87+#ifndef INCLUDE_STB_EASY_FONT_H88+#define INCLUDE_STB_EASY_FONT_H89+90+#include <stdlib.h>91+92+struct {93+ unsigned char advance;94+ unsigned char h_seg;95+ unsigned char v_seg;96+} stb_easy_font_charinfo[96] = {97+ { 5, 0, 0 }, { 3, 0, 0 }, { 5, 1, 1 }, { 7, 1, 4 },98+ { 7, 3, 7 }, { 7, 6, 12 }, { 7, 8, 19 }, { 4, 16, 21 },99+ { 4, 17, 22 }, { 4, 19, 23 }, { 23, 21, 24 }, { 23, 22, 31 },100+ { 20, 23, 34 }, { 22, 23, 36 }, { 19, 24, 36 }, { 21, 25, 36 },101+ { 6, 25, 39 }, { 6, 27, 43 }, { 6, 28, 45 }, { 6, 30, 49 },102+ { 6, 33, 53 }, { 6, 34, 57 }, { 6, 40, 58 }, { 6, 46, 59 },103+ { 6, 47, 62 }, { 6, 55, 64 }, { 19, 57, 68 }, { 20, 59, 68 },104+ { 21, 61, 69 }, { 22, 66, 69 }, { 21, 68, 69 }, { 7, 73, 69 },105+ { 9, 75, 74 }, { 6, 78, 81 }, { 6, 80, 85 }, { 6, 83, 90 },106+ { 6, 85, 91 }, { 6, 87, 95 }, { 6, 90, 96 }, { 7, 92, 97 },107+ { 6, 96,102 }, { 5, 97,106 }, { 6, 99,107 }, { 6,100,110 },108+ { 6,100,115 }, { 7,101,116 }, { 6,101,121 }, { 6,101,125 },109+ { 6,102,129 }, { 7,103,133 }, { 6,104,140 }, { 6,105,145 },110+ { 7,107,149 }, { 6,108,151 }, { 7,109,155 }, { 7,109,160 },111+ { 7,109,165 }, { 7,118,167 }, { 6,118,172 }, { 4,120,176 },112+ { 6,122,177 }, { 4,122,181 }, { 23,124,182 }, { 22,129,182 },113+ { 4,130,182 }, { 22,131,183 }, { 6,133,187 }, { 22,135,191 },114+ { 6,137,192 }, { 22,139,196 }, { 5,144,197 }, { 22,147,198 },115+ { 6,150,202 }, { 19,151,206 }, { 21,152,207 }, { 6,155,209 },116+ { 3,160,210 }, { 23,160,211 }, { 22,164,216 }, { 22,165,220 },117+ { 22,167,224 }, { 22,169,228 }, { 21,171,232 }, { 21,173,233 },118+ { 5,178,233 }, { 22,179,234 }, { 23,180,238 }, { 23,180,243 },119+ { 23,180,248 }, { 22,189,248 }, { 22,191,252 }, { 5,196,252 },120+ { 3,203,252 }, { 5,203,253 }, { 22,210,253 }, { 0,214,253 },121+};122+123+unsigned char stb_easy_font_hseg[214] = {124+ 97,37,69,84,28,51,2,18,10,49,98,41,65,25,81,105,33,9,97,1,97,37,37,36,125+ 81,10,98,107,3,100,3,99,58,51,4,99,58,8,73,81,10,50,98,8,73,81,4,10,50,126+ 98,8,25,33,65,81,10,50,17,65,97,25,33,25,49,9,65,20,68,1,65,25,49,41,127+ 11,105,13,101,76,10,50,10,50,98,11,99,10,98,11,50,99,11,50,11,99,8,57,128+ 58,3,99,99,107,10,10,11,10,99,11,5,100,41,65,57,41,65,9,17,81,97,3,107,129+ 9,97,1,97,33,25,9,25,41,100,41,26,82,42,98,27,83,42,98,26,51,82,8,41,130+ 35,8,10,26,82,114,42,1,114,8,9,73,57,81,41,97,18,8,8,25,26,26,82,26,82,131+ 26,82,41,25,33,82,26,49,73,35,90,17,81,41,65,57,41,65,25,81,90,114,20,132+ 84,73,57,41,49,25,33,65,81,9,97,1,97,25,33,65,81,57,33,25,41,25,133+};134+135+unsigned char stb_easy_font_vseg[253] = {136+ 4,2,8,10,15,8,15,33,8,15,8,73,82,73,57,41,82,10,82,18,66,10,21,29,1,65,137+ 27,8,27,9,65,8,10,50,97,74,66,42,10,21,57,41,29,25,14,81,73,57,26,8,8,138+ 26,66,3,8,8,15,19,21,90,58,26,18,66,18,105,89,28,74,17,8,73,57,26,21,139+ 8,42,41,42,8,28,22,8,8,30,7,8,8,26,66,21,7,8,8,29,7,7,21,8,8,8,59,7,8,140+ 8,15,29,8,8,14,7,57,43,10,82,7,7,25,42,25,15,7,25,41,15,21,105,105,29,141+ 7,57,57,26,21,105,73,97,89,28,97,7,57,58,26,82,18,57,57,74,8,30,6,8,8,142+ 14,3,58,90,58,11,7,74,43,74,15,2,82,2,42,75,42,10,67,57,41,10,7,2,42,143+ 74,106,15,2,35,8,8,29,7,8,8,59,35,51,8,8,15,35,30,35,8,8,30,7,8,8,60,144+ 36,8,45,7,7,36,8,43,8,44,21,8,8,44,35,8,8,43,23,8,8,43,35,8,8,31,21,15,145+ 20,8,8,28,18,58,89,58,26,21,89,73,89,29,20,8,8,30,7,146+};147+148+typedef struct149+{150+ unsigned char c[4];151+} stb_easy_font_color;152+153+static int stb_easy_font_draw_segs(float x, float y, unsigned char *segs, int num_segs, int vertical, stb_easy_font_color c, char *vbuf, int vbuf_size, int offset)154+{155+ int i,j;156+ for (i=0; i < num_segs; ++i) {157+ int len = segs[i] & 7;158+ x += (float) ((segs[i] >> 3) & 1);159+ if (len && offset+64 <= vbuf_size) {160+ float y0 = y + (float) (segs[i]>>4);161+ for (j=0; j < 4; ++j) {162+ * (float *) (vbuf+offset+0) = x + (j==1 || j==2 ? (vertical ? 1 : len) : 0);163+ * (float *) (vbuf+offset+4) = y0 + ( j >= 2 ? (vertical ? len : 1) : 0);164+ * (float *) (vbuf+offset+8) = 0.f;165+ * (stb_easy_font_color *) (vbuf+offset+12) = c;166+ offset += 16;167+ }168+ }169+ }170+ return offset;171+}172+173+float stb_easy_font_spacing_val = 0;174+static void stb_easy_font_spacing(float spacing)175+{176+ stb_easy_font_spacing_val = spacing;177+}178+179+static int stb_easy_font_print(float x, float y, char *text, unsigned char color[4], void *vertex_buffer, int vbuf_size)180+{181+ char *vbuf = (char *) vertex_buffer;182+ float start_x = x;183+ int offset = 0;184+185+ stb_easy_font_color c = { 255,255,255,255 }; // use structure copying to avoid needing depending on memcpy()186+ if (color) { c.c[0] = color[0]; c.c[1] = color[1]; c.c[2] = color[2]; c.c[3] = color[3]; }187+188+ while (*text && offset < vbuf_size) {189+ if (*text == '\n') {190+ y += 12;191+ x = start_x;192+ } else {193+ unsigned char advance = stb_easy_font_charinfo[*text-32].advance;194+ float y_ch = advance & 16 ? y+1 : y;195+ int h_seg, v_seg, num_h, num_v;196+ h_seg = stb_easy_font_charinfo[*text-32 ].h_seg;197+ v_seg = stb_easy_font_charinfo[*text-32 ].v_seg;198+ num_h = stb_easy_font_charinfo[*text-32+1].h_seg - h_seg;199+ num_v = stb_easy_font_charinfo[*text-32+1].v_seg - v_seg;200+ offset = stb_easy_font_draw_segs(x, y_ch, &stb_easy_font_hseg[h_seg], num_h, 0, c, vbuf, vbuf_size, offset);201+ offset = stb_easy_font_draw_segs(x, y_ch, &stb_easy_font_vseg[v_seg], num_v, 1, c, vbuf, vbuf_size, offset);202+ x += advance & 15;203+ x += stb_easy_font_spacing_val;204+ }205+ ++text;206+ }207+ return (unsigned) offset/64;208+}209+210+static int stb_easy_font_width(char *text)211+{212+ int len = 0;213+ while (*text) {214+ len += stb_easy_font_charinfo[*text-32].advance & 15;215+ len += stb_easy_font_spacing_val;216+ ++text;217+ }218+ return len;219+}220+#endif