MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextEdit.cpp
Go to the documentation of this file.
1#include "TextEdit.hpp"
2
4
5void insert_codepoint(EditableText& s, uint32_t codepoint)
6{
7 if (is_control(codepoint))
8 return;
9 const std::string encoded = encode_utf8(codepoint);
10 s.text.insert(s.cursor, encoded);
11 s.cursor += encoded.size();
12}
13
15{
16 s.text.insert(s.cursor, 1, c);
17 s.cursor += 1;
18}
19
20void move(EditableText& s, int n)
21{
22 if (n > 0) {
23 for (int i = 0; i < n && s.cursor < s.text.size(); ++i)
25 } else {
26 for (int i = 0; i > n && s.cursor > 0; --i)
28 }
29}
30
31void erase(EditableText& s, int n)
32{
33 if (n > 0) {
34 for (int i = 0; i < n && s.cursor < s.text.size(); ++i) {
35 const size_t next = next_codepoint_offset(s.text, s.cursor);
36 s.text.erase(s.cursor, next - s.cursor);
37 }
38 } else {
39 for (int i = 0; i > n && s.cursor > 0; --i) {
40 const size_t prev = previous_codepoint_offset(s.text, s.cursor);
41 s.text.erase(prev, s.cursor - prev);
42 s.cursor = prev;
43 }
44 }
45}
46
47} // namespace MayaFlux::Portal::Text
Text editing operations: a mutable buffer plus a byte-offset cursor, and the operations that keep it ...
void move(EditableText &s, int n)
Move the cursor by n codepoints.
Definition TextEdit.cpp:20
size_t previous_codepoint_offset(std::string_view text, size_t byte_offset)
Byte offset of the codepoint boundary before byte_offset.
std::string encode_utf8(uint32_t codepoint)
Encode a Unicode codepoint as a UTF-8 byte sequence.
bool is_control(uint32_t codepoint) noexcept
True if a codepoint must never be rasterized or treated as printable content.
Definition TypeSetter.cpp:9
size_t next_codepoint_offset(std::string_view text, size_t byte_offset)
Byte offset of the codepoint boundary after byte_offset.
void erase(EditableText &s, int n)
Erase n codepoints from the cursor.
Definition TextEdit.cpp:31
void insert_codepoint(EditableText &s, uint32_t codepoint)
Insert one codepoint at the cursor, advancing it past the insert.
Definition TextEdit.cpp:5
void insert_literal(EditableText &s, char c)
Insert one raw ASCII byte at the cursor, advancing it past the insert.
Definition TextEdit.cpp:14
Mutable UTF-8 text plus a byte-offset cursor.
Definition TextEdit.hpp:31