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

namespace DsImagingWeb.Demos
{
    // This example demonstrates the differences in fidelity between texts
    // rendered on bitmaps using the different DsImaging libraries:
    // - DsImaging (cross-platform, package GrapeCity.Documents.Imaging);
    // - DsImaging.Skia (cross-platform, package GrapeCity.Documents.Imaging.Skia);
    // - DsImaging.Windows (Windows only, package GrapeCity.Documents.Imaging.Windows).
    // Note that the results are rather noticeable here because the text is rendered
    // using a very small font, and then significantly enlarged when dawn on the
    // resulting image generated by the sample. In most real-life applications
    // the differences are very subtle if noticeable at all.
    public class GcLibsComparison
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            const string text = "Skia is an open source 2D graphics library which provides common APIs that work across a variety of hardware and software platforms.";
            int q = 4;
            int width = pixelSize.Width / q;
            int height = pixelSize.Height / (q * 3);
            int margin = 12;
            var tf = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "tahoma.ttf")),
                FontSize = 10
            };
            string msg = $"Text with font size {tf.FontSize} rendered using {{0}} and enlarged x{q}:";

            using var gcBmp = new GcBitmap(width, height, false);
            using (var g = gcBmp.CreateGraphics(Color.Transparent))
            {
                var tl = g.CreateTextLayout();
                tl.MaxWidth = width;
                tl.MarginAll = margin;
                tl.Append(text, tf);
                g.DrawTextLayout(tl, PointF.Empty);
            }

            using var gcSkiaBmp = new GcSkiaBitmap(width, height, false);
            using (var g = gcSkiaBmp.CreateGraphics(Color.Transparent))
            {
                var tl = g.CreateTextLayout();
                tl.MaxWidth = width;
                tl.MarginAll = margin;
                tl.Append(text, tf);
                g.DrawTextLayout(tl, PointF.Empty);
            }

            GcWicBitmap gcWicBmp = null;
            if (GcWicBitmap.IsSupported)
            {
                gcWicBmp = new GcWicBitmap(width, height, false);
                using (var g = gcWicBmp.CreateGraphics(Color.Transparent))
                {
                    var tl = g.CreateTextLayout();
                    tl.MaxWidth = width;
                    tl.MarginAll = margin;
                    tl.Append(text, tf);
                    g.DrawTextLayout(tl, PointF.Empty);
                }
            }

            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque);
            using (var g = bmp.CreateGraphics(Color.White))
            {
                g.Renderer.InterpolationMode = InterpolationMode.NearestNeighbor;
                var fs = 16;
                var dy = pixelSize.Height / 3;
                tf.FontSize = fs;
                g.DrawString(string.Format(msg, "GcBitmapGraphics (cross-platform)"), tf, new PointF(margin / 3, margin / 3));
                var rc = new RectangleF(0, fs * 2, width * q, height * q - fs * 2);
                rc.Inflate(-margin, -margin);
                g.DrawImage(gcBmp, rc, null, ImageAlign.StretchImage);
                g.DrawRoundRect(rc, margin, Color.MediumVioletRed);

                g.DrawString(string.Format(msg, "GcSkiaGraphics (cross-platform)"), tf, new PointF(margin / 3, dy + margin / 3));
                rc = new RectangleF(0, fs * 2 + dy, width * q, height * q - fs * 2);
                rc.Inflate(-margin, -margin);
                g.DrawImage(gcSkiaBmp, rc, null, ImageAlign.StretchImage);
                g.DrawRoundRect(rc, margin, Color.MediumVioletRed);

                g.DrawString(string.Format(msg, "GcWicBitmapGraphics (Windows-only)"), tf, new PointF(margin / 3, dy * 2 + margin / 3));
                rc = new RectangleF(0, fs * 2 + dy * 2, width * q, height * q - fs * 2);
                rc.Inflate(-margin, -margin);
                if (gcWicBmp != null)
                {
                    g.DrawImage(gcWicBmp, rc, null, ImageAlign.StretchImage);
                }
                else
                {
                    var tl = g.CreateTextLayout();
                    tl.MaxWidth = rc.Width;
                    tl.MarginAll = margin;
                    tl.DefaultFormat.Font = tf.Font;
                    tl.DefaultFormat.FontSize = 16;
                    tl.DefaultFormat.ForeColor = Color.OrangeRed;
                    tl.AppendLine(
                        "Looks like this demo is not running on a Windows system, so WIC (GcWicBitmap, GcWicBitmapGraphics) " +
                        "is not supported. To see how text rendered using GcWicBitmapGraphics looks, download this demo project " +
                        "and run it on a Windows system.");
                    g.DrawTextLayout(tl, rc.Location);
                }
                g.DrawRoundRect(rc, margin, Color.MediumVioletRed);
            }
            if (gcWicBmp != null)
                gcWicBmp.Dispose();

            return bmp;
        }
    }
}