This commit is contained in:
2010-06-08 11:57:22 +00:00
parent 11f2d476b1
commit d465521f2c
9 changed files with 1462 additions and 371 deletions

View File

@@ -4,17 +4,36 @@ using System.Text;
using System.Drawing;
using iTextSharp.text.pdf;
using iTextSharp.text;
using Itenso.Rtf;
using Itenso.Rtf.Parser;
using Itenso.Rtf.Interpreter;
using Itenso.Rtf.Support;
using Volian.Controls.Library;
using VEPROMS.CSLA.Library;
namespace Volian.Print.Library
{
public abstract partial class vlnPrintObject
{
// Used for debugging:
private static int _UniqueDebugId = 0;
public static int UniqueDebugId
{
get { return _UniqueDebugId++; }
set { _UniqueDebugId = value; }
}
private int _DebugId = UniqueDebugId;
public int DebugId
{
get { return _DebugId; }
set { _DebugId = value; }
}
protected static float _WidthAdjust = 1; // 1 Point - adjusted to match 16-bit line wrapping. For editting it's 3.
protected static float _WidthAdjustBox = 6;
protected static float _CharsToTwips = 6; // assumes 12 char per inch
internal static float _SixLinesPerInch = 12; // twips
protected static float _SevenLinesPerInch = 72F / 7F;
protected static float _SevenLinesPerInch = 10.1F;//72F / 7F;
protected float _XOffset;
public float XOffset
@@ -28,6 +47,7 @@ namespace Volian.Print.Library
get { return _YOffset; }
set { _YOffset = value; }
}
protected vlnParagraph _MyParent;
public vlnParagraph MyParent
{
@@ -66,7 +86,7 @@ namespace Volian.Print.Library
get { return _Width; }
set { _Width = value; }
}
private float _Height;
protected float _Height;
public float Height
{
get
@@ -75,6 +95,7 @@ namespace Volian.Print.Library
_Height = GetParagraphHeight(MyContentByte, IParagraph, Width);
return _Height;
}
set { _Height = value; }
}
public static float GetParagraphHeight(PdfContentByte cb, Paragraph iParagraph, float width)
{
@@ -132,7 +153,7 @@ namespace Volian.Print.Library
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ToPrint(int value)
public static int ToInt(int value)
{
return value;
}
@@ -141,28 +162,28 @@ namespace Volian.Print.Library
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ToPrint(int? value)
public static int ToInt(int? value)
{
return ToPrint((int)value);
return ToInt((int)value);
}
/// <summary>
/// No conversion necessary: string twips -> int twips
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ToPrint(string value)
public static int ToInt(string value)
{
return ToPrint(Convert.ToInt32(value));
return ToInt(Convert.ToInt32(value));
}
/// <summary>
/// No conversion necessary: value from a list in a string, twips -> int twips
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ToPrint(string value, int i)
public static int ToInt(string value, int i)
{
string s = value.Split(",".ToCharArray())[i];
return ToPrint(s);
return ToInt(s);
}
public float AdjustToCharPosition(float position)
{
@@ -170,10 +191,109 @@ namespace Volian.Print.Library
iPosition = (iPosition / (int)_CharsToTwips) * (int)_CharsToTwips;
return (float)iPosition;
}
public static string GetRtf(string text, VE_Font vFont)
{
StringBuilder rtfSB = new StringBuilder();
//DisplayText vlntxt = new DisplayText(text.TrimStart(" ".ToCharArray()), vFont, false);
DisplayText vlntxt = new DisplayText(text, vFont, false);
rtfSB.Append(AddFontTable(vlntxt.TextFont.WindowsFont));
rtfSB.Append(vlntxt.StartText);
rtfSB.Append("}");
return rtfSB.ToString();
}
protected static string AddFontTable(System.Drawing.Font font)
{
StringBuilder rtfSB = new StringBuilder();
StringBuilder sbbeg = new StringBuilder();
StringBuilder sbend = new StringBuilder();
if (font.Bold)
{
sbbeg.Append(@"\b");
sbend.Append(@"\b0");
}
if (font.Underline)
{
sbbeg.Append(@"\ul");
sbend.Insert(0, @"\ulnone");
}
if (font.Italic)
{
sbbeg.Append(@"\i");
sbend.Insert(0, @"\i0");
}
rtfSB.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset2 " + font.FontFamily.Name + @";}"); //}\f0\fs" + this.Font.SizeInPoints * 2 + @" " + myDisplayTextElement.Text + @"}}";
if (!FontIsFixed(font))
rtfSB.Append(@"{\f1\fnil\fcharset0 Arial Unicode MS;}}{\colortbl ;\red255\green0\blue0;}");
else
rtfSB.Append(@"{\f1\fnil\fcharset0 VESymbFix;}}{\colortbl ;\red255\green0\blue0;}");
rtfSB.Append("\r\n");
// use styles to construct rtf commands to insert into next line (where \b, etc is)
rtfSB.Append(@"\viewkind4\uc1\pard\sl-240\slmult0" + sbbeg.ToString() + @"\fs" + Convert.ToInt32(font.SizeInPoints * 2).ToString() + @" "); // \f0\fs" + this.Font.SizeInPoints * 2 + @" " + myDisplayTextElement.Text + @"}";
return rtfSB.ToString();
}
private static bool FontIsFixed(System.Drawing.Font font)
{
iTextSharp.text.Font iFont = Rtf2iTextSharp.GetFont(font);
float fW = iFont.BaseFont.GetWidthPointKerned("W", 12);
float fE = iFont.BaseFont.GetWidthPointKerned("!", 12);
return fW == fE;
}
protected static iTextSharp.text.Paragraph RtfToParagraph(string rtf)
{
IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtf);
Rtf2iTextSharp rtf2IText = new Rtf2iTextSharp(rtfDoc);
iTextSharp.text.Paragraph para = rtf2IText.Convert();
para.SetLeading(_SixLinesPerInch, 0);
return para;
}
public abstract float ToPdf(PdfContentByte cb, float yPageStart, float yTopMargin, float yBottomMargin);
protected float CalculateYOffset(float yPageStart, float yTopMargin)
{
float yLocation = yPageStart - YOffset;
if (MyPageHelper.YMultiplier != 1)
{
yLocation = -1 + yTopMargin - (yTopMargin - yLocation) * MyPageHelper.YMultiplier;
if (Rtf != null)
IParagraph.Leading = _SevenLinesPerInch;
}
return yLocation;
}
protected float CalculateYLocation(float yLocation, float yTopMargin)
{
if (MyPageHelper.YMultiplier != 1)
yLocation = -1 + yTopMargin - (yTopMargin - yLocation) * MyPageHelper.YMultiplier;
return yLocation;
}
public void DebugPdf(PdfContentByte cb, float left, float top)
{
cb.SaveState();
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer debugLayer = _MyPageHelper == null ? null : _MyPageHelper.DebugLayer;
if (debugLayer != null) cb.BeginLayer(debugLayer);
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(left, top, left+50, top - 50);
iTextSharp.text.Font font = FontFactory.GetFont("Arial", 2);
Chunk chk = new Chunk(DebugId.ToString(), font);
Phrase ph = new Phrase(chk);
ct.AddElement(ph);
cb.SetColorFill(new iTextSharp.text.Color(System.Drawing.Color.CadetBlue));
ct.Go();
if (debugLayer != null) cb.EndLayer();
cb.RestoreState();
}
}
public partial class vlnPrintObjects : List<vlnPrintObject>
{
};
public float ToPdf(PdfContentByte cb, float yPageStart, float yTopMargin, float yBottomMargin)
{
foreach (vlnPrintObject part in this)
{
yPageStart = part.ToPdf(cb, yPageStart, yTopMargin, yBottomMargin);
}
return yPageStart;
}
}
public enum PartLocation : int
{
None = 0, // Non-printable
@@ -183,237 +303,6 @@ namespace Volian.Print.Library
Left = 4, // Tabs, Checkoffs? (maybe part of tab)
Container = 5 // Box
};
public partial class vlnParagraphs : List<vlnParagraph>
{
private vlnParagraph _Parent;
public vlnParagraph Parent
{
get { return _Parent; }
set { _Parent = value; }
}
public vlnParagraphs(vlnParagraph parent)
{
_Parent = parent;
}
};
public partial class vlnParagraph : vlnPrintObject
{
private static string _Prefix = "";
public static string Prefix
{
get { return vlnParagraph._Prefix; }
set { vlnParagraph._Prefix = value; }
}
protected float _YBottomMost; // Bottom of the paragraph including the children
public float YBottomMost
{
get { return _YBottomMost; }
set { _YBottomMost = value; }
}
protected float _YTopMost; // Top of the paragraph including the children
public float YTopMost
{
get { return _YTopMost; }
set { _YTopMost = value; }
}
protected float _YTop; // Top of the paragraph including parts
public float YTop
{
get { return _YTop; }
set { _YTop = value; }
}
public float YSize // How big this paragraph is with all of its children
{
get { return _YBottomMost - _YTopMost; }
}
private ItemInfo _MyItemInfo;
public ItemInfo MyItemInfo
{
get { return _MyItemInfo; }
set { _MyItemInfo = value; }
}
private vlnParagraph _MyHighLevelParagraph;
public vlnParagraph MyHighLevelParagraph
{
get
{
if (_MyHighLevelParagraph == null)
{
_MyHighLevelParagraph = GetHighLevelParagraph();
}
return _MyHighLevelParagraph;
}
}
private vlnParagraph GetHighLevelParagraph()
{
if (MyItemInfo.IsHigh) return this;
return MyParent.GetHighLevelParagraph();
}
// Tab, Separator, ChangeBar, Box, Circle, Checkoff
private vlnPrintObjects _PartsAbove;
public vlnPrintObjects PartsAbove
{
get
{
if (_PartsAbove == null) _PartsAbove = new vlnPrintObjects();
return _PartsAbove;
}
}
private vlnPrintObjects _PartsBelow;
public vlnPrintObjects PartsBelow
{
get
{
if (_PartsBelow == null) _PartsBelow = new vlnPrintObjects();
return _PartsBelow;
}
}
private vlnPrintObjects _PartsRight;
public vlnPrintObjects PartsRight
{
get
{
if (_PartsRight == null) _PartsRight = new vlnPrintObjects();
return _PartsRight;
}
}
private vlnPrintObjects _PartsLeft;
public vlnPrintObjects PartsLeft
{
get
{
if (_PartsLeft == null) _PartsLeft = new vlnPrintObjects();
return _PartsLeft;
}
}
private vlnPrintObjects _PartsContainer;
public vlnPrintObjects PartsContainer
{
get
{
if (_PartsContainer == null) _PartsContainer = new vlnPrintObjects();
return _PartsContainer;
}
}
private vlnParagraphs _ChildrenAbove;
public vlnParagraphs ChildrenAbove
{
get
{
if (_ChildrenAbove == null) _ChildrenAbove = new vlnParagraphs(this);
return _ChildrenAbove;
}
}
private vlnParagraphs _ChildrenBelow;
public vlnParagraphs ChildrenBelow
{
get
{
if (_ChildrenBelow == null) _ChildrenBelow = new vlnParagraphs(this);
return _ChildrenBelow;
}
}
private vlnParagraphs _ChildrenRight;
public vlnParagraphs ChildrenRight
{
get
{
if (_ChildrenRight == null) _ChildrenRight = new vlnParagraphs(this);
return _ChildrenRight;
}
}
private vlnParagraphs _ChildrenLeft;
public vlnParagraphs ChildrenLeft
{
get
{
if (_ChildrenLeft == null) _ChildrenLeft = new vlnParagraphs(this);
return _ChildrenLeft;
}
}
//public vlnParagraph(vlnParagraph myParent, ItemInfo myItemInfo, float xoff, float yoff, float width, string rtf, ChildLocation childRelation)
//{
// MyParent = myParent;
// MyItemInfo = myItemInfo;
// XOffset = xoff;
// YOffset = yoff;
// Width = width;
// Rtf = rtf;
// switch (childRelation)
// {
// case ChildLocation.Below:
// myParent.ChildrenBelow.Add(this);
// break;
// case ChildLocation.Above:
// myParent.ChildrenAbove.Add(this);
// break;
// case ChildLocation.Right:
// myParent.ChildrenRight.Add(this);
// break;
// }
//}
public void AdjustXOffsetForTab(ItemInfo itemInfo, int maxRNO, FormatInfo formatInfo, vlnTab myTab)
{
float tabWidth = (myTab == null) ? 0 : myTab.TabWidth;
int typ = ((int)itemInfo.MyContent.Type) % 10000;
int? bxIndx = formatInfo.PlantFormat.FormatData.StepDataList[typ].StepLayoutData.STBoxindex;
if (bxIndx != null)
{
Box bx = formatInfo.PlantFormat.FormatData.BoxList[(int)bxIndx];
//XOffset += (float)bx.TxtStart + tabWidth + XOffsetBox;
XOffset = (float)itemInfo.MyDocStyle.Layout.LeftMargin + (float)bx.TxtStart + tabWidth + XOffsetBox;
if (myTab!=null)myTab.XOffset = XOffset - tabWidth;
}
else if (itemInfo.IsHigh)
{
XOffset += tabWidth - myTab.TabAlign;
if (myTab != null) myTab.XOffset += tabWidth - myTab.TabAlign;
}
else if (itemInfo.IsRNOPart && !((ItemInfo)itemInfo.ActiveParent).IsHigh)
{
// don't adjust for rno
}
else if (MyParent != null)
{
XOffset += tabWidth - (myTab == null ? 0 : myTab.TabAlign);
if (myTab != null) myTab.XOffset += tabWidth - myTab.TabAlign;
}
}
public void AdjustWidth(ItemInfo itemInfo, int maxRNO, FormatInfo formatInfo, vlnTab myTab)
{
float tabWidth = (myTab == null) ? 0 : myTab.TabWidth;
int typ = ((int)itemInfo.MyContent.Type) % 10000;
int? bxIndx = formatInfo.PlantFormat.FormatData.StepDataList[typ].StepLayoutData.STBoxindex;
if (bxIndx != null)
{
Box bx = formatInfo.PlantFormat.FormatData.BoxList[(int)bxIndx];
Width = _WidthAdjustBox + (float)bx.TxtWidth - tabWidth;
}
else if (itemInfo.IsHigh)
{
Width = _WidthAdjust + ToPrint(formatInfo.PlantFormat.FormatData.SectData.StepSectionData.StepSectionLayoutData.WidSTablePrint, maxRNO);
}
else if (MyParent == null)
{
// 72 points / inch - 7 inches (about width of page)
Width = 72 * 7;
}
else if (itemInfo.IsRNOPart && !((ItemInfo)itemInfo.ActiveParent).IsHigh)
{
Width = MyParent.Width;
}
else if (itemInfo.IsTablePart)
{
Width = 72*7; // TODO: Need to determine the Width of the Table based upon the contents
}
else
{
Width = MyParent.Width - tabWidth + (myTab == null ? 0 : myTab.TabAlign);
}
}
}
public enum ChildLocation : int
{
None = 0,
@@ -422,129 +311,5 @@ namespace Volian.Print.Library
Right = 3, // RNO
Left = 4
}
public partial class vlnTab : vlnPrintObject
{
public float TabWidth
{
get { return _CharsToTwips * TabText.Length; }
}
private float? _TabAlign;
/// <summary>
/// Used to Align Tabs for numeric tabs that can go to 2 digits
/// </summary>
public float TabAlign // Offset to Last printable character
{
get
{
if (_TabAlign == null)
{
_TabAlign = 0;
if (_TabText != null)
{
while (_TabText[(int)_TabAlign] == ' ')
_TabAlign++;
if ("0123456789".Contains(_TabText[(int)_TabAlign].ToString()))
{
while ("0123456789".Contains(_TabText[(int)_TabAlign].ToString()))
_TabAlign++;
_TabAlign--;
}
}
}
return (float)_TabAlign * _CharsToTwips;
}
}
private float? _TabOffset;
public float TabOffset // Offset to first printable character
{
get
{
if (_TabOffset == null)
{
_TabOffset = 0;
if(_TabText != null)
while(_TabText[(int)_TabOffset]==' ')
_TabOffset ++;
}
return (float)_TabOffset * _CharsToTwips;
}
}
private string _TabText;
public string TabText
{
get { return _TabText; }
set { _TabText = value; }
}
private VE_Font _MyFont;
public VE_Font MyFont
{
get { return _MyFont; }
set { _MyFont = value; }
}
}
public partial class vlnHeader : vlnPrintObject
{
public float HeaderWidth
{
get { return _WidthAdjust + (_CharsToTwips * HeaderText.Length); }
}
private string _HeaderText;
public string HeaderText
{
get { return _HeaderText; }
set { _HeaderText = value; }
}
private VE_Font _MyFont;
public VE_Font MyFont
{
get { return _MyFont; }
set { _MyFont = value; }
}
private ContentAlignment _ContentAlignment;
public ContentAlignment ContentAlignment
{
get { return _ContentAlignment; }
set { _ContentAlignment = value; }
}
}
public partial class vlnBox : vlnPrintObject
{
private int _LineType; /* how to represent?? */
private System.Drawing.Color _Color;
public System.Drawing.Color Color
{
get { return _Color; }
set { _Color = value; }
}
public vlnBox(vlnParagraph paragraph/*, FormatBox box*/)
{
}
}
public partial class vlnChangeBar : vlnPrintObject
{
public vlnChangeBar(vlnParagraph parent, float xoff, float yoff)
{
_XOffset = xoff;
_YOffset = yoff;
_MyParent = parent;
}
}
public partial class vlnRNOSeparator : vlnPrintObject
{
private VE_Font _MyFont;
public VE_Font MyFont
{
get { return _MyFont; }
set { _MyFont = value; }
}
}
public partial class vlnMacro : vlnPrintObject
{
private string _MacroDef;
public string MacroDef
{
get { return _MacroDef; }
set { _MacroDef = value; }
}
}
}