Antialiasing.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' 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
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = bmp.CreateGraphics(Color.FromArgb(&HFFCCF2FF))
            g.Renderer.Multithreaded = True

            Dim text = Util.LoremIpsum()
            Dim tsize = New SizeF(bmp.Width / 2, bmp.Height / 2)
            Dim pad = 96.0F / 4

            Dim tfcap = New TextFormat() With
                {
                    .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "timesbd.ttf")),
                    .FontSize = 16
                }

            Dim 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

            Dim 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)
            End Using
            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)
            End Using
            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)
            End Using
        End Using
        Return bmp
    End Function
End Class