OutlinedText.cs
//
// This code is part of GrapeCity Documents for Imaging samples.
// Copyright (c) GrapeCity, 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;

namespace GcImagingWeb.Samples
{
    // Creating outlined text
    public class OutlinedText
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            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, pixelWidth, pixelHeight);
                g.DrawImage(Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")), rcBack, null, ImageAlign.StretchImage);

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

                TextFormat tf1 = new TextFormat()
                {
                    StrokePen = Color.DarkMagenta,
                    FillBrush = new SolidBrush(Color.Yellow),
                    Font = 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 = 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 = 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 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;
        }
    }
}