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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
pdf / pdf_ink_undo_redo_model.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/pdf_ink_undo_redo_model.h"
#include <stddef.h>
#include <optional>
#include <set>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/types/strong_alias.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
namespace chrome_pdf {
namespace {
PdfInkUndoRedoModel::DrawCommands& GetModifiableDrawCommands(
PdfInkUndoRedoModel::Commands& commands) {
return absl::get<PdfInkUndoRedoModel::DrawCommands>(commands);
}
PdfInkUndoRedoModel::EraseCommands& GetModifiableEraseCommands(
PdfInkUndoRedoModel::Commands& commands) {
return absl::get<PdfInkUndoRedoModel::EraseCommands>(commands);
}
} // namespace
PdfInkUndoRedoModel::PdfInkUndoRedoModel() = default;
PdfInkUndoRedoModel::~PdfInkUndoRedoModel() = default;
std::optional<PdfInkUndoRedoModel::DiscardedDrawCommands>
PdfInkUndoRedoModel::StartDraw() {
return StartImpl<DrawCommands>();
}
bool PdfInkUndoRedoModel::Draw(InkStrokeId id) {
CHECK(!commands_stack_.empty());
if (!IsAtTopOfStackWithGivenCommandType(CommandsType::kDraw)) {
// Can only draw at top of the stack, and the entry there must be for
// drawing.
return false;
}
if (HasIdInDrawCommands(id)) {
return false; // Failed invariant 3.
}
// Invariant 4 holds if invariant 6 holds.
CHECK(!HasIdInEraseCommands(id));
GetModifiableDrawCommands(commands_stack_.back())->insert(id);
return true;
}
bool PdfInkUndoRedoModel::FinishDraw() {
CHECK(!commands_stack_.empty());
if (!IsAtTopOfStackWithGivenCommandType(CommandsType::kDraw)) {
// Can only draw at top of the stack, and the entry there must be for
// drawing.
return false;
}
auto& commands = commands_stack_.back();
if (GetDrawCommands(commands)->empty()) {
commands = absl::monostate(); // Reuse top of stack if empty.
} else {
// Otherwise push new item onto the stack.
++stack_position_;
commands_stack_.push_back(absl::monostate());
}
return true;
}
std::optional<PdfInkUndoRedoModel::DiscardedDrawCommands>
PdfInkUndoRedoModel::StartErase() {
return StartImpl<EraseCommands>();
}
bool PdfInkUndoRedoModel::EraseStroke(InkStrokeId id) {
CHECK(!commands_stack_.empty());
if (!IsAtTopOfStackWithGivenCommandType(CommandsType::kErase)) {
// Can only erase at top of the stack, and the entry there must be for
// erasing.
return false;
}
if (!HasIdInDrawCommands(id)) {
return false; // Failed invariant 6.
}
if (HasIdInEraseCommands(id)) {
return false; // Failed invariant 5.
}
GetModifiableEraseCommands(commands_stack_.back())->insert(id);
return true;
}
bool PdfInkUndoRedoModel::EraseShape(InkModeledShapeId id) {
CHECK(!commands_stack_.empty());
if (!IsAtTopOfStackWithGivenCommandType(CommandsType::kErase)) {
// Can only erase at top of the stack, and the entry there must be for
// erasing.
return false;
}
if (HasIdInEraseCommands(id)) {
return false; // Failed invariant 5.
}
GetModifiableEraseCommands(commands_stack_.back())->insert(id);
return true;
}
bool PdfInkUndoRedoModel::FinishErase() {
CHECK(!commands_stack_.empty());
if (!IsAtTopOfStackWithGivenCommandType(CommandsType::kErase)) {
// Can only erase at top of the stack, and the entry there must be for
// erasing.
return false;
}
auto& commands = commands_stack_.back();
if (GetEraseCommands(commands)->empty()) {
commands = absl::monostate(); // Reuse top of stack if empty.
} else {
// Otherwise push new item onto the stack.
++stack_position_;
commands_stack_.push_back(absl::monostate());
}
return true;
}
PdfInkUndoRedoModel::Commands PdfInkUndoRedoModel::Undo() {
CHECK(!commands_stack_.empty());
CHECK_LT(stack_position_, commands_stack_.size());
if (stack_position_ == 0) {
return absl::monostate(); // Already at bottom of stack.
}
// Result is reverse of the recorded commands.
--stack_position_;
const auto& commands = commands_stack_[stack_position_];
switch (GetCommandsType(commands)) {
case CommandsType::kNone: {
NOTREACHED(); // Invariant 2.
}
case CommandsType::kDraw: {
EraseCommands result;
for (IdType id : GetDrawCommands(commands).value()) {
result->insert(id);
}
return result;
}
case CommandsType::kErase: {
DrawCommands result;
for (IdType id : GetEraseCommands(commands).value()) {
result->insert(id);
}
return result;
}
}
NOTREACHED();
}
PdfInkUndoRedoModel::Commands PdfInkUndoRedoModel::Redo() {
CHECK(!commands_stack_.empty());
CHECK_LT(stack_position_, commands_stack_.size());
if (stack_position_ == commands_stack_.size() - 1) {
return absl::monostate(); // Already at top of stack.
}
// Result is the recorded commands as-is.
const auto& commands = commands_stack_[stack_position_];
++stack_position_;
switch (GetCommandsType(commands)) {
case CommandsType::kNone: {
NOTREACHED(); // Invariant 2.
}
case CommandsType::kDraw: {
return GetDrawCommands(commands);
}
case CommandsType::kErase: {
return GetEraseCommands(commands);
}
}
NOTREACHED();
}
// static
PdfInkUndoRedoModel::CommandsType PdfInkUndoRedoModel::GetCommandsType(
const Commands& commands) {
if (absl::holds_alternative<absl::monostate>(commands)) {
return CommandsType::kNone;
}
if (absl::holds_alternative<DrawCommands>(commands)) {
return CommandsType::kDraw;
}
CHECK(absl::holds_alternative<EraseCommands>(commands));
return CommandsType::kErase;
}
// static
const PdfInkUndoRedoModel::DrawCommands& PdfInkUndoRedoModel::GetDrawCommands(
const Commands& commands) {
return absl::get<DrawCommands>(commands);
}
// static
const PdfInkUndoRedoModel::EraseCommands& PdfInkUndoRedoModel::GetEraseCommands(
const Commands& commands) {
return absl::get<EraseCommands>(commands);
}
template <typename T>
std::optional<PdfInkUndoRedoModel::DiscardedDrawCommands>
PdfInkUndoRedoModel::StartImpl() {
CHECK(!commands_stack_.empty());
CHECK_LT(stack_position_, commands_stack_.size());
DiscardedDrawCommands discarded_commands;
auto& commands = commands_stack_[stack_position_];
const bool has_commands = GetCommandsType(commands) != CommandsType::kNone;
if (stack_position_ == commands_stack_.size() - 1) {
if (has_commands) {
// Cannot start when drawing/erasing already started.
return std::nullopt;
}
} else {
CHECK(has_commands); // Invariant 2.
// Record the draw commands to discard.
for (size_t i = stack_position_; i < commands_stack_.size(); ++i) {
if (GetCommandsType(commands_stack_[i]) == CommandsType::kDraw) {
for (IdType id : GetDrawCommands(commands_stack_[i]).value()) {
bool inserted =
discarded_commands.insert(absl::get<InkStrokeId>(id)).second;
CHECK(inserted);
}
}
}
// Discard rest of stack.
//
// The vector capacity is never reduced when resizing to smaller size. Thus
// references to `commands_stack_` elements are not invalidated and safe to
// use.
commands_stack_.resize(stack_position_ + 1);
}
// Set the top of the stack to the appropriate command type.
//
// Safe to reuse `commands`, which references an element inside of
// `commands_stack_`. See note above the resize() call regarding safety.
commands = T();
return discarded_commands;
}
bool PdfInkUndoRedoModel::IsAtTopOfStackWithGivenCommandType(
CommandsType type) const {
if (stack_position_ != commands_stack_.size() - 1) {
return false;
}
return GetCommandsType(commands_stack_.back()) == type;
}
bool PdfInkUndoRedoModel::HasIdInDrawCommands(IdType id) const {
for (const auto& commands : commands_stack_) {
if (GetCommandsType(commands) == CommandsType::kDraw &&
GetDrawCommands(commands)->contains(id)) {
return true;
}
}
return false;
}
bool PdfInkUndoRedoModel::HasIdInEraseCommands(IdType id) const {
for (const auto& commands : commands_stack_) {
if (GetCommandsType(commands) == CommandsType::kErase &&
GetEraseCommands(commands)->contains(id)) {
return true;
}
}
return false;
}
} // namespace chrome_pdf