Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
cell_buffer.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <portals/geometry.hpp>
7#include <portals/text.hpp>
8
9#include <cstddef>
10#include <cstdint>
11#include <expected>
12#include <memory>
13#include <optional>
14#include <span>
15#include <string_view>
16
17namespace portals {
18
20struct RgbColor {
22 std::uint8_t red{};
24 std::uint8_t green{};
26 std::uint8_t blue{};
31 bool operator==(const RgbColor &other) const noexcept = default;
32};
33
35struct CellStyle {
37 std::optional<RgbColor> foreground;
39 std::optional<RgbColor> background;
41 bool bold{};
43 bool italic{};
45 bool underline{};
50 bool operator==(const CellStyle &other) const noexcept = default;
51};
52
54struct GlyphId {
56 std::uint32_t value{};
61 bool operator==(const GlyphId &other) const noexcept = default;
62};
63
65struct StyleId {
67 std::uint32_t value{};
72 bool operator==(const StyleId &other) const noexcept = default;
73};
74
84
86struct Cell {
94 std::uint8_t columns{1};
99 bool operator==(const Cell &other) const noexcept = default;
100};
101
103struct CellView {
107 std::string_view glyph;
110};
111
115 std::size_t max_columns{4096};
117 std::size_t max_rows{4096};
119 std::size_t max_cells{4 * 1024 * 1024};
121 std::size_t max_glyphs{65536};
123 std::size_t max_glyph_bytes{1024 * 1024};
125 std::size_t max_styles{4096};
130 bool operator==(const CellBufferLimits &other) const noexcept = default;
131};
132
162
166 std::size_t object_bytes{};
168 std::size_t cell_storage_bytes{};
174 std::size_t style_storage_bytes{};
176 std::size_t total_bytes{};
178 std::size_t glyph_count{};
180 std::size_t glyph_bytes{};
182 std::size_t style_count{};
187 bool operator==(const CellBufferMemory &other) const noexcept = default;
188};
189
204public:
208 CellBuffer(CellBuffer &&other) noexcept;
213 CellBuffer &operator=(CellBuffer &&other) noexcept;
215 CellBuffer(const CellBuffer &) = delete;
217 CellBuffer &operator=(const CellBuffer &) = delete;
218
227 [[nodiscard]] static std::expected<CellBuffer, CellBufferError> from(CellSize size, WidthPolicy policy = {},
232 [[nodiscard]] CellSize size() const noexcept;
236 [[nodiscard]] WidthPolicy policy() const noexcept;
240 [[nodiscard]] CellBufferLimits limits() const noexcept;
244 [[nodiscard]] std::span<const Cell> cells() const noexcept;
251 [[nodiscard]] std::expected<CellView, CellBufferError> at(CellPoint position) const noexcept;
264 [[nodiscard]] std::expected<void, CellBufferError> put(CellPoint position, const GraphemeText &text,
265 std::size_t grapheme_index, CellStyle style = {}) noexcept;
267 void clear() noexcept;
274 [[nodiscard]] std::expected<void, CellBufferError> resize(CellSize size);
278 [[nodiscard]] CellBufferMemory memory() const noexcept;
279
280private:
281 struct GlyphRecord {
282 std::size_t offset{};
283 std::size_t bytes{};
284 std::uint8_t columns{};
285 };
286
287 CellBuffer() = default;
288 void swap(CellBuffer &other) noexcept;
289 [[nodiscard]] bool contains(CellPoint position) const noexcept;
290 [[nodiscard]] std::size_t index(CellPoint position) const noexcept;
291 void clearGlyph(std::size_t cell_index) noexcept;
292
293 CellSize size_{};
294 WidthPolicy policy_{};
295 CellBufferLimits limits_{};
296 std::unique_ptr<Cell[]> cells_;
297 std::unique_ptr<GlyphRecord[]> glyphs_;
298 std::unique_ptr<char[]> bytes_;
299 std::unique_ptr<CellStyle[]> styles_;
300 std::size_t cell_count_{};
301 std::size_t glyph_count_{};
302 std::size_t byte_count_{};
303 std::size_t style_count_{};
304};
305
306} // namespace portals
Owning bounded cell framebuffer and immutable-width-policy intern pools.
Definition cell_buffer.hpp:203
WidthPolicy policy() const noexcept
Return the owning captured width policy; no errors or allocation.
void clear() noexcept
Reset to default blanks and reset intern counts; retain capacity, no errors/allocation.
CellBuffer(const CellBuffer &)=delete
Buffers have one owner; copying is disabled.
std::expected< void, CellBufferError > put(CellPoint position, const GraphemeText &text, std::size_t grapheme_index, CellStyle style={}) noexcept
Atomically write one complete grapheme, clearing every intersected old glyph.
CellBufferLimits limits() const noexcept
Return the owning resource caps; no errors or allocation.
CellSize size() const noexcept
Return owning normalized extents; no errors or allocation.
CellBuffer & operator=(const CellBuffer &)=delete
Buffers have one owner; copy assignment is disabled.
CellBufferMemory memory() const noexcept
Return measured owned storage/counts, excluding allocator overhead; no errors/allocation.
static std::expected< CellBuffer, CellBufferError > from(CellSize size, WidthPolicy policy={}, CellBufferLimits limits={})
Allocate a blank buffer and fixed-capacity glyph/byte/style pools.
std::expected< void, CellBufferError > resize(CellSize size)
Allocate a new blank extent with the same policy/caps; discard old contents on success.
std::span< const Cell > cells() const noexcept
Borrow row-major metadata until successful mutation/move/destruction; no errors.
CellBuffer & operator=(CellBuffer &&other) noexcept
Transfer storage, invalidate both objects' views and empty a distinct source.
CellBuffer(CellBuffer &&other) noexcept
Transfer storage and leave a distinct source empty; no allocation or errors.
std::expected< CellView, CellBufferError > at(CellPoint position) const noexcept
Resolve by coordinate; no API accepts arbitrary glyph/style IDs.
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Public Portals API.
Definition action.hpp:8
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
CellKind
A cell's role in a complete display grapheme.
Definition cell_buffer.hpp:76
@ BLANK
Default-style ASCII space, occupying one cell.
Definition cell_buffer.hpp:78
@ CONTINUATION
Second cell of the immediately preceding two-cell lead.
Definition cell_buffer.hpp:82
@ LEAD
First cell of a one- or two-cell grapheme, including a styled cleanup space.
Definition cell_buffer.hpp:80
CellBufferError
Observable, owning buffer failure; no partial write or resize is returned.
Definition cell_buffer.hpp:134
@ ROW_LIMIT
Normalized rows exceed the cap.
Definition cell_buffer.hpp:142
@ GLYPH_LIMIT
The glyph record pool is full.
Definition cell_buffer.hpp:154
@ OUT_OF_BOUNDS
A position lies outside the buffer.
Definition cell_buffer.hpp:146
@ ALLOCATION_FAILURE
Storage allocation failed; the original buffer remains unchanged.
Definition cell_buffer.hpp:160
@ STYLE_LIMIT
The style pool is full.
Definition cell_buffer.hpp:158
@ INVALID_GRAPHEME
A grapheme index lies outside the validated text.
Definition cell_buffer.hpp:148
@ GLYPH_BYTE_LIMIT
The glyph byte pool cannot hold the new complete glyph.
Definition cell_buffer.hpp:156
@ CELL_LIMIT
Checked cell product exceeds the cap or representation.
Definition cell_buffer.hpp:144
@ INVALID_POLICY
An unrecognized width policy value.
Definition cell_buffer.hpp:138
@ POLICY_MISMATCH
The text's complete captured width policy differs from this buffer.
Definition cell_buffer.hpp:150
@ COLUMN_LIMIT
Normalized columns exceed the cap.
Definition cell_buffer.hpp:140
@ INVALID_LIMITS
Invalid caps, ID representation or allocation-size arithmetic.
Definition cell_buffer.hpp:136
@ WIDE_AT_EDGE
The whole wide glyph cannot fit at the right edge.
Definition cell_buffer.hpp:152
Owning caps; creation allocates all intern pool capacity, including defaults.
Definition cell_buffer.hpp:113
std::size_t max_columns
Maximum normalized columns, bounded additionally by INT32_MAX.
Definition cell_buffer.hpp:115
bool operator==(const CellBufferLimits &other) const noexcept=default
Compare all resource caps.
std::size_t max_cells
Maximum cells in the checked rows-times-columns product.
Definition cell_buffer.hpp:119
std::size_t max_glyph_bytes
Maximum interned UTF-8 bytes, including one blank byte. Must be positive.
Definition cell_buffer.hpp:123
std::size_t max_rows
Maximum normalized rows, bounded additionally by INT32_MAX.
Definition cell_buffer.hpp:117
std::size_t max_glyphs
Maximum interned display glyphs, including the blank glyph. Must be positive.
Definition cell_buffer.hpp:121
std::size_t max_styles
Maximum interned styles, including the default style. Must be positive.
Definition cell_buffer.hpp:125
Measured C++ storage accounting, excluding allocator overhead and process RSS.
Definition cell_buffer.hpp:164
std::size_t glyph_bytes
Current used UTF-8 bytes, including the blank when storage exists.
Definition cell_buffer.hpp:180
bool operator==(const CellBufferMemory &other) const noexcept=default
Compare accounting and counts.
std::size_t total_bytes
Sum of object and allocated array bytes; not RSS or peak resize memory.
Definition cell_buffer.hpp:176
std::size_t glyph_record_storage_bytes
Allocated glyph-record array bytes, including unused slots.
Definition cell_buffer.hpp:170
std::size_t style_storage_bytes
Allocated style array bytes, including unused slots.
Definition cell_buffer.hpp:174
std::size_t cell_storage_bytes
Allocated cell array bytes (exact element count times sizeof(Cell)).
Definition cell_buffer.hpp:168
std::size_t glyph_count
Current interned glyph count, including the blank when storage exists.
Definition cell_buffer.hpp:178
std::size_t style_count
Current interned style count, including the default when storage exists.
Definition cell_buffer.hpp:182
std::size_t glyph_byte_storage_bytes
Allocated UTF-8 byte array capacity.
Definition cell_buffer.hpp:172
std::size_t object_bytes
sizeof(CellBuffer), including inline metadata and unique_ptr owners.
Definition cell_buffer.hpp:166
An owning value in terminal columns/rows, x right and y down.
Definition geometry.hpp:24
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
Small owning paint value, independent of themes and text metrics.
Definition cell_buffer.hpp:35
std::optional< RgbColor > foreground
Foreground RGB, or the terminal default when absent.
Definition cell_buffer.hpp:37
bool italic
Request italic rendering.
Definition cell_buffer.hpp:43
bool operator==(const CellStyle &other) const noexcept=default
Compare the complete value.
std::optional< RgbColor > background
Background RGB, or the terminal default when absent.
Definition cell_buffer.hpp:39
bool bold
Request bold rendering.
Definition cell_buffer.hpp:41
bool underline
Request underlined rendering.
Definition cell_buffer.hpp:45
A synchronous borrowed display glyph plus owning metadata and style.
Definition cell_buffer.hpp:103
std::string_view glyph
Borrowed display UTF-8; continuation resolves the same bytes as its lead.
Definition cell_buffer.hpp:107
Cell cell
Copied cell metadata with source-buffer-local IDs.
Definition cell_buffer.hpp:105
CellStyle style
Independent style value.
Definition cell_buffer.hpp:109
Owning metadata; IDs may only be interpreted within the source buffer.
Definition cell_buffer.hpp:86
CellKind kind
Blank, lead or continuation role.
Definition cell_buffer.hpp:92
bool operator==(const Cell &other) const noexcept=default
Compare metadata, including local IDs.
StyleId style
Interned style, shared by both halves of a wide glyph.
Definition cell_buffer.hpp:90
std::uint8_t columns
Display width: one for blank/narrow, two for wide lead, zero for continuation.
Definition cell_buffer.hpp:94
GlyphId glyph
Interned display grapheme, shared by both halves of a wide glyph.
Definition cell_buffer.hpp:88
Output-only glyph index scoped to one buffer between clear/resize/move.
Definition cell_buffer.hpp:54
std::uint32_t value
Local index; zero identifies the blank glyph. Never external authority.
Definition cell_buffer.hpp:56
bool operator==(const GlyphId &other) const noexcept=default
Compare local indices; equality across buffers has no semantic meaning.
Owning RGB color; terminal encoding belongs to the output adapter.
Definition cell_buffer.hpp:20
std::uint8_t red
Red component.
Definition cell_buffer.hpp:22
bool operator==(const RgbColor &other) const noexcept=default
Compare all components.
std::uint8_t blue
Blue component.
Definition cell_buffer.hpp:26
std::uint8_t green
Green component.
Definition cell_buffer.hpp:24
Output-only style index scoped to one buffer between clear/resize/move.
Definition cell_buffer.hpp:65
bool operator==(const StyleId &other) const noexcept=default
Compare local indices; equality across buffers has no semantic meaning.
std::uint32_t value
Local index; zero identifies the default style.
Definition cell_buffer.hpp:67
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42