Files
SourceCode/PROMS/Volian.Svg.Library/SvgScale.cs
T

81 lines
3.8 KiB
C#

using System.Drawing;
namespace Volian.Svg.Library
{
public class SvgScale
{
public float XLowerLimit { get; set; }
public float YLowerLimit { get; set; }
private float _Scale; // Scale from Logical to Physical
public float Scale => _Scale;
private float _Width; // Physical Width
public float Width => _Width;
private float _Height; // Physical Height
public float Height => _Height;
public float XUpperLimit => _Width * _Scale + XLowerLimit;
public float YUpperLimit => _Height * _Scale + YLowerLimit;
private float _DPI;
public float DPI => _DPI;
private SvgScale(SvgScale tmp,SvgMeasurement newX, SvgMeasurement newY)
{
_DPI = tmp.DPI;
_Scale = tmp.Scale;
_Width = tmp.Width;
_Height = tmp.Height;
XLowerLimit = tmp.XLowerLimit - newX.GetSizeInPixels(DPI);
YLowerLimit = tmp.YLowerLimit - newY.GetSizeInPixels(DPI);
}
public SvgScale(float myDPI, RectangleF bounds,SvgMeasurement width, SvgMeasurement height, SvgViewBox myViewBox)
{
// TODO: Should I convert to pixels or something fixed (Inches, Points, Picas, CMs, MMs, Twips)
_DPI = myDPI;
float outputWidth = width.GetSizeInPixels(100);
if(outputWidth == 0) outputWidth = bounds.Width;
float outputHeight = height.GetSizeInPixels(100);
if (outputHeight == 0) outputHeight = bounds.Height;
float inputWidth = myViewBox.Width;
if (inputWidth == 0) inputWidth = outputWidth;
float scalex = outputWidth / inputWidth;
float inputHeight = myViewBox.Height;
if (inputHeight == 0) inputHeight = outputHeight;
float scaley = outputHeight / inputHeight;
_Scale = (scalex < scaley) ? scaley : scalex;
//Console.WriteLine("'Scale',{0}",_Scale);
_Width = outputWidth / _Scale;
_Height = outputHeight / _Scale;
XLowerLimit = myViewBox.X + (inputWidth - _Width) / 2;
YLowerLimit = myViewBox.Y + (inputHeight - _Height) / 2;
//if (myDPI != 96) Console.WriteLine("DPI {0}", myDPI);
//Console.WriteLine("Scale,{0},{1},{2},{3},{4},{5},{6}", Scale, Width, Height, XLowerLimit, XUpperLimit, YLowerLimit, YUpperLimit);
}
public SvgScale AdjustOrigin(SvgMeasurement newX, SvgMeasurement newY) => new SvgScale(this, newX, newY);
public float X(float x) => _Scale * (x - XLowerLimit);
public float Y(float y) => _Scale * (y - YLowerLimit);
public float AbsX(SvgMeasurement x) => x.GetSizeInPixels(DPI) + Svg.AbsoluteOffset.X;
public float AbsY(iTextSharp.text.pdf.PdfContentByte cb, SvgMeasurement y)
{
// the following line of code is 'taken' from the 16bit code: \promsnt\exe\wined\togdi.cpp
// whenever doing an 'Absolute' macro.
float yOffset = 54 * 4.8f + 0.5f; // yOffset starts in 'twips' (twip = 1/20 point)
yOffset = yOffset / 20; // convert from twips to points (point = 1/72 inch)
return cb.PdfDocument.PageSize.Height - y.GetSizeInPixels(DPI) + yOffset + Svg.AbsoluteOffset.Y;
}
public float Y(iTextSharp.text.pdf.PdfContentByte cb, float y) => cb.PdfDocument.PageSize.Height - _Scale * (y - YLowerLimit);
public float M(float m) => _Scale * m;
public float X(SvgMeasurement x)
{
float retval = _Scale * (x.GetSizeInPixels(DPI) - XLowerLimit);
return retval;
}
public float Y(SvgMeasurement y) => _Scale * (y.GetSizeInPixels(DPI) - YLowerLimit);
public float Y(iTextSharp.text.pdf.PdfContentByte cb, SvgMeasurement y)
{
float yOffset = 0; // = (cb.PdfWriter.CurrentPageNumber -1) * .8F * cb.PdfDocument.PageSize.Height;
float myY = yOffset + cb.PdfDocument.PageSize.Height - _Scale * (y.GetSizeInPixels(DPI) - YLowerLimit);
return myY;
}
public float YY(SvgMeasurement y) => -_Scale * y.GetSizeInPixels(DPI);
public float M(SvgMeasurement m) => _Scale * m.GetSizeInPixels(DPI);
}
}