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", "arialuni.ttf")),
  46. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "l_10646.ttf")),
  47. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "seguiemj.ttf")),
  48. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "seguisym.ttf")),
  49. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "simsun.ttc")),
  50. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  51. GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "YuGothR.ttc"))
  52. );
  53. // Restricting default font lookup is done in the TextLayout, so unlike the
  54. // {Surrogates} sample, here we cannot use DrawString, but must use
  55. // TextLayout and DrawTextLayout directly:
  56. // - specify the font collection to use;
  57. // - restrict default fonts/fallbacks lookup to the specified collection only;
  58. // - set up other props to render the text.
  59. var tl = new TextLayout(72)
  60. {
  61. FontCollection = fc,
  62. RestrictedFontLookup = true,
  63. FontFallbackScope = FontFallbackScope.FontCollectionOnly,
  64. MaxWidth = page.Size.Width,
  65. MaxHeight = page.Size.Height,
  66. MarginLeft = 72,
  67. MarginRight = 72,
  68. MarginTop = 36,
  69. MarginBottom = 36,
  70. TextAlignment = TextAlignment.Center,
  71. };
  72.  
  73. tl.Append("Some Interesting Unicode Characters (system-independent)",
  74. new TextFormat(tf) { Underline = true, FontSize = tf.FontSize + 2 });
  75. tl.PerformLayout(true);
  76. g.DrawTextLayout(tl, PointF.Empty);
  77.  
  78. tl.MarginTop = tl.ContentRectangle.Bottom + 20;
  79. tl.Clear();
  80. tl.TextAlignment = TextAlignment.Leading;
  81.  
  82. // Draw the strings:
  83. tl.Append("Surrogate Pairs:\n", tf);
  84. tl.Append("\uD867\uDEDB \uD840\uDC0B \uD834\uDD1E \uD834\uDD61 \uD83D\uDC04\n", tf1);
  85. tl.Append("\n", tfs);
  86.  
  87. tl.Append("Currency Symbols:\n", tf);
  88. tl.Append("\u0024 \u20A0 \u20A1 \u20A2 \u20A3 \u20A4 \u20AC \u20B9 \x20BD\n", tf1);
  89. tl.Append("\n", tfs);
  90.  
  91. tl.Append("Mathematical Operators:\n", tf);
  92. tl.Append("\u221A \u222B \u2211 \u2210 \u2264 \u2265 \u2202 \u2208\n", tf1);
  93. tl.Append("\n", tfs);
  94.  
  95. tl.Append("CJK Ideographs Extension A:\n", tf);
  96. tl.Append("\u3400 \u3401 \u3402 \u3403 \u3404 \u3405 \u3406 \u3407\n", tf1);
  97. tl.Append("\n", tfs);
  98.  
  99. tl.Append("Letterlike Symbols:\n", tf);
  100. tl.Append("\u2110 \u2111 \u2112 \u2113 \u2114 \u2115 \u211B \u211C\n", tf1);
  101. tl.Append("\n", tfs);
  102.  
  103. tl.Append("Private Use Area:\n", tf);
  104. tl.Append("\uE000 \uE001 \uE010 \uE011 \uE012 \uE013 \uE014 \uE015\n", tf1);
  105. tl.Append("\n", tfs);
  106.  
  107. tl.Append("Arrows:\n", tf);
  108. tl.Append("\u2190 \u2191 \u2192 \u2193 \u21B0 \u21E6 \u21CB \u21A9\n", tf1);
  109. tl.Append("\n", tfs);
  110.  
  111. tl.Append("Dingbats:\n", tf);
  112. tl.Append("\u2714 \u2717 \u275B \u275C \u2706 \u2707 \u2708 \u2709\n", tf1);
  113. tl.Append("\n", tfs);
  114.  
  115. tl.Append("Braille Patterns:\n", tf);
  116. tl.Append("\u2830 \u2831 \u2832 \u2833 \u2834 \u2835 \u2836 \u2837\n", tf1);
  117. tl.Append("\n", tfs);
  118.  
  119. tl.Append("Geometric Shapes:\n", tf);
  120. tl.Append("\u25D0 \u25D1 \u25D2 \u25D3 \u25A4 \u25F0 \u25BC \u25CE\n", tf1);
  121. tl.Append("\n", tfs);
  122.  
  123. tl.Append("Latin Extended A:\n", tf);
  124. tl.Append("\u0100 \u0101 \u0102 \u0103 \u0104 \u0105 \u0106 \u0107\n", tf1);
  125. tl.Append("\n", tfs);
  126.  
  127. tl.Append("Miscellaneous Symbols:\n", tf);
  128. tl.Append("\u2600 \u2601 \u2602 \u2603 \u2604 \u2605 \u2606 \u2607 \u2608 \u2609 \u2614 \u2615 \u26F0\n", tf1);
  129. tl.Append("\n", tfs);
  130.  
  131. tl.PerformLayout(true);
  132. g.DrawTextLayout(tl, PointF.Empty);
  133.  
  134. // Done:
  135. doc.Save(stream);
  136. return doc.Pages.Count;
  137. }
  138. }
  139. }
  140.