GcLibsComparison.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Imaging.Skia
Imports GrapeCity.Documents.Imaging.Windows
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 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 drawn 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 Function GenerateImage(
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Const text As String = "Skia is an open source 2D graphics library which provides common APIs that work across a variety of hardware and software platforms."
        Dim q As Integer = 4
        Dim width As Integer = pixelSize.Width \ q
        Dim height As Integer = pixelSize.Height \ (q * 3)
        Dim margin As Integer = 12
        Dim tf As New TextFormat() With {
            .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")),
            .FontSize = 10
        }
        Dim msg As String = $"Text with font size {tf.FontSize} rendered using {{0}} and enlarged x{q}:"

        Using gcBmp As New GcBitmap(width, height, False)
            Using g = gcBmp.CreateGraphics(Color.Transparent)
                Dim tl = g.CreateTextLayout()
                tl.MaxWidth = width
                tl.MarginAll = margin
                tl.Append(text, tf)
                g.DrawTextLayout(tl, PointF.Empty)
            End Using

            Using gcSkiaBmp As New GcSkiaBitmap(width, height, False)
                Using g = gcSkiaBmp.CreateGraphics(Color.Transparent)
                    Dim tl = g.CreateTextLayout()
                    tl.MaxWidth = width
                    tl.MarginAll = margin
                    tl.Append(text, tf)
                    g.DrawTextLayout(tl, PointF.Empty)
                End Using

                Dim gcWicBmp As GcWicBitmap = Nothing
                If GcWicBitmap.IsSupported Then
                    gcWicBmp = New GcWicBitmap(width, height, False)
                    Using g = gcWicBmp.CreateGraphics(Color.Transparent)
                        Dim tl = g.CreateTextLayout()
                        tl.MaxWidth = width
                        tl.MarginAll = margin
                        tl.Append(text, tf)
                        g.DrawTextLayout(tl, PointF.Empty)
                    End Using
                End If

                Dim bmp As New GcBitmap(pixelSize.Width, pixelSize.Height, opaque)
                Using g = bmp.CreateGraphics(Color.White)
                    g.Renderer.InterpolationMode = InterpolationMode.NearestNeighbor
                    Dim fs As Integer = 16
                    Dim dy As Integer = pixelSize.Height \ 3
                    tf.FontSize = fs

                    g.DrawString(String.Format(msg, "GcBitmapGraphics (cross-platform)"), tf, New PointF(margin / 3.0F, margin / 3.0F))
                    Dim rc As New RectangleF(0, fs * 2, width * q, height * q - fs * 2)
                    rc.Inflate(-margin, -margin)
                    g.DrawImage(gcBmp, rc, Nothing, ImageAlign.StretchImage)
                    g.DrawRoundRect(rc, margin, Color.MediumVioletRed)

                    g.DrawString(String.Format(msg, "GcSkiaGraphics (cross-platform)"), tf, New PointF(margin / 3.0F, dy + margin / 3.0F))
                    rc = New RectangleF(0, fs * 2 + dy, width * q, height * q - fs * 2)
                    rc.Inflate(-margin, -margin)
                    g.DrawImage(gcSkiaBmp, rc, Nothing, ImageAlign.StretchImage)
                    g.DrawRoundRect(rc, margin, Color.MediumVioletRed)

                    g.DrawString(String.Format(msg, "GcWicBitmapGraphics (Windows-only)"), tf, New PointF(margin / 3.0F, dy * 2 + margin / 3.0F))
                    rc = New RectangleF(0, fs * 2 + dy * 2, width * q, height * q - fs * 2)
                    rc.Inflate(-margin, -margin)
                    If gcWicBmp IsNot Nothing Then
                        g.DrawImage(gcWicBmp, rc, Nothing, ImageAlign.StretchImage)
                    Else
                        Dim 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)
                    End If
                    g.DrawRoundRect(rc, margin, Color.MediumVioletRed)
                End Using

                If gcWicBmp IsNot Nothing Then
                    gcWicBmp.Dispose()
                End If

                Return bmp
            End Using
        End Using
    End Function
End Class