SvgClipArt.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Svg;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
using DsImagingWeb.Demos.Common;

namespace DsImagingWeb.Demos
{
    // Use GcSvgDocument to rasterize and render SVG files to raster images.
    // The SVG clip art used in this sample is from freesvg.org.
    public class SvgClipArt
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            const int rows = 4;
            const int cols = 4;
            float margin = dpi / 2;
            float sMargin = margin / 4;

            // Load images from the resources folder:
            List<string> fnames = new List<string>(Directory.GetFiles(Path.Combine("Resources", "SvgClipArt"), "*", SearchOption.AllDirectories));
            fnames.Shuffle();
            var images = new List<(string, GcSvgDocument)>();
            foreach (var fname in fnames.Take(rows * cols))
                images.Add((Path.GetFileName(fname), GcSvgDocument.FromFile(fname)));

            // Font and format for captions:
            var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
            var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };

            // Set up a 4x4 layout grid:
            float gapx = margin / 4, gapy = gapx;
            float sWidth = (pixelSize.Width - margin * 2 + gapx) / cols;
            float sHeight = (pixelSize.Height - margin * 2 + gapy) / rows;
            if (sWidth > sHeight)
            {
                gapx += sWidth - sHeight;
                sWidth = sHeight;
            }
            else
            {
                gapy += sHeight - sWidth;
                sHeight = sWidth;
            }
            var ip = new PointF(margin, margin);

            // Resulting bitmap:
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            // Render images that fit within the grid:
            using var g = bmp.CreateGraphics(Color.White);
            for (int i = 0; i < images.Count(); ++i)
            {
                // Draw border around image:
                var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
                g.FillRectangle(rect, Color.LightGray);
                g.DrawRectangle(rect, Color.Black, 0.5f);
                rect.Inflate(-sMargin, -sMargin);
                // Print image file name as caption:
                g.DrawString(Path.GetFileName(images[i].Item1), tf,
                    new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
                    TextAlignment.Center, ParagraphAlignment.Near, false);
                // Draw the SVG:
                var svg = images[i].Item2;
                var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
                if (s.Width > 0 && s.Height > 0)
                {
                    // If image proportions are different from our target rectangle,
                    // we resize the rectangle centering the image in it:
                    var qSrc = s.Width / s.Height;
                    var qTgt = rect.Width / rect.Height;
                    if (qSrc < qTgt)
                        rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0);
                    else if (qSrc > qTgt)
                        rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2);
                }
                // Render the SVG:
                g.DrawSvg(svg, rect);
                // Move on:
                ip.X += sWidth;
                if (ip.X + sWidth > pixelSize.Width && i < images.Count() - 1)
                {
                    ip.X = margin;
                    ip.Y += sHeight;
                }
            }
            // Dispose images after saving the result:
            images.ForEach(t_ => t_.Item2.Dispose());
            // Done:
            return bmp;
        }
    }
}