HpglDocument.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using FastReport.Export.Hpgl.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text;
  8. namespace FastReport.Export.Hpgl
  9. {
  10. public class HpglDocument
  11. {
  12. StringBuilder docString;
  13. private List<CommandBase> commands;
  14. private string separator;
  15. private string terminator;
  16. public HpglDocument()
  17. {
  18. docString = new StringBuilder();
  19. commands = new List<CommandBase>();
  20. separator = ",";
  21. terminator = ";";
  22. }
  23. public void Start(SizeF pageSize)
  24. {
  25. commands.Initialize();
  26. commands.InputP1P2(new PointF(0, 0), new PointF(pageSize.Width, pageSize.Height));
  27. commands.Scale(0, 100, 0, 100);
  28. }
  29. public void Clear()
  30. {
  31. docString.Clear();
  32. commands.Clear();
  33. }
  34. public override string ToString()
  35. {
  36. foreach (CommandBase cmnd in commands)
  37. {
  38. cmnd.AppendTo(docString);
  39. docString.Append("\n");
  40. }
  41. return docString.ToString();
  42. }
  43. public void DrawLine(float x1, float y1, float x2, float y2, Color strokeColor, float strokeThickness)
  44. {
  45. DrawLine(x1, y1, x2, y2, strokeColor, strokeThickness, LineStyle.Solid);
  46. }
  47. LinePattern GetPattern(LineStyle lineStyle)
  48. {
  49. LinePattern pattern = LinePattern.None;
  50. switch (lineStyle)
  51. {
  52. case LineStyle.Solid:
  53. pattern = LinePattern.None; break;
  54. case LineStyle.Dash:
  55. pattern = LinePattern.LongDashed; break;
  56. case LineStyle.DashDot:
  57. pattern = LinePattern.ShortDashed; break;
  58. case LineStyle.DashDotDot:
  59. pattern = LinePattern.ShortDotted; break;
  60. case LineStyle.Dot:
  61. pattern = LinePattern.Dotted; break;
  62. }
  63. return pattern;
  64. }
  65. public void DrawLine(float x1, float y1, float x2, float y2, Color strokeColor, float strokeThickness, LineStyle lineStyle)
  66. {
  67. //commands.SelectPen();
  68. commands.PenThickness(strokeThickness);
  69. LinePattern pattern = GetPattern(lineStyle);
  70. commands.LineType(pattern);
  71. commands.PlotAbsolute(x1, y1);
  72. commands.PenDown();
  73. commands.PlotAbsolute(x2, y2);
  74. commands.PenUp();
  75. }
  76. internal void DrawPolyLine(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  77. float polyLineWidth, LineStyle polyLineStyle, bool isClosedPolyline, Brush brush, PolygonType mode)
  78. {
  79. bool isPenDown = false;
  80. commands.PenThickness(polyLineWidth);
  81. LinePattern pattern = GetPattern(polyLineStyle);
  82. commands.LineType(pattern);
  83. // set fill mode: solid, hatch
  84. ShadingModel shading = ShadingModel.None;
  85. if (brush != null)
  86. {
  87. if (brush.GetType() == typeof(SolidBrush))
  88. {
  89. }
  90. else if (brush.GetType() == typeof(HatchBrush))
  91. {
  92. shading = ShadingModel.Hatching;
  93. }
  94. }
  95. if (isClosedPolyline)
  96. {
  97. if (brush != null)
  98. commands.FillType(shading);
  99. commands.PlotAbsolute(x + points[points.Length - 1].X, y + points[points.Length - 1].Y);
  100. commands.PolygonMode(mode);
  101. commands.PenDown();
  102. isPenDown = true;
  103. }
  104. //else
  105. // commands.PolygonMode(PolygonType.Cleared);
  106. foreach (PointF point in points)
  107. {
  108. commands.PlotAbsolute(x + point.X, y + point.Y);
  109. if (!isClosedPolyline && !isPenDown)
  110. {
  111. commands.PenDown();
  112. isPenDown = true;
  113. }
  114. }
  115. if (isClosedPolyline && brush != null)
  116. {
  117. if ((brush is SolidBrush))
  118. {
  119. if ((brush as SolidBrush).Color != Color.Transparent)
  120. commands.FillPolygon();
  121. }
  122. else
  123. commands.FillPolygon();
  124. }
  125. commands.EdgePolygon();
  126. commands.PenUp();
  127. }
  128. internal void DrawPolyLine(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  129. float polyLineWidth, LineStyle polyLineStyle, bool isClosedPolyline, Brush brush)
  130. {
  131. DrawPolyLine(x, y, points, pointTypes, polyLineColor, polyLineWidth, polyLineStyle, isClosedPolyline, brush, PolygonType.Cleared);
  132. }
  133. internal void DrawPolyLine(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  134. float polyLineWidth, LineStyle polyLineStyle, bool isClosedPolyline)
  135. {
  136. DrawPolyLine(x, y, points, pointTypes, polyLineColor, polyLineWidth, polyLineStyle, isClosedPolyline, null);
  137. }
  138. internal void DrawPolygon(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  139. float polyLineWidth, LineStyle polyLineStyle, Brush brush, PolygonType mode)
  140. {
  141. DrawPolyLine(x, y, points, pointTypes, polyLineColor, polyLineWidth, polyLineStyle, true, brush, mode);
  142. }
  143. internal void DrawPolygon(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  144. float polyLineWidth, LineStyle polyLineStyle, Brush brush)
  145. {
  146. DrawPolyLine(x, y, points, pointTypes, polyLineColor, polyLineWidth, polyLineStyle, true, brush, PolygonType.Cleared);
  147. }
  148. internal void DrawPolygon(float x, float y, PointF[] points, byte[] pointTypes, Color polyLineColor,
  149. float polyLineWidth, LineStyle polyLineStyle)
  150. {
  151. DrawPolyLine(x, y, points, pointTypes, polyLineColor, polyLineWidth, polyLineStyle, true, null);
  152. }
  153. internal void FillPolygon(float x, float y, PointF[] points, byte[] pointTypes, Brush brush)
  154. {
  155. DrawPolygon(x, y, points, pointTypes, (brush as SolidBrush).Color, 0.1f, LineStyle.Solid, brush, PolygonType.Cleared);
  156. }
  157. internal void FillRectangle(float x, float y, float width, float height, Brush brush)
  158. {
  159. //Entities.AddSolid(x, y, width, height, color);
  160. PointF[] points = new PointF[]
  161. {
  162. new PointF(0, 0),
  163. new PointF(width, 0),
  164. new PointF(width, height),
  165. new PointF(0, height)
  166. };
  167. FillPolygon(x, y, points, new byte[] { }, brush);
  168. }
  169. internal void DrawEllipse(float x, float y, float width, float height, Color ellipseColor, float ellipseWidth, LineStyle ellipseStyle)
  170. {
  171. //convert ellipse to polylines
  172. float a = width / 2;
  173. float b = height / 2;
  174. float step = 0.1f;
  175. int pointsCount = (int)Math.Ceiling(width / step);
  176. PointF[] pointsTop = new PointF[pointsCount + 1];
  177. PointF[] pointsBot = new PointF[pointsCount + 1];
  178. float xCur = -a;
  179. float yTop;
  180. for (int i = 0; i < pointsCount; i++)
  181. {
  182. yTop = (float)Math.Sqrt(Math.Pow(b, 2) - (Math.Pow(xCur, 2) * Math.Pow(b, 2) / Math.Pow(a, 2)));
  183. pointsTop[i] = new PointF(xCur, yTop);
  184. pointsBot[i] = new PointF(xCur, -yTop);
  185. xCur += step;
  186. }
  187. yTop = (float)Math.Sqrt(Math.Pow(b, 2) - (Math.Pow(a, 2) * Math.Pow(b, 2) / Math.Pow(a, 2)));
  188. pointsTop[pointsCount] = new PointF(a, yTop);
  189. pointsBot[pointsCount] = new PointF(a, -yTop);
  190. DrawPolyLine(x + a, y - b, pointsTop, new byte[pointsTop.Length], ellipseColor, ellipseWidth, ellipseStyle, false);
  191. DrawPolyLine(x + a, y - b, pointsBot, new byte[pointsTop.Length], ellipseColor, ellipseWidth, ellipseStyle, false);
  192. }
  193. }
  194. }