EUDC.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 System.Collections.Generic
  10. Imports System.Linq
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' Shows how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte).
  15. Public Class EUDC
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. '' Test string using EUDC codes and two regular chars (& and !): 0xE620 0xE621 0xE622 0xE624 & 0xE623 !
  18. Const tstr = "&!"
  19. '' Set up:
  20. Dim doc = New GcPdfDocument()
  21. Dim page = doc.NewPage()
  22. Dim g = page.Graphics
  23. Dim tf = New TextFormat() With {.FontSize = 20}
  24. Dim rc = Util.AddNote(
  25. "This sample demonstrates how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte)." + vbLf +
  26. "A GrapeCity.Documents.Text.Font can be created from an EUDC .tte file, " +
  27. "and linked to one or more fonts using Font.AddEudcFont() method.",
  28. page)
  29. Const dy = 36.0F
  30. Dim ip = New PointF(rc.X, rc.Bottom + dy / 2)
  31.  
  32. '' Use FontCollection to allow fetching fonts by family names:
  33. Dim fc = New FontCollection()
  34.  
  35. '' Assign the font collection to the graphics so that MeasureString/DrawString
  36. '' methods on the graphics can find fallback fonts:
  37. g.FontCollection = fc
  38.  
  39. '' Register some regular fonts with the FontCollection:
  40. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"))
  41. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf"))
  42. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerifSC-Regular.ttf"))
  43. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Light.ttf"))
  44. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSans-Condensed.ttf"))
  45.  
  46. '' Tell the font collection to use Noto Serif as a fallback:
  47. fc.AppendFallbackFonts(fc.FindFamilyName("Noto Serif"))
  48.  
  49. '' Using Arial font renders the test string as empty rectangles, as suitable glyphs are not present in Noto Sans:
  50. tf.Font = fc.FindFamilyName("Noto Sans", False, False)
  51. g.DrawString($"Arial: {tstr} (no EUDC font has been linked yet)", tf, ip)
  52. ip.Y += dy
  53.  
  54. '' Load two custom EUDC fonts:
  55. Dim eudcF0 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte"))
  56. Dim eudcF1 = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"))
  57.  
  58. '' Link one EUDC font to Noto Sans - now in strings rendered with Noto Sans, EUDC chars will be looked up in this font:
  59. Dim fnt = fc.FindFamilyName("Noto Sans")
  60. fnt.AddEudcFont(eudcF0)
  61. '' Ditto for Noto Serif font:
  62. fnt = fc.FindFamilyName("Noto Serif")
  63. fnt.AddEudcFont(eudcF0)
  64. '' Link another EUDC font to Noto Sans Light:
  65. fnt = fc.FindFamilyName("Noto Sans Light")
  66. fnt.AddEudcFont(eudcF1)
  67.  
  68. '' Render strings with EUDC chars using fonts to which our custom EUDC font is linked:
  69. tf.Font = fc.FindFamilyName("Noto Sans", False, False)
  70. g.DrawString($"Noto Sans, linked with Eudc0.tte: {tstr}", tf, ip)
  71. ip.Y += dy
  72. tf.Font = fc.FindFileName("NotoSerif-Regular.ttf")
  73. g.DrawString($"Noto Sans, fallback via Noto Serif: {tstr}", tf, ip)
  74. ip.Y += dy
  75. tf.Font = fc.FindFamilyName("Noto Sans Condensed")
  76. g.DrawString($"Noto Sans Condensed, fallback via Noto Serif: {tstr}", tf, ip)
  77. ip.Y += dy
  78. tf.Font = fc.FindFamilyName("Noto Sans Light")
  79. g.DrawString($"Noto Sans Light, linked with Eudc1.tte: {tstr}", tf, ip)
  80. ip.Y += dy
  81.  
  82. '' FontCollection adds some services (like font lookup by family name),
  83. '' but EUDC fonts can be linked to fonts that are not in a collection:
  84. fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "VLADIMIR.TTF"))
  85. fnt.AddEudcFont(eudcF0)
  86. tf.Font = fnt
  87. g.DrawString($"VLADIMIR Font, linked with Eudc0.tte: {tstr}", tf, ip)
  88. ip.Y += dy
  89. ''
  90. '' Done:
  91. doc.Save(stream)
  92. Return doc.Pages.Count
  93. End Function
  94. End Class
  95.