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