Merge branch 'magic_const_refactor'

* magic_const_refactor:
  Fixed magic number in CalcWatermarkOrigin method
This commit is contained in:
Andrew
2026-06-04 08:29:47 +03:00
@@ -39,43 +39,68 @@ namespace Watermark.Net.src.WatermarkNet.Core
} }
/// <summary> /// <summary>
/// Calculates origin point for text watermark based on position and size. /// Calculates precise origin point for text watermark based on position and metrics.
/// </summary> /// </summary>
private PointF CalcWatermarkOrigin(int width, int height, float watermarkSize, ImagePosition position) private PointF CalcWatermarkOrigin(int width, int height, float watermarkSize, ImagePosition position, FontRectangle textSize)
{ {
var origin = new PointF(0, 0); var origin = new PointF(0, 0);
//Static value 1 pt is 72 px per inch var textPading = height / 10; // Minimum padding from edges
var pixelsPerInch = 72;
var wmHeight = watermarkSize / pixelsPerInch;
switch (position) switch (position)
{ {
case ImagePosition.TopLeft: case ImagePosition.TopLeft:
origin = new PointF(watermarkSize / 2, wmHeight / 2); origin = new PointF(
Math.Max(0, -textSize.Left) + textPading,
Math.Max(0, -textSize.Top) + textPading
);
break; break;
case ImagePosition.TopCenter: case ImagePosition.TopCenter:
origin = new PointF(width / 2, wmHeight / 2); origin = new PointF(
Math.Max(0, width / 2 - textSize.Width / 2 - textSize.Left),
Math.Max(0, -textSize.Top) + textPading
);
break; break;
case ImagePosition.TopRight: case ImagePosition.TopRight:
origin = new PointF(width - watermarkSize * 2, wmHeight / 2); origin = new PointF(
Math.Max(0, width - textSize.Width - textSize.Left) - textPading,
Math.Max(0, -textSize.Top) + textPading
);
break; break;
case ImagePosition.CenterLeft: case ImagePosition.CenterLeft:
origin = new PointF(watermarkSize / 2, height / 2.5f); origin = new PointF(
Math.Max(0, -textSize.Left) + textPading,
Math.Max(0, height / 2 - textSize.Height / 2 - textSize.Top)
);
break; break;
case ImagePosition.Center: case ImagePosition.Center:
origin = new PointF(width / 2, height / 2.5f); origin = new PointF(
Math.Max(0, width / 2 - textSize.Width / 2 - textSize.Left),
Math.Max(0, height / 2 - textSize.Height / 2 - textSize.Top)
);
break; break;
case ImagePosition.CenterRight: case ImagePosition.CenterRight:
origin = new PointF(width - watermarkSize * 2, height / 2.5f); origin = new PointF(
Math.Max(0, width - textSize.Width - textSize.Left) - textPading,
Math.Max(0, height / 2 - textSize.Height / 2 - textSize.Top)
);
break; break;
case ImagePosition.BottomLeft: case ImagePosition.BottomLeft:
origin = new PointF(watermarkSize / 2, height - watermarkSize * 2); origin = new PointF(
Math.Max(0, - textSize.Left) + textPading,
Math.Max(0, height - textSize.Height - textSize.Top) - textPading
);
break; break;
case ImagePosition.BottomCenter: case ImagePosition.BottomCenter:
origin = new PointF(width / 2, height - watermarkSize * 2); origin = new PointF(
Math.Max(0, width / 2 - textSize.Width / 2 - textSize.Left),
Math.Max(0, height - textSize.Height - textSize.Top) - textPading
);
break; break;
case ImagePosition.BottomRight: case ImagePosition.BottomRight:
origin = new PointF(width - watermarkSize * 2, height - watermarkSize * 2); origin = new PointF(
Math.Max(0, width - textSize.Width - textSize.Left) - textPading,
Math.Max(0, height - textSize.Height - textSize.Top) - textPading
);
break; break;
default: default:
break; break;
@@ -167,7 +192,7 @@ namespace Watermark.Net.src.WatermarkNet.Core
var textOptions = new RichTextOptions(scaledFont) var textOptions = new RichTextOptions(scaledFont)
{ {
ColorFontSupport = ColorFontSupport.MicrosoftColrFormat, ColorFontSupport = ColorFontSupport.MicrosoftColrFormat,
Origin = CalcWatermarkOrigin(imgSize.Width, imgSize.Height, scaledFont.Size, watermark.Layout.Position), Origin = CalcWatermarkOrigin(imgSize.Width, imgSize.Height, scaledFont.Size, watermark.Layout.Position, size),
HorizontalAlignment = HorizontalAlignmentFromPosition(watermark.Layout.Position), HorizontalAlignment = HorizontalAlignmentFromPosition(watermark.Layout.Position),
VerticalAlignment = VerticalAlignment.Top, VerticalAlignment = VerticalAlignment.Top,
}; };
@@ -176,7 +201,7 @@ namespace Watermark.Net.src.WatermarkNet.Core
{ {
foreach (ImagePosition position in (ImagePosition[])Enum.GetValues(typeof(ImagePosition))) foreach (ImagePosition position in (ImagePosition[])Enum.GetValues(typeof(ImagePosition)))
{ {
textOptions.Origin = CalcWatermarkOrigin(imgSize.Width, imgSize.Height, scaledFont.Size, position); textOptions.Origin = CalcWatermarkOrigin(imgSize.Width, imgSize.Height, scaledFont.Size, position, size);
textOptions.HorizontalAlignment = HorizontalAlignmentFromPosition(position); textOptions.HorizontalAlignment = HorizontalAlignmentFromPosition(position);
processingContext.DrawText(textOptions, watermark.Text, watermark.Style.Color); processingContext.DrawText(textOptions, watermark.Text, watermark.Style.Color);
} }