stb_easy_font: support '\n' in _width(), add _height() [untested]
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 30015ef · Browse files · 1 file·+37 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
1 file+37 added−11 removed
- other:+37 added−11 removed
Provenance
- Commit
8b144eb- Tree
742e1cc- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
30015ef
1 file changed+37 added−11 removed
modified:stb_easy_font.h+37 added-11 removed
1-// stb_easy_font.h - v0.6 - bitmap font for 3D rendering - public domain1+// stb_easy_font.h - v0.7 - bitmap font for 3D rendering - public domain2 // Sean Barrett, Feb 20153 //4 // Easy-to-deploy,5 // reasonably compact,9 unmodified lines15 //16 // DOCUMENTATION:17 //18 // int stb_easy_font_width(char *text)19+// int stb_easy_font_height(char *text)20 //20-// Takes a string without newlines and returns the horizontal size.21+// Takes a string and returns the horizontal size and the22+// vertical size (which can vary if 'text' has newlines).23 //24 // int stb_easy_font_print(float x, float y,25 // char *text, unsigned char color[4],26 // void *vertex_buffer, int vbuf_size)14 unmodified lines41 // color:uint8[4]42 //43 // You can ignore z and color if you get them from elsewhere44 // This format was chosen in the hopes it would make it43-// easier for you to reuse existing buffer-drawing code.45+// easier for you to reuse existing vertex-buffer-drawing code.46 //47 // If you pass in NULL for color, it becomes 255,255,255,255.48 //49 // Returns the number of quads.14 unmodified lines64 // characters. spacing = -1 is reasonable but feels a bit too65 // compact to me; -0.5 is a reasonable compromise as long as66 // you're scaling the font up.67 //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-//68 // LICENSE69 //70 // This software is in the public domain. Where that dedication is not71 // recognized, you are granted a perpetual, irrevocable license to copy,72 // distribute, and modify this file as you see fit.73 //74 // VERSION HISTORY75 //76+// (2016-01-22) 0.7 width() supports multiline text; add height()77 // (2015-09-13) 0.6 #include <math.h>; updated license78 // (2015-02-01) 0.5 First release79 80 #if 081+// SAMPLE CODE:82+//83+// Here's sample code for old OpenGL; it's a lot more complicated84+// to make work on modern APIs, and that's your problem.85+//86 void print_string(float x, float y, char *text, float r, float g, float b)87 {88 static char buffer[99999]; // ~500 chars89 int num_quads;134 unmodified lines224 225 static int stb_easy_font_width(char *text)226 {227 float len = 0;228+ float max_len = 0;229 while (*text) {230+ if (*text == '\n') {231+ if (len > max_len) max_len = len;232+ len = 0;233+ } else {226- len += stb_easy_font_charinfo[*text-32].advance & 15;227- len += stb_easy_font_spacing_val;234+ len += stb_easy_font_charinfo[*text-32].advance & 15;235+ len += stb_easy_font_spacing_val;236+ }237 ++text;238 }239+ if (len > max_len) max_len = len;230- return (int) ceil(len);240+ return (int) ceil(max_len);241 }242+243+static int stb_easy_font_height(char *text)244+{245+ float y = 0;246+ int nonempty_line=0;247+ while (*text) {248+ if (*text == '\n') {249+ y += 12;250+ nonempty_line = 0;251+ } else {252+ nonempty_line = 1;253+ }254+ ++text;255+ }256+ return (int) ceil(y + (nonempty_line ? 12 : 0));257+}258 #endif