Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #region MIT License
  2. /*Copyright (c) 2012 Robert Rouhani <robert.rouhani@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. this software and associated documentation files (the "Software"), to deal in
  5. the Software without restriction, including without limitation the rights to
  6. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is furnished to do
  8. so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.*/
  18. #endregion
  19. using System;
  20. using System.Drawing;
  21. using System.Drawing.Imaging;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Threading;
  25. using SharpFont;
  26. namespace Examples
  27. {
  28. class Program
  29. {
  30. public static void Main(string[] args)
  31. {
  32. //TODO make several examples in an example browser
  33. try
  34. {
  35. using (Library lib = new Library())
  36. {
  37. Console.WriteLine("FreeType version: " + lib.Version + "\n");
  38. using (Face face = lib.NewFace(@"Fonts/Cousine-Regular-Latin.ttf", 0))
  39. {
  40. //attach a finalizer delegate
  41. face.Generic = new Generic(IntPtr.Zero, OnFaceDestroyed);
  42. //write out some basic font information
  43. Console.WriteLine("Information for font " + face.FamilyName);
  44. Console.WriteLine("====================================");
  45. Console.WriteLine("Number of faces: " + face.FaceCount);
  46. Console.WriteLine("Face flags: " + face.FaceFlags);
  47. Console.WriteLine("Style: " + face.StyleName);
  48. Console.WriteLine("Style flags: " + face.StyleFlags);
  49. face.SetCharSize(0, 32 * 64, 0, 96);
  50. Console.WriteLine("\nWriting string \"Hello World!\":");
  51. Bitmap bmp = RenderString(face, "Hello World!");
  52. bmp.Save("helloworld.png", ImageFormat.Png);
  53. bmp.Dispose();
  54. Console.WriteLine("Done!\n");
  55. }
  56. }
  57. }
  58. catch (FreeTypeException e)
  59. {
  60. Console.Write(e.Error.ToString());
  61. }
  62. Console.ReadKey();
  63. }
  64. public static Bitmap RenderString(Face face, string text)
  65. {
  66. int penX = 0, penY = 0;
  67. int width = 0;
  68. int height = 0;
  69. //measure the size of the string before rendering it, requirement of Bitmap.
  70. for (int i = 0; i < text.Length; i++)
  71. {
  72. char c = text[i];
  73. uint glyphIndex = face.GetCharIndex(c);
  74. face.LoadGlyph(glyphIndex, LoadFlags.Default, LoadTarget.Normal);
  75. width += (int)face.Glyph.Advance.X >> 6;
  76. if (face.HasKerning && i < text.Length - 1)
  77. {
  78. char cNext = text[i + 1];
  79. width += (int)face.GetKerning(glyphIndex, face.GetCharIndex(cNext), KerningMode.Default).X >> 6;
  80. }
  81. if (face.Glyph.Metrics.Height >> 6 > height)
  82. height = (int)face.Glyph.Metrics.Height >> 6;
  83. }
  84. //create a new bitmap that fits the string.
  85. Bitmap bmp = new Bitmap(width, height);
  86. //draw the string
  87. for (int i = 0; i < text.Length; i++)
  88. {
  89. char c = text[i];
  90. uint glyphIndex = face.GetCharIndex(c);
  91. face.LoadGlyph(glyphIndex, LoadFlags.Default, LoadTarget.Normal);
  92. face.Glyph.RenderGlyph(RenderMode.Normal);
  93. if (c == ' ')
  94. {
  95. penX += (int)face.Glyph.Advance.X >> 6;
  96. if (face.HasKerning && i < text.Length - 1)
  97. {
  98. char cNext = text[i + 1];
  99. width += (int)face.GetKerning(glyphIndex, face.GetCharIndex(cNext), KerningMode.Default).X >> 6;
  100. }
  101. penY += (int)face.Glyph.Advance.Y >> 6;
  102. continue;
  103. }
  104. BitmapData data = bmp.LockBits(new Rectangle(penX, penY + (bmp.Height - face.Glyph.Bitmap.Rows), face.Glyph.Bitmap.Width, face.Glyph.Bitmap.Rows), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  105. byte[] pixelAlphas = new byte[face.Glyph.Bitmap.Width * face.Glyph.Bitmap.Rows];
  106. Marshal.Copy(face.Glyph.Bitmap.Buffer, pixelAlphas, 0, pixelAlphas.Length);
  107. for (int j = 0; j < pixelAlphas.Length; j++)
  108. {
  109. int pixelOffset = (j / data.Width) * data.Stride + (j % data.Width * 4);
  110. Marshal.WriteByte(data.Scan0, pixelOffset + 3, pixelAlphas[j]);
  111. }
  112. bmp.UnlockBits(data);
  113. penX += (int)face.Glyph.Advance.X >> 6;
  114. penY += (int)face.Glyph.Advance.Y >> 6;
  115. if (face.HasKerning && i < text.Length - 1)
  116. {
  117. char cNext = text[i + 1];
  118. width += (int)face.GetKerning(glyphIndex, face.GetCharIndex(cNext), KerningMode.Default).X >> 6;
  119. }
  120. }
  121. return bmp;
  122. }
  123. /// <summary>
  124. /// Called when Face is destroyed.
  125. /// </summary>
  126. /// <param name="face">Pointer to the face</param>
  127. public static void OnFaceDestroyed(IntPtr face)
  128. {
  129. //Console.WriteLine(face.FamilyName);
  130. Console.WriteLine("Face destroyed!");
  131. }
  132. }
  133. }