Skip to content

Commit c1c04fe

Browse files
committed
make TextOptions and ShapeOptions composed rather than inherited.
1 parent 17f02b4 commit c1c04fe

36 files changed

Lines changed: 568 additions & 702 deletions

src/ImageSharp.Drawing/Processing/Processors/Drawing/FillRegionProcessor{TPixel}.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public FillRegionProcessor(Configuration configuration, FillRegionProcessor defi
3030
protected override void OnFrameApply(ImageFrame<TPixel> source)
3131
{
3232
Configuration configuration = this.Configuration;
33-
ShapeGraphicsOptions shapeOptions = this.definition.Options;
34-
GraphicsOptions forApplicator = (GraphicsOptions)shapeOptions;
33+
ShapeOptions shapeOptions = this.definition.Options.ShapeOptions;
34+
GraphicsOptions graphicsOptions = this.definition.Options.GraphicsOptions;
3535
IBrush brush = this.definition.Brush;
3636
Region region = this.definition.Region;
3737
Rectangle rect = region.Bounds;
3838

39-
bool isSolidBrushWithoutBlending = IsSolidBrushWithoutBlending(forApplicator, this.definition.Brush, out SolidBrush solidBrush);
39+
bool isSolidBrushWithoutBlending = IsSolidBrushWithoutBlending(graphicsOptions, this.definition.Brush, out SolidBrush solidBrush);
4040
TPixel solidBrushColor = isSolidBrushWithoutBlending ? solidBrush.Color.ToPixel<TPixel>() : default;
4141

4242
// Align start/end positions.
@@ -62,17 +62,17 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
6262
// and this can cause missed fills when not using antialiasing.so we offset the pixel grid by 0.5 in the x & y direction thus causing the#
6363
// region to align with the pixel grid.
6464
float offset = 0.5f;
65-
if (shapeOptions.Antialias)
65+
if (graphicsOptions.Antialias)
6666
{
6767
offset = 0f; // we are antialiasing skip offsetting as real antialiasing should take care of offset.
68-
subpixelCount = shapeOptions.AntialiasSubpixelDepth;
68+
subpixelCount = graphicsOptions.AntialiasSubpixelDepth;
6969
if (subpixelCount < 4)
7070
{
7171
subpixelCount = 4;
7272
}
7373
}
7474

75-
using (BrushApplicator<TPixel> applicator = brush.CreateApplicator(configuration, forApplicator, source, rect))
75+
using (BrushApplicator<TPixel> applicator = brush.CreateApplicator(configuration, graphicsOptions, source, rect))
7676
{
7777
int scanlineWidth = maxX - minX;
7878
MemoryAllocator allocator = this.Configuration.MemoryAllocator;
@@ -143,7 +143,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
143143

144144
if (scanlineDirty)
145145
{
146-
if (!shapeOptions.Antialias)
146+
if (!graphicsOptions.Antialias)
147147
{
148148
bool hasOnes = false;
149149
bool hasZeros = false;

src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor{TPixel}.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ protected override void BeforeImageApply()
4646
base.BeforeImageApply();
4747

4848
// do everything at the image level as we are delegating the processing down to other processors
49-
var style = new RendererOptions(this.Font, this.Options.DpiX, this.Options.DpiY, this.Location)
49+
var style = new RendererOptions(this.Font, this.Options.TextOptions.DpiX, this.Options.TextOptions.DpiY, this.Location)
5050
{
51-
ApplyKerning = this.Options.ApplyKerning,
52-
TabWidth = this.Options.TabWidth,
53-
WrappingWidth = this.Options.WrapTextWidth,
54-
HorizontalAlignment = this.Options.HorizontalAlignment,
55-
VerticalAlignment = this.Options.VerticalAlignment,
56-
FallbackFontFamilies = this.Options.FallbackFonts,
57-
ColorFontSupport = this.definition.Options.RenderColorFonts ? ColorFontSupport.MicrosoftColrFormat : ColorFontSupport.None,
51+
ApplyKerning = this.Options.TextOptions.ApplyKerning,
52+
TabWidth = this.Options.TextOptions.TabWidth,
53+
WrappingWidth = this.Options.TextOptions.WrapTextWidth,
54+
HorizontalAlignment = this.Options.TextOptions.HorizontalAlignment,
55+
VerticalAlignment = this.Options.TextOptions.VerticalAlignment,
56+
FallbackFontFamilies = this.Options.TextOptions.FallbackFonts,
57+
ColorFontSupport = this.definition.Options.TextOptions.RenderColorFonts ? ColorFontSupport.MicrosoftColrFormat : ColorFontSupport.None,
5858
};
5959

6060
this.textRenderer = new CachingGlyphRenderer(this.Configuration.MemoryAllocator, this.Text.Length, this.Pen, this.Brush != null);
6161

62-
this.textRenderer.Options = (GraphicsOptions)this.Options;
62+
this.textRenderer.Options = this.Options.GraphicsOptions;
6363
var renderer = new TextRenderer(this.textRenderer);
6464
renderer.RenderText(this.Text, style);
6565
}

src/ImageSharp.Drawing/Processing/ShapeGraphicOptionsDefaultsExtensions.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public static class ShapeGraphicOptionsDefaultsExtensions
1717
/// <param name="context">The image processing context to store default against.</param>
1818
/// <param name="optionsBuilder">The action to update instance of the default options used.</param>
1919
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
20-
public static IImageProcessingContext SetShapeGraphicsOptions(this IImageProcessingContext context, Action<ShapeGraphicsOptions> optionsBuilder)
20+
public static IImageProcessingContext SetShapeOptions(this IImageProcessingContext context, Action<ShapeOptions> optionsBuilder)
2121
{
22-
var cloned = context.GetShapeGraphicsOptions().DeepClone();
22+
var cloned = context.GetShapeOptions().DeepClone();
2323
optionsBuilder(cloned);
24-
context.Properties[typeof(ShapeGraphicsOptions)] = cloned;
24+
context.Properties[typeof(ShapeOptions)] = cloned;
2525
return context;
2626
}
2727

@@ -30,11 +30,11 @@ public static IImageProcessingContext SetShapeGraphicsOptions(this IImageProcess
3030
/// </summary>
3131
/// <param name="configuration">The configuration to store default against.</param>
3232
/// <param name="optionsBuilder">The default options to use.</param>
33-
public static void SetShapeGraphicsOptions(this Configuration configuration, Action<ShapeGraphicsOptions> optionsBuilder)
33+
public static void SetShapeOptions(this Configuration configuration, Action<ShapeOptions> optionsBuilder)
3434
{
35-
var cloned = configuration.GetShapeGraphicsOptions().DeepClone();
35+
var cloned = configuration.GetShapeOptions().DeepClone();
3636
optionsBuilder(cloned);
37-
configuration.Properties[typeof(ShapeGraphicsOptions)] = cloned;
37+
configuration.Properties[typeof(ShapeOptions)] = cloned;
3838
}
3939

4040
/// <summary>
@@ -43,9 +43,9 @@ public static void SetShapeGraphicsOptions(this Configuration configuration, Act
4343
/// <param name="context">The image processing context to store default against.</param>
4444
/// <param name="options">The default options to use.</param>
4545
/// <returns>The passed in <paramref name="context"/> to allow chaining.</returns>
46-
public static IImageProcessingContext SetShapeGraphicsOptions(this IImageProcessingContext context, ShapeGraphicsOptions options)
46+
public static IImageProcessingContext SetShapeOptions(this IImageProcessingContext context, ShapeOptions options)
4747
{
48-
context.Properties[typeof(ShapeGraphicsOptions)] = options;
48+
context.Properties[typeof(ShapeOptions)] = options;
4949
return context;
5050
}
5151

@@ -54,9 +54,9 @@ public static IImageProcessingContext SetShapeGraphicsOptions(this IImageProcess
5454
/// </summary>
5555
/// <param name="configuration">The configuration to store default against.</param>
5656
/// <param name="options">The default options to use.</param>
57-
public static void SetShapeGraphicsOptions(this Configuration configuration, ShapeGraphicsOptions options)
57+
public static void SetShapeOptions(this Configuration configuration, ShapeOptions options)
5858
{
59-
configuration.Properties[typeof(ShapeGraphicsOptions)] = options;
59+
configuration.Properties[typeof(ShapeOptions)] = options;
6060
}
6161

6262
/// <summary>
@@ -65,13 +65,21 @@ public static void SetShapeGraphicsOptions(this Configuration configuration, Sha
6565
/// <param name="context">The image processing context to retrieve defaults from.</param>
6666
/// <returns>The globaly configued default options.</returns>
6767
public static ShapeGraphicsOptions GetShapeGraphicsOptions(this IImageProcessingContext context)
68+
=> new ShapeGraphicsOptions(context.GetGraphicsOptions(), context.GetShapeOptions());
69+
70+
/// <summary>
71+
/// Gets the default shape processing options against the image processing context.
72+
/// </summary>
73+
/// <param name="context">The image processing context to retrieve defaults from.</param>
74+
/// <returns>The globaly configued default options.</returns>
75+
public static ShapeOptions GetShapeOptions(this IImageProcessingContext context)
6876
{
69-
if (context.Properties.TryGetValue(typeof(ShapeGraphicsOptions), out var options) && options is ShapeGraphicsOptions go)
77+
if (context.Properties.TryGetValue(typeof(ShapeOptions), out var options) && options is ShapeOptions go)
7078
{
7179
return go;
7280
}
7381

74-
var configOptions = context.Configuration.GetShapeGraphicsOptions();
82+
var configOptions = context.Configuration.GetShapeOptions();
7583

7684
// do not cache the fall back to config into the the processing context
7785
// in case someone want to change the value on the config and expects it re trflow thru
@@ -83,17 +91,17 @@ public static ShapeGraphicsOptions GetShapeGraphicsOptions(this IImageProcessing
8391
/// </summary>
8492
/// <param name="configuration">The configuration to retrieve defaults from.</param>
8593
/// <returns>The globaly configued default options.</returns>
86-
public static ShapeGraphicsOptions GetShapeGraphicsOptions(this Configuration configuration)
94+
public static ShapeOptions GetShapeOptions(this Configuration configuration)
8795
{
88-
if (configuration.Properties.TryGetValue(typeof(ShapeGraphicsOptions), out var options) && options is ShapeGraphicsOptions go)
96+
if (configuration.Properties.TryGetValue(typeof(ShapeOptions), out var options) && options is ShapeOptions go)
8997
{
9098
return go;
9199
}
92100

93-
var configOptions = new ShapeGraphicsOptions(configuration.GetGraphicsOptions());
101+
var configOptions = new ShapeOptions();
94102

95103
// capture the fallback so the same instance will always be returned in case its mutated
96-
configuration.Properties[typeof(ShapeGraphicsOptions)] = configOptions;
104+
configuration.Properties[typeof(ShapeOptions)] = configOptions;
97105
return configOptions;
98106
}
99107
}

src/ImageSharp.Drawing/Processing/ShapeGraphicsOptions.cs

Lines changed: 22 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -10,131 +10,57 @@ namespace SixLabors.ImageSharp.Drawing.Processing
1010
/// <summary>
1111
/// Options for influencing the drawing functions.
1212
/// </summary>
13-
public class ShapeGraphicsOptions : IDeepCloneable<ShapeGraphicsOptions>
13+
public class ShapeGraphicsOptions
1414
{
15-
private int antialiasSubpixelDepth = 16;
16-
private float blendPercentage = 1F;
15+
private GraphicsOptions graphicsOptions;
16+
private ShapeOptions shapeOptions;
1717

1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="ShapeGraphicsOptions"/> class.
2020
/// </summary>
2121
public ShapeGraphicsOptions()
2222
{
23+
this.graphicsOptions = new GraphicsOptions();
24+
this.shapeOptions = new ShapeOptions();
2325
}
2426

2527
/// <summary>
2628
/// Initializes a new instance of the <see cref="ShapeGraphicsOptions"/> class.
2729
/// </summary>
28-
/// <param name="source">The source to clone from.</param>
29-
public ShapeGraphicsOptions(GraphicsOptions source)
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)
3033
{
31-
this.AlphaCompositionMode = source.AlphaCompositionMode;
32-
this.Antialias = source.Antialias;
33-
this.AntialiasSubpixelDepth = source.AntialiasSubpixelDepth;
34-
this.BlendPercentage = source.BlendPercentage;
35-
this.ColorBlendingMode = source.ColorBlendingMode;
34+
Guard.NotNull(graphicsOptions, nameof(graphicsOptions));
35+
Guard.NotNull(shapeOptions, nameof(shapeOptions));
36+
this.graphicsOptions = graphicsOptions;
37+
this.shapeOptions = shapeOptions;
3638
}
3739

38-
private ShapeGraphicsOptions(ShapeGraphicsOptions source)
39-
{
40-
this.AlphaCompositionMode = source.AlphaCompositionMode;
41-
this.Antialias = source.Antialias;
42-
this.AntialiasSubpixelDepth = source.AntialiasSubpixelDepth;
43-
this.BlendPercentage = source.BlendPercentage;
44-
this.ColorBlendingMode = source.ColorBlendingMode;
45-
this.IntersectionRule = source.IntersectionRule;
46-
}
47-
48-
/// <summary>
49-
/// Gets or sets a value indicating whether antialiasing should be applied.
50-
/// Defaults to true.
51-
/// </summary>
52-
public IntersectionRule IntersectionRule { get; set; } = IntersectionRule.OddEven;
53-
54-
/// <summary>
55-
/// Gets or sets a value indicating whether antialiasing should be applied.
56-
/// Defaults to true.
57-
/// </summary>
58-
public bool Antialias { get; set; } = true;
59-
6040
/// <summary>
61-
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
41+
/// Gets or sets the Graphics Options.
6242
/// </summary>
63-
public int AntialiasSubpixelDepth
43+
public GraphicsOptions GraphicsOptions
6444
{
65-
get
66-
{
67-
return this.antialiasSubpixelDepth;
68-
}
69-
45+
get => this.graphicsOptions;
7046
set
7147
{
72-
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth));
73-
this.antialiasSubpixelDepth = value;
48+
Guard.NotNull(value, nameof(this.GraphicsOptions));
49+
this.graphicsOptions = value;
7450
}
7551
}
7652

7753
/// <summary>
78-
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation.
54+
/// Gets or sets the Text Options.
7955
/// </summary>
80-
public float BlendPercentage
56+
public ShapeOptions ShapeOptions
8157
{
82-
get
83-
{
84-
return this.blendPercentage;
85-
}
86-
58+
get => this.shapeOptions;
8759
set
8860
{
89-
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage));
90-
this.blendPercentage = value;
61+
Guard.NotNull(value, nameof(this.ShapeOptions));
62+
this.shapeOptions = value;
9163
}
9264
}
93-
94-
/// <summary>
95-
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation.
96-
/// Defaults to <see cref= "PixelColorBlendingMode.Normal" />.
97-
/// </summary>
98-
public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal;
99-
100-
/// <summary>
101-
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation
102-
/// Defaults to <see cref= "PixelAlphaCompositionMode.SrcOver" />.
103-
/// </summary>
104-
public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver;
105-
106-
/// <summary>
107-
/// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>.
108-
/// </summary>
109-
/// <param name="options">The options.</param>
110-
/// <returns>
111-
/// The result of the conversion.
112-
/// </returns>
113-
public static implicit operator ShapeGraphicsOptions(GraphicsOptions options)
114-
{
115-
return new ShapeGraphicsOptions(options);
116-
}
117-
118-
/// <summary>
119-
/// Performs an explicit conversion from <see cref="TextGraphicsOptions"/> to <see cref="GraphicsOptions"/>.
120-
/// </summary>
121-
/// <param name="options">The options.</param>
122-
/// <returns>
123-
/// The result of the conversion.
124-
/// </returns>
125-
public static explicit operator GraphicsOptions(ShapeGraphicsOptions options)
126-
{
127-
return new GraphicsOptions()
128-
{
129-
Antialias = options.Antialias,
130-
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
131-
ColorBlendingMode = options.ColorBlendingMode,
132-
AlphaCompositionMode = options.AlphaCompositionMode,
133-
BlendPercentage = options.BlendPercentage
134-
};
135-
}
136-
137-
/// <inheritdoc/>
138-
public ShapeGraphicsOptions DeepClone() => new ShapeGraphicsOptions(this);
13965
}
14066
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Drawing.Processing
5+
{
6+
/// <summary>
7+
/// Options for influencing the drawing functions.
8+
/// </summary>
9+
public class ShapeOptions : IDeepCloneable<ShapeOptions>
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="ShapeOptions"/> class.
13+
/// </summary>
14+
public ShapeOptions()
15+
{
16+
}
17+
18+
private ShapeOptions(ShapeOptions source)
19+
{
20+
this.IntersectionRule = source.IntersectionRule;
21+
}
22+
23+
/// <summary>
24+
/// Gets or sets a value indicating whether antialiasing should be applied.
25+
/// Defaults to true.
26+
/// </summary>
27+
public IntersectionRule IntersectionRule { get; set; } = IntersectionRule.OddEven;
28+
29+
/// <inheritdoc/>
30+
public ShapeOptions DeepClone() => new ShapeOptions(this);
31+
}
32+
}

0 commit comments

Comments
 (0)