SvgFontAwesome.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System
  6. Imports System.IO
  7. Imports System.Drawing
  8. Imports System.Linq
  9. Imports System.Collections.Generic
  10. Imports GrapeCity.Documents.Imaging
  11. Imports GrapeCity.Documents.Text
  12. Imports GrapeCity.Documents.Drawing
  13. Imports GrapeCity.Documents.Svg
  14. Imports GCTEXT = GrapeCity.Documents.Text
  15. Imports GCDRAW = GrapeCity.Documents.Drawing
  16.  
  17. '' This sample shows how to render SVG icons included in the "Free for Web" download of Font Awesome.
  18. '' The sample code also shows how to change the default color of the glyphs.
  19. Public Class SvgFontAwesome
  20. Private _rnd As Random = Util.NewRandom()
  21.  
  22. Public Function GenerateImageStream(
  23. ByVal targetMime As String,
  24. ByVal pixelSize As Size,
  25. ByVal dpi As Single,
  26. ByVal opaque As Boolean,
  27. Optional ByVal sampleParams As String() = Nothing) As Stream
  28.  
  29. If sampleParams Is Nothing Then
  30. sampleParams = GetSampleParamsList()(0)
  31. End If
  32.  
  33. '' Font and format for captions:
  34. Const sMargin As Single = 96.0F / 8.0F
  35. Dim font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf"))
  36. Dim tf = New TextFormat() With {.Font = font, .FontSize = sMargin * 0.65F}
  37.  
  38. '' Set up a layout grid:
  39. Dim background = Color.White
  40. Dim foreground = Color.Black
  41. Const margin As Single = 36.0F
  42. Const rows As Integer = 10
  43. Const cols As Integer = 10
  44. Dim gapx As Single = 96.0F / 8.0F, gapy As Single = gapx
  45. Dim sWidth As Single = (pixelSize.Width - margin * 2 + gapx) / cols
  46. Dim sHeight As Single = (pixelSize.Height - margin * 2 + gapy) / rows
  47. If sWidth > sHeight Then
  48. gapx += sWidth - sHeight
  49. sWidth = sHeight
  50. Else
  51. gapy += sHeight - sWidth
  52. sHeight = sWidth
  53. End If
  54. Dim ip As New PointF(margin, margin)
  55. Dim colorize As Boolean = (sampleParams(3) = "colorize")
  56. Dim maxFiles As Integer = If(targetMime = Util.MimeTypes.TIFF, Integer.MaxValue, rows * cols)
  57.  
  58. Dim basePath = Path.Combine("Resources", "FontAwesome", If(colorize, String.Empty, sampleParams(3)))
  59. Dim fileNames = Directory.GetFiles(basePath, "*.svg", SearchOption.AllDirectories).ToList()
  60. If colorize Then
  61. fileNames.Shuffle()
  62. Else
  63. fileNames.Sort(StringComparer.Ordinal)
  64. End If
  65. Dim files = fileNames.Take(maxFiles)
  66.  
  67. Dim images As New List(Of Tuple(Of String, GcSvgDocument))(files.Count())
  68. For Each fname In files
  69. images.Add(Tuple.Create(fname, GcSvgDocument.FromFile(fname)))
  70. Next
  71.  
  72. '' Render all images within the grid, adding new pages as needed:
  73. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, False, dpi, dpi)
  74. Dim g = bmp.CreateGraphics(Color.White)
  75. Dim ms As New MemoryStream()
  76. Dim tw As GcTiffWriter = Nothing
  77. If targetMime = Util.MimeTypes.TIFF Then
  78. tw = New GcTiffWriter(ms)
  79. End If
  80.  
  81. For i As Integer = 0 To images.Count() - 1
  82. '' Draw border around image:
  83. Dim rect As 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. Dim svg = images(i).Item2
  90. Dim s = svg.GetIntrinsicSize(SvgLengthUnits.Points)
  91. If s.Width > 0 AndAlso s.Height > 0 Then
  92. '' If image proportions are different from our target rectangle,
  93. '' we resize the rectangle centering the image in it:
  94. Dim qSrc = s.Width / s.Height
  95. Dim qTgt = rect.Width / rect.Height
  96. If qSrc < qTgt Then
  97. rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2.0F, 0)
  98. ElseIf qSrc > qTgt Then
  99. rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2.0F)
  100. End If
  101. End If
  102.  
  103. If colorize Then
  104. svg.RootSvg.Fill = New SvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)))
  105. End If
  106.  
  107. '' Render the SVG:
  108. g.DrawSvg(svg, rect)
  109.  
  110. '' Print the icon file name as caption in the bottom margin:
  111. g.DrawString(Path.GetFileNameWithoutExtension(images(i).Item1), tf,
  112. New RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  113. TextAlignment.Center, ParagraphAlignment.Near, False)
  114.  
  115. ip.X += sWidth
  116. If (ip.X + sWidth > pixelSize.Width) AndAlso (i < images.Count() - 1) Then
  117. ip.X = margin
  118. ip.Y += sHeight
  119. If ip.Y + sHeight > pixelSize.Height Then
  120. If targetMime <> Util.MimeTypes.TIFF Then
  121. Throw New Exception("Unexpected: should get here only if target is TIFF.")
  122. End If
  123. tw.AppendFrame(bmp)
  124. bmp.Clear(Color.White)
  125. ip.Y = margin
  126. End If
  127. End If
  128. Next
  129.  
  130. Select Case targetMime
  131. Case Util.MimeTypes.TIFF
  132. tw.Dispose()
  133. Case Util.MimeTypes.JPEG
  134. bmp.SaveAsJpeg(ms)
  135. Case Util.MimeTypes.PNG
  136. bmp.SaveAsPng(ms)
  137. Case Util.MimeTypes.BMP
  138. bmp.SaveAsBmp(ms)
  139. Case Util.MimeTypes.GIF
  140. bmp.SaveAsGif(ms)
  141. Case Util.MimeTypes.WEBP
  142. bmp.SaveAsWebp(ms)
  143. Case Else
  144. Throw New Exception($"Encoding {targetMime} is not supported.")
  145. End Select
  146.  
  147. bmp.Dispose()
  148. g.Dispose()
  149.  
  150. '' Dispose images when done:
  151. images.ForEach(Sub(t_) t_.Item2.Dispose())
  152.  
  153. Return ms
  154. End Function
  155.  
  156. Public Shared Function GetSampleParamsList() As List(Of String())
  157. '' Strings are name, description, info, rest are arbitrary strings:
  158. Return New List(Of String()) From {
  159. New String() {"@b-svg/Font Awesome - brands", "Render Font Awesome SVG glyphs from the ""svgs/brands"" directory",
  160. "This sample renders the SVG icons included in the ""svgs/brands/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
  161. "brands"},
  162. New String() {"@b-svg/Font Awesome - regular", "Render Font Awesome SVG glyphs from the ""svgs/brands"" directory",
  163. "This sample renders the SVG icons included in the ""svgs/regular/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
  164. "regular"},
  165. New String() {"@b-svg/Font Awesome - solid", "Render Font Awesome SVG glyphs from the ""svgs/solid"" directory",
  166. "This sample renders the SVG icons included in the ""svgs/solid/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.",
  167. "solid"},
  168. New String() {"@b-svg/Font Awesome - colorize", "Render Font Awesome SVG glyphs using random order and colors",
  169. "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.",
  170. "colorize"}
  171. }
  172. End Function
  173. End Class
  174.