Repository navigation
Expand file tree
/
Copy pathMainWindow.AnnotationManagement.cs
More file actions
511 lines (486 loc) · 23.5 KB
/
Copy pathMainWindow.AnnotationManagement.cs
File metadata and controls
511 lines (486 loc) · 23.5 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Docnet.Core;
using Docnet.Core.Models;
using Microsoft.Win32;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
using Scalpel.Services;
using PdfPigDoc = UglyToad.PdfPig.PdfDocument;
namespace Scalpel
{
public partial class MainWindow
{
// ============================================================
// Annotation management
// ============================================================
private void AddAnnotation(PageAnnotation annotation)
{
if (!_annotations.ContainsKey(annotation.PageIndex))
_annotations[annotation.PageIndex] = [];
_annotations[annotation.PageIndex].Add(annotation);
PushUndoEntry(new UndoEntry(UndoKind.Annotation, annotation.PageIndex, WasDirty: _isDirty));
// Any new edit invalidates the redo chain, the way every editor behaves.
_redoStack.Clear();
MarkDirty();
}
/// <summary>
/// Replaces <paramref name="before"/> with <paramref name="after"/> on a page, keeping its
/// position in the draw order, as one undoable step. Either may be null: null
/// <paramref name="after"/> deletes, null <paramref name="before"/> adds.
/// </summary>
private void ReplaceAnnotation(int pageIdx, PageAnnotation? before, PageAnnotation? after)
{
SwapAnnotation(pageIdx, before, after);
PushUndoEntry(new UndoEntry(UndoKind.Replace, pageIdx, WasDirty: _isDirty,
Annotation: before, Replacement: after));
_redoStack.Clear();
MarkDirty();
}
/// <summary>Puts <paramref name="to"/> where <paramref name="from"/> was (or appends it).</summary>
private void SwapAnnotation(int pageIdx, PageAnnotation? from, PageAnnotation? to)
{
if (!_annotations.ContainsKey(pageIdx)) _annotations[pageIdx] = [];
var list = _annotations[pageIdx];
int at = from is null ? -1 : list.IndexOf(from);
if (at >= 0) list.RemoveAt(at);
if (to is not null)
{
if (at >= 0) list.Insert(at, to);
else list.Add(to);
}
}
/// <summary>
/// Saves the current in-memory document bytes onto the undo stack so that
/// document-level operations (crop, delete page, merge, reorder) can be undone.
/// Must be called BEFORE modifying _doc.
/// </summary>
private void PushDocUndo()
{
if (_doc is null) return;
using var ms = new System.IO.MemoryStream();
_doc.Save(ms);
PushUndoEntry(new UndoEntry(UndoKind.Document, DocBytes: ms.ToArray(), WasDirty: _isDirty));
_redoStack.Clear();
TrimUndoAcrossTabs();
}
/// <summary>
/// Measures the on-screen box of a text annotation. Scalpel's model stores only the anchor
/// point - the box is laid out at render time - so a rotation remap needs this to carry the
/// box through the turn instead of treating the text as a zero-size point.
/// </summary>
private static Size MeasureTextAnnotation(TextAnnotation ta)
{
try
{
var tb = new TextBlock
{
Text = ta.Content,
FontSize = ta.FontSize,
// Bold and italic change the measured width, so the box must be measured
// in the style it will actually be drawn in.
FontWeight = ta.Bold ? FontWeights.Bold : FontWeights.Normal,
FontStyle = ta.Italic ? FontStyles.Italic : FontStyles.Normal,
Padding = new Thickness(2)
};
tb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var d = tb.DesiredSize;
// net48 has no double.IsFinite.
bool finite = !double.IsNaN(d.Width) && !double.IsInfinity(d.Width)
&& !double.IsNaN(d.Height) && !double.IsInfinity(d.Height);
return finite ? d : new Size(0, 0);
}
catch { return new Size(0, 0); }
}
private void RenderTextAnnotation(TextAnnotation ta)
{
var tb = new TextBlock
{
Text = ta.Content,
Foreground = new SolidColorBrush(ta.GetColor()),
FontFamily = (FontFamily)FindResource("FontUI"),
FontSize = ta.FontSize,
FontWeight = ta.Bold ? FontWeights.Bold : FontWeights.Normal,
FontStyle = ta.Italic ? FontStyles.Italic : FontStyles.Normal,
TextDecorations = ta.Underline ? TextDecorations.Underline : null,
Padding = new Thickness(2)
};
if (Scalpel.Services.BidiReorder.ContainsRtl(ta.Content))
{
tb.FontFamily = new FontFamily("Segoe UI, Noto Sans Hebrew, Noto Sans Arabic");
tb.FlowDirection = FlowDirection.RightToLeft;
}
Canvas.SetLeft(tb, ta.Position.X);
Canvas.SetTop(tb, ta.Position.Y);
_activeCanvas.Children.Add(tb);
}
private void RenderAllAnnotations(int pageIndex)
{
// Resolve this page's annotation surface from the unified per-page overlay map, which
// every multi-page view populates; fall back to the single-page canvas. View-mode
// independent on purpose so the tools behave identically in all four modes.
_activeCanvas = _continuousCanvases.TryGetValue(pageIndex, out var pageCanvas)
? pageCanvas : _annotationCanvas;
_activeCanvas.Children.Clear();
if (_annotations.TryGetValue(pageIndex, out var annotList))
foreach (var annot in annotList)
{
switch (annot)
{
case TextAnnotation ta:
RenderTextAnnotation(ta);
break;
case HighlightAnnotation ha:
var rect = new Rectangle
{
Fill = new SolidColorBrush(ha.GetColor()),
Width = ha.Bounds.Width,
Height = ha.Bounds.Height
};
Canvas.SetLeft(rect, ha.Bounds.X);
Canvas.SetTop(rect, ha.Bounds.Y);
_activeCanvas.Children.Add(rect);
break;
case InkAnnotation ia:
if (ia.Points.Count < 2) continue;
var poly = new Polyline
{
Stroke = new SolidColorBrush(ia.GetColor()),
StrokeThickness = ia.StrokeWidth,
StrokeLineJoin = PenLineJoin.Round,
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round
};
foreach (var pt in ia.Points) poly.Points.Add(pt);
_activeCanvas.Children.Add(poly);
break;
case TextEditAnnotation tea:
// White-out original text
var wo = new Rectangle
{
Fill = Brushes.White,
Width = tea.OriginalBounds.Width + 4,
Height = tea.OriginalBounds.Height + 4,
IsHitTestVisible = false
};
Canvas.SetLeft(wo, tea.OriginalBounds.X - 2);
Canvas.SetTop(wo, tea.OriginalBounds.Y - 2);
_activeCanvas.Children.Add(wo);
// Draw replacement text
var etb = new TextBlock
{
Text = tea.NewContent,
Foreground = tea.TextColor is Color tcol ? new SolidColorBrush(tcol) : Brushes.Black,
FontFamily = new FontFamily(tea.FontName),
FontSize = tea.FontSize,
FontWeight = ToWeight(tea.IsBold),
FontStyle = ToStyle(tea.IsItalic),
Padding = new Thickness(0)
};
// Match the save: a Hebrew/Arabic line is anchored at the RIGHT edge of the
// original line (DrawTextRun right-aligns it), so a longer or shorter
// replacement grows leftwards on screen too, not only in the saved file.
double etbLeft = tea.Position.X;
if (Scalpel.Services.BidiReorder.ContainsRtl(tea.NewContent))
{
etb.FlowDirection = FlowDirection.RightToLeft;
etbLeft = tea.OriginalBounds.Right - MeasureEditWidth(tea);
}
Canvas.SetLeft(etb, etbLeft);
Canvas.SetTop(etb, tea.Position.Y);
_activeCanvas.Children.Add(etb);
break;
case SignatureAnnotation sa:
if (sa.ImageData is not null)
{
// Image-based signature
try
{
var imgBytes = Convert.FromBase64String(sa.ImageData);
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new System.IO.MemoryStream(imgBytes);
bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
bmp.EndInit();
var imgCtrl = new System.Windows.Controls.Image
{
Source = bmp,
Width = sa.SourceWidth * sa.Scale,
Height = sa.SourceHeight * sa.Scale,
Stretch = System.Windows.Media.Stretch.Uniform,
IsHitTestVisible = false
};
Canvas.SetLeft(imgCtrl, sa.Position.X);
Canvas.SetTop(imgCtrl, sa.Position.Y);
_activeCanvas.Children.Add(imgCtrl);
}
catch { /* skip broken image */ }
}
else
{
foreach (var stroke in sa.Strokes)
{
if (stroke.Count < 2) continue;
var sigPoly = new Polyline
{
Stroke = Brushes.Black,
StrokeThickness = 2 * sa.Scale,
StrokeLineJoin = PenLineJoin.Round,
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round
};
foreach (var pt in stroke)
sigPoly.Points.Add(new Point(
sa.Position.X + pt.X * sa.Scale,
sa.Position.Y + pt.Y * sa.Scale));
_activeCanvas.Children.Add(sigPoly);
}
}
break;
case ImageAnnotation ia:
try
{
var iaBytes = Convert.FromBase64String(ia.ImageData);
var iaBmp = new System.Windows.Media.Imaging.BitmapImage();
iaBmp.BeginInit();
iaBmp.StreamSource = new System.IO.MemoryStream(iaBytes);
iaBmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
iaBmp.EndInit();
var iaCtrl = new System.Windows.Controls.Image
{
Source = iaBmp,
Width = ia.SourceWidth * ia.Scale,
Height = ia.SourceHeight * ia.Scale,
Stretch = System.Windows.Media.Stretch.Uniform,
IsHitTestVisible = false
};
Canvas.SetLeft(iaCtrl, ia.Position.X);
Canvas.SetTop(iaCtrl, ia.Position.Y);
_activeCanvas.Children.Add(iaCtrl);
}
catch { /* skip broken image */ }
break;
}
}
// Re-add form field overlays — RenderAllAnnotations clears the canvas so they must be restored.
if (_renderDims.TryGetValue(pageIndex, out var dims))
RenderFormFields(pageIndex, dims.w, dims.h);
}
private void Undo_Click(object sender, RoutedEventArgs e)
{
if (_undoStack.Count == 0)
{
SetStatus(Loc("Str_St_NothingToUndo"));
return;
}
var entry = _undoStack.Pop();
if (entry.Kind == UndoKind.Replace)
{
SwapAnnotation(entry.PageIdx, entry.Replacement, entry.Annotation);
PushRedoEntry(entry with { WasDirty = _isDirty });
ClearSelection();
RenderAllAnnotations(entry.PageIdx);
MarkDirty(entry.WasDirty);
SetStatus(Loc("Str_St_Undid"));
return;
}
if (entry.Kind == UndoKind.Annotation)
{
int pageIdx = entry.PageIdx;
PageAnnotation? removed = null;
if (_annotations.ContainsKey(pageIdx) && _annotations[pageIdx].Count > 0)
{
removed = _annotations[pageIdx][^1];
_annotations[pageIdx].RemoveAt(_annotations[pageIdx].Count - 1);
}
PushRedoEntry(entry with { Annotation = removed });
ClearSelection();
RenderAllAnnotations(pageIdx);
MarkDirty(entry.WasDirty);
SetStatus(Loc("Str_St_Undid"));
}
else // Document snapshot
{
if (entry.DocBytes is null) return;
// Snapshot the current state first so Redo can return to it.
try
{
if (_doc is not null)
{
using var redoMs = new System.IO.MemoryStream();
_doc.Save(redoMs);
PushRedoEntry(new UndoEntry(UndoKind.Document, DocBytes: redoMs.ToArray(),
WasDirty: _isDirty));
TrimUndoAcrossTabs();
}
}
catch { /* a missing redo step must never block the undo */ }
int selectedIdx = PageList.SelectedIndex;
var tempPath = App.MakeTempFile("undo", _s.Id);
System.IO.File.WriteAllBytes(tempPath, entry.DocBytes);
_doc?.Close();
// PdfSharpCore can write a snapshot whose xref offset points at the xref table,
// producing "Unexpected token 'xref'" on reopen. Repair via Import (preserves
// rotations) then PDFium, mirroring the save/reload path, instead of crashing.
try
{
_doc = PdfReader.Open(tempPath, PdfDocumentOpenMode.Modify);
}
catch (Exception undoOpenEx) when (IsXRefException(undoOpenEx))
{
var fixedPath = App.MakeTempFile("undofixed", _s.Id);
if (!TryImportRepairToPath(tempPath, fixedPath)
&& !TryPdfiumSaveWithZeroRotations(tempPath, fixedPath))
throw;
tempPath = fixedPath;
_doc = PdfReader.Open(tempPath, PdfDocumentOpenMode.Modify);
}
_currentFile = tempPath;
_annotations.Clear();
_renderDims.Clear();
ClearSelection();
MarkDirty(entry.WasDirty);
RefreshPageList();
if (selectedIdx >= 0 && selectedIdx < PageList.Items.Count)
PageList.SelectedIndex = selectedIdx;
else if (PageList.Items.Count > 0)
PageList.SelectedIndex = 0;
// Re-render the current view so the main page(s) reflect the restored document.
// RefreshPageList only updates the sidebar, and re-selecting the same page does not
// fire SelectionChanged, so grid/two-page tiles would otherwise stay stale.
int reIdx = PageList.SelectedIndex;
// A tab switch before these run makes them another document's work: bail.
int reIdxGen = _sessionGeneration;
if (_viewMode == ViewMode.Continuous)
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded,
(Action)(() => { if (!IsStale(reIdxGen)) SetupContinuousView(reIdx); }));
else
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, (Action)(() =>
{
if (IsStale(reIdxGen)) return;
RenderPage(_viewMode == ViewMode.Grid ? 0 : reIdx);
ReapplyGridOrFit();
}));
SetStatus(Loc("Str_St_UndidDoc"));
}
}
/// <summary>
/// Ctrl+Y / Ctrl+Shift+Z. Re-applies what Undo took away. The chain is cleared by any
/// new edit, so redo can only ever walk back up the path the user just undid.
/// </summary>
private void Redo_Click(object sender, RoutedEventArgs e)
{
if (_redoStack.Count == 0)
{
SetStatus(Loc("Str_St_NothingToRedo"));
return;
}
var entry = _redoStack.Pop();
if (entry.Kind == UndoKind.Replace)
{
SwapAnnotation(entry.PageIdx, entry.Annotation, entry.Replacement);
PushUndoEntry(entry with { WasDirty = _isDirty });
ClearSelection();
RenderAllAnnotations(entry.PageIdx);
MarkDirty();
SetStatus(Loc("Str_St_Redid"));
return;
}
if (entry.Kind == UndoKind.Annotation)
{
if (entry.Annotation is null) { SetStatus(Loc("Str_St_NothingToRedo")); return; }
int pageIdx = entry.PageIdx;
if (!_annotations.ContainsKey(pageIdx)) _annotations[pageIdx] = [];
_annotations[pageIdx].Add(entry.Annotation);
// Push straight onto the undo stack rather than through AddAnnotation, which
// would clear the redo chain we are walking.
PushUndoEntry(new UndoEntry(UndoKind.Annotation, pageIdx, WasDirty: _isDirty));
ClearSelection();
RenderAllAnnotations(pageIdx);
MarkDirty();
SetStatus(Loc("Str_St_Redid"));
return;
}
if (entry.DocBytes is null) return;
try
{
int selectedIdx = PageList.SelectedIndex;
using (var undoMs = new System.IO.MemoryStream())
{
_doc?.Save(undoMs);
PushUndoEntry(new UndoEntry(UndoKind.Document, DocBytes: undoMs.ToArray(),
WasDirty: _isDirty));
TrimUndoAcrossTabs();
}
var tempPath = App.MakeTempFile("redo", _s.Id);
System.IO.File.WriteAllBytes(tempPath, entry.DocBytes);
_doc?.Close();
try
{
_doc = PdfReader.Open(tempPath, PdfDocumentOpenMode.Modify);
}
catch (Exception redoOpenEx) when (IsXRefException(redoOpenEx))
{
var fixedPath = App.MakeTempFile("redofixed", _s.Id);
if (!TryImportRepairToPath(tempPath, fixedPath)
&& !TryPdfiumSaveWithZeroRotations(tempPath, fixedPath))
throw;
tempPath = fixedPath;
_doc = PdfReader.Open(tempPath, PdfDocumentOpenMode.Modify);
}
_currentFile = tempPath;
_annotations.Clear();
_renderDims.Clear();
ClearSelection();
MarkDirty();
RefreshPageList();
if (selectedIdx >= 0 && selectedIdx < PageList.Items.Count)
PageList.SelectedIndex = selectedIdx;
else if (PageList.Items.Count > 0)
PageList.SelectedIndex = 0;
int reIdx2 = PageList.SelectedIndex;
// A tab switch before these run makes them another document's work: bail.
int reIdx2Gen = _sessionGeneration;
if (_viewMode == ViewMode.Continuous)
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded,
(Action)(() => { if (!IsStale(reIdx2Gen)) SetupContinuousView(reIdx2); }));
else
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, (Action)(() =>
{
if (IsStale(reIdx2Gen)) return;
RenderPage(_viewMode == ViewMode.Grid ? 0 : reIdx2);
ReapplyGridOrFit();
}));
SetStatus(Loc("Str_St_RedidDoc"));
}
catch (Exception ex)
{
Scalpel.Services.Logger.Error("Edit", "redo.fail", ex.Message, ex);
SetStatus(Loc("Str_St_NothingToRedo"));
}
}
private void ClearAnnotations_Click(object sender, RoutedEventArgs e)
{
int pageIdx = PageList.SelectedIndex;
if (pageIdx < 0) return;
if (_annotations.ContainsKey(pageIdx) && _annotations[pageIdx].Count > 0)
{
_annotations[pageIdx].Clear();
MarkDirty();
}
ClearSelection();
_annotationCanvas.Children.Clear();
SetStatus("Cleared annotations on this page");
}
}
}