SurrogatesPort.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // This sample renders a variety of interesting Unicode characters
  16. // including surrogate pairs, similar to the Surrogates sample.
  17. // Unlike that sample though, it does not rely on any system-provided
  18. // fallbacks. Instead, in this sample we purposefully restrict fallback
  19. // font lookup to the program's own font collection, and provide
  20. // our own set of fallback fonts.
  21. // This makes the code platform and system independent, so it produces
  22. // exactly the same result on Windows, Linux or Mac.
  23. public class SurrogatesPort
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. var doc = new GcPdfDocument();
  28. var page = doc.NewPage();
  29. var g = page.Graphics;
  30. // As in the Surrogates sample, we specify a standard font
  31. // (which lacks many of the glyphs we will be rendering),
  32. // and will rely on font fallback support provided by FontCollection:
  33. var font = StandardFonts.Helvetica;
  34.  
  35. // Set up text formats for captions, "interesting chars" and spacing:
  36. var tf = new TextFormat() { Font = font, FontSize = 12 };
  37. var tf1 = new TextFormat(tf) { FontSize = 14 };
  38. var tfs = new TextFormat(tf) { FontSize = 6 };
  39.  
  40. // Create a font collection to use:
  41. var fc = new FontCollection();
  42. // Add our own fallback fonts to use (note that order is important here,
  43. // first come first found):
  44. fc.AppendFallbackFonts(
  45. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")),
  46. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansSC-Regular.ttf")),
  47. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansJP-Regular.ttf")),
  48. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansSymbols-Regular.ttf")),
  49. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansSymbols2-Regular.ttf")),
  50. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoEmoji-Regular.ttf")),
  51. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoMusic-Regular.ttf")),
  52. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Motomangucode-p7Jj1.ttf"))
  53. );
  54. // Restricting default font lookup is done in the TextLayout, so unlike the
  55. // {Surrogates} sample, here we cannot use DrawString, but must use
  56. // TextLayout and DrawTextLayout directly:
  57. // - specify the font collection to use;
  58. // - restrict default fonts/fallbacks lookup to the specified collection only;
  59. // - set up other props to render the text.
  60. var tl = new TextLayout(72)
  61. {
  62. FontCollection = fc,
  63. RestrictedFontLookup = true,
  64. FontFallbackScope = FontFallbackScope.FontCollectionOnly,
  65. MaxWidth = page.Size.Width,
  66. MaxHeight = page.Size.Height,
  67. MarginLeft = 72,
  68. MarginRight = 72,
  69. MarginTop = 36,
  70. MarginBottom = 36,
  71. TextAlignment = TextAlignment.Center,
  72. };
  73.  
  74. tl.Append("Some Interesting Unicode Characters (system-independent)",
  75. new TextFormat(tf) { Underline = true, FontSize = tf.FontSize + 2 });
  76. tl.PerformLayout(true);
  77. g.DrawTextLayout(tl, PointF.Empty);
  78.  
  79. tl.MarginTop = tl.ContentRectangle.Bottom + 20;
  80. tl.Clear();
  81. tl.TextAlignment = TextAlignment.Leading;
  82.  
  83. // Draw the strings:
  84. tl.Append("Surrogate Pairs:\n", tf);
  85. tl.Append("\uD867\uDEDB \uD840\uDC0B \uD834\uDD1E \uD834\uDD61 \uD83D\uDC04\n", tf1);
  86. tl.Append("\n", tfs);
  87.  
  88. tl.Append("Currency Symbols:\n", tf);
  89. tl.Append("\u0024 \u20A0 \u20A1 \u20A2 \u20A3 \u20A4 \u20AC \u20B9 \x20BD\n", tf1);
  90. tl.Append("\n", tfs);
  91.  
  92. tl.Append("Mathematical Operators:\n", tf);
  93. tl.Append("\u221A \u222B \u2211 \u2210 \u2264 \u2265 \u2202 \u2208\n", tf1);
  94. tl.Append("\n", tfs);
  95.  
  96. tl.Append("CJK Ideographs Extension A:\n", tf);
  97. tl.Append("\u3400 \u3401 \u3402 \u3403 \u3404 \u3405 \u3406 \u3407\n", tf1);
  98. tl.Append("\n", tfs);
  99.  
  100. tl.Append("Letterlike Symbols:\n", tf);
  101. tl.Append("\u2110 \u2111 \u2112 \u2113 \u2114 \u2115 \u211B \u211C\n", tf1);
  102. tl.Append("\n", tfs);
  103.  
  104. tl.Append("Private Use Area:\n", tf);
  105. tl.Append("\uE000 \uE001 \uE010 \uE011 \uE012 \uE013 \uE014 \uE015\n", tf1);
  106. tl.Append("\n", tfs);
  107.  
  108. tl.Append("Arrows:\n", tf);
  109. tl.Append("\u2190 \u2191 \u2192 \u2193 \u21B0 \u21E6 \u21CB \u21A9\n", tf1);
  110. tl.Append("\n", tfs);
  111.  
  112. tl.Append("Dingbats:\n", tf);
  113. tl.Append("\u2714 \u2717 \u275B \u275C \u2706 \u2707 \u2708 \u2709\n", tf1);
  114. tl.Append("\n", tfs);
  115.  
  116. tl.Append("Braille Patterns:\n", tf);
  117. tl.Append("\u2830 \u2831 \u2832 \u2833 \u2834 \u2835 \u2836 \u2837\n", tf1);
  118. tl.Append("\n", tfs);
  119.  
  120. tl.Append("Geometric Shapes:\n", tf);
  121. tl.Append("\u25D0 \u25D1 \u25D2 \u25D3 \u25A4 \u25F0 \u25BC \u25CE\n", tf1);
  122. tl.Append("\n", tfs);
  123.  
  124. tl.Append("Latin Extended A:\n", tf);
  125. tl.Append("\u0100 \u0101 \u0102 \u0103 \u0104 \u0105 \u0106 \u0107\n", tf1);
  126. tl.Append("\n", tfs);
  127.  
  128. tl.Append("Miscellaneous Symbols:\n", tf);
  129. tl.Append("\u2600 \u2601 \u2602 \u2603 \u2604 \u2605 \u2606 \u2607 \u2608 \u2609 \u2614 \u2615 \u26F0\n", tf1);
  130. tl.Append("\n", tfs);
  131.  
  132. tl.PerformLayout(true);
  133. g.DrawTextLayout(tl, PointF.Empty);
  134.  
  135. // Done:
  136. doc.Save(stream);
  137. return doc.Pages.Count;
  138. }
  139. }
  140. }
  141.