TrueTypeHinting.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Imaging;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsImagingWeb.Demos
  15. {
  16. // This sample demonstrates how to use TrueType font hinting instructions.
  17. //
  18. // Many TrueType fonts include low-level hinting instructions.
  19. // The original purpose of introducing hinting instructions was
  20. // to improve the look of glyphs when the font size is comparable
  21. // to the device resolution.
  22. // Such instructions are also used (especially in CJK fonts)
  23. // to reuse some glyph parts in different glyphs regardless of the font size.
  24. // GcGraphics supports hinting instructions. To enable it, set
  25. // TextFormat.EnableFontHinting property to true when rendering text.
  26. //
  27. // This sample renders a string using small sans-serif and serif fonts
  28. // with hinting turned OFF and ON.
  29. public class TrueTypeHinting
  30. {
  31. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  32. {
  33. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  34. var dy = dpi / 2;
  35. var ip = new PointF(dpi, dpi);
  36. using (var g = bmp.CreateGraphics(Color.White))
  37. {
  38. // Turning anti-aliasing off makes the hinting effect on small text more obvious:
  39. g.Renderer.Aliased = true;
  40.  
  41. var sOff = "Hinting OFF: ";
  42. var sOn = "Hinting ON: ";
  43.  
  44. // Draw a string with hinting instructions off and on:
  45. var text = "The quick brown fox jumps over the lazy dog.";
  46.  
  47. var tf = new TextFormat()
  48. {
  49. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")),
  50. FontSize = 12,
  51. EnableFontHinting = false
  52. };
  53. g.DrawString(sOff + text, tf, ip);
  54. ip.Y += dy;
  55. tf.FontSize = 10;
  56. g.DrawString(sOff + text, tf, ip);
  57. ip.Y += dy * 2;
  58.  
  59. tf.EnableFontHinting = true;
  60. tf.FontSize = 12;
  61. g.DrawString(sOn + text, tf, ip);
  62. ip.Y += dy;
  63. tf.FontSize = 10;
  64. g.DrawString(sOn + text, tf, ip);
  65. ip.Y += dy * 2;
  66.  
  67. // Draw the same string with a serif font:
  68. tf.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf"));
  69. tf.EnableFontHinting = false;
  70. tf.FontSize = 12;
  71. g.DrawString(sOff + text, tf, ip);
  72. ip.Y += dy;
  73. tf.FontSize = 10;
  74. g.DrawString(sOff + text, tf, ip);
  75. ip.Y += dy * 2;
  76.  
  77. tf.EnableFontHinting = true;
  78. tf.FontSize = 12;
  79. g.DrawString(sOn + text, tf, ip);
  80. ip.Y += dy;
  81. tf.FontSize = 10;
  82. g.DrawString(sOn + text, tf, ip);
  83. ip.Y += dy * 2;
  84. }
  85. // Done:
  86. return bmp;
  87. }
  88. }
  89. }
  90.