FontFallbacks.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. Imports GCTEXT = GrapeCity.Documents.Text
  10. Imports GCDRAW = GrapeCity.Documents.Drawing
  11.  
  12. '' Fallback fonts are fonts used to draw glyphs that are not present
  13. '' in a font specified by the application.
  14. '' DsPdf provides a default list of fallback font families
  15. '' that is automatically initialized, and includes large fonts
  16. '' that are usually suitable to be used as fallbacks for many
  17. '' languages for which some common fonts do not have the glyphs.
  18. '' These automatically added fallback font families are available
  19. '' via methods on the FontCollection.SystemFonts static collection.
  20. '' You can customize the default (and system-dependent) behavior
  21. '' by providing your own fallback fonts, and by adding them either
  22. '' to fallbacks managed by the global FontCollection.SystemFonts,
  23. '' by adding them to your own instance of the FontCollection,
  24. '' or to specific fonts that you are using.
  25. '' In this way the fallback font behavior can be finely tuned
  26. '' and be completely system-independent.
  27. ''
  28. '' This sample demonstrates the basic fallback behavior -
  29. '' clearing system fallbacks and re-adding them again.
  30. '' Additionally, it prints the list of fallback fonts
  31. '' found on the current system.
  32. Public Class FontFallbacks
  33. Function CreatePDF(ByVal stream As Stream) As Integer
  34. '' Set up GcPdfDocument:
  35. Dim doc = New GcPdfDocument()
  36. Dim g = doc.NewPage().Graphics
  37.  
  38. '' Set up some helper vars for rendering lines of text:
  39. Const margin = 36.0F
  40. '' Insertion point (DsPdf's default resolution is 72dpi, use 1/2" margins all around):
  41. Dim ip = New PointF(margin, margin)
  42. '' Init a text format with one of the standard fonts. Standard fonts are minimal
  43. '' and contain very few glyphs for non-Latin characters.
  44. Dim tf = New TextFormat() With {.Font = StandardFonts.Courier, .FontSize = 14}
  45.  
  46. '' Get the list of fallback font families:
  47. Dim fallbacks As String() = FontCollection.SystemFonts.GetFallbackFontFamilies()
  48.  
  49. '' Clear global fallbacks list:
  50. FontCollection.SystemFonts.ClearFallbackFontFamilies()
  51. FontCollection.SystemFonts.ClearFallbackFonts()
  52.  
  53. '' Now there are no global fallback fonts, so Japanese text rendered using
  54. '' a standard font will produce 'blank boxes' instead of real Japanese characters:
  55. g.DrawString("A Japanese text that won't render: あなたは日本語を話せますか?", tf, ip)
  56. ip.Y += 36
  57.  
  58. '' Re-add the original list of fallback font families to global SystemFonts:
  59. FontCollection.SystemFonts.AppendFallbackFontFamilies(fallbacks)
  60. '' On some systems, default system fallbacks might not provide Japanese glyphs,
  61. '' so we add our own fallback just in case:
  62. Dim arialuni = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialuni.ttf"))
  63. FontCollection.SystemFonts.AppendFallbackFonts(arialuni)
  64.  
  65. '' Now that fallback fonts are available again, the same Japanese text will render
  66. '' correctly as an appropriate fallback will have been found:
  67. g.DrawString("Same text with fallbacks available: あなたは日本語を話せますか?", tf, ip)
  68. ip.Y += 36
  69.  
  70. '' Finally, we list all fallbacks and print a test line using each:
  71. Dim drawTestLine As Action(Of String) =
  72. Sub(fnt_ As String)
  73. Dim tf1 = New TextFormat() With {.FontName = fnt_}
  74. Dim tstr = $"{fnt_}: The quick brown fox jumps over the lazy dog."
  75. Dim s = g.MeasureString(tstr, tf1, doc.PageSize.Width - margin * 2)
  76. g.DrawString(tstr, tf1, New RectangleF(ip, s))
  77. ip.Y += s.Height * 1.5F
  78. If ip.Y > doc.Pages.Last.Size.Height - margin * 2 Then
  79. g = doc.NewPage().Graphics
  80. ip.Y = 36
  81. End If
  82. End Sub
  83.  
  84. For Each fnt In fallbacks
  85. drawTestLine(fnt)
  86. Next
  87. ''
  88. '' Done:
  89. doc.Save(stream)
  90. Return doc.Pages.Count
  91. End Function
  92. End Class
  93.