GcLibsComparison.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © 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

'' このサンプルは、異なる DsImaging ライブラリを使用して
'' ビットマップ上に描画されたテキストの忠実度の違いを示します。
'' - DsImaging(クロスプラットフォーム、パッケージ: GrapeCity.Documents.Imaging)
'' - DsImaging.Skia(クロスプラットフォーム、パッケージ: GrapeCity.Documents.Imaging.Skia)
'' - DsImaging.Windows(Windows 専用、パッケージ: GrapeCity.Documents.Imaging.Windows)
''
'' 本サンプルでは、非常に小さいフォントでテキストを描画した後、
'' サンプルによって生成された結果画像上で大幅に拡大しているため、
'' 差異が比較的明確に確認できます。
'' 実際の業務アプリケーションにおいては、これらの差異が
'' 問題となるケースは少なく、視認上ほとんど認識されない場合が大半です。
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