Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .reviewmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ reviews:
- "test/DemaConsulting.CanvasNet.Tests/Drawing/EdgeFlattenerTests.cs" # unit tests
- "test/DemaConsulting.CanvasNet.Tests/Drawing/ScanlineRasterizerTests.cs" # unit tests

# CanvasNet-Drawing-PathStroker Review (one per unit)
- id: CanvasNet-Drawing-PathStroker
title: Review that CanvasNet Drawing PathStroker Implementation is Correct
context:
- "docs/design/canvas-net.md"
- "docs/reqstream/canvas-net.yaml"
- "docs/design/canvas-net/drawing.md"
- "docs/reqstream/canvas-net/drawing.yaml"
paths:
- "docs/reqstream/canvas-net/drawing/path-stroker.yaml" # unit requirements
- "docs/design/canvas-net/drawing/path-stroker.md" # unit design
- "docs/verification/canvas-net/drawing/path-stroker.md" # unit verification
- "src/DemaConsulting.CanvasNet/Drawing/LineCap.cs" # supporting enum source
- "src/DemaConsulting.CanvasNet/Drawing/LineJoin.cs" # supporting enum source
- "src/DemaConsulting.CanvasNet/Drawing/StrokeStyle.cs" # public style source
- "src/DemaConsulting.CanvasNet/Drawing/StrokePathFlattener.cs" # internal helper source
- "src/DemaConsulting.CanvasNet/Drawing/DashSplitter.cs" # internal helper source
- "src/DemaConsulting.CanvasNet/Drawing/StrokeOutliner.cs" # internal helper source
- "src/DemaConsulting.CanvasNet/Drawing/PathStroker.cs" # unit source
- "src/DemaConsulting.CanvasNet/Drawing/NamespaceDoc.cs" # Drawing namespace documentation
- "test/DemaConsulting.CanvasNet.Tests/Drawing/StrokeStyleTests.cs" # unit tests
- "test/DemaConsulting.CanvasNet.Tests/Drawing/StrokePathFlattenerTests.cs" # unit tests
- "test/DemaConsulting.CanvasNet.Tests/Drawing/DashSplitterTests.cs" # unit tests
- "test/DemaConsulting.CanvasNet.Tests/Drawing/StrokeOutlinerTests.cs" # unit tests
- "test/DemaConsulting.CanvasNet.Tests/Drawing/PathStrokerTests.cs" # unit tests

# OTS-BuildMark Review (one per OTS item)
- id: OTS-BuildMark
title: Review that BuildMark Provides Required Functionality
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ image operations using `Span<T>`, and supports independent-copy cropping for loa
- 🔍 **Header-Only Probing** - `GetInfo` reads only image headers (dimensions/channels/alpha) without
decoding pixel data, letting callers triage untrusted files before calling `Load`
- 🖌️ **Path Filling** - Antialiased nonzero/even-odd fill of closed vector paths onto a `Surface`
- 🖊️ **Stroke-to-Fill** - Convert stroked vector paths (caps, joins, dashes, miter limits) into
fillable outline geometry and render them through the same antialiased fill pipeline
- ⚡ **Span-Based** - Fast, allocation-conscious row and pixel access
- 🔄 **Multi-Target** - Supports .NET 8, 9, and 10
- 📦 **NuGet Ready** - Easy integration via NuGet package
Expand Down Expand Up @@ -103,6 +105,31 @@ var triangle = new PathBuilder()
PathFiller.Fill(canvas, triangle, new Rgba32(0, 128, 255, 255)); // antialiased solid fill
```

Stroking a vector path onto a surface:

```csharp
using DemaConsulting.CanvasNet.Canvas;
using DemaConsulting.CanvasNet.Drawing;
using DemaConsulting.CanvasNet.Geometry;
using System.Numerics;

var canvas = new Surface(64, 64);
var polyline = new PathBuilder()
.MoveTo(new Vector2(8, 48))
.LineTo(new Vector2(32, 16))
.LineTo(new Vector2(56, 48))
.Build();

var style = new StrokeStyle(
width: 6f,
cap: LineCap.Round,
join: LineJoin.Round,
dashArray: [10f, 6f]);

var strokedOutline = PathStroker.Stroke(polyline, style);
PathFiller.Fill(canvas, strokedOutline, new Rgba32(255, 128, 0, 255));
```

## Building

```pwsh
Expand Down
77 changes: 57 additions & 20 deletions docs/design/canvas-net.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ DEMA Consulting best practices. The system consists of four implemented subsyste
or `Codecs`, and neither of those subsystems depends on `Geometry`. See
_Geometry Subsystem Design_ (`geometry.md`).
- **Drawing subsystem** (namespace `DemaConsulting.CanvasNet.Drawing`, folder
`src/DemaConsulting.CanvasNet/Drawing/`, flat — no further nesting): an antialiased
scanline-coverage fill rasterizer for closed `Geometry.Path` geometry with solid-color paint —
the `PathFiller` unit (a public static `Fill` entry point, with the supporting `FillRule` enum
and the internal `EdgeFlattener`/`ScanlineRasterizer` helpers documented inline). `Drawing`
consumes both the `Canvas` subsystem's `Surface` unit (via `Surface.CompositeOverSpan`) and the
`Geometry` subsystem's `Path`/`BezierFlattening`/`SvgArcConverter` units; neither `Canvas` nor
`Geometry` depends on `Drawing`. `Geometry` is deliberately distinct from `Drawing`: `Geometry`
describes shape geometry (paths, bounds, curve math) with no notion of pixels, color, or
rasterization, while `Drawing` turns that geometry into pixels. Strokes, gradients, and fonts
are reserved for later phases. See _Drawing Subsystem Design_ (`drawing.md`).
`src/DemaConsulting.CanvasNet/Drawing/`, flat — no further nesting): solid-color vector
rendering for `Geometry.Path` geometry through two public entry points — the `PathFiller` unit
(a public static `Fill` entry point, with the supporting `FillRule` enum and the internal
`EdgeFlattener`/`ScanlineRasterizer` helpers documented inline) and the `PathStroker` unit (a
public static `Stroke` entry point, with the supporting `LineCap`/`LineJoin`/`StrokeStyle`
types and the internal `StrokePathFlattener`/`DashSplitter`/`StrokeOutliner` helpers documented
inline). `Drawing` consumes both the `Canvas` subsystem's `Surface` unit (via
`Surface.CompositeOverSpan`) and the `Geometry` subsystem's `Path`/`PathBuilder`/
`BezierFlattening`/`SvgArcConverter` units; neither `Canvas` nor `Geometry` depends on
`Drawing`. `Geometry` is deliberately distinct from `Drawing`: `Geometry` describes shape
geometry (paths, bounds, curve math) with no notion of pixels, color, or rasterization, while
`Drawing` turns that geometry into pixels. Gradients and fonts are reserved for later phases.
See _Drawing Subsystem Design_ (`drawing.md`).

The `Codecs` subsystem depends on the `Canvas` subsystem's `Surface` unit (constructing surfaces
and reading/writing rows via `Surface.GetRowSpanBytes`); the `Canvas` subsystem has no dependency
Expand Down Expand Up @@ -71,15 +74,20 @@ _Path Unit Design_ (`geometry/path.md`), _BezierFlattening Unit Design_
(`geometry/bezier-flattening.md`), and _SvgArcConverter Unit Design_
(`geometry/svg-arc-converter.md`) for each unit's internal collaboration.

The `Drawing` subsystem's single unit, `PathFiller`, collaborates as follows: `PathFiller.Fill`
first delegates to the internal `EdgeFlattener` (which converts the target `Geometry.Path`'s
subpaths to closed polygons, flattening curves via `Geometry.BezierFlattening` and arcs via
The `Drawing` subsystem's two units collaborate as follows. `PathFiller.Fill` first delegates to
the internal `EdgeFlattener` (which converts the target `Geometry.Path`'s subpaths to closed
polygons, flattening curves via `Geometry.BezierFlattening` and arcs via
`Geometry.SvgArcConverter`), then computes the flattened polygons' bounds and intersects them with
the `Canvas.Surface`'s pixel extent (a no-op if the path is empty or the intersection is empty),
then delegates to the internal `ScanlineRasterizer` (which rasterizes those polygons into per-row
antialiased coverage and composites each row directly via `Canvas.Surface.CompositeOverSpan`). See
_Drawing Subsystem Design_ (`drawing.md`) and _PathFiller Unit Design_
(`drawing/path-filler.md`) for full detail.
antialiased coverage and composites each row directly via `Canvas.Surface.CompositeOverSpan`).
`PathStroker.Stroke` runs earlier in the pipeline: it flattens each source subpath while
preserving `Subpath.IsClosed`, applies optional dash-array and dash-offset semantics, offsets the
visible polyline segments into closed outline polygons with cap and join geometry, and assembles
those polygons into a new `Geometry.Path` via `PathBuilder`; callers render the returned outline
path through `PathFiller.Fill`. See _Drawing Subsystem Design_ (`drawing.md`), _PathFiller Unit
Design_ (`drawing/path-filler.md`), and _PathStroker Unit Design_ (`drawing/path-stroker.md`) for
full detail.

## External Interfaces

Expand Down Expand Up @@ -191,6 +199,16 @@ The system exposes the following public API to external consumers:
Throws `ArgumentNullException` for a null `surface`/`path`, and
`ArgumentOutOfRangeException` for an undefined `fillRule` value or a non-finite or
non-positive `flattenTolerance`.
- **LineCap** / **LineJoin**: Public enums selecting stroke end-cap and corner-join geometry.
- **StrokeStyle(float width, LineCap cap, LineJoin join, float miterLimit, IReadOnlyList<float>? dashArray, float dashOffset)**:
Immutable public stroke-style snapshot. Throws `ArgumentOutOfRangeException` for invalid width,
enum, miter-limit, or dash-offset values, and `ArgumentException` for invalid dash-array
entries.
- **PathStroker.Stroke(Path, StrokeStyle, float)**: Converts a `Path` plus `StrokeStyle` into a
new closed-outline `Path` suitable for filling with `FillRule.NonZero`. No-ops by returning
`Path.Empty` when the stroke contributes no visible area. Throws `ArgumentNullException` for a
null `path`/`style`, and `ArgumentOutOfRangeException` for a non-finite or non-positive
`flattenTolerance`.

| Interface | Direction | Format | Constraints |
| -------------------------------- | ---------------- | ------------------------------ | ----------------------------- |
Expand Down Expand Up @@ -221,6 +239,8 @@ The system exposes the following public API to external consumers:
| `BezierFlattening.Flatten*(...)` | Inbound/Outbound | Method call / list append | `tolerance` greater than zero |
| `SvgArcConverter.ToBeziers(...)` | Inbound/Outbound | Method call / list append | None |
| `PathFiller.Fill(...)` | Inbound | Method call | Non-null; fillRule ok; tol>0 |
| `StrokeStyle(...)` | Inbound | Constructor call | Width > 0; valid style data |
| `PathStroker.Stroke(...)` | Inbound/Outbound | Method call / `Path` return | Non-null; tol > 0 |

## Dependencies

Expand All @@ -238,10 +258,12 @@ subsystem introduces zero new runtime NuGet dependencies: it is implemented enti
`System.Numerics.Vector2` and `System.Numerics.Matrix3x2`, which are in-box BCL types available
natively on every target framework this library supports (net8.0, net9.0, net10.0) — no custom
point/vector wrapper types were introduced. The `Drawing` subsystem likewise introduces zero new
runtime NuGet dependencies: `PathFiller`, `FillRule`, `EdgeFlattener`, and `ScanlineRasterizer`
are implemented entirely against `System.Numerics.Vector2`, in-box `List<T>`/array types, and the
existing `Geometry` and `Canvas` subsystem APIs (`Path`, `BezierFlattening`, `SvgArcConverter`,
`Surface.CompositeOverSpan`) — no new package reference was added to the project file. The
runtime NuGet dependencies: `PathFiller`, `FillRule`, `EdgeFlattener`, `ScanlineRasterizer`,
`PathStroker`, `LineCap`, `LineJoin`, `StrokeStyle`, `StrokePathFlattener`, `DashSplitter`, and
`StrokeOutliner` are implemented entirely against `System.Numerics.Vector2`, in-box `List<T>`/
array types, and the existing `Geometry` and `Canvas` subsystem APIs (`Path`, `PathBuilder`,
`BezierFlattening`, `SvgArcConverter`, `Surface.CompositeOverSpan`) — no new package reference
was added to the project file. The
following OTS
items are used for building and verifying this system (not consumed at runtime); see
_OTS Integration Design_ (`docs/design/ots.md`) and each item's dedicated design document for
Expand Down Expand Up @@ -393,6 +415,20 @@ measures (IEC 62304 §5.3.3).
pixels into the destination `Surface`
4. **Output**: A new `Surface` containing the decoded pixels

**Path stroke conversion path:**

1. **Input**: Method parameters `path`, `style`, and optional `flattenTolerance`
2. **Validation**: `PathStroker.Stroke` rejects a null `path` or `style` with
`ArgumentNullException`, and rejects a non-positive or non-finite
`flattenTolerance` with `ArgumentOutOfRangeException`; `StrokeStyle` itself already
validated width, cap, join, miter limit, dash array, and dash offset at construction time
3. **Processing**: Each subpath is flattened while preserving open/closed state, optional dashes
are split into visible runs, each visible run is converted into one or more closed outline
polygons with cap/join geometry, and those polygons are assembled into a new `Path` via
`PathBuilder`
4. **Output**: A new closed-outline `Path` suitable for rendering through `PathFiller.Fill` with
`FillRule.NonZero`

## Design Constraints

- **Simplicity**: Minimal functionality kept easy to understand and extend
Expand Down Expand Up @@ -427,3 +463,4 @@ APIs, or framework-version-specific features are used.
- **CI/CD Integration**: Automated build, test, and quality validation
- **Requirements Traceability**: All features linked to passing tests
- **Review Management**: Systematic file review using ReviewMark patterns
<!-- cspell:ignore Outliner -->
56 changes: 34 additions & 22 deletions docs/design/canvas-net/drawing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

![Drawing Structure](DrawingView.svg)

The `Drawing` subsystem is the fourth software subsystem in CanvasNet. It provides an
antialiased scanline-coverage fill rasterizer for closed `Geometry.Path` geometry with
solid-color paint (`PathFiller`), consuming both the `Canvas` subsystem's `Surface` unit and the
`Geometry` subsystem's vector primitives.
The `Drawing` subsystem is the fourth software subsystem in CanvasNet. It provides solid-color
vector rendering for `Geometry.Path` geometry through two public entry points:
direct path filling (`PathFiller`) and stroke-to-fill conversion (`PathStroker`). It consumes
both the `Canvas` subsystem's `Surface` unit and the `Geometry` subsystem's vector primitives.

### Purpose

The `Drawing` subsystem groups the software units responsible for turning vector geometry into
rendered pixels on a `Canvas.Surface`. It has exactly one unit this phase, `PathFiller`, which
fills a closed `Geometry.Path` with a single solid color using an antialiased
signed-area/coverage-accumulation scanline algorithm supporting both nonzero and even-odd fill
rules. `Geometry` is deliberately distinct from `Drawing`: `Geometry` describes shape geometry
(paths, bounds, curve math) with no notion of pixels, color, or rasterization; `Drawing` is where
that geometry is turned into pixels. `Drawing` depends on both `Canvas` (`Surface`,
`Surface.CompositeOverSpan`) and `Geometry` (`Path`, `BezierFlattening`, `SvgArcConverter`);
neither `Canvas` nor `Geometry` has any dependency on `Drawing`. Strokes (outlining a path rather
than filling it), gradients (multi-color paint), and fonts/text rendering are all reserved for
later phases and are not implemented by this subsystem yet — there is deliberate room for future
sibling units (for example a future `PathStroker` unit) alongside `PathFiller` within this same
subsystem.
rendered pixels on a `Canvas.Surface`. It currently contains two sibling units:
`PathFiller`, which fills a closed `Geometry.Path` with a single solid color using an
antialiased signed-area/coverage-accumulation scanline algorithm supporting both nonzero and
even-odd fill rules; and `PathStroker`, which converts a path stroke (configured by width, cap,
join, miter limit, and optional dashing) into closed outline geometry that `PathFiller` can
render unchanged. `Geometry` is deliberately distinct from `Drawing`: `Geometry` describes shape
geometry (paths, bounds, curve math) with no notion of pixels, color, or rasterization;
`Drawing` is where that geometry is turned into pixels. `Drawing` depends on both `Canvas`
(`Surface`, `Surface.CompositeOverSpan`) and `Geometry` (`Path`, `PathBuilder`,
`BezierFlattening`, `SvgArcConverter`); neither `Canvas` nor `Geometry` has any dependency on
`Drawing`. Gradients (multi-color paint) and fonts/text rendering remain reserved for later
phases.

### Units

Expand All @@ -31,6 +31,11 @@ subsystem.
`EdgeFlattener`/`ScanlineRasterizer` helpers, all documented inline within the `PathFiller`
unit design rather than as their own units, because none of them has any independent behavior
beyond supporting `PathFiller.Fill`; see _PathFiller Unit Design_ (`drawing/path-filler.md`)
- **PathStroker** — a public static `Stroke` entry point that converts a `Path` centerline plus a
`StrokeStyle` into one or more closed outline polygons expressed as a new `Path`, together with
the supporting public `LineCap`, `LineJoin`, and `StrokeStyle` types and the internal
`StrokePathFlattener`/`DashSplitter`/`StrokeOutliner` helpers, all documented inline within the
`PathStroker` unit design; see _PathStroker Unit Design_ (`drawing/path-stroker.md`)

### Dependencies

Expand All @@ -41,12 +46,19 @@ reference was introduced. Within the subsystem, `PathFiller.Fill` depends on the
`EdgeFlattener` (to convert a `Path`'s subpaths to closed polygons, itself depending on
`Geometry.BezierFlattening` and `Geometry.SvgArcConverter` to flatten curves and arcs) and the
internal `ScanlineRasterizer` (to rasterize those polygons into per-row antialiased coverage and
composite each row via `Canvas.Surface.CompositeOverSpan`).
composite each row via `Canvas.Surface.CompositeOverSpan`). `PathStroker.Stroke` depends on the
internal `StrokePathFlattener` (to flatten each subpath while preserving `Subpath.IsClosed`),
`DashSplitter` (to apply dash-array and dash-offset semantics), and `StrokeOutliner` (to offset
segments and resolve joins/caps into closed polygons), plus `Geometry.PathBuilder` to assemble the
returned outline path.

### Callers

`PathFiller.Fill` is a public API entry point, invoked externally by consumers of the CanvasNet
package. It is exercised end to end by this subsystem's own unit tests, and by a
system-integration test that builds a `Path` via `PathBuilder` and fills it onto a `Surface` (see
`CanvasNetTests.cs`). The `Drawing` subsystem itself has no dependency on `Codecs`; it depends on
`Canvas` and `Geometry` as described above.
`PathFiller.Fill` and `PathStroker.Stroke` are public API entry points, invoked externally by
consumers of the CanvasNet package. `PathFiller` is exercised end to end by this subsystem's own
unit tests and by a system-integration test that builds a `Path` via `PathBuilder` and fills it
onto a `Surface` (see `CanvasNetTests.cs`). `PathStroker` is exercised end to end by its own unit
tests, which stroke a path, fill the returned outline path, and inspect the resulting pixels. The
`Drawing` subsystem itself has no dependency on `Codecs`; it depends on `Canvas` and `Geometry`
as described above.
<!-- cspell:ignore Outliner -->
Loading
Loading