SvgFontAwesome.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Svg;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15. using DsPdfWeb.Demos.Common;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This sample shows how to render SVG icons included in the "Free for Web" download of Font Awesome.
  20. // The sample code also shows how to change the default color of the glyphs.
  21. public class SvgFontAwesome
  22. {
  23. private Random _rnd = Util.NewRandom();
  24.  
  25. public int CreatePDF(Stream stream, int paramsIdx = 0)
  26. {
  27. return CreatePDF(stream, GetSampleParamsList()[paramsIdx]);
  28. }
  29.  
  30. public int CreatePDF(Stream stream, string[] sampleParams)
  31. {
  32. // Load Font Awesome glyphs from the resources:
  33. var images = new List<(string, GcSvgDocument)>();
  34. var colorize = sampleParams[3] == "colorize";
  35.  
  36. if (colorize)
  37. {
  38. foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "FontAwesome"), "*.svg", SearchOption.AllDirectories))
  39. images.Add((fname, GcSvgDocument.FromFile(fname)));
  40. // Randomize image order:
  41. images.Shuffle();
  42. }
  43. else
  44. {
  45. foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "FontAwesome", sampleParams[3]), "*.svg", SearchOption.TopDirectoryOnly))
  46. images.Add((fname, GcSvgDocument.FromFile(fname)));
  47. // Sort images by file name:
  48. images.Sort(new Comparison<(string, GcSvgDocument)>((t1_, t2_) => StringComparer.OrdinalIgnoreCase.Compare(t1_.Item1, t2_.Item1)));
  49. }
  50.  
  51. var doc = new GcPdfDocument();
  52. // Font and format for captions:
  53. const float sMargin = 72f / 8;
  54. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
  55. var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
  56.  
  57. // Set up a layout grid:
  58. var background = Color.White;
  59. var foreground = Color.Black;
  60. const float margin = 36;
  61. const int rows = 12;
  62. const int cols = 9;
  63. float gapx = 72f / 8, gapy = gapx;
  64. float sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols;
  65. float sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows;
  66. if (sWidth > sHeight)
  67. {
  68. gapx += sWidth - sHeight;
  69. sWidth = sHeight;
  70. }
  71. else
  72. {
  73. gapy += sHeight - sWidth;
  74. sHeight = sWidth;
  75. }
  76. var ip = new PointF(margin, margin);
  77.  
  78. // Render all images within the grid, adding new pages as needed:
  79. var g = doc.NewPage().Graphics;
  80. for (int i = 0; i < images.Count(); ++i)
  81. {
  82. // Draw border around image:
  83. var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
  84. g.FillRectangle(rect, background);
  85. g.DrawRectangle(rect, foreground, 0.5f);
  86. rect.Inflate(-sMargin, -sMargin);
  87.  
  88. // Draw the SVG:
  89. var svg = images[i].Item2;
  90. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  91. if (s.Width > 0 && s.Height > 0)
  92. {
  93. // If image proportions are different from our target rectangle,
  94. // we resize the rectangle centering the image in it:
  95. var qSrc = s.Width / s.Height;
  96. var qTgt = rect.Width / rect.Height;
  97. if (qSrc < qTgt)
  98. rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0);
  99. else if (qSrc > qTgt)
  100. rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2);
  101. }
  102. //
  103. if (colorize)
  104. svg.RootSvg.Fill = new SvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)));
  105.  
  106. // Render the SVG:
  107. g.DrawSvg(svg, rect);
  108.  
  109. // Print the icon file name as caption in the bottom margin:
  110. g.DrawString(Path.GetFileNameWithoutExtension(images[i].Item1), tf,
  111. new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  112. TextAlignment.Center, ParagraphAlignment.Near, false);
  113. ip.X += sWidth;
  114. if (ip.X + sWidth > doc.PageSize.Width && i < images.Count() - 1)
  115. {
  116. ip.X = margin;
  117. ip.Y += sHeight;
  118. if (ip.Y + sHeight > doc.PageSize.Height)
  119. {
  120. g = doc.NewPage().Graphics;
  121. ip.Y = margin;
  122. }
  123. }
  124. }
  125. // Done:
  126. doc.Save(stream);
  127. // Dispose images after saving the PDF:
  128. images.ForEach(t_ => t_.Item2.Dispose());
  129. return doc.Pages.Count;
  130. }
  131.  
  132. public static List<string[]> GetSampleParamsList()
  133. {
  134. // Strings are name, description, info, rest are arbitrary strings:
  135. return new List<string[]>()
  136. {
  137. new string[] { "@b-svg/Font Awesome - brands", "Render Font Awesome SVG glyphs from the \"svgs/brands\" directory",
  138. "This sample renders the SVG icons included in the \"svgs/brands/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  139. "brands" },
  140. new string[] { "@b-svg/Font Awesome - regular", "Render Font Awesome SVG glyphs from the \"svgs/regular\" directory",
  141. "This sample renders the SVG icons included in the \"svgs/regular/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  142. "regular" },
  143. new string[] { "@b-svg/Font Awesome - solid", "Render Font Awesome SVG glyphs from the \"svgs/solid\" directory",
  144. "This sample renders the SVG icons included in the \"svgs/solid/\" directory of the Font Awesome \"Free for Web\" download, sorted by file name.",
  145. "solid" },
  146. new string[] { "@b-svg/Font Awesome - colorize", "Render Font Awesome SVG glyphs using random order and colors",
  147. "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.",
  148. "colorize" },
  149. };
  150. }
  151. }
  152. }
  153.