posizione Testo
This commit is contained in:
parent
a2dca4b750
commit
922315061d
5 changed files with 285 additions and 27 deletions
|
|
@ -9,7 +9,9 @@ using MaddoLibrary.Base.Log;
|
|||
using SixLabors.Fonts;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Drawing;
|
||||
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using SixLabors.Primitives;
|
||||
using Font = SixLabors.Fonts.Font;
|
||||
using FontFamily = SixLabors.Fonts.FontFamily;
|
||||
using FontStyle = SixLabors.Fonts.FontStyle;
|
||||
|
|
@ -36,10 +38,25 @@ namespace CatalogLib
|
|||
{
|
||||
MaddoLogger.Log("Loaded Image: {0}", workFile.FullName);
|
||||
//image.Rotate(-90);
|
||||
bool isRotated;
|
||||
|
||||
//if (PicSettings.Instance.r)
|
||||
image.Mutate(x => x.Resize(PicSettings.Instance.FotoAltezza, PicSettings.Instance.FotoLarghezza, new BoxResampler()));
|
||||
var orientation = image.MetaData.ExifProfile.GetValue(ExifTag.Orientation);
|
||||
if ((ushort)orientation.Value != 1)
|
||||
{
|
||||
isRotated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isRotated = false;
|
||||
}
|
||||
|
||||
if (PicSettings.Instance.FotoRidimensiona)
|
||||
{
|
||||
// todo calcolare ridimensionamento
|
||||
image.Mutate(x => x.Resize(PicSettings.Instance.FotoAltezza, PicSettings.Instance.FotoLarghezza, new BicubicResampler()));
|
||||
}
|
||||
|
||||
|
||||
if (PicSettings.Instance.GeneraleRotazioneAutomatica)
|
||||
{
|
||||
image.Mutate(img => img.AutoOrient());
|
||||
|
|
@ -107,7 +124,10 @@ namespace CatalogLib
|
|||
|
||||
if (PicSettings.Instance.EnableText)
|
||||
{
|
||||
SetExtraText(image);
|
||||
|
||||
SetExtraText(image, isRotated);
|
||||
|
||||
|
||||
MaddoLogger.Log("Drawn text on Image: {0}", workFile.FullName);
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +150,7 @@ namespace CatalogLib
|
|||
MaddoLogger.Log("Time Taken for {0}: {1}", workFile.FullName, s.Elapsed);
|
||||
}
|
||||
|
||||
private void SetExtraText(Image<Rgba32> image)
|
||||
private void SetExtraText(Image<Rgba32> image, bool isRotated)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(PicSettings.Instance.TestoApplicareOrizzontale))
|
||||
{
|
||||
|
|
@ -138,6 +158,24 @@ namespace CatalogLib
|
|||
return;
|
||||
}
|
||||
|
||||
string text;
|
||||
if (isRotated)
|
||||
{
|
||||
if (PicSettings.Instance.TestoApplicareOrizzontale.Contains("$_"))
|
||||
{
|
||||
text = PicSettings.Instance.TestoApplicareOrizzontale.Replace("$_", "\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
text = PicSettings.Instance.TestoApplicareOrizzontale.Replace("$_", "");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
text = PicSettings.Instance.TestoApplicareOrizzontale.Replace("$_", "");
|
||||
}
|
||||
|
||||
|
||||
var fo = SixLabors.Fonts.SystemFonts.Find(PicSettings.Instance.NomeFont);
|
||||
|
||||
|
|
@ -145,12 +183,27 @@ namespace CatalogLib
|
|||
//var fff = FontCollection.SystemFonts.Find(PicSettings.Instance.NomeFont);
|
||||
//var fff = FontCollection.SystemFonts.Find("Segoe Print");
|
||||
|
||||
var font = new Font(fo, (float)PicSettings.Instance.DimensioneFont, FontStyle.Regular);
|
||||
Font font;
|
||||
|
||||
if (!PicSettings.Instance.Grassetto)
|
||||
{
|
||||
font = new Font(fo, (float)PicSettings.Instance.DimensioneFont, FontStyle.Regular);
|
||||
}
|
||||
else
|
||||
{
|
||||
font = new Font(fo, (float)PicSettings.Instance.DimensioneFont, FontStyle.Bold);
|
||||
}
|
||||
// todo corsivo
|
||||
|
||||
|
||||
|
||||
//var font = new Font(fff, 8f, FontStyle.Regular);
|
||||
|
||||
|
||||
//Color c = Color.FromHex(FlipRgbString(PicSettings.Instance.ColoreTestoRGB));
|
||||
Rgba32 g = Rgba32.FromHex(FlipRgbString(PicSettings.Instance.ColoreTestoRGB));
|
||||
|
||||
Rgba32 gBack = Rgba32.Black; // todo alpha
|
||||
//TextMeasurer measurer = new TextMeasurer();
|
||||
|
||||
//var size = measurer.MeasureText(PicSettings.Instance.TestoApplicareOrizzontale, font, 72);
|
||||
|
|
@ -161,7 +214,108 @@ namespace CatalogLib
|
|||
|
||||
Vector2 center = new Vector2(image.Width / 2, image.Height / 2); //center horizontally, 10px down
|
||||
|
||||
var size = TextMeasurer.Measure(PicSettings.Instance.TestoApplicareOrizzontale, new RendererOptions(font));
|
||||
var size = TextMeasurer.Measure(text, new RendererOptions(font));
|
||||
|
||||
var larghezzaStandard = size.Width;
|
||||
var dimensioneStandard = (int) Math.Round(PicSettings.Instance.DimensioneFont);
|
||||
if (size.Width > image.Width)
|
||||
{
|
||||
var c = dimensioneStandard;
|
||||
do
|
||||
{
|
||||
if (c > 20)
|
||||
{
|
||||
c -= 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
c -= 1;
|
||||
}
|
||||
|
||||
if (PicSettings.Instance.Grassetto)
|
||||
{
|
||||
font = new Font(fo, c, FontStyle.Bold);
|
||||
}
|
||||
else
|
||||
{
|
||||
font = new Font(fo, c, FontStyle.Regular);
|
||||
}
|
||||
size = TextMeasurer.Measure(text,
|
||||
new RendererOptions(font));
|
||||
if (size.Width < image.Width)
|
||||
{
|
||||
larghezzaStandard = (int) Math.Round(size.Width);
|
||||
break;
|
||||
}
|
||||
if (c <= 5)
|
||||
{
|
||||
break;
|
||||
}
|
||||
} while (dimensioneStandard == c);
|
||||
}
|
||||
|
||||
float yPosFromBottom = 0;
|
||||
|
||||
switch (PicSettings.Instance.TextPosition)
|
||||
{
|
||||
case PicSettings.Positions.Alto:
|
||||
yPosFromBottom = PicSettings.Instance.Margine;
|
||||
break;
|
||||
case PicSettings.Positions.Basso:
|
||||
yPosFromBottom = image.Height - size.Height - (image.Height * PicSettings.Instance.Margine / 100);
|
||||
break;
|
||||
}
|
||||
|
||||
float xCenterofImg = 0;
|
||||
|
||||
// stringformat
|
||||
|
||||
switch (PicSettings.Instance.TextAlignment)
|
||||
{
|
||||
case PicSettings.Alignments.Sinistra:
|
||||
xCenterofImg = PicSettings.Instance.Margine + (larghezzaStandard / 2);
|
||||
if ((larghezzaStandard / 2) > (image.Width / 2) - PicSettings.Instance.Margine)
|
||||
{
|
||||
xCenterofImg = image.Width / 2;
|
||||
}
|
||||
break;
|
||||
case PicSettings.Alignments.Centro:
|
||||
xCenterofImg = image.Width / 2;
|
||||
break;
|
||||
case PicSettings.Alignments.Destra:
|
||||
xCenterofImg = image.Width - PicSettings.Instance.Margine - larghezzaStandard / 2;
|
||||
|
||||
if (larghezzaStandard / 2 > image.Width / 2 - PicSettings.Instance.Margine)
|
||||
{
|
||||
xCenterofImg = image.Width / 2;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
// stringformat alignment center
|
||||
|
||||
if (PicSettings.Instance.Grassetto)
|
||||
{
|
||||
font = new Font(fo, dimensioneStandard, FontStyle.Bold);
|
||||
}
|
||||
else
|
||||
{
|
||||
font = new Font(fo, dimensioneStandard, FontStyle.Regular);
|
||||
}
|
||||
|
||||
image.Mutate(x => x.DrawText(text, font, gBack, new PointF(xCenterofImg + 1 , yPosFromBottom + 1), new TextGraphicsOptions()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Center
|
||||
}));
|
||||
|
||||
image.Mutate(x => x.DrawText(text, font, g, new PointF(xCenterofImg, yPosFromBottom), new TextGraphicsOptions()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Center
|
||||
}));
|
||||
|
||||
return;
|
||||
|
||||
|
||||
float scalingFactor = Math.Min(image.Width / size.Width, image.Height / size.Height);
|
||||
Font scaledFont = new Font(font, scalingFactor * font.Size);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue