2010-03-25 19:38:51 +00:00

120 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Volian.Svg.Library
{
public class SvgScale
{
private float _XLowerLimit;
public float XLowerLimit
{
get { return _XLowerLimit; }
set { _XLowerLimit = value; }
}
private float _YLowerLimit;
public float YLowerLimit
{
get { return _YLowerLimit; }
set { _YLowerLimit = value; }
}
private float _Scale; // Scale from Logical to Physical
public float Scale
{ get { return _Scale; } }
private float _Width; // Physical Width
public float Width
{ get { return _Width; } }
private float _Height; // Physical Height
public float Height
{ get { return _Height; } }
public float XUpperLimit
{ get { return _Width * _Scale + _XLowerLimit; } }
public float YUpperLimit
{ get { return _Height * _Scale + _YLowerLimit; } }
private float _DPI;
public float DPI
{ get { return _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)
{
return new SvgScale(this, newX, newY);
}
public float X(float x)
{
return _Scale * (x - XLowerLimit);
}
public float Y(float y)
{
return _Scale * (y - YLowerLimit);
}
public float Y(iTextSharp.text.pdf.PdfContentByte cb, float y)
{
return cb.PdfDocument.PageSize.Height - _Scale * (y - YLowerLimit);
}
public float M(float m)
{
return _Scale * m;
}
public float X(SvgMeasurement x)
{
float retval = _Scale * (x.GetSizeInPixels(DPI) - XLowerLimit);
return retval;
}
public float Y(SvgMeasurement y)
{
return _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);
if (myY < .1F * cb.PdfDocument.PageSize.Height)
{
cb.PdfDocument.NewPage();
myY += .8F * cb.PdfDocument.PageSize.Height;
}
return myY;
}
public float YY(SvgMeasurement y)
{
return -_Scale * y.GetSizeInPixels(DPI);
}
public float M(SvgMeasurement m)
{
return _Scale * m.GetSizeInPixels(DPI);
}
}
}