/
ShapeGraphicOptionsDefaultsExtensions.cs
92 lines (82 loc) · 4.12 KB
/
ShapeGraphicOptionsDefaultsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Drawing.Processing;
/// <summary>
/// Adds extensions that allow the configuration of <see cref="ShapeOptions"/>.
/// </summary>
public static class ShapeGraphicOptionsDefaultsExtensions
{
/// <summary>
/// Sets the default shape processing options against The source image processing context.
/// </summary>
/// <param name="context">The image processing context to store default against.</param>
/// <param name="optionsBuilder">The action to update instance of the default options used.</param>
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
public static IImageProcessingContext SetShapeOptions(this IImageProcessingContext context, Action<ShapeOptions> optionsBuilder)
{
ShapeOptions cloned = context.GetShapeOptions().DeepClone();
optionsBuilder(cloned);
context.Properties[typeof(ShapeOptions)] = cloned;
return context;
}
/// <summary>
/// Sets the default shape processing options against the configuration.
/// </summary>
/// <param name="configuration">The configuration to store default against.</param>
/// <param name="optionsBuilder">The default options to use.</param>
public static void SetShapeOptions(this Configuration configuration, Action<ShapeOptions> optionsBuilder)
{
ShapeOptions cloned = configuration.GetShapeOptions().DeepClone();
optionsBuilder(cloned);
configuration.Properties[typeof(ShapeOptions)] = cloned;
}
/// <summary>
/// Sets the default shape processing options against The source image processing context.
/// </summary>
/// <param name="context">The image processing context to store default against.</param>
/// <param name="options">The default options to use.</param>
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
public static IImageProcessingContext SetShapeOptions(this IImageProcessingContext context, ShapeOptions options)
{
context.Properties[typeof(ShapeOptions)] = options;
return context;
}
/// <summary>
/// Sets the default shape processing options against the configuration.
/// </summary>
/// <param name="configuration">The configuration to store default against.</param>
/// <param name="options">The default options to use.</param>
public static void SetShapeOptions(this Configuration configuration, ShapeOptions options)
=> configuration.Properties[typeof(ShapeOptions)] = options;
/// <summary>
/// Gets the default shape processing options against The source image processing context.
/// </summary>
/// <param name="context">The image processing context to retrieve defaults from.</param>
/// <returns>The globally configured default options.</returns>
public static ShapeOptions GetShapeOptions(this IImageProcessingContext context)
{
if (context.Properties.TryGetValue(typeof(ShapeOptions), out object? options) && options is ShapeOptions go)
{
return go;
}
// do not cache the fall back to config into the the processing context
// in case someone want to change the value on the config and expects it reflow thru
return context.Configuration.GetShapeOptions();
}
/// <summary>
/// Gets the default shape processing options against The source image processing context.
/// </summary>
/// <param name="configuration">The configuration to retrieve defaults from.</param>
/// <returns>The globally configured default options.</returns>
public static ShapeOptions GetShapeOptions(this Configuration configuration)
{
if (configuration.Properties.TryGetValue(typeof(ShapeOptions), out object? options) && options is ShapeOptions go)
{
return go;
}
ShapeOptions configOptions = new();
// capture the fallback so the same instance will always be returned in case its mutated
configuration.Properties[typeof(ShapeOptions)] = configOptions;
return configOptions;
}
}