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

'' This sample demonstrates how to draw round rectangles
'' using dedicated DrawRoundRect/FillRoundRect methods.
'' It also shows how the same result may be achieved with
'' graphics paths.
Public Class RoundRectangle
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

        Dim rc = Util.AddNote(
            "GcPdfGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
            "This sample also shows how the same result may be achieved using graphics paths. " +
            "While they are not really needed for drawing round rectangles, graphics paths allow " +
            "to draw and fill arbitrary figures with complex geometries.",

        '' Rounded rectangle's radii:
        Dim rx = 36, ry = 24

        '' Using dedicated methods to draw And fill round rectangles:
        Dim rEasy = New RectangleF(rc.Left, rc.Bottom + 36, 144, 72)
        g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen)
        g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4)
        '' Add a label:
        Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
        g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, False)

        '' Using graphics path to achieve the same result:
        Dim rHard = rEasy
        rHard.Offset(0, rEasy.Height + 36)
        Dim path = MakeRoundRect(g, rHard, rx, ry)
        g.FillPath(path, Color.PaleVioletRed)
        g.DrawPath(path, Color.Purple, 4)
        '' Add a label:
        g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, False)

        '' Done
        Return doc.Pages.Count
    End Function

    '' This method shows how to create a graphics path that may be used
    '' to fill Or draw arbitrary shapes on a GcGraphics.
    Private Function MakeRoundRect(ByVal g As GcGraphics, ByVal rc As RectangleF, ByVal rx As Single, ByVal ry As Single) As IPath
        Dim path = g.CreatePath()
        Dim sz = New SizeF(rx, ry)
        '' start from horizontal top left
        path.BeginFigure(New PointF(rc.Left + rx, rc.Top))
        path.AddLine(New PointF(rc.Right - rx, rc.Top))
        path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right, rc.Top + ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
        path.AddLine(New PointF(rc.Right, rc.Bottom - ry))
        path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right - rx, rc.Bottom), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
        path.AddLine(New PointF(rc.Left + rx, rc.Bottom))
        path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left, rc.Bottom - ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
        path.AddLine(New PointF(rc.Left, rc.Top + ry))
        path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left + rx, rc.Top), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
        Return path
    End Function
End Class