Antialiasing.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
{
    // This sample shows the different (anti)aliasing modes of rendering text:
    // - No anti-aliasing, very fast but poor quality (not recommended).
    // - Fast anti-aliasing. This is the default that provides good quality and fast rendering.
    // - Slow anti-aliasing. Highest quality but slower than the default.
    public class Antialiasing
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
            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 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "timesbd.ttf")),
                    FontSize = 16,
                };

                var tl = g.CreateTextLayout();
                tl.DefaultFormat.Font = GCTEXT.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 anti-aliasing (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 anti-aliasing (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 anti-aliasing (highest quality)", tfcap);
                    tl.Append(text);
                    tl.PerformLayout(true);
                    g.DrawTextLayout(tl, tloc);
                }
            }
            return bmp;
        }
    }
}