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

namespace DsImagingWeb.Demos
{
    // This sample demonstrates how to use TrueType font hinting instructions.
    //
    // Many TrueType fonts include low-level hinting instructions.
    // The original purpose of introducing hinting instructions was 
    // to improve the look of glyphs when the font size is comparable
    // to the device resolution. But such instructions are now also used
    // (especially in CJK fonts) to reuse some glyph parts in different
    // glyphs regardless of the font size.
    // GcGraphics supports hinting instructions. To enable it, set
    // TextFormat.EnableFontHinting property to true when rendering text.
    // 
    // This sample renders a Latin text in a small size with hinting off and on,
    // and then renders a CJK text also with hinting off and on.
    public class TrueTypeHinting
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
            var dy = dpi / 2;
            var ip = new PointF(dpi, dpi);
            using (var g = bmp.CreateGraphics(Color.White))
            {
                // Turning anti-aliasing off makes the hinting effect on small text more obvious:
                g.Renderer.Aliased = true;

                var sOff = "Hinting OFF: ";
                var sOn = "Hinting ON: ";

                // Draw a Latin string with hinting instructions off and on:
                var sLat = "The quick brown fox jumps over the lazy dog.";

                var tfLat = new TextFormat()
                {
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arial.ttf")),
                    FontSize = 12,
                    FontName = "Arial",
                    EnableFontHinting = false
                };
                g.DrawString(sOff + sLat, tfLat, ip);
                ip.Y += dy;

                tfLat.FontSize = 10;
                g.DrawString(sOff + sLat, tfLat, ip);
                ip.Y += dy * 2;

                tfLat.EnableFontHinting = true;
                tfLat.FontSize = 12;
                g.DrawString(sOn + sLat, tfLat, ip);
                ip.Y += dy;

                tfLat.FontSize = 10;
                g.DrawString(sOn + sLat, tfLat, ip);
                ip.Y += dy * 2;

                // Draw a CJK string with hinting instructions off and on:
                // "Best year in spring, best day in morning":
                string sCJK = @"一年之计在于春,一日之计在于晨";

                // For CJK text, we can turn anti-aliasing on as the effect of
                // hinting is obvious in the characters' form itself:
                g.Renderer.Aliased = false;

                var tfCJK = new TextFormat()
                {
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "kaiu.ttf")),
                    FontSize = 30,
                    EnableFontHinting = false
                };
                g.DrawString(sOff + "\n" + sCJK, tfCJK, ip);
                ip.Y += dy * 3;

                tfCJK.EnableFontHinting = true;
                g.DrawString(sOn + "\n" + sCJK, tfCJK, ip);
            }
            // Done:
            return bmp;
        }
    }
}