Skip to content

Commit 7797d99

Browse files
authored
Merge pull request SixLabors#1008 from Turnerj/fix-issue-688
Fix horizontally out-of-bounds error when drawing text
2 parents ee7471f + 05fd9c7 commit 7797d99

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,31 @@ void Draw(List<DrawingOperation> operations, IBrush brush)
9090
Buffer2D<float> buffer = operation.Map;
9191
int startY = operation.Location.Y;
9292
int startX = operation.Location.X;
93-
int offSetSpan = 0;
93+
int offsetSpan = 0;
9494
if (startX < 0)
9595
{
96-
offSetSpan = -startX;
96+
offsetSpan = -startX;
9797
startX = 0;
9898
}
9999

100-
int fistRow = 0;
100+
if (startX >= source.Width)
101+
{
102+
continue;
103+
}
104+
105+
int firstRow = 0;
101106
if (startY < 0)
102107
{
103-
fistRow = -startY;
108+
firstRow = -startY;
104109
}
105110

106111
int maxHeight = source.Height - startY;
107112
int end = Math.Min(operation.Map.Height, maxHeight);
108113

109-
for (int row = fistRow; row < end; row++)
114+
for (int row = firstRow; row < end; row++)
110115
{
111116
int y = startY + row;
112-
Span<float> span = buffer.GetRowSpan(row).Slice(offSetSpan);
117+
Span<float> span = buffer.GetRowSpan(row).Slice(offsetSpan);
113118
app.Apply(span, startX, y);
114119
}
115120
}

tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ public void DoesntThrowExceptionWhenOverlappingRightEdge_Issue688<TPixel>(TestIm
6565
}
6666
}
6767

68+
[Theory]
69+
[WithSolidFilledImages(1500, 500, "White", PixelTypes.Rgba32)]
70+
public void DoesntThrowExceptionWhenOverlappingRightEdge_Issue688_2<TPixel>(TestImageProvider<TPixel> provider)
71+
where TPixel : struct, IPixel<TPixel>
72+
{
73+
using (Image<TPixel> img = provider.GetImage())
74+
{
75+
Font font = CreateFont("OpenSans-Regular.ttf", 39);
76+
string text = new string('a', 10000); // exception
77+
// string text = "Hello"; // no exception
78+
Rgba32 color = Rgba32.Black;
79+
var point = new PointF(100, 100);
80+
81+
img.Mutate(ctx => ctx.DrawText(text, font, color, point));
82+
}
83+
}
84+
85+
6886
[Theory]
6987
[WithSolidFilledImages(200, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "SixLaborsSampleAB.woff", AB)]
7088
[WithSolidFilledImages(900, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "OpenSans-Regular.ttf", TestText)]

0 commit comments

Comments
 (0)