// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using SixLabors.Fonts; using SixLabors.ImageSharp.Drawing.Processing.Backends; using SixLabors.ImageSharp.Drawing.Text; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Transforms; using System; using System.Collections.Generic; namespace SixLabors.ImageSharp.Drawing.Processing { /// /// Represents a drawing canvas over a frame target. /// public abstract partial class DrawingCanvas : IDisposable { /// /// Gets the local bounds of this canvas. /// public abstract Rectangle Bounds { get; } /// /// Gets the number of saved states currently on the canvas stack. /// public abstract int SaveCount { get; } /// /// Saves the current drawing state on the state stack. /// /// /// This operation stores the current canvas state by reference. /// If the same instance is mutated after /// , those mutations are visible when restoring. /// /// The save count after the state has been pushed. public abstract int Save(); /// /// Saves the current drawing state and replaces the active state with the provided options and clip paths. /// /// /// The provided instance is stored by reference. /// Mutating it after this call mutates the active/restored state behavior. /// /// Drawing options for the new active state. /// Clip paths for the new active state. /// The save count after the previous state has been pushed. public abstract int Save(DrawingOptions options, params IPath[] clipPaths); /// /// Saves the current drawing state and begins an isolated compositing layer /// bounded to a subregion. Subsequent draw commands are recorded into that isolated /// logical layer. When closes the layer, it is recorded into the /// canvas timeline and later composed during using the specified /// . /// /// /// The layer bounds are expressed in the current local coordinate system and are /// transformed with the active drawing transform when the layer is created. They /// limit allocation and compositing only; they do not change the canvas coordinate /// system used by commands recorded inside the layer. /// /// /// Graphics options controlling how the closed layer is composited against the parent canvas /// when the canvas timeline is rendered during . /// /// /// The local bounds of the layer. Only this region is allocated and composited. /// /// The save count after the layer state has been pushed. public abstract int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds); /// /// Restores the most recently saved state. /// /// /// If the most recently saved state was created by a SaveLayer overload, /// the layer is closed in the recorded timeline. Actual composition happens during /// . /// public abstract void Restore(); /// /// Restores to a specific save count. /// /// /// State frames above are discarded, /// and the last discarded frame becomes the current state. /// If any discarded state was created by a SaveLayer overload, /// those layers are closed in the recorded timeline and composed during /// . /// /// The save count to restore to. public abstract void RestoreTo(int saveCount); /// /// Creates a child canvas over a subregion in local coordinates. /// /// The child region in local coordinates. /// A child canvas with local origin at (0,0). public abstract DrawingCanvas CreateRegion(Rectangle region); /// /// Clears a path region using the given brush and clear-style composition options. /// /// Brush used to shade destination pixels during clear. /// The path region to clear. public abstract void Clear(Brush brush, IPath path); /// /// Fills a path in local coordinates using the given brush. /// /// Brush used to shade covered pixels. /// The path to fill. public abstract void Fill(Brush brush, IPath path); /// /// Applies an image-processing operation to a local region. /// /// The local region to process. /// The image-processing operation to apply to the region. public abstract void Apply(Rectangle region, Action operation); /// /// Applies an image-processing operation to a region described by a path builder. /// /// The path builder describing the region to process. /// The image-processing operation to apply to the region. public abstract void Apply(PathBuilder pathBuilder, Action operation); /// /// Applies an image-processing operation to a path region. /// /// /// The operation affects only pixels covered by the supplied path. /// /// The path region to process. /// The image-processing operation to apply to the region. public abstract void Apply(IPath path, Action operation); /// /// Draws a polyline outline using the provided pen and drawing options. /// /// Pen used to generate the line outline. /// Polyline points. public abstract void DrawLine(Pen pen, params PointF[] points); /// /// Draws a path outline in local coordinates using the given pen. /// /// Pen used to generate the outline fill path. /// The path to stroke. public abstract void Draw(Pen pen, IPath path); /// /// Draws text onto this canvas. /// /// The text rendering options. /// The text to draw. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( RichTextOptions textOptions, ReadOnlySpan text, Brush? brush, Pen? pen); /// /// Draws text along a path baseline onto this canvas. /// /// The text rendering options. /// The text to draw. /// The path used as the text baseline in local canvas coordinates. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( RichTextOptions textOptions, ReadOnlySpan text, IPath path, Brush? brush, Pen? pen); /// /// Draws a prepared text block onto this canvas. /// /// The prepared text block to draw. /// The drawing location in local canvas coordinates. /// The wrapping length in pixels. Use -1 to disable wrapping. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( TextBlock textBlock, PointF location, float wrappingLength, Brush? brush, Pen? pen); /// /// Draws a prepared text block along a path baseline onto this canvas. /// /// The prepared text block to draw. /// The path used as the text baseline in local canvas coordinates. /// The wrapping length in pixels. Use -1 to disable wrapping. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( TextBlock textBlock, IPath path, float wrappingLength, Brush? brush, Pen? pen); /// /// Draws one prepared line layout onto this canvas. /// /// The prepared line layout to draw. /// The drawing location in local canvas coordinates. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( LineLayout lineLayout, PointF location, Brush? brush, Pen? pen); /// /// Draws one prepared line layout along a path baseline onto this canvas. /// /// The prepared line layout to draw. /// The path used as the text baseline in local canvas coordinates. /// Optional brush used to fill glyphs. /// Optional pen used to outline glyphs. public abstract void DrawText( LineLayout lineLayout, IPath path, Brush? brush, Pen? pen); /// /// Draws layered glyph geometry. /// /// Brush used to fill glyph layers. /// Pen used to outline dominant painted layers. /// Layered glyph geometry to draw. public abstract void DrawGlyphs( Brush brush, Pen pen, IEnumerable glyphs); /// /// Measures the full set of layout metrics for the supplied text. /// /// The text shaping and layout options. /// The text to measure. /// A value containing the metrics for the laid-out text. public abstract TextMetrics MeasureText(RichTextOptions textOptions, ReadOnlySpan text); /// /// Draws an image source region into a destination rectangle. /// /// The source image. /// The source rectangle within . /// The destination rectangle in local canvas coordinates. /// /// Optional resampler used when scaling or transforming the image. Defaults to . /// public abstract void DrawImage( Image image, Rectangle sourceRect, RectangleF destinationRect, IResampler? sampler = null); /// /// Creates a retained backend scene from the drawing commands currently queued on this canvas. /// /// A retained backend scene. public abstract DrawingBackendScene CreateScene(); /// /// Renders a retained backend scene into this canvas target. /// /// The retained backend scene to render. public abstract void RenderScene(DrawingBackendScene scene); /// /// Seals queued drawing commands into the canvas timeline. /// public abstract void Flush(); /// public abstract void Dispose(); } }