FontCollection.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.  
  11. namespace DsPdfWeb.Demos
  12. {
  13. // This sample shows how to create, initialize and use the FontCollection class,
  14. // which is the recommended way to manage fonts and use them when rendering texts
  15. // in Document Solutions for PDF.
  16. //
  17. // The main points to keep in mind, and the recommended steps to follow,
  18. // when using FontCollection with DsPdf:
  19. //
  20. // 1. Create an instance of the FontCollection class.
  21. // FontCollection is not a static class, you need an instance of it to use.
  22. // Also, it is a regular .NET collection of Font objects, so all usual
  23. // collection manipulation methods (Add, Insert, Remove etc) can be used on it.
  24. //
  25. // 2. Populate the font collection with fonts using any of the following methods:
  26. // - RegisterDirectory(): registers all fonts found in a specified directory;
  27. // - RegisterFont(): registers font(s) found in a specified file;
  28. // - Add(Font): adds a font instance that you created.
  29. // Note that registering directories or fonts with a font collection is a fast
  30. // and light-weight operation. The font collection does not actually load all font data
  31. // when directories or individual fonts are registered with it. Instead, it loads only
  32. // the minimal info so that it can find and provide fonts quickly and efficiently
  33. // when needed.
  34. //
  35. // 3. Assign your instance of the font collection to TextLayout.FontCollection (and to
  36. // GcGraphics.FontCollection if using GcGraphics.MeasureString/DrawString) so that
  37. // the correct fonts can be found.
  38. //
  39. // 4. In your text rendering code, select fonts by specifying font names (TextFormat.FontName,
  40. // the names must match exactly but the case is not important), and font bold and italic
  41. // flags (TextFormat.FontBold/FontItalic). If a suitable bold/italic version of the requested
  42. // font is found in the collection, it will be used; otherwise font emulation will be applied.
  43. //
  44. // 5. FontCollection methods and properties are thread-safe, so once your font collection
  45. // has been populated, you can cache and share it between sessions and/or modules
  46. // of your application. You do need to exercise caution when modifying and accessing
  47. // the font collection simultaneously from different threads though, as it may change
  48. // between a check of some condition on the collection, and action on that check.
  49. // For such cases the FontCollection.SyncRoot property is provided, and should be used.
  50. //
  51. // The code in this sample illustrates most of the points above.
  52. public class FontCollectionTest
  53. {
  54. public int CreatePDF(Stream stream)
  55. {
  56. // Create a FontCollection instance:
  57. var fc = new FontCollection();
  58. // Populate it with fonts from the specified directory:
  59. fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
  60.  
  61. // Generate a sample document using the font collection to provide fonts:
  62. var doc = new GcPdfDocument();
  63. var page = doc.Pages.Add();
  64. var g = page.Graphics;
  65.  
  66. // For TextLayout/TextFormat to be able to use a font collection, it must be
  67. // associated with it like so:
  68. var tl = new TextLayout(g.Resolution) { FontCollection = fc };
  69.  
  70. // Render some strings using the different fonts from our collection:
  71. var tf = new TextFormat() { FontName = "times new roman", FontSize = 16 };
  72. tl.Append("Using FontCollection to manage fonts and render text\n\n", tf);
  73. tf.FontSize = 12;
  74. tl.Append("Text rendered using Times New Roman regular font. \n", tf);
  75. // Setting a font style (bold or italic) will tell the font collection
  76. // to search for a suitable font (if it is not found, emulation will be used):
  77. tf.FontItalic = true;
  78. tl.Append("Text rendered using Times New Roman italic font. \n", tf);
  79. // Text format is applied to a text run when the text is appended,
  80. // so we can re-use the same format, modifying its properties
  81. // to render differently formatted texts:
  82. tf.FontBold = true;
  83. tl.Append("Text rendered using Times New Roman bold italic font. \n", tf);
  84. tf.FontItalic = false;
  85. tl.Append("Text rendered using Times New Roman bold font. \n", tf);
  86. tf.FontName = "segoe ui";
  87. tl.Append("Text rendered using Segoe UI bold font. \n", tf);
  88. tf.FontBold = false;
  89. tl.Append("Text rendered using Segoe UI regular font. \n", tf);
  90.  
  91. // Apply page settings to the page layout and render the page:
  92. tl.MaxWidth = page.Size.Width;
  93. tl.MaxHeight = page.Size.Height;
  94. tl.MarginAll = 72;
  95. tl.PerformLayout(true);
  96. g.DrawTextLayout(tl, PointF.Empty);
  97.  
  98. // If using GcGraphics.DrawString/MeasureString, this will allow the TextLayout
  99. // created internally by GcGraphics to find the specified fonts in the font collection:
  100. g.FontCollection = fc;
  101.  
  102. // Use GcGraphics.DrawString to show that the font collection is also used
  103. // by the graphics once the FontCollection has been set on it:
  104. g.DrawString("Text rendered using Segoe UI bold, drawn by GcGraphics.DrawString() method.",
  105. new TextFormat() { FontName = "segoe ui", FontBold = true, FontSize = 10 },
  106. new PointF(72, tl.ContentRectangle.Bottom + 12));
  107.  
  108. // Done:
  109. doc.Save(stream);
  110. return doc.Pages.Count;
  111. }
  112. }
  113. }
  114.