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

'' This short sample demonstrates how a Font can be loaded from a file
'' and used in your code to render text.
'' The sample relies on font files NotoSerif-Regular.ttf and
'' NotoSerif-BoldItalic.ttf to be present in the Resources/Fonts folder.
'' 
'' NOTE 1: When Font.FromFile() is used, the actual data is loaded on demand,
'' so that usually a Font instance will not take too much space.
'' The situation is different for fonts created using Font.FromArray()
'' and Font.FromStream() methods - in those cases the whole font is
'' immediately loaded into memory. The font will still be parsed
'' only on demand, but memory consumption is slightly higher,
'' so using Font.FromFile() should generally be preferred.
''
'' NOTE 2: When different Font instances (created using any of the static ctors
'' mentioned above) are used to render text in a PDF, each instance will result
'' in embedding a separate subset of glyphs even if the glyphs are the same,
'' because DsPdf has no way of knowing that two different Font instances 
'' represent the same physical font. So either make sure that only one Font instance
'' is created for each physical font, or better yet use the FontCollection class
'' to add the fonts you need, and specify them via TextFormat.FontName.
Public Class FontFromFile
    Function CreatePDF(ByVal stream As Stream) As Integer

        Const sample = "The quick brown fox jumps over the lazy dog."
        Const fnRegular = "NotoSerif-Regular.ttf"
        Const fnBoldItalic = "NotoSerif-BoldItalic.ttf"


        Dim fRegular = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnRegular))
        If fRegular Is Nothing Then
            Throw New Exception($"Could not load font {fnRegular}")
        End If

        '' Use the loaded font to draw some text:
        Dim tf = New TextFormat() With {.Font = fRegular, .FontSize = 12}
        Dim doc = New GcPdfDocument()
        Dim g = doc.NewPage().Graphics
        g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, New PointF(18, 72))
        '' We can change the font size:
        tf.FontSize += 2
        g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, New PointF(18, 72 * 2))
        '' We can tell DsPdf to emulate bold and/or italic style with a regular font:
        tf.FontStyle = GCTEXT.FontStyle.BoldItalic
        g.DrawString($"Font {fRegular.FontFamilyName}, emulated Bold Italic: {sample}", tf, New PointF(18, 72 * 3))
        '' But of course rather than emulated, it is better to use real bold/italic fonts.
        '' So finally, get a real bold italic font and print a line with it:
        Dim fBoldItalic = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnBoldItalic))
        If fBoldItalic Is Nothing Then
            Throw New Exception($"Could not load font {fnBoldItalic}")
        End If
        tf.Font = fBoldItalic
        tf.FontStyle = GCTEXT.FontStyle.Regular
        g.DrawString($"Font {fBoldItalic.FontFamilyName}, real Bold Italic: {sample}", tf, New PointF(18, 72 * 4))
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class