LCOV - code coverage report
Current view: top level - engine/render - rendertext.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 57.0 % 114 65
Test Date: 2025-11-14 06:24:09 Functions: 50.0 % 18 9

            Line data    Source code
       1              : /**
       2              :  * @brief core text functionality for console/ui rendering
       3              :  *
       4              :  * libprimis uses a font image generated by the `tessfont` utility which has
       5              :  * letters sampled from it by reading small sub-rectangles of the overall font
       6              :  * image
       7              :  *
       8              :  * rendertext supports loading, recoloring, resizing, and rendering text to the
       9              :  * screen from these font images, which contain raster images of the font to be
      10              :  * rendered
      11              :  *
      12              :  * only a single font can be used at a time, though it can be scaled as needed
      13              :  * for various different purposes in UIs (e.g. titles vs body text)
      14              :  *
      15              :  * fonts are generated by the external `tessfont` utility and consist of a
      16              :  * raster image containing the acceptable characters which are sampled as needed
      17              :  */
      18              : #include "../libprimis-headers/cube.h"
      19              : #include "../../shared/geomexts.h"
      20              : #include "../../shared/glemu.h"
      21              : #include "../../shared/glexts.h"
      22              : 
      23              : #include "rendergl.h"
      24              : #include "rendertext.h"
      25              : #include "shaderparam.h"
      26              : #include "texture.h"
      27              : 
      28              : #include "interface/control.h"
      29              : 
      30              : constexpr int minreswidth = 640,
      31              :               minresheight = 480;
      32              : 
      33              : static std::unordered_map<std::string, Font> fonts;
      34              : static Font *fontdef = nullptr;
      35              : static int fontdeftex = 0;
      36              : 
      37              : Font *curfont = nullptr;
      38              : 
      39              : //adds a new font to the hashnameset "fonts" given the parameters passed
      40            2 : static void newfont(const char *name, const char *tex, const int *defaultw, const int *defaulth, const int *scale)
      41              : {
      42            2 :     auto insert = fonts.insert( {name, Font()} ).first;
      43            2 :     Font *f = &((*insert).second);
      44            2 :     f->name = std::string(name);
      45            2 :     f->texs.clear();
      46            2 :     f->texs.push_back(textureload(tex));
      47            2 :     f->chars.clear();
      48            2 :     f->charoffset = '!';
      49            2 :     f->defaultw = *defaultw;
      50            2 :     f->defaulth = *defaulth;
      51            2 :     f->scale = *scale > 0 ? *scale : f->defaulth;
      52            2 :     f->bordermin = 0.49f;
      53            2 :     f->bordermax = 0.5f;
      54            2 :     f->outlinemin = -1;
      55            2 :     f->outlinemax = 0;
      56              : 
      57            2 :     fontdef = f;
      58            2 :     fontdeftex = 0;
      59            2 : }
      60              : 
      61              : //sets the fontdef gvar's bordermin/max to the values passed
      62            1 : static void fontborder(const float *bordermin, const float *bordermax)
      63              : {
      64            1 :     if(!fontdef)
      65              :     {
      66            0 :         return;
      67              :     }
      68            1 :     fontdef->bordermin = *bordermin;
      69            1 :     fontdef->bordermax = std::max(*bordermax, *bordermin+0.01f);
      70              : }
      71              : 
      72              : //sets the fontdef gvar's outlinemin/max to the values passed
      73            1 : static void fontoutline(const float *outlinemin, const float *outlinemax)
      74              : {
      75            1 :     if(!fontdef)
      76              :     {
      77            0 :         return;
      78              :     }
      79            1 :     fontdef->outlinemin = std::min(*outlinemin, *outlinemax-0.01f);
      80            1 :     fontdef->outlinemax = *outlinemax;
      81              : }
      82              : 
      83              : /**
      84              :  * @brief sets the character offset for the currently loaded font
      85              :  *
      86              :  * Only the first element of the array is accepted, all others are ignored
      87              :  *
      88              :  * @param c a pointer to a char * array representing the character to be offset to
      89              :  */
      90            1 : static void fontoffset(const char *c)
      91              : {
      92            1 :     if(!fontdef)
      93              :     {
      94            0 :         return;
      95              :     }
      96            1 :     fontdef->charoffset = c[0];
      97              : }
      98              : 
      99              : /**
     100              :  * @brief sets the global scale for fonts
     101              :  *
     102              :  * If the scale parameter points to the value 0, the font scale is et to its default value.
     103              :  *
     104              :  * @param scale a pointer to an integer representing the new scale to set the font to
     105              :  */
     106            1 : static void fontscale(int *scale)
     107              : {
     108            1 :     if(!fontdef)
     109              :     {
     110            0 :         return;
     111              :     }
     112              : 
     113            1 : fontdef->scale = *scale > 0 ? *scale : fontdef->defaulth;
     114              : }
     115              : 
     116              : /**
     117              :  * @brief Adds an entry to the fontdef vector
     118              :  *
     119              :  * sets the new entry in the vector to have the parameters passed
     120              :  *
     121              :  * @param x x size of the charinfo
     122              :  * @param y y size of the charinfo
     123              :  * @param w width of the charinfo
     124              :  * @param h width of the charinfo
     125              :  * @param offsetx positional offset in x direction
     126              :  * @param offsety positional offset in y direction
     127              :  * @param advance x advance, if zero, set to offsetx + width
     128              :  */
     129            1 : static void fontchar(const float *x, const float *y, const float *w, const float *h, const float *offsetx, const float *offsety, const float *advance)
     130              : {
     131            1 :     if(!fontdef)
     132              :     {
     133            0 :         return;
     134              :     }
     135            1 :     fontdef->chars.emplace_back();
     136            1 :     Font::CharInfo &c = fontdef->chars.back();
     137            1 :     c.x = *x;
     138            1 :     c.y = *y;
     139            1 :     c.w = *w ? *w : fontdef->defaultw;
     140            1 :     c.h = *h ? *h : fontdef->defaulth;
     141            1 :     c.offsetx = *offsetx;
     142            1 :     c.offsety = *offsety;
     143            1 :     c.advance = *advance ? *advance : c.offsetx + c.w;
     144            1 :     c.tex = fontdeftex;
     145              : }
     146              : 
     147              : /**
     148              :  * @brief Adds empty entries to fontdef
     149              :  *
     150              :  * adds entries to the fontdef vector, which are empty. All of their values are set
     151              :  * to zero.
     152              :  *
     153              :  * @param n pointer to the number of elements to add. At least one element will be added.
     154              :  */
     155            1 : static void fontskip(const int *n)
     156              : {
     157            1 :     if(!fontdef)
     158              :     {
     159            0 :         return;
     160              :     }
     161            2 :     for(int i = 0; i < std::max(*n, 1); ++i)
     162              :     {
     163            1 :         fontdef->chars.emplace_back();
     164            1 :         Font::CharInfo &c = fontdef->chars.back();
     165            1 :         c.x = c.y = c.w = c.h = c.offsetx = c.offsety = c.advance = 0;
     166            1 :         c.tex = 0;
     167              :     }
     168              : }
     169              : 
     170            1 : bool setfont(const char *name)
     171              : {
     172            1 :     auto itr = fonts.find(name);
     173            1 :     if(itr == fonts.end())
     174              :     {
     175            0 :         return false;
     176              :     }
     177            1 :     curfont = &(*itr).second;
     178            1 :     return true;
     179              : }
     180              : 
     181              : static std::stack<Font *> fontstack;
     182              : 
     183            0 : void pushfont()
     184              : {
     185            0 :     fontstack.push(curfont);
     186            0 : }
     187              : 
     188            0 : bool popfont()
     189              : {
     190            0 :     if(fontstack.empty())
     191              :     {
     192            0 :         return false;
     193              :     }
     194            0 :     curfont = fontstack.top();
     195            0 :     fontstack.pop();
     196            0 :     return true;
     197              : }
     198              : 
     199            0 : void gettextres(int &w, int &h)
     200              : {
     201            0 :     if(w < minreswidth || h < minresheight)
     202              :     {
     203            0 :         if(minreswidth > w*minresheight/h)
     204              :         {
     205            0 :             h = h*minreswidth/w;
     206            0 :             w = minreswidth;
     207              :         }
     208              :         else
     209              :         {
     210            0 :             w = w*minresheight/h;
     211            0 :             h = minresheight;
     212              :         }
     213              :     }
     214            0 : }
     215              : 
     216            0 : float text_widthf(const char *str)
     217              : {
     218              :     float width, height;
     219            0 :     text_boundsf(str, width, height);
     220            0 :     return width;
     221              : }
     222              : 
     223            0 : static int texttab(float x)
     224              : {
     225            0 :     return (static_cast<int>((x)/(4*fontwidth()))+1.0f)*(4*fontwidth());
     226              : }
     227              : 
     228              : float textscale = 1;
     229              : 
     230              : #define TEXTSKELETON \
     231              :     float y = 0, \
     232              :           x = 0, \
     233              :           scale = curfont->scale/static_cast<float>(curfont->defaulth);\
     234              :     int i;\
     235              :     for(i = 0; str[i]; i++)\
     236              :     {\
     237              :         TEXTINDEX(i) /*textindex *must* be defined before runtime, it is not defined above here*/ \
     238              :         int c = static_cast<uchar>(str[i]);\
     239              :         if(c=='\t')\
     240              :         {\
     241              :             x = texttab(x);\
     242              :             TEXTWHITE(i)\
     243              :         }\
     244              :         else if(c==' ') \
     245              :         { \
     246              :             x += scale*curfont->defaultw; \
     247              :             TEXTWHITE(i) /*textwhite *must* be defined before runtime, it is not defined above here*/ \
     248              :         }\
     249              :         else if(c=='\n') \
     250              :         { \
     251              :             TEXTLINE(i) x = 0; \
     252              :             y += FONTH; \
     253              :         }\
     254              :         else if(c=='\f') \
     255              :         { \
     256              :             if(str[i+1]) \
     257              :             { \
     258              :                 i++; \
     259              :                 TEXTCOLOR(i) /*textcolor *must* be defined before runtime, it is not defined above here*/ \
     260              :             } \
     261              :         }\
     262              :         else if(curfont->chars.size() > static_cast<uint>(c-curfont->charoffset))\
     263              :         {\
     264              :             float cw = scale*curfont->chars[c-curfont->charoffset].advance;\
     265              :             if(cw <= 0) \
     266              :             { \
     267              :                 continue; \
     268              :             } \
     269              :             if(maxwidth >= 0)\
     270              :             {\
     271              :                 int j = i;\
     272              :                 float w = cw;\
     273              :                 for(; str[i+1]; i++)\
     274              :                 {\
     275              :                     int c = static_cast<uchar>(str[i+1]);\
     276              :                     if(c=='\f') \
     277              :                     { \
     278              :                         if(str[i+2]) \
     279              :                         { \
     280              :                             i++; \
     281              :                         } \
     282              :                         continue; \
     283              :                     } \
     284              :                     if(!(curfont->chars.size() > static_cast<uint>(c-curfont->charoffset))) \
     285              :                     { \
     286              :                         break; \
     287              :                     } \
     288              :                     float cw = scale*curfont->chars[c-curfont->charoffset].advance; \
     289              :                     if(cw <= 0 || w + cw > maxwidth) \
     290              :                     { \
     291              :                         break; \
     292              :                     } \
     293              :                     w += cw; \
     294              :                 } \
     295              :                 if(x + w > maxwidth && x > 0) \
     296              :                 { \
     297              :                     static_cast<void>(j); \
     298              :                     TEXTLINE(j-1); \
     299              :                     x = 0; \
     300              :                     y += FONTH; } \
     301              :                 TEXTWORD \
     302              :             } \
     303              :             else \
     304              :             { \
     305              :                 TEXTCHAR(i) \
     306              :             }\
     307              :         }\
     308              :     }
     309              : 
     310              : //all the chars are guaranteed to be either drawable or color commands
     311              : #define TEXTWORDSKELETON \
     312              :     for(; j <= i; j++)\
     313              :     {\
     314              :         TEXTINDEX(j) /*textindex *must* be defined before runtime, it is not defined above here*/ \
     315              :         int c = static_cast<uchar>(str[j]);\
     316              :         if(c=='\f') \
     317              :         { \
     318              :             if(str[j+1]) \
     319              :             { \
     320              :                 j++; \
     321              :                 TEXTCOLOR(j) /*textcolor *must* be defined before runtime, it is not defined above here*/ \
     322              :             } \
     323              :         }\
     324              :         else \
     325              :         { \
     326              :             float cw = scale*curfont->chars[c-curfont->charoffset].advance; \
     327              :             TEXTCHAR(j); \
     328              :         }\
     329              :     }
     330              : 
     331              : #define TEXTEND(cursor) \
     332              :     if(cursor >= i) \
     333              :     { \
     334              :         do \
     335              :         { \
     336              :             TEXTINDEX(cursor); /*textindex *must* be defined before runtime, it is not defined above here*/ \
     337              :         } while(0); \
     338              :     } \
     339              : 
     340            0 : int text_visible(const char *str, float hitx, float hity, int maxwidth)
     341              : {
     342              :     #define TEXTINDEX(idx)
     343              :     #define TEXTWHITE(idx) \
     344              :     { \
     345              :         if(y+FONTH > hity && x >= hitx) \
     346              :         { \
     347              :             return idx; \
     348              :         } \
     349              :     }
     350              :     #define TEXTLINE(idx) \
     351              :     { \
     352              :         if(y+FONTH > hity) \
     353              :         { \
     354              :             return idx; \
     355              :         } \
     356              :     }
     357              :     #define TEXTCOLOR(idx)
     358              :     #define TEXTCHAR(idx) \
     359              :     { \
     360              :         x += cw; \
     361              :         TEXTWHITE(idx) \
     362              :     }
     363              :     #define TEXTWORD TEXTWORDSKELETON
     364            0 :     TEXTSKELETON
     365              :     #undef TEXTINDEX
     366              :     #undef TEXTWHITE
     367              :     #undef TEXTLINE
     368              :     #undef TEXTCOLOR
     369              :     #undef TEXTCHAR
     370              :     #undef TEXTWORD
     371            0 :     return i;
     372              : }
     373              : 
     374              : //inverse of text_visible
     375            0 : void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth)
     376              : {
     377              :     #define TEXTINDEX(idx) \
     378              :     { \
     379              :         if(idx == cursor) \
     380              :         { \
     381              :             cx = x; \
     382              :             cy = y; \
     383              :             break; \
     384              :         } \
     385              :     }
     386              :     #define TEXTWHITE(idx)
     387              :     #define TEXTLINE(idx)
     388              :     #define TEXTCOLOR(idx)
     389              :     #define TEXTCHAR(idx) x += cw;
     390              :     #define TEXTWORD TEXTWORDSKELETON if(i >= cursor) break;
     391            0 :     cx = cy = 0;
     392            0 :     TEXTSKELETON
     393            0 :     TEXTEND(cursor)
     394              :     #undef TEXTINDEX
     395              :     #undef TEXTWHITE
     396              :     #undef TEXTLINE
     397              :     #undef TEXTCOLOR
     398              :     #undef TEXTCHAR
     399              :     #undef TEXTWORD
     400            0 : }
     401              : 
     402            0 : void text_boundsf(const char *str, float &width, float &height, int maxwidth)
     403              : {
     404              :     #define TEXTINDEX(idx)
     405              :     #define TEXTWHITE(idx)
     406              :     #define TEXTLINE(idx) if(x > width) width = x;
     407              :     #define TEXTCOLOR(idx)
     408              :     #define TEXTCHAR(idx) x += cw;
     409              :     #define TEXTWORD x += w;
     410            0 :     width = 0;
     411            0 :     TEXTSKELETON
     412            0 :     height = y + FONTH;
     413            0 :     TEXTLINE(_)
     414              :     #undef TEXTINDEX
     415              :     #undef TEXTWHITE
     416              :     #undef TEXTLINE
     417              :     #undef TEXTCOLOR
     418              :     #undef TEXTCHAR
     419              :     #undef TEXTWORD
     420            0 : }
     421              : 
     422            0 : void reloadfonts()
     423              : {
     424            0 :     for(auto &[k, f] : fonts)
     425              :     {
     426            0 :         for(size_t i = 0; i < f.texs.size(); i++)
     427              :         {
     428            0 :             if(!f.texs[i]->reload())
     429              :             {
     430            0 :                 fatal("failed to reload font texture");
     431              :             }
     432              :         }
     433              :     }
     434            0 : }
     435              : 
     436            1 : void initrendertextcmds()
     437              : {
     438            1 :     addcommand("font", reinterpret_cast<identfun>(newfont), "ssiii", Id_Command);
     439            1 :     addcommand("fontborder", reinterpret_cast<identfun>(fontborder), "ff", Id_Command);
     440            1 :     addcommand("fontoutline", reinterpret_cast<identfun>(fontoutline), "ff", Id_Command);
     441            1 :     addcommand("fontoffset", reinterpret_cast<identfun>(fontoffset), "s", Id_Command);
     442            1 :     addcommand("fontscale", reinterpret_cast<identfun>(fontscale), "i", Id_Command);
     443            1 :     addcommand("fontchar", reinterpret_cast<identfun>(fontchar), "fffffff", Id_Command);
     444            1 :     addcommand("fontskip", reinterpret_cast<identfun>(fontskip), "i", Id_Command);
     445            1 : }
        

Generated by: LCOV version 2.0-1