Files
BadNote/test/export_geometry_test.dart
Akiba So 0feca74278
All checks were successful
CI / Windows build (push) Successful in 14m54s
feat(pen): extensible brush model (4 brushes)
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.
2026-06-24 11:13:43 +08:00

122 lines
3.6 KiB
Dart

// R7 regression (P0 step 7/8): the on-screen painter and the PDF export must
// build their freehand outline from ONE shared recipe. The export bug was
// pdf_service hardcoding `thinning: 0.7, streamline: 0.5` while the screen used
// 0.85 / 0.32 — a hairline mismatch. These tests pin the single source so a
// future edit to one side can't silently re-diverge.
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:perfect_freehand/perfect_freehand.dart' as pf;
import 'package:badnote/editor/engine/brush.dart';
import 'package:badnote/editor/engine/stroke_geometry.dart';
import 'package:badnote/editor/engine/stroke_model.dart';
void main() {
const size = Size(800, 600);
EditorStroke pen() => EditorStroke.create(
id: 'a',
points: const [
EditorPoint(x: 0.1, y: 0.1, pressure: 0.9),
EditorPoint(x: 0.4, y: 0.2, pressure: 0.5),
EditorPoint(x: 0.7, y: 0.15, pressure: 0.2),
],
width: 0.01,
);
test('buildStrokeOutline traces exactly the shared freehandOutlinePoints', () {
final stroke = pen();
final pfPoints = stroke.points
.map((p) => pf.PointVector(
p.x * size.width, p.y * size.height, p.pressure ?? 0.5))
.toList();
final shared = freehandOutlinePoints(
pfPoints: pfPoints,
size: stroke.width * size.width,
isHighlighter: false,
hasRealPressure: true,
isComplete: true,
// buildStrokeOutline now resolves the stroke's brush into the recipe, so
// the reference must pass the SAME brush to still pin "one shared recipe".
brush: brushProfileFor(stroke.brush),
);
expect(shared, isNotEmpty);
final ref = Path()..moveTo(shared.first.dx, shared.first.dy);
for (var i = 1; i < shared.length; i++) {
ref.lineTo(shared[i].dx, shared[i].dy);
}
ref.close();
final screen = buildStrokeOutline(stroke, size, isComplete: true);
// Same vertices ⇒ identical bounds. (If the screen path used different
// options than the shared core, the outline would differ.)
expect(screen.getBounds(), ref.getBounds());
});
test('default thinning IS kDefaultPenThinning (export passes the default)', () {
final pts = [
pf.PointVector(10, 10, 0.9),
pf.PointVector(100, 40, 0.5),
pf.PointVector(200, 30, 0.2),
];
final byDefault = freehandOutlinePoints(
pfPoints: pts,
size: 8,
isHighlighter: false,
hasRealPressure: true,
isComplete: true,
);
final explicit = freehandOutlinePoints(
pfPoints: pts,
size: 8,
isHighlighter: false,
hasRealPressure: true,
isComplete: true,
thinning: kDefaultPenThinning,
);
expect(byDefault, explicit);
});
test('thinning actually changes the outline (param is wired, not ignored)',
() {
final pts = [
pf.PointVector(10, 10, 0.9),
pf.PointVector(100, 40, 0.5),
pf.PointVector(200, 30, 0.2),
];
final strong = freehandOutlinePoints(
pfPoints: pts,
size: 8,
isHighlighter: false,
hasRealPressure: true,
isComplete: true,
thinning: kDefaultPenThinning,
);
final none = freehandOutlinePoints(
pfPoints: pts,
size: 8,
isHighlighter: false,
hasRealPressure: true,
isComplete: true,
thinning: 0.0,
);
expect(strong, isNot(equals(none)));
});
test('empty input yields an empty outline (no crash)', () {
expect(
freehandOutlinePoints(
pfPoints: const [],
size: 8,
isHighlighter: false,
hasRealPressure: false,
isComplete: true,
),
isEmpty,
);
});
}