Repository navigation
Expand file tree
/
Copy pathmodel_document.h
More file actions
178 lines (145 loc) · 6.24 KB
/
Copy pathmodel_document.h
File metadata and controls
178 lines (145 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: The read-only document the selection panel shows for a file Diffractor has no viewer
// for: the head of its bytes as text when they read as text, and as a hex dump when they do not,
// laid out in rows, selected and copied. Decoding is pure and runs on a load thread; layout and the
// selection belong to the UI thread. No drawing and no I/O - ui_document.h draws it, and
// docs/selection-controls.md owns the behaviour.
#pragma once
#include "libdesktop/ui/syntax.h"
namespace ui
{
class doc_view;
}
enum class document_encoding : uint8_t
{
binary,
utf8,
utf16le,
utf16be,
windows_1252,
};
// A place in the document: a line and a byte offset into its UTF-8. In a hex dump the line is a row of
// the dump and the offset a column of its text, which is ASCII.
struct document_position
{
int line = 0;
int offset = 0;
friend bool operator==(const document_position&, const document_position&) = default;
friend auto operator<=>(const document_position&, const document_position&) = default;
};
// The decoded text of a file. Immutable once made, so a load thread builds it and hands it over.
struct document_text
{
struct line_span
{
uint32_t start = 0;
uint32_t length = 0;
};
document_encoding encoding = document_encoding::binary;
ui::syntax::language language = ui::syntax::language::plain;
bool csv = false;
// UTF-8 without its line breaks; lines index into it.
std::string text;
std::vector<line_span> lines;
// The highlighter's state at the start of each line, so any line can be coloured without the lines
// above it. Empty when the language carries no state from line to line.
std::vector<uint32_t> cookies;
bool is_text() const { return encoding != document_encoding::binary; }
std::string_view line(const size_t i) const
{
return std::string_view(text).substr(lines[i].start, lines[i].length);
}
};
// Reads the head of a file as text. Bytes that do not read as text - or text_allowed false - leave the
// encoding binary and the text empty, and the document is a hex dump of the bytes themselves. The
// extension, with or without its dot, chooses the highlighter.
document_text decode_document(core::cspan bytes, std::string_view extension, bool text_allowed);
// The columns a code point takes in a monospaced font: two for the wide East Asian ranges and emoji,
// none for combining marks, joiners and variation selectors, one for anything else.
int document_column_width(uint32_t cp);
// One code point of a row as it is drawn: where it starts in the source line, where in the drawn
// text, and at which column. A row's cells end with one for the row's end.
struct document_cell
{
uint32_t source = 0;
uint32_t display = 0;
int column = 0;
};
struct document_row_text
{
std::string display;
std::vector<document_cell> cells;
// The drawn text of a dump, which a copy takes, rather than the source text.
bool is_dump = false;
};
// A document's layout and selection. Text wraps at word boundaries to the columns it is given; a dump
// shows as many bytes a row as it is given. Every call that needs the bytes of a dump is passed them,
// because they belong to whoever loaded them.
class document_state
{
public:
static constexpr int tab_size = 4;
// A line longer than this is drawn without syntax colouring: a minified file is one line, and
// colouring a row starts from the start of its line.
static constexpr size_t max_highlighted_line = 16 * 1024;
explicit document_state(document_text text = {});
const document_text& text() const { return _text; }
bool is_text() const { return _text.is_text(); }
// Text: wraps to `columns` characters a row. Dump: `columns` bytes a row over byte_count bytes.
// A dump laid out at a new width loses its selection, whose columns no longer name the same bytes.
void layout(int columns, size_t byte_count);
int columns() const { return _columns; }
size_t row_count() const;
document_row_text row_text(size_t row, core::cspan bytes) const;
// The document line a row shows: its own index in a dump.
int row_line(size_t row) const;
size_t row_of(document_position pos) const;
// The position nearest a column of a row, rounding to the closer side of the character it lands on.
document_position position_at(size_t row, double column, core::cspan bytes) const;
// The character a column of a row falls on - what a double click selects the word of - where
// position_at gives the gap nearest it. Past the end of the row, its last character.
document_position character_at(size_t row, double column, core::cspan bytes) const;
// The drawn text of one row of a dump: the offset, then the bytes in hex, then the bytes as ASCII.
static std::string dump_row(core::cspan bytes, int bytes_per_row, size_t row);
static int dump_ascii_column(int bytes_per_row);
static int dump_row_chars(int bytes_per_row);
document_position anchor;
document_position caret;
// Set by a press on the document and cleared by any other press in the view. Copy and Select all
// act on the document only while it is set.
bool focused = false;
// UI-owned, like the selection: keep the common view and its column sizes when the panel rebuilds.
std::shared_ptr<ui::doc_view> formatted_view;
bool has_selection() const { return anchor != caret; }
std::pair<document_position, document_position> selection() const
{
return anchor < caret ? std::make_pair(anchor, caret) : std::make_pair(caret, anchor);
}
void select(const document_position from, const document_position to)
{
anchor = from;
caret = to;
}
void clear_selection() { anchor = caret = {}; }
void select_all(core::cspan bytes);
void select_word(document_position at, core::cspan bytes);
std::string selected_text(core::cspan bytes) const;
private:
struct text_row
{
uint32_t line = 0;
uint32_t begin = 0;
uint32_t end = 0;
};
document_text _text;
std::vector<text_row> _rows;
int _columns = 0;
size_t _byte_count = 0;
std::string_view line_text(int line, core::cspan bytes, std::string& dump) const;
int line_count(core::cspan bytes) const;
};