EUDC.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // 演示如何使用自定义 EUDC 字体 (.tte) 渲染专用 Unicode 字符 (PUA)。
    public class EUDC
    {
        public int CreatePDF(Stream stream)
        {
            // 使用 EUDC 代码和两个常规字符(& 和 !)测试字符串:0xE620 0xE621 0xE622 0xE624 amp; 0xE623!
            const string tstr = "&!";
            // 设置:
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
            var tf = new TextFormat() { FontSize = 20 };
            var rc = Common.Util.AddNote(
                "此示例演示如何使用自定义 EUDC 字体(.tte) 呈现专用 Unicode 字符(PUA)。\n" +
                "",
                page);
            const float dy = 36;
            var ip = new PointF(rc.X, rc.Bottom + dy / 2);

            // 使用 FontCollection 允许按家族名称获取字体:
            var 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 字体:
            var eudcF0 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte"));
            var eudcF1 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"));

            // 将一种 EUDC 字体链接到 Arial - 现在在使用 Arial 渲染的字符串中,EUDC 字符将以此字体查找:
            var font = fc.FindFamilyName("Arial");
            font.AddEudcFont(eudcF0);
            // Yu Mincho 字体也是如此:
            font = fc.FindFamilyName("Yu Mincho");
            font.AddEudcFont(eudcF0);
            // 将另一个 EUDC 字体链接到 Yu Gothic:
            font = fc.FindFamilyName("Yu Gothic");
            font.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 字体可以链接到不在集合中的字体:
            font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"));
            font.AddEudcFont(eudcF0);
            tf.Font = font;
            g.DrawString($"Gabriola Font, linked with Eudc0.tte: {tstr}", tf, ip);
            ip.Y += dy;
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}