HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_shaper_char.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "text_style.hpp"
8#include "../font/font.hpp"
9#include "../unicode/unicode.hpp"
10#include "../geometry/module.hpp"
11#include "../macros.hpp"
12
13namespace hi::inline v1 {
14
16public:
19 hi::grapheme grapheme;
20
23 hi::text_style style;
24
27 float dpi_scale = 1.0f;
28
39 hi::font_book::font_glyphs_type glyphs;
40
45 hi::glyph_metrics metrics;
46
49 size_t line_nr;
50
53 size_t column_nr;
54
62
83 aarectangle rectangle;
84
87 unicode_general_category general_category;
88
93 unicode_bidi_class direction;
94
101 iso_15924 script;
102
105 float scale = 1.0f;
106
112 float width = 0.0f;
113
116 bool is_trailing_white_space = false;
117
124 bool glyph_is_initial = false;
125
126 [[nodiscard]] text_shaper_char(hi::grapheme const& grapheme, text_style const& style, float dpi_scale) noexcept :
128 style(style),
129 dpi_scale(dpi_scale),
132 general_category(ucd_get_general_category(grapheme.starter()))
133 {
134 }
135
141 void initialize_glyph(hi::font const& font) noexcept
142 {
143 if (not glyph_is_initial) {
144 set_glyph(find_glyph(font, grapheme));
145
146 width = metrics.advance;
147 glyph_is_initial = true;
148 }
149 }
150
156 void initialize_glyph() noexcept
157 {
158 return initialize_glyph(find_font(style->family_id, style->variant));
159 }
160
169 void replace_glyph(char32_t code_point) noexcept
170 {
171 hi_axiom_not_null(glyphs.font);
172 hilet& font = *glyphs.font;
173 set_glyph(font_book::font_glyphs_type{font, font.find_glyph(code_point)});
174
175 glyph_is_initial = false;
176 }
177
180 [[nodiscard]] hi::font_metrics font_metrics() const noexcept
181 {
182 hi_axiom_not_null(glyphs.font);
183 return scale * glyphs.font->metrics;
184 }
185
186 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char32_t const& rhs) noexcept
187 {
188 return lhs.grapheme == rhs;
189 }
190
191 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char const& rhs) noexcept
192 {
193 return lhs.grapheme == rhs;
194 }
195
196private:
199 void set_glyph(hi::font_book::font_glyphs_type&& new_glyphs) noexcept
200 {
201 glyphs = std::move(new_glyphs);
202 hi_axiom_not_null(glyphs.font);
203 scale = glyphs.get_font_metrics().round_scale(dpi_scale * style->size);
204 metrics = scale * glyphs.get_starter_metrics();
205 }
206};
207
208} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
hi_export font const & find_font(font_family_id family_id, font_variant variant=font_variant{}) noexcept
Find a font closest to the variant.
Definition font_book.hpp:412
hi_export auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:437
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition font_book.hpp:62
Definition font_font.hpp:31
glyph_id find_glyph(char32_t c) const noexcept
Get the glyph for a code-point.
Definition font_font.hpp:89
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point2.hpp:26
Definition text_shaper_char.hpp:15
size_t line_nr
The line number where this character is located, counting from top to bottom line.
Definition text_shaper_char.hpp:49
unicode_bidi_class direction
The text direction for this glyph.
Definition text_shaper_char.hpp:93
hi::font_metrics font_metrics() const noexcept
Get the scaled font metrics for this character.
Definition text_shaper_char.hpp:180
hi::grapheme grapheme
The grapheme.
Definition text_shaper_char.hpp:19
void initialize_glyph() noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:156
hi::font_book::font_glyphs_type glyphs
The glyph representing one or more graphemes.
Definition text_shaper_char.hpp:39
point2 position
Position of the character.
Definition text_shaper_char.hpp:61
hi::glyph_metrics metrics
The glyph metrics of the current starter glyph.
Definition text_shaper_char.hpp:45
iso_15924 script
The script of this character.
Definition text_shaper_char.hpp:101
unicode_general_category general_category
The general category of this grapheme.
Definition text_shaper_char.hpp:87
size_t column_nr
The column number where the character is located on the line, counting from left to right in display ...
Definition text_shaper_char.hpp:53
aarectangle rectangle
The rectangle for this character.
Definition text_shaper_char.hpp:83
hi::text_style style
The style of how to display the grapheme.
Definition text_shaper_char.hpp:23
void replace_glyph(char32_t code_point) noexcept
Called by the bidi-algorithm to mirror glyphs.
Definition text_shaper_char.hpp:169
void initialize_glyph(hi::font const &font) noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:141
Definition text_style.hpp:181
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:160
T move(T... args)