SvgGraphicsArch.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GrapeCity.Documents.Svg;
  15. using DsImagingWeb.Demos.Common;
  16. using GCTEXT = GrapeCity.Documents.Text;
  17. using GCDRAW = GrapeCity.Documents.Drawing;
  18.  
  19. namespace DsImagingWeb.Demos
  20. {
  21. // This example shows how to render an arch on GcSvgGraphics
  22. // using graphics paths. The arch is positioned in the center
  23. // of the resultant image using graphics transformation matrix.
  24. public class SvgGraphicsArch
  25. {
  26. public string DefaultMime { get => Common.Util.MimeTypes.SVG; }
  27.  
  28. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  29. {
  30. if (targetMime != Common.Util.MimeTypes.SVG)
  31. throw new Exception("This sample only supports SVG output format.");
  32.  
  33. Color background = Color.PaleGoldenrod,
  34. line = Color.FromArgb(11, 83, 69),
  35. arc = Color.FromArgb(22, 160, 133),
  36. fill = Color.FromArgb(255, 171, 145),
  37. marks = Color.DarkGray,
  38. text = Color.Black,
  39. textBack = Color.Cornsilk;
  40.  
  41. var Inch = dpi;
  42. var ms = new MemoryStream();
  43. using var g = new GcSvgGraphics(pixelSize.Width, pixelSize.Height);
  44. if (opaque)
  45. g.FillRectangle(new RectangleF(0, 0, g.Width, g.Height), background);
  46.  
  47. // Arc bounding rectangle:
  48. var rc = new RectangleF(0, 0, Inch * 4, Inch * 4.8f);
  49.  
  50. // Use transform to center all drawing:
  51. var t = g.Transform;
  52. g.Transform = Matrix3x2.CreateTranslation(g.Width / 2 - rc.Width / 2, g.Height / 2 - rc.Height / 2);
  53.  
  54. // Draw the arc:
  55. DrawArch(g, rc, rc.Height * 0.3f, rc.Width * 0.08f, line, arc, fill);
  56.  
  57. // Add a label:
  58. var tf = new TextFormat()
  59. {
  60. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  61. FontSize = Inch / 6,
  62. ForeColor = text,
  63. };
  64. g.DrawString("ARCH", tf, rc, TextAlignment.Center, ParagraphAlignment.Center, false);
  65.  
  66. // Measurement lines:
  67. var w = Inch / 8;
  68. var pen = new GCDRAW.Pen(marks);
  69.  
  70. var txt = $"{(rc.Width / Inch):F}\"";
  71. var s = g.MeasureString(txt, tf);
  72. var d = s.Height * 1.5f;
  73. g.DrawLine(rc.Left, rc.Top - d, rc.Right, rc.Top - d, pen);
  74. g.DrawLine(rc.Left, rc.Top - d, rc.Left + w, rc.Top - d - w, pen);
  75. g.DrawLine(rc.Left, rc.Top - d, rc.Left + w, rc.Top - d + w, pen);
  76. g.DrawLine(rc.Right, rc.Top - d, rc.Right - w, rc.Top - d - w, pen);
  77. g.DrawLine(rc.Right, rc.Top - d, rc.Right - w, rc.Top - d + w, pen);
  78.  
  79. var rcTxt = new RectangleF(rc.Left + rc.Width / 2 - s.Width / 2, rc.Top - d - s.Height / 2, s.Width, s.Height);
  80. rcTxt.Inflate(2, 2);
  81. g.FillRectangle(rcTxt, textBack);
  82. g.DrawString(txt, tf, rcTxt, TextAlignment.Center, ParagraphAlignment.Center, false);
  83.  
  84. txt = $"{(rc.Height / Inch):F}\"";
  85. s = g.MeasureString(txt, tf);
  86. d = s.Width;
  87. g.DrawLine(rc.Left - d, rc.Top, rc.Left - d, rc.Bottom, pen);
  88. g.DrawLine(rc.Left - d, rc.Top, rc.Left - d - w, rc.Top + w, pen);
  89. g.DrawLine(rc.Left - d, rc.Top, rc.Left - d + w, rc.Top + w, pen);
  90. g.DrawLine(rc.Left - d, rc.Bottom, rc.Left - d - w, rc.Bottom - w, pen);
  91. g.DrawLine(rc.Left - d, rc.Bottom, rc.Left - d + w, rc.Bottom - w, pen);
  92. rcTxt = new RectangleF(rc.Left - d - s.Width / 2, rc.Top + rc.Height / 2 - s.Height / 2, s.Width, s.Height);
  93. rcTxt.Inflate(2, 2);
  94. g.FillRectangle(rcTxt, textBack);
  95. g.DrawString(txt, tf, rcTxt, TextAlignment.Center, ParagraphAlignment.Center, false);
  96.  
  97. // Restore transform:
  98. g.Transform = t;
  99.  
  100. // Done:
  101. var svg = g.ToSvgDocument();
  102. svg.Save(ms);
  103. ms.Seek(0, SeekOrigin.Begin);
  104. return ms;
  105. }
  106.  
  107. private void DrawArch(GcGraphics g, RectangleF rc, float harc, float wd, Color line, Color arch, Color fill)
  108. {
  109. var path = g.CreatePath();
  110. // Insides filler (start from bottom left, to clockwise):
  111. path.BeginFigure(rc.Left + wd, rc.Bottom - wd);
  112. path.AddLine(rc.Left + wd, rc.Top + harc);
  113. path.AddArc(new ArcSegment()
  114. {
  115. ArcSize = ArcSize.Small,
  116. Point = new PointF(rc.Right - wd, rc.Top + harc),
  117. RotationAngle = 0,
  118. Size = new SizeF(rc.Width / 2f - wd, harc - wd),
  119. SweepDirection = SweepDirection.Clockwise
  120. });
  121. path.AddLine(rc.Right - wd, rc.Bottom - wd);
  122. path.EndFigure(FigureEnd.Closed);
  123. g.FillPath(path, fill);
  124. path.Dispose();
  125.  
  126. // Arc outlines (start from bottom left, to clockwise):
  127. path = g.CreatePath();
  128. path.BeginFigure(rc.Left, rc.Bottom);
  129. path.AddLine(rc.Left, rc.Top + harc);
  130. path.AddLine(rc.Left + wd, rc.Top + harc);
  131. path.AddLine(rc.Left + wd, rc.Bottom - wd);
  132. path.EndFigure(FigureEnd.Closed);
  133. path.BeginFigure(rc.Left, rc.Top + harc);
  134. path.AddArc(new ArcSegment()
  135. {
  136. ArcSize = ArcSize.Small,
  137. Point = new PointF(rc.Right, rc.Top + harc),
  138. RotationAngle = 0,
  139. Size = new SizeF(rc.Width / 2f, harc),
  140. SweepDirection = SweepDirection.Clockwise
  141. });
  142. path.AddLine(rc.Right - wd, rc.Top + harc);
  143. path.AddArc(new ArcSegment()
  144. {
  145. ArcSize = ArcSize.Small,
  146. Point = new PointF(rc.Left + wd, rc.Top + harc),
  147. RotationAngle = 0,
  148. Size = new SizeF(rc.Width / 2f - wd, harc - wd),
  149. SweepDirection = SweepDirection.CounterClockwise
  150. });
  151. path.EndFigure(FigureEnd.Closed);
  152. path.BeginFigure(rc.Right, rc.Top + harc);
  153. path.AddLine(rc.Right, rc.Bottom);
  154. path.AddLine(rc.Right - wd, rc.Bottom - wd);
  155. path.AddLine(rc.Right - wd, rc.Top + harc);
  156. path.EndFigure(FigureEnd.Closed);
  157. path.BeginFigure(rc.Right, rc.Bottom);
  158. path.AddLine(rc.Left, rc.Bottom);
  159. path.AddLine(rc.Left + wd, rc.Bottom - wd);
  160. path.AddLine(rc.Right - wd, rc.Bottom - wd);
  161. path.EndFigure(FigureEnd.Closed);
  162.  
  163. g.FillPath(path, arch);
  164. g.DrawPath(path, new GCDRAW.Pen(line, 2));
  165.  
  166. path.Dispose();
  167. }
  168. }
  169. }
  170.