Skip to content

Commit 91fe704

Browse files
Normalize pixel clamping
1 parent 00b054f commit 91fe704

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara
142142
new RectangleF(bounds.Location, new(bounds.Width, bounds.Height)),
143143
this.drawingOptions.Transform);
144144

145-
PointF subPixelOffset = currentBounds.Location - Point.Truncate(currentBounds.Location);
145+
PointF subPixelOffset = currentBounds.Location - ClampToPixel(currentBounds.Location);
146146
subPixelOffset.X = MathF.Round(subPixelOffset.X * AccuracyMultiple) / AccuracyMultiple;
147147
subPixelOffset.Y = MathF.Round(subPixelOffset.Y * AccuracyMultiple) / AccuracyMultiple;
148148

@@ -237,11 +237,9 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
237237
Vector2 bl = start + pad;
238238
Vector2 tr = end - pad;
239239

240-
tl.Y = MathF.Round(tl.Y, MidpointRounding.AwayFromZero);
241-
tr.Y = MathF.Round(tr.Y, MidpointRounding.AwayFromZero);
242-
bl.Y = MathF.Round(bl.Y, MidpointRounding.AwayFromZero);
243-
tl.X = MathF.Round(tl.X, MidpointRounding.AwayFromZero);
244-
tr.X = MathF.Round(tr.X, MidpointRounding.AwayFromZero);
240+
tl = ClampToPixel(tl);
241+
bl = ClampToPixel(bl);
242+
tr = ClampToPixel(tr);
245243

246244
// Always respect the pen stroke width if explicitly set.
247245
if (pen is null)
@@ -347,7 +345,7 @@ protected override void EndGlyph()
347345
{
348346
this.DrawingOperations.Add(new DrawingOperation
349347
{
350-
Location = Point.Truncate(path.Bounds.Location),
348+
Location = ClampToPixel(path.Bounds.Location),
351349
Map = renderData.FillMap,
352350
Brush = this.currentBrush,
353351
RenderPass = RenderOrderFill
@@ -358,7 +356,7 @@ protected override void EndGlyph()
358356
{
359357
this.DrawingOperations.Add(new DrawingOperation
360358
{
361-
Location = Point.Truncate(path.Bounds.Location),
359+
Location = ClampToPixel(path.Bounds.Location),
362360
Map = renderData.OutlineMap,
363361
Brush = this.currentPen?.StrokeFill ?? this.currentBrush,
364362
RenderPass = RenderOrderOutline
@@ -376,6 +374,10 @@ protected override void EndText()
376374

377375
public void Dispose() => this.Dispose(true);
378376

377+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
378+
private static Point ClampToPixel(PointF point)
379+
=> Point.Truncate(point);
380+
379381
private void FinalizeDecoration(ref TextDecorationDetails? decoration)
380382
{
381383
if (decoration != null)
@@ -398,7 +400,7 @@ private void FinalizeDecoration(ref TextDecorationDetails? decoration)
398400
this.DrawingOperations.Add(new DrawingOperation
399401
{
400402
Brush = decoration.Value.Pen.StrokeFill,
401-
Location = Point.Truncate(outline.Bounds.Location),
403+
Location = ClampToPixel(outline.Bounds.Location),
402404
Map = this.Render(outline),
403405
RenderPass = RenderOrderDecoration
404406
});

0 commit comments

Comments
 (0)