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/geometry.hpp"
11#include "../macros.hpp"
12
13hi_export_module(hikogui.text.text_shaper_char);
14
15hi_export namespace hi::inline v1 {
16
18public:
21 hi::grapheme grapheme;
22
25 hi::text_style style;
26
29 float dpi_scale = 1.0f;
30
41 hi::font_book::font_glyphs_type glyphs;
42
47 hi::glyph_metrics metrics;
48
51 size_t line_nr;
52
55 size_t column_nr;
56
64
85 aarectangle rectangle;
86
89 unicode_general_category general_category;
90
95 unicode_bidi_class direction;
96
103 iso_15924 script;
104
107 float scale = 1.0f;
108
114 float width = 0.0f;
115
118 bool is_trailing_white_space = false;
119
126 bool glyph_is_initial = false;
127
128 [[nodiscard]] text_shaper_char(hi::grapheme const& grapheme, text_style const& style, float dpi_scale) noexcept :
130 style(style),
131 dpi_scale(dpi_scale),
134 general_category(ucd_get_general_category(grapheme.starter()))
135 {
136 }
137
143 void initialize_glyph(hi::font const& font) noexcept
144 {
145 if (not glyph_is_initial) {
146 set_glyph(find_glyph(font, grapheme));
147
148 width = metrics.advance;
149 glyph_is_initial = true;
150 }
151 }
152
158 void initialize_glyph() noexcept
159 {
160 return initialize_glyph(find_font(style->family_id, style->variant));
161 }
162
171 void replace_glyph(char32_t code_point) noexcept
172 {
173 hi_axiom_not_null(glyphs.font);
174 auto const& font = *glyphs.font;
175 set_glyph(font_book::font_glyphs_type{font, font.find_glyph(code_point)});
176
177 glyph_is_initial = false;
178 }
179
182 [[nodiscard]] hi::font_metrics font_metrics() const noexcept
183 {
184 hi_axiom_not_null(glyphs.font);
185 return scale * glyphs.font->metrics;
186 }
187
188 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char32_t const& rhs) noexcept
189 {
190 return lhs.grapheme == rhs;
191 }
192
193 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char const& rhs) noexcept
194 {
195 return lhs.grapheme == rhs;
196 }
197
198private:
201 void set_glyph(hi::font_book::font_glyphs_type&& new_glyphs) noexcept
202 {
203 glyphs = std::move(new_glyphs);
204 hi_axiom_not_null(glyphs.font);
205 scale = glyphs.get_font_metrics().round_scale(dpi_scale * style->size);
206 metrics = scale * glyphs.get_starter_metrics();
207 }
208};
209
210} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_export hi_inline 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:415
hi_export hi_inline auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
Definition font_book.hpp:61
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:28
Definition text_shaper_char.hpp:17
size_t line_nr
The line number where this character is located, counting from top to bottom line.
Definition text_shaper_char.hpp:51
unicode_bidi_class direction
The text direction for this glyph.
Definition text_shaper_char.hpp:95
hi::font_metrics font_metrics() const noexcept
Get the scaled font metrics for this character.
Definition text_shaper_char.hpp:182
hi::grapheme grapheme
The grapheme.
Definition text_shaper_char.hpp:21
void initialize_glyph() noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:158
hi::font_book::font_glyphs_type glyphs
The glyph representing one or more graphemes.
Definition text_shaper_char.hpp:41
point2 position
Position of the character.
Definition text_shaper_char.hpp:63
hi::glyph_metrics metrics
The glyph metrics of the current starter glyph.
Definition text_shaper_char.hpp:47
iso_15924 script
The script of this character.
Definition text_shaper_char.hpp:103
unicode_general_category general_category
The general category of this grapheme.
Definition text_shaper_char.hpp:89
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:55
aarectangle rectangle
The rectangle for this character.
Definition text_shaper_char.hpp:85
hi::text_style style
The style of how to display the grapheme.
Definition text_shaper_char.hpp:25
void replace_glyph(char32_t code_point) noexcept
Called by the bidi-algorithm to mirror glyphs.
Definition text_shaper_char.hpp:171
void initialize_glyph(hi::font const &font) noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:143
Definition text_style.hpp:183
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:167
T move(T... args)