Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_font_painter.hpp
1#pragma once
2
3#include "structure/graphics/painter/spk_mesh_painter.hpp"
4#include "structure/graphics/spk_color.hpp"
5#include "structure/graphics/spk_font.hpp"
6
7#include "structure/geometry/spk_mesh.hpp"
8#include "structure/opengl/spk_vertex_buffer_object.hpp"
9
10#include "type/spk_text_aligmnent.hpp"
11
12#include <algorithm>
13#include <cstddef>
14#include <limits>
15#include <string>
16#include <utility>
17#include <vector>
18
19namespace spk
20{
30 {
34 spk::Vector2Int pos;
38 spk::Vector2 uv;
39 };
40
50 struct FontMesh : public spk::IMesh<FontVertex>
51 {
52 public:
63 class Builder
64 {
65 private:
66 Font *_font = nullptr;
67 Font::Size _fontSize{};
68 size_t _tabSize = 4;
69
70 spk::Vector2Int _cursor{0, 0};
71 wchar_t _previousGlyph = 0;
72 int _lineHeight = 0;
73
74 struct Bounds
75 {
76 spk::Vector2Int min{std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};
77 spk::Vector2Int max{std::numeric_limits<int>::min(), std::numeric_limits<int>::min()};
78 bool has = false;
79
80 void include(const spk::Vector2Int &p_point)
81 {
82 if (!has)
83 {
84 min = max = p_point;
85 has = true;
86 return;
87 }
88
89 min.x = std::min(min.x, p_point.x);
90 min.y = std::min(min.y, p_point.y);
91 max.x = std::max(max.x, p_point.x);
92 max.y = std::max(max.y, p_point.y);
93 }
94
95 spk::Vector2Int size() const
96 {
97 return max - min;
98 }
99 };
100
101 Bounds _bounds;
102
103 using Placement = std::pair<const Font::Bitmap::Glyph *, spk::Vector2Int>;
104 std::vector<Placement> _placements;
105 std::vector<size_t> _lineBreaks{0};
106
107 void _validateSingleLine(const std::wstring &p_text) const;
108
109 int _ensureLineHeight();
110
111 void _newline();
112
113 spk::Vector2Int _computeOffset(const Bounds &p_bounds, const spk::Vector2Int &p_anchor, spk::HorizontalAlignment p_horizontalAlignment, spk::VerticalAlignment p_verticalAlignment) const;
114
115 struct LayoutResult
116 {
117 std::vector<Placement> placements;
118 Bounds bounds;
119 };
120
121 LayoutResult _layout(const std::wstring &p_text, Font::Bitmap &p_bitmap, int p_lineHeight, bool p_buildPlacements) const;
122
123 int _glyphAdvance(Font::Bitmap &p_bitmap, const Font::Bitmap::Glyph &p_glyph, wchar_t p_previousGlyph, wchar_t p_currentGlyph) const;
124
125 public:
129 Builder() = default;
130
137 Builder(Font *p_font, const Font::Size &p_fontSize, size_t p_tabSize = 4);
138
144 Builder &setTabSize(size_t p_tabSize);
145
150 Builder &clear();
151
157 Builder &addText(const std::wstring &p_text);
158
164
170 spk::Vector2UInt computeTextSize(const std::wstring &p_text) const;
171
179 FontMesh construct(const spk::Vector2Int &p_anchor, spk::HorizontalAlignment p_horizontalAlignment = spk::HorizontalAlignment::Centered, spk::VerticalAlignment p_verticalAlignment = spk::VerticalAlignment::Centered);
180 };
181
182 private:
183 Font *_font = nullptr;
184 Font::Size _fontSize = {16, 0};
185
186 public:
190 FontMesh() = default;
191
197 FontMesh(Font *p_font, const Font::Size &p_fontSize);
198
203 const Font *font() const;
204
210
215 const Font::Bitmap &bitmap() const;
216
221 const Font::Size &fontSize() const;
222
223 protected:
227 void _configureBufferSet() const override
228 {
229 auto &layout = bufferSet()->vbo().layout();
231 layout.addAttribute({0, Attribute::Type::Vector2Int});
232 layout.addAttribute({1, Attribute::Type::Vector2});
233 }
234 };
235
247 class FontPainter : public spk::MeshPainter<FontMesh>
248 {
249 private:
250 static spk::Lumina::Pipeline &_fontPipeline();
251 OpenGL::UBO &_modelData;
252 float &_layerRef;
253 OpenGL::UBO &_styleUBO;
254 spk::Color &_glyphColorRef;
255 spk::Color &_outlineColorRef;
256
257 public:
261 FontPainter();
262
267 void setLayer(const float &p_layer);
272 const float &layer() const;
273
278 void setMesh(const FontMesh *p_mesh) override;
279
284 void setGlyphColor(const spk::Color &p_color);
289 void setOutlineColor(const spk::Color &p_color);
295 void setColors(const spk::Color &p_glyphColor, const spk::Color &p_outlineColor);
296
301 const spk::Color &glyphColor() const;
306 const spk::Color &outlineColor() const;
307 };
308}
Font atlas texture and glyph metadata for a specific size.
Definition spk_font.hpp:78
Loads font data and provides cached bitmap atlases per size.
Definition spk_font.hpp:33
spk::Vector2UInt computeTextSize(const std::wstring &p_text) const
Computes the pixel size for a text string.
Definition spk_font_painter.cpp:218
FontMesh construct(const spk::Vector2Int &p_anchor, spk::HorizontalAlignment p_horizontalAlignment=spk::HorizontalAlignment::Centered, spk::VerticalAlignment p_verticalAlignment=spk::VerticalAlignment::Centered)
Builds a FontMesh with alignment around an anchor.
Definition spk_font_painter.cpp:236
Builder()=default
Builds an empty builder.
Builder & setTabSize(size_t p_tabSize)
Sets the tab size used for layout.
Definition spk_font_painter.cpp:145
Builder & addNewline()
Inserts a line break in the layout buffer.
Definition spk_font_painter.cpp:212
Builder & addText(const std::wstring &p_text)
Appends text to the layout buffer.
Definition spk_font_painter.cpp:162
Builder & clear()
Clears any accumulated text and layout state.
Definition spk_font_painter.cpp:151
void setLayer(const float &p_layer)
Sets the layer value used during rendering.
Definition spk_font_painter.cpp:359
void setColors(const spk::Color &p_glyphColor, const spk::Color &p_outlineColor)
Sets glyph and outline colors together.
Definition spk_font_painter.cpp:407
const float & layer() const
Returns the current layer value.
Definition spk_font_painter.cpp:370
const spk::Color & outlineColor() const
Returns the current outline color.
Definition spk_font_painter.cpp:420
const spk::Color & glyphColor() const
Returns the current glyph color.
Definition spk_font_painter.cpp:415
void setMesh(const FontMesh *p_mesh) override
Sets the font mesh to render.
Definition spk_font_painter.cpp:375
void setGlyphColor(const spk::Color &p_color)
Sets the glyph fill color.
Definition spk_font_painter.cpp:395
FontPainter()
Builds a font painter with its rendering pipeline.
Definition spk_font_painter.cpp:348
void setOutlineColor(const spk::Color &p_color)
Sets the glyph outline color.
Definition spk_font_painter.cpp:401
Stores polygon data and exposes GPU-ready vertex/index buffers.
Definition spk_mesh.hpp:29
const std::shared_ptr< spk::OpenGL::BufferSetObject > & bufferSet() const
Definition spk_mesh.hpp:338
Loads shader artifacts and orchestrates draw calls with strongly typed constants and attributes.
Definition spk_pipeline.hpp:38
Base painter that renders a mesh through a Lumina pipeline.
Definition spk_mesh_painter.hpp:24
VertexBufferObject & vbo()
Access mutable VBO.
Definition spk_buffer_set_object.cpp:76
Layout & layout()
Access layout descriptor.
Definition spk_vertex_buffer_object.cpp:253
RGBA color utility storing normalized floating components.
Definition spk_color.hpp:18
Glyph quad data and layout metrics.
Definition spk_font.hpp:119
Defines glyph and outline sizes for a font bitmap.
Definition spk_font.hpp:40
Mesh specialization for font glyphs.
Definition spk_font_painter.hpp:51
void _configureBufferSet() const override
Configures the vertex buffer layout for font vertices.
Definition spk_font_painter.hpp:227
FontMesh()=default
Builds an empty font mesh.
const Font::Size & fontSize() const
Returns the configured font size.
Definition spk_font_painter.cpp:318
Font::Bitmap & bitmap()
Returns the bitmap associated with the font.
Definition spk_font_painter.cpp:308
const Font * font() const
Returns the font used by this mesh.
Definition spk_font_painter.cpp:303
Vertex containing a position and UV coordinates for font rendering.
Definition spk_font_painter.hpp:30
spk::Vector2 uv
Texture coordinates.
Definition spk_font_painter.hpp:38
spk::Vector2Int pos
Vertex position in integer coordinates.
Definition spk_font_painter.hpp:34
TType x
X component.
Definition spk_vector2.hpp:44
TType y
Y component.
Definition spk_vector2.hpp:48
Describes a vertex attribute entry in the layout.
Definition spk_vertex_buffer_object.hpp:38