FontCollection.vb
''
'' 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

'' This sample shows how to create, initialize and use the FontCollection class,
'' which is the recommended way to manage fonts and use them when rendering texts
'' in GrapeCity Documents for PDF.
'' 
'' The main points to keep in mind, and the recommended steps to follow,
'' when using FontCollection with GcPdf:
''
'' 1. Create an instance of the FontCollection class.
''    FontCollection is not a static class, you need an instance of it to use.
''    Also, it is a regular .NET collection of Font objects, so all usual 
''    collection manipulation methods (Add, Insert, Remove etc) can be used on it.
''
'' 2. Populate the font collection with fonts using any of the follwing methods:
''    - RegisterDirectory(): registers all fonts found in a specified directory;
''    - RegisterFont(): registers font(s) found in a specified fiile;
''    - Add(Font): adds a font instance that you created.
''    Note that registering directories or fonts with a font collection is a fast 
''    and light-weight operation. The font collection does not actually load all font data
''    when directories or individual fonts are registered with it. Instead, it loads only
''    the minimal info so that it can find and provide fonts quickly and efficiently
''    when needed.
''
'' 3. Assign your instance of the font collection to TextLayout.FontCollection (and to 
''    GcGraphics.FontCollection if using GcGraphics.MeasureString/DrawString) so that
''    the correct fonts can be found.
''
'' 4. In your text rendering code, select fonts by specifying font names (TextFormat.FontName,
''    the names must match exactly but the case is not important), and font bold and italic
''    flags (TextFormat.FontBold/FontItalic). If a suitable bold/italic version of the requested
''    font is found in the collection, it will be used; otherwise font emulation will be applied.
''
'' 5. FontCollection methods and properties are thread-safe, so once your font collection
''    has been populated, you can cache and share it between sessions and/or modules
''    of your application. You do need to exercise caution when modifying and accessing
''    the font collection simultaneously from different threads though, as it may change
''    between a check of some condition on the collection, and action on that check.
''    For such cases the FontCollection.SyncRoot property is provided, and should be used.
''
'' The code in this sample illustrates most of the points above. 
Public Class FontCollectionTest
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Create a FontCollection instance:
        Dim fc = New FontCollection()
        '' Populate it with fonts from the specified directory:
        fc.RegisterDirectory(Path.Combine("Resources", "Fonts"))

        '' Generate a sample document using the font collection to provide fonts:
        Dim doc = New GcPdfDocument()
        Dim page = doc.Pages.Add()
        Dim g = page.Graphics

        '' For TextLayout/TextFormat to be able to use a font collection, it must be
        '' associated with it like so:
        Dim tl = New TextLayout() With {.FontCollection = fc}

        '' Render some strings using the different fonts from our collection:
        Dim tf = New TextFormat() With {.FontName = "times new roman", .FontSize = 16}
        tl.Append("Using FontCollection to manage fonts and render text" + vbCrLf + vbCrLf, tf)
        tf.FontSize = 12
        tl.Append("Text rendered using Times New Roman regular font." + vbCrLf, tf)
        '' Setting a font style (bold or italic) will tell the font collection
        '' to search for a suitable font (if it is not found, emulation will be used):
        tf.FontItalic = True
        tl.Append("Text rendered using Times New Roman italic font." + vbCrLf, tf)
        '' Text format is applied to a text run when the text is appended,
        '' so we can re-use the same format, modifying its properties
        '' to render differently formatted texts:
        tf.FontBold = True
        tl.Append("Text rendered using Times New Roman bold italic font." + vbCrLf, tf)
        tf.FontItalic = False
        tl.Append("Text rendered using Times New Roman bold font." + vbCrLf, tf)
        tf.FontName = "segoe ui"
        tl.Append("Text rendered using Segoe UI bold font." + vbCrLf, tf)
        tf.FontBold = False
        tl.Append("Text rendered using Segoe UI regular font." + vbCrLf, tf)

        '' Apply page settings to the page layout and render the page:
        tl.MaxWidth = page.Size.Width
        tl.MaxHeight = page.Size.Height
        tl.MarginAll = 72
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)

        '' If using GcGraphics.DrawString/MeasureString, this will allow the TextLayout
        '' created internally by GcGraphics to find the specified fonts in the font collection:
        g.FontCollection = fc

        '' Use GcGraphics.DrawString to show that the font collection is also used
        '' by the graphics once the FontCollection has been set on it:
        g.DrawString("Text rendered using Segoe UI bold, drawn by GcGraphics.DrawString() method.",
            New TextFormat() With {.FontName = "segoe ui", .FontBold = True, .FontSize = 10},
            New PointF(72, tl.ContentRectangle.Bottom + 12))
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class