EUDC.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports System.Collections.Generic
Imports System.Linq
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 演示如何使用自定义 EUDC 字体 (.tte) 渲染专用 Unicode 字符 (PUA)。
Public Class EUDC
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' 使用 EUDC 代码和两个常规字符(& 和 !)测试字符串:0xE620 0xE621 0xE622 0xE624 amp; 0xE623!
        Const tstr = "&!"
        '' 设置:
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics
        Dim tf = New TextFormat() With {.FontSize = 20}
        Dim rc = Util.AddNote(
                "此示例演示如何使用自定义 EUDC 字体(.tte) 呈现专用 Unicode 字符(PUA)。" + vbLf +
                "",
                page)
        Const dy = 36.0F
        Dim ip = New PointF(rc.X, rc.Bottom + dy / 2)

        '' 使用 FontCollection 允许按家族名称获取字体:
        Dim fc = New FontCollection()

        '' 将字体集合分配给图形,以便MeasureString/DrawString
        '' 图形上的方法可以找到后备字体:
        g.FontCollection = fc

        '' 使用 FontCollection 注册一些常规字体:
        fc.RegisterFont(Path.Combine("Resources", "Fonts", "arial.ttf"))
        fc.RegisterFont(Path.Combine("Resources", "Fonts", "times.ttf"))
        fc.RegisterFont(Path.Combine("Resources", "Fonts", "yumin.ttf"))
        fc.RegisterFont(Path.Combine("Resources", "Fonts", "msgothic.ttc"))
        fc.RegisterFont(Path.Combine("Resources", "Fonts", "YuGothR.ttc"))

        '' 告诉字体集合使用 Yu Mincho 作为后备:
        fc.AppendFallbackFonts(fc.FindFamilyName("Yu Mincho"))

        '' 使用 Arial 字体会将测试字符串呈现为空矩形,因为 Arial 中不存在合适的字形:
        tf.Font = fc.FindFamilyName("Arial", False, False)
        g.DrawString($"Arial: {tstr} (no EUDC font has been linked yet)", tf, ip)
        ip.Y += dy

        '' 加载两种客户 EUDC 字体:
        Dim eudcF0 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte"))
        Dim eudcF1 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"))

        '' 将一种 EUDC 字体链接到 Arial - 现在在使用 Arial 渲染的字符串中,EUDC 字符将以此字体查找:
        Dim fnt = fc.FindFamilyName("Arial")
        fnt.AddEudcFont(eudcF0)
        '' Yu Mincho 字体也是如此:
        fnt = fc.FindFamilyName("Yu Mincho")
        fnt.AddEudcFont(eudcF0)
        '' 将另一个 EUDC 字体链接到 Yu Gothic:
        fnt = fc.FindFamilyName("Yu Gothic")
        fnt.AddEudcFont(eudcF1)

        '' 使用我们的自定义 EUDC 字体链接到的字体,用 EUDC 字符渲染字符串:
        tf.Font = fc.FindFamilyName("Arial", False, False)
        g.DrawString($"Arial, linked with Eudc0.tte: {tstr}", tf, ip)
        ip.Y += dy
        tf.Font = fc.FindFileName("times.ttf")
        g.DrawString($"Times, fallback via Yu Mincho: {tstr}", tf, ip)
        ip.Y += dy
        tf.Font = fc.FindFamilyName("MS Gothic")
        g.DrawString($"MS Gothic, fallback via Yu Mincho: {tstr}", tf, ip)
        ip.Y += dy
        tf.Font = fc.FindFamilyName("Yu Gothic")
        g.DrawString($"Yu Gothic, linked with Eudc1.tte: {tstr}", tf, ip)
        ip.Y += dy

        '' FontCollection 添加了一些服务(例如按姓氏查找字体),
        '' 但 EUDC 字体可以链接到不在集合中的字体:
        fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"))
        fnt.AddEudcFont(eudcF0)
        tf.Font = fnt
        g.DrawString($"Gabriola Font, linked with Eudc0.tte: {tstr}", tf, ip)
        ip.Y += dy
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class