Repository navigation
Expand file tree
/
Copy pathAnnotationStore.cs
More file actions
156 lines (137 loc) · 4.64 KB
/
Copy pathAnnotationStore.cs
File metadata and controls
156 lines (137 loc) · 4.64 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
using System.Collections.Generic;
using Windows.Foundation;
using Windows.UI.Input.Inking;
// HashSet used by EnumeratePageIndices
namespace MicaPDF
{
public sealed class TextAnnotation
{
public string Id { get; set; } = System.Guid.NewGuid().ToString();
public uint PageIndex { get; set; }
public string Text { get; set; } = "Text";
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; } = 180;
public double Height { get; set; } = 48;
public double FontSize { get; set; } = 18;
public bool IsBold { get; set; }
public bool IsItalic { get; set; }
public Windows.UI.Color Color { get; set; } = Windows.UI.Color.FromArgb(255, 20, 20, 20);
public TextAnnotation Snapshot() => new()
{
Id = Id,
PageIndex = PageIndex,
Text = Text,
X = X,
Y = Y,
Width = Width,
Height = Height,
FontSize = FontSize,
IsBold = IsBold,
IsItalic = IsItalic,
Color = Color
};
}
public sealed class AnnotationStore
{
private readonly Dictionary<uint, InkStrokeContainer> _strokes = new();
private readonly Dictionary<uint, List<TextAnnotation>> _texts = new();
public InkStrokeContainer GetStrokes(uint pageIndex)
{
if (!_strokes.TryGetValue(pageIndex, out var container))
{
container = new InkStrokeContainer();
_strokes[pageIndex] = container;
}
return container;
}
public List<TextAnnotation> GetTexts(uint pageIndex)
{
if (!_texts.TryGetValue(pageIndex, out var list))
{
list = new List<TextAnnotation>();
_texts[pageIndex] = list;
}
return list;
}
public void AddText(TextAnnotation annotation)
{
GetTexts(annotation.PageIndex).Add(annotation);
}
public void RemoveText(TextAnnotation annotation)
{
if (_texts.TryGetValue(annotation.PageIndex, out var list))
list.Remove(annotation);
}
public void RemoveStroke(uint pageIndex, InkStroke stroke)
{
var container = GetStrokes(pageIndex);
foreach (var item in container.GetStrokes())
item.Selected = false;
stroke.Selected = true;
container.DeleteSelected();
}
public void Clear()
{
_strokes.Clear();
_texts.Clear();
}
public IEnumerable<uint> EnumeratePageIndices()
{
var set = new HashSet<uint>();
foreach (var key in _strokes.Keys)
set.Add(key);
foreach (var key in _texts.Keys)
set.Add(key);
return set;
}
public bool HasAny()
{
foreach (var container in _strokes.Values)
{
if (container.GetStrokes().Count > 0)
return true;
}
foreach (var list in _texts.Values)
{
if (list.Count > 0)
return true;
}
return false;
}
public InkStroke? HitTestStroke(uint pageIndex, Point point, double inflate = 8)
{
var rect = new Rect(point.X - inflate, point.Y - inflate, inflate * 2, inflate * 2);
foreach (var stroke in GetStrokes(pageIndex).GetStrokes())
{
var bounds = stroke.BoundingRect;
bounds.X -= inflate;
bounds.Y -= inflate;
bounds.Width += inflate * 2;
bounds.Height += inflate * 2;
if (bounds.X <= point.X && point.X <= bounds.X + bounds.Width &&
bounds.Y <= point.Y && point.Y <= bounds.Y + bounds.Height)
{
return stroke;
}
}
_ = rect;
return null;
}
public TextAnnotation? HitTestText(uint pageIndex, Point point)
{
var list = GetTexts(pageIndex);
// Topmost (last created) first.
for (var i = list.Count - 1; i >= 0; i--)
{
var text = list[i];
if (point.X >= text.X && point.X <= text.X + text.Width &&
point.Y >= text.Y && point.Y <= text.Y + text.Height)
{
return text;
}
}
return null;
}
}
}