Changed to support C1PDF and System.Drawing.Graphics

This commit is contained in:
Rich 2010-02-02 20:55:07 +00:00
parent f2f3f030be
commit f8b0964258

View File

@ -1,30 +1,11 @@
/********************************************************************************************* /*********************************************************************************************
* Copyright 2005 - Volian Enterprises, Inc. All rights reserved. * Copyright 2010 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE * 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;
using System.Xml; using System.Xml;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using C1.C1Pdf;
namespace VG namespace VG
{ {
@ -33,26 +14,71 @@ namespace VG
/// Line, Rectangle, Text, Ellipse, Bitmaps, etc. /// Line, Rectangle, Text, Ellipse, Bitmaps, etc.
/// VG provides constructors for these objects from coordinate system locations /// VG provides constructors for these objects from coordinate system locations
/// (such as starting x,y ending x,y, etc), xml and svg. /// (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) /// VG data is stored in TWIPS (1440 per inch)
/// </summary> /// </summary>
// Temporary page class to store portrait/landscape, Left Margin, Vertical offset, etc // Temporary page class to store portrait/landscape, Left Margin, Vertical offset, etc
public class Page public class Page
{ {
public bool portrait=true; private bool _Portrait = true;
public float LeftMargin=0f; public bool Portrait
public float VertOffset=0f;
public Page(bool iport, float lm, float voff)
{ {
portrait=iport; get { return _Portrait; }
LeftMargin=lm; set { _Portrait = value; }
VertOffset=voff; }
private float _LeftMargin = 0;
public float LeftMargin
{
get { return _LeftMargin; }
set { _LeftMargin = value; }
}
private float _TopMargin = 0;
public float TopMargin
{
get { return _TopMargin; }
set { _TopMargin = value; }
}
private float _RightMargin = 0;
public float RightMargin
{
get { return _RightMargin; }
set { _RightMargin = value; }
}
private float _BottomMargin = 0;
public float BottomMargin
{
get { return _BottomMargin; }
set { _BottomMargin = value; }
}
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 class VG
{ {
private static Brush _BlackBrush = Brushes.Black;
public static Brush BlackBrush
{
get { return _BlackBrush; }
set { _BlackBrush = value; }
}
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) // Values of vector graphics items are stored as twips (1440 twips in an inch)
public int cx; public int cx;
public int cy; public int cy;
@ -61,7 +87,6 @@ namespace VG
public float lnwid; public float lnwid;
public Page pg; public Page pg;
public bool MoveAbsolute; // TODO: Not sure what to do with this. public bool MoveAbsolute; // TODO: Not sure what to do with this.
public VG() public VG()
{ {
cx = 0; cx = 0;
@ -69,9 +94,8 @@ namespace VG
dx = 0; dx = 0;
dy = 0; dy = 0;
MoveAbsolute = false; MoveAbsolute = false;
pg=null; pg = null;
} }
public VG(int sx, int sy, int ex, int ey, bool ima, Page ipg) public VG(int sx, int sy, int ex, int ey, bool ima, Page ipg)
{ {
cx = sx; cx = sx;
@ -79,38 +103,53 @@ namespace VG
dx = ex; dx = ex;
dy = ey; dy = ey;
MoveAbsolute = ima; MoveAbsolute = ima;
pg=ipg; pg = ipg;
} }
public float ToPoints(float twip) public float ToPoints(float twip)
{ {
float r = ((float)twip) * 0.05f; float r = ((float)twip) * 0.05f;
return r; return r;
} }
public void UpdatePageInfo(Page pa) public void UpdatePageInfo(Page pa)
{ {
pg.LeftMargin=pa.LeftMargin; pg.LeftMargin = pa.LeftMargin;
pg.portrait=pa.portrait; // RHM 20081003 - This had been pg.portrait=pg.portrait 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 pg.TopMargin = pa.TopMargin; // RHM 20081003 - This had been pg.VertOffset=pg.VertOffset
} }
public string AddRTFBUI(string origStr, string bold, string underline, string italics) public string AddRTFBUI(string origStr, string bold, string underline, string italics)
{ {
if (bold==""&&underline==""&&italics=="")return origStr; if (bold == "" && underline == "" && italics == "") return origStr;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (bold == "bold")sb.Append("{\\b"); if (bold == "bold") sb.Append("{\\b");
if (underline == "underline")sb.Append("{\\u"); if (underline == "underline") sb.Append("{\\u");
if (italics == "italics") sb.Append("{\\i"); if (italics == "italics") sb.Append("{\\i");
sb.Append(" "); sb.Append(" ");
sb.Append(origStr); sb.Append(origStr);
if (bold == "bold")sb.Append("}"); if (bold == "bold") sb.Append("}");
if (underline == "underline")sb.Append("}"); if (underline == "underline") sb.Append("}");
if (italics == "italics") sb.Append("}"); if (italics == "italics") sb.Append("}");
return sb.ToString(); return sb.ToString();
} }
public static Pen CreatePen(Color color, float wid, float scale)
{
Pen pn;
if ((wid * scale) < .5F)
{
//Console.WriteLine("Width = {0}", wid);
int R1 = (int)(wid * color.R);
int R2 = (int)((1 - wid) * Color.White.R);
int G1 = (int)(wid * color.G);
int G2 = (int)((1 - wid) * Color.White.G);
int B1 = (int)(wid * color.B);
int B2 = (int)((1 - wid) * Color.White.B);
pn = new Pen(Color.FromArgb(R1 + R2, G1 + G2, B1 + B2), wid);
//pn = new Pen(color, wid*wid);
}
else
pn = new Pen(color, wid);
return pn;
}
} }
public class VG_Line : VG public class VG_Line : VG
{ {
// Line is defined as starting & ending points. // Line is defined as starting & ending points.
@ -121,7 +160,7 @@ namespace VG
dx = ex; dx = ex;
dy = ey; dy = ey;
lnwid = w; lnwid = w;
pg=ipg; pg = ipg;
} }
public VG_Line(XmlElement svg, Page ipg) public VG_Line(XmlElement svg, Page ipg)
@ -133,19 +172,16 @@ namespace VG
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width")); lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg; pg = ipg;
} }
public void Draw(IVGOutput vgOutput)
public void ToPdf(C1PdfDocument pdf)
{ {
float startx = pg.portrait ? (cx + pg.LeftMargin): (pg.VertOffset + cy); float startx = pg.Portrait ? (cx + pg.LeftMargin) : (pg.TopMargin + cy);
float starty = pg.portrait ? (cy + pg.VertOffset): (pg.LeftMargin - cx); float starty = pg.Portrait ? (cy + pg.TopMargin) : (pg.LeftMargin - cx);
float endx = pg.portrait ? (dx + pg.LeftMargin): (pg.VertOffset + dy); float endx = pg.Portrait ? (dx + pg.LeftMargin) : (pg.TopMargin + dy);
float endy = pg.portrait ? (dy + pg.VertOffset): (pg.LeftMargin - dx); float endy = pg.Portrait ? (dy + pg.TopMargin) : (pg.LeftMargin - dx);
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid)); Pen pn = VG.CreatePen(VG.BlackColor, ToPoints(lnwid), vgOutput.Scale);
pdf.DrawLine(pn, ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy)); vgOutput.DrawLine(pn, ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy));
} }
} }
public class VG_Rect : VG public class VG_Rect : VG
{ {
// Rectangle is defined as starting point & length/width // Rectangle is defined as starting point & length/width
@ -158,7 +194,6 @@ namespace VG
lnwid = w; lnwid = w;
pg = ipg; pg = ipg;
} }
public VG_Rect(XmlElement svg, Page ipg) public VG_Rect(XmlElement svg, Page ipg)
{ {
cx = System.Convert.ToInt32(svg.GetAttribute("x")); cx = System.Convert.ToInt32(svg.GetAttribute("x"));
@ -168,25 +203,22 @@ namespace VG
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width")); lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg; pg = ipg;
} }
public void Draw(IVGOutput vgOutput)
public void ToPdf(C1PdfDocument pdf)
{ {
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid)); Pen pn = new Pen(VG.BlackBrush, ToPoints(lnwid));
float startx = pg.portrait ? (cx + pg.LeftMargin): (pg.VertOffset + cy) ; float startx = pg.Portrait ? (cx + pg.LeftMargin) : (pg.TopMargin + cy);
float starty = pg.portrait ? (cy + pg.VertOffset): (pg.LeftMargin - cx); float starty = pg.Portrait ? (cy + pg.TopMargin) : (pg.LeftMargin - cx);
float endx = pg.portrait ? dx: dy; float endx = pg.Portrait ? dx : dy;
float endy = pg.portrait ? dy: dx; float endy = pg.Portrait ? dy : dx;
RectangleF rc = new RectangleF(ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy)); RectangleF rc = new RectangleF(ToPoints(startx), ToPoints(starty), ToPoints(endx), ToPoints(endy));
pdf.DrawRectangle(pn,rc); vgOutput.DrawRectangle(pn, rc);
} }
} }
public class VG_Image : VG public class VG_Image : VG
{ {
private int Scale; private int Scale;
private string FileName; private string FileName;
// Image is defined as starting point, scale factor (defaults to 1) & image file name // 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) public VG_Image(int sx, int sy, int iscale, string fname)
{ {
@ -195,30 +227,27 @@ namespace VG
Scale = 1; Scale = 1;
FileName = fname; FileName = fname;
} }
public VG_Image(XmlElement svg, Page ipg) public VG_Image(XmlElement svg, Page ipg)
{ {
cx = System.Convert.ToInt32(svg.GetAttribute("x")); cx = System.Convert.ToInt32(svg.GetAttribute("x"));
cy = System.Convert.ToInt32(svg.GetAttribute("y")); cy = System.Convert.ToInt32(svg.GetAttribute("y"));
Scale=1; Scale = 1;
if (svg.HasAttribute("scale")) Scale = System.Convert.ToInt32(svg.GetAttribute("scale")); if (svg.HasAttribute("scale")) Scale = System.Convert.ToInt32(svg.GetAttribute("scale"));
FileName = svg.GetAttribute("bname"); FileName = svg.GetAttribute("bname");
pg = ipg; pg = ipg;
} }
public void Draw(IVGOutput vgOutput)
public void ToPdf(C1PdfDocument pdf)
{ {
Pen pn = new Pen(Brushes.Black,lnwid); Pen pn = new Pen(VG.BlackBrush, lnwid);
Bitmap bm = new Bitmap(FileName); Bitmap bm = new Bitmap(FileName);
RectangleF rc = new RectangleF(); RectangleF rc = new RectangleF();
rc.Height = ToPoints((bm.Height*Scale)*4.8f); rc.Height = ToPoints((bm.Height * Scale) * 4.8f);
rc.Width = ToPoints((bm.Width*Scale)*4.8f); rc.Width = ToPoints((bm.Width * Scale) * 4.8f);
rc.X = ToPoints(pg.LeftMargin+cx); rc.X = ToPoints(pg.LeftMargin + cx);
rc.Y = ToPoints(pg.VertOffset+cy); rc.Y = ToPoints(pg.TopMargin + cy);
pdf.DrawImage(bm, rc, ContentAlignment.MiddleCenter, ImageSizeModeEnum.Scale); // TODO: grfx.DrawImage(bm, rc, ContentAlignment.MiddleCenter, ImageSizeModeEnum.Scale);
} }
} }
public class VG_Ellipse : VG public class VG_Ellipse : VG
{ {
// Ellipse (circle) is defined by a bounding rectangle specified by a coordinate pair, a width, and a height // Ellipse (circle) is defined by a bounding rectangle specified by a coordinate pair, a width, and a height
@ -231,7 +260,6 @@ namespace VG
lnwid = w; lnwid = w;
pg = ipg; pg = ipg;
} }
public VG_Ellipse(XmlElement svg, Page ipg) public VG_Ellipse(XmlElement svg, Page ipg)
{ {
cx = System.Convert.ToInt32(svg.GetAttribute("cx")); cx = System.Convert.ToInt32(svg.GetAttribute("cx"));
@ -241,14 +269,12 @@ namespace VG
lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width")); lnwid = System.Convert.ToInt32(svg.GetAttribute("stroke-width"));
pg = ipg; pg = ipg;
} }
public void Draw(IVGOutput vgOutput)
public void ToPdf(C1PdfDocument pdf)
{ {
Pen pn = new Pen(Brushes.Black,ToPoints(lnwid)); Pen pn = new Pen(VG.BlackBrush, ToPoints(lnwid));
pdf.DrawEllipse(pn,ToPoints(pg.LeftMargin+cx), ToPoints(pg.VertOffset+cy), ToPoints(dx), ToPoints(dy)); vgOutput.DrawEllipse(pn, ToPoints(pg.LeftMargin + cx), ToPoints(pg.TopMargin + cy), ToPoints(dx), ToPoints(dy));
} }
} }
public class VG_Text : VG public class VG_Text : VG
{ {
private string FontName; private string FontName;
@ -257,7 +283,6 @@ namespace VG
private string Underline; private string Underline;
private string Italics; private string Italics;
private string Text; private string Text;
// Text is defined by a starting location, attributes & the string // 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) public VG_Text(int sx, int sy, int fs, string txt, string fn, string ibold, string iunder, string iitalics, Page ipg)
{ {
@ -271,12 +296,11 @@ namespace VG
Italics = iitalics; Italics = iitalics;
pg = ipg; pg = ipg;
// TODO: The following were in the 16-bit code - we may need to use these, wait and see // TODO: The following were in the 16-bit code - we may need to use these, wait and see
// strokeWidth = stroke; // strokeWidth = stroke;
// strokeWidthBold = strokebold; // strokeWidthBold = strokebold;
// ULOffset = UnderlineOffset; // ULOffset = UnderlineOffset;
// ULWidth = UnderlineWidth; // ULWidth = UnderlineWidth;
} }
public VG_Text(XmlElement svg, Page ipg) public VG_Text(XmlElement svg, Page ipg)
{ {
cx = System.Convert.ToInt32(svg.GetAttribute("x")); cx = System.Convert.ToInt32(svg.GetAttribute("x"));
@ -289,78 +313,99 @@ namespace VG
Text = svg.InnerText; Text = svg.InnerText;
pg = ipg; pg = ipg;
} }
public void Draw(IVGOutput vgOutput)
public void ToPdf(C1PdfDocument pdf)
{ {
Font myfont = new Font(FontName,FontSize); // TODO: Font myfont = new Font(FontName, (FontSize*9)/10);
Font myfont = new Font(FontName, FontSize * vgOutput.FontSizeAdjust); // TODO: Trying 80%
RectangleF rc = new RectangleF(); RectangleF rc = new RectangleF();
// the returned value from c1 pdf code is already in points, // the returned value from c1 grfx code is already in points,
// so need to convert the size. // so need to convert the size.
rc.Size = pdf.MeasureStringRtf(Text,myfont,500); // TODO: 500 or pagewidth, or what rc.Size = vgOutput.MeasureString(Text, myfont); // TODO: 500 or pagewidth, or what
rc.X = ToPoints(pg.LeftMargin+cx); //rc.Size = grfx.MeasureString(Text, myfont, 500); // TODO: 500 or pagewidth, or what
rc.Y = ToPoints(pg.VertOffset+cy)-FontSize; rc.X = ToPoints(pg.LeftMargin + cx);
rc.Y = ToPoints(pg.TopMargin + cy) - FontSize;
rc.Height *= 1.0000001F; // Small factor to make text visible.
// add bold, underline & italics for entire object. // add bold, underline & italics for entire object.
string rtfout = AddRTFBUI(Text,Bold,Underline,Italics); // TODO: string rtfout = AddRTFBUI(Text, Bold, Underline, Italics);
pdf.DrawStringRtf(rtfout,myfont,Brushes.Black,rc); //using(Pen pn = new Pen(Color.DarkBlue, .1F))
// grfx.DrawRectangle(pn, rc.X,rc.Y,rc.Width,rc.Height);
vgOutput.DrawString(Text, myfont, VG.BlackBrush, rc);
//grfx.DrawString(Text, myfont, VG.BlackBrush, rc);
} }
} }
public class VG_Arc : VG public class VG_Arc : VG
{ {
public Single sweepAngle; public Single sweepAngle;
public Single startAngle; public Single startAngle;
public float stX,stY; public float stX, stY;
public float rWid,rHgt; 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 // 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) public VG_Arc(float sx, float sy, float width, float hight, float stAngle, float swpAngle, int w, Page ipg)
{ {
stX = sx; if (width > 0)
stY = sy; {
rWid = width; stX = sx;
rHgt = hight; rWid = width;
}
else
{
stX = sx + width;
rWid = -width;
}
if (hight > 0)
{
stY = sy;
rHgt = hight;
}
else
{
stY = sy + hight;
rHgt = -hight;
}
startAngle = stAngle; startAngle = stAngle;
// startAngle = 90; // startAngle = 90;
sweepAngle = swpAngle; sweepAngle = swpAngle;
// sweepAngle = -45; // sweepAngle = -45;
lnwid = w; lnwid = w;
pg = ipg; 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 #if DEBUG
//Pen pn = new Pen(myColors[iColor],ToPoints(lnwid));
//iColor=(iColor+1)%myColors.Length;
Pen pn = new Pen(Color.Black, ToPoints(lnwid));
#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); public static int iColor = 0;
pdf.DrawArc(pn,rc,startAngle,sweepAngle); public Color[] myColors = { Color.Red, Color.Green, Color.OrangeRed, Color.Cyan, Color.Orange, Color.Blue, Color.Yellow, Color.Indigo, Color.YellowGreen, Color.Violet };
// c1pdf.DrawArc(new Pen(Color.Black, 4),rc,startAngle,sweepAngle); //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++;
//#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 (iColor == 7 || iColor == 8) return;
//if (iColor == 6) return;
//if (iColor == 4 || iColor == 6)
//{
// using (Pen lightPen = new Pen(myColors[iColor % myColors.Length], .01F))
//{
// vgOutput.DrawEllipse(lightPen, rc.X, rc.Y, rc.Width, rc.Height);
// float xc = rc.X + (rc.Width / 2);
// float yc = rc.Y + (rc.Height / 2);
// vgOutput.DrawEllipse(lightPen, xc - .5F, yc - .5F, 1F, 1F);
//}
//}
if (sweepAngle > 0)
vgOutput.DrawArc(pn, rc, startAngle, sweepAngle);
else
vgOutput.DrawArc(pn, rc, startAngle + sweepAngle, -sweepAngle);
} }
} }
} }