EUDC.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.IO;using System.Drawing;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Text;using GCTEXT = GrapeCity.Documents.Text;using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos.Basics{ // Shows how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte). public class EUDC { public int CreatePDF(Stream stream) { // Test string using EUDC codes and two regular chars (& and !): 0xE620 0xE621 0xE622 0xE624 & 0xE623 ! const string tstr = "&!"; // Set up: var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; var tf = new TextFormat() { FontSize = 20 }; var rc = Common.Util.AddNote( "This sample demonstrates how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte).\n" + "A GrapeCity.Documents.Text.Font can be created from an EUDC .tte file, " + "and linked to one or more fonts using Font.AddEudcFont() method.", page); const float dy = 36; var ip = new PointF(rc.X, rc.Bottom + dy / 2);
// Use FontCollection to allow fetching fonts by family names: var fc = new FontCollection();
// Assign the font collection to the graphics so that MeasureString/DrawString // methods on the graphics can find fallback fonts: g.FontCollection = fc;
// Register some regular fonts with the FontCollection: fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf")); fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")); fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerifSC-Regular.ttf")); fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Light.ttf")); fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Condensed.ttf"));
// Tell the font collection to use Noto Serif as a fallback: fc.AppendFallbackFonts(fc.FindFamilyName("Noto Serif"));
// Using Noto Sans font renders the test string as empty rectangles, as suitable glyphs are not present in Noto Sans: tf.Font = fc.FindFamilyName("Noto Sans", false, false); g.DrawString($"Noto Sans: {tstr} (no EUDC font has been linked yet)", tf, ip); ip.Y += dy;
// Load two custom EUDC fonts: var eudcF0 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte")); var eudcF1 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"));
// Link one EUDC font to Noto Sans - now in strings rendered with Noto Sans, EUDC chars will be looked up in this font: var font = fc.FindFamilyName("Noto Sans"); font.AddEudcFont(eudcF0); // Ditto for Noto Serif font: font = fc.FindFamilyName("Noto Serif"); font.AddEudcFont(eudcF0); // Link another EUDC font to Noto Sans Light: font = fc.FindFamilyName("Noto Sans Light"); font.AddEudcFont(eudcF1);
// Render strings with EUDC chars using fonts to which our custom EUDC font is linked: tf.Font = fc.FindFamilyName("Noto Sans", false, false); g.DrawString($"Noto Sans, linked with Eudc0.tte: {tstr}", tf, ip); ip.Y += dy; tf.Font = fc.FindFileName("NotoSerif-Regular.ttf"); g.DrawString($"Noto Sans, fallback via Noto Serif: {tstr}", tf, ip); ip.Y += dy; tf.Font = fc.FindFamilyName("Noto Sans Condensed"); g.DrawString($"Noto Sans Condensed, fallback via Noto Serif: {tstr}", tf, ip); ip.Y += dy; tf.Font = fc.FindFamilyName("Noto Sans Light"); g.DrawString($"Noto Sans Light, linked with Eudc1.tte: {tstr}", tf, ip); ip.Y += dy;
// FontCollection adds some services (like font lookup by family name), // but EUDC fonts can be linked to fonts that are not in a collection: font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "VLADIMIR.TTF")); font.AddEudcFont(eudcF0); tf.Font = font; g.DrawString($"VLADIMIR Font, linked with Eudc0.tte: {tstr}", tf, ip); ip.Y += dy; // Done: doc.Save(stream); return doc.Pages.Count; } }}