Rich 1731dbe43b Preparation to switch to ITextSharp library
Corrected logic to process window size/movement adjustments
2009-02-06 15:52:27 +00:00

366 lines
11 KiB
C#

/*********************************************************************************************
* Copyright 2005 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
* ------------------------------------------------------------------------------
* $Workfile: VG.cs $ $Revision: 2 $
* $Author: Jsj $ $Date: 2/28/06 1:33p $
*
* $History: VG.cs $
*
* ***************** Version 2 *****************
* User: Jsj Date: 2/28/06 Time: 1:33p
* Updated in $/LibSource/VG
* updated for X/Y Plots
*
* ***************** Version 1 *****************
* User: Kathy Date: 12/06/05 Time: 12:06p
* Created in $/LibSource/VG
*
* ***************** Version 1 *****************
* User: Kathy Date: 12/06/05 Time: 12:01p
* Created in $/LibSource/VG.root/VG
*********************************************************************************************/
using System;
using System.Xml;
using System.Drawing;
using System.Text;
using C1.C1Pdf;
namespace VG
{
/// <summary>
/// VG is the class representing vector graphics. It provides objects for:
/// Line, Rectangle, Text, Ellipse, Bitmaps, etc.
/// VG provides constructors for these objects from coordinate system locations
/// (such as starting x,y ending x,y, etc), xml and svg.
/// VG provides output to PDF. (More can be added).
/// VG data is stored in TWIPS (1440 per inch)
/// </summary>
// Temporary page class to store portrait/landscape, Left Margin, Vertical offset, etc
public class Page
{
public bool portrait=true;
public float LeftMargin=0f;
public float VertOffset=0f;
public Page(bool iport, float lm, float voff)
{
portrait=iport;
LeftMargin=lm;
VertOffset=voff;
}
}
public class VG
{
// Values of vector graphics items are stored as twips (1440 twips in an inch)
public int cx;
public int cy;
public int dx;
public int dy;
public float lnwid;
public Page pg;
public bool MoveAbsolute; // TODO: Not sure what to do with this.
public VG()
{
cx = 0;
cy = 0;
dx = 0;
dy = 0;
MoveAbsolute = false;
pg=null;
}
public VG(int sx, int sy, int ex, int ey, bool ima, Page ipg)
{
cx = sx;
cy = sy;
dx = ex;
dy = ey;
MoveAbsolute = ima;
pg=ipg;
}
public float ToPoints(float twip)
{
float r = ((float)twip) * 0.05f;
return r;
}
public void UpdatePageInfo(Page pa)
{
pg.LeftMargin=pa.LeftMargin;
pg.portrait=pa.portrait; // RHM 20081003 - This had been pg.portrait=pg.portrait
pg.VertOffset=pa.VertOffset; // RHM 20081003 - This had been pg.VertOffset=pg.VertOffset
}
public string AddRTFBUI(string origStr, string bold, string underline, string italics)
{
if (bold==""&&underline==""&&italics=="")return origStr;
StringBuilder sb = new StringBuilder();
if (bold == "bold")sb.Append("{\\b");
if (underline == "underline")sb.Append("{\\u");
if (italics == "italics") sb.Append("{\\i");
sb.Append(" ");
sb.Append(origStr);
if (bold == "bold")sb.Append("}");
if (underline == "underline")sb.Append("}");
if (italics == "italics") sb.Append("}");
return sb.ToString();
}
}
public class VG_Line : VG
{
// Line is defined as starting & ending points.
public VG_Line(int sx, int sy, int ex, int ey, int w, Page ipg)
{
cx = sx;
cy = sy;
dx = ex;
dy = ey;
lnwid = w;
pg=ipg;
}
public VG_Line(XmlElement svg, Page ipg)
{
cx = System.Convert.ToInt32(svg.GetAttribute("x1"));
cy = System.Convert.ToInt32(svg.GetAttribute("y1"));
dx = System.Convert.ToInt32(svg.GetAttribute("x2"));
dy = System.Convert.ToInt32(svg.GetAttribute("y2"));
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg;
}
public void ToPdf(C1PdfDocument pdf)
{
float startx = pg.portrait ? (cx + pg.LeftMargin): (pg.VertOffset + cy);
float starty = pg.portrait ? (cy + pg.VertOffset): (pg.LeftMargin - cx);
float endx = pg.portrait ? (dx + pg.LeftMargin): (pg.VertOffset + dy);
float endy = pg.portrait ? (dy + pg.VertOffset): (pg.LeftMargin - dx);
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid));
pdf.DrawLine(pn, ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy));
}
}
public class VG_Rect : VG
{
// Rectangle is defined as starting point & length/width
public VG_Rect(int sx, int sy, int ex, int ey, int w, Page ipg)
{
cx = sx;
cy = sy;
dx = ex;
dy = ey;
lnwid = w;
pg = ipg;
}
public VG_Rect(XmlElement svg, Page ipg)
{
cx = System.Convert.ToInt32(svg.GetAttribute("x"));
cy = System.Convert.ToInt32(svg.GetAttribute("y"));
dx = System.Convert.ToInt32(svg.GetAttribute("width"));
dy = System.Convert.ToInt32(svg.GetAttribute("height"));
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg;
}
public void ToPdf(C1PdfDocument pdf)
{
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid));
float startx = pg.portrait ? (cx + pg.LeftMargin): (pg.VertOffset + cy) ;
float starty = pg.portrait ? (cy + pg.VertOffset): (pg.LeftMargin - cx);
float endx = pg.portrait ? dx: dy;
float endy = pg.portrait ? dy: dx;
RectangleF rc = new RectangleF(ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy));
pdf.DrawRectangle(pn,rc);
}
}
public class VG_Image : VG
{
private int Scale;
private string FileName;
// Image is defined as starting point, scale factor (defaults to 1) & image file name
public VG_Image(int sx, int sy, int iscale, string fname)
{
cx = sx;
cy = sy;
Scale = 1;
FileName = fname;
}
public VG_Image(XmlElement svg, Page ipg)
{
cx = System.Convert.ToInt32(svg.GetAttribute("x"));
cy = System.Convert.ToInt32(svg.GetAttribute("y"));
Scale=1;
if (svg.HasAttribute("scale")) Scale = System.Convert.ToInt32(svg.GetAttribute("scale"));
FileName = svg.GetAttribute("bname");
pg = ipg;
}
public void ToPdf(C1PdfDocument pdf)
{
Pen pn = new Pen(Brushes.Black,lnwid);
Bitmap bm = new Bitmap(FileName);
RectangleF rc = new RectangleF();
rc.Height = ToPoints((bm.Height*Scale)*4.8f);
rc.Width = ToPoints((bm.Width*Scale)*4.8f);
rc.X = ToPoints(pg.LeftMargin+cx);
rc.Y = ToPoints(pg.VertOffset+cy);
pdf.DrawImage(bm, rc, ContentAlignment.MiddleCenter, ImageSizeModeEnum.Scale);
}
}
public class VG_Ellipse : VG
{
// Ellipse (circle) is defined by a bounding rectangle specified by a coordinate pair, a width, and a height
public VG_Ellipse(int sx, int sy, int ex, int ey, int w, Page ipg)
{
cx = sx;
cy = sy;
dx = ex;
dy = ey;
lnwid = w;
pg = ipg;
}
public VG_Ellipse(XmlElement svg, Page ipg)
{
cx = System.Convert.ToInt32(svg.GetAttribute("cx"));
cy = System.Convert.ToInt32(svg.GetAttribute("cy"));
dx = System.Convert.ToInt32(svg.GetAttribute("rx"));
dy = System.Convert.ToInt32(svg.GetAttribute("ry"));
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg;
}
public void ToPdf(C1PdfDocument pdf)
{
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid));
pdf.DrawEllipse(pn,ToPoints(pg.LeftMargin+cx), ToPoints(pg.VertOffset+cy), ToPoints(dx), ToPoints(dy));
}
}
public class VG_Text : VG
{
private string FontName;
private int FontSize;
private string Bold;
private string Underline;
private string Italics;
private string Text;
// Text is defined by a starting location, attributes & the string
public VG_Text(int sx, int sy, int fs, string txt, string fn, string ibold, string iunder, string iitalics, Page ipg)
{
cx = sx;
cy = sy;
FontSize = fs;
FontName = fn;
Text = txt;
Bold = ibold;
Underline = iunder;
Italics = iitalics;
pg = ipg;
// TODO: The following were in the 16-bit code - we may need to use these, wait and see
// strokeWidth = stroke;
// strokeWidthBold = strokebold;
// ULOffset = UnderlineOffset;
// ULWidth = UnderlineWidth;
}
public VG_Text(XmlElement svg, Page ipg)
{
cx = System.Convert.ToInt32(svg.GetAttribute("x"));
cy = System.Convert.ToInt32(svg.GetAttribute("y"));
FontSize = System.Convert.ToInt32(svg.GetAttribute("font-size"));
Bold = svg.GetAttribute("font-weight");
Underline = svg.GetAttribute("text-decoration");
Italics = svg.GetAttribute("font-style");
FontName = svg.GetAttribute("font-family");
Text = svg.InnerText;
pg = ipg;
}
public void ToPdf(C1PdfDocument pdf)
{
Font myfont = new Font(FontName,FontSize);
RectangleF rc = new RectangleF();
// the returned value from c1 pdf code is already in points,
// so need to convert the size.
rc.Size = pdf.MeasureStringRtf(Text,myfont,500); // TODO: 500 or pagewidth, or what
rc.X = ToPoints(pg.LeftMargin+cx);
rc.Y = ToPoints(pg.VertOffset+cy)-FontSize;
// add bold, underline & italics for entire object.
string rtfout = AddRTFBUI(Text,Bold,Underline,Italics);
pdf.DrawStringRtf(rtfout,myfont,Brushes.Black,rc);
}
}
public class VG_Arc : VG
{
public Single sweepAngle;
public Single startAngle;
public float stX,stY;
public float rWid,rHgt;
#if DEBUG
public static int iColor=0;
public Color [] myColors={Color.Red,Color.Orange,Color.Yellow,Color.Green,Color.Blue,Color.BlueViolet,Color.Violet};
#endif
// Arc is defined by a bounding rectangle specified by a coordinate pair, a width, and a height
public VG_Arc(float sx, float sy, float width, float hight, float stAngle, float swpAngle, int w, Page ipg)
{
stX = sx;
stY = sy;
rWid = width;
rHgt = hight;
startAngle = stAngle;
// startAngle = 90;
sweepAngle = swpAngle;
// sweepAngle = -45;
lnwid = w;
pg = ipg;
}
// public VG_Arc(XmlElement svg, Page ipg)
// {
// cx = System.Convert.ToInt32(svg.GetAttribute("cx"));
// cy = System.Convert.ToInt32(svg.GetAttribute("cy"));
// dx = System.Convert.ToInt32(svg.GetAttribute("rx"));
// dy = System.Convert.ToInt32(svg.GetAttribute("ry"));
// lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
// pg = ipg;
// }
public void ToPdf(C1PdfDocument pdf)
{
// Pen pn = new Pen(Brushes.Black,ToPoints(lnwid));
#if DEBUG
Pen pn = new Pen(myColors[iColor],ToPoints(lnwid));
iColor=(iColor+1)%myColors.Length;
#else
Pen pn = new Pen(Color.Black,ToPoints(lnwid));
#endif
// PointF startpt = new PointF(stX,stY);
// SizeF recsize = new SizeF(rWid,rHgt);
// RectangleF rc = new RectangleF(startpt,recsize);
float startx = pg.portrait ? (stX + pg.LeftMargin): (pg.VertOffset + stY);
float starty = pg.portrait ? (stY + pg.VertOffset): (pg.LeftMargin - stX);
RectangleF rc = new RectangleF(ToPoints(startx),ToPoints(starty),ToPoints(rWid),ToPoints(rHgt));
// c1pdf.DrawRectangle(pn,rc); //debug
// c1pdf.DrawArc(new Pen(Color.Black,ToPoints(lnwid)),new RectangleF(stX,stY,rWid,rHgt),startAngle,sweepAngle);
pdf.DrawArc(pn,rc,startAngle,sweepAngle);
// c1pdf.DrawArc(new Pen(Color.Black, 4),rc,startAngle,sweepAngle);
}
}
}