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 "../units/units.hpp"
12#include "../macros.hpp"
13
14hi_export_module(hikogui.text.text_shaper_char);
15
16hi_export namespace hi::inline v1 {
17
19public:
22 hi::grapheme grapheme;
23
26 hi::text_style style;
27
30 pixel_density pixel_density;
31
42 hi::font_book::font_glyphs_type glyphs;
43
48 hi::glyph_metrics metrics;
49
52 size_t line_nr;
53
56 size_t column_nr;
57
65
86 aarectangle rectangle;
87
90 unicode_general_category general_category;
91
96 unicode_bidi_class direction;
97
104 iso_15924 script;
105
109
115 float width = 0.0f;
116
119 bool is_trailing_white_space = false;
120
127 bool glyph_is_initial = false;
128
129 [[nodiscard]] text_shaper_char(hi::grapheme const& grapheme, text_style const& style, hi::pixel_density pixel_density) noexcept :
131 style(style),
132 pixel_density(pixel_density),
135 general_category(ucd_get_general_category(grapheme.starter()))
136 {
137 }
138
144 void initialize_glyph(hi::font const& font) noexcept
145 {
146 if (not glyph_is_initial) {
147 set_glyph(find_glyph(font, grapheme));
148
149 width = metrics.advance;
150 glyph_is_initial = true;
151 }
152 }
153
159 void initialize_glyph() noexcept
160 {
161 return initialize_glyph(find_font(style->family_id, style->variant));
162 }
163
172 void replace_glyph(char32_t code_point) noexcept
173 {
174 hi_axiom_not_null(glyphs.font);
175 auto const& font = *glyphs.font;
176 set_glyph(font_book::font_glyphs_type{font, font.find_glyph(code_point)});
177
178 glyph_is_initial = false;
179 }
180
183 [[nodiscard]] font_metrics_px font_metrics() const noexcept
184 {
185 hi_axiom_not_null(glyphs.font);
186 return font_size * glyphs.font->metrics;
187 }
188
189 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char32_t const& rhs) noexcept
190 {
191 return lhs.grapheme == rhs;
192 }
193
194 [[nodiscard]] friend bool operator==(text_shaper_char const& lhs, char const& rhs) noexcept
195 {
196 return lhs.grapheme == rhs;
197 }
198
199private:
202 void set_glyph(hi::font_book::font_glyphs_type&& new_glyphs) noexcept
203 {
204 glyphs = std::move(new_glyphs);
205 hi_axiom_not_null(glyphs.font);
206 font_size = round(style->size * pixel_density, glyphs.get_font_metrics().x_height);
207 metrics = font_size.in(pixels_per_em) * glyphs.get_starter_metrics();
208 }
209};
210
211} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
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:415
hi_export auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
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:87
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:18
size_t line_nr
The line number where this character is located, counting from top to bottom line.
Definition text_shaper_char.hpp:52
unicode_bidi_class direction
The text direction for this glyph.
Definition text_shaper_char.hpp:96
hi::grapheme grapheme
The grapheme.
Definition text_shaper_char.hpp:22
void initialize_glyph() noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:159
hi::font_book::font_glyphs_type glyphs
The glyph representing one or more graphemes.
Definition text_shaper_char.hpp:42
pixel_density pixel_density
The scale to resize the font's size to match the physical display.
Definition text_shaper_char.hpp:30
au::Quantity< PixelsPerEm, float > font_size
The font size in pixels, rounded so that the x-height is rounded to the nearest pixel.
Definition text_shaper_char.hpp:108
point2 position
Position of the character.
Definition text_shaper_char.hpp:64
hi::glyph_metrics metrics
The glyph metrics of the current starter glyph.
Definition text_shaper_char.hpp:48
font_metrics_px font_metrics() const noexcept
Get the scaled font metrics for this character.
Definition text_shaper_char.hpp:183
iso_15924 script
The script of this character.
Definition text_shaper_char.hpp:104
unicode_general_category general_category
The general category of this grapheme.
Definition text_shaper_char.hpp:90
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:56
aarectangle rectangle
The rectangle for this character.
Definition text_shaper_char.hpp:86
hi::text_style style
The style of how to display the grapheme.
Definition text_shaper_char.hpp:26
void replace_glyph(char32_t code_point) noexcept
Called by the bidi-algorithm to mirror glyphs.
Definition text_shaper_char.hpp:172
void initialize_glyph(hi::font const &font) noexcept
Initialize the glyph based on the grapheme.
Definition text_shaper_char.hpp:144
Definition text_style.hpp:183
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:167
Definition pixel_density.hpp:16
Definition au.hh:3518
T move(T... args)
T round(T... args)