SvgGraphicsText.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 GrapeCity.Documents.Imaging;
  9. using GrapeCity.Documents.Svg;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsImagingWeb.Demos
  16. {
  17. // TBD.
  18. public class SvgGraphicsText
  19. {
  20. public string DefaultMime { get => Common.Util.MimeTypes.SVG; }
  21.  
  22. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. if (targetMime != Common.Util.MimeTypes.SVG)
  25. throw new Exception("This sample only supports SVG output format.");
  26.  
  27. var Inch = dpi;
  28. var ms = new MemoryStream();
  29. using var g = new GcSvgGraphics(pixelSize.Width, pixelSize.Height);
  30.  
  31. // IMPORTANT: this property is true by default on GcSvgGraphics, which usually yields better text fidelity,
  32. // but text cannot be searched/selected/copied in this case, and the SVG file size is larger.
  33. // Setting this property to false results in text rendered using SVG 'text' elements, so that
  34. // text can be searched/selected/copied. Compare to the SvgGraphicsRoundRectangle example
  35. // which is identical to this example but draws text as paths:
  36. g.DrawTextAsPath = false;
  37.  
  38. if (opaque)
  39. g.FillRectangle(new RectangleF(0, 0, g.Width, g.Height), Color.White);
  40.  
  41. var rc = Common.Util.AddNote(
  42. "GcGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
  43. "This sample also shows how the same result may be achieved using graphics paths. " +
  44. "While they are not really needed for drawing round rectangles, graphics paths allow " +
  45. "to draw and fill arbitrary figures with complex geometries.\r\n\r\n" +
  46. "Note that this version of the \"Round Rectangles\" sample draws on a GcSvgGraphics, " +
  47. "and produces a vector SVG image rather than a raster image (JPEG, PNG etc.).",
  48. g);
  49.  
  50. // Rounded rectangle's radii:
  51. float rx = 36, ry = 24;
  52.  
  53. // Using dedicated methods to draw and fill round rectangles:
  54. var rEasy = new RectangleF(rc.Left, rc.Bottom + Inch / 2, Inch * 2, Inch);
  55. g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen);
  56. g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4);
  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. };
  63. g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, false);
  64.  
  65. // Using graphics path to achieve the same result:
  66. var rHard = rEasy;
  67. rHard.Offset(0, rEasy.Height + Inch / 2);
  68. var path = MakeRoundRect(g, rHard, rx, ry);
  69. g.FillPath(path, Color.PaleVioletRed);
  70. g.DrawPath(path, Color.Purple, 4);
  71. // Add a label:
  72. g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, false);
  73.  
  74. // Done:
  75. var svg = g.ToSvgDocument();
  76. svg.Save(ms);
  77. ms.Seek(0, SeekOrigin.Begin);
  78. return ms;
  79. }
  80.  
  81. // This method shows how to create a graphics path that may be used
  82. // to fill or draw arbitrary shapes on a GcGraphics.
  83. private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
  84. {
  85. var path = g.CreatePath();
  86. var sz = new SizeF(rx, ry);
  87. // start from horizontal top left
  88. path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
  89. path.AddLine(new PointF(rc.Right - rx, rc.Top));
  90. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  91. path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
  92. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
  93. path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
  94. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  95. path.AddLine(new PointF(rc.Left, rc.Top + ry));
  96. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
  97. path.EndFigure(FigureEnd.Closed);
  98. return path;
  99. }
  100. }
  101. }
  102.