/* * This file is shared between the open source stm32plus and * Arduino XMEM graphics libraries. * * Copyright (c) 2011,2012 Andy Brown * Please see website for licensing terms. */ using System; using System.Drawing; using System.Drawing.Text; using System.Globalization; using System.Windows.Forms; namespace LzgFontConv { /// /// main form class /// public partial class FormMain : Form { /// /// font definition class /// private FontDefinition _fd=new FontDefinition(); /// /// constructor /// public FormMain() { InitializeComponent(); } /// /// form loaded /// private void FormMain_Load(object sender, EventArgs e) { EnumFonts(); this._preview.FontDef=_fd; } /// /// Enumerate fonts and add to dropdown /// private void EnumFonts() { foreach(System.Windows.Media.FontFamily fontFamily in System.Windows.Media.Fonts.SystemFontFamilies) _cmbFont.Items.Add(fontFamily.Source); _cmbFont.SelectedIndex=0; _cmbRenderingHint.SelectedIndex=5; _cmbStyles.SelectedIndex=0; } /// /// Insert character ranges /// private void _btnNumbers_Click(object sender, EventArgs e) { InsertChars(48,57); } private void _btnUpperCase_Click(object sender, EventArgs e) { InsertChars(65,90); } private void _btnLowerCase_Click(object sender, EventArgs e) { InsertChars(97,122); } private void _btnSymbols_Click(object sender, EventArgs e) { InsertChars(32,47); InsertChars(58,64); InsertChars(91,96); InsertChars(123,126); } private void _btnAscii_Click(object sender, EventArgs e) { InsertChars(32,126); } /// /// Insert a range of characters /// private void InsertChars(int first,int last) { String str; int i; str=_editSelectedCharacters.Text.Substring(0,_editSelectedCharacters.SelectionStart); for(i=first;i<=last;i++) str+=Convert.ToChar(i); str+=_editSelectedCharacters.Text.Substring(_editSelectedCharacters.SelectionStart); _editSelectedCharacters.Text=str; } /// /// Choose background /// private void _btnChooseBackground_Click(object sender, EventArgs e) { _colorDialog.Color=_backgroundPreview.Value; if(_colorDialog.ShowDialog()==DialogResult.Cancel) return; _editBackground.Text=ColourToString(_colorDialog.Color); } /// /// Choose foreground /// private void _btnChooseForeground_Click(object sender, EventArgs e) { _colorDialog.Color=_foregroundPreview.Value; if(_colorDialog.ShowDialog()==DialogResult.Cancel) return; _editForeground.Text=ColourToString(_colorDialog.Color); } /// /// Convert a colour to a RRGGBB string /// private String ColourToString(Color cr) { return cr.R.ToString("X2")+cr.G.ToString("X2")+cr.B.ToString("X2"); } /// /// Something happened that means the preview is to be updated /// private void UpdatePreviewEvent(object sender, EventArgs e) { // update colour preview try { UpdateFont(); UpdateFontSize(); UpdateSelectedCharacters(); UpdateCharacterSpacing(); UpdateExtraSize(); UpdateBackground(); UpdateForeground(); UpdateCompressionOptions(); _fd.PreviewPosition=UpdatePosition(); _fd.TftResolution=new Size(UpdateTftWidth(),UpdateTftHeight()); switch(_cmbRenderingHint.SelectedIndex) { case 0: _preview.RenderingHint=TextRenderingHint.SystemDefault; break; case 1: _preview.RenderingHint=TextRenderingHint.SingleBitPerPixelGridFit; break; case 2: _preview.RenderingHint=TextRenderingHint.SingleBitPerPixel; break; case 3: _preview.RenderingHint=TextRenderingHint.AntiAliasGridFit; break; case 4: _preview.RenderingHint=TextRenderingHint.AntiAlias; break; case 5: _preview.RenderingHint=TextRenderingHint.ClearTypeGridFit; break; } _fd.Hint=_preview.RenderingHint; _preview.Position=_fd.PreviewPosition; _preview.Font=CreateFont(); _preview.Size=_fd.TftResolution; _preview.BackgroundColour=_backgroundPreview.Value; _preview.ForegroundColour=_foregroundPreview.Value; _preview.Text=_editPreviewText.Text; _preview.Invalidate(); _fd.PreviewText=_preview.Text; } catch { } } /// /// validate the preview text position /// private Point UpdatePosition() { Point p; p=new Point( ValidateIntRange(_editPreviewX.Text,_editPreviewX,0,9999), ValidateIntRange(_editPreviewY.Text,_editPreviewY,0,9999) ); return p; } /// /// Create the font for the preview /// private Font CreateFont() { FontStyle style; switch(_cmbStyles.SelectedIndex) { case 1: style=FontStyle.Bold; break; case 2: style=FontStyle.Italic; break; case 3: style=FontStyle.Bold | FontStyle.Italic; break; default: style=FontStyle.Regular; break; } _fd.Style=style; return new Font(_cmbFont.Text,float.Parse(_cmbSize.Text),style,GraphicsUnit.Pixel); } /// /// update TFT width /// private int UpdateTftWidth() { return ValidateIntRange(_editTftWidth.Text,_editTftWidth,1,9999); } /// /// update TFT height /// private int UpdateTftHeight() { return ValidateIntRange(_editTftHeight.Text,_editTftHeight,1,9999); } /// /// Update the character spacing /// private void UpdateCharacterSpacing() { _fd.Spacing=ValidateIntRange(_editCharacterSpacing.Text,_editCharacterSpacing,0,9999); } /// /// update the extra size /// private void UpdateExtraSize() { _fd.ExtraSpacing=new Size(ValidateIntRange(_editExtraW.Text,_editExtraW,0,9999), ValidateIntRange(_editExtraH.Text,_editExtraH,0,9999)); } /// /// validate the font /// private void UpdateFont() { if(_cmbFont.Items.Contains(_cmbFont.Text)) _cmbFont.BackColor=SystemColors.Window; else { _cmbFont.BackColor=Color.PaleVioletRed; throw new Exception("Invalid font name"); } _fd.Family=_cmbFont.Text; } /// /// validate font size /// private void UpdateFontSize() { _fd.Size=ValidateIntRange(_cmbSize.Text,_cmbSize,1,9999); } /// /// validate that an integer is not zero /// private int ValidateIntRange(String str,Control c,int low,int high) { int size; try { size=int.Parse(str); if(sizehigh) throw new Exception("Invalid size"); c.BackColor=SystemColors.Window; return size; } catch(Exception ex) { c.BackColor=Color.PaleVioletRed; throw ex; } } /// /// Update the selected characters /// private void UpdateSelectedCharacters() { if(_editSelectedCharacters.TextLength==0) { _editSelectedCharacters.BackColor=Color.PaleVioletRed; throw new Exception("No characters selected"); } _editSelectedCharacters.BackColor=SystemColors.Window; _fd.Characters=_editSelectedCharacters.Text; } /// /// Update the compression options /// private void UpdateCompressionOptions() { if(_editCompressionOptions.Text.Length==0) { _editCompressionOptions.BackColor=Color.PaleVioletRed; throw new Exception("No compression options entered"); } _editCompressionOptions.BackColor=SystemColors.Window; _fd.CompressionOptions=_editCompressionOptions.Text; } /// /// validate background colour /// private void UpdateBackground() { Color cr; String str; try { str=_editBackground.Text; cr=Color.FromArgb( 0xff, byte.Parse(str.Substring(0,2),NumberStyles.HexNumber), byte.Parse(str.Substring(2,2),NumberStyles.HexNumber), byte.Parse(str.Substring(4,2),NumberStyles.HexNumber) ); _editBackground.BackColor=SystemColors.Window; _backgroundPreview.Value=cr; _backgroundPreview.Invalidate(); _fd.Background=cr; } catch(Exception ex) { _editBackground.BackColor=Color.PaleVioletRed; throw ex; } } /// /// validate foreground colour /// private void UpdateForeground() { Color cr; String str; try { str=_editForeground.Text; cr=Color.FromArgb( 0xff, byte.Parse(str.Substring(0,2),NumberStyles.HexNumber), byte.Parse(str.Substring(2,2),NumberStyles.HexNumber), byte.Parse(str.Substring(4,2),NumberStyles.HexNumber) ); _editForeground.BackColor=SystemColors.Window; _foregroundPreview.Value=cr; _foregroundPreview.Invalidate(); _fd.Foreground=cr; } catch(Exception ex) { _editForeground.BackColor=Color.PaleVioletRed; throw ex; } } /// /// save the selected font for the Arduino /// private void _btnSaveArduino_Click(object sender, EventArgs e) { ArduinoFontWriter fw; try { UpdatePreviewEvent(null,null); fw=new ArduinoFontWriter(); fw.FontDef=_fd; fw.Write(this); } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } } /// /// save the selected font for stm32plus /// private void _btnSaveStm32plus_Click(object sender, EventArgs e) { Stm32plusFontWriter fw; try { UpdatePreviewEvent(null,null); fw=new Stm32plusFontWriter(); fw.FontDef=_fd; fw.Write(this); } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } } /// /// estimate the memory requirement /// private void _btnMemory_Click(object sender, EventArgs e) { MemoryEstimator me; int size; try { UpdatePreviewEvent(null,null); me=new MemoryEstimator(); me.FontDef=_fd; size=me.Estimate(this); MessageBox.Show("Approximately "+size+" bytes of flash memory are required.","Memory Requirement",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } } /// /// load from disk /// 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); } } /// /// click on logo /// private void _logo_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http://www.andybrown.me.uk"); } } }