All checks were successful
CI / Windows build (push) Successful in 14m54s
Replace the 2-tool ink system with a data-driven, Krita-style BrushProfile (lib/editor/engine/brush.dart). Adding a brush is a const map entry, not render-path branching. Four presets from the rnote/krita spec: - fountain pen: quadratic (p^2) pressure, wide dynamic width - ballpoint: near-constant width (thinning 0.15) - highlighter: flat width, square caps - pencil: sqrt(p) pressure, moderate width Pressure is pre-warped per brush via PressureCurve(gamma) before perfect_freehand; geometry fields (thinning/streamline/smoothing/ caps) flow through the shared stroke recipe so the PDF overlay and the note/slide PenCanvas both honor the brush. Brush kind is now persisted on the stroke model. Picker added to all three toolbars. Opacity/multiply and pencil grain are carried as data but not yet composited (TODO brush-opacity / brush-texture); this increment is width + pressure-curve differentiation. analyze clean, 283 tests.
198 lines
6.6 KiB
Dart
198 lines
6.6 KiB
Dart
// lib/editor/engine/stroke_model.dart
|
|
//
|
|
// Canonical, persistable stroke model for the BadNote editor engine.
|
|
//
|
|
// This is the single source of truth for ink strokes across the new own-canvas
|
|
// engine (screen render + export + persistence). It is a deliberate SUPERSET of
|
|
// both the in-memory live `PenStroke`/`PenPoint` (lib/editor/canvas/pen_stroke.dart)
|
|
// and the freezed/JSON `InkStroke`/`InkPoint` (lib/models/ink_stroke.dart) so the
|
|
// adapters below round-trip losslessly with `InkStroke` (SF1): `tilt`,
|
|
// `timestamp` and `pointerDeviceKind` are preserved, never dropped.
|
|
//
|
|
// Coordinate semantics (matching the live conventions):
|
|
// * Point x/y are NORMALIZED to the page rectangle, i.e. in [0,1].
|
|
// * Stroke `width` is a FRACTION of the page width, so it scales with zoom.
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../../models/ink_point.dart';
|
|
import '../../models/ink_stroke.dart';
|
|
import '../../models/pen_tool.dart';
|
|
import '../../models/pointer_device_kind.dart';
|
|
import '../canvas/pen_stroke.dart';
|
|
import 'brush.dart';
|
|
|
|
part 'stroke_model.freezed.dart';
|
|
part 'stroke_model.g.dart';
|
|
|
|
const _uuid = Uuid();
|
|
|
|
/// The drawing tools the engine knows about. Extensible; P0 uses these three.
|
|
enum EditorTool {
|
|
@JsonValue('pen')
|
|
pen,
|
|
@JsonValue('highlighter')
|
|
highlighter,
|
|
@JsonValue('eraser')
|
|
eraser,
|
|
}
|
|
|
|
/// A single captured sample of a stroke.
|
|
///
|
|
/// [x]/[y] are normalized to the page rectangle ([0,1]). The remaining fields
|
|
/// are a superset of [InkPoint] (nullable here so the live capture path can
|
|
/// leave them unset, while [InkStroke] data round-trips intact through the
|
|
/// adapters below).
|
|
@freezed
|
|
abstract class EditorPoint with _$EditorPoint {
|
|
const factory EditorPoint({
|
|
required double x,
|
|
required double y,
|
|
double? pressure,
|
|
double? tilt,
|
|
int? timestamp,
|
|
InputDeviceKind? pointerDeviceKind,
|
|
}) = _EditorPoint;
|
|
|
|
factory EditorPoint.fromJson(Map<String, dynamic> json) =>
|
|
_$EditorPointFromJson(json);
|
|
}
|
|
|
|
/// A committed stroke in normalized page coordinates.
|
|
///
|
|
/// [width] is a fraction of page width (matches live `PenStroke.width`).
|
|
@freezed
|
|
abstract class EditorStroke with _$EditorStroke {
|
|
const EditorStroke._();
|
|
|
|
factory EditorStroke({
|
|
required String id,
|
|
required List<EditorPoint> points,
|
|
@Default(EditorTool.pen) EditorTool tool,
|
|
@Default(0xFF000000) int color,
|
|
@Default(0.003) double width,
|
|
@Default(false) bool filled,
|
|
String? textContent,
|
|
@Default(14.0) double fontSize,
|
|
// Brush the stroke was drawn with. NOT serialized (increment 1 keeps brush
|
|
// out of persistence / InkStroke round-trip — see TODO(brush-persist)); it
|
|
// is an IN-MEMORY render hint only, so the committed render path can resolve
|
|
// each stroke's perfect_freehand geometry. Defaults to fountainPen so loaded
|
|
// (deserialized) strokes keep the legacy pen visual.
|
|
// ignore: invalid_annotation_target
|
|
@JsonKey(includeFromJson: false, includeToJson: false)
|
|
@Default(BrushKind.fountainPen)
|
|
BrushKind brush,
|
|
}) = _EditorStroke;
|
|
|
|
/// Convenience constructor that generates a uuid [id] when none is supplied.
|
|
factory EditorStroke.create({
|
|
String? id,
|
|
required List<EditorPoint> points,
|
|
EditorTool tool = EditorTool.pen,
|
|
int color = 0xFF000000,
|
|
double width = 0.003,
|
|
bool filled = false,
|
|
String? textContent,
|
|
double fontSize = 14.0,
|
|
BrushKind brush = BrushKind.fountainPen,
|
|
}) =>
|
|
EditorStroke(
|
|
id: id ?? _uuid.v4(),
|
|
points: points,
|
|
tool: tool,
|
|
color: color,
|
|
width: width,
|
|
filled: filled,
|
|
textContent: textContent,
|
|
fontSize: fontSize,
|
|
brush: brush,
|
|
);
|
|
|
|
factory EditorStroke.fromJson(Map<String, dynamic> json) =>
|
|
_$EditorStrokeFromJson(json);
|
|
|
|
// ---- Adapters -----------------------------------------------------------
|
|
|
|
/// Adapts an in-memory live [PenStroke] (normalized; carries tilt when the
|
|
/// native pen plugin supplied it, else null; no timestamp/pointerDeviceKind).
|
|
factory EditorStroke.fromPenStroke(PenStroke stroke, {String? id}) =>
|
|
EditorStroke(
|
|
id: id ?? _uuid.v4(),
|
|
points: stroke.points
|
|
.map((p) =>
|
|
EditorPoint(x: p.x, y: p.y, pressure: p.pressure, tilt: p.tilt))
|
|
.toList(),
|
|
tool: switch (stroke.kind) {
|
|
PenStrokeKind.pen => EditorTool.pen,
|
|
PenStrokeKind.highlighter => EditorTool.highlighter,
|
|
},
|
|
color: stroke.color,
|
|
width: stroke.width,
|
|
brush: stroke.brush,
|
|
);
|
|
|
|
/// Lossless adapter from the freezed/JSON [InkStroke] model.
|
|
factory EditorStroke.fromInkStroke(InkStroke stroke) => EditorStroke(
|
|
id: stroke.id,
|
|
points: stroke.points
|
|
.map(
|
|
(p) => EditorPoint(
|
|
x: p.x,
|
|
y: p.y,
|
|
pressure: p.pressure,
|
|
tilt: p.tilt,
|
|
timestamp: p.timestamp,
|
|
pointerDeviceKind: p.pointerDeviceKind,
|
|
),
|
|
)
|
|
.toList(),
|
|
tool: _toolFromPenTool(stroke.tool),
|
|
color: stroke.color,
|
|
width: stroke.strokeWidth,
|
|
filled: stroke.filled,
|
|
textContent: stroke.textContent,
|
|
fontSize: stroke.fontSize,
|
|
);
|
|
|
|
/// Lossless adapter to the freezed/JSON [InkStroke] model. Null superset
|
|
/// fields fall back to [InkPoint]'s own defaults so the InkStroke round-trip
|
|
/// (fromInkStroke → toInkStroke) reproduces the original exactly.
|
|
InkStroke toInkStroke({DateTime? createdAt}) => InkStroke(
|
|
id: id,
|
|
points: points
|
|
.map(
|
|
(p) => InkPoint(
|
|
x: p.x,
|
|
y: p.y,
|
|
pressure: p.pressure ?? 0.5,
|
|
tilt: p.tilt ?? 0.0,
|
|
timestamp: p.timestamp ?? 0,
|
|
pointerDeviceKind:
|
|
p.pointerDeviceKind ?? InputDeviceKind.unknown,
|
|
),
|
|
)
|
|
.toList(),
|
|
tool: _toolToPenTool(tool),
|
|
color: color,
|
|
strokeWidth: width,
|
|
createdAt: createdAt ?? DateTime.fromMillisecondsSinceEpoch(0),
|
|
filled: filled,
|
|
textContent: textContent,
|
|
fontSize: fontSize,
|
|
);
|
|
|
|
static EditorTool _toolFromPenTool(PenTool tool) => switch (tool) {
|
|
PenTool.highlighter => EditorTool.highlighter,
|
|
PenTool.eraser => EditorTool.eraser,
|
|
_ => EditorTool.pen,
|
|
};
|
|
|
|
static PenTool _toolToPenTool(EditorTool tool) => switch (tool) {
|
|
EditorTool.pen => PenTool.pen,
|
|
EditorTool.highlighter => PenTool.highlighter,
|
|
EditorTool.eraser => PenTool.eraser,
|
|
};
|
|
}
|