TrueTypeHinting.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Imaging
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' This sample demonstrates how to use TrueType font hinting instructions.
  14. ''
  15. '' Many TrueType fonts include low-level hinting instructions.
  16. '' The original purpose of introducing hinting instructions was
  17. '' to improve the look of glyphs when the font size Is comparable
  18. '' to the device resolution. But such instructions are now also used
  19. '' (especially in CJK fonts) to reuse some glyph parts in different
  20. '' glyphs regardless of the font size.
  21. '' GcGraphics supports hinting instructions. To enable it, set
  22. '' TextFormat.EnableFontHinting property to true when rendering text.
  23. ''
  24. '' This sample renders a Latin text in a small size with hinting off And on,
  25. '' And then renders a CJK text also with hinting off And on.
  26. Public Class TrueTypeHinting
  27. Function GenerateImage(
  28. ByVal pixelSize As Size,
  29. ByVal dpi As Single,
  30. ByVal opaque As Boolean,
  31. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  32.  
  33. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  34. Dim dy = dpi / 2
  35. Dim ip = New PointF(dpi, dpi)
  36. Using g = bmp.CreateGraphics(Color.White)
  37. '' Turning anti-aliasing off makes the hinting effect on small text more obvious:
  38. g.Renderer.Aliased = True
  39.  
  40. Const sOff = "Hinting OFF: "
  41. Const sOn = "Hinting ON: "
  42.  
  43. '' Draw a Latin string with hinting instructions off And on:
  44. Const sLat = "The quick brown fox jumps over the lazy dog."
  45.  
  46. Dim tfLat = New TextFormat() With
  47. {
  48. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arial.ttf")),
  49. .FontSize = 12,
  50. .FontName = "Arial",
  51. .EnableFontHinting = False
  52. }
  53. g.DrawString(sOff + sLat, tfLat, ip)
  54. ip.Y += dy
  55.  
  56. tfLat.FontSize = 10
  57. g.DrawString(sOff + sLat, tfLat, ip)
  58. ip.Y += dy * 2
  59.  
  60. tfLat.EnableFontHinting = True
  61. tfLat.FontSize = 12
  62. g.DrawString(sOn + sLat, tfLat, ip)
  63. ip.Y += dy
  64.  
  65. tfLat.FontSize = 10
  66. g.DrawString(sOn + sLat, tfLat, ip)
  67. ip.Y += dy * 2
  68.  
  69. '' Draw a CJK string with hinting instructions off And on
  70. '' "Best year in spring, best day in morning"
  71. Const sCJK = "一年之计在于春,一日之计在于晨"
  72.  
  73. '' For CJK text, we can turn anti-aliasing on as the effect of
  74. '' hinting Is obvious in the characters' form itself:
  75. g.Renderer.Aliased = False
  76.  
  77. Dim tfCJK = New TextFormat() With
  78. {
  79. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "kaiu.ttf")),
  80. .FontSize = 30,
  81. .EnableFontHinting = False
  82. }
  83. g.DrawString(sOff + vbCrLf + sCJK, tfCJK, ip)
  84. ip.Y += dy * 3
  85.  
  86. tfCJK.EnableFontHinting = True
  87. g.DrawString(sOn + vbCrLf + sCJK, tfCJK, ip)
  88. End Using
  89. '' Done
  90. Return bmp
  91. End Function
  92. End Class
  93.