2026-06-22 02:10:05 +08:00
|
|
|
// test/pen_polish_test.dart
|
|
|
|
|
//
|
|
|
|
|
// Guards the pen-polish work (W1 configurable thinning + W3 tilt):
|
|
|
|
|
// - the on-screen painter (ink_painters.buildStrokePath) and the export/engine
|
|
|
|
|
// path (stroke_geometry.buildStrokeOutline) share ONE thinning default
|
|
|
|
|
// (kDefaultPenThinning) and respond to it identically — the user's core
|
|
|
|
|
// "thinning 写死" complaint, and the single-source-of-truth invariant the
|
|
|
|
|
// Critic required;
|
|
|
|
|
// - tilt survives the PenStroke -> EditorStroke adapter (W3 model fix).
|
|
|
|
|
|
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:badnote/editor/canvas/ink_painters.dart';
|
|
|
|
|
import 'package:badnote/editor/canvas/pen_stroke.dart';
|
2026-06-24 11:13:43 +08:00
|
|
|
import 'package:badnote/editor/engine/brush.dart';
|
2026-06-22 02:10:05 +08:00
|
|
|
import 'package:badnote/editor/engine/stroke_geometry.dart';
|
|
|
|
|
import 'package:badnote/editor/engine/stroke_model.dart';
|
|
|
|
|
|
|
|
|
|
PenStroke _pressuredPen() => PenStroke(
|
|
|
|
|
// Varied pressure, deliberately NOT reaching full force, so that
|
|
|
|
|
// thinning>0 (pressure-modulated, narrower) vs thinning=0 (constant full
|
|
|
|
|
// width) produces a measurably different bounding box.
|
|
|
|
|
points: const [
|
|
|
|
|
PenPoint(0.10, 0.50, 0.10),
|
|
|
|
|
PenPoint(0.30, 0.50, 0.25),
|
|
|
|
|
PenPoint(0.50, 0.50, 0.40),
|
|
|
|
|
PenPoint(0.70, 0.50, 0.55),
|
|
|
|
|
],
|
|
|
|
|
color: 0xFF000000,
|
|
|
|
|
width: 0.01,
|
|
|
|
|
kind: PenStrokeKind.pen,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
const size = Size(1000, 1000);
|
|
|
|
|
|
|
|
|
|
group('W1 — thinning is configurable and single-sourced', () {
|
|
|
|
|
test('buildStrokePath default == explicit kDefaultPenThinning', () {
|
|
|
|
|
final pen = _pressuredPen();
|
|
|
|
|
final byDefault = buildStrokePath(pen, size, isComplete: true);
|
|
|
|
|
final explicit = buildStrokePath(pen, size,
|
|
|
|
|
isComplete: true, thinning: kDefaultPenThinning);
|
|
|
|
|
expect(byDefault.getBounds(), explicit.getBounds());
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-24 11:13:43 +08:00
|
|
|
test('brush thinning actually affects the outline (param is wired)', () {
|
|
|
|
|
// After the brush-engine rebuild each brush owns its perfect_freehand
|
|
|
|
|
// thinning (spec §4): fountainPen = 0.9 (pressure-modulated, pinched
|
|
|
|
|
// middle) vs highlighter = 0.0 (constant full width). The bounding box is
|
|
|
|
|
// thinning-INVARIANT (round caps at full size), so compare the outline
|
|
|
|
|
// PERIMETER, which reflects the pinched middle.
|
2026-06-22 02:10:05 +08:00
|
|
|
double perimeter(Path p) =>
|
|
|
|
|
p.computeMetrics().fold(0.0, (sum, m) => sum + m.length);
|
2026-06-24 11:13:43 +08:00
|
|
|
final pressured = _pressuredPen(); // fountainPen brush, thinning 0.9
|
|
|
|
|
final flat = PenStroke(
|
|
|
|
|
points: _pressuredPen().points,
|
|
|
|
|
color: 0xFF000000,
|
|
|
|
|
width: 0.01,
|
|
|
|
|
kind: PenStrokeKind.highlighter,
|
|
|
|
|
brush: BrushKind.highlighter, // highlighter brush, thinning 0.0
|
|
|
|
|
);
|
|
|
|
|
final strong =
|
|
|
|
|
perimeter(buildStrokePath(pressured, size, isComplete: true));
|
|
|
|
|
final none = perimeter(buildStrokePath(flat, size, isComplete: true));
|
2026-06-22 02:10:05 +08:00
|
|
|
expect((strong - none).abs(), greaterThan(1.0),
|
2026-06-24 11:13:43 +08:00
|
|
|
reason: 'brush thinning had no effect on the outline — not wired');
|
2026-06-22 02:10:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('screen and export builders agree for the same stroke + thinning', () {
|
|
|
|
|
final pen = _pressuredPen();
|
|
|
|
|
final editor = EditorStroke.fromPenStroke(pen);
|
|
|
|
|
for (final thinning in const [0.0, kDefaultPenThinning, 1.0]) {
|
|
|
|
|
final screen =
|
|
|
|
|
buildStrokePath(pen, size, isComplete: true, thinning: thinning)
|
|
|
|
|
.getBounds();
|
|
|
|
|
final export =
|
|
|
|
|
buildStrokeOutline(editor, size, isComplete: true, thinning: thinning)
|
|
|
|
|
.getBounds();
|
|
|
|
|
expect(screen, export,
|
|
|
|
|
reason: 'screen vs export diverged at thinning=$thinning');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
group('W3 — tilt survives the PenStroke adapter', () {
|
|
|
|
|
test('PenPoint.tilt maps into EditorPoint.tilt', () {
|
|
|
|
|
const pen = PenStroke(
|
|
|
|
|
points: [PenPoint(0.1, 0.2, 0.5, tilt: 23.5)],
|
|
|
|
|
color: 0xFF112233,
|
|
|
|
|
width: 0.01,
|
|
|
|
|
kind: PenStrokeKind.pen,
|
|
|
|
|
);
|
|
|
|
|
final editor = EditorStroke.fromPenStroke(pen);
|
|
|
|
|
expect(editor.points.single.tilt, 23.5);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('null tilt stays null through the adapter', () {
|
|
|
|
|
const pen = PenStroke(
|
|
|
|
|
points: [PenPoint(0.1, 0.2, 0.5)],
|
|
|
|
|
color: 0xFF112233,
|
|
|
|
|
width: 0.01,
|
|
|
|
|
kind: PenStrokeKind.pen,
|
|
|
|
|
);
|
|
|
|
|
final editor = EditorStroke.fromPenStroke(pen);
|
|
|
|
|
expect(editor.points.single.tilt, isNull);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|