SvgFontAwesome.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.Linq;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Imaging;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Drawing;
  13. using GrapeCity.Documents.Svg;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16. using DsImagingWeb.Demos.Common;
  17.  
  18. namespace DsImagingWeb.Demos
  19. {
  20. // This sample shows how to render SVG icons included in the "Free for Web" download of Font Awesome.
  21. // The sample code also shows how to change the default color of the glyphs.
  22. public class SvgFontAwesome
  23. {
  24. private Random _rnd = Util.NewRandom();
  25.  
  26. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  27. {
  28. if (sampleParams == null)
  29. sampleParams = GetSampleParamsList()[0];
  30.  
  31. // Font and format for captions:
  32. const float sMargin = 96f / 8;
  33. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
  34. var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
  35.  
  36. // Set up a layout grid:
  37. var background = Color.White;
  38. var foreground = Color.Black;
  39. const float margin = 36;
  40. const int rows = 10;
  41. const int cols = 10;
  42. float gapx = 96f / 8, gapy = gapx;
  43. float sWidth = (pixelSize.Width - margin * 2 + gapx) / cols;
  44. float sHeight = (pixelSize.Height - margin * 2 + gapy) / rows;
  45. if (sWidth > sHeight)
  46. {
  47. gapx += sWidth - sHeight;
  48. sWidth = sHeight;
  49. }
  50. else
  51. {
  52. gapy += sHeight - sWidth;
  53. sHeight = sWidth;
  54. }
  55. var ip = new PointF(margin, margin);
  56. var colorize = sampleParams[3] == "colorize";
  57. var maxFiles = targetMime == Util.MimeTypes.TIFF ? int.MaxValue : rows * cols;
  58. var fileNames = Directory.GetFiles(Path.Combine("Resources", "FontAwesome", colorize ? string.Empty : sampleParams[3]), "*.svg", SearchOption.AllDirectories).ToList();
  59. if (colorize)
  60. fileNames.Shuffle();
  61. else
  62. fileNames.Sort(StringComparer.Ordinal);
  63. var files = fileNames.Take(maxFiles);
  64.  
  65. var images = new List<(string, GcSvgDocument)>(files.Count());
  66. foreach (var fname in files)
  67. images.Add((fname, GcSvgDocument.FromFile(fname)));
  68.  
  69. // Render all images within the grid, adding new pages as needed:
  70. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, false, dpi, dpi);
  71. var g = bmp.CreateGraphics(Color.White);
  72. var ms = new MemoryStream();
  73. GcTiffWriter tw = null;
  74. if (targetMime == Util.MimeTypes.TIFF)
  75. tw = new GcTiffWriter(ms);
  76.  
  77. for (int i = 0; i < images.Count(); ++i)
  78. {
  79. // Draw border around image:
  80. var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
  81. g.FillRectangle(rect, background);
  82. g.DrawRectangle(rect, foreground, 0.5f);
  83. rect.Inflate(-sMargin, -sMargin);
  84.  
  85. // Draw the SVG:
  86. var svg = images[i].Item2;
  87. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  88. if (s.Width > 0 && s.Height > 0)
  89. {
  90. // If image proportions are different from our target rectangle,
  91. // we resize the rectangle centering the image in it:
  92. var qSrc = s.Width / s.Height;
  93. var qTgt = rect.Width / rect.Height;
  94. if (qSrc < qTgt)
  95. rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0);
  96. else if (qSrc > qTgt)
  97. rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2);
  98. }
  99. //
  100. if (colorize)
  101. svg.RootSvg.Fill = new SvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)));
  102.  
  103. // Render the SVG:
  104. g.DrawSvg(svg, rect);
  105.  
  106. // Print the icon file name as caption in the bottom margin:
  107. g.DrawString(Path.GetFileNameWithoutExtension(images[i].Item1), tf,
  108. new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  109. TextAlignment.Center, ParagraphAlignment.Near, false);
  110. ip.X += sWidth;
  111. if (ip.X + sWidth > pixelSize.Width && i < images.Count() - 1)
  112. {
  113. ip.X = margin;
  114. ip.Y += sHeight;
  115. if (ip.Y + sHeight > pixelSize.Height)
  116. {
  117. if (targetMime != Util.MimeTypes.TIFF)
  118. throw new Exception("Unexpected: should get here only if target is TIFF.");
  119.  
  120. tw.AppendFrame(bmp);
  121. bmp.Clear(Color.White);
  122. ip.Y = margin;
  123. }
  124. }
  125. }
  126. switch (targetMime)
  127. {
  128. case Common.Util.MimeTypes.TIFF:
  129. tw.Dispose();
  130. break;
  131. case Common.Util.MimeTypes.JPEG:
  132. bmp.SaveAsJpeg(ms);
  133. break;
  134. case Common.Util.MimeTypes.PNG:
  135. bmp.SaveAsPng(ms);
  136. break;
  137. case Common.Util.MimeTypes.BMP:
  138. bmp.SaveAsBmp(ms);
  139. break;
  140. case Common.Util.MimeTypes.GIF:
  141. bmp.SaveAsGif(ms);
  142. break;
  143. case Common.Util.MimeTypes.WEBP:
  144. bmp.SaveAsWebp(ms);
  145. break;
  146. default:
  147. throw new Exception($"Encoding {targetMime} is not supported.");
  148. }
  149. bmp.Dispose();
  150. g.Dispose();
  151. // Dispose images when done:
  152. images.ForEach(t_ => t_.Item2.Dispose());
  153. return ms;
  154. }
  155.  
  156. public static List<string[]> GetSampleParamsList()
  157. {
  158. // Strings are name, description, info, rest are arbitrary strings:
  159. return new List<string[]>()
  160. {
  161. new string[] { "@b-svg/Font Awesome - brands", "Render Font Awesome SVG glyphs from the \"svgs/brands\" directory",
  162. "This sample renders the SVG icons included in the \"svgs/brands/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  163. "brands" },
  164. new string[] { "@b-svg/Font Awesome - regular", "Render Font Awesome SVG glyphs from the \"svgs/brands\" directory",
  165. "This sample renders the SVG icons included in the \"svgs/regular/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  166. "regular" },
  167. new string[] { "@b-svg/Font Awesome - solid", "Render Font Awesome SVG glyphs from the \"svgs/solid\" directory",
  168. "This sample renders the SVG icons included in the \"svgs/solid/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  169. "solid" },
  170. new string[] { "@b-svg/Font Awesome - colorize", "Render Font Awesome SVG glyphs using random order and colors",
  171. "This sample renders the SVG icons included in the \"svgs/\" directory of the Font Awesome \"Free for Web\" download, randomizing the order of the icons and their colors.",
  172. "colorize" },
  173. };
  174. }
  175. }
  176. }
  177.