Improve text overlay for rotated and multi-line images

- Use vertical text variant (TestoFirmaV) for rotated images to ensure correct orientation of overlays.
- Adjust font sizing logic for multi-line text to better fit within image bounds, accounting for line count and spacing.
- Set rotation flags (FotoRuotaADestra, FotoRuotaASinistra) before drawing text, enabling correct text variant selection.
- Ensure vertical text field is initialized from settings for consistent behavior.
This commit is contained in:
MaddoScientisto 2026-02-21 15:43:26 +01:00
commit 7e1ca95a25

View file

@ -54,6 +54,9 @@ public class ImageCreatorAlternate : IImageCreator
ExtractExif(img, imgState);
// Apply orientation
// Set rotation flags and apply orientation so downstream logic can
// use imgState.FotoRuotaADestra / FotoRuotaASinistra to decide which
// text to draw (horizontal vs vertical).
ApplyExifOrientation(img, imgState);
// Determine output format
@ -144,8 +147,11 @@ public class ImageCreatorAlternate : IImageCreator
}
}
// Prepare text
var text = isThumbnail ? imgState.TestoFirmaPiccola : imgState.TestoFirma;
// Prepare text. For rotated (vertical) photos prefer the vertical text
// variant coming from the viewmodel/settings (TestoFirmaStartV).
var text = isThumbnail
? imgState.TestoFirmaPiccola
: ((imgState.FotoRuotaADestra || imgState.FotoRuotaASinistra) ? imgState.TestoFirmaV : imgState.TestoFirma);
if (string.IsNullOrEmpty(text) && _picSettings.TestoNome)
text = imgState.NomeFileBig;
@ -179,6 +185,7 @@ public class ImageCreatorAlternate : IImageCreator
}
var shadowColor = SixLabors.ImageSharp.Color.FromRgba(0, 0, 0, (byte)imgState.AlphaScelta);
// Find best font size so text fits image width (allow 95% width) and not exceed ~15% height
var maxTextWidth = working.Width * 0.95f;
var maxTextHeight = working.Height * 0.15f;
@ -188,7 +195,14 @@ public class ImageCreatorAlternate : IImageCreator
var lines = normalizedText.Split(new[] { '\n' }, StringSplitOptions.None);
var longestLine = lines.OrderByDescending(l => l.Length).FirstOrDefault() ?? string.Empty;
var chosenSize = FindBestFontSize(longestLine, font.Name, Math.Max(6, baseSize), maxTextWidth, maxTextHeight);
// Use a line spacing factor when computing allowed font size so multi-line
// text is sized to fit vertically. The existing FindBestFontSize helper
// approximates single-line height; to account for multiple lines pass a
// reduced maxHeight to that helper.
var lineSpacing = 1.1f; // small extra spacing between lines
var adjustedMaxHeight = Math.Max(6f, maxTextHeight / (lines.Length * lineSpacing));
var chosenSize = FindBestFontSize(longestLine, font.Name, Math.Max(6, baseSize), maxTextWidth, adjustedMaxHeight);
// Use final font
var finalFont = SixLabors.Fonts.SystemFonts.CreateFont(font.Name, chosenSize);
@ -197,7 +211,6 @@ public class ImageCreatorAlternate : IImageCreator
// Approximate measured size since TextMeasurer/RendererOptions may not be available in this Fonts version
var approxWidth = finalFont.Size * longestLine.Length * 0.6f;
// Account for multiple lines: height = font size * lineCount * lineSpacing
var lineSpacing = 1.1f; // small extra spacing between lines
var approxHeight = finalFont.Size * lines.Length * lineSpacing;
// Compute horizontal position based on alignment
@ -442,6 +455,9 @@ public class ImageCreatorAlternate : IImageCreator
// Set minimal text fields so text drawing has fallback values
imgState.TestoFirma ??= _picSettings.TestoFirmaStart ?? string.Empty;
// Ensure vertical text is populated from settings so callers can
// select the proper variant when drawing vertical photos.
imgState.TestoFirmaV ??= _picSettings.TestoFirmaStartV ?? string.Empty;
imgState.TestoFirmaPiccola ??= string.Empty;
imgState.DataPartenzaI = _picSettings.DataPartenza;
imgState.TestoOrario = _picSettings.TestoOrario ?? string.Empty;
@ -498,6 +514,30 @@ public class ImageCreatorAlternate : IImageCreator
private void ApplyExifOrientation(Image<Rgba32> img, ImageState imgState)
{
// Common EXIF orientations: 1=TopLeft, 3=BottomRight (rotate 180), 6=RightTop (rotate 90 CW), 8=LeftBottom (rotate 270 CW)
// Set rotation flags on the state so other code can pick the correct
// text variant (vertical vs horizontal). Mirror ImageCreatorSharp logic.
imgState.FotoRuotaADestra = false;
imgState.FotoRuotaASinistra = false;
if (_picSettings.UsaRotazioneAutomatica)
{
switch (imgState.Orientation)
{
case Orientations.BottomLeft:
case Orientations.BottomRight:
case Orientations.LeftTop:
case Orientations.LftBottom:
imgState.FotoRuotaASinistra = true;
break;
case Orientations.RightBottom:
case Orientations.RightTop:
case Orientations.TopLeft:
case Orientations.TopRight:
// no-op
break;
}
}
switch (imgState.Orientation)
{
case Orientations.RightTop: // 6