10#include <smk/RenderTarget.hpp>
11#include <smk/Text.hpp>
12#include <smk/VertexArray.hpp>
21bool EatCodePoint(
const std::string& input,
25 if (start >= input.size()) {
29 const uint8_t C0 = input[start];
32 if ((C0 & 0b1000'0000) == 0b0000'0000) {
33 *ucs = C0 & 0b0111'1111;
39 if ((C0 & 0b1110'0000) == 0b1100'0000 &&
40 start + 1 < input.size()) {
41 const uint8_t C1 = input[start + 1];
43 *ucs += C0 & 0b0001'1111;
45 *ucs += C1 & 0b0011'1111;
51 if ((C0 & 0b1111'0000) == 0b1110'0000 &&
52 start + 2 < input.size()) {
53 const uint8_t C1 = input[start + 1];
54 const uint8_t C2 = input[start + 2];
56 *ucs += C0 & 0b0000'1111;
58 *ucs += C1 & 0b0011'1111;
60 *ucs += C2 & 0b0011'1111;
66 if ((C0 & 0b1111'1000) == 0b1111'0000 &&
67 start + 3 < input.size()) {
68 const uint8_t C1 = input[start + 1];
69 const uint8_t C2 = input[start + 2];
70 const uint8_t C3 = input[start + 3];
72 *ucs += C0 & 0b0000'0111;
74 *ucs += C1 & 0b0011'1111;
76 *ucs += C2 & 0b0011'1111;
78 *ucs += C3 & 0b0011'1111;
89std::wstring to_wstring(
const std::string& s) {
93 uint32_t codepoint = 0;
94 while (EatCodePoint(s, i, &i, &codepoint)) {
96 if constexpr (
sizeof(wchar_t) == 4) {
97 out.push_back(codepoint);
105 if (codepoint < 0xD800 || (codepoint > 0xDFFF && codepoint < 0x10000)) {
106 uint16_t p0 = codepoint;
112 codepoint -= 0x010000;
113 uint16_t p0 = (((codepoint << 12) >> 22) + 0xD800);
114 uint16_t p1 = (((codepoint << 22) >> 22) + 0xDC00);
146 string_ = wide_string;
151 string_ = to_wstring(
string);
161 state.
color *= color();
163 float advance_x = 0.f;
164 float advance_y = font_->baseline_position();
167 {{0.f, 0.f}, {0.f, 0.f}},
168 {{0.f, 1.f}, {0.f, 1.f}},
169 {{1.f, 1.f}, {1.f, 1.f}},
170 {{0.f, 0.f}, {0.f, 0.f}},
171 {{1.f, 1.f}, {1.f, 1.f}},
172 {{1.f, 0.f}, {1.f, 0.f}},
175 for (
const auto& it : string_) {
178 advance_y += font_->line_height();
182 auto* character = font_->FetchGlyph(it);
187 if (character->texture.id()) {
188 const float x = advance_x + float(character->bearing.x);
189 const float y = advance_y + float(character->bearing.y);
190 const float w = float(character->texture.width());
191 const float h = float(character->texture.height());
192 state.
texture = character->texture;
201 advance_x += character->advance;
208 glm::vec2 dimension(0.f, 0.f);
209 float advance_x = 0.f;
210 dimension.y += font_->line_height();
211 for (
const auto& it : string_) {
214 dimension.y += font_->line_height();
217 auto* character = font_->FetchGlyph(it);
221 advance_x += character->advance;
223 dimension.x = std::max(dimension.x, advance_x);
virtual void Draw(const Drawable &drawable)
Draw on the surface.
A class for displaying text.
glm::vec2 ComputeDimensions() const
void SetFont(Font &font)
Update the Font to be used.
void Draw(RenderTarget &target, RenderState state) const override
Draw the Text to the screen.
Text()
Construct a null Text. It can't be drawn.
void SetString(const std::wstring &wide_string)
Update the text to be drawn.
An array of smk::Vertex moved to the GPU memory. This represent a set of triangles to be drawn by the...
Contain all the data needed to draw.
glm::mat4 view
The "view" transformation.
glm::vec4 color
The masking color.
Texture texture
The texture 0 bound.
VertexArray vertex_array
The shape to to be drawn.