Repository navigation
SVG rendering proposal #435
Replies: 1 comment
|
Hey @sebastienros! Thanks for the write-up. SVG rendering on top of the canvas would be great to have. You called out two changes the canvas needs, and I've made both of them. They're merged into Gradient interpolation
Both the CPU and the WebGPU backends use it. // PixelAlphaRepresentation.Unassociated interpolates the stop colors without premultiplication,
// as SVG requires. Opaque red to transparent blue shows purple between the two stops.
LinearGradientBrush brush = new(
new PointF(0, 0),
new PointF(200, 0),
GradientRepetitionMode.None,
Matrix4x4.Identity,
PixelAlphaRepresentation.Unassociated,
new ColorStop(0, Color.Red),
new ColorStop(1, Color.Blue.WithAlpha(0)));Strokes under non-uniform scale or skew I didn't add a user-space stroke mode to
// Fill(Pen, IPath) strokes in user space: the drawing transform scales and skews the stroke
// outline as it does every filled shape. Draw(Pen, IPath) keeps an even stroke width.
canvas.Fill(pen, path);Your SVG emitter can call API shape The load and draw split looks good to me. One change: please make the option types init-only, as in the rest of our newer API: public sealed class SvgLoadOptions
{
public float Dpi { get; init; } = 96F;
public Color CurrentColor { get; init; } = Color.Black;
public SvgLimits Limits { get; init; } = new();
}
public sealed class SvgLimits
{
public int MaxElements { get; init; } = 1_000_000;
public int MaxNodes { get; init; } = 1_000_000;
public int MaxNestingDepth { get; init; } = 512;
public long MaxCharactersFromEntities { get; init; } = 1 << 20;
}Then the options cannot change after a load starts, and a loaded Your open questions
|
Uh oh!
There was an error while loading. Please reload this page.
SVG rendering:
SvgDocument+DrawingCanvas.DrawSvgProblem
There is no fully managed way to render SVG with ImageSharp. Today the options in .NET are:
SkSVGDOM(SkSVGDOM bindings mono/SkiaSharp#2689).Users who already draw with ImageSharp.Drawing must add a second graphics stack to rasterize an icon, a logo or a chart.
Proposed solution
Add an SVG renderer to ImageSharp.Drawing that draws through the existing
DrawingCanvas, so it works on both the CPU and WebGPU backends and composes with other canvas drawing.A document is immutable after load and can be drawn many times, at any size, from any thread.
Design
Load and draw are separate steps, so repeated draws pay no parsing cost.
XmlReaderwith a pre-seeded name table and span-based value parsers, into a flat element array. No DOM.<style>sheets and inlinestyle, with CSS specificity and!important.useinstancing, shapes toIPath, paint servers to brushes. Geometry and paint are shared betweenuseinstances.transform,viewBoxDrawingOptions.TransformopacitySaveLayerfill,strokeFill,DrawwithSolidBrush,LinearGradientBrush,RadialGradientBrush,Penfill-ruleIntersectionRuleclip-path(phase 2)ClipSecurity
InvalidImageContentException.Scope
use/symbol, opacity, CSS with simple selectorsclipPath,mask,pattern, markers,image,switch, blend modes,paint-order, full selectorsImage.Loadfor.svg)Not in scope: animation, scripting, interactivity, SVG fonts.
Status
I already have Phase 1 implemented with 650 tests. Reference images were checked against headless Chromium. Total difference, counting only pixels that differ by more than 64 levels:
And lots of tokens left to implement the remaining phases ...
Changes this needs in the canvas
GradientBrushinterpolates premultiplied. An interpolation option onGradientBrushwould fix gradients that change colour and alpha together.Open questions
SixLabors.ImageSharp.Drawing.Svgpackage?GradientBrushinterpolation option acceptable as a public API change?All reactions