OutlinedText.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 GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // Creating outlined text
    public class OutlinedText
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using (var g = bmp.CreateGraphics(Color.White))
            {
                var tl = g.CreateTextLayout();

                // Set up text layout:
                tl.MaxWidth = g.Width;
                tl.MaxHeight = g.Height;
                tl.MarginAll = g.Resolution / 2;
                const float fontSize = 64;

                var rcBack = new RectangleF(0, 0, pixelSize.Width, pixelSize.Height);
                g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")), rcBack, null, ImageAlign.StretchImage);

                TextFormat tf0 = new TextFormat()
                {
                    ForeColor = Color.LemonChiffon,
                    Hollow = true,
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "GOTHICB.TTF")),
                    FontSize = fontSize,
                };
                tl.AppendLine("Hollow Text", tf0);

                TextFormat tf1 = new TextFormat()
                {
                    StrokePen = Color.DarkMagenta,
                    FillBrush = new GCDRAW.SolidBrush(Color.Yellow),
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf")),
                    FontSize = fontSize,
                };
                tl.AppendLine("Outlined Text", tf1);

                var grad0 = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Blue, new PointF(1, 0));
                TextFormat tf2 = new TextFormat()
                {
                    // StrokePen = Color.DarkMagenta,
                    FillBrush = grad0,
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
                    FontSize = fontSize,
                };
                tl.AppendLine("Gradient Fill", tf2);

                var grad1 = new LinearGradientBrush(Color.Red, Color.Purple);
                grad1.GradientStops = new List<GradientStop>()
                {
                    new GradientStop(Color.Orange, 1 / 6f),
                    new GradientStop(Color.Yellow, 2 / 6f),
                    new GradientStop(Color.Green, 3 / 6f),
                    new GradientStop(Color.Cyan, 4 / 6f),
                    new GradientStop(Color.Blue, 5 / 6f)
                };
                TextFormat tf3 = new TextFormat()
                {
                    // StrokePen = Color.DarkMagenta,
                    FillBrush = grad1,
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
                    FontSize = fontSize,
                };
                tl.AppendLine("Multi-stop gradient", tf3);

                var tf4 = new TextFormat(tf3);
                tf4.StrokePen = Color.GreenYellow;
                tl.AppendLine("Multi-stop gradient with outline", tf4);

                var tf5 = new TextFormat(tf4);
                tf5.Hollow = true;
                tf5.StrokePen = new GCDRAW.Pen(Color.DarkRed, g.Resolution / 72);
                tl.AppendLine("Text outlined with 1pt wide pen", tf5);

                // It is not necessary to call PerformLayout() for a newly created TextLayout
                // or after a call to TextLayout.Clear():
                g.DrawTextLayout(tl, PointF.Empty);
            }
            // Done:
            return bmp;
        }
    }
}