/// <summary> /// run bm2rgbi on the temp file /// </summary> private string CompressFile(FontDefinition fd, string tempFile) { ProcessStartInfo pi; Process p; FileInfo fi; string compressedName, args; // run bm2rgbi on the file compressedName = tempFile + ".lzg"; args = "\"" + tempFile + "\" \"" + compressedName + "\" " + fd.CompressionOptions + " -c"; pi = new ProcessStartInfo("bm2rgbi.exe", args); pi.CreateNoWindow = true; pi.UseShellExecute = false; pi.WindowStyle = ProcessWindowStyle.Hidden; p = Process.Start(pi); p.WaitForExit(); // make sure that it did something fi = new FileInfo(compressedName); if (fi.Length == 0) { throw new Exception("Failed to compress the font character. Verify that bm2rgbi.exe and lzg.exe are both present in the same directory as LzgFontConv.exe"); } return(compressedName); }
/// <summary> /// parse character definitions. The input string must be sorted. /// </summary> public void ParseCharacters(Control refCtrl, FontDefinition fd, string str, bool doCompression) { CharDef cd; FormProgress progress; Font font; progress = new FormProgress(); progress.ProgressBar.Minimum = 0; progress.ProgressBar.Maximum = str.Length; if (doCompression) { progress.Show(); } // create a font font = new Font(fd.Family, fd.Size, fd.Style, GraphicsUnit.Pixel); this.Height = Convert.ToInt32(font.GetHeight()); try { this.Definitions = new List <CharDef>(); foreach (char c in str) { // update progress progress.ProgressBar.PerformStep(); Application.DoEvents(); // create character cd = new CharDef(); cd.Parse(refCtrl, fd, font, c, doCompression); Definitions.Add(cd); } } finally { if (doCompression) { progress.Close(); } } }
/// <summary> /// parse the values for the character /// </summary> public void Parse(Control refCtrl, FontDefinition fd, Font font, char c, bool doCompression) { SizeF size; this.Character = c; // space is a special case if (c == ' ') { ParseSpace(refCtrl, font); return; } using (Graphics g = refCtrl.CreateGraphics()) { g.TextRenderingHint = fd.Hint; // get the size that the system thinks the character occupies size = TextRenderer.MeasureText(g, c.ToString(), font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix); this.CharacterBitmap = new Bitmap(Convert.ToInt32(size.Width) + fd.ExtraSpacing.Width, Convert.ToInt32(font.GetHeight()) + fd.ExtraSpacing.Height); this.Size = this.CharacterBitmap.Size; using (Graphics charGraphics = Graphics.FromImage(this.CharacterBitmap)) { charGraphics.TextRenderingHint = fd.Hint; using (SolidBrush backBrush = new SolidBrush(fd.Background)) charGraphics.FillRectangle(backBrush, 0, 0, this.CharacterBitmap.Width, this.CharacterBitmap.Height); // draw the character into the center of the bitmap TextRenderer.DrawText(charGraphics, c.ToString(), font, new Point(0, 0), fd.Foreground, fd.Background, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix); } // scan the bitmap if (doCompression) { ScanBitmap(this.CharacterBitmap, fd); } } }
/// <summary> /// parse character definitions. The input string must be sorted. /// </summary> public void ParseCharacters(Control refCtrl,FontDefinition fd,string str,bool doCompression) { CharDef cd; FormProgress progress; Font font; progress=new FormProgress(); progress.ProgressBar.Minimum=0; progress.ProgressBar.Maximum=str.Length; if(doCompression) progress.Show(); // create a font font=new Font(fd.Family,fd.Size,fd.Style,GraphicsUnit.Pixel); this.Height=Convert.ToInt32(font.GetHeight()); try { this.Definitions=new List<CharDef>(); foreach(char c in str) { // update progress progress.ProgressBar.PerformStep(); Application.DoEvents(); // create character cd=new CharDef(); cd.Parse(refCtrl,fd,font,c,doCompression); Definitions.Add(cd); } } finally { if(doCompression) progress.Close(); } }
/// <summary> /// parse the values for the character /// </summary> public void Parse(Control refCtrl,FontDefinition fd,Font font,char c,bool doCompression) { SizeF size; this.Character=c; // space is a special case if(c==' ') { ParseSpace(refCtrl,font); return; } using(Graphics g=refCtrl.CreateGraphics()) { g.TextRenderingHint=fd.Hint; // get the size that the system thinks the character occupies size=TextRenderer.MeasureText(g,c.ToString(),font,new Size(int.MaxValue,int.MaxValue),TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix); this.CharacterBitmap=new Bitmap(Convert.ToInt32(size.Width)+fd.ExtraSpacing.Width,Convert.ToInt32(font.GetHeight())+fd.ExtraSpacing.Height); this.Size=this.CharacterBitmap.Size; using(Graphics charGraphics=Graphics.FromImage(this.CharacterBitmap)) { charGraphics.TextRenderingHint=fd.Hint; using(SolidBrush backBrush=new SolidBrush(fd.Background)) charGraphics.FillRectangle(backBrush,0,0,this.CharacterBitmap.Width,this.CharacterBitmap.Height); // draw the character into the center of the bitmap TextRenderer.DrawText(charGraphics,c.ToString(),font,new Point(0,0),fd.Foreground,fd.Background,TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix); } // scan the bitmap if(doCompression) ScanBitmap(this.CharacterBitmap,fd); } }
/// <summary> /// scan the bitmap for its values /// </summary> protected void ScanBitmap(Bitmap bm, FontDefinition fd) { string tempFile, compressedName = null; byte[] bytes; // get a temporary filename tempFile = Path.GetTempFileName() + ".png"; try { // write the bitmap as a PNG to the temporary file bm.Save(tempFile, ImageFormat.Png); // compress the bitmap compressedName = CompressFile(fd, tempFile); // read back the bytes bytes = File.ReadAllBytes(compressedName); // encode the bytes with a length prefix in LSB order this.CompressedBytes = new byte[bytes.Length + 2]; this.CompressedBytes[0] = (byte)(bytes.Length & 0xff); this.CompressedBytes[1] = (byte)(bytes.Length >> 8); bytes.CopyTo(this.CompressedBytes, 2); } finally { File.Delete(tempFile); if (compressedName != null) { File.Delete(compressedName); } } }
/// <summary> /// load from disk /// </summary> private void _btnLoad_Click(object sender, EventArgs e) { OpenFileDialog ofd; FontDefinition fd; try { // choose file ofd = new OpenFileDialog(); ofd.Filter = "Saved Fonts (*.xml)|*.xml|All Files (*.*)|*.*||"; if (ofd.ShowDialog() == DialogResult.Cancel) { return; } // read document to the font definition fd = new FontDefinition(); fd.ReadXml(ofd.FileName); // set up the controls _cmbFont.Text = fd.Family; _cmbSize.Text = fd.Size.ToString(); _editSelectedCharacters.Text = fd.Characters; _editBackground.Text = ColourToString(fd.Background); _editForeground.Text = ColourToString(fd.Foreground); switch (fd.Hint) { case TextRenderingHint.SystemDefault: _cmbRenderingHint.SelectedIndex = 0; break; case TextRenderingHint.SingleBitPerPixelGridFit: _cmbRenderingHint.SelectedIndex = 1; break; case TextRenderingHint.SingleBitPerPixel: _cmbRenderingHint.SelectedIndex = 2; break; case TextRenderingHint.AntiAliasGridFit: _cmbRenderingHint.SelectedIndex = 3; break; case TextRenderingHint.AntiAlias: _cmbRenderingHint.SelectedIndex = 4; break; default: _cmbRenderingHint.SelectedIndex = 5; break; } switch (fd.Style) { case FontStyle.Bold: _cmbStyles.SelectedIndex = 1; break; case FontStyle.Italic: _cmbStyles.SelectedIndex = 2; break; case FontStyle.Bold | FontStyle.Italic: _cmbStyles.SelectedIndex = 3; break; default: _cmbStyles.SelectedIndex = 0; break; } _editTftWidth.Value = fd.TftResolution.Width; _editTftHeight.Value = fd.TftResolution.Height; _editCharacterSpacing.Value = fd.Spacing; _editPreviewText.Text = fd.PreviewText; _editPreviewX.Value = fd.PreviewPosition.X; _editPreviewY.Value = fd.PreviewPosition.Y; _editCompressionOptions.Text = fd.CompressionOptions; _editExtraW.Value = fd.ExtraSpacing.Width; _editExtraH.Value = fd.ExtraSpacing.Height; // new font def _fd = fd; _preview.FontDef = _fd; // update UpdatePreviewEvent(null, null); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// run bm2rgbi on the temp file /// </summary> private string CompressFile(FontDefinition fd,string tempFile) { ProcessStartInfo pi; Process p; FileInfo fi; string compressedName,args; // run bm2rgbi on the file compressedName=tempFile+".lzg"; args="\""+tempFile+"\" \""+compressedName+"\" "+fd.CompressionOptions+" -c"; pi=new ProcessStartInfo("bm2rgbi.exe",args); pi.CreateNoWindow=true; pi.UseShellExecute=false; pi.WindowStyle=ProcessWindowStyle.Hidden; p=Process.Start(pi); p.WaitForExit(); // make sure that it did something fi=new FileInfo(compressedName); if(fi.Length==0) throw new Exception("Failed to compress the font character. Verify that bm2rgbi.exe and lzg.exe are both present in the same directory as LzgFontConv.exe"); return compressedName; }
/// <summary> /// scan the bitmap for its values /// </summary> protected void ScanBitmap(Bitmap bm,FontDefinition fd) { string tempFile,compressedName=null; byte[] bytes; // get a temporary filename tempFile=Path.GetTempFileName()+".png"; try { // write the bitmap as a PNG to the temporary file bm.Save(tempFile,ImageFormat.Png); // compress the bitmap compressedName=CompressFile(fd,tempFile); // read back the bytes bytes=File.ReadAllBytes(compressedName); // encode the bytes with a length prefix in LSB order this.CompressedBytes=new byte[bytes.Length+2]; this.CompressedBytes[0]=(byte)(bytes.Length & 0xff); this.CompressedBytes[1]=(byte)(bytes.Length >> 8); bytes.CopyTo(this.CompressedBytes,2); } finally { File.Delete(tempFile); if(compressedName!=null) File.Delete(compressedName); } }
/// <summary> /// load from disk /// </summary> private void _btnLoad_Click(object sender, EventArgs e) { OpenFileDialog ofd; FontDefinition fd; try { // choose file ofd=new OpenFileDialog(); ofd.Filter="Saved Fonts (*.xml)|*.xml|All Files (*.*)|*.*||"; if(ofd.ShowDialog()==DialogResult.Cancel) return; // read document to the font definition fd=new FontDefinition(); fd.ReadXml(ofd.FileName); // set up the controls _cmbFont.Text=fd.Family; _cmbSize.Text=fd.Size.ToString(); _editSelectedCharacters.Text=fd.Characters; _editBackground.Text=ColourToString(fd.Background); _editForeground.Text=ColourToString(fd.Foreground); switch(fd.Hint) { case TextRenderingHint.SystemDefault: _cmbRenderingHint.SelectedIndex=0; break; case TextRenderingHint.SingleBitPerPixelGridFit: _cmbRenderingHint.SelectedIndex=1; break; case TextRenderingHint.SingleBitPerPixel: _cmbRenderingHint.SelectedIndex=2; break; case TextRenderingHint.AntiAliasGridFit: _cmbRenderingHint.SelectedIndex=3; break; case TextRenderingHint.AntiAlias: _cmbRenderingHint.SelectedIndex=4; break; default: _cmbRenderingHint.SelectedIndex=5; break; } switch(fd.Style) { case FontStyle.Bold: _cmbStyles.SelectedIndex=1; break; case FontStyle.Italic: _cmbStyles.SelectedIndex=2; break; case FontStyle.Bold | FontStyle.Italic: _cmbStyles.SelectedIndex=3; break; default: _cmbStyles.SelectedIndex=0; break; } _editTftWidth.Value=fd.TftResolution.Width; _editTftHeight.Value=fd.TftResolution.Height; _editCharacterSpacing.Value=fd.Spacing; _editPreviewText.Text=fd.PreviewText; _editPreviewX.Value=fd.PreviewPosition.X; _editPreviewY.Value=fd.PreviewPosition.Y; _editCompressionOptions.Text=fd.CompressionOptions; _editExtraW.Value=fd.ExtraSpacing.Width; _editExtraH.Value=fd.ExtraSpacing.Height; // new font def _fd=fd; _preview.FontDef=_fd; // update UpdatePreviewEvent(null,null); } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } }