Line data Source code
1 : #ifndef RENDERTEXT_H_
2 : #define RENDERTEXT_H_
3 : struct Font final
4 : {
5 : struct CharInfo final
6 : {
7 : float x, y, w, h, offsetx, offsety, advance;
8 : int tex;
9 :
10 : };
11 :
12 : std::string name;
13 : std::vector<Texture *> texs;
14 : std::vector<CharInfo> chars;
15 : int charoffset, defaultw, defaulth, scale;
16 : float bordermin, bordermax, outlinemin, outlinemax;
17 :
18 2 : Font() {}
19 : };
20 :
21 : #define FONTH (curfont->scale)
22 :
23 : extern Font *curfont;
24 :
25 0 : inline int fontwidth()
26 : {
27 0 : return FONTH/2;
28 : }
29 :
30 : extern float textscale;
31 :
32 : /**
33 : * @brief Reloads all fonts in the font map.
34 : *
35 : * If any fonts fail to reload, terminates the program with an error message.
36 : */
37 : extern void reloadfonts();
38 :
39 : /**
40 : * @brief Attempts to set the global curfont variable to the passed object.
41 : *
42 : * No effect if the pointer passed is null.
43 : *
44 : * @param f the font to attempt to set.
45 : */
46 0 : inline void setfont(Font *f)
47 : {
48 0 : if(f)
49 : {
50 0 : curfont = f;
51 : }
52 0 : }
53 :
54 : /**
55 : * @brief Attempts to set the global curfont variable with a named entry in fonts global
56 : *
57 : * If no font exists by the name passed, returns false and curfont remains unchanged.
58 : *
59 : * @param name the name of the font to search for
60 : *
61 : * @return true if successfully set curfont, false otherwise
62 : */
63 : extern bool setfont(const char *name);
64 :
65 : /**
66 : * @brief Push whatever font is assigned to curfont to the font stack.
67 : *
68 : * fontstack is a std::stack of font pointers. The value in curfont will be
69 : * added to fontstack even if it is null or duplicated.
70 : */
71 : extern void pushfont();
72 : extern bool popfont();
73 : extern void gettextres(int &w, int &h);
74 :
75 : extern float text_widthf(const char *str);
76 : extern void text_boundsf(const char *str, float &width, float &height, int maxwidth = -1);
77 : extern int text_visible(const char *str, float hitx, float hity, int maxwidth);
78 : extern void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth);
79 :
80 0 : inline int text_width(const char *str)
81 : {
82 0 : return static_cast<int>(std::ceil(text_widthf(str)));
83 : }
84 : #endif
|