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
51 tt::font_book const &font_book, attributed_grapheme const &attr_grapheme,
52 attributed_glyph const *next_attr_glyph = nullptr) noexcept;
53
54 attributed_glyph(attributed_glyph const &other) = default;
55 attributed_glyph(attributed_glyph &&other) noexcept = default;
56 attributed_glyph &operator=(attributed_glyph const &other) = default;
57 attributed_glyph &operator=(attributed_glyph &&other) noexcept = default;
58 ~attributed_glyph() = default;
59
62 [[nodiscard]] bool containsLogicalIndex(ssize_t index) const noexcept {
63 ttlet first = logicalIndex;
64 ttlet last = first + graphemeCount;
65 return index >= first && index < last;
66 }
67
68 [[nodiscard]] bool isLetter() const noexcept
69 {
70 return is_L(general_category);
71 }
72
73 [[nodiscard]] bool isDigit() const noexcept
74 {
75 return is_N(general_category);
76 }
77
78 [[nodiscard]] bool isIdentifier() const noexcept { return isLetter() || isDigit(); }
79
80 [[nodiscard]] bool isWhiteSpace() const noexcept
81 {
82 return general_category == unicode_general_category::Zs || general_category == unicode_general_category::Zl;
83 }
84
85 [[nodiscard]] bool isParagraphSeparator() const noexcept
86 {
87 return general_category == unicode_general_category::Zp;
88 }
89 [[nodiscard]] bool isVisible() const noexcept
90 {
91 return is_visible(general_category);
92 }
93
101 [[nodiscard]] int selectionWordClusterID() const noexcept {
102 if (isParagraphSeparator()) {
103 return 0;
104 } else if (isIdentifier()) {
105 return 1;
106 } else if (isVisible()) {
107 return 2;
108 } else {
109 return 3;
110 }
111 }
112
117 [[nodiscard]] aarectangle boundingBox(float border) const noexcept {
118 return translate2{position} * expand(metrics.boundingBox, border * style.scaled_size());
119 }
120
125 [[nodiscard]] ssize_t relativeIndexAtCoordinate(point2 coordinate) const noexcept {
126 ttlet relativePositionInGlyph = (coordinate.x() - position.x()) / metrics.advance.x();
127 ttlet relativePositionPergrapheme = relativePositionInGlyph * narrow_cast<float>(graphemeCount);
128 return narrow_cast<ssize_t>(std::round(relativePositionPergrapheme));
129 }
130
131 [[nodiscard]] graphic_path get_path() const noexcept;
132};
133
134}
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:125
int selectionWordClusterID() const noexcept
return a cluster id for word selection.
Definition attributed_glyph.hpp:101
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
attributed_glyph(tt::font_book const &font_book, attributed_grapheme const &attr_grapheme, attributed_glyph const *next_attr_glyph=nullptr) noexcept
Constructed an attributed glyph from an attributed grapheme.
aarectangle boundingBox(float border) const noexcept
Get the bounding box for this glyph.
Definition attributed_glyph.hpp:117
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:62
Definition attributed_grapheme.hpp:14
font_book keeps track of multiple fonts.
Definition font_book.hpp:30
Definition font_glyph_ids.hpp:80
Definition glyph_metrics.hpp:17
aarectangle boundingBox
Definition glyph_metrics.hpp:20
vector2 advance
Definition glyph_metrics.hpp:58
Definition text_style.hpp:17
T round(T... args)