271 lines
9.8 KiB
C#
271 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using iTextSharp.text.factories;
|
|
using Volian.Svg.Library;
|
|
using System.Text.RegularExpressions;
|
|
using System.Xml;
|
|
|
|
namespace Volian.Print.Library
|
|
{
|
|
public class VlnSvgPageHelper:SvgPageHelper
|
|
{
|
|
private VEPROMS.CSLA.Library.SectionInfo _MySection;
|
|
public VEPROMS.CSLA.Library.SectionInfo MySection
|
|
{
|
|
get { return _MySection; }
|
|
set
|
|
{
|
|
_MySection = value;
|
|
MySvg = BuildSvg(_MySection);
|
|
}
|
|
}
|
|
private int _CurrentPageOf = 0;
|
|
public int CurrentPageOf
|
|
{
|
|
get { return _CurrentPageOf; }
|
|
set { _CurrentPageOf = value; }
|
|
}
|
|
private string _Rev = string.Empty;
|
|
public string Rev
|
|
{
|
|
get { return _Rev; }
|
|
set { _Rev = value; }
|
|
}
|
|
private string _RevDate = string.Empty;
|
|
public string RevDate
|
|
{
|
|
get { return _RevDate; }
|
|
set { _RevDate = value; }
|
|
}
|
|
public VlnSvgPageHelper(VEPROMS.CSLA.Library.SectionInfo mySection)
|
|
{
|
|
MySection = mySection;
|
|
}
|
|
private Volian.Svg.Library.Svg BuildSvg(VEPROMS.CSLA.Library.SectionInfo mySection)
|
|
{
|
|
VEPROMS.CSLA.Library.FormatInfo activeFormat = mySection.ActiveFormat;
|
|
VEPROMS.CSLA.Library.DocStyle docStyle = mySection.MyDocStyle;
|
|
Volian.Svg.Library.Svg mySvg = null;
|
|
mySvg = SvgSerializer<Volian.Svg.Library.Svg>.StringDeserialize(BuildMyText(activeFormat));
|
|
mySvg.ViewBox.Height = 1100;
|
|
mySvg.ViewBox.Width = 850;
|
|
mySvg.Height = new SvgMeasurement(11, E_MeasurementUnits.IN);
|
|
mySvg.Width = new SvgMeasurement(8.5F, E_MeasurementUnits.IN);
|
|
mySvg.LeftMargin = (float)docStyle.Layout.LeftMargin;
|
|
mySvg.TopMargin = 9.6F;
|
|
VEPROMS.CSLA.Library.PageStyle pageStyle = docStyle.pagestyle;
|
|
AddPageListItems(mySvg, pageStyle, mySection);
|
|
//DocStyle docStyle = GetDocStyle(activeFormat, (section.MyContent.Type ?? 10000));
|
|
mySvg.ProcessText += new SvgProcessTextEvent(mySvg_ProcessText);
|
|
return mySvg;
|
|
}
|
|
private static Regex regexFindToken = new Regex("{[^{}]*}");
|
|
private string mySvg_ProcessText(object sender, SvgProcessTextArgs args)
|
|
{
|
|
if (!args.MyText.Contains("{"))
|
|
return args.MyText;
|
|
return regexFindToken.Replace(args.MyText, new MatchEvaluator(ReplacePageListToken));
|
|
}
|
|
private string BuildMyText(VEPROMS.CSLA.Library.FormatInfo activeFormat)
|
|
{
|
|
string sGenMac = activeFormat.GenMac;
|
|
if (!sGenMac.Contains("xmlns"))
|
|
sGenMac = sGenMac.Replace("<svg ", "<svg xmlns='http://www.w3.org/2000/svg' ");
|
|
XmlDocument xDocGenMac = new XmlDocument();
|
|
xDocGenMac.LoadXml(sGenMac); // Add xmlns if necessary
|
|
Volian.Svg.Library.Svg mySvg = new Volian.Svg.Library.Svg();
|
|
SvgDefine def = new SvgDefine();
|
|
mySvg.Add(def);
|
|
def.ID = "GenMac Templates";
|
|
string str = SvgSerializer<Volian.Svg.Library.Svg>.StringSerialize(mySvg);
|
|
XmlDocument xDocPrintout = new XmlDocument();
|
|
xDocPrintout.LoadXml(str);
|
|
XmlNode xn = xDocPrintout.DocumentElement.ChildNodes[0];
|
|
xn.AppendChild(xDocPrintout.ImportNode(xDocGenMac.DocumentElement, true));
|
|
return xDocPrintout.OuterXml;
|
|
}
|
|
private string FirstAndLast(string token)
|
|
{
|
|
// strip the curly braces and return the first and last character
|
|
// For example Header1 becomes H1 and Box2 becomes B2
|
|
return token.Substring(1, 1) + token.Substring(token.Length - 2, 1);
|
|
}
|
|
private static Regex regexJustTokens = new Regex(@"^{([^{}\?]*}{)*[^{}\?]*}$");
|
|
|
|
private void AddPageListItems(Volian.Svg.Library.Svg mySvg, VEPROMS.CSLA.Library.PageStyle pageStyle, VEPROMS.CSLA.Library.SectionInfo section)
|
|
{
|
|
SvgGroup svgGroup = new SvgGroup();
|
|
foreach (VEPROMS.CSLA.Library.PageItem pageItem in pageStyle.PageItems)
|
|
{
|
|
if (regexJustTokens.IsMatch(pageItem.Token))
|
|
{
|
|
MatchCollection matches = regexFindToken.Matches(pageItem.Token);
|
|
foreach (Match match in matches)
|
|
{
|
|
string token = match.Value;
|
|
switch (match.Value)
|
|
{
|
|
case "{HEADER1}":
|
|
case "{HEADER2}":
|
|
case "{HEADER3}":
|
|
case "{HEADER4}":
|
|
case "{HEADER5}":
|
|
case "{BOX1}":
|
|
case "{BOX2}":
|
|
case "{BOX3}":
|
|
case "{BOX4}":
|
|
case "{BOX5}":
|
|
case "{BOX6}":
|
|
case "{BOX7}":
|
|
case "{BOX8}":
|
|
svgGroup.Add(PageItemToSvgUse(pageItem, FirstAndLast(token)));
|
|
break;
|
|
case "{DRAFTPAGE}":
|
|
case "{REFERENCEPAGE}":
|
|
case "{MASTERPAGE}":
|
|
case "{SAMPLEPAGE}":
|
|
//mySvg.SetValidWaterMark(token, _Watermark); // Need logic to limit watermark to tokens.
|
|
break;
|
|
case "{PROCTITLE}":
|
|
SplitTitle(svgGroup, pageItem, section.MyProcedure.DisplayText, (int)section.ActiveFormat.PlantFormat.FormatData.ProcData.TitleLength);
|
|
break;
|
|
case "{EOPNUM}":
|
|
svgGroup.Add(PageItemToSvgText(pageItem, section.MyProcedure.DisplayNumber));
|
|
break;
|
|
case "{SECTIONLEVELTITLE}":
|
|
SplitTitle(svgGroup, pageItem, section.DisplayText, section.ActiveFormat.PlantFormat.FormatData.SectData.SectionTitleLength);
|
|
svgGroup.Add(PageItemToSvgText(pageItem, section.DisplayText));
|
|
break;
|
|
case "{SECTIONLEVELNUMBER}":
|
|
svgGroup.Add(PageItemToSvgText(pageItem, section.DisplayNumber));
|
|
break;
|
|
default:
|
|
Console.WriteLine("Token not processed {0}", token);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
svgGroup.Add(PageItemToSvgText(pageItem, pageItem.Token));
|
|
}
|
|
}
|
|
mySvg.Add(svgGroup);
|
|
}
|
|
private void SplitTitle(SvgGroup svgGroup, VEPROMS.CSLA.Library.PageItem pageItem, string title, int? len)
|
|
{
|
|
// TODO: need to calculate len in either points or inches and use the font from the pageItem to determine the number of characters
|
|
if (len == null || title.Length < len)
|
|
{
|
|
svgGroup.Add(PageItemToSvgText(pageItem, title));
|
|
return;
|
|
}
|
|
// Otherwise determine how many line to split the text into
|
|
List<string> titleLines = SplitText(title, (int)len);
|
|
// Move up 6 Points per line to find the starting point
|
|
float yOffset = -6 * (titleLines.Count - 1);
|
|
foreach (string line in titleLines)
|
|
{
|
|
svgGroup.Add(PageItemToSvgText(pageItem, line, yOffset));
|
|
yOffset += 12;
|
|
}
|
|
}
|
|
private List<string> SplitText(string title, int len)
|
|
{
|
|
List<string> retval = new List<string>();
|
|
int strlen = title.Length;
|
|
int start = 0;
|
|
while ((strlen - start) > len)
|
|
{
|
|
int width = FindWidth(title, start, len);
|
|
retval.Add(title.Substring(start, width));
|
|
start += width;
|
|
while (title[start] == ' ') start++;
|
|
}
|
|
if (strlen - start > 0)
|
|
retval.Add(title.Substring(start, strlen - start));
|
|
return retval;
|
|
}
|
|
private int FindWidth(string title, int start, int len)
|
|
{
|
|
for (int ii = start + len; ii > start; ii--)
|
|
{
|
|
if (title[ii] == ' ')
|
|
{
|
|
while (title[ii] == ' ') ii--;
|
|
if (ii > start)
|
|
return 2 + ii - start;
|
|
return len;
|
|
}
|
|
}
|
|
return len;
|
|
}
|
|
private SvgPart PageItemToSvgUse(VEPROMS.CSLA.Library.PageItem pageItem, string templateName)
|
|
{
|
|
SvgUse svgUse = new SvgUse();
|
|
svgUse.UseID = templateName;
|
|
svgUse.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT);
|
|
svgUse.Y = new SvgMeasurement((float)(pageItem.Row ?? 0), E_MeasurementUnits.PT);
|
|
return svgUse;
|
|
}
|
|
private static SvgText PageItemToSvgText(VEPROMS.CSLA.Library.PageItem pageItem, string text)
|
|
{
|
|
SvgText svgText = new SvgText();
|
|
svgText.Text = text;
|
|
VEPROMS.CSLA.Library.E_Justify justify = pageItem.Justify ?? VEPROMS.CSLA.Library.E_Justify.PSLeft;
|
|
if ((justify & VEPROMS.CSLA.Library.E_Justify.PSLeft) == VEPROMS.CSLA.Library.E_Justify.PSLeft)
|
|
svgText.Justify = SvgJustify.Left;
|
|
else if ((justify & VEPROMS.CSLA.Library.E_Justify.PSRight) == VEPROMS.CSLA.Library.E_Justify.PSRight)
|
|
svgText.Justify = SvgJustify.Right;
|
|
else
|
|
svgText.Justify = SvgJustify.Center;
|
|
svgText.Font = pageItem.Font.WindowsFont;
|
|
svgText.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT);
|
|
svgText.Y = new SvgMeasurement((float)(pageItem.Row ?? 0), E_MeasurementUnits.PT);
|
|
if (svgText.Font.Underline && svgText.Text.EndsWith(" ")) svgText.Text = svgText.Text.Substring(0, svgText.Text.Length - 1) + "\xA0";// replace last space with a hardspace
|
|
return svgText;
|
|
}
|
|
private SvgPart PageItemToSvgText(VEPROMS.CSLA.Library.PageItem pageItem, string text, float yOffset)
|
|
{
|
|
SvgText svgText = new SvgText();
|
|
svgText.Text = text;
|
|
VEPROMS.CSLA.Library.E_Justify justify = pageItem.Justify ?? VEPROMS.CSLA.Library.E_Justify.PSLeft;
|
|
if ((justify & VEPROMS.CSLA.Library.E_Justify.PSLeft) == VEPROMS.CSLA.Library.E_Justify.PSLeft)
|
|
svgText.Justify = SvgJustify.Left;
|
|
else if ((justify & VEPROMS.CSLA.Library.E_Justify.PSRight) == VEPROMS.CSLA.Library.E_Justify.PSRight)
|
|
svgText.Justify = SvgJustify.Right;
|
|
else
|
|
svgText.Justify = SvgJustify.Center;
|
|
svgText.Font = pageItem.Font.WindowsFont;
|
|
svgText.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT);
|
|
svgText.Y = new SvgMeasurement((float)(yOffset + pageItem.Row ?? 0), E_MeasurementUnits.PT);
|
|
if (svgText.Font.Underline && svgText.Text.EndsWith(" ")) svgText.Text = svgText.Text.Substring(0, svgText.Text.Length - 1) + "\xA0";// replace last space with a hardspace
|
|
return svgText;
|
|
}
|
|
private List<string> _MissingTokens = new List<string>();
|
|
protected override string ReplacePageListToken(Match match)
|
|
{
|
|
switch (match.Value)
|
|
{
|
|
case "{PAGE}": // Current Page Number
|
|
return CurrentPageNumber.ToString();
|
|
case "{OF}": // Total Page Count for this section
|
|
return CurrentPageOf.ToString();
|
|
case "{REVDATE}": // Revision Date
|
|
return RevDate;
|
|
case "{REV}": // Revision Number
|
|
return Rev;
|
|
}
|
|
if (!_MissingTokens.Contains(match.Value))
|
|
{
|
|
_MissingTokens.Add(match.Value);
|
|
Console.WriteLine("\t\t\t\tcase \"{0}\": // Unhandled Token\r\n\t\t\t\t\treturn \"\";", match.Value);
|
|
}
|
|
return "";
|
|
}
|
|
}
|
|
}
|