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", "arial.ttf"))
  41. fc.RegisterFont(Path.Combine("Resources", "Fonts", "times.ttf"))
  42. fc.RegisterFont(Path.Combine("Resources", "Fonts", "yumin.ttf"))
  43. fc.RegisterFont(Path.Combine("Resources", "Fonts", "msgothic.ttc"))
  44. fc.RegisterFont(Path.Combine("Resources", "Fonts", "YuGothR.ttc"))
  45.  
  46. '' Tell the font collection to use Yu Mincho as a fallback:
  47. fc.AppendFallbackFonts(fc.FindFamilyName("Yu Mincho"))
  48.  
  49. '' Using Arial font renders the test string as empty rectangles, as suitable glyphs are not present in Arial:
  50. tf.Font = fc.FindFamilyName("Arial", False, False)
  51. g.DrawString($"Arial: {tstr} (no EUDC font has been linked yet)", tf, ip)
  52. ip.Y += dy
  53.  
  54. '' Load two custome 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 Arial - now in strings rendered with Arial, EUDC chars will be looked up in this font:
  59. Dim fnt = fc.FindFamilyName("Arial")
  60. fnt.AddEudcFont(eudcF0)
  61. '' Ditto for Yu Mincho font:
  62. fnt = fc.FindFamilyName("Yu Mincho")
  63. fnt.AddEudcFont(eudcF0)
  64. '' Link another EUDC font to Yu Gothic:
  65. fnt = fc.FindFamilyName("Yu Gothic")
  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("Arial", False, False)
  70. g.DrawString($"Arial, linked with Eudc0.tte: {tstr}", tf, ip)
  71. ip.Y += dy
  72. tf.Font = fc.FindFileName("times.ttf")
  73. g.DrawString($"Times, fallback via Yu Mincho: {tstr}", tf, ip)
  74. ip.Y += dy
  75. tf.Font = fc.FindFamilyName("MS Gothic")
  76. g.DrawString($"MS Gothic, fallback via Yu Mincho: {tstr}", tf, ip)
  77. ip.Y += dy
  78. tf.Font = fc.FindFamilyName("Yu Gothic")
  79. g.DrawString($"Yu Gothic, 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", "Gabriola.ttf"))
  85. fnt.AddEudcFont(eudcF0)
  86. tf.Font = fnt
  87. g.DrawString($"Gabriola 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.