static void Main(string[] args) { if (args.Length > 0) { string filename = args[0]; if (System.IO.File.Exists(filename)) { FileStream stream = File.OpenRead(filename); BmfFile FontFile = new BmfFile(stream); string saveLocation = Path.GetDirectoryName(filename); saveLocation += Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(filename); if(!Directory.Exists(saveLocation)) { Directory.CreateDirectory(saveLocation); } var outputObj = new { fontHeight = FontFile.Height, fontAscent = FontFile.Ascent, fontDescent = FontFile.Descent }; JavaScriptSerializer jsonMaker = new JavaScriptSerializer(); string output = jsonMaker.Serialize(outputObj); using (StreamWriter fileWriter = File.CreateText(saveLocation + Path.DirectorySeparatorChar + @"font.txt")) { fileWriter.Write(output); } foreach (KeyValuePair<byte, BmfCharacter> character in FontFile.Characters) { int keyVal = (int)(uint)character.Key; int Width = (int)(uint)character.Value.charWidth; int Height = (int)(uint)character.Value.charHeight; int FullWidth = (int)(uint)character.Value.fullWidth; int FullHeight = (int)(uint)FontFile.Height; int XOffset = (int)(uint)character.Value.rectX0; int YOffset = (int)(uint)character.Value.rectY0; FullWidth = Math.Max(FullWidth, (int)(uint)character.Value.rectX1); FullHeight = Math.Max(FullHeight, (int)(uint)character.Value.rectY1); string outputIDString = Convert.ToString(keyVal, 16).PadLeft(2, '0'); if (FullWidth == 0 || FullWidth == 0) { continue; } else { Bitmap tmpImage = new Bitmap(FullWidth, FullHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); for (int y = 0; y < FullHeight; y++) { for (int x = 0; x < FullWidth; x++) { tmpImage.SetPixel(x, y, Color.White); } } for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { int z = 255 - character.Value.charData[y * Width + x]; tmpImage.SetPixel(XOffset + x, YOffset + y, Color.FromArgb(255, z, z, z)); } } tmpImage.Save(saveLocation + Path.DirectorySeparatorChar + outputIDString + @".bmp"); } } } } else { Console.WriteLine("{Write Usage}"); } }
static void Main(string[] args) { if (args.Length > 0) { string filename = args[0]; if (Directory.Exists(filename)) { string decriptionF = filename + Path.DirectorySeparatorChar + @"font.txt"; if (File.Exists(decriptionF)) { string description; using (StreamReader descriptionR = File.OpenText(decriptionF)) { description = descriptionR.ReadToEnd(); } if (description == null) return; JavaScriptSerializer jsonMaker = new JavaScriptSerializer(); JSONOutputObject data = jsonMaker.Deserialize <JSONOutputObject>(description); BmfFile FontFile = new BmfFile(data.fontHeight, data.fontAscent, data.fontDescent); for (int x = 0; x < 256; x++) { string imageFilename = filename + Path.DirectorySeparatorChar + Convert.ToString(x, 16).PadLeft(2, '0') + @".bmp"; if (File.Exists(imageFilename)) { using (Bitmap thisLetter = (Bitmap)Bitmap.FromFile(imageFilename)) { int Height = thisLetter.Size.Height; int Width = thisLetter.Size.Width; int FullWidth = Width; int OffsetX0 = 0; int OffsetY0 = 0; int OffsetX1 = Width; int OffsetY1 = Height; Console.WriteLine("Processing letter: " + (uint)x + "\t" + Convert.ToString(x, 16) + "\t" + (!char.IsControl((char)x) ? "" + (char)x : "")); bool nonWhiteLine = false; for (int Iy = 0; Iy < Height; Iy++) { for (int Ix = 0; Ix < Width; Ix++) { if (thisLetter.GetPixel(Ix, Iy).R != 0xFF) { nonWhiteLine = true; OffsetY0 = Iy; break; } } if (nonWhiteLine) break; } Console.WriteLine("Got OffsetY0: " + OffsetY0); nonWhiteLine = false; for (int Iy = Height - 1; Iy >= 0; Iy--) { for (int Ix = 0; Ix < Width; Ix++) { if (thisLetter.GetPixel(Ix, Iy).R != 0xFF) { nonWhiteLine = true; OffsetY1 = Iy + 1; break; } } if (nonWhiteLine) break; } if (!nonWhiteLine) OffsetY1 = 0; Console.WriteLine("Got OffsetY1: " + OffsetY1); nonWhiteLine = false; for (int Ix = 0; Ix < Width; Ix++) { for (int Iy = 0; Iy < Height; Iy++) { if (thisLetter.GetPixel(Ix, Iy).R != 0xFF) { nonWhiteLine = true; OffsetX0 = Ix; break; } } if (nonWhiteLine) break; } Console.WriteLine("Got OffsetX0: " + OffsetX0); nonWhiteLine = false; for (int Ix = Width - 1; Ix >= 0; Ix--) { for (int Iy = 0; Iy < Height; Iy++) { if (thisLetter.GetPixel(Ix, Iy).R != 0xFF) { nonWhiteLine = true; OffsetX1 = Ix + 1; break; } } if (nonWhiteLine) break; } if (!nonWhiteLine) OffsetX1 = 0; Console.WriteLine("Got OffsetX1: " + OffsetX1); int croppedWidth = OffsetX1 - OffsetX0; int croppedHeight = OffsetY1 - OffsetY0; if (croppedWidth < 0 || croppedHeight < 0) { Console.WriteLine("wtf"); } byte[] colorBytes = new byte[croppedWidth * croppedHeight]; for (int Iy = OffsetY0; Iy < OffsetY1; Iy++) { for (int Ix = OffsetX0; Ix < OffsetX1; Ix++) { colorBytes[(Iy - OffsetY0) * croppedWidth + (Ix - OffsetX0)] = (byte)(255 - thisLetter.GetPixel(Ix, Iy).R); } } FontFile.Characters.Add((byte)x, new BmfCharacter( (byte)FullWidth, (byte)OffsetX0, (byte)OffsetY0, (byte)OffsetX1, (byte)OffsetY1, (byte)croppedWidth, (byte)croppedHeight, colorBytes)); Console.WriteLine("------------"); } } } using (FileStream outputStream = File.Create(filename.TrimEnd(Path.DirectorySeparatorChar) + @".bmf")) { FontFile.Write(outputStream); } } } } else { Console.WriteLine("{Write Usage}"); } }
private void loadSelectedFile() { if (System.IO.File.Exists(filename)) { // unload anything loaded? listBox1.Items.Clear(); pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; imgWords.Image = null; // /> FontFile = new BmfFile(System.IO.File.OpenRead(filename)); lblFontIdent.Text = FontFile.Indent; lblNumChar.Text = "" + (uint)FontFile.CharCount; nudFontHeight.Value = (uint)FontFile.Height; nudFontAscent.Value = (uint)FontFile.Ascent; nudFontDescent.Value = (uint)FontFile.Descent; listBox1.BeginUpdate(); foreach (KeyValuePair<byte, BmfCharacter> character in FontFile.Characters) { listBox1.Items.Add(new BmfCharacterListItem(character)); } listBox1.EndUpdate(); UpdateTextPreview(); } }