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 : /**
26 : * @brief Returns half of the nominal font height.
27 : *
28 : * Takes no parameters and uses no information about text being rendered, so only
29 : * provides the nominal size of 1/2 the height.
30 : *
31 : * @return half of the font height (curfont->scale)
32 : */
33 : extern int fontwidth();
34 :
35 : extern float textscale;
36 :
37 : /**
38 : * @brief Reloads all fonts in the font map.
39 : *
40 : * If any fonts fail to reload, terminates the program with an error message.
41 : */
42 : extern void reloadfonts();
43 :
44 : /**
45 : * @brief Attempts to set the global curfont variable to the passed object.
46 : *
47 : * No effect if the pointer passed is null.
48 : *
49 : * @param f the font to attempt to set.
50 : */
51 : extern void setfont(Font *f);
52 :
53 : /**
54 : * @brief Attempts to set the global curfont variable with a named entry in fonts global
55 : *
56 : * If no font exists by the name passed, returns false and curfont remains unchanged.
57 : *
58 : * @param name the name of the font to search for
59 : *
60 : * @return true if successfully set curfont, false otherwise
61 : */
62 : extern bool setfont(const char *name);
63 :
64 : /**
65 : * @brief Push whatever font is assigned to curfont to the font stack.
66 : *
67 : * fontstack is a std::stack of font pointers. The value in curfont will be
68 : * added to fontstack even if it is null or duplicated.
69 : */
70 : extern void pushfont();
71 : extern bool popfont();
72 : extern void gettextres(int &w, int &h);
73 :
74 : extern float text_widthf(const char *str);
75 : extern void text_boundsf(const char *str, float &width, float &height, int maxwidth = -1);
76 : extern int text_visible(const char *str, float hitx, float hity, int maxwidth);
77 : extern void text_posf(const char *str, int cursor, float &cx, float &cy, int maxwidth);
78 :
79 0 : inline int text_width(const char *str)
80 : {
81 0 : return static_cast<int>(std::ceil(text_widthf(str)));
82 : }
83 : #endif
|