TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_text_system_skribidi.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <span>
10#include <unordered_map>
11#include <unordered_set>
12#include <string>
13#include <vector>
14
15struct skb_layout_t;
16struct skb_text_position_t;
17struct skb_editor_t;
18
19namespace tachyon
20{
23
25 class SkribidiContext;
26 struct RegisteredText;
27 struct EditSelection;
28
31 {
32 public:
35 explicit SkribidiTextSystem(const AllocationHooks& hooks = {});
37
38 // -------------------- Initialization --------------------
39
40 bool Initialize(GraphicsContext* graphicsContext, StyleManager* styleManager,
41 TextAtlasManager* textAtlasManager,
42 GUIStateManager* guiStateManager, IClipboard* clipboard) override;
43 void Shutdown() override;
44 void Update() override;
45 void BeginFrame(int2 screenMousePos, MouseButtons mouseButtons, uint32_t timeMs) override;
46 void BeginAtlasRecord(ID scopeId) override;
47 void EndAtlasRecord() override;
48 bool ReplayAtlasTouches(ID scopeId) override;
49
50 // -------------------- Editor Input Handling --------------------
51
52 void OnKeyboard(ID windowId, KeyCode key, int repeat, KeyModifier modifiers, KeyAction action) override;
53 void OnMouseMove(ID windowId, int2 screenPos, MouseButtons button, KeyModifier modifiers) override;
54 void OnMouseButton(ID windowId, int2 screenPos, MouseButtons button, KeyModifier modifiers, MouseAction action) override;
55 void OnMouseScroll(ID windowId, int2 screenPos, float2 scrollOffset, KeyModifier modifiers) override;
56 void OnTextComposition(ID windowId, std::string_view text, int32_t cursor) override;
57
58 void SwitchTextEditor(ID from, ID to) override;
59 void SetUndoHistory(UndoHistory* history) override { m_undoHistory = history; }
62 bool DoesTextEditorAcceptTabs(ID elementId) const override;
63 bool IsTextEditor(ID elementId) const override;
64 void RequestSelectAll(ID elementId) override;
65 TextEditUpdate ConsumeTextEditUpdate(TextID textId, std::string& outText) override;
66 bool UpdateText(TextID textId, std::string_view text) override;
67
68 std::string Copy() override;
69 std::string Cut() override;
70 void Paste(std::string_view text) override;
71
72 bool IsHoveringSelectedText(ID textEditorId, ID windowId, tachyon::int2 screenSpace) const override;
73 bool TryGetTextDragCursor(ID windowId, tachyon::int2 screenSpace, MouseCursor& outCursor) const override;
74 bool TryGetTextDragHoverTarget(ID windowId, tachyon::int2 screenSpace, ID& outTargetId) const override;
75
76 // -------------------- Cache Management --------------------
77
78 void ClearCache() override;
80 size_t RegisteredTextCount() const;
82 uint64_t LayoutsMade() const;
83
84
85
86 // -------------------- Cursor Visibility --------------------
87
88 void EnsureCursorVisible(ID textEditorId) override;
89 std::optional<IntRect> GetCursorRect(ID textEditorId) override;
90 std::optional<IntRect> GetCaretScreenRect() const override;
91
92 // -------------------- Editor Registration --------------------
93
94
95 TextID RegisterEditableText(ID elementId, std::string& text, TextEditBehavior behavior, StyleRef styleIndex, ElementState state, bool& wasModified,
96 const ITextValidator* validator = nullptr) override;
97 std::string* ValueEditText(ID elementId, bool& started) override;
98 void SetTextColor(TextID textId, VisualColorStyleIndex color) override;
100
101
102 // -------------------- Font Registration --------------------
103
104 void SetFontDirectory(std::string_view directory) override;
105 void SetFallbackFontDirectory(std::string_view directory) override;
106 void SetLanguage(std::string_view languageTag) override;
108 FontID RegisterFontFamily(std::string_view familyName, std::span<const FontFaceSource> faces) override;
109
110 // -------------------- Text Registration --------------------
111
112 TextID RegisterText(ID elementId, IconID iconId, std::string_view text, StyleRef styleIndex, ElementState state) override;
113 TextID RegisterRichText(ID elementId, const std::span<ContentRun>& contentRuns,
114 StyleRef styleIndex, ElementState state) override;
115 TextID RegisterEditableRichText(ID elementId, const std::span<ContentRun>& contentRuns,
116 TextEditBehavior behavior, StyleRef styleIndex,
117 ElementState state, bool& wasModified) override;
118 TextID GetTextID(ID elementId) const override;
119 std::string_view GetText(TextID textId) const override;
120
121 // -------------------- Text Measurement --------------------
122
124 int32_t GetTextHeightWithoutIcon(TextID textId) const override;
125 TextMeasurement MeasureText(TextID textId, const TextWrapContext& wrapContext) const override;
126
127 // -------------------- Text Rendering --------------------
128
129 void RenderText(TextID textId, IntRect rect, IntRect innerRect, IntRect clipRect, float pixelScale = 1.f,
130 const float3x2& screenToLayout = float3x2::Identity()) override;
131
132 std::optional<MouseCursor> LinkCursorAt(ID elementId, int32_t localX, int32_t localY) const override;
133 intptr_t HitTestContent(TextID textId, int32_t localX, int32_t localY) const override;
134 void SetHoveredContent(TextID textId, intptr_t contentId) override;
135
136
137 // -------------------- Icon Registration --------------------
138
139 bool IsValidIcon(IconID iconId) const override;
140
141 IconID RegisterIconFromSVG(const char* name, const char* svg_data, size_t svg_size) override;
142
143 IconID RegisterIconFromSVGFile(const char* path) override;
144 IconID RegisterIconFromPixels(const char* name, uint2 size, std::span<const uint8_t> rgba) override;
145 void UnregisterIcon(IconID iconId) override;
146
147 // -------------------- Icon Measurement --------------------
148
150 IconMeasurement MeasureIcon(IconID iconId) const override;
151
152 // -------------------- Icon Rendering --------------------
153
154 void RenderIcon(IconID iconId, IntRect rect, IntRect clipRect, VisualColorStyleIndex tint,
155 std::span<const VisualShadow> shadows = {}) override;
156
157 static void ScreenToTextEditorLocalPoint(tachyon::int2 screenSpace, const IntRect& textEditorInnerRect, tachyon::float2& outLocal,
158 const float3x2& screenToLayout = float3x2::Identity());
159
160 private:
161 void UpdateGlyphs();
164 bool RebuildFontCollection(FontID fontId);
165 void EvictUnusedTexts();
166
167 bool TryGetCaretPositionInternal(ID windowId, tachyon::int2 screenPos, ID targetTextEditorId, skb_editor_t*& outTargetEditor, skb_text_position_t& outPosition);
168 void DropDraggedText(ID windowId, tachyon::int2 screenPos, ID targetTextEditorId);
169 void SelectFromDropPoint(skb_editor_t* editor, skb_text_position_t dropPoint);
170 void EndTextDragging();
171
172 void RenderText(RegisteredText* entry, IntRect rect, IntRect innerRectScreen, IntRect clipRect, float pixelScale);
176 void BuildGlyphCache(RegisteredText* entry, const skb_layout_t* layout, float pixelScale);
178 void DrawIcon(IconID iconId, IntRect rect, IntRect clipRect, VisualColorStyleIndex tint);
179 void RenderEditor(RegisteredText* entry, IntRect rect, IntRect innerRectScreen, IntRect clipRect, float pixelScale);
180 bool UpdateText(RegisteredText* entry, std::string_view text);
181
182 bool ApplyTextEditUpdate(ID elementId, std::string& currentText, bool& wasModified);
183 void ClearComposition();
184 bool TryGetActiveTextEditorLocalPoint(ID windowId, tachyon::int2 screenSpace, tachyon::float2& outLocal) const;
187 bool IsActiveEditorPosOnLink(float localX, float localY) const;
188 bool TryGetTextEditorLocalPoint(ID textEditorId, ID windowId, tachyon::int2 screenSpace, tachyon::float2& outLocal) const;
189 ID FindTextEditorAtScreenPoint(ID windowId, tachyon::int2 screenSpace) const;
191 const ITextValidator* TextEditorValidator(ID elementId) const;
192 bool CanDropDraggedTextInto(ID targetTextEditorId) const;
193 void* UpdateLayoutWhenNecessary(RegisteredText* entry, float width) const;
196 IconMeasurement SideIconMeasurement(const RegisteredText* entry) const;
199 int32_t LayoutWidth(const RegisteredText* entry, int32_t width) const;
200 void* UpdateEditorWhenNecessary(RegisteredText* entry, float width) const;
201 TextMeasurement UpdateBaseMeasurementWhenNecessary(RegisteredText* entry) const;
202 TextMeasurement UpdateWrapMeasurementWhenNecessary(RegisteredText* entry, const TextWrapContext& wrapContext) const;
203 TextMeasurement MeasureTextInternal(RegisteredText* entry, const skb_layout_t* layout) const;
204
205 void UpdateRegisteredTextStyleState(RegisteredText* entry, tachyon::StyleRef styleIndex, tachyon::ElementState state);
206
207 std::unordered_map<uint64_t, TextID> m_elementIdToTextIdMap;
208 std::unordered_map<uint64_t, TextID> m_textKeyToIdMap;
209 std::unordered_set<uint64_t> m_textEditorElements;
210 std::unordered_set<uint64_t> m_readOnlyEditorElements;
211
212 std::unordered_map<uint64_t, const ITextValidator*> m_textEditorValidators;
213
214 ID m_textDragSourceId = ID::Invalid();
215 UndoHistory* m_undoHistory = nullptr;
218 std::unordered_map<uint64_t, std::string> m_externalText;
219 void PushTextEdit(ID elementId, const std::string& before, const std::string& after);
222 void FocusChangedField(ID elementId, EditSelection selection);
225 void NoteSelectionBeforeEdit();
226 std::string m_textDragPayload;
227 ID m_skipNextSelectAllId = ID::Invalid();
228 int2 m_textDragStartScreenPos = int2 { 0, 0 };
229 ID m_valueEditElementId = ID::Invalid();
230 std::string m_valueEditText;
231
232 ID m_pendingCursorVisibleEditorId = ID::Invalid();
233
237 struct EditorInput
238 {
240 KeyModifier modifiers = KeyModifier::None;
242 int32_t cursor = -1;
243 std::string composition;
244 };
245 std::vector<EditorInput> m_inputBeforeEditor;
247 bool HoldUntilEditorExists(EditorInput&& input);
249 void TypeInputHeldForEditor();
250
251 LibraryAllocator::RouteHandle m_libraryRoute = nullptr;
252 SkribidiContext* m_skribidiContext;
253 std::string m_fontDirectory { "fonts" };
254 std::string m_fallbackFontDirectory;
255 };
256
258} // namespace tachyon
Owns the per-frame state objects and runs their BeginFrame / EndFrame in order.
Definition gui_state_manager.h:65
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
The clipboard text editors copy to and paste from, and images go through; GUIApplication::SetClipboar...
Definition gui_clipboard.h:29
Text and icon registration, measurement, rendering and editing.
Definition gui_text_system.h:189
virtual FontID RegisterFontFamily(std::string_view familyName, std::span< const FontFaceSource > faces)=0
Registers a font family from its faces (regular, bold, italic, ...); each face's weight and style com...
What an editable field takes as text, asked while it is edited.
Definition gui_text_converter.h:22
void EndAtlasRecord() override
static void ScreenToTextEditorLocalPoint(tachyon::int2 screenSpace, const IntRect &textEditorInnerRect, tachyon::float2 &outLocal, const float3x2 &screenToLayout=float3x2::Identity())
void OnMouseButton(ID windowId, int2 screenPos, MouseButtons button, KeyModifier modifiers, MouseAction action) override
Mouse buttons for text editors: caret placement, selection, drag-drop.
intptr_t HitTestContent(TextID textId, int32_t localX, int32_t localY) const override
The contentId of the run at (localX, localY), element-local; 0 if none.
void SwitchTextEditor(ID from, ID to) override
The focused text field changed (GUIStateManager::TextEditorFocus): commit from (ID::Invalid if none) ...
IconMeasurement MeasureIcon(IconID iconId) const override
The natural size of iconId, in pixels.
TextMeasurement MeasureText(TextID textId, const TextWrapContext &wrapContext) const override
The measurement of textId wrapped to wrapContext.maxWidth, in pixels.
void BeginFrame(int2 screenMousePos, MouseButtons mouseButtons, uint32_t timeMs) override
Starts a frame, before the UI is built: clears the per-frame editor registrations that this frame's R...
bool UpdateText(TextID textId, std::string_view text) override
Replaces the text of textId; true when it changed.
std::string * ValueEditText(ID elementId, bool &started) override
The text the focused field elementId edits, kept while it has the focus (its value isn't formatted ag...
bool TryGetTextDragCursor(ID windowId, tachyon::int2 screenSpace, MouseCursor &outCursor) const override
The mouse cursor to show while text is dragged; true when it overrides the hover cursor.
bool IsHoveringSelectedText(ID textEditorId, ID windowId, tachyon::int2 screenSpace) const override
True when the screen point screenSpace is over selected text in textEditorId.
bool IsTextEditor(ID elementId) const override
True when elementId registered editable text this frame.
uint64_t LayoutsMade() const
Number of text layouts made so far, for diagnostics.
TextID RegisterEditableText(ID elementId, std::string &text, TextEditBehavior behavior, StyleRef styleIndex, ElementState state, bool &wasModified, const ITextValidator *validator=nullptr) override
Registers text as editable by elementId; wasModified is set when an edit changed it.
std::optional< IntRect > GetCaretScreenRect() const override
The active editor's caret in absolute screen coordinates (through any zoom/pan), to place the IME can...
void OnKeyboard(ID windowId, KeyCode key, int repeat, KeyModifier modifiers, KeyAction action) override
Keyboard input for the active text editor.
void OnMouseMove(ID windowId, int2 screenPos, MouseButtons button, KeyModifier modifiers) override
Mouse movement for text selection and drag-drop.
void ClearCache() override
Drops every registered text and the live editor; earlier TextIDs become invalid.
IconMeasurement GetIconMeasurement(IconID iconId) const override
The natural size of iconId, in pixels (same as MeasureIcon).
void SetHoveredContent(TextID textId, intptr_t contentId) override
Sets the hovered run of textId (by contentId), drawn in its hover style; 0 for none.
bool DoesTextEditorAcceptTabs(ID elementId) const override
True when the editor elementId inserts tabs (TextEditBehavior::AcceptTabs).
void BeginAtlasRecord(ID scopeId) override
Records the glyph and icon atlas entries a cached scope's primitives use, until EndAtlasRecord.
bool IsValidIcon(IconID iconId) const override
True when iconId is a registered icon.
std::string Cut() override
Removes the active editor's selected text and returns it.
SkribidiTextSystem(const AllocationHooks &hooks={})
With hooks set, the text libraries (HarfBuzz, skribidi, SheenBidi, nanosvg) allocate through them whi...
IconID RegisterIconFromPixels(const char *name, uint2 size, std::span< const uint8_t > rgba) override
Registers an icon from size.x x size.y RGBA pixels (straight alpha, rows from the top) under a unique...
bool TryGetTextDragHoverTarget(ID windowId, tachyon::int2 screenSpace, ID &outTargetId) const override
While text is dragged, sets outTargetId to the drop target under the cursor (ID::Invalid if none) and...
IconID RegisterIconFromSVG(const char *name, const char *svg_data, size_t svg_size) override
Registers an icon from SVG data under a unique name.
bool Initialize(GraphicsContext *graphicsContext, StyleManager *styleManager, TextAtlasManager *textAtlasManager, GUIStateManager *guiStateManager, IClipboard *clipboard) override
TextID RegisterRichText(ID elementId, const std::span< ContentRun > &contentRuns, StyleRef styleIndex, ElementState state) override
Registers text made of runs: styled text, inline icons and links.
std::optional< MouseCursor > LinkCursorAt(ID elementId, int32_t localX, int32_t localY) const override
The cursor of the link at (localX, localY), element-local (its LinkStyle::cursor); none when there's ...
void OnMouseScroll(ID windowId, int2 screenPos, float2 scrollOffset, KeyModifier modifiers) override
Mouse wheel input for text editors.
void SetFallbackFontDirectory(std::string_view directory) override
Where the optional fallback fonts are: Noto, for other scripts, emoji and symbols.
int32_t GetTextHeightWithoutIcon(TextID textId) const override
How high textId's text is unwrapped, in pixels, without the icon it was registered with (which its me...
TextID RegisterEditableRichText(ID elementId, const std::span< ContentRun > &contentRuns, TextEditBehavior behavior, StyleRef styleIndex, ElementState state, bool &wasModified) override
Registers rich text that renders like RegisterRichText but can also be selected and copied (the runs'...
std::string Copy() override
The active editor's selected text.
TextEditUpdate ConsumeTextEditUpdate(TextID textId, std::string &outText) override
Takes the pending edit of textId: the edited text goes to outText, the result says whether it changed...
TextMeasurement GetTextMeasurement(TextID textId) const override
The unwrapped measurement of textId, in pixels; cached until its text or style changes.
void OnTextComposition(ID windowId, std::string_view text, int32_t cursor) override
IME composition (pre-edit) text, UTF-8, shown but not yet committed; empty when composing ends.
TextID GetTextID(ID elementId) const override
The TextID elementId registered this frame, or TextID::Invalid.
void SetFontDirectory(std::string_view directory) override
Where the built-in fonts are (default "fonts", relative to the working directory): Inter for text and...
size_t RegisteredTextCount() const
Number of registered texts held, for diagnostics.
void SetLanguage(std::string_view languageTag) override
The default language as a BCP 47 tag ("ja", "zh-Hant", "ko"), for text whose style has no language of...
IconID RegisterIconFromSVGFile(const char *path) override
Registers an icon from an SVG file, named by its path.
FontID RegisterFontFamily(std::string_view familyName, std::span< const FontFaceSource > faces) override
Registers a font family from its faces (regular, bold, italic, ...); each face's weight and style com...
std::optional< IntRect > GetCursorRect(ID textEditorId) override
Takes a pending EnsureCursorVisible request for textEditorId and returns the caret rect,...
void RenderText(TextID textId, IntRect rect, IntRect innerRect, IntRect clipRect, float pixelScale=1.f, const float3x2 &screenToLayout=float3x2::Identity()) override
Draws textId into rect (renderer space, window-local), clipped to clipRect.
bool ReplayAtlasTouches(ID scopeId) override
void SetCaretColor(VisualColorStyleIndex color) override
The text caret's color (the theme's –caret). Set again after the style manager is cleared.
void RequestSelectAll(ID elementId) override
Selects all text of the editor elementId the next time it renders.
void Paste(std::string_view text) override
Inserts text into the active editor at the caret, replacing the selection.
void SetTextColor(TextID textId, VisualColorStyleIndex color) override
Draw a registered text in color instead of its style's text color, until it is registered again (call...
void RenderIcon(IconID iconId, IntRect rect, IntRect clipRect, VisualColorStyleIndex tint, std::span< const VisualShadow > shadows={}) override
Draws iconId fitted into rect (aspect kept, centered), clipped to clipRect.
ID ActiveElementID() const
The field the editor holds: the text focus as of the last switch.
std::string_view GetText(TextID textId) const override
The text registered as textId.
void EnsureCursorVisible(ID textEditorId) override
Asks for the caret of textEditorId to be scrolled into view; editor input calls it when the caret mov...
void SetUndoHistory(UndoHistory *history) override
Where committed text edits go as undo steps (none: not recorded).
Definition gui_text_system_skribidi.h:59
void UnregisterIcon(IconID iconId) override
Lets go of one registration of an icon from RegisterIconFromPixels.
TextID RegisterText(ID elementId, IconID iconId, std::string_view text, StyleRef styleIndex, ElementState state) override
Registers static text, with an optional icon, for elementId.
Bakes serializable Styles into deduplicated runtime layout/visual styles; a StyleRef holds indices in...
Definition gui_style_manager.h:39
Definition gui_text_atlas_manager.h:17
The app's undo history: steps that each undo and redo one change.
Definition gui_undo_history.h:20
KeyCode
Keys by what the keyboard layout makes them, not by position.
Definition gui_input_enums.h:106
KeyAction
A keyboard event: a key down or up, or a typed character (KeyChar).
Definition gui_input_enums.h:50
KeyModifier
Modifier keys held, combined with |.
Definition gui_input_enums.h:26
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
MouseAction
Whether a mouse button went down or up.
Definition gui_input_enums.h:58
@ None
Definition gui_input_enums.h:107
@ KeyDown
Definition gui_input_enums.h:51
@ None
Definition gui_input_enums.h:27
MouseCursor
Mouse cursor shown over an element (CSS: cursor).
Definition gui_style.h:481
VisualColorStyleIndex
Index into StyleManager's color style pool; Invalid (0) draws nothing (transparent).
Definition gui_style.h:33
FontID
Handle for a font family registered with ITextSystem::RegisterFontFamily().
Definition gui_style_text.h:20
TextID
Handle for text registered with the text system (ITextSystem::RegisterText and friends).
Definition gui_style_text.h:13
IconID
Handle for an icon registered with ITextSystem::RegisterIconFromSVG / RegisterIconFromSVGFile.
Definition gui_style_text.h:15
ElementState
Interaction state bits of an element; they pick the Style::states variant it draws with.
Definition gui_style.h:42
TextEditBehavior
Definition gui_text_system.h:98
const void * RouteHandle
A route, as Route returns it; null when nothing was routed.
Definition gui_library_allocator.h:28
Definition gui_logging.h:24
Memory functions a host gives the library, for the allocations that can be routed: Vulkan objects,...
Definition gui_allocation_hooks.h:17
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
Definition gui_text_system.h:194
Definition gui_text_system.h:53
Definition gui_base_types.h:406
A registered style, as StyleManager::Register returns it: indices into the shared style pools,...
Definition gui_style_ref.h:25
Definition gui_text_system.h:65
Definition gui_text_system.h:83
Definition gui_base_types.h:120
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition gui_base_types.h:218
static constexpr float3x2 Identity()
Definition gui_base_types.h:227
Integer 2D vector.
Definition gui_base_types.h:18
Definition gui_base_types.h:74