EUDC.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // Shows how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte).
  18. public class EUDC
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. // Test string using EUDC codes and two regular chars (& and !): 0xE620 0xE621 0xE622 0xE624 & 0xE623 !
  23. const string tstr = "&!";
  24. // Set up:
  25. var doc = new GcPdfDocument();
  26. var page = doc.NewPage();
  27. var g = page.Graphics;
  28. var tf = new TextFormat() { FontSize = 20 };
  29. var rc = Common.Util.AddNote(
  30. "This sample demonstrates how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte).\n" +
  31. "A GrapeCity.Documents.Text.Font can be created from an EUDC .tte file, " +
  32. "and linked to one or more fonts using Font.AddEudcFont() method.",
  33. page);
  34. const float dy = 36;
  35. var ip = new PointF(rc.X, rc.Bottom + dy / 2);
  36.  
  37. // Use FontCollection to allow fetching fonts by family names:
  38. var fc = new FontCollection();
  39.  
  40. // Assign the font collection to the graphics so that MeasureString/DrawString
  41. // methods on the graphics can find fallback fonts:
  42. g.FontCollection = fc;
  43.  
  44. // Register some regular fonts with the FontCollection:
  45. fc.RegisterFont(Path.Combine("Resources", "Fonts", "arial.ttf"));
  46. fc.RegisterFont(Path.Combine("Resources", "Fonts", "times.ttf"));
  47. fc.RegisterFont(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  48. fc.RegisterFont(Path.Combine("Resources", "Fonts", "msgothic.ttc"));
  49. fc.RegisterFont(Path.Combine("Resources", "Fonts", "YuGothR.ttc"));
  50.  
  51. // Tell the font collection to use Yu Mincho as a fallback:
  52. fc.AppendFallbackFonts(fc.FindFamilyName("Yu Mincho"));
  53.  
  54. // Using Arial font renders the test string as empty rectangles, as suitable glyphs are not present in Arial:
  55. tf.Font = fc.FindFamilyName("Arial", false, false);
  56. g.DrawString($"Arial: {tstr} (no EUDC font has been linked yet)", tf, ip);
  57. ip.Y += dy;
  58.  
  59. // Load two custome EUDC fonts:
  60. var eudcF0 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte"));
  61. var eudcF1 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"));
  62.  
  63. // Link one EUDC font to Arial - now in strings rendered with Arial, EUDC chars will be looked up in this font:
  64. var font = fc.FindFamilyName("Arial");
  65. font.AddEudcFont(eudcF0);
  66. // Ditto for Yu Mincho font:
  67. font = fc.FindFamilyName("Yu Mincho");
  68. font.AddEudcFont(eudcF0);
  69. // Link another EUDC font to Yu Gothic:
  70. font = fc.FindFamilyName("Yu Gothic");
  71. font.AddEudcFont(eudcF1);
  72.  
  73. // Render strings with EUDC chars using fonts to which our custom EUDC font is linked:
  74. tf.Font = fc.FindFamilyName("Arial", false, false);
  75. g.DrawString($"Arial, linked with Eudc0.tte: {tstr}", tf, ip);
  76. ip.Y += dy;
  77. tf.Font = fc.FindFileName("times.ttf");
  78. g.DrawString($"Times, fallback via Yu Mincho: {tstr}", tf, ip);
  79. ip.Y += dy;
  80. tf.Font = fc.FindFamilyName("MS Gothic");
  81. g.DrawString($"MS Gothic, fallback via Yu Mincho: {tstr}", tf, ip);
  82. ip.Y += dy;
  83. tf.Font = fc.FindFamilyName("Yu Gothic");
  84. g.DrawString($"Yu Gothic, linked with Eudc1.tte: {tstr}", tf, ip);
  85. ip.Y += dy;
  86.  
  87. // FontCollection adds some services (like font lookup by family name),
  88. // but EUDC fonts can be linked to fonts that are not in a collection:
  89. font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"));
  90. font.AddEudcFont(eudcF0);
  91. tf.Font = font;
  92. g.DrawString($"Gabriola Font, linked with Eudc0.tte: {tstr}", tf, ip);
  93. ip.Y += dy;
  94. // Done:
  95. doc.Save(stream);
  96. return doc.Pages.Count;
  97. }
  98. }
  99. }
  100.