Skip to content

Commit b8ff644

Browse files
authored
Merge pull request #126 from SixLabors/sw/drawing-transforms
Apply global matrix transform drawing text and shapes.
2 parents c29e5de + 45c2a26 commit b8ff644

67 files changed

Lines changed: 425 additions & 240 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ImageSharp.Drawing/Processing/ShapeGraphicsOptions.cs renamed to src/ImageSharp.Drawing/Processing/DrawingOptions.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Numerics;
56
using SixLabors.ImageSharp.PixelFormats;
67
using SixLabors.ImageSharp.Processing;
78

@@ -10,31 +11,37 @@ namespace SixLabors.ImageSharp.Drawing.Processing
1011
/// <summary>
1112
/// Options for influencing the drawing functions.
1213
/// </summary>
13-
public class ShapeGraphicsOptions
14+
public class DrawingOptions
1415
{
1516
private GraphicsOptions graphicsOptions;
1617
private ShapeOptions shapeOptions;
18+
private TextOptions textOptions;
1719

1820
/// <summary>
19-
/// Initializes a new instance of the <see cref="ShapeGraphicsOptions"/> class.
21+
/// Initializes a new instance of the <see cref="DrawingOptions"/> class.
2022
/// </summary>
21-
public ShapeGraphicsOptions()
23+
public DrawingOptions()
2224
{
2325
this.graphicsOptions = new GraphicsOptions();
2426
this.shapeOptions = new ShapeOptions();
27+
this.textOptions = new TextOptions();
28+
this.Transform = Matrix3x2.Identity;
2529
}
2630

27-
/// <summary>
28-
/// Initializes a new instance of the <see cref="ShapeGraphicsOptions"/> class.
29-
/// </summary>
30-
/// <param name="graphicsOptions">The graphic options to use</param>
31-
/// <param name="shapeOptions">The text options to use</param>
32-
public ShapeGraphicsOptions(GraphicsOptions graphicsOptions, ShapeOptions shapeOptions)
31+
internal DrawingOptions(
32+
GraphicsOptions graphicsOptions,
33+
ShapeOptions shapeOptions,
34+
TextOptions textOptions,
35+
Matrix3x2 transform)
3336
{
34-
Guard.NotNull(graphicsOptions, nameof(graphicsOptions));
35-
Guard.NotNull(shapeOptions, nameof(shapeOptions));
37+
DebugGuard.NotNull(graphicsOptions, nameof(graphicsOptions));
38+
DebugGuard.NotNull(shapeOptions, nameof(shapeOptions));
39+
DebugGuard.NotNull(textOptions, nameof(textOptions));
40+
3641
this.graphicsOptions = graphicsOptions;
3742
this.shapeOptions = shapeOptions;
43+
this.textOptions = textOptions;
44+
this.Transform = transform;
3845
}
3946

4047
/// <summary>
@@ -51,7 +58,7 @@ public GraphicsOptions GraphicsOptions
5158
}
5259

5360
/// <summary>
54-
/// Gets or sets the Text Options.
61+
/// Gets or sets the Shape Options.
5562
/// </summary>
5663
public ShapeOptions ShapeOptions
5764
{
@@ -62,5 +69,23 @@ public ShapeOptions ShapeOptions
6269
this.shapeOptions = value;
6370
}
6471
}
72+
73+
/// <summary>
74+
/// Gets or sets the Text Options.
75+
/// </summary>
76+
public TextOptions TextOptions
77+
{
78+
get => this.textOptions;
79+
set
80+
{
81+
Guard.NotNull(value, nameof(this.TextOptions));
82+
this.textOptions = value;
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Gets or sets the Transform to apply during rasterization.
88+
/// </summary>
89+
public Matrix3x2 Transform { get; set; }
6590
}
6691
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Numerics;
5+
using SixLabors.ImageSharp.Processing;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Processing
8+
{
9+
/// <summary>
10+
/// Adds extensions that help working with <see cref="DrawingOptions" />.
11+
/// </summary>
12+
public static class DrawingOptionsDefaultsExtensions
13+
{
14+
private const string DrawingTransformMatrixKey = "DrawingTransformMatrix3x2";
15+
16+
/// <summary>
17+
/// Gets the default shape processing options against the image processing context.
18+
/// </summary>
19+
/// <param name="context">The image processing context to retrieve defaults from.</param>
20+
/// <returns>The globally configured default options.</returns>
21+
public static DrawingOptions GetDrawingOptions(this IImageProcessingContext context)
22+
=> new DrawingOptions(context.GetGraphicsOptions(), context.GetShapeOptions(), context.GetTextOptions(), context.GetDrawingTransform());
23+
24+
/// <summary>
25+
/// Sets the 2D transformation matrix to be used during rasterization when drawing shapes or text.
26+
/// </summary>
27+
/// <param name="context">The image processing context to store default against.</param>
28+
/// <param name="matrix">The matrix to use.</param>
29+
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
30+
public static IImageProcessingContext SetDrawingTransform(this IImageProcessingContext context, Matrix3x2 matrix)
31+
{
32+
context.Properties[DrawingTransformMatrixKey] = matrix;
33+
return context;
34+
}
35+
36+
/// <summary>
37+
/// Sets the default 2D transformation matrix to be used during rasterization when drawing shapes or text.
38+
/// </summary>
39+
/// <param name="configuration">The configuration to store default against.</param>
40+
/// <param name="matrix">The default matrix to use.</param>
41+
public static void SetDrawingTransform(this Configuration configuration, Matrix3x2 matrix)
42+
{
43+
configuration.Properties[DrawingTransformMatrixKey] = matrix;
44+
}
45+
46+
/// <summary>
47+
/// Gets the default 2D transformation matrix to be used during rasterization when drawing shapes or text.
48+
/// </summary>
49+
/// <param name="context">The image processing context to retrieve defaults from.</param>
50+
/// <returns>The matrix.</returns>
51+
public static Matrix3x2 GetDrawingTransform(this IImageProcessingContext context)
52+
{
53+
if (context.Properties.TryGetValue(DrawingTransformMatrixKey, out var options) && options is Matrix3x2 go)
54+
{
55+
return go;
56+
}
57+
58+
Matrix3x2 matrix = context.Configuration.GetDrawingTransform();
59+
60+
// do not cache the fall back to config into the processing context
61+
// in case someone want to change the value on the config and expects it re-flow thru.
62+
return matrix;
63+
}
64+
65+
/// <summary>
66+
/// Gets the default 2D transformation matrix to be used during rasterization when drawing shapes or text.
67+
/// </summary>
68+
/// <param name="configuration">The configuration to retrieve defaults from.</param>
69+
/// <returns>The globally configured default matrix.</returns>
70+
public static Matrix3x2 GetDrawingTransform(this Configuration configuration)
71+
{
72+
if (configuration.Properties.TryGetValue(DrawingTransformMatrixKey, out var options) && options is Matrix3x2 go)
73+
{
74+
return go;
75+
}
76+
77+
return Matrix3x2.Identity;
78+
}
79+
}
80+
}

src/ImageSharp.Drawing/Processing/Extensions/ClearPathExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public static class ClearPathExtensions
1616
/// </summary>
1717
/// <param name="shapeOptions">The options to clone</param>
1818
/// <returns>A clone of shapeOptions with ColorBlendingMode, AlphaCompositionMode, and BlendPercentage set</returns>
19-
internal static ShapeGraphicsOptions CloneForClearOperation(this ShapeGraphicsOptions shapeOptions)
19+
internal static DrawingOptions CloneForClearOperation(this DrawingOptions shapeOptions)
2020
{
21-
var options = shapeOptions.GraphicsOptions.DeepClone();
21+
GraphicsOptions options = shapeOptions.GraphicsOptions.DeepClone();
2222
options.ColorBlendingMode = PixelFormats.PixelColorBlendingMode.Normal;
2323
options.AlphaCompositionMode = PixelFormats.PixelAlphaCompositionMode.Src;
2424
options.BlendPercentage = 1;
2525

26-
return new ShapeGraphicsOptions(options, shapeOptions.ShapeOptions);
26+
return new DrawingOptions(options, shapeOptions.ShapeOptions, shapeOptions.TextOptions, shapeOptions.Transform);
2727
}
2828

2929
/// <summary>
@@ -36,7 +36,7 @@ internal static ShapeGraphicsOptions CloneForClearOperation(this ShapeGraphicsOp
3636
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3737
public static IImageProcessingContext Clear(
3838
this IImageProcessingContext source,
39-
ShapeGraphicsOptions options,
39+
DrawingOptions options,
4040
IBrush brush,
4141
IPath path) =>
4242
source.Fill(options.CloneForClearOperation(), brush, path);
@@ -49,7 +49,7 @@ public static IImageProcessingContext Clear(
4949
/// <param name="path">The path.</param>
5050
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5151
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, IPath path) =>
52-
source.Clear(source.GetShapeGraphicsOptions(), brush, path);
52+
source.Clear(source.GetDrawingOptions(), brush, path);
5353

5454
/// <summary>
5555
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
@@ -61,7 +61,7 @@ public static IImageProcessingContext Clear(this IImageProcessingContext source,
6161
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
6262
public static IImageProcessingContext Clear(
6363
this IImageProcessingContext source,
64-
ShapeGraphicsOptions options,
64+
DrawingOptions options,
6565
Color color,
6666
IPath path) =>
6767
source.Clear(options, new SolidBrush(color), path);

src/ImageSharp.Drawing/Processing/Extensions/ClearRectangleExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class ClearRectangleExtensions
2020
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2121
public static IImageProcessingContext Clear(
2222
this IImageProcessingContext source,
23-
ShapeGraphicsOptions options,
23+
DrawingOptions options,
2424
IBrush brush,
2525
RectangleF shape) =>
2626
source.Clear(options, brush, new RectangularPolygon(shape.X, shape.Y, shape.Width, shape.Height));
@@ -45,7 +45,7 @@ public static IImageProcessingContext Clear(this IImageProcessingContext source,
4545
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
4646
public static IImageProcessingContext Clear(
4747
this IImageProcessingContext source,
48-
ShapeGraphicsOptions options,
48+
DrawingOptions options,
4949
Color color,
5050
RectangleF shape) =>
5151
source.Clear(options, new SolidBrush(color), shape);

src/ImageSharp.Drawing/Processing/Extensions/ClearRegionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class ClearRegionExtensions
1919
/// <param name="region">The region.</param>
2020
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2121
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, Region region) =>
22-
source.Clear(source.GetShapeGraphicsOptions(), brush, region);
22+
source.Clear(source.GetDrawingOptions(), brush, region);
2323

2424
/// <summary>
2525
/// Flood fills the image with in the region with the specified brush without any blending.
@@ -31,7 +31,7 @@ public static IImageProcessingContext Clear(this IImageProcessingContext source,
3131
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3232
public static IImageProcessingContext Clear(
3333
this IImageProcessingContext source,
34-
ShapeGraphicsOptions options,
34+
DrawingOptions options,
3535
IBrush brush,
3636
Region region) =>
3737
source.Fill(options.CloneForClearOperation(), brush, region);
@@ -46,7 +46,7 @@ public static IImageProcessingContext Clear(
4646
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
4747
public static IImageProcessingContext Clear(
4848
this IImageProcessingContext source,
49-
ShapeGraphicsOptions options,
49+
DrawingOptions options,
5050
Color color,
5151
Region region) =>
5252
source.Clear(options, new SolidBrush(color), region);

src/ImageSharp.Drawing/Processing/Extensions/DrawBezierExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class DrawBezierExtensions
2020
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2121
public static IImageProcessingContext DrawBeziers(
2222
this IImageProcessingContext source,
23-
ShapeGraphicsOptions options,
23+
DrawingOptions options,
2424
IPen pen,
2525
params PointF[] points) =>
2626
source.Draw(options, pen, new Path(new CubicBezierLineSegment(points)));
@@ -49,7 +49,7 @@ public static IImageProcessingContext DrawBeziers(
4949
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5050
public static IImageProcessingContext DrawBeziers(
5151
this IImageProcessingContext source,
52-
ShapeGraphicsOptions options,
52+
DrawingOptions options,
5353
IBrush brush,
5454
float thickness,
5555
params PointF[] points) =>
@@ -96,7 +96,7 @@ public static IImageProcessingContext DrawBeziers(
9696
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
9797
public static IImageProcessingContext DrawBeziers(
9898
this IImageProcessingContext source,
99-
ShapeGraphicsOptions options,
99+
DrawingOptions options,
100100
Color color,
101101
float thickness,
102102
params PointF[] points) =>

src/ImageSharp.Drawing/Processing/Extensions/DrawLineExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class DrawLineExtensions
2121
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2222
public static IImageProcessingContext DrawLines(
2323
this IImageProcessingContext source,
24-
ShapeGraphicsOptions options,
24+
DrawingOptions options,
2525
IBrush brush,
2626
float thickness,
2727
params PointF[] points) =>
@@ -68,7 +68,7 @@ public static IImageProcessingContext DrawLines(
6868
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>>
6969
public static IImageProcessingContext DrawLines(
7070
this IImageProcessingContext source,
71-
ShapeGraphicsOptions options,
71+
DrawingOptions options,
7272
Color color,
7373
float thickness,
7474
params PointF[] points) =>
@@ -84,7 +84,7 @@ public static IImageProcessingContext DrawLines(
8484
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
8585
public static IImageProcessingContext DrawLines(
8686
this IImageProcessingContext source,
87-
ShapeGraphicsOptions options,
87+
DrawingOptions options,
8888
IPen pen,
8989
params PointF[] points) =>
9090
source.Draw(options, pen, new Path(new LinearLineSegment(points)));

src/ImageSharp.Drawing/Processing/Extensions/DrawPathCollectionExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class DrawPathCollectionExtensions
2020
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2121
public static IImageProcessingContext Draw(
2222
this IImageProcessingContext source,
23-
ShapeGraphicsOptions options,
23+
DrawingOptions options,
2424
IPen pen,
2525
IPathCollection paths)
2626
{
@@ -41,7 +41,7 @@ public static IImageProcessingContext Draw(
4141
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
4242
public static IImageProcessingContext
4343
Draw(this IImageProcessingContext source, IPen pen, IPathCollection paths) =>
44-
source.Draw(source.GetShapeGraphicsOptions(), pen, paths);
44+
source.Draw(source.GetDrawingOptions(), pen, paths);
4545

4646
/// <summary>
4747
/// Draws the outline of the polygon with the provided brush at the provided thickness.
@@ -54,7 +54,7 @@ public static IImageProcessingContext
5454
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5555
public static IImageProcessingContext Draw(
5656
this IImageProcessingContext source,
57-
ShapeGraphicsOptions options,
57+
DrawingOptions options,
5858
IBrush brush,
5959
float thickness,
6060
IPathCollection paths) =>
@@ -86,7 +86,7 @@ public static IImageProcessingContext Draw(
8686
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
8787
public static IImageProcessingContext Draw(
8888
this IImageProcessingContext source,
89-
ShapeGraphicsOptions options,
89+
DrawingOptions options,
9090
Color color,
9191
float thickness,
9292
IPathCollection paths) =>

src/ImageSharp.Drawing/Processing/Extensions/DrawPathExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class DrawPathExtensions
2121
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2222
public static IImageProcessingContext Draw(
2323
this IImageProcessingContext source,
24-
ShapeGraphicsOptions options,
24+
DrawingOptions options,
2525
IPen pen,
2626
IPath path) =>
2727
source.ApplyProcessor(new DrawPathProcessor(options, pen, path));
@@ -34,7 +34,7 @@ public static IImageProcessingContext Draw(
3434
/// <param name="path">The path.</param>
3535
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3636
public static IImageProcessingContext Draw(this IImageProcessingContext source, IPen pen, IPath path) =>
37-
source.Draw(source.GetShapeGraphicsOptions(), pen, path);
37+
source.Draw(source.GetDrawingOptions(), pen, path);
3838

3939
/// <summary>
4040
/// Draws the outline of the polygon with the provided brush at the provided thickness.
@@ -47,7 +47,7 @@ public static IImageProcessingContext Draw(this IImageProcessingContext source,
4747
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
4848
public static IImageProcessingContext Draw(
4949
this IImageProcessingContext source,
50-
ShapeGraphicsOptions options,
50+
DrawingOptions options,
5151
IBrush brush,
5252
float thickness,
5353
IPath path) =>
@@ -79,7 +79,7 @@ public static IImageProcessingContext Draw(
7979
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
8080
public static IImageProcessingContext Draw(
8181
this IImageProcessingContext source,
82-
ShapeGraphicsOptions options,
82+
DrawingOptions options,
8383
Color color,
8484
float thickness,
8585
IPath path) =>

0 commit comments

Comments
 (0)