-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
_canvas_painter.dart
306 lines (268 loc) · 9.34 KB
/
_canvas_painter.dart
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
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart' hide TextStyle;
import 'package:one_dollar_unistroke_recognizer/one_dollar_unistroke_recognizer.dart';
import 'package:path_drawing/path_drawing.dart';
import 'package:perfect_freehand/perfect_freehand.dart';
import 'package:saber/components/canvas/_circle_stroke.dart';
import 'package:saber/components/canvas/_rectangle_stroke.dart';
import 'package:saber/components/canvas/_stroke.dart';
import 'package:saber/components/theming/font_fallbacks.dart';
import 'package:saber/data/editor/page.dart';
import 'package:saber/data/extensions/color_extensions.dart';
import 'package:saber/data/tools/highlighter.dart';
import 'package:saber/data/tools/laser_pointer.dart';
import 'package:saber/data/tools/pencil.dart';
import 'package:saber/data/tools/select.dart';
import 'package:saber/data/tools/shape_pen.dart';
class CanvasPainter extends CustomPainter {
const CanvasPainter({
super.repaint,
this.invert = false,
required this.strokes,
required this.laserStrokes,
required this.currentStroke,
required this.currentSelection,
required this.primaryColor,
required this.page,
required this.showPageIndicator,
required this.pageIndex,
required this.totalPages,
required this.currentScale,
});
final bool invert;
final List<Stroke> strokes;
final List<LaserStroke> laserStrokes;
final Stroke? currentStroke;
final SelectResult? currentSelection;
final Color primaryColor;
final EditorPage page;
final bool showPageIndicator;
final int pageIndex;
final int totalPages;
final double currentScale;
@override
void paint(Canvas canvas, Size size) {
Rect canvasRect = Offset.zero & size;
_drawHighlighterStrokes(canvas, canvasRect);
_drawNonHighlighterStrokes(canvas);
for (final stroke in laserStrokes) _drawLaserStroke(canvas, stroke);
_drawCurrentStroke(canvas);
_drawDetectedShape(canvas);
_drawSelection(canvas);
_drawPageIndicator(canvas, size);
}
@override
bool shouldRepaint(CanvasPainter oldDelegate) {
return currentStroke != null ||
oldDelegate.currentStroke != null ||
strokes.length != oldDelegate.strokes.length;
}
void _drawHighlighterStrokes(Canvas canvas, Rect canvasRect) {
final layerPaint = Paint()
..blendMode = invert ? BlendMode.lighten : BlendMode.darken
..color = Colors.white.withAlpha(Highlighter.alpha);
bool needToRestoreCanvasLayer = false;
Color? lastColor;
for (Stroke stroke in strokes) {
if (stroke.penType != (Highlighter).toString()) continue;
final color = stroke.color.withOpacity(1).withInversion(invert);
if (color != lastColor) {
// new layer for each color
if (needToRestoreCanvasLayer) canvas.restore();
canvas.saveLayer(canvasRect, layerPaint);
needToRestoreCanvasLayer = true;
lastColor = color;
}
canvas.drawPath(_selectPath(stroke), Paint()..color = color);
}
if (needToRestoreCanvasLayer) canvas.restore();
}
void _drawNonHighlighterStrokes(Canvas canvas) {
late final paint = Paint();
for (final stroke in strokes) {
if (stroke.penType == (Highlighter).toString()) continue;
var color = stroke.color.withInversion(invert);
if (currentSelection?.strokes.contains(stroke) ?? false) {
color = Color.lerp(color, primaryColor, 0.5)!;
}
paint.color = color;
paint.shader = null;
paint.maskFilter = null;
if (stroke.penType == (Pencil).toString()) {
paint.color = Colors.white;
paint.shader = page.pencilShader
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
paint.maskFilter = _getPencilMaskFilter(stroke.options.size);
}
late final shapePaint = Paint()
..color = paint.color
..style = PaintingStyle.stroke
..strokeWidth = stroke.options.size;
if (stroke is CircleStroke) {
canvas.drawCircle(
stroke.center,
stroke.radius,
shapePaint,
);
} else if (stroke is RectangleStroke) {
final strokeSize = stroke.options.size;
canvas.drawRRect(
RRect.fromRectAndRadius(
stroke.rect,
Radius.circular(strokeSize / 4),
),
shapePaint,
);
} else if (stroke.length <= 2) {
// a dot
final bounds = stroke.lowQualityPath.getBounds();
final radius = max(bounds.size.width, stroke.options.size) / 2;
canvas.drawCircle(bounds.center, radius, paint);
} else {
canvas.drawPath(_selectPath(stroke), paint);
}
}
}
void _drawCurrentStroke(Canvas canvas) {
if (currentStroke == null) return;
if (currentStroke! is LaserStroke) {
return _drawLaserStroke(canvas, currentStroke as LaserStroke);
}
final color = currentStroke!.color.withInversion(invert);
final paint = Paint();
paint.color = color;
paint.shader = null;
paint.maskFilter = null;
if (currentStroke!.penType == (Pencil).toString()) {
paint.color = Colors.white;
paint.shader = page.pencilShader
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
paint.maskFilter = _getPencilMaskFilter(currentStroke!.options.size);
}
if (currentStroke!.length <= 2) {
// a dot
final bounds = currentStroke!.highQualityPath.getBounds();
final radius = max(
bounds.size.width * 0.5,
currentStroke!.options.size * 0.25,
);
canvas.drawCircle(bounds.center, radius, paint);
} else {
// Current stroke always uses high quality
canvas.drawPath(currentStroke!.highQualityPath, paint);
}
}
void _drawLaserStroke(Canvas canvas, LaserStroke stroke) {
canvas.drawPath(
_selectPath(stroke),
Paint()
..color = stroke.color.withInversion(invert)
..maskFilter = MaskFilter.blur(
BlurStyle.solid,
stroke.options.size * 0.4,
),
);
canvas.drawPath(
stroke.innerPath,
Paint()..color = const Color(0xDDffffff),
);
}
void _drawDetectedShape(Canvas canvas) {
final shape = ShapePen.detectedShape;
if (shape == null) return;
final color = currentStroke?.color.withInversion(invert) ?? Colors.black;
final shapePaint = Paint()
..color = Color.lerp(color, primaryColor, 0.5)!.withOpacity(0.7)
..style = PaintingStyle.stroke
..strokeWidth = currentStroke?.options.size ?? 3;
switch (shape.name) {
case null:
break;
case DefaultUnistrokeNames.line:
var (firstPoint, lastPoint) = shape.convertToLine();
(firstPoint, lastPoint) = Stroke.snapLine(
firstPoint is PointVector
? firstPoint
: PointVector.fromOffset(offset: firstPoint),
lastPoint is PointVector
? lastPoint
: PointVector.fromOffset(offset: lastPoint),
);
canvas.drawLine(firstPoint, lastPoint, shapePaint);
case DefaultUnistrokeNames.rectangle:
final rect = shape.convertToRect();
canvas.drawRect(rect, shapePaint);
case DefaultUnistrokeNames.circle:
final (center, radius) = shape.convertToCircle();
canvas.drawCircle(center, radius, shapePaint);
case DefaultUnistrokeNames.triangle:
case DefaultUnistrokeNames.star:
final polygon = shape.convertToCanonicalPolygon();
canvas.drawPath(
Path()..addPolygon(polygon, true),
shapePaint,
);
}
}
void _drawSelection(Canvas canvas) {
if (currentSelection == null) return;
// draw translucent fill
canvas.drawPath(
currentSelection!.path,
Paint()..color = primaryColor.withOpacity(0.1),
);
// draw dashed stroke
canvas.drawPath(
dashPath(
currentSelection!.path,
dashArray: CircularIntervalList([10, 10]),
),
Paint()
..color = primaryColor
..strokeWidth = 3
..style = PaintingStyle.stroke,
);
}
static const double _pageIndicatorFontSize = 20;
static const double _pageIndicatorPadding = 5;
void _drawPageIndicator(Canvas canvas, Size pageSize) {
if (!showPageIndicator) return;
ParagraphStyle style = ParagraphStyle(
textAlign: TextAlign.end,
textDirection: TextDirection.ltr,
maxLines: 1,
);
ParagraphBuilder builder = ParagraphBuilder(style)
..pushStyle(TextStyle(
color: Colors.black.withInversion(invert).withOpacity(0.5),
fontSize: _pageIndicatorFontSize,
fontFamily: 'Roboto',
fontFamilyFallback: FallbackTextStyle.sansSerifFallbacks,
))
..addText('${pageIndex + 1} / $totalPages');
Paragraph paragraph = builder.build();
paragraph.layout(ParagraphConstraints(
width: pageSize.width - 2 * _pageIndicatorPadding,
));
canvas.drawParagraph(
paragraph,
Offset(
_pageIndicatorPadding,
pageSize.height - _pageIndicatorPadding - _pageIndicatorFontSize * 1.2,
),
);
}
static MaskFilter _getPencilMaskFilter(double size) => MaskFilter.blur(
BlurStyle.normal,
min(size * 0.3, 5),
);
Path _selectPath(Stroke stroke) => switch (currentScale) {
< 1 => stroke.lowQualityPath,
_ => stroke.highQualityPath,
};
}