// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Web; using System.Drawing; namespace Spludlow.Printing { public class PrintDocReader: IDisposable { private IPrintDoc Printer; private StringReader Reader; public PrintDocHeader Header; private Spludlow.Drawing.BarCode BarCodeEncoder; public PrintDocReader(string printData, IPrintDoc printer) { this.Printer = printer; this.Reader = new StringReader(printData); string line = this.Reader.ReadLine(); this.Header = new PrintDocHeader(line); this.BarCodeEncoder = new Drawing.BarCode(); } public void Print() { while (PrintNext() == true) { } } public bool PrintNext() { string line = this.Reader.ReadLine(); if (line == null) return false; string[] words = line.Split(new char[] { '\t' }); switch (words[0]) { case "NewPage": // this.AddItem("NewPage", new string[] { (this.PageCount - 1).ToString() }); use stored index ??? this.Printer.NewPage(); break; case "Text": // void Text(string text, string font, float x, float y, Color colour); this.Printer.Text(HttpUtility.UrlDecode(words[1]), words[2], Single.Parse(words[3]), Single.Parse(words[4]), Spludlow.Drawing.Colour.Decode(words[5]), Single.Parse(words[6]), StringAlignmentParse(words[7]), Single.Parse(words[8]), Spludlow.Drawing.Colour.Decode(words[9])); break; case "TextBox": // void TextBox(string text, string font, float x, float y, float width, float height, Color colour); this.Printer.TextBox(HttpUtility.UrlDecode(words[1]), words[2], Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Single.Parse(words[6]), Spludlow.Drawing.Colour.Decode(words[7]), Single.Parse(words[8]), StringAlignmentParse(words[9]), Single.Parse(words[10]), Spludlow.Drawing.Colour.Decode(words[11])); break; case "BarCode": // void BarCode(string data, string type, float x, float y, float width, float height, Color foreColour, Color backColour, bool rotate); this.Printer.BarCode(words[1], words[2], Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Single.Parse(words[6]), Spludlow.Drawing.Colour.Decode(words[7]), Spludlow.Drawing.Colour.Decode(words[8]), Boolean.Parse(words[9])); break; case "Line": // void Line(float x1, float y1, float x2, float y2, float lineWidth, Color lineColour); this.Printer.Line(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Spludlow.Drawing.Colour.Decode(words[6])); break; case "Rectangle": // void Rectangle(float x, float y, float width, float height, float lineWidth, Color lineColour, Color fillColour); this.Printer.Rectangle(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Spludlow.Drawing.Colour.Decode(words[6]), Spludlow.Drawing.Colour.Decode(words[7])); break; case "Cirlce": //void Cirlce(float x, float y, float radius, float lineWidth, Color lineColour, Color fillColour); this.Printer.Cirlce(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Spludlow.Drawing.Colour.Decode(words[5]), Spludlow.Drawing.Colour.Decode(words[6])); break; case "Ellipse": //void Ellipse(float x, float y, float xRadius, float yRadius, float lineWidth, Color lineColour, Color fillColour); this.Printer.Ellipse(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Spludlow.Drawing.Colour.Decode(words[6]), Spludlow.Drawing.Colour.Decode(words[7])); break; case "Arc": //void Arc(float x, float y, float xRadius, float yRadius, float startAngle, float endAngle, float lineWidth, Color lineColour); this.Printer.Arc(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Single.Parse(words[6]), Single.Parse(words[7]), Spludlow.Drawing.Colour.Decode(words[8])); break; case "ArcSector": //void ArcSector(float x, float y, float xOuterRadius, float yOuterRadius, float xInnerRadius, float yInnerRadius, float startAngle, float endAngle, float lineWidth, Color lineColour, Color fillColour); this.Printer.ArcSector(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Single.Parse(words[6]), Single.Parse(words[7]), Single.Parse(words[8]), Single.Parse(words[9]), Spludlow.Drawing.Colour.Decode(words[10]), Spludlow.Drawing.Colour.Decode(words[11])); break; case "Polygon": //void Polygon(float x, float y, float xRadius, float yRadius, int sides, float rotateAngle, float lineWidth, Color lineColour, Color fillColour); this.Printer.Polygon(Single.Parse(words[1]), Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Int32.Parse(words[5]), Single.Parse(words[6]), Single.Parse(words[7]), Spludlow.Drawing.Colour.Decode(words[8]), Spludlow.Drawing.Colour.Decode(words[9])); break; case "Shape": // void Shape(PointF[] points, float lineWidth, Color lineColour, Color fillColour); this.Printer.Shape(PrintDoc.DecodePoints(words[1]), Single.Parse(words[2]), Spludlow.Drawing.Colour.Decode(words[3]), Spludlow.Drawing.Colour.Decode(words[4])); break; case "Curve": // void Curve(PointF[] points, float lineWidth, Color lineColour); this.Printer.Curve(PrintDoc.DecodePoints(words[1]), Single.Parse(words[2]), Spludlow.Drawing.Colour.Decode(words[3])); break; case "Lines": // void Lines(PointF[] points, float lineWidth, Color lineColour); this.Printer.Lines(PrintDoc.DecodePoints(words[1]), Single.Parse(words[2]), Spludlow.Drawing.Colour.Decode(words[3])); break; case "Place": // void Place(string filename, float x, float y, float scale, int sourcePageNumber); this.Printer.Place(words[1], Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Int32.Parse(words[5])); break; case "PlaceAngle": // void Place(string filename, float x, float y, float scale, float angle, int sourcePageNumber); this.Printer.PlaceAngle(words[1], Single.Parse(words[2]), Single.Parse(words[3]), Single.Parse(words[4]), Single.Parse(words[5]), Int32.Parse(words[6])); break; default: throw new ApplicationException("Bad item in print doc: " + line); } return true; } private static StringAlignment StringAlignmentParse(string text) { return (StringAlignment)Enum.Parse(typeof(StringAlignment), text); } public void Dispose() { if (this.Reader != null) { this.Reader.Close(); this.Reader = null; } } } }