-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas.cs
More file actions
267 lines (225 loc) · 8.23 KB
/
Canvas.cs
File metadata and controls
267 lines (225 loc) · 8.23 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Numerics;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Text;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Geometry;
using Microsoft.Graphics.Canvas.Text;
using MyScript.IInk.Graphics;
using MyScript.InteractiveInk.Annotations;
using MyScript.InteractiveInk.Extensions;
using Color = Windows.UI.Color;
namespace MyScript.InteractiveInk.UI.Commands
{
public sealed partial class Canvas
{
[CanBeNull] public CanvasDrawingSession DrawingSession { get; set; }
}
/// <summary>
/// Implements <see cref="ICanvas" />.
/// <inheritdoc cref="ICanvas" />
/// </summary>
[SuppressMessage("ReSharper", "RedundantExtendsListEntry")]
public sealed partial class Canvas : ICanvas
{
private Dictionary<string, CanvasActiveLayer> Layers { get; } = new Dictionary<string, CanvasActiveLayer>();
public void StartGroup(string id, float x, float y, float width, float height, bool clipContent)
{
if (!clipContent)
{
return;
}
Layers[id] = DrawingSession?.CreateLayer(1, new Rect(x, y, width, height));
}
public void EndGroup(string id)
{
if (!Layers.ContainsKey(id))
{
return;
}
Layers.Remove(id, out var layer);
layer?.Dispose();
}
public void StartItem(string id)
{
}
public void EndItem(string id)
{
}
#region Rendering
public IPath CreatePath()
{
if (DrawingSession == null)
{
return null;
}
var builder = new CanvasPathBuilder(DrawingSession.Device);
builder.SetFilledRegionDetermination(FillRule);
builder.SetSegmentOptions(CanvasFigureSegmentOptions.None);
return new Path {PathBuilder = builder};
}
public void DrawPath(IPath path)
{
if (!(path is Path target) || !(target.Geometry is { } geometry))
{
return;
}
DrawingSession?.DrawGeometry(geometry, StrokeColor, StrokeThickness, StrokeStyle);
DrawingSession?.FillGeometry(geometry, FillColor);
}
public void DrawRectangle(float x, float y, float width, float height)
{
DrawingSession?.DrawRectangle(x, y, width, height, StrokeColor, StrokeThickness, StrokeStyle);
DrawingSession?.FillRectangle(x, y, width, height, FillColor);
}
public void DrawLine(float x1, float y1, float x2, float y2)
{
DrawingSession?.DrawLine(x1, y1, x2, y2, StrokeColor, StrokeThickness, StrokeStyle);
}
public void DrawObject(string url, string mimeType, float x, float y, float width, float height)
{
if (!File.Exists(url))
{
return;
}
var device = DrawingSession?.Device ?? CanvasDevice.GetSharedDevice();
using var image = CanvasBitmap.LoadAsync(device, url).GetAwaiter().GetResult();
DrawingSession?.DrawImage(image, new Rect(x, y, width, height), image.Bounds);
}
public void DrawText(string label, float x, float y, float minX, float minY, float maxX, float maxY)
{
// TODO: don't rely on the max / min values, issue when moving typeset text.
DrawingSession?.DrawText(label, x, y - TextBaseLine, FillColor, TextFormat);
}
public Transform Transform
{
get => _transform ??= Matrix3x2.Identity.ToNative();
set
{
_transform = value;
if (DrawingSession == null)
{
return;
}
DrawingSession.Transform = _transform.ToPlatform();
}
}
private Transform _transform;
#endregion
#region Styles
private Color FillColor { get; set; } = Colors.Black;
private CanvasFilledRegionDetermination FillRule { get; set; } = CanvasFilledRegionDetermination.Winding;
private Color StrokeColor { get; set; } = Colors.Transparent;
private CanvasStrokeStyle StrokeStyle { get; } = new CanvasStrokeStyle();
private float StrokeThickness { get; set; } = 1;
private float TextBaseLine { get; set; } = 1;
private CanvasTextFormat TextFormat { get; } = new CanvasTextFormat();
public void SetStrokeColor(IInk.Graphics.Color color)
{
StrokeColor = color.ToPlatform();
}
public void SetStrokeWidth(float width)
{
StrokeThickness = width;
}
public void SetStrokeLineCap(LineCap lineCap)
{
StrokeStyle.DashCap = StrokeStyle.EndCap = StrokeStyle.StartCap = lineCap.ToPlatform();
}
public void SetStrokeLineJoin(LineJoin lineJoin)
{
StrokeStyle.LineJoin = lineJoin.ToPlatform();
}
public void SetStrokeMiterLimit(float limit)
{
StrokeStyle.MiterLimit = limit;
}
public void SetStrokeDashArray(float[] array)
{
StrokeStyle.CustomDashStyle = array;
}
public void SetStrokeDashOffset(float offset)
{
StrokeStyle.DashOffset = offset;
}
public void SetFillColor(IInk.Graphics.Color color)
{
FillColor = color.ToPlatform();
}
public void SetFillRule(FillRule rule)
{
FillRule = rule.ToPlatform();
}
public void SetFontProperties(string family, float lineHeight, float size, string style, string variant,
int weight)
{
if (DrawingSession == null)
{
return;
}
var fontStyle = Enum.TryParse<FontStyle>(style, true, out var result) ? result : FontStyle.Normal;
TextFormat.FontFamily = family.ToPlatformFontFamily(fontStyle);
TextFormat.FontSize = size;
TextFormat.FontStyle = fontStyle;
TextFormat.FontWeight = weight >= 700 ? FontWeights.Bold :
weight < 400 ? FontWeights.Light : FontWeights.Normal;
using var layout =
new CanvasTextLayout(DrawingSession.Device, "k", TextFormat, float.MaxValue, float.MaxValue);
TextBaseLine = layout.LineMetrics.First().Baseline;
}
#endregion
}
/// <summary>
/// Implements <see cref="ICanvas2" />.
/// <inheritdoc cref="ICanvas2" />
/// </summary>
public sealed partial class Canvas : ICanvas2
{
private CanvasActiveLayer DrawingLayer { get; set; }
public void StartDraw(int x, int y, int width, int height)
{
var style = new Style();
style.SetChangeFlags((uint)StyleFlag.StyleFlag_ALL);
style.ApplyTo(this);
if (DrawingSession != null)
{
DrawingSession.Antialiasing = CanvasAntialiasing.Antialiased;
DrawingSession.TextAntialiasing = CanvasTextAntialiasing.Auto;
}
DrawingLayer = DrawingSession?.CreateLayer(1, new Rect(x, y, width, height));
DrawingSession?.Clear(Colors.Transparent);
}
public void EndDraw()
{
DrawingSession?.Flush();
DrawingLayer?.Dispose();
}
public void BlendOffscreen(uint id,
float srcX, float srcY, float srcWidth, float srcHeight,
float destX, float destY, float destWidth, float destHeight,
IInk.Graphics.Color color)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Implements <see cref="IDisposable" />.
/// <inheritdoc cref="IDisposable" />
/// </summary>
public sealed partial class Canvas : IDisposable
{
public void Dispose()
{
DrawingSession?.Flush();
Layers.Values.Dispose();
Layers.Clear();
DrawingLayer?.Dispose();
DrawingSession?.Dispose();
}
}
}