Files
SourceCode/PROMS/VG/VG.cs
T

286 lines
9.1 KiB
C#

/*********************************************************************************************
* Copyright 2010 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
*********************************************************************************************/
using System;
using System.Xml;
using System.Drawing;
using System.Text;
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 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 { get; set; } = true;
public float LeftMargin { get; set; } = 0;
public float TopMargin { get; set; } = 0;
public float RightMargin { get; set; } = 0;
public float BottomMargin { get; set; } = 0;
public Page(bool portrait, float leftMargin, float topMargin, float rightMargin, float bottomMargin)
{
Portrait = portrait;
LeftMargin = leftMargin;
TopMargin = topMargin;
RightMargin = rightMargin;
BottomMargin = bottomMargin;
}
public Page(bool portrait, float leftMargin, float topMargin)
{
Portrait = portrait;
LeftMargin = leftMargin;
TopMargin = topMargin;
}
}
public class VG
{
public static Brush BlackBrush { get; set; } = Brushes.Black;
private static Color _BlackColor = Color.Black;
public static Color BlackColor
{
get { return _BlackColor; }
set { _BlackColor = value; BlackBrush = new SolidBrush(value); }
}
// 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)(((float)twip) * 0.05f);
public void UpdatePageInfo(Page pa)
{
pg.LeftMargin = pa.LeftMargin;
pg.Portrait = pa.Portrait;
pg.TopMargin = pa.TopMargin;
}
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 static Pen CreatePen(Color color, float wid, float scale)
{
Pen pn;
float scaleWid = wid * scale;
float colorFraction = (float)Math.Sqrt(scaleWid);
if (scaleWid < .5F)
{
//Console.WriteLine("Width = {0}", wid);
int R1 = (int)(colorFraction * color.R);
int R2 = (int)((1 - colorFraction) * Color.White.R);
int G1 = (int)(colorFraction * color.G);
int G2 = (int)((1 - colorFraction) * Color.White.G);
int B1 = (int)(colorFraction * color.B);
int B2 = (int)((1 - colorFraction) * Color.White.B);
pn = new Pen(Color.FromArgb(R1 + R2, G1 + G2, B1 + B2), scaleWid);
}
else
pn = new Pen(color, scaleWid);
return pn;
}
}
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 Draw(IVGOutput vgOutput)
{
float startx = pg.Portrait ? (cx + pg.LeftMargin) : (pg.TopMargin + cy);
float starty = pg.Portrait ? (cy + pg.TopMargin) : (pg.LeftMargin - cx);
float endx = pg.Portrait ? (dx + pg.LeftMargin) : (pg.TopMargin + dy);
float endy = pg.Portrait ? (dy + pg.TopMargin) : (pg.LeftMargin - dx);
Pen pn = VG.CreatePen(VG.BlackColor, ToPoints(lnwid), vgOutput.Scale);
vgOutput.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 Draw(IVGOutput vgOutput)
{
Pen pn = new Pen(VG.BlackBrush, ToPoints(lnwid));
float startx = pg.Portrait ? (cx + pg.LeftMargin) : (pg.TopMargin + cy);
float starty = pg.Portrait ? (cy + pg.TopMargin) : (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));
vgOutput.DrawRectangle(pn, rc);
}
}
public class VG_Text : VG
{
private readonly string FontName;
private readonly int FontSize;
private readonly string Bold;
private readonly string Underline;
private readonly string Italics;
private readonly 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;
}
public void Draw(IVGOutput vgOutput)
{
// TODO: Font myfont = new Font(FontName, (FontSize*9)/10);
Font myfont = new Font(FontName, FontSize * vgOutput.FontSizeAdjust); // TODO: Trying 80%
RectangleF rc = new RectangleF
{
// the returned value from c1 grfx code is already in points,
// so need to convert the size.
Size = vgOutput.MeasureString(Text, myfont), // TODO: 500 or pagewidth, or what
//rc.Size = grfx.MeasureString(Text, myfont, 500); // TODO: 500 or pagewidth, or what
X = ToPoints(pg.LeftMargin + cx),
Y = ToPoints(pg.TopMargin + cy) - FontSize
};
rc.Height *= 1.0000001F; // Small factor to make text visible.
vgOutput.DrawString(Text, myfont, VG.BlackBrush, rc);
}
}
public class VG_Arc : VG
{
public float sweepAngle;
public float startAngle;
public float stX, stY;
public float rWid, rHgt;
// 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)
{
if (width > 0)
{
stX = sx;
rWid = width;
}
else
{
stX = sx + width;
rWid = -width;
}
if (hight > 0)
{
stY = sy;
rHgt = hight;
}
else
{
stY = sy + hight;
rHgt = -hight;
}
startAngle = stAngle;
sweepAngle = swpAngle;
lnwid = w;
pg = ipg;
}
//#if DEBUG
//public static int iColor = 0;
//public Color[] myColors = { Color.Red, Color.Green, Color.OrangeRed, Color.Cyan, Color.Orange, Color.Blue, Color.Yellow, Color.Indigo, Color.YellowGreen, Color.Violet };
////public Color[] myColors ={ Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.BlueViolet, Color.Violet };
//#endif
public void Draw(IVGOutput vgOutput)
{
//#if DEBUG
// Pen pn = new Pen(myColors[iColor % myColors.Length], ToPoints(lnwid));
//iColor++;
//if (iColor >= myColors.Length) iColor = 0;
//#else
Pen pn = new Pen(VG.BlackBrush, ToPoints(lnwid));
//#endif
float startx = pg.Portrait ? (stX + pg.LeftMargin) : (pg.TopMargin + stY);
float starty = pg.Portrait ? (stY + pg.TopMargin) : (pg.LeftMargin - stX);
RectangleF rc = new RectangleF(ToPoints(startx), ToPoints(starty), ToPoints(rWid), ToPoints(rHgt));
//Console.WriteLine("{0},'{1}',{2},{3},{4},{5},{6},{7}", iColor, pn.Color, rc.X, rc.Y, rc.Width, rc.Height, startAngle, sweepAngle);
if (sweepAngle > 0)
vgOutput.DrawArc(pn, rc, startAngle, sweepAngle);
else
vgOutput.DrawArc(pn, rc, startAngle + sweepAngle, -sweepAngle);
}
}
}