Euphoria
font.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <map>
5 #include <memory>
6 
7 #include "core/loadedfont.h"
8 #include "base/vec2.h"
9 #include "base/vec3.h"
10 #include "base/rgb.h"
11 #include "base/rect.h"
12 
13 #include "core/ui_text.h"
14 
15 #include "render/texture.h"
16 
17 
18 namespace eu::io
19 {
20  struct FileSystem;
21  struct FilePath;
22 }
23 
24 
25 namespace eu::render
26 {
27  struct SpriteRenderer;
28  struct TextureCache;
29 
30  // todo(Gustav): separate rendering and the rest and move to core
31 
32  struct Glyph
33  {
34  Rectf sprite_rect; // relative to 0,0
35  Rectf texture_rect; // image texture uvs
36  int code_point; // the character or string id
37  float advance;
38 
39  Glyph
40  (
41  const Rectf& sprite,
42  const Rectf& texture,
43  int ch,
44  float ad
45  );
46  };
47 
48  using CharToGlyphMap = std::map<int, std::shared_ptr<Glyph>>;
49 
50  struct DrawableFont;
51 
52  enum class Align
53  {
54  top_left,
55  top_center,
56  top_right,
63  };
64 
65 
67  {
68  const Texture2* texture;
71  bool hi;
72 
74  (
75  const Texture2* texture,
76  const Rectf& sprite_rect,
77  const Rectf& texture_rect,
78  bool hi
79  );
80  };
81 
82 
84  {
85  std::vector<TextDrawCommand> commands;
86 
87  void
88  add
89  (
90  const Texture2* texture,
91  const Rectf& sprite_rect,
92  const Rectf& texture_rect,
93  bool hi
94  );
95 
96  void
97  draw
98  (
99  SpriteRenderer* renderer,
100  const vec2f& start_position,
101  const Rgb& base_color,
102  const Rgb& hi_color
103  );
104 
105  [[nodiscard]] Rectf
106  get_extents() const;
107  };
108 
109 
111  {
112  public:
113  explicit DrawableText(DrawableFont* the_font);
115 
116  DrawableText(const DrawableText& other) = delete;
117  void operator=(const DrawableText&) = delete;
118  DrawableText(DrawableText&& other) = delete;
119  void operator=(DrawableText&&) = delete;
120 
121  void
122  set_text(const core::UiText& new_text);
123 
124  void
125  set_background(bool new_use_background, float new_alpha = 0.5f);
126 
127  void
128  set_alignment(Align new_alignment);
129 
130  void
131  set_size(float new_size);
132 
133  void
134  draw
135  (
136  SpriteRenderer* renderer,
137  const vec2f& p,
138  const Rgb& base_hi_color
139  ) const;
140 
141  void
142  draw
143  (
144  SpriteRenderer* renderer,
145  const vec2f& p,
146  const Rgb& base_color,
147  const Rgb& hi_color
148  ) const;
149 
150  Rectf
151  get_extents() const;
152 
153  void
154  compile() const;
155 
156  private:
157  const DrawableFont* font;
158  float size;
159  core::UiText text;
160  Align alignment;
161 
162  bool use_background;
163  float background_alpha;
164 
165  // updated in Compile function
166  mutable bool is_dirty;
167  mutable ListOfTextDrawCommands commands;
168  };
169 
170 
171  struct UiTextCompileVisitor;
172 
173 
175  {
176  float line_height = 1;
177  std::unique_ptr<Texture2> texture;
178  std::shared_ptr<Texture2> background;
181  std::map<std::string, int> private_use_aliases;
182 
184  (
185  io::FileSystem* fs,
186  TextureCache* cache,
187  const io::FilePath& font_file
188  );
189 
190  // todo(Gustav): expose background property and move this away from font
191  void
193  (
194  SpriteRenderer* renderer,
195  float alpha,
196  const Rectf& where
197  ) const;
198 
200  (const core::UiText& text, float size) const;
201  };
202 }
std::map< std::pair< int, int >, float > KerningMap
Definition: loadedfont.h:47
Definition: enum.h:8
std::map< int, std::shared_ptr< Glyph > > CharToGlyphMap
Definition: font.h:48
int ch
Definition: nlp_sentence.cc:92
Definition: rect.h:27
Definition: rgb.h:62
Represents displayed text.
Definition: ui_text.h:107
core::KerningMap kernings
Definition: font.h:180
std::shared_ptr< Texture2 > background
Definition: font.h:178
CharToGlyphMap char_to_glyph
Definition: font.h:179
std::map< std::string, int > private_use_aliases
Definition: font.h:181
std::unique_ptr< Texture2 > texture
Definition: font.h:177
ListOfTextDrawCommands compile_list(const core::UiText &text, float size) const
Definition: font.cc:489
void draw_background(SpriteRenderer *renderer, float alpha, const Rectf &where) const
Definition: font.cc:301
DrawableFont(io::FileSystem *fs, TextureCache *cache, const io::FilePath &font_file)
Definition: font.cc:126
void set_text(const core::UiText &new_text)
Definition: font.cc:527
DrawableText(DrawableFont *the_font)
Definition: font.cc:512
void operator=(DrawableText &&)=delete
Rectf get_extents() const
Definition: font.cc:641
DrawableText(DrawableText &&other)=delete
void set_size(float new_size)
Definition: font.cc:550
void operator=(const DrawableText &)=delete
void set_background(bool new_use_background, float new_alpha=0.5f)
Definition: font.cc:535
void draw(SpriteRenderer *renderer, const vec2f &p, const Rgb &base_hi_color) const
Definition: font.cc:587
DrawableText(const DrawableText &other)=delete
void set_alignment(Align new_alignment)
Definition: font.cc:543
void compile() const
Definition: font.cc:630
Rectf sprite_rect
Definition: font.h:34
float advance
Definition: font.h:37
Glyph(const Rectf &sprite, const Rectf &texture, int ch, float ad)
Definition: font.cc:34
Rectf texture_rect
Definition: font.h:35
int code_point
Definition: font.h:36
void draw(SpriteRenderer *renderer, const vec2f &start_position, const Rgb &base_color, const Rgb &hi_color)
Definition: font.cc:349
std::vector< TextDrawCommand > commands
Definition: font.h:85
void add(const Texture2 *texture, const Rectf &sprite_rect, const Rectf &texture_rect, bool hi)
Definition: font.cc:336
const Texture2 * texture
Definition: font.h:68
TextDrawCommand(const Texture2 *texture, const Rectf &sprite_rect, const Rectf &texture_rect, bool hi)
Definition: font.cc:320
Definition: vec2.h:33