SlantedTableHeaders.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This demo shows how to draw a table with slanted column headers,
'' so that long headers can be squeezed into narrow columns' headers.
'' The demo uses the GcGraphics.DrawSlantedText() method to draw the headers.
'' See also the DrawRotatedText_0 and DrawSlantedText_0 demos.
Public Class SlantedTableHeaders
    Public Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)

            '' This is just to better position the table in the resulting image:
            g.Transform = Matrix3x2.CreateScale(2) * Matrix3x2.CreateTranslation(g.Resolution, g.Resolution)

            Dim x = New Single() {10, 70, 120, 170, 220, 270, 320, 370}
            Dim y = New Single() {10, 70, 90, 110, 130}
            Dim lastX = x.Length - 1
            Dim lastY = y.Length - 1

            Dim angle = -50
            Dim dx = CSng((y(0) - y(1)) / Math.Tan(Math.PI * angle / 180))

            Using path = g.CreatePath()
                path.BeginFigure(New PointF(x(1) + dx, y(0)))
                path.AddLine(New PointF(x(lastX) + dx, y(0)))
                path.AddLine(New PointF(x(lastX), y(1)))
                path.AddLine(New PointF(x(1), y(1)))
                path.EndFigure(FigureEnd.Closed)
                g.FillPath(path, Color.Bisque)
            End Using
            DrawLine(g, x(1) + dx, y(0), x(lastX) + dx, y(0))
            For i = 1 To lastX
                DrawLine(g, x(i), y(1), x(i) + dx, y(0))
            Next

            Dim arrH = New String() {"January", "February", "March", "April", "May", "June"}
            For i = 1 To lastX - 1
                DrawSlantedText(g, angle, x(i), y(0), x(i + 1), y(1), arrH(i - 1))
            Next

            For i = 1 To lastY
                DrawLine(g, x(0), y(i), x(lastX), y(i))
            Next
            For i = 0 To lastX
                DrawLine(g, x(i), y(1), x(i), y(lastY))
            Next

            Dim arr1 = New String() {"Store 1", "83424", "13558", "2348", "6429", "7565", "2833"}
            Dim arr2 = New String() {"Store 2", "53423", "3573", "32321", "16474", "97553", "8364"}
            Dim arr3 = New String() {"Store 3", "3421", "83553", "2343", "6428", "77557", "8347"}

            Dim bold = True
            For i = 0 To lastX - 1
                DrawText(g, y(1), x(i), x(i + 1), arr1(i), bold)
                DrawText(g, y(2), x(i), x(i + 1), arr2(i), bold)
                DrawText(g, y(3), x(i), x(i + 1), arr3(i), bold)
                bold = False
            Next
        End Using
        Return bmp
    End Function

    Private Shared Sub DrawLine(g As GcGraphics, x1 As Single, y1 As Single, x2 As Single, y2 As Single)
        g.DrawLine(New PointF(x1, y1), New PointF(x2, y2), New GCDRAW.Pen(Color.Gray, 1))
    End Sub

    Private Shared Sub DrawSlantedText(g As GcGraphics, angle As Integer, x1 As Single, y1 As Single, x2 As Single, y2 As Single, s As String)
        Dim tl = g.CreateTextLayout()
        tl.TextAlignment = TextAlignment.Center
        tl.Append(s, New TextFormat() With
            {
                .FontName = "Segoe UI",
                .FontSize = 10,
                .FontBold = True
            })
        Dim rc = New RectangleF(x1, y1, x2 - x1, y2 - y1)
        g.DrawSlantedText(tl, angle, False, rc, SlantedTextAlignment.CenterInsideOutside)
    End Sub

    Private Shared Sub DrawText(g As GcGraphics, y1 As Single, x1 As Single, x2 As Single, s As String, Optional bold As Boolean = False)
        Dim tl = g.CreateTextLayout()
        tl.TextAlignment = If(bold, TextAlignment.Leading, TextAlignment.Trailing)
        tl.MaxWidth = x2 - x1 - 6
        tl.Append(s, New TextFormat() With
            {
                .FontName = "Segoe UI",
                .FontSize = 10,
                .FontBold = bold
            })
        g.DrawTextLayout(tl, New PointF(x1 + 3, y1))
    End Sub
End Class