HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
attributed_glyph.hpp
1// Copyright Take Vos 2020-2021.
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 "font_glyph_ids.hpp"
8#include "attributed_grapheme.hpp"
9#include "glyph_metrics.hpp"
10#include "../graphic_path.hpp"
11#include "../geometry/translate.hpp"
12
13namespace tt {
14
18 font_glyph_ids glyphs;
19
23
26
30
33
34 unicode_general_category general_category;
35
38
49 attributed_glyph(attributed_grapheme const &attr_grapheme, attributed_glyph const *next_attr_glyph=nullptr) noexcept;
50
51 attributed_glyph(attributed_glyph const &other) = default;
52 attributed_glyph(attributed_glyph &&other) noexcept = default;
53 attributed_glyph &operator=(attributed_glyph const &other) = default;
54 attributed_glyph &operator=(attributed_glyph &&other) noexcept = default;
55 ~attributed_glyph() = default;
56
59 [[nodiscard]] bool containsLogicalIndex(ssize_t index) const noexcept {
60 ttlet first = logicalIndex;
61 ttlet last = first + graphemeCount;
62 return index >= first && index < last;
63 }
64
65 [[nodiscard]] bool isLetter() const noexcept
66 {
67 return is_L(general_category);
68 }
69
70 [[nodiscard]] bool isDigit() const noexcept
71 {
72 return is_N(general_category);
73 }
74
75 [[nodiscard]] bool isIdentifier() const noexcept { return isLetter() || isDigit(); }
76
77 [[nodiscard]] bool isWhiteSpace() const noexcept
78 {
79 return general_category == unicode_general_category::Zs || general_category == unicode_general_category::Zl;
80 }
81
82 [[nodiscard]] bool isParagraphSeparator() const noexcept
83 {
84 return general_category == unicode_general_category::Zp;
85 }
86 [[nodiscard]] bool isVisible() const noexcept
87 {
88 return is_visible(general_category);
89 }
90
98 [[nodiscard]] int selectionWordClusterID() const noexcept {
99 if (isParagraphSeparator()) {
100 return 0;
101 } else if (isIdentifier()) {
102 return 1;
103 } else if (isVisible()) {
104 return 2;
105 } else {
106 return 3;
107 }
108 }
109
114 [[nodiscard]] aarectangle boundingBox(float border) const noexcept {
115 return translate2{position} * expand(metrics.boundingBox, border * style.scaled_size());
116 }
117
122 [[nodiscard]] ssize_t relativeIndexAtCoordinate(point2 coordinate) const noexcept {
123 ttlet relativePositionInGlyph = (coordinate.x() - position.x()) / metrics.advance.x();
124 ttlet relativePositionPergrapheme = relativePositionInGlyph * narrow_cast<float>(graphemeCount);
125 return narrow_cast<ssize_t>(std::round(relativePositionPergrapheme));
126 }
127
128 [[nodiscard]] graphic_path get_path() const noexcept;
129};
130
131}
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
constexpr float & x() noexcept
Access the x element from the point.
Definition point.hpp:86
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector.hpp:83
A path is a vector graphics object.
Definition graphic_path.hpp:28
Definition attributed_glyph.hpp:17
ssize_t relativeIndexAtCoordinate(point2 coordinate) const noexcept
Find the logical index closest to the coordinate.
Definition attributed_glyph.hpp:122
attributed_glyph(attributed_grapheme const &attr_grapheme, attributed_glyph const *next_attr_glyph=nullptr) noexcept
Constructed an attributed glyph from an attributed grapheme.
int selectionWordClusterID() const noexcept
return a cluster id for word selection.
Definition attributed_glyph.hpp:98
point2 position
Position of the glyph.
Definition attributed_glyph.hpp:29
ssize_t logicalIndex
The logical index of the grapheme before bidi-algorithm.
Definition attributed_glyph.hpp:22
int8_t graphemeCount
Number of graphemes merged (ligature) into this attributed-glyph.
Definition attributed_glyph.hpp:32
text_style style
Copied from the original attributed-grapheme.
Definition attributed_glyph.hpp:37
aarectangle boundingBox(float border) const noexcept
Get the bounding box for this glyph.
Definition attributed_glyph.hpp:114
glyph_metrics metrics
Metrics taken from the font file, pre-scaled to the font-size.
Definition attributed_glyph.hpp:25
bool containsLogicalIndex(ssize_t index) const noexcept
Check if this glyph contains the grapheme at index.
Definition attributed_glyph.hpp:59
Definition attributed_grapheme.hpp:14
Definition font_glyph_ids.hpp:78
Definition glyph_metrics.hpp:17
aarectangle boundingBox
Definition glyph_metrics.hpp:20
vector2 advance
Definition glyph_metrics.hpp:58
Definition text_style.hpp:16
T round(T... args)