private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog fileSelector = new OpenFileDialog(); fileSelector.Title = "Inport Font Data"; fileSelector.DefaultExt = "xml"; fileSelector.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"; if (fileSelector.ShowDialog() == DialogResult.OK) { XmlSerializer ser = new XmlSerializer(typeof(Font_Builder.Font)); FileStream reader = new FileStream(fileSelector.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Stream reader = new Stream( // (fileSelector.FileName); m_oFontData = ser.Deserialize(reader) as Font_Builder.Font; reader.Close(); // bind Font Class to Controls: m_oUVCoordsPanel.DataSource = m_oFontData; // Set Current Directpory to be the Font files location, this allows us to use relative paths: Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(fileSelector.FileName); // load Image: m_oFontTexture = new Bitmap(m_oFontData.Texture); } }
private void GenFontTextureButton_Click(object sender, EventArgs e) { // Create New Font Class m_oFontData = new Font_Builder.Font(); try { // If the current font is invalid, report that to the user. if (fontError != null) { throw new ArgumentException(fontError); } // Convert the character range from string to integer, // and validate it. int minChar = ParseHex(m_oControlsPanel.MinChar); int maxChar = ParseHex(m_oControlsPanel.MaxChar); if ((minChar >= maxChar) || (minChar < 0) || (minChar > 0x00FF) || (maxChar < 0) || (maxChar > 0x00FF)) { throw new ArgumentException("Invalid character range " + m_oControlsPanel.MinChar + " - " + m_oControlsPanel.MaxChar); } // Choose the output file. SaveFileDialog fileSelector = new SaveFileDialog(); fileSelector.Title = "Export Font"; fileSelector.DefaultExt = "bmp"; fileSelector.Filter = "PNG Files (*.png)|*.png|All files (*.*)|*.*"; if (fileSelector.ShowDialog() == DialogResult.OK) { m_oFontData.Texture = "./" + Path.GetFileName(fileSelector.FileName); // Build up a list of all the glyphs to be output. List <Bitmap> bitmaps = new List <Bitmap>(); List <int> xPositions = new List <int>(); List <int> yPositions = new List <int>(); try { int padding = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int width = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int height = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int lineWidth = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int lineHeight = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int count = 0; // Rasterize each character in turn, // and add it to the output list. for (char ch = (char)minChar; ch < maxChar; ch++) { Bitmap bitmap = RasterizeCharacter(ch); // renders the char to its own small bitmap! bitmaps.Add(bitmap); xPositions.Add(lineWidth); yPositions.Add(height); Character oCharacter = new Character(); oCharacter.Char = ch.ToString(); // note that in windows/.net/directX the we work from top left so lineWidth/height = top left. // in openGL we work from bottom left, note that this is the default. if (Font_Builder.Properties.Settings.Default.UVCoordSystem == UV_COORDS_SYSTEMS.DIRECTX) { oCharacter.Umin = (float)lineWidth; oCharacter.Vmax = (float)height; oCharacter.Umax = (float)(lineWidth + bitmap.Width); oCharacter.Vmin = (float)(height + bitmap.Height); } else { oCharacter.Umin = (float)lineWidth; oCharacter.Vmin = (float)height; oCharacter.Umax = (float)(lineWidth + bitmap.Width); oCharacter.Vmax = (float)(height + bitmap.Height); } // move along to the next Position: lineWidth += bitmap.Width + padding; lineHeight = Math.Max(lineHeight, bitmap.Height + padding); // wrap depending on stly set in the settings: if (Font_Builder.Properties.Settings.Default.MaxLineWidthStyle == BITMAP_WIDTH_CAP_STYLE.CAP_BY_NO_OF_PIXELS) { // check to see if we are less then the max width + padding current char size (as aprox) // if not wrap to next line. if ((lineWidth + padding + bitmap.Width) > Font_Builder.Properties.Settings.Default.MaxLineWidthInPixels) { width = Math.Max(width, lineWidth); height += lineHeight; lineWidth = padding; lineHeight = padding; } } else { // Output 16 glyphs per line, then wrap to the next line. if ((++count == 16) || (ch == maxChar - 1)) { // wrap: width = Math.Max(width, lineWidth); height += lineHeight; lineWidth = padding; lineHeight = padding; count = 0; } } // Add char to font data: m_oFontData.Characters.Add(oCharacter); } if (Font_Builder.Properties.Settings.Default.PadBitmapToNextPowerOfTwo) { // Make sure our texture is sized to a power of 2: width = NextPowOf2(width); height = NextPowOf2(height); } m_oFontTexture = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Arrage all the glyphs onto a single larger bitmap. Graphics graphics = Graphics.FromImage(m_oFontTexture); graphics.Clear(Font_Builder.Properties.Settings.Default.BitmapBackgroundColor); graphics.CompositingMode = CompositingMode.SourceCopy; for (int i = 0; i < bitmaps.Count; i++) { graphics.DrawImage(bitmaps[i], xPositions[i], yPositions[i]); } graphics.Flush(); // Save out the combined bitmap. ImageCodecInfo PngEncoder = PngEncoder = ImageCodecInfo.GetImageEncoders()[4]; // Built-in PNG Codec System.Drawing.Imaging.Encoder PngEncoderQL = System.Drawing.Imaging.Encoder.Quality; EncoderParameters pngEncoderParameters = new EncoderParameters(1); EncoderParameter PngEncoderParam = new EncoderParameter(PngEncoderQL, 50L); pngEncoderParameters.Param[0] = PngEncoderParam; m_oFontTexture.Save(fileSelector.FileName, PngEncoder, pngEncoderParameters); //set pitcure box to bitmap: //m_oTexturePanel.FontPictureBox.ImageLocation = fileSelector.FileName; //m_oTexturePanel.FontPictureBox.Image = m_oFontTexture; //Set Proper UV Coords: m_oFontData.ConvertUVCoords(m_oFontTexture.Width, m_oFontTexture.Height); // update prop values: m_oFontData.UpdateProportions(); // bind Font Class to Controls m_oUVCoordsPanel.DataSource = m_oFontData; } finally { // Clean up temporary objects. foreach (Bitmap bitmap in bitmaps) { bitmap.Dispose(); } } } } catch (Exception exception) { // Report any errors to the user. MessageBox.Show(exception.Message, Text + " Error"); } m_oTexturePanel.Repaint(); }
private void GenFontTextureButton_Click(object sender, EventArgs e) { // Create New Font Class m_oFontData = new Font_Builder.Font(); try { // If the current font is invalid, report that to the user. if (fontError != null) throw new ArgumentException(fontError); // Convert the character range from string to integer, // and validate it. int minChar = ParseHex(m_oControlsPanel.MinChar); int maxChar = ParseHex(m_oControlsPanel.MaxChar); if ((minChar >= maxChar) || (minChar < 0) || (minChar > 0x00FF) || (maxChar < 0) || (maxChar > 0x00FF)) { throw new ArgumentException("Invalid character range " + m_oControlsPanel.MinChar + " - " + m_oControlsPanel.MaxChar); } // Choose the output file. SaveFileDialog fileSelector = new SaveFileDialog(); fileSelector.Title = "Export Font"; fileSelector.DefaultExt = "bmp"; fileSelector.Filter = "PNG Files (*.png)|*.png|All files (*.*)|*.*"; if (fileSelector.ShowDialog() == DialogResult.OK) { m_oFontData.Texture = "./" + Path.GetFileName(fileSelector.FileName); // Build up a list of all the glyphs to be output. List<Bitmap> bitmaps = new List<Bitmap>(); List<int> xPositions = new List<int>(); List<int> yPositions = new List<int>(); try { int padding = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int width = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int height = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int lineWidth = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int lineHeight = Font_Builder.Properties.Settings.Default.BitmatPaddingBetweenChars; int count = 0; // Rasterize each character in turn, // and add it to the output list. for (char ch = (char)minChar; ch < maxChar; ch++) { Bitmap bitmap = RasterizeCharacter(ch); // renders the char to its own small bitmap! bitmaps.Add(bitmap); xPositions.Add(lineWidth); yPositions.Add(height); Character oCharacter = new Character(); oCharacter.Char = ch.ToString(); // note that in windows/.net/directX the we work from top left so lineWidth/height = top left. // in openGL we work from bottom left, note that this is the default. if (Font_Builder.Properties.Settings.Default.UVCoordSystem == UV_COORDS_SYSTEMS.DIRECTX) { oCharacter.Umin = (float)lineWidth; oCharacter.Vmax = (float)height; oCharacter.Umax = (float)(lineWidth + bitmap.Width); oCharacter.Vmin = (float)(height + bitmap.Height); } else { oCharacter.Umin = (float)lineWidth; oCharacter.Vmin = (float)height; oCharacter.Umax = (float)(lineWidth + bitmap.Width); oCharacter.Vmax = (float)(height + bitmap.Height); } // move along to the next Position: lineWidth += bitmap.Width + padding; lineHeight = Math.Max(lineHeight, bitmap.Height + padding); // wrap depending on stly set in the settings: if (Font_Builder.Properties.Settings.Default.MaxLineWidthStyle == BITMAP_WIDTH_CAP_STYLE.CAP_BY_NO_OF_PIXELS) { // check to see if we are less then the max width + padding current char size (as aprox) // if not wrap to next line. if ((lineWidth + padding + bitmap.Width) > Font_Builder.Properties.Settings.Default.MaxLineWidthInPixels) { width = Math.Max(width, lineWidth); height += lineHeight; lineWidth = padding; lineHeight = padding; } } else { // Output 16 glyphs per line, then wrap to the next line. if ((++count == 16) || (ch == maxChar - 1)) { // wrap: width = Math.Max(width, lineWidth); height += lineHeight; lineWidth = padding; lineHeight = padding; count = 0; } } // Add char to font data: m_oFontData.Characters.Add(oCharacter); } if (Font_Builder.Properties.Settings.Default.PadBitmapToNextPowerOfTwo) { // Make sure our texture is sized to a power of 2: width = NextPowOf2(width); height = NextPowOf2(height); } m_oFontTexture = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Arrage all the glyphs onto a single larger bitmap. Graphics graphics = Graphics.FromImage(m_oFontTexture); graphics.Clear(Font_Builder.Properties.Settings.Default.BitmapBackgroundColor); graphics.CompositingMode = CompositingMode.SourceCopy; for (int i = 0; i < bitmaps.Count; i++) { graphics.DrawImage(bitmaps[i], xPositions[i], yPositions[i]); } graphics.Flush(); // Save out the combined bitmap. ImageCodecInfo PngEncoder = PngEncoder = ImageCodecInfo.GetImageEncoders()[4]; // Built-in PNG Codec System.Drawing.Imaging.Encoder PngEncoderQL = System.Drawing.Imaging.Encoder.Quality; EncoderParameters pngEncoderParameters = new EncoderParameters(1); EncoderParameter PngEncoderParam = new EncoderParameter(PngEncoderQL, 50L); pngEncoderParameters.Param[0] = PngEncoderParam; m_oFontTexture.Save(fileSelector.FileName, PngEncoder, pngEncoderParameters); //set pitcure box to bitmap: //m_oTexturePanel.FontPictureBox.ImageLocation = fileSelector.FileName; //m_oTexturePanel.FontPictureBox.Image = m_oFontTexture; //Set Proper UV Coords: m_oFontData.ConvertUVCoords(m_oFontTexture.Width, m_oFontTexture.Height); // update prop values: m_oFontData.UpdateProportions(); // bind Font Class to Controls m_oUVCoordsPanel.DataSource = m_oFontData; } finally { // Clean up temporary objects. foreach (Bitmap bitmap in bitmaps) bitmap.Dispose(); } } } catch (Exception exception) { // Report any errors to the user. MessageBox.Show(exception.Message, Text + " Error"); } m_oTexturePanel.Repaint(); }