HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
AttributedGlyph.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Text/FontGlyphIDs.hpp"
7#include "TTauri/Text/AttributedGrapheme.hpp"
8#include "TTauri/Text/GlyphMetrics.hpp"
9#include "TTauri/Foundation/mat.hpp"
10
11namespace tt {
12
16 FontGlyphIDs glyphs;
17
21
24
28
31
32 GeneralCharacterClass charClass;
33
36
47 AttributedGlyph(AttributedGrapheme const &attr_grapheme, AttributedGlyph const *next_attr_glyph=nullptr) noexcept;
48
49 AttributedGlyph(AttributedGlyph const &other) = default;
50 AttributedGlyph(AttributedGlyph &&other) noexcept = default;
51 AttributedGlyph &operator=(AttributedGlyph const &other) = default;
52 AttributedGlyph &operator=(AttributedGlyph &&other) noexcept = default;
53 ~AttributedGlyph() = default;
54
57 [[nodiscard]] bool containsLogicalIndex(ssize_t index) const noexcept {
58 ttlet first = logicalIndex;
59 ttlet last = first + graphemeCount;
60 return index >= first && index < last;
61 }
62
63 [[nodiscard]] bool isLetter() const noexcept { return charClass == GeneralCharacterClass::Letter; }
64 [[nodiscard]] bool isDigit() const noexcept { return charClass == GeneralCharacterClass::Digit; }
65 [[nodiscard]] bool isWord() const noexcept { return isLetter() || isDigit(); }
66 [[nodiscard]] bool isWhiteSpace() const noexcept { return charClass == GeneralCharacterClass::WhiteSpace; }
67 [[nodiscard]] bool isParagraphSeparator() const noexcept { return charClass == GeneralCharacterClass::ParagraphSeparator; }
68 [[nodiscard]] bool isVisible() const noexcept { return isWord() || charClass == GeneralCharacterClass::Unknown; }
69
77 [[nodiscard]] int selectionWordClusterID() const noexcept {
78 switch (charClass) {
79 case GeneralCharacterClass::ParagraphSeparator: return 0;
80 case GeneralCharacterClass::Digit:
81 case GeneralCharacterClass::Letter: return 1;
82 case GeneralCharacterClass::Unknown: return 2;
83 case GeneralCharacterClass::WhiteSpace: return 3;
84 default: tt_no_default;
85 }
86 }
87
92 [[nodiscard]] aarect boundingBox(float border) const noexcept {
93 return mat::T2(position.xyz0()) * expand(metrics.boundingBox, border * style.scaled_size());
94 }
95
100 [[nodiscard]] ssize_t relativeIndexAtCoordinate(vec coordinate) const noexcept {
101 ttlet relativePositionInGlyph = (coordinate.x() - position.x()) / metrics.advance.x();
102 ttlet relativePositionPerGrapheme = relativePositionInGlyph * numeric_cast<float>(graphemeCount);
103 return numeric_cast<ssize_t>(std::round(relativePositionPerGrapheme));
104 }
105
106 [[nodiscard]] Path get_path() const noexcept;
107};
108
109}
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:13
Optimized 2D translate matrix.
Definition mat.hpp:184
Definition Path.hpp:29
A 4D vector.
Definition vec.hpp:37
Definition AttributedGlyph.hpp:15
TextStyle style
Copied from the original attributed-grapheme.
Definition AttributedGlyph.hpp:35
bool containsLogicalIndex(ssize_t index) const noexcept
Check if this glyph contains the grapheme at index.
Definition AttributedGlyph.hpp:57
ssize_t logicalIndex
The logical index of the grapheme before bidi-algorithm.
Definition AttributedGlyph.hpp:20
ssize_t relativeIndexAtCoordinate(vec coordinate) const noexcept
Find the logical index closest to the coordinate.
Definition AttributedGlyph.hpp:100
int selectionWordClusterID() const noexcept
return a cluster id for word selection.
Definition AttributedGlyph.hpp:77
GlyphMetrics metrics
Metrics taken from the font file, pre-scaled to the font-size.
Definition AttributedGlyph.hpp:23
vec position
Position of the glyph.
Definition AttributedGlyph.hpp:27
int8_t graphemeCount
Number of graphemes merged (ligature) into this attributed-glyph.
Definition AttributedGlyph.hpp:30
AttributedGlyph(AttributedGrapheme const &attr_grapheme, AttributedGlyph const *next_attr_glyph=nullptr) noexcept
Constructed an attributed glyph from an attributed grapheme.
aarect boundingBox(float border) const noexcept
Get the bounding box for this glyph.
Definition AttributedGlyph.hpp:92
Definition AttributedGrapheme.hpp:12
Definition FontGlyphIDs.hpp:77
Definition GlyphMetrics.hpp:17
vec advance
Definition GlyphMetrics.hpp:58
aarect boundingBox
Definition GlyphMetrics.hpp:20
Definition TextStyle.hpp:15
T round(T... args)