Center each text line individually when drawing on image

Updated text rendering logic to center each line horizontally
based on its estimated width, ensuring multi-line text is
visually centered. Previously, all lines were drawn at a fixed
horizontal position. Both shadow and main text now use the
calculated centered position.
This commit is contained in:
MaddoScientisto 2026-02-21 15:46:03 +01:00
commit 4091fb78c5

View file

@ -271,8 +271,16 @@ public class ImageCreatorAlternate : IImageCreator
// Ensure we're drawing inside the canvas vertically // Ensure we're drawing inside the canvas vertically
if (y + finalFont.Size < 0 || y > working.Height) continue; if (y + finalFont.Size < 0 || y > working.Height) continue;
ctx.DrawText(line, finalFont, shadowColor, new SixLabors.ImageSharp.PointF(originX + 1, y + 1)); // Center each line individually so multi-line (vertical) text is justified to center
ctx.DrawText(line, finalFont, textColor, new SixLabors.ImageSharp.PointF(originX, y)); var approxWidthLine = finalFont.Size * (line?.Length ?? 0) * 0.6f;
if (approxWidthLine > working.Width)
approxWidthLine = working.Width * 0.95f;
var x = xCenterOfImg - (approxWidthLine / 2f);
x = Math.Max(0, Math.Min(x, working.Width - approxWidthLine));
ctx.DrawText(line, finalFont, shadowColor, new SixLabors.ImageSharp.PointF(x + 1, y + 1));
ctx.DrawText(line, finalFont, textColor, new SixLabors.ImageSharp.PointF(x, y));
} }
}); });
} }