Skip to content

Commit e2e1efb

Browse files
Rename options
1 parent 5a04d6a commit e2e1efb

17 files changed

Lines changed: 104 additions & 88 deletions

File tree

samples/DrawShapesWithImageSharp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void DrawText(string text)
6262
{
6363
FontFamily fam = SystemFonts.Get("Arial");
6464
var font = new Font(fam, 30);
65-
TextDrawingOptions textOptions = new(font);
65+
TextOptions textOptions = new(font);
6666
IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, textOptions);
6767

6868
glyphs.SaveImage("Text", text + ".png");
@@ -72,7 +72,7 @@ private static void DrawText(string text, IPath path)
7272
{
7373
FontFamily family = SystemFonts.Get("Arial");
7474
Font font = new(family, 30);
75-
TextDrawingOptions textOptions = new(font)
75+
TextOptions textOptions = new(font)
7676
{
7777
WrappingLength = path.ComputeLength(),
7878
VerticalAlignment = VerticalAlignment.Top,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static IImageProcessingContext DrawText(
5858
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5959
public static IImageProcessingContext DrawText(
6060
this IImageProcessingContext source,
61-
TextDrawingOptions textOptions,
61+
RichTextOptions textOptions,
6262
string text,
6363
Color color) =>
6464
source.DrawText(textOptions, text, Brushes.Solid(color), null);
@@ -115,7 +115,7 @@ public static IImageProcessingContext DrawText(
115115
Pen pen,
116116
PointF location)
117117
{
118-
TextDrawingOptions textOptions = new(font) { Origin = location };
118+
RichTextOptions textOptions = new(font) { Origin = location };
119119
return source.DrawText(textOptions, text, brush, pen);
120120
}
121121

@@ -129,7 +129,7 @@ public static IImageProcessingContext DrawText(
129129
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
130130
public static IImageProcessingContext DrawText(
131131
this IImageProcessingContext source,
132-
TextDrawingOptions textOptions,
132+
RichTextOptions textOptions,
133133
string text,
134134
Brush brush) =>
135135
source.DrawText(source.GetDrawingOptions(), textOptions, text, brush, null);
@@ -144,7 +144,7 @@ public static IImageProcessingContext DrawText(
144144
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
145145
public static IImageProcessingContext DrawText(
146146
this IImageProcessingContext source,
147-
TextDrawingOptions textOptions,
147+
RichTextOptions textOptions,
148148
string text,
149149
Pen pen) =>
150150
source.DrawText(source.GetDrawingOptions(), textOptions, text, null, pen);
@@ -160,7 +160,7 @@ public static IImageProcessingContext DrawText(
160160
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
161161
public static IImageProcessingContext DrawText(
162162
this IImageProcessingContext source,
163-
TextDrawingOptions textOptions,
163+
RichTextOptions textOptions,
164164
string text,
165165
Brush brush,
166166
Pen pen) =>
@@ -224,7 +224,7 @@ public static IImageProcessingContext DrawText(
224224
Pen pen,
225225
PointF location)
226226
{
227-
TextDrawingOptions textOptions = new(font) { Origin = location };
227+
RichTextOptions textOptions = new(font) { Origin = location };
228228
return source.ApplyProcessor(new DrawTextProcessor(drawingOptions, textOptions, text, brush, pen));
229229
}
230230

@@ -241,7 +241,7 @@ public static IImageProcessingContext DrawText(
241241
public static IImageProcessingContext DrawText(
242242
this IImageProcessingContext source,
243243
DrawingOptions drawingOptions,
244-
TextDrawingOptions textOptions,
244+
RichTextOptions textOptions,
245245
string text,
246246
Brush brush,
247247
Pen pen)

src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class DrawTextProcessor : IImageProcessor
2020
/// <param name="text">The text we want to render</param>
2121
/// <param name="brush">The brush to source pixel colors from.</param>
2222
/// <param name="pen">The pen to outline text with.</param>
23-
public DrawTextProcessor(DrawingOptions drawingOptions, TextDrawingOptions textOptions, string text, Brush brush, Pen pen)
23+
public DrawTextProcessor(DrawingOptions drawingOptions, RichTextOptions textOptions, string text, Brush brush, Pen pen)
2424
{
2525
Guard.NotNull(text, nameof(text));
2626
if (brush is null && pen is null)
@@ -46,9 +46,9 @@ public DrawTextProcessor(DrawingOptions drawingOptions, TextDrawingOptions textO
4646
public DrawingOptions DrawingOptions { get; }
4747

4848
/// <summary>
49-
/// Gets the <see cref="TextDrawingOptions"/> defining text-specific drawing settings.
49+
/// Gets the <see cref="RichTextOptions"/> defining text-specific drawing settings.
5050
/// </summary>
51-
public TextDrawingOptions TextOptions { get; }
51+
public RichTextOptions TextOptions { get; }
5252

5353
/// <summary>
5454
/// Gets the text to draw.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void BeforeImageApply()
3232

3333
// Do everything at the image level as we are delegating
3434
// the processing down to other processors
35-
TextDrawingOptions textOptions = ConfigureOptions(this.definition.TextOptions);
35+
RichTextOptions textOptions = ConfigureOptions(this.definition.TextOptions);
3636

3737
this.textRenderer = new RichTextGlyphRenderer(
3838
textOptions,
@@ -115,10 +115,10 @@ void Draw(IEnumerable<DrawingOperation> operations)
115115
}
116116
}
117117

118-
private static TextDrawingOptions ConfigureOptions(TextDrawingOptions options)
118+
private static RichTextOptions ConfigureOptions(RichTextOptions options)
119119
{
120120
// When a path is specified we should explicitly follow that path
121-
// and not adjust the origin. Any tranlation should be applied to the path.
121+
// and not adjust the origin. Any translation should be applied to the path.
122122
if (options.Path is not null && options.Origin != Vector2.Zero)
123123
{
124124
return new(options)

src/ImageSharp.Drawing/Processing/Processors/Text/RichTextGlyphRenderer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
2222
private const byte RenderOrderOutline = 1;
2323
private const byte RenderOrderDecoration = 2;
2424

25-
private readonly TextDrawingOptions textOptions;
25+
private readonly RichTextOptions textOptions;
2626
private readonly DrawingOptions drawingOptions;
2727
private readonly MemoryAllocator memoryAllocator;
2828
private readonly Pen defaultPen;
@@ -52,7 +52,7 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
5252
private (GlyphRendererParameters Glyph, PointF SubPixelOffset) currentCacheKey;
5353

5454
public RichTextGlyphRenderer(
55-
TextDrawingOptions textOptions,
55+
RichTextOptions textOptions,
5656
DrawingOptions drawingOptions,
5757
MemoryAllocator memoryAllocator,
5858
Pen pen,
@@ -122,7 +122,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara
122122
this.currentColor = null;
123123

124124
this.currentTextRun = parameters.TextRun;
125-
if (parameters.TextRun is TextDrawingRun drawingRun)
125+
if (parameters.TextRun is RichTextRun drawingRun)
126126
{
127127
this.currentBrush = drawingRun.Brush;
128128
this.currentPen = drawingRun.Pen;
@@ -166,7 +166,7 @@ public override TextDecorations EnabledDecorations()
166166
TextRun run = this.currentTextRun;
167167
TextDecorations decorations = run.TextDecorations;
168168

169-
if (this.currentTextRun is TextDrawingRun drawingRun)
169+
if (this.currentTextRun is RichTextRun drawingRun)
170170
{
171171
if (drawingRun.UnderlinePen != null)
172172
{
@@ -213,7 +213,7 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
213213
}
214214

215215
Pen pen = null;
216-
if (this.currentTextRun is TextDrawingRun drawingRun)
216+
if (this.currentTextRun is RichTextRun drawingRun)
217217
{
218218
if (textDecorations == TextDecorations.Strikeout)
219219
{

src/ImageSharp.Drawing/Processing/TextDrawingOptions.cs renamed to src/ImageSharp.Drawing/Processing/RichTextOptions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77
namespace SixLabors.ImageSharp.Drawing.Processing
88
{
99
/// <summary>
10-
/// Provides configuration options for rendering and shaping of drawable text.
10+
/// Provides configuration options for rendering and shaping of rich text.
1111
/// </summary>
12-
public class TextDrawingOptions : TextOptions
12+
public class RichTextOptions : TextOptions
1313
{
1414
/// <summary>
15-
/// Initializes a new instance of the <see cref="TextDrawingOptions" /> class.
15+
/// Initializes a new instance of the <see cref="RichTextOptions" /> class.
1616
/// </summary>
1717
/// <param name="font">The font.</param>
18-
public TextDrawingOptions(Font font)
18+
public RichTextOptions(Font font)
1919
: base(font)
2020
{
2121
}
2222

2323
/// <summary>
24-
/// Initializes a new instance of the <see cref="TextDrawingOptions" /> class from properties
24+
/// Initializes a new instance of the <see cref="RichTextOptions" /> class from properties
2525
/// copied from the given instance.
2626
/// </summary>
2727
/// <param name="options">The options whose properties are copied into this instance.</param>
28-
public TextDrawingOptions(TextDrawingOptions options)
28+
public RichTextOptions(RichTextOptions options)
2929
: base(options)
3030
=> this.Path = options.Path;
3131

3232
/// <summary>
3333
/// Gets or sets an optional collection of text runs to apply to the body of text.
3434
/// </summary>
35-
public new IReadOnlyList<TextDrawingRun> TextRuns
35+
public new IReadOnlyList<RichTextRun> TextRuns
3636
{
37-
get => (IReadOnlyList<TextDrawingRun>)base.TextRuns;
37+
get => (IReadOnlyList<RichTextRun>)base.TextRuns;
3838
set => base.TextRuns = value;
3939
}
4040

src/ImageSharp.Drawing/Processing/TextDrawingRun.cs renamed to src/ImageSharp.Drawing/Processing/RichTextRun.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Drawing.Processing
88
/// <summary>
99
/// Represents a run of drawable text spanning a series of graphemes within a string.
1010
/// </summary>
11-
public class TextDrawingRun : TextRun
11+
public class RichTextRun : TextRun
1212
{
1313
/// <summary>
1414
/// Gets or sets the brush used for filling this run.

src/ImageSharp.Drawing/Shapes/Text/TextBuilder.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Numerics;
45
using SixLabors.Fonts;
5-
using SixLabors.ImageSharp.Drawing.Processing;
66
using SixLabors.ImageSharp.Drawing.Text;
77

88
namespace SixLabors.ImageSharp.Drawing
@@ -18,7 +18,7 @@ public static class TextBuilder
1818
/// <param name="text">The text to generate glyphs for.</param>
1919
/// <param name="textOptions">The text rendering options.</param>
2020
/// <returns>The <see cref="IPathCollection"/></returns>
21-
public static IPathCollection GenerateGlyphs(string text, TextDrawingOptions textOptions)
21+
public static IPathCollection GenerateGlyphs(string text, TextOptions textOptions)
2222
{
2323
GlyphBuilder glyphBuilder = new();
2424
TextRenderer renderer = new(glyphBuilder);
@@ -35,14 +35,31 @@ public static IPathCollection GenerateGlyphs(string text, TextDrawingOptions tex
3535
/// <param name="path">The path to draw the text in relation to</param>
3636
/// <param name="textOptions">The text rendering options.</param>
3737
/// <returns>The <see cref="IPathCollection"/></returns>
38-
public static IPathCollection GenerateGlyphs(string text, IPath path, TextDrawingOptions textOptions)
38+
public static IPathCollection GenerateGlyphs(string text, IPath path, TextOptions textOptions)
3939
{
40-
PathGlyphBuilder glyphBuilder = new(path, textOptions);
40+
TextOptions options = ConfigureOptions(textOptions, path);
41+
42+
PathGlyphBuilder glyphBuilder = new(path, options);
4143
TextRenderer renderer = new(glyphBuilder);
4244

43-
renderer.RenderText(text, textOptions);
45+
renderer.RenderText(text, options);
4446

4547
return glyphBuilder.Paths;
4648
}
49+
50+
private static TextOptions ConfigureOptions(TextOptions options, IPath path)
51+
{
52+
// When a path is specified we should explicitly follow that path
53+
// and not adjust the origin. Any translation should be applied to the path.
54+
if (path is not null && options.Origin != Vector2.Zero)
55+
{
56+
return new(options)
57+
{
58+
Origin = Vector2.Zero
59+
};
60+
}
61+
62+
return options;
63+
}
4764
}
4865
}

tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void SystemDrawing()
7979
[Benchmark]
8080
public void ImageSharp()
8181
{
82-
TextDrawingOptions textOptions = new(this.font)
82+
RichTextOptions textOptions = new(this.font)
8383
{
8484
WrappingLength = 780,
8585
Origin = new PointF(10, 10)

tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawTextOutline.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Drawing.Drawing2D;
66
using System.Linq;
77
using BenchmarkDotNet.Attributes;
8+
using SixLabors.Fonts;
89
using SixLabors.ImageSharp.Drawing.Processing;
910
using SixLabors.ImageSharp.PixelFormats;
1011
using SixLabors.ImageSharp.Processing;
@@ -27,34 +28,32 @@ public class DrawTextOutline
2728
[Benchmark(Baseline = true, Description = "System.Drawing Draw Text Outline")]
2829
public void DrawTextSystemDrawing()
2930
{
30-
using (var destination = new Bitmap(800, 800))
31-
using (var graphics = Graphics.FromImage(destination))
32-
{
33-
graphics.InterpolationMode = InterpolationMode.Default;
34-
graphics.SmoothingMode = SmoothingMode.AntiAlias;
35-
using (var pen = new System.Drawing.Pen(System.Drawing.Color.HotPink, 10))
36-
using (var font = new System.Drawing.Font("Arial", 12, GraphicsUnit.Point))
37-
using (var gp = new GraphicsPath())
38-
{
39-
gp.AddString(
40-
this.TextToRender,
41-
font.FontFamily,
42-
(int)font.Style,
43-
font.Size,
44-
new SDRectangleF(10, 10, 780, 780),
45-
new StringFormat());
31+
using var destination = new Bitmap(800, 800);
32+
using var graphics = Graphics.FromImage(destination);
33+
graphics.InterpolationMode = InterpolationMode.Default;
34+
graphics.SmoothingMode = SmoothingMode.AntiAlias;
4635

47-
graphics.DrawPath(pen, gp);
48-
}
49-
}
36+
using var pen = new System.Drawing.Pen(System.Drawing.Color.HotPink, 10);
37+
using var font = new System.Drawing.Font("Arial", 12, GraphicsUnit.Point);
38+
using var gp = new GraphicsPath();
39+
40+
gp.AddString(
41+
this.TextToRender,
42+
font.FontFamily,
43+
(int)font.Style,
44+
font.Size,
45+
new SDRectangleF(10, 10, 780, 780),
46+
new StringFormat());
47+
48+
graphics.DrawPath(pen, gp);
5049
}
5150

5251
[Benchmark(Description = "ImageSharp Draw Text Outline - Cached Glyphs")]
5352
public void DrawTextCore()
5453
{
5554
using var image = new Image<Rgba32>(800, 800);
5655
Fonts.Font font = Fonts.SystemFonts.CreateFont("Arial", 12);
57-
TextDrawingOptions textOptions = new(font)
56+
RichTextOptions textOptions = new(font)
5857
{
5958
WrappingLength = 780,
6059
Origin = new PointF(10, 10)
@@ -72,7 +71,7 @@ public void DrawTextCoreOld()
7271
using (var image = new Image<Rgba32>(800, 800))
7372
{
7473
Fonts.Font font = Fonts.SystemFonts.CreateFont("Arial", 12);
75-
TextDrawingOptions textOptions = new(font)
74+
TextOptions textOptions = new(font)
7675
{
7776
WrappingLength = 780,
7877
Origin = new PointF(10, 10)
@@ -91,7 +90,7 @@ public void DrawTextCoreOld()
9190
static IImageProcessingContext DrawTextOldVersion(
9291
IImageProcessingContext source,
9392
DrawingOptions options,
94-
TextDrawingOptions textOptions,
93+
TextOptions textOptions,
9594
string text,
9695
Brush brush,
9796
Pen pen)

0 commit comments

Comments
 (0)