stb_easy_font.h
Go to the documentation of this file.
00001 // stb_easy_font.h - v0.6 - bitmap font for 3D rendering - public domain
00002 // Sean Barrett, Feb 2015
00003 //
00004 //    Easy-to-deploy,
00005 //    reasonably compact,
00006 //    extremely inefficient performance-wise,
00007 //    crappy-looking,
00008 //    ASCII-only,
00009 //    bitmap font for use in 3D APIs.
00010 //
00011 // Intended for when you just want to get some text displaying
00012 // in a 3D app as quickly as possible.
00013 //
00014 // Doesn't use any textures, instead builds characters out of quads.
00015 //
00016 // DOCUMENTATION:
00017 //
00018 //   int stb_easy_font_width(char *text)
00019 //
00020 //      Takes a string without newlines and returns the horizontal size.
00021 //
00022 //   int stb_easy_font_print(float x, float y,
00023 //                           char *text, unsigned char color[4],
00024 //                           void *vertex_buffer, int vbuf_size)
00025 //
00026 //      Takes a string (which can contain '\n') and fills out a
00027 //      vertex buffer with renderable data to draw the string.
00028 //      Output data assumes increasing x is rightwards, increasing y
00029 //      is downwards.
00030 //
00031 //      The vertex data is divided into quads, i.e. there are four
00032 //      vertices in the vertex buffer for each quad.
00033 //
00034 //      The vertices are stored in an interleaved format:
00035 //
00036 //         x:float
00037 //         y:float
00038 //         z:float
00039 //         color:uint8[4]
00040 //
00041 //      You can ignore z and color if you get them from elsewhere
00042 //      This format was chosen in the hopes it would make it
00043 //      easier for you to reuse existing buffer-drawing code.
00044 //
00045 //      If you pass in NULL for color, it becomes 255,255,255,255.
00046 //
00047 //      Returns the number of quads.
00048 //
00049 //      If the buffer isn't large enough, it will truncate.
00050 //      Expect it to use an average of ~270 bytes per character.
00051 //
00052 //      If your API doesn't draw quads, build a reusable index
00053 //      list that allows you to render quads as indexed triangles.
00054 //
00055 //   void stb_easy_font_spacing(float spacing)
00056 //
00057 //      Use positive values to expand the space between characters,
00058 //      and small negative values (no smaller than -1.5) to contract
00059 //      the space between characters. 
00060 //
00061 //      E.g. spacing = 1 adds one "pixel" of spacing between the
00062 //      characters. spacing = -1 is reasonable but feels a bit too
00063 //      compact to me; -0.5 is a reasonable compromise as long as
00064 //      you're scaling the font up.
00065 //
00066 // SAMPLE CODE:
00067 //
00068 //    Here's sample code for old OpenGL; it's a lot more complicated
00069 //    to make work on modern APIs, and that's your problem.
00070 //
00071 // LICENSE
00072 //
00073 //   This software is in the public domain. Where that dedication is not
00074 //   recognized, you are granted a perpetual, irrevocable license to copy,
00075 //   distribute, and modify this file as you see fit.
00076 //
00077 // VERSION HISTORY
00078 //
00079 //   (2016-01-26)        some variables below must be static to have multiple modules with 
00080 //   (2015-09-13)  0.6   #include <math.h>; updated license
00081 //   (2015-02-01)  0.5   First release
00082 
00083 #if 0
00084 void print_string(float x, float y, char *text, float r, float g, float b)
00085 {
00086   static char buffer[99999]; // ~500 chars
00087   int num_quads;
00088 
00089   num_quads = stb_easy_font_print(x, y, text, NULL, buffer, sizeof(buffer));
00090 
00091   glColor3f(r,g,b);
00092   glEnableClientState(GL_VERTEX_ARRAY);
00093   glVertexPointer(2, GL_FLOAT, 16, buffer);
00094   glDrawArrays(GL_QUADS, 0, num_quads*4);
00095   glDisableClientState(GL_VERTEX_ARRAY);
00096 }
00097 #endif
00098 
00099 #ifndef INCLUDE_STB_EASY_FONT_H
00100 #define INCLUDE_STB_EASY_FONT_H
00101 
00102 #include <stdlib.h>
00103 #include <math.h>
00104 
00105 struct {
00106     unsigned char advance;
00107     unsigned char h_seg;
00108     unsigned char v_seg;
00109 } static stb_easy_font_charinfo[96] = {
00110     {  5,  0,  0 },  {  3,  0,  0 },  {  5,  1,  1 },  {  7,  1,  4 },
00111     {  7,  3,  7 },  {  7,  6, 12 },  {  7,  8, 19 },  {  4, 16, 21 },
00112     {  4, 17, 22 },  {  4, 19, 23 },  { 23, 21, 24 },  { 23, 22, 31 },
00113     { 20, 23, 34 },  { 22, 23, 36 },  { 19, 24, 36 },  { 21, 25, 36 },
00114     {  6, 25, 39 },  {  6, 27, 43 },  {  6, 28, 45 },  {  6, 30, 49 },
00115     {  6, 33, 53 },  {  6, 34, 57 },  {  6, 40, 58 },  {  6, 46, 59 },
00116     {  6, 47, 62 },  {  6, 55, 64 },  { 19, 57, 68 },  { 20, 59, 68 },
00117     { 21, 61, 69 },  { 22, 66, 69 },  { 21, 68, 69 },  {  7, 73, 69 },
00118     {  9, 75, 74 },  {  6, 78, 81 },  {  6, 80, 85 },  {  6, 83, 90 },
00119     {  6, 85, 91 },  {  6, 87, 95 },  {  6, 90, 96 },  {  7, 92, 97 },
00120     {  6, 96,102 },  {  5, 97,106 },  {  6, 99,107 },  {  6,100,110 },
00121     {  6,100,115 },  {  7,101,116 },  {  6,101,121 },  {  6,101,125 },
00122     {  6,102,129 },  {  7,103,133 },  {  6,104,140 },  {  6,105,145 },
00123     {  7,107,149 },  {  6,108,151 },  {  7,109,155 },  {  7,109,160 },
00124     {  7,109,165 },  {  7,118,167 },  {  6,118,172 },  {  4,120,176 },
00125     {  6,122,177 },  {  4,122,181 },  { 23,124,182 },  { 22,129,182 },
00126     {  4,130,182 },  { 22,131,183 },  {  6,133,187 },  { 22,135,191 },
00127     {  6,137,192 },  { 22,139,196 },  {  5,144,197 },  { 22,147,198 },
00128     {  6,150,202 },  { 19,151,206 },  { 21,152,207 },  {  6,155,209 },
00129     {  3,160,210 },  { 23,160,211 },  { 22,164,216 },  { 22,165,220 },
00130     { 22,167,224 },  { 22,169,228 },  { 21,171,232 },  { 21,173,233 },
00131     {  5,178,233 },  { 22,179,234 },  { 23,180,238 },  { 23,180,243 },
00132     { 23,180,248 },  { 22,189,248 },  { 22,191,252 },  {  5,196,252 },
00133     {  3,203,252 },  {  5,203,253 },  { 22,210,253 },  {  0,214,253 },
00134 };
00135 
00136 static unsigned char stb_easy_font_hseg[214] = {
00137    97,37,69,84,28,51,2,18,10,49,98,41,65,25,81,105,33,9,97,1,97,37,37,36,
00138     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,
00139     98,8,25,33,65,81,10,50,17,65,97,25,33,25,49,9,65,20,68,1,65,25,49,41,
00140     11,105,13,101,76,10,50,10,50,98,11,99,10,98,11,50,99,11,50,11,99,8,57,
00141     58,3,99,99,107,10,10,11,10,99,11,5,100,41,65,57,41,65,9,17,81,97,3,107,
00142     9,97,1,97,33,25,9,25,41,100,41,26,82,42,98,27,83,42,98,26,51,82,8,41,
00143     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,
00144     26,82,41,25,33,82,26,49,73,35,90,17,81,41,65,57,41,65,25,81,90,114,20,
00145     84,73,57,41,49,25,33,65,81,9,97,1,97,25,33,65,81,57,33,25,41,25,
00146 };
00147 
00148 static unsigned char stb_easy_font_vseg[253] = {
00149    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,
00150     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,
00151     26,66,3,8,8,15,19,21,90,58,26,18,66,18,105,89,28,74,17,8,73,57,26,21,
00152     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,
00153     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,
00154     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,
00155     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,
00156     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,
00157     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,
00158     20,8,8,28,18,58,89,58,26,21,89,73,89,29,20,8,8,30,7,
00159 };
00160 
00161 typedef struct
00162 {
00163    unsigned char c[4];
00164 } stb_easy_font_color;
00165 
00166 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)
00167 {
00168     int i,j;
00169     for (i=0; i < num_segs; ++i) {
00170         int len = segs[i] & 7;
00171         x += (float) ((segs[i] >> 3) & 1);
00172         if (len && offset+64 <= vbuf_size) {
00173             float y0 = y + (float) (segs[i]>>4);
00174             for (j=0; j < 4; ++j) {
00175                 * (float *) (vbuf+offset+0) = x  + (j==1 || j==2 ? (vertical ? 1 : len) : 0);
00176                 * (float *) (vbuf+offset+4) = y0 + (    j >= 2   ? (vertical ? len : 1) : 0);
00177                 * (float *) (vbuf+offset+8) = 0.f;
00178                 * (stb_easy_font_color *) (vbuf+offset+12) = c;
00179                 offset += 16;
00180             }
00181         }
00182     }
00183     return offset;
00184 }
00185 
00186 static float stb_easy_font_spacing_val = 0;
00187 static void stb_easy_font_spacing(float spacing)
00188 {
00189    stb_easy_font_spacing_val = spacing;
00190 }
00191 
00192 static int stb_easy_font_print(float x, float y, char *text, unsigned char color[4], void *vertex_buffer, int vbuf_size)
00193 {
00194     char *vbuf = (char *) vertex_buffer;
00195     float start_x = x;
00196     int offset = 0;
00197 
00198     stb_easy_font_color c = { 255,255,255,255 }; // use structure copying to avoid needing depending on memcpy()
00199     if (color) { c.c[0] = color[0]; c.c[1] = color[1]; c.c[2] = color[2]; c.c[3] = color[3]; }
00200 
00201     while (*text && offset < vbuf_size) {
00202         if (*text == '\n') {
00203             y += 12;
00204             x = start_x;
00205         } else {
00206             unsigned char advance = stb_easy_font_charinfo[*text-32].advance;
00207             float y_ch = advance & 16 ? y+1 : y;
00208             int h_seg, v_seg, num_h, num_v;
00209             h_seg = stb_easy_font_charinfo[*text-32  ].h_seg;
00210             v_seg = stb_easy_font_charinfo[*text-32  ].v_seg;
00211             num_h = stb_easy_font_charinfo[*text-32+1].h_seg - h_seg;
00212             num_v = stb_easy_font_charinfo[*text-32+1].v_seg - v_seg;
00213             offset = stb_easy_font_draw_segs(x, y_ch, &stb_easy_font_hseg[h_seg], num_h, 0, c, vbuf, vbuf_size, offset);
00214             offset = stb_easy_font_draw_segs(x, y_ch, &stb_easy_font_vseg[v_seg], num_v, 1, c, vbuf, vbuf_size, offset);
00215             x += advance & 15;
00216             x += stb_easy_font_spacing_val;
00217         }
00218         ++text;
00219     }
00220     return (unsigned) offset/64;
00221 }
00222 
00223 static int stb_easy_font_width(char *text)
00224 {
00225     float len = 0;
00226     while (*text) {
00227         len += stb_easy_font_charinfo[*text-32].advance & 15;
00228         len += stb_easy_font_spacing_val;
00229         ++text;
00230     }
00231     return (int) ceil(len);
00232 }
00233 #endif


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:39