Repository navigation
Expand file tree
/
Copy pathAnnotationOverlay.cs
More file actions
848 lines (758 loc) · 32.3 KB
/
Copy pathAnnotationOverlay.cs
File metadata and controls
848 lines (758 loc) · 32.3 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Geometry;
using Microsoft.Graphics.Canvas.UI.Xaml;
using Microsoft.UI.Input;
using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Input.Inking;
namespace MicaPDF
{
public enum AnnotationTool
{
Select,
Pen,
Eraser,
Text
}
public sealed class AnnotationOverlay : UserControl
{
private readonly Grid _root;
private readonly CanvasControl _inkCanvas;
private readonly Canvas _textLayer;
private readonly Microsoft.UI.Xaml.Shapes.Rectangle _searchHighlight;
private readonly InkStrokeBuilder _strokeBuilder = new();
private readonly List<InkPoint> _wetPoints = new();
private readonly List<PdfGlyph> _selectedGlyphs = new();
private Rect? _searchRect;
private AnnotationStore? _store;
private AnnotationHistory? _history;
private PdfTextIndex? _textIndex;
private uint _pageIndex;
private double _pageWidth = 1;
private double _pageHeight = 1;
private AnnotationTool _tool = AnnotationTool.Select;
private bool _isPointerDown;
private uint _activePointerId;
private InkStroke? _selectedStroke;
private TextAnnotation? _selectedText;
private TextAnnotation? _editingText;
private Point _lastDragPos;
private Point _moveOrigin;
private TextBox? _editingBox;
private string _editSnapshot = "";
private bool _selectingPdfText;
private Point _pdfSelectStart;
private Rect? _pdfSelectRect;
private CanvasRenderTarget? _inkCache;
private bool _inkCacheValid;
private DateTime _lastInkInvalidate = DateTime.MinValue;
public double DefaultFontSize { get; set; } = 18;
public bool DefaultBold { get; set; }
public bool DefaultItalic { get; set; }
public Color DefaultTextColor { get; set; } = Color.FromArgb(255, 20, 20, 20);
public event EventHandler? AnnotationsChanged;
public event EventHandler? SelectionChanged;
public TextAnnotation? SelectedText => _selectedText;
public string SelectedPdfText => PdfTextIndex.BuildText(_selectedGlyphs);
public AnnotationOverlay()
{
_inkCanvas = new CanvasControl
{
ClearColor = Microsoft.UI.Colors.Transparent,
IsHitTestVisible = false
};
_inkCanvas.Draw += InkCanvas_Draw;
_textLayer = new Canvas
{
Background = null,
IsHitTestVisible = false
};
_searchHighlight = new Microsoft.UI.Xaml.Shapes.Rectangle
{
Fill = new SolidColorBrush(Color.FromArgb(80, 255, 200, 0)),
Stroke = new SolidColorBrush(Color.FromArgb(200, 255, 160, 0)),
StrokeThickness = 1.5,
Visibility = Visibility.Collapsed,
IsHitTestVisible = false
};
_root = new Grid { Background = new SolidColorBrush(Microsoft.UI.Colors.Transparent) };
_root.Children.Add(_inkCanvas);
_root.Children.Add(_searchHighlight);
_root.Children.Add(_textLayer);
_root.PointerPressed += OnPointerPressed;
_root.PointerMoved += OnPointerMoved;
_root.PointerReleased += OnPointerReleased;
_root.PointerCanceled += OnPointerReleased;
_root.PointerCaptureLost += OnPointerReleased;
_root.PointerExited += OnPointerExited;
_root.DoubleTapped += OnDoubleTapped;
SizeChanged += (_, _) => Refresh();
Content = _root;
var copyItem = new MenuFlyoutItem { Text = Loc.Get("overlay.copy") };
copyItem.Click += (_, _) => CopyPdfSelection();
var flyout = new MenuFlyout { Items = { copyItem } };
flyout.Opening += (_, _) =>
{
copyItem.Text = Loc.Get("overlay.copy");
copyItem.IsEnabled = !string.IsNullOrEmpty(SelectedPdfText);
};
ContextFlyout = flyout;
}
public void RefreshLocalizedUi()
{
if (ContextFlyout is MenuFlyout flyout && flyout.Items.OfType<MenuFlyoutItem>().FirstOrDefault() is { } copy)
copy.Text = Loc.Get("overlay.copy");
}
public double OverlayScale
{
get
{
var w = ActualWidth > 1 ? ActualWidth : Width;
if (double.IsNaN(w) || double.IsInfinity(w) || w <= 1 || _pageWidth <= 1)
return 1;
var scale = w / _pageWidth;
if (double.IsNaN(scale) || double.IsInfinity(scale) || scale <= 0)
return 1;
return scale;
}
}
public uint PageIndex => _pageIndex;
public void Attach(AnnotationStore store, uint pageIndex, double pageWidth, double pageHeight)
{
_store = store;
_pageIndex = pageIndex;
_pageWidth = Math.Max(1, pageWidth);
_pageHeight = Math.Max(1, pageHeight);
Refresh();
}
public void SetHistory(AnnotationHistory history) => _history = history;
public void SetTextIndex(PdfTextIndex? index)
{
_textIndex = index;
_selectedGlyphs.Clear();
InvalidateInk(force: true);
}
public void SetSearchHighlight(Rect? pageRect)
{
_searchRect = pageRect;
ApplySearchHighlightLayout();
}
public void ClearSearchHighlight() => SetSearchHighlight(null);
private void ApplySearchHighlightLayout()
{
if (_searchRect is not { } r || r.Width <= 0 || r.Height <= 0)
{
_searchHighlight.Visibility = Visibility.Collapsed;
return;
}
var scale = OverlayScale;
_searchHighlight.HorizontalAlignment = HorizontalAlignment.Left;
_searchHighlight.VerticalAlignment = VerticalAlignment.Top;
_searchHighlight.Margin = new Thickness(r.X * scale, r.Y * scale, 0, 0);
_searchHighlight.Width = Math.Max(2, r.Width * scale);
_searchHighlight.Height = Math.Max(2, r.Height * scale);
_searchHighlight.Visibility = Visibility.Visible;
}
public void SetTool(AnnotationTool tool)
{
_tool = tool;
_selectedStroke = null;
_selectedText = null;
_editingText = null;
_wetPoints.Clear();
_selectedGlyphs.Clear();
_selectingPdfText = false;
_pdfSelectRect = null;
_isPointerDown = false;
InvalidateInk(force: true);
RebuildTextLayer();
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
public void SetDrawingAttributes(InkDrawingAttributes attributes)
{
_strokeBuilder.SetDefaultDrawingAttributes(attributes);
}
public void Refresh()
{
if (double.IsNaN(Width) || double.IsNaN(Height) ||
(ActualWidth <= 0 && (double.IsNaN(Width) || Width <= 0)))
{
return;
}
ApplySearchHighlightLayout();
InvalidateInk(force: true);
RebuildTextLayer();
}
public bool CopyPdfSelection()
{
var text = SelectedPdfText;
if (string.IsNullOrEmpty(text)) return false;
var package = new DataPackage();
package.SetText(text);
Clipboard.SetContent(package);
return true;
}
public bool DeleteSelection()
{
if (_store == null) return false;
var deleted = false;
if (_selectedStroke != null)
{
_history?.Push(new RemoveStrokeCommand(_store, _pageIndex, _selectedStroke));
_store.RemoveStroke(_pageIndex, _selectedStroke);
_selectedStroke = null;
deleted = true;
InvalidateInk(force: true);
}
if (_selectedText != null)
{
_history?.Push(new RemoveTextCommand(_store, _selectedText));
_store.RemoveText(_selectedText);
_selectedText = null;
_editingText = null;
deleted = true;
RebuildTextLayer();
}
if (deleted)
{
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
return deleted;
}
public void ApplyStyleToSelectedText(double? fontSize = null, bool? bold = null, bool? italic = null, Color? color = null)
{
if (_selectedText == null) return;
var before = _selectedText.Snapshot();
if (fontSize.HasValue) _selectedText.FontSize = fontSize.Value;
if (bold.HasValue) _selectedText.IsBold = bold.Value;
if (italic.HasValue) _selectedText.IsItalic = italic.Value;
if (color.HasValue) _selectedText.Color = color.Value;
FitTextHeight(_selectedText);
_history?.Push(new StyleTextCommand(_selectedText, before));
RebuildTextLayer();
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
}
private Point ToPage(Point overlay) => new(overlay.X / OverlayScale, overlay.Y / OverlayScale);
private float PageInflate(double overlayPx) => (float)(overlayPx / OverlayScale);
private void InvalidateInk(bool force = false)
{
_inkCacheValid = false;
if (force)
{
_inkCanvas.Invalidate();
return;
}
var now = DateTime.UtcNow;
if ((now - _lastInkInvalidate).TotalMilliseconds < 16)
return;
_lastInkInvalidate = now;
_inkCanvas.Invalidate();
}
private void TryRebuildInkCache(CanvasControl sender)
{
if (_store == null) return;
var width = (float)Math.Max(1, sender.ActualWidth);
var height = (float)Math.Max(1, sender.ActualHeight);
if (width < 2 || height < 2) return;
_inkCache?.Dispose();
_inkCache = new CanvasRenderTarget(sender, width, height, sender.Dpi);
using var ds = _inkCache.CreateDrawingSession();
ds.Clear(Microsoft.UI.Colors.Transparent);
var scale = (float)OverlayScale;
ds.Transform = Matrix3x2.CreateScale(scale);
foreach (var stroke in _store.GetStrokes(_pageIndex).GetStrokes())
DrawStroke(ds, stroke);
ds.Transform = Matrix3x2.Identity;
_inkCacheValid = true;
}
private void InkCanvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (_store == null) return;
var scale = (float)OverlayScale;
if (_wetPoints.Count <= 1 && !_inkCacheValid)
TryRebuildInkCache(sender);
if (_inkCacheValid && _inkCache != null)
args.DrawingSession.DrawImage(_inkCache);
else
{
args.DrawingSession.Transform = Matrix3x2.CreateScale(scale);
foreach (var stroke in _store.GetStrokes(_pageIndex).GetStrokes())
DrawStroke(args.DrawingSession, stroke);
args.DrawingSession.Transform = Matrix3x2.Identity;
}
foreach (var glyph in _selectedGlyphs)
args.DrawingSession.FillRectangle(ToWin2d(glyph.Bounds), Color.FromArgb(90, 51, 153, 255));
if (_pdfSelectRect is Rect r)
args.DrawingSession.DrawRectangle(ToWin2d(r), Color.FromArgb(180, 51, 153, 255), 1f / scale);
if (_wetPoints.Count > 1)
{
try
{
var wet = _strokeBuilder.CreateStrokeFromInkPoints(_wetPoints, Matrix3x2.Identity);
args.DrawingSession.Transform = Matrix3x2.CreateScale(scale);
DrawStroke(args.DrawingSession, wet);
args.DrawingSession.Transform = Matrix3x2.Identity;
}
catch { }
}
if (_selectedStroke != null)
{
var bounds = _selectedStroke.BoundingRect;
args.DrawingSession.Transform = Matrix3x2.CreateScale(scale);
args.DrawingSession.DrawRectangle(bounds, Color.FromArgb(180, 0, 120, 215), 2f / scale);
args.DrawingSession.Transform = Matrix3x2.Identity;
}
}
private static Windows.Foundation.Rect ToWin2d(Rect r) => r;
public static void DrawStroke(CanvasDrawingSession ds, InkStroke stroke)
{
var segments = stroke.GetRenderingSegments();
if (segments.Count == 0) return;
using var builder = new CanvasPathBuilder(ds);
var started = false;
foreach (var seg in segments)
{
var pos = new Vector2((float)seg.Position.X, (float)seg.Position.Y);
if (!started)
{
builder.BeginFigure(pos);
started = true;
}
else
{
builder.AddCubicBezier(
new Vector2((float)seg.BezierControlPoint1.X, (float)seg.BezierControlPoint1.Y),
new Vector2((float)seg.BezierControlPoint2.X, (float)seg.BezierControlPoint2.Y),
pos);
}
}
builder.EndFigure(CanvasFigureLoop.Open);
using var geometry = CanvasGeometry.CreatePath(builder);
var attr = stroke.DrawingAttributes;
var color = attr.Color;
if (attr.DrawAsHighlighter)
color = Color.FromArgb(90, color.R, color.G, color.B);
ds.DrawGeometry(geometry, color, (float)Math.Max(0.5, attr.Size.Width));
}
private void OnDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (_tool != AnnotationTool.Select || _store == null) return;
var pos = ToPage(e.GetPosition(_root));
var text = _store.HitTestText(_pageIndex, pos);
if (text == null) return;
_selectedText = text;
_selectedStroke = null;
_editingText = text;
_editSnapshot = text.Text;
RebuildTextLayer();
SelectionChanged?.Invoke(this, EventArgs.Empty);
_editingBox?.Focus(FocusState.Programmatic);
e.Handled = true;
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (_store == null) return;
var point = e.GetCurrentPoint(_root);
var overlayPos = point.Position;
var pos = ToPage(overlayPos);
var isEraser = point.Properties.IsEraser || _tool == AnnotationTool.Eraser;
if (_tool == AnnotationTool.Select && !isEraser)
{
if (_editingText != null && _store.HitTestText(_pageIndex, pos)?.Id != _editingText.Id)
CommitTextEdit();
var hitText = _store.HitTestText(_pageIndex, pos);
var hitStroke = hitText == null ? _store.HitTestStroke(_pageIndex, pos, PageInflate(12)) : null;
if (hitText != null || hitStroke != null)
{
_selectedGlyphs.Clear();
_pdfSelectRect = null;
_isPointerDown = true;
_activePointerId = e.Pointer.PointerId;
_root.CapturePointer(e.Pointer);
BeginSelect(pos);
e.Handled = true;
return;
}
var glyph = _textIndex?.HitTest(_pageIndex, pos);
if (glyph != null)
{
_selectedText = null;
_selectedStroke = null;
_selectingPdfText = true;
_pdfSelectStart = pos;
_pdfSelectRect = new Rect(pos, new Size(0, 0));
_selectedGlyphs.Clear();
_selectedGlyphs.Add(glyph);
_isPointerDown = true;
_activePointerId = e.Pointer.PointerId;
_root.CapturePointer(e.Pointer);
InvalidateInk(force: true);
RebuildTextLayer();
SelectionChanged?.Invoke(this, EventArgs.Empty);
e.Handled = true;
return;
}
_selectedText = null;
_selectedStroke = null;
_selectedGlyphs.Clear();
_pdfSelectRect = null;
InvalidateInk(force: true);
RebuildTextLayer();
SelectionChanged?.Invoke(this, EventArgs.Empty);
return;
}
if (isEraser)
{
_isPointerDown = true;
_activePointerId = e.Pointer.PointerId;
if (point.PointerDeviceType != PointerDeviceType.Pen)
_root.CapturePointer(e.Pointer);
EraseAt(pos);
e.Handled = true;
return;
}
if (_tool == AnnotationTool.Text)
{
_isPointerDown = true;
_activePointerId = e.Pointer.PointerId;
CreateTextAt(pos);
e.Handled = true;
return;
}
if (_tool != AnnotationTool.Pen)
return;
if (point.PointerDeviceType != PointerDeviceType.Mouse && !point.IsInContact)
return;
_isPointerDown = true;
_activePointerId = e.Pointer.PointerId;
if (point.PointerDeviceType != PointerDeviceType.Pen)
_root.CapturePointer(e.Pointer);
_wetPoints.Clear();
_wetPoints.Add(CreateInkPoint(point, pos));
e.Handled = true;
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(_root);
var pos = ToPage(point.Position);
var isEraser = point.Properties.IsEraser || (_tool == AnnotationTool.Eraser && _isPointerDown);
if (_tool == AnnotationTool.Pen && _isPointerDown && !point.IsInContact && point.PointerDeviceType != PointerDeviceType.Mouse)
{
EndStroke(e.Pointer);
return;
}
if (!_isPointerDown || _store == null) return;
if (e.Pointer.PointerId != _activePointerId && _activePointerId != 0) return;
if (isEraser)
{
EraseAt(pos);
e.Handled = true;
return;
}
switch (_tool)
{
case AnnotationTool.Pen:
if (point.PointerDeviceType != PointerDeviceType.Mouse && !point.IsInContact)
return;
_wetPoints.Add(CreateInkPoint(point, pos));
InvalidateInk();
break;
case AnnotationTool.Select:
if (_selectingPdfText)
{
_pdfSelectRect = NormalizeRect(_pdfSelectStart, pos);
_selectedGlyphs.Clear();
_selectedGlyphs.AddRange(_textIndex?.GlyphsInRect(_pageIndex, _pdfSelectRect.Value) ?? new List<PdfGlyph>());
InvalidateInk(force: true);
}
else
{
DragSelection(pos);
}
break;
}
e.Handled = true;
}
private void OnPointerExited(object sender, PointerRoutedEventArgs e)
{
if (_tool == AnnotationTool.Pen && _isPointerDown)
EndStroke(e.Pointer);
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (!_isPointerDown) return;
if (_activePointerId != 0 && e.Pointer.PointerId != _activePointerId) return;
EndStroke(e.Pointer);
e.Handled = true;
}
private void EndStroke(Pointer pointer)
{
if (!_isPointerDown) return;
_isPointerDown = false;
_activePointerId = 0;
if (_selectingPdfText)
{
_selectingPdfText = false;
_pdfSelectRect = null;
InvalidateInk(force: true);
}
else if (_tool == AnnotationTool.Select)
{
CommitMove();
}
else if (_tool == AnnotationTool.Pen && _store != null && _wetPoints.Count > 1)
{
try
{
var stroke = _strokeBuilder.CreateStrokeFromInkPoints(_wetPoints, Matrix3x2.Identity);
_store.GetStrokes(_pageIndex).AddStroke(stroke);
_history?.Push(new AddStrokeCommand(_store, _pageIndex, stroke));
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
}
catch { }
}
_wetPoints.Clear();
try { _root.ReleasePointerCapture(pointer); } catch { }
InvalidateInk(force: true);
}
private static InkPoint CreateInkPoint(PointerPoint point, Point pagePos)
{
var pressure = point.Properties.Pressure;
if (pressure <= 0) pressure = 0.5f;
return new InkPoint(pagePos, pressure);
}
private void EraseAt(Point pos)
{
if (_store == null) return;
var hit = _store.HitTestStroke(_pageIndex, pos, PageInflate(20));
if (hit != null)
{
_history?.Push(new RemoveStrokeCommand(_store, _pageIndex, hit));
_store.RemoveStroke(_pageIndex, hit);
if (ReferenceEquals(_selectedStroke, hit))
_selectedStroke = null;
InvalidateInk(force: true);
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
return;
}
var text = _store.HitTestText(_pageIndex, pos);
if (text != null)
{
if (_selectedText?.Id == text.Id) _selectedText = null;
if (_editingText?.Id == text.Id) _editingText = null;
_history?.Push(new RemoveTextCommand(_store, text));
_store.RemoveText(text);
RebuildTextLayer();
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
}
private void BeginSelect(Point pos)
{
if (_store == null) return;
_selectedText = _store.HitTestText(_pageIndex, pos);
_selectedStroke = _selectedText == null ? _store.HitTestStroke(_pageIndex, pos, PageInflate(12)) : null;
_lastDragPos = pos;
_moveOrigin = pos;
InvalidateInk(force: true);
RebuildTextLayer();
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
private void DragSelection(Point pos)
{
var dx = pos.X - _lastDragPos.X;
var dy = pos.Y - _lastDragPos.Y;
if (Math.Abs(dx) < 0.01 && Math.Abs(dy) < 0.01) return;
_lastDragPos = pos;
if (_selectedStroke != null)
{
_selectedStroke.PointTransform =
_selectedStroke.PointTransform * Matrix3x2.CreateTranslation((float)dx, (float)dy);
InvalidateInk(force: true);
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
}
if (_selectedText != null && _editingText?.Id != _selectedText.Id)
{
_selectedText.X += dx;
_selectedText.Y += dy;
foreach (var child in _textLayer.Children)
{
if (child is Border border && border.Tag is TextAnnotation ann && ann.Id == _selectedText.Id)
{
var s = OverlayScale;
Canvas.SetLeft(border, _selectedText.X * s);
Canvas.SetTop(border, _selectedText.Y * s);
break;
}
}
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
}
}
private void CommitMove()
{
var dx = _lastDragPos.X - _moveOrigin.X;
var dy = _lastDragPos.Y - _moveOrigin.Y;
if (Math.Abs(dx) < 0.2 && Math.Abs(dy) < 0.2) return;
if (_selectedStroke != null)
_history?.Push(new TranslateStrokeCommand(_selectedStroke, (float)dx, (float)dy));
if (_selectedText != null && _editingText?.Id != _selectedText.Id)
_history?.Push(new TranslateTextCommand(_selectedText, dx, dy));
}
private void CreateTextAt(Point pos)
{
if (_store == null) return;
var annotation = new TextAnnotation
{
PageIndex = _pageIndex,
X = pos.X,
Y = pos.Y,
Text = "",
FontSize = DefaultFontSize,
IsBold = DefaultBold,
IsItalic = DefaultItalic,
Color = DefaultTextColor,
Width = 180,
Height = Math.Max(32, DefaultFontSize * 1.6)
};
_store.AddText(annotation);
_selectedText = annotation;
_editingText = annotation;
_editSnapshot = "";
_history?.Push(new AddTextCommand(_store, annotation));
RebuildTextLayer();
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
SelectionChanged?.Invoke(this, EventArgs.Empty);
if (_editingBox != null)
{
_editingBox.PlaceholderText = Loc.Get("overlay.textPlaceholder");
_editingBox.Focus(FocusState.Programmatic);
}
}
private void CommitTextEdit()
{
if (_editingText != null && _editingText.Text != _editSnapshot)
_history?.Push(new EditTextCommand(_editingText, _editSnapshot, _editingText.Text));
_editingText = null;
RebuildTextLayer();
}
private static void FitTextHeight(TextAnnotation text)
{
var raw = text.Text ?? "";
var hardLines = Math.Max(1, raw.Replace("\r\n", "\n").Replace('\r', '\n')
.Split('\n').Length);
// Estimate wrapped lines (TextWrapping) from content width.
var avgChar = Math.Max(4.0, text.FontSize * 0.55);
var charsPerLine = Math.Max(8, (int)(text.Width / avgChar));
var wrapped = 0;
foreach (var line in raw.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'))
wrapped += Math.Max(1, (int)Math.Ceiling(Math.Max(1, line.Length) / (double)charsPerLine));
var lines = Math.Max(hardLines, wrapped);
text.Height = Math.Max(text.FontSize * 1.8, lines * text.FontSize * 1.5 + 16);
}
private static Rect NormalizeRect(Point a, Point b)
{
var x = Math.Min(a.X, b.X);
var y = Math.Min(a.Y, b.Y);
return new Rect(x, y, Math.Abs(a.X - b.X), Math.Abs(a.Y - b.Y));
}
private void RebuildTextLayer()
{
_textLayer.Children.Clear();
_editingBox = null;
_textLayer.IsHitTestVisible = _editingText != null;
if (_store == null) return;
var s = OverlayScale;
if (double.IsNaN(s) || double.IsInfinity(s) || s <= 0)
s = 1;
foreach (var text in _store.GetTexts(_pageIndex))
{
FitTextHeight(text);
var isEditing = _editingText?.Id == text.Id;
var isSelected = _selectedText?.Id == text.Id;
var width = text.Width * s;
var height = text.Height * s;
if (double.IsNaN(width) || double.IsNaN(height) || width <= 0 || height <= 0)
continue;
var border = new Border
{
Width = width,
Height = height,
MinHeight = Math.Max(1, text.FontSize * 1.8 * s),
Background = new SolidColorBrush(Color.FromArgb((byte)(isSelected ? 60 : 30), 255, 255, 0)),
BorderBrush = new SolidColorBrush(
isSelected
? Color.FromArgb(255, 0, 120, 215)
: Color.FromArgb(120, 100, 100, 100)),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(4),
Padding = new Thickness(4),
Tag = text,
IsHitTestVisible = isEditing
};
if (isEditing)
{
var box = new TextBox
{
Text = text.Text,
FontSize = Math.Max(8, text.FontSize * s),
FontWeight = text.IsBold ? FontWeights.Bold : FontWeights.Normal,
FontStyle = text.IsItalic ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal,
Foreground = new SolidColorBrush(text.Color),
AcceptsReturn = true,
TextWrapping = TextWrapping.Wrap,
Background = new SolidColorBrush(Microsoft.UI.Colors.Transparent),
BorderThickness = new Thickness(0),
MinHeight = text.FontSize * 1.4 * s,
VerticalAlignment = VerticalAlignment.Stretch,
PlaceholderText = string.IsNullOrEmpty(text.Text) ? Loc.Get("overlay.textPlaceholder") : ""
};
box.LostFocus += (_, _) =>
{
if (_editingText?.Id == text.Id)
CommitTextEdit();
};
box.TextChanged += (_, _) =>
{
text.Text = box.Text;
FitTextHeight(text);
border.Height = Math.Max(border.MinHeight, text.Height * OverlayScale);
AnnotationsChanged?.Invoke(this, EventArgs.Empty);
};
border.Child = box;
_editingBox = box;
}
else
{
// TextBlock preserves multi-line display; read-only TextBox often clips to one line.
border.Child = new TextBlock
{
Text = string.IsNullOrEmpty(text.Text) ? Loc.Get("overlay.textPlaceholder") : text.Text,
FontSize = Math.Max(8, text.FontSize * s),
FontWeight = text.IsBold ? FontWeights.Bold : FontWeights.Normal,
FontStyle = text.IsItalic ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal,
Foreground = new SolidColorBrush(
string.IsNullOrEmpty(text.Text)
? Color.FromArgb(160, text.Color.R, text.Color.G, text.Color.B)
: text.Color),
TextWrapping = TextWrapping.WrapWholeWords,
IsHitTestVisible = false
};
}
Canvas.SetLeft(border, text.X * s);
Canvas.SetTop(border, text.Y * s);
_textLayer.Children.Add(border);
}
}
}
}