C2022-004 Logic to decode ARGB colors and logic to not rotate watermarks that are only one or two characters long

This commit is contained in:
John Jenko 2022-02-23 21:03:37 +00:00
parent 78e408706e
commit 311e597338

View File

@ -1151,6 +1151,38 @@ namespace Volian.Svg.Library
get { return _AllowAllWatermarks; }
set { _AllowAllWatermarks = value; }
}
//C2022-004 This will return the system drawing color from either the color name or ARGB values
public System.Drawing.Color GetWatermarkColor(string colorDescription)
{
System.Drawing.Color systemColor;
if (colorDescription.StartsWith("[A=")) // if color is ARGB representation, parse out the values
{
// initialize the ARGB value to white
int Alpha = 255;
int Red = 255;
int Green = 255;
int Blue = 255;
// The color is formatted as ARGB in a string (i.e. "[A=255, R=157, G=187, B=97]") in the format file
string argbColor = colorDescription.Replace("[", "").Replace("]", "").Replace(" ", ""); // remove starting and ending square brackets
// split the string on the commas and find each ARGB value
string[] argbSplit = argbColor.Split(',');
foreach (string s in argbSplit)
{
if (s[0] == 'A')
Alpha = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'R')
Red = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'G')
Green = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'B')
Blue = Convert.ToInt32(s.Substring(2));
}
systemColor = System.Drawing.Color.FromArgb(Alpha, Red, Green, Blue);
}
else
systemColor = System.Drawing.Color.FromName(colorDescription); // get from color name that was passed in
return systemColor;
}
private void DrawWatermark(PdfContentByte cb)
{
if (Watermark == null || Watermark.ToLower().Contains("none") || Watermark == "") return;
@ -1166,7 +1198,7 @@ namespace Volian.Svg.Library
System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix((float)Math.Cos(a), (float)Math.Sin(a), (float)-Math.Sin(a), (float)Math.Cos(a), 0, cb.PdfDocument.PageSize.Height);
cb.Transform(myMatrix);
}
SvgWatermark myWatermark = new SvgWatermark(cb, Watermark, System.Drawing.Color.FromName(WatermarkColor), .15F);
SvgWatermark myWatermark = new SvgWatermark(cb, Watermark, GetWatermarkColor(WatermarkColor), .15F);
//myWatermark.SetSquareDotPattern(.7F);
myWatermark.Draw();
if (_WatermarkLayer != null) cb.EndLayer();
@ -1408,19 +1440,26 @@ namespace Volian.Svg.Library
//g.DrawRectangle(System.Drawing.Pens.Cyan, 0, 0, bnds.Width, bnds.Height);
}
_WatermarkImage = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png);
_WatermarkImage.SetAbsolutePosition(72, 72);
float H = _ContentByte.PdfWriter.PageSize.Height - 144;
float W = _ContentByte.PdfWriter.PageSize.Width - 144;
float ratio = bnds.Width / bnds.Height;
float W2 = (H - W / ratio) / (ratio - 1 / ratio);
float W1 = W - W2;
float H1 = (float)(Math.Sqrt(-4*(W1*W2)+H*H)+H)/2;
float H2 = H-H1;
float Theta = (float) Math.Atan(H1 / W1);
float h = H2 / (float) Math.Cos(Theta);
float w = H1 / (float) Math.Sin(Theta);
_WatermarkImage.Rotation = Theta; // 3.141592653F / 4;
_WatermarkImage.ScaleAbsolute(w, h);
_WatermarkImage.SetAbsolutePosition(72, 72);
float H = _ContentByte.PdfWriter.PageSize.Height - 144;
float W = _ContentByte.PdfWriter.PageSize.Width - 144;
float ratio = bnds.Width / bnds.Height;
float W2 = (H - W / ratio) / (ratio - 1 / ratio);
float W1 = W - W2;
float H1 = (float)(Math.Sqrt(-4 * (W1 * W2) + H * H) + H) / 2;
float H2 = H - H1;
float Theta = (float)Math.Atan(H1 / W1);
float h = H2 / (float)Math.Cos(Theta);
float w = H1 / (float)Math.Sin(Theta);
// C2022-004 if the watermark is only one character, don't rotate and scale
if (msg.Length > 2)
{
_WatermarkImage.Rotation = Theta; // 3.141592653F / 4;
_WatermarkImage.ScaleAbsolute(w, h);
}
else
_WatermarkImage.ScaleAbsolute(W, H);// C2022-004 if the watermark is only one character, scale with respect to page size
}
return _WatermarkImage;
}