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

namespace DsPdfWeb.Demos
{
    // This sample shows how to measure and scale the actual content of an SVG image.
    // It uses a sample SVG with intrinsic size larger than the actual content,
    // and content that is offset within the viewport.
    //
    // The same SVG is used to illustrate the difference between various SVG sizes in RenderSvgContent sample.
    //
    // The SVG art used in this sample is from freesvg.org.
    public class ScaleSvg
    {
        public int CreatePDF(Stream stream)
        {
            var svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg");
            using var svg = GcSvgDocument.FromFile(svgPath);

            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            page.Landscape = true;
            var g = page.Graphics;
            var margin = g.Resolution / 4;
            // var s = new SizeF(page.Size.Width - margin * 2, page.Size.Height - margin * 2);

            // Get the actual SVG image content bounds:
            var contentRc = g.MeasureSvg(svg, PointF.Empty);
            // Make sure the SVG content fits height-wise twice:
            var q = page.Size.Height / (contentRc.Height * 2.1f);
            contentRc.X *= q;
            contentRc.Y *= q;
            contentRc.Width *= q;
            contentRc.Height *= q;

            // Align the actual SVG content to point (0,0):
            var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
            s.Width *= q;
            s.Height *= q;
            // This rectangle (in points) is similar to the SVG view port.
            // It is also scaled so that the SVG content fits twice heightwise,
            // and the actual SVG content's top left corner is at the top left corner
            // of the rectangle:
            var rc = new RectangleF(-contentRc.X, -contentRc.Y, s.Width, s.Height);

            // Set up padding:
            const float pad = 12;
            rc.Offset(pad, pad);

            // Scale down, limit number of iterations for sanity:
            const float qDown = 0.8f;
            var currRc = rc;
            var currContentRc = contentRc;
            while (currRc.X + currContentRc.Right < page.Size.Width && currContentRc.Height > 8)
            {
                g.DrawSvg(svg, currRc);
                // Draw content bounds if debugging:
                // var trc = currContentRc;
                // trc.Offset(currRc.Location);
                // g.DrawRectangle(trc, Color.MediumPurple);
                // Scale SVG content down:
                currRc.Height *= qDown;
                currRc.Width *= qDown;
                currRc.Y -= (currContentRc.Top * qDown - currContentRc.Top);
                currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qDown;
                currContentRc.X *= qDown;
                currContentRc.Y *= qDown;
                currContentRc.Width *= qDown;
                currContentRc.Height *= qDown;
            }
            // Scale up:
            const float qUp = 1.2f;
            currRc = rc;
            currContentRc = contentRc;
            currRc.Offset(0, page.Size.Height - contentRc.Height - pad * 2);
            while (currRc.X + currContentRc.Right < page.Size.Width)
            {
                g.DrawSvg(svg, currRc);
                // Draw content bounds if debugging:
                // var trc = currContentRc;
                // trc.Offset(currRc.Location);
                // g.DrawRectangle(trc, Color.MediumPurple);
                // Scale SVG content up:
                currRc.Height *= qUp;
                currRc.Width *= qUp;
                currRc.Y -= (currContentRc.Bottom * qUp - currContentRc.Bottom);
                currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qUp;
                currContentRc.X *= qUp;
                currContentRc.Y *= qUp;
                currContentRc.Width *= qUp;
                currContentRc.Height *= qUp;
            }
            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}