Skip to content

Commit 060d03e

Browse files
Fix oob rendering when drawing text.
1 parent 85721f8 commit 060d03e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void Draw(IEnumerable<DrawingOperation> operations)
9999
for (int row = firstRow; row < end; row++)
100100
{
101101
int y = startY + row;
102-
Span<float> span = buffer.DangerousGetRowSpan(row)[offsetSpan..];
102+
Span<float> span = buffer.DangerousGetRowSpan(row).Slice(offsetSpan, Math.Min(buffer.Width - offsetSpan, source.Width));
103103
app.Apply(span, startX, y);
104104
}
105105
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,48 @@ public void CanDrawTextVerticalMixed<TPixel>(TestImageProvider<TPixel> provider)
887887
comparer: ImageComparer.TolerantPercentage(0.002f));
888888
}
889889

890+
[Theory]
891+
[WithBlankImage(200, 200, PixelTypes.Rgba32)]
892+
public void CanRenderTextOutOfBoundsIssue301<TPixel>(TestImageProvider<TPixel> provider)
893+
where TPixel : unmanaged, IPixel<TPixel>
894+
=> provider.VerifyOperation(
895+
ImageComparer.TolerantPercentage(0.01f),
896+
img =>
897+
{
898+
Font font = CreateFont(TestFonts.OpenSans, 70);
899+
900+
const string txt = "V";
901+
FontRectangle size = TextMeasurer.MeasureBounds(txt, new TextOptions(font));
902+
903+
img.Mutate(x => x.Resize((int)size.Width, (int)size.Height));
904+
905+
RichTextOptions options = new(font)
906+
{
907+
HorizontalAlignment = HorizontalAlignment.Center,
908+
VerticalAlignment = VerticalAlignment.Center,
909+
Origin = new Vector2(size.Width / 2, size.Height / 2)
910+
};
911+
912+
LinearGradientBrush brush = new(
913+
new PointF(0, 0),
914+
new PointF(20, 20),
915+
GradientRepetitionMode.Repeat,
916+
new ColorStop(0, Color.Red),
917+
new ColorStop(0.5f, Color.Green),
918+
new ColorStop(0.5f, Color.Yellow),
919+
new ColorStop(1f, Color.Blue));
920+
921+
img.Mutate(m => m.DrawText(options, txt, brush));
922+
},
923+
false,
924+
false);
925+
890926
private static string Repeat(string str, int times) => string.Concat(Enumerable.Repeat(str, times));
891927

892928
private static string ToTestOutputDisplayText(string text)
893929
{
894930
string fnDisplayText = text.Replace("\n", string.Empty);
895-
return fnDisplayText.Substring(0, Math.Min(fnDisplayText.Length, 4));
931+
return fnDisplayText[..Math.Min(fnDisplayText.Length, 4)];
896932
}
897933

898934
private static Font CreateFont(string fontName, float size)
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)