libs/render/src/eu/render/font.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | |||
| 4 | #include <map> | ||
| 5 | #include <memory> | ||
| 6 | |||
| 7 | #include "eu/base/vec2.h" | ||
| 8 | #include "eu/base/vec3.h" | ||
| 9 | #include "eu/base/colors.h" | ||
| 10 | #include "eu/base/rect.h" | ||
| 11 | |||
| 12 | #include "eu/core/loadedfont.h" | ||
| 13 | // #include "eu/core/ui_text.h" | ||
| 14 | |||
| 15 | #include "eu/render/texture.h" | ||
| 16 | |||
| 17 | |||
| 18 | namespace eu::render | ||
| 19 | { | ||
| 20 | struct SpriteBatch; | ||
| 21 | |||
| 22 | // todo(Gustav): separate rendering and the rest and move to core | ||
| 23 | |||
| 24 | struct Glyph | ||
| 25 | { | ||
| 26 | Rect sprite_rect; // relative to 0,0 | ||
| 27 | Rect texture_rect; // image texture uvs | ||
| 28 | int code_point; // the character or string id | ||
| 29 | float advance; | ||
| 30 | |||
| 31 | Glyph | ||
| 32 | ( | ||
| 33 | const Rect& sprite, | ||
| 34 | const Rect& texture, | ||
| 35 | int ch, | ||
| 36 | float ad | ||
| 37 | ); | ||
| 38 | }; | ||
| 39 | |||
| 40 | using CharToGlyphMap = std::map<int, std::shared_ptr<Glyph>>; | ||
| 41 | |||
| 42 | struct DrawableFont; | ||
| 43 | |||
| 44 | enum class Align | ||
| 45 | { | ||
| 46 | top_left, | ||
| 47 | top_center, | ||
| 48 | top_right, | ||
| 49 | baseline_left, | ||
| 50 | baseline_center, | ||
| 51 | baseline_right, | ||
| 52 | bottom_left, | ||
| 53 | bottom_center, | ||
| 54 | bottom_right | ||
| 55 | }; | ||
| 56 | |||
| 57 | |||
| 58 | struct TextDrawCommand | ||
| 59 | { | ||
| 60 | const Texture2d* texture; | ||
| 61 | Rect sprite_rect; | ||
| 62 | Rect texture_rect; | ||
| 63 | bool hi; | ||
| 64 | |||
| 65 | TextDrawCommand | ||
| 66 | ( | ||
| 67 | const Texture2d* texture, | ||
| 68 | const Rect& sprite_rect, | ||
| 69 | const Rect& texture_rect, | ||
| 70 | bool hi | ||
| 71 | ); | ||
| 72 | }; | ||
| 73 | |||
| 74 | |||
| 75 | struct ListOfTextDrawCommands | ||
| 76 | { | ||
| 77 | std::vector<TextDrawCommand> commands; | ||
| 78 | |||
| 79 | void | ||
| 80 | add | ||
| 81 | ( | ||
| 82 | const Texture2d* texture, | ||
| 83 | const Rect& sprite_rect, | ||
| 84 | const Rect& texture_rect, | ||
| 85 | bool hi | ||
| 86 | ); | ||
| 87 | |||
| 88 | void | ||
| 89 | draw | ||
| 90 | ( | ||
| 91 | SpriteBatch* renderer, | ||
| 92 | const v2& start_position, | ||
| 93 | const Rgb& base_color, | ||
| 94 | const Rgb& hi_color | ||
| 95 | ); | ||
| 96 | |||
| 97 | [[nodiscard]] Rect | ||
| 98 | get_extents() const; | ||
| 99 | }; | ||
| 100 | |||
| 101 | |||
| 102 | struct DrawableText | ||
| 103 | { | ||
| 104 | public: | ||
| 105 | explicit DrawableText(DrawableFont* the_font); | ||
| 106 | ~DrawableText(); | ||
| 107 | |||
| 108 | DrawableText(const DrawableText& other) = delete; | ||
| 109 | void operator=(const DrawableText&) = delete; | ||
| 110 | DrawableText(DrawableText&& other) = delete; | ||
| 111 | void operator=(DrawableText&&) = delete; | ||
| 112 | |||
| 113 | void | ||
| 114 | set_text(const std::string& new_text); | ||
| 115 | |||
| 116 | void | ||
| 117 | set_background(bool new_use_background, float new_alpha = 0.5f); | ||
| 118 | |||
| 119 | void | ||
| 120 | set_alignment(Align new_alignment); | ||
| 121 | |||
| 122 | void | ||
| 123 | set_size(float new_size); | ||
| 124 | |||
| 125 | void | ||
| 126 | draw | ||
| 127 | ( | ||
| 128 | SpriteBatch* renderer, | ||
| 129 | const v2& p, | ||
| 130 | const Rgb& base_hi_color | ||
| 131 | ) const; | ||
| 132 | |||
| 133 | void | ||
| 134 | draw | ||
| 135 | ( | ||
| 136 | SpriteBatch* renderer, | ||
| 137 | const v2& p, | ||
| 138 | const Rgb& base_color, | ||
| 139 | const Rgb& hi_color | ||
| 140 | ) const; | ||
| 141 | |||
| 142 | Rect | ||
| 143 | get_extents() const; | ||
| 144 | |||
| 145 | void | ||
| 146 | compile() const; | ||
| 147 | |||
| 148 | private: | ||
| 149 | const DrawableFont* font; | ||
| 150 | float size; | ||
| 151 | std::string text; | ||
| 152 | Align alignment; | ||
| 153 | |||
| 154 | bool use_background; | ||
| 155 | float background_alpha; | ||
| 156 | |||
| 157 | // updated in Compile function | ||
| 158 | mutable bool is_dirty; | ||
| 159 | mutable ListOfTextDrawCommands commands; | ||
| 160 | }; | ||
| 161 | |||
| 162 | |||
| 163 | struct UiTextCompileVisitor; | ||
| 164 | |||
| 165 | |||
| 166 | struct DrawableFont | ||
| 167 | { | ||
| 168 | float ascent = 0; | ||
| 169 | float descent = 0; | ||
| 170 | float line_gap = 0; | ||
| 171 | ✗ | constexpr float get_line_height() const | |
| 172 | { | ||
| 173 | ✗ | return ascent - descent + line_gap; | |
| 174 | } | ||
| 175 | |||
| 176 | std::unique_ptr<Texture2d> texture; | ||
| 177 | CharToGlyphMap char_to_glyph; | ||
| 178 | core::KerningMap kernings; | ||
| 179 | std::map<std::string, int> private_use_aliases; | ||
| 180 | |||
| 181 | explicit DrawableFont | ||
| 182 | ( | ||
| 183 | const MemoryChunk& font_file | ||
| 184 | ); | ||
| 185 | |||
| 186 | // todo(Gustav): expose background property and move this away from font | ||
| 187 | void | ||
| 188 | draw_background | ||
| 189 | ( | ||
| 190 | SpriteBatch* renderer, | ||
| 191 | float alpha, | ||
| 192 | const Rect& where | ||
| 193 | ) const; | ||
| 194 | |||
| 195 | [[nodiscard]] ListOfTextDrawCommands compile_list | ||
| 196 | (const std::string& text, float size) const; | ||
| 197 | }; | ||
| 198 | } | ||
| 199 |