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.
  19. '' Such instructions are also used (especially in CJK fonts)
  20. '' to reuse some glyph parts in different 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 string using small sans-serif and serif fonts
  25. '' with hinting turned 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 string with hinting instructions off and on:
  44. Const text = "The quick brown fox jumps over the lazy dog."
  45.  
  46. Dim tf = New TextFormat() With
  47. {
  48. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")),
  49. .FontSize = 12,
  50. .EnableFontHinting = False
  51. }
  52. g.DrawString(sOff + text, tf, ip)
  53. ip.Y += dy
  54. tf.FontSize = 10
  55. g.DrawString(sOff + text, tf, ip)
  56. ip.Y += dy * 2
  57.  
  58. tf.EnableFontHinting = True
  59. tf.FontSize = 12
  60. g.DrawString(sOn + text, tf, ip)
  61. ip.Y += dy
  62. tf.FontSize = 10
  63. g.DrawString(sOn + text, tf, ip)
  64. ip.Y += dy * 2
  65.  
  66. '' Draw the same string with a serif font
  67. tf.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf"))
  68. tf.EnableFontHinting = False
  69. tf.FontSize = 12
  70. g.DrawString(sOff + text, tf, ip)
  71. ip.Y += dy
  72. tf.FontSize = 10
  73. g.DrawString(sOff + text, tf, ip)
  74. ip.Y += dy * 2
  75.  
  76. tf.EnableFontHinting = True
  77. tf.FontSize = 12
  78. g.DrawString(sOn + text, tf, ip)
  79. ip.Y += dy
  80. tf.FontSize = 10
  81. g.DrawString(sOn + text, tf, ip)
  82. ip.Y += dy * 2
  83. End Using
  84. '' Done
  85. Return bmp
  86. End Function
  87. End Class
  88.