Program.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace FontBuilder
  8. {
  9. class Program
  10. {
  11. public static System.Drawing.Bitmap LoadImage(string Path)
  12. {
  13. var image = System.Drawing.Image.FromFile(Path);
  14. return new System.Drawing.Bitmap(image);
  15. }
  16. static void Main(string[] args)
  17. {
  18. Console.OutputEncoding = Encoding.UTF8;
  19. var options = Newtonsoft.Json.JsonConvert.DeserializeObject<Options>(File.ReadAllText(args[0]));
  20. var workingDirectory = args[1];
  21. var characters = new List<char>();
  22. foreach (var range in options.Ranges)
  23. for (var i = range.Low; range.High >= range.Low && i <= range.High; ++i)
  24. characters.Add((char)i);
  25. if (options.SearchForCharacters)
  26. RecursivelySearchForCharacters(Path.Combine(workingDirectory, options.SearchPath), options, characters);
  27. characters = characters.Distinct().ToList();
  28. foreach (var target in options.Targets)
  29. {
  30. var glyphs = new List<Glyph>();
  31. Dictionary<char, System.Drawing.Bitmap> baseFontGlyphs;
  32. if (!String.IsNullOrEmpty(target.BaseFont))
  33. {
  34. try
  35. {
  36. var baseFontImage = System.Drawing.Image.FromFile(Path.Combine(workingDirectory, target.BaseFont));
  37. baseFontGlyphs = VariableWidthBitmapFont.DecodeVariableWidthBitmapFont(new System.Drawing.Bitmap(baseFontImage));
  38. foreach (var glyph in baseFontGlyphs)
  39. glyphs.Add(new Glyph
  40. {
  41. Code = glyph.Key,
  42. X = 0,
  43. Y = 0,
  44. Width = glyph.Value.Width,
  45. Height = glyph.Value.Height,
  46. Bitmap = glyph.Value
  47. });
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine("Error loading basefont: {0}", e.Message);
  52. baseFontGlyphs = new Dictionary<char, System.Drawing.Bitmap>();
  53. }
  54. }
  55. else
  56. baseFontGlyphs = new Dictionary<char, System.Drawing.Bitmap>();
  57. var font = new System.Drawing.Font(options.FontName, target.FontSize);
  58. var bitmap = new System.Drawing.Bitmap(1, 1);
  59. var graphics = System.Drawing.Graphics.FromImage(bitmap);
  60. foreach (var c in characters)
  61. {
  62. if (baseFontGlyphs.ContainsKey(c)) continue;
  63. try
  64. {
  65. var stringSize = graphics.MeasureString(new string(c, 1), font);
  66. var g = new Glyph { Code = c, Width = (int)stringSize.Width, Height = (int)stringSize.Height };
  67. g.Bitmap = new System.Drawing.Bitmap(g.Width, g.Height);
  68. var glyphGraphics = System.Drawing.Graphics.FromImage(g.Bitmap);
  69. glyphGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
  70. glyphGraphics.DrawString(new string(c, 1), font, System.Drawing.Brushes.White, 0, 0);
  71. glyphGraphics.Flush();
  72. glyphGraphics.Dispose();
  73. glyphs.Add(g);
  74. Console.Write(c);
  75. }
  76. catch (Exception e)
  77. {
  78. Console.WriteLine("{2} {3} Exception: {0} {1}", e.Message, e.StackTrace, target.FontSize, c);
  79. break;
  80. }
  81. }
  82. Console.WriteLine();
  83. //font.Dispose();
  84. var imagePath = String.IsNullOrEmpty(target.OutputName) ? String.Format("__{0}.bmp", options.FontName) : target.OutputName + ".bmp";
  85. if (glyphs.Count > 0)
  86. {
  87. var atlas = AtlasCompiler.Compile(glyphs);
  88. bitmap = new System.Drawing.Bitmap(atlas.Dimensions.Width, atlas.Dimensions.Height);
  89. var composeGraphics = System.Drawing.Graphics.FromImage(bitmap);
  90. foreach (var glyph in atlas.Glyphs)
  91. composeGraphics.DrawImageUnscaled(glyph.Bitmap, new System.Drawing.Point(glyph.X, glyph.Y));
  92. composeGraphics.Flush();
  93. bitmap.Save(Path.Combine(workingDirectory, imagePath));
  94. var jsonPath = String.IsNullOrEmpty(target.OutputName) ? String.Format("__{0}_def.font", options.FontName) : target.OutputName + "_def.font";
  95. var json = Newtonsoft.Json.JsonConvert.SerializeObject(atlas);
  96. File.WriteAllText(Path.Combine(workingDirectory, jsonPath), json);
  97. composeGraphics.Dispose();
  98. Console.WriteLine("Generated target {0}", imagePath);
  99. }
  100. else
  101. Console.WriteLine("Target {0} generated no glyphs.", imagePath);
  102. graphics.Dispose();
  103. }
  104. }
  105. static void RecursivelySearchForCharacters(String Path, Options Options, List<char> Into)
  106. {
  107. Console.WriteLine("Directory: {0}", Path);
  108. if (!System.IO.Directory.Exists(Path))
  109. {
  110. Console.WriteLine("Attempted to search directory that does not exist.");
  111. return;
  112. }
  113. foreach (var file in System.IO.Directory.EnumerateFiles(Path))
  114. {
  115. var extension = System.IO.Path.GetExtension(file);
  116. if (Options.SearchExtensions.Contains(extension))
  117. {
  118. Console.WriteLine("File: {0}", file);
  119. var chars = File.ReadAllText(file).Distinct();
  120. foreach (var c in chars)
  121. {
  122. if (Into.Contains(c)) continue;
  123. Into.Add(c);
  124. Console.WriteLine("Found {0}", c);
  125. }
  126. }
  127. }
  128. foreach (var directory in System.IO.Directory.EnumerateDirectories(Path))
  129. RecursivelySearchForCharacters(directory, Options, Into);
  130. }
  131. }
  132. }