Antialiasing.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
{
    // This sample shows the different (anti)aliasing modes of rendering text:
    // - No antialiasing, very fast but poor quality (not recommended).
    // - Fast antialiasing. This is the default that provides good quality and fast rendering.
    // - Slow antialiasing. Highest quality but slower than the default.
    public class Antialiasing
    {
        public GcBitmap GenerateImage (int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY);
            using (var g = bmp.CreateGraphics(Color.FromArgb(unchecked((int)0xffccf2ff))))
            {
                g.Renderer.Multithreaded = true;

                var text = Common.Util.LoremIpsum();
                var tsize = new SizeF(bmp.Width / 2, bmp.Height / 2);
                var pad = 96f / 4;

                var tfcap = new TextFormat()
                {
                    Font = Font.FromFile(Path.Combine("Resources", "Fonts", "timesbd.ttf")),
                    FontSize = 16,
                };

                var tl = g.CreateTextLayout();
                tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
                tl.DefaultFormat.FontSize = 14;
                tl.TextAlignment = TextAlignment.Justified;
                tl.FirstLineIndent = 96 / 2;
                tl.ParagraphSpacing = 96 / 8;
                tl.MaxWidth = tsize.Width;
                tl.MaxHeight = tsize.Height;
                tl.MarginAll = pad;

                var tloc = PointF.Empty;
                using (g.PushClip(new RectangleF(tloc, tsize)))
                {
                    bmp.Renderer.Aliased = true;
                    tl.AppendLine("No antialiasing (worst quality)", tfcap);
                    tl.Append(text);
                    tl.PerformLayout(true);
                    g.DrawTextLayout(tl, tloc);
                }
                tloc.X += tsize.Width;
                using (g.PushClip(new RectangleF(tloc, tsize)))
                {
                    bmp.Renderer.Aliased = false;
                    bmp.Renderer.SlowAntialiasing = false;
                    tl.Clear();
                    tl.AppendLine("Fast antialiasing (default quality)", tfcap);
                    tl.Append(text);
                    tl.PerformLayout(true);
                    g.DrawTextLayout(tl, tloc);
                }
                tloc.X = 0;
                tloc.Y += tsize.Height;
                using (g.PushClip(new RectangleF(tloc, tsize)))
                {
                    bmp.Renderer.Aliased = false;
                    bmp.Renderer.SlowAntialiasing = true;
                    tl.Clear();
                    tl.AppendLine("Slow antialiasing (highest quality)", tfcap);
                    tl.Append(text);
                    tl.PerformLayout(true);
                    g.DrawTextLayout(tl, tloc);
                }
            }
            return bmp;
        }
    }
}