fix(export): single-source freehand recipe screen+export (P0 step 7, R7)

pdf_service._buildFreehandPdfPath hardcoded its OWN StrokeOptions
(thinning:0.7, streamline:0.5) instead of the shared geometry — the R7
hairline-export divergence. The prior pen-feel commit (streamline 0.5→0.32 on
screen) widened the gap: export still rendered at 0.5.

Extract the ONE perfect_freehand recipe into stroke_geometry.freehandOutlinePoints
(owns thinning/smoothing/streamline/simulatePressure). buildStrokeOutline (screen)
and pdf_service (export, via InkStroke→pfPoints) now both call it, so the
StrokeOptions live in exactly one place and screen↔export can't drift again.
Export now matches screen: thinning 0.85 (kDefaultPenThinning), streamline 0.32.

test/export_geometry_test.dart pins it: buildStrokeOutline traces exactly the
shared outline; default thinning == kDefaultPenThinning; thinning is wired;
empty input is safe.

flutter analyze lib/editor clean; 78/78 tests pass (+4).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-23 02:50:53 +08:00
parent a48c0e7e56
commit d50087247c
3 changed files with 173 additions and 33 deletions

View File

@@ -32,6 +32,45 @@ const double kDefaultPenThinning = 0.85;
const double kPenStreamline = 0.32;
const double kPenSmoothing = 0.5;
/// THE single perfect_freehand outline recipe — the raw outline points for a
/// stroke. Both the on-screen painter ([buildStrokeOutline] / the live
/// `ink_painters.buildStrokePath`) and the PDF export
/// (`pdf_service._buildFreehandPdfPath`) call THIS, so the `StrokeOptions`
/// (thinning / smoothing / streamline / simulatePressure) live in exactly one
/// place and screen↔export can never drift again (R7 — the hairline-export bug
/// was pdf_service hardcoding its own `thinning: 0.7, streamline: 0.5`).
///
/// Callers supply already-pixel-scaled [pfPoints] (because the two stroke
/// models scale differently) plus the per-stroke flags. Returns the closed
/// outline as `List<Offset>` (perfect_freehand 2.x); empty when freehand
/// produces nothing.
List<Offset> freehandOutlinePoints({
required List<pf.PointVector> pfPoints,
required double size,
required bool isHighlighter,
required bool hasRealPressure,
required bool isComplete,
double thinning = kDefaultPenThinning,
}) {
if (pfPoints.isEmpty) return const <Offset>[];
return pf.getStroke(
pfPoints,
options: pf.StrokeOptions(
size: size,
// Highlighter keeps a constant width (no thinning); pen uses the
// configurable [thinning] so Surface-Pen pressure changes width.
thinning: isHighlighter ? 0.0 : thinning,
smoothing: kPenSmoothing,
streamline: kPenStreamline,
// Real stylus pressure -> don't simulate; no pressure -> let freehand
// fake it based on velocity (highlighter never simulates). perfect_freehand
// 2.x honors real pressure when simulatePressure is false.
simulatePressure: !hasRealPressure && !isHighlighter,
isComplete: isComplete,
),
);
}
/// Builds a closed, fillable outline [Path] for one [stroke], scaled into the
/// pixel space of [pageSize] (which maps normalized [0,1] coords to pixels).
///
@@ -52,11 +91,6 @@ Path buildStrokeOutline(
final path = Path();
if (stroke.points.isEmpty) return path;
final pixelWidth = stroke.width * pageSize.width;
final hasRealPressure = stroke.points.any((p) => p.pressure != null);
final isHighlighter = stroke.tool == EditorTool.highlighter;
final pfPoints = stroke.points
.map(
(p) => pf.PointVector(
@@ -67,21 +101,13 @@ Path buildStrokeOutline(
)
.toList();
final outline = pf.getStroke(
pfPoints,
options: pf.StrokeOptions(
size: pixelWidth,
// Highlighter keeps a constant width (no thinning); pen uses the
// configurable [thinning] so Surface-Pen pressure changes width.
thinning: isHighlighter ? 0.0 : thinning,
smoothing: kPenSmoothing,
streamline: kPenStreamline,
// Real stylus pressure -> don't simulate; no pressure -> let freehand
// fake it based on velocity (highlighter never simulates). perfect_freehand
// 2.x honors real pressure when simulatePressure is false.
simulatePressure: !hasRealPressure && !isHighlighter,
isComplete: isComplete,
),
final outline = freehandOutlinePoints(
pfPoints: pfPoints,
size: stroke.width * pageSize.width,
isHighlighter: stroke.tool == EditorTool.highlighter,
hasRealPressure: stroke.points.any((p) => p.pressure != null),
isComplete: isComplete,
thinning: thinning,
);
if (outline.isEmpty) return path;