ShapeFillFormats.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample demonstrates the various fill formats
    // that can be used with shapes.
    public class ShapeFillFormats
    {
        public GcWordDocument CreateDocx()
        {
            Color[] gradColors = new Color[] {
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Green
            };

            var patts = Enum.GetValues(typeof(PatternFillType));

            var doc = new GcWordDocument();
            var shapes = AddGeometryTypes(doc, new SizeF(100, 120), patts.Length + 12, true, true);

            foreach (var s in shapes)
            {
                s.Size.Width.Value *= 2;
                s.Size.Height.Value *= 2;
            }

            doc.Body.Paragraphs.Insert("Fill formats", doc.Styles[BuiltInStyleId.Title], InsertLocation.Start);

            int sIdx = 0;

            var shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomLeftToTopRight);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
            shape.AddTextFrame("Linear grad");

            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.ApplyLinearDirection(GradientLinearDirection.BottomRightToTopLeft);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Position = 30;
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
            shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
            shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
            shape.AddTextFrame("Linear grad");

            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.Type = GradientType.Circle; // not really needed if using ApplyPathDirection() method
            shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.FromBottomRight);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Position = 30;
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
            shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
            shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
            shape.AddTextFrame("Circle grad");

            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.ApplyCircleDirection(GradientPathDirection.Center);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
            shape.AddTextFrame("Circle grad");

            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.Center);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Position = 30;
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[1];
            shape.Fill.GradientFill.Stops.Add(gradColors[2], 60);
            shape.Fill.GradientFill.Stops.Add(gradColors[3], 90);
            shape.AddTextFrame("Rectangle grad");

            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Gradient;
            shape.Fill.GradientFill.ApplyRectangleDirection(GradientPathDirection.FromTopRight);
            shape.Fill.GradientFill.Stops[0].Color.RGB = gradColors[0];
            shape.Fill.GradientFill.Stops[1].Color.RGB = gradColors[3];
            shape.AddTextFrame("Rectangle grad");

            // Image fills:
            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Image;
            var bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "butterfly.jpg"));
            shape.Fill.ImageFill.SetImage(bytes, "image/jpeg");
            shape = shapes[sIdx++];
            shape.Fill.Type = FillType.Image;
            shape.Fill.ImageFill.FillType = ImageFillType.Tile;
            //use tile image as flipped both vertical and horizontal
            shape.Fill.ImageFill.Tile.Flip = TileFlipMode.HorizontalAndVertical;
            //scale image down horizontally to 30% and vertically to 25%
            shape.Fill.ImageFill.Tile.HorizontalScale = 30;
            shape.Fill.ImageFill.Tile.VerticalScale = 25;
            bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "gcd-hex-logo-80x80.png"));
            shape.Fill.ImageFill.SetImage(bytes, "image/png");

            var r = shape.GetRange().Runs.Insert("\n\nPattern fills:\n", doc.Styles[BuiltInStyleId.Strong], InsertLocation.After);
            r.Font.Size *= 3;

            // Pattern fills:
            foreach (var p in patts)
            {
                shape = shapes[sIdx++];
                shape.Size.Width.Value /= 3;
                shape.Size.Height.Value /= 3;
                shape.Fill.Type = FillType.Pattern;
                //type represents how it will look
                shape.Fill.PatternFill.Type = (PatternFillType)p;
                shape.Fill.PatternFill.ForeColor.RGB = gradColors[0];
                shape.Fill.PatternFill.BackColor.RGB = gradColors[gradColors.Length - 1];
            }

            if (sIdx < shapes.Count)
                shapes.Skip(sIdx).ToList().ForEach(s_ => s_.Delete());

            return doc;
        }

        /// <summary>
        /// Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
        /// The fill and line colors of the shapes are varied.
        /// </summary>
        /// <param name="doc">The target document.</param>
        /// <param name="size">The size of shapes to create.</param>
        /// <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
        /// <param name="skipUnfillable">Add only shapes that support fills.</param>
        /// <param name="noNames">Do not add geometry names as shape text frames.</param>
        /// <returns>The list of shapes added to the document.</returns>
        private static List<Shape> AddGeometryTypes(GcWordDocument doc, SizeF size, int count = -1, bool skipUnfillable = false, bool noNames = false)
        {
            // Line and fill colors:
            Color[] lines = new Color[] { Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue, };
            int line = 0;
            Color[] fills = new Color[] { Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed, };
            int fill = 0;

            // The supported geometry types:
            var geoms = Enum.GetValues(typeof(GeometryType));

            // Add a paragraph and a run where the shapes will live:
            doc.Body.Paragraphs.Add("");

            var shapes = new List<Shape>();
            foreach (GeometryType g in geoms)
            {
                // Line geometries do not support fills:
                if (skipUnfillable && g.IsLineGeometry())
                    continue;

                if (count-- == 0)
                    break;

                float w = size.Width, h = size.Height;
                var shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g);
                if (!g.IsLineGeometry())
                {
                    shape.Fill.Type = FillType.Solid;
                    shape.Fill.SolidFill.RGB = fills[fill < fills.Length - 1 ? ++fill : (fill = 0)];
                }
                shape.Line.Width = 3;
                shape.Line.Fill.SolidFill.RGB = lines[line < lines.Length - 1 ? ++line : (line = 0)];
                if (!noNames && g.TextFrameSupported())
                    shape.AddTextFrame(g.ToString());
                shape.AlternativeText = $"This is shape {g}";
                shape.Size.EffectExtent.AllEdges = 8;
                shapes.Add(shape);
            }
            return shapes;
        }
    }
}