'''' This code is part of Document Solutions for Imaging demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystemImportsSystem.IOImportsSystem.DrawingImportsSystem.LinqImportsSystem.Collections.GenericImportsGrapeCity.Documents.ImagingImportsGrapeCity.Documents.TextImportsGrapeCity.Documents.SvgImports GCTEXT = GrapeCity.Documents.Text '' This sample shows how to render SVG icons included in the "Free for Web" download of Font Awesome.'' The sample code also shows how to change the default color of the glyphs.PublicClassSvgFontAwesomePrivate_rndAsRandom = Util.NewRandom() PublicFunctionGenerateImageStream(ByVal targetMime AsString,ByVal pixelSize AsSize,ByVal dpi AsSingle,ByVal opaque AsBoolean,OptionalByVal sampleParams AsString() = Nothing) AsStream If sampleParams IsNothingThen sampleParams = GetSampleParamsList()(0)EndIf '' Font and format for captions:ConstsMarginAsSingle = 96.0F / 8.0FDim font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf"))Dim tf = NewTextFormat() With {.Font = font, .FontSize = sMargin * 0.65F} '' Set up a layout grid:Dim background = Color.WhiteDim foreground = Color.BlackConstmarginAsSingle = 36.0FConstrowsAsInteger = 10ConstcolsAsInteger = 10Dim gapx AsSingle = 96.0F / 8.0F, gapy AsSingle = gapxDim sWidth AsSingle = (pixelSize.Width - margin * 2 + gapx) / colsDim sHeight AsSingle = (pixelSize.Height - margin * 2 + gapy) / rowsIf sWidth > sHeight Then gapx += sWidth - sHeight sWidth = sHeightElse gapy += sHeight - sWidth sHeight = sWidthEndIfDim ip AsNewPointF(margin, margin)Dim colorize AsBoolean = (sampleParams(3) = "colorize")Dim maxFiles AsInteger = If(targetMime = Util.MimeTypes.TIFF, Integer.MaxValue, rows * cols) Dim basePath = Path.Combine("Resources", "FontAwesome", If(colorize, String.Empty, sampleParams(3)))Dim fileNames = Directory.GetFiles(basePath, "*.svg", SearchOption.AllDirectories).ToList()If colorize Then fileNames.Shuffle()Else fileNames.Sort(StringComparer.Ordinal)EndIfDim files = fileNames.Take(maxFiles) Dim images AsNewList(OfTuple(OfString, GcSvgDocument))(files.Count())ForEach fname In files images.Add(Tuple.Create(fname, GcSvgDocument.FromFile(fname)))Next '' Render all images within the grid, adding new pages as needed:Dim bmp = NewGcBitmap(pixelSize.Width, pixelSize.Height, False, dpi, dpi)Dim g = bmp.CreateGraphics(Color.White)Dim ms AsNewMemoryStream()Dim tw AsGcTiffWriter = NothingIf targetMime = Util.MimeTypes.TIFF Then tw = NewGcTiffWriter(ms)EndIf For i AsInteger = 0To images.Count() - 1'' Draw border around image:Dim rect AsNewRectangleF(ip, NewSizeF(sWidth - gapx, sHeight - gapy)) g.FillRectangle(rect, background) g.DrawRectangle(rect, foreground, 0.5F) rect.Inflate(-sMargin, -sMargin) '' Draw the SVG:Dim svg = images(i).Item2Dim s = svg.GetIntrinsicSize(SvgLengthUnits.Points)If s.Width > 0AndAlso s.Height > 0Then'' If image proportions are different from our target rectangle,'' we resize the rectangle centering the image in it:Dim qSrc = s.Width / s.HeightDim qTgt = rect.Width / rect.HeightIf qSrc < qTgt Then rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2.0F, 0)ElseIf qSrc > qTgt Then rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2.0F)EndIfEndIf If colorize Then svg.RootSvg.Fill = NewSvgPaint(Color.FromArgb(_rnd.Next(256), _rnd.Next(256), _rnd.Next(256)))EndIf '' Render the SVG: g.DrawSvg(svg, rect) '' Print the icon file name as caption in the bottom margin: g.DrawString(Path.GetFileNameWithoutExtension(images(i).Item1), tf,NewRectangleF(rect.X, rect.Bottom, rect.Width, sMargin),TextAlignment.Center, ParagraphAlignment.Near, False) ip.X += sWidthIf (ip.X + sWidth > pixelSize.Width) AndAlso (i < images.Count() - 1) Then ip.X = margin ip.Y += sHeightIf ip.Y + sHeight > pixelSize.HeightThenIf targetMime <> Util.MimeTypes.TIFF ThenThrowNewException("Unexpected: should get here only if target is TIFF.")EndIf tw.AppendFrame(bmp) bmp.Clear(Color.White) ip.Y = marginEndIfEndIfNext SelectCase targetMimeCase Util.MimeTypes.TIFF tw.Dispose()Case Util.MimeTypes.JPEG bmp.SaveAsJpeg(ms)Case Util.MimeTypes.PNG bmp.SaveAsPng(ms)Case Util.MimeTypes.BMP bmp.SaveAsBmp(ms)Case Util.MimeTypes.GIF bmp.SaveAsGif(ms)Case Util.MimeTypes.WEBP bmp.SaveAsWebp(ms)CaseElseThrowNewException($"Encoding {targetMime} is not supported.")EndSelect bmp.Dispose() g.Dispose() '' Dispose images when done: images.ForEach(Sub(t_) t_.Item2.Dispose()) Return msEndFunction PublicSharedFunctionGetSampleParamsList() AsList(OfString())'' Strings are name, description, info, rest are arbitrary strings:ReturnNewList(OfString()) From {NewString() {"@b-svg/Font Awesome - brands", "Render Font Awesome SVG glyphs from the ""svgs/brands"" directory","This sample renders the SVG icons included in the ""svgs/brands/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.","brands"},NewString() {"@b-svg/Font Awesome - regular", "Render Font Awesome SVG glyphs from the ""svgs/brands"" directory","This sample renders the SVG icons included in the ""svgs/regular/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.","regular"},NewString() {"@b-svg/Font Awesome - solid", "Render Font Awesome SVG glyphs from the ""svgs/solid"" directory","This sample renders the SVG icons included in the ""svgs/solid/"" directory of the Font Awesome ""Free for Web"" download, sorted by file name.","solid"},NewString() {"@b-svg/Font Awesome - colorize", "Render Font Awesome SVG glyphs using random order and colors","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.","colorize"} }EndFunctionEndClass