From 84561f1ac145329f49544dd6d6666f9f2f571c18 Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Thu, 11 May 2023 14:00:37 +0300 Subject: [PATCH 1/3] rich text box test 1 --- McBitFont/Export.cs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/McBitFont/Export.cs b/McBitFont/Export.cs index 94f9fcb..3d320d2 100644 --- a/McBitFont/Export.cs +++ b/McBitFont/Export.cs @@ -124,7 +124,7 @@ namespace McBitFont { } private void btnGenerate_Click(object sender, EventArgs e) { - //string output = ""; + string output = ""; txtOutput.Clear(); bool com = cbComments.Checked; bool hdr = cbHeader.Checked; @@ -132,21 +132,21 @@ namespace McBitFont { if (com) { //Header comments - txtOutput.AppendText(comments["header"]); - txtOutput.AppendText(comments["scan_order"] + cbOrder.Text + "\n"); - txtOutput.AppendText(comments["scan_hdir"] + cbHDir.Text + "\n"); - txtOutput.AppendText(comments["scan_vdir"] + cbVDir.Text + "\n\n"); + output += comments["header"]; + output += comments["scan_order"] + cbOrder.Text + "\n"; + output += comments["scan_hdir"] + cbHDir.Text + "\n"; + output += comments["scan_vdir"] + cbVDir.Text + "\n\n"; - txtOutput.AppendText(comments["num_bit_order"] + cbBitOrder.Text + "\n"); - txtOutput.AppendText(comments["num_base"] + cbNumBase.Text + "\n"); - txtOutput.AppendText(comments["num_size"] + cbNumSize.Text + "\n\n"); + output += comments["num_bit_order"] + cbBitOrder.Text + "\n"; + output += comments["num_base"] + cbNumBase.Text + "\n"; + output += comments["num_size"] + cbNumSize.Text + "\n\n"; - txtOutput.AppendText(comments["text_format"] + cbFormat.Text + "\n"); - txtOutput.AppendText(comments["text_lines"] + cbLines.Text + "\n\n"); + output += comments["text_format"] + cbFormat.Text + "\n"; + output += comments["text_lines"] + cbLines.Text + "\n\n"; if (hdr) { // comment about meta header map - txtOutput.AppendText(comments["font_header_map"] + "\n\n"); + output += comments["font_header_map"] + "\n\n"; } } @@ -240,9 +240,9 @@ namespace McBitFont { } //Array definition - txtOutput.AppendText("const " + dataType + " " + mainForm.prjName + "[] = {\n"); + output += "const " + dataType + " " + mainForm.prjName + "[] = {\n"; - if (com) txtOutput.AppendText(" // Data:\n"); + if (com) output += " // Data:\n"; foreach ( MainForm.FrameMiniature f in mainForm.frames) { // For each frame string str; @@ -252,7 +252,7 @@ namespace McBitFont { if (com && lines != 1) { // Comments enabled and other than "1 symbol per line" selected // Print a symbol comment before its data - txtOutput.AppendText(" // " + f.code.ToString() + " --> \n"); + output += " // " + f.code.ToString() + " --> \n"; } for (int i = imin; i != imax; i += idir) { str = " "; @@ -283,16 +283,17 @@ namespace McBitFont { str += pref + Convert.ToString(b, nbase).PadLeft(pad, '0') + ','; } } - txtOutput.AppendText(str); - txtOutput.AppendText(Environment.NewLine); + output += str; + output += Environment.NewLine; } - txtOutput.AppendText(Environment.NewLine); + output += Environment.NewLine; } // Close array definition - txtOutput.AppendText("};\n"); + output += "};\n"; - txtOutput.SelectAll(); - ParseText(); + txtOutput.Text = output; + //txtOutput.SelectAll(); + //ParseText(); txtOutput.SelectionStart = 0; txtOutput.ScrollToCaret(); } From ceddd5f66fc3ed517175e461478ed03cddee9037 Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Thu, 11 May 2023 17:21:07 +0300 Subject: [PATCH 2/3] WIP: export text formats --- McBitFont/Export.cs | 113 ++++++++++++++++++++------------------------ McBitFont/Form1.cs | 19 ++++++-- 2 files changed, 65 insertions(+), 67 deletions(-) diff --git a/McBitFont/Export.cs b/McBitFont/Export.cs index 3d320d2..71460b3 100644 --- a/McBitFont/Export.cs +++ b/McBitFont/Export.cs @@ -8,6 +8,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; +using System.Xml.Linq; namespace McBitFont { public partial class Export : Form { @@ -73,62 +74,13 @@ namespace McBitFont { } } - void ParseLine(string line) { - Regex r = new Regex("([ \\t{}():;])"); - string[] tokens = r.Split(line); - - string[] keywords = { "const", "array", "char", "byte" }; - string[] datatypes = { "uint8_t", "uint16_t", "uint32_t" }; - foreach (string token in tokens) { - // Set the tokens default color and font. - txtOutput.SelectionColor = Color.Black; - txtOutput.SelectionFont = new Font("Courier New", (float)9.75, FontStyle.Regular); - - // Check for a comment. - if (token == "//" || token.StartsWith("//")) { - // Find the start of the comment and then extract the whole comment. - int index = line.IndexOf("//"); - string comment = line.Substring(index, line.Length - index); - txtOutput.SelectionColor = Color.FromArgb(0xFF, 0x3F, 0x7F, 0x5F); - txtOutput.SelectionFont = new Font("Courier New", (float)9.75, FontStyle.Regular); - txtOutput.SelectedText = comment; - break; - } - - // Check whether the token is a keyword. - for (int i = 0; i < keywords.Length; i++) { - if (keywords[i] == token) { - // Apply alternative color and font to highlight keyword. - txtOutput.SelectionColor = Color.FromArgb(0xFF, 0x7F, 0x00, 0x55); - txtOutput.SelectionFont = new Font("Courier New", (float)9.75, FontStyle.Bold); - break; - } - } - for (int i = 0; i < datatypes.Length; i++) { - if (datatypes[i] == token) { - // Apply alternative color and font to highlight data type. - txtOutput.SelectionColor = Color.FromArgb(0xFF, 0x00, 0x50, 0x32); ; - txtOutput.SelectionFont = new Font("Courier New", (float)9.75, FontStyle.Regular); - break; - } - } - txtOutput.SelectedText = token; - } - txtOutput.SelectedText = "\n"; - } - - void ParseText() { - foreach (string l in txtOutput.Lines) { - ParseLine(l); - } - } - private void btnGenerate_Click(object sender, EventArgs e) { string output = ""; txtOutput.Clear(); bool com = cbComments.Checked; bool hdr = cbHeader.Checked; int lines = cbLines.SelectedIndex; + int format = cbFormat.SelectedIndex; if (com) { //Header comments @@ -239,23 +191,39 @@ namespace McBitFont { break; } - //Array definition - output += "const " + dataType + " " + mainForm.prjName + "[] = {\n"; + // Array definition + if ( format == 0 || format == 1 ) output += "const " + dataType + " " + mainForm.prjName + "[] = {\n"; + + // Brackets for 2D array definition + string obracket = format == 1 ? "{ " : ""; + string cbracket = format == 1 ? " }" : ""; + // Number output counter + int numcount = 0; + if (com) output += " // Data:\n"; foreach ( MainForm.FrameMiniature f in mainForm.frames) { // For each frame string str; - uint b = 0; + uint b = 0; // current number bits int t, x, y; // t - calculated bit number; x - actual x; y - actual y if (com && lines != 1) { // Comments enabled and other than "1 symbol per line" selected // Print a symbol comment before its data - output += " // " + f.code.ToString() + " --> \n"; + output += " // " + f.code.ToString() + " --> " + mainForm.decodeSymbol(f.code) + "\n"; + } + if (lines == 1) { + // "1 symbol per line" - new line offset + output += " "; + numcount = 0; } for (int i = imin; i != imax; i += idir) { - str = " "; + if (lines == 0) { + // "Column/Row per line" - new line offset + output += " " + obracket; + numcount = 0; + } for (int j = jmin; j != jmax; j += jdir) { if (jdir < 0) t = jmin - j; else t = j; @@ -278,22 +246,37 @@ namespace McBitFont { } if (((t + 1) % bits == 0) || j + jdir == jmax) { - if (str.Length > 2) str += " "; + // we have filled a number with bits - let's post it + + // should we post a comma before the number? + output += numcount == 0 ? "" : ", "; + + // should we post a prefix to the number? pref = (!cbZeroes.Checked && ((b < 10 && nbase == 16) || (b < 2 && nbase == 2))) ? "" : prefix; - str += pref + Convert.ToString(b, nbase).PadLeft(pad, '0') + ','; + output += pref + Convert.ToString(b, nbase).PadLeft(pad, '0'); + + // count posted numbers + numcount++; } } - output += str; - output += Environment.NewLine; + if (lines == 0) { + // "Column/Row per line" - closing line + output += cbracket + (i+idir!=imax ? "," : "") + "\n"; + } + } + if (lines == 1) { + // "1 symbol per line" - closing line + if (com) { + //...with a comment + output += " // " + f.code.ToString() + " --> " + mainForm.decodeSymbol(f.code); + } + output += "\n"; } - output += Environment.NewLine; } // Close array definition output += "};\n"; txtOutput.Text = output; - //txtOutput.SelectAll(); - //ParseText(); txtOutput.SelectionStart = 0; txtOutput.ScrollToCaret(); } @@ -303,6 +286,12 @@ namespace McBitFont { MessageBox.Show("Cannot pack the font meta header into 2D array!\nChoose 1D array or disable the meta header.\n(Setting array format to 1D array)", "No header in 2D array!", MessageBoxButtons.OK, MessageBoxIcon.Information); cbFormat.SelectedIndex = 0; } + if (cbFormat.SelectedIndex == 1) { + cbLines.SelectedIndex = 0; + cbLines.Enabled = false; + } else { + cbLines.Enabled = true; + } } } } diff --git a/McBitFont/Form1.cs b/McBitFont/Form1.cs index 28307a2..f582e7d 100644 --- a/McBitFont/Form1.cs +++ b/McBitFont/Form1.cs @@ -35,6 +35,7 @@ namespace McBitFont { [Serializable] public struct SaveBlock { public bool monospaced; + public int codepage; public List frames; } @@ -50,6 +51,7 @@ namespace McBitFont { bool prjModified = false; public const string version = "1.0"; public string prjName = "Untitled"; + public int codepage = 1251; public MainForm() { InitializeComponent(); @@ -441,6 +443,12 @@ namespace McBitFont { Application.Exit(); } + public string decodeSymbol(int code) { + var enc = Encoding.GetEncoding(codepage); + if (code < 32) return ""; + return enc.GetString(new byte[] { (byte)code }); + } + private void newToolStripMenuItem_Click(object sender, EventArgs e) { if (prjModified) { if (MessageBox.Show("The project is modified.\nDo you want to save it first?", "Project was modified!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { @@ -457,6 +465,8 @@ namespace McBitFont { string append = ""; int neww = (int)form.nudNewX.Value; int newh = (int)form.nudNewY.Value; + nudX.Value = neww; + nudY.Value = newh; if (form.cbSingle.Checked) { frames.Add( new FrameMiniature(0, neww, newh)); //f = frames.Find(x => x.code == 0); @@ -482,12 +492,10 @@ namespace McBitFont { foreach (FrameMiniature ff in frames) { var s = ff.code.ToString(); ilMiniatures.Images.Add(s, (Image)getMiniPictue(ff)); - var enc = Encoding.GetEncoding((form.cbEncoding.SelectedItem as New.EncodingItem).Code); - var sss = enc.GetString(new byte[] { (byte)ff.code }); + codepage =(form.cbEncoding.SelectedItem as New.EncodingItem).Code; + var sss = decodeSymbol(ff.code); miniList.Items.Add(s, s+' '+append+sss, s); } - nudX.Value =neww; - nudY.Value = newh; f = copyFrame(frames.First()); dotPanel.Refresh(); miniList.Refresh(); @@ -523,6 +531,7 @@ namespace McBitFont { SaveBlock sav; sav.monospaced = monospaced; sav.frames = frames; + sav.codepage = codepage; BinaryFormatter formatter = new BinaryFormatter(); using (Stream ms = File.OpenWrite(dlgSave.FileName)) { formatter.Serialize(ms, sav); @@ -542,7 +551,6 @@ namespace McBitFont { } } if (dlgOpen.ShowDialog() == DialogResult.OK) { - //MessageBox.Show(dlgOpen.FileName); SaveBlock sav; BinaryFormatter formatter = new BinaryFormatter(); @@ -551,6 +559,7 @@ namespace McBitFont { fs.Close(); } monospaced = sav.monospaced; + codepage = sav.codepage; lblType.Text = monospaced ? "Monospaced" : "Variable width / Single"; frames = sav.frames; miniList.Items.Clear(); From 691b87b8426ec205b6cf2e7d881bb18e444c517e Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Fri, 12 May 2023 04:35:26 +0300 Subject: [PATCH 3/3] WIP: working on text formats --- McBitFont/Export.Designer.cs | 1 + McBitFont/Export.cs | 71 ++++++++++++++++++++++++++++++++++-- McBitFont/Form1.cs | 2 +- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/McBitFont/Export.Designer.cs b/McBitFont/Export.Designer.cs index 8c138c2..b733b18 100644 --- a/McBitFont/Export.Designer.cs +++ b/McBitFont/Export.Designer.cs @@ -65,6 +65,7 @@ this.txtOutput.Size = new System.Drawing.Size(529, 507); this.txtOutput.TabIndex = 0; this.txtOutput.Text = ""; + this.txtOutput.WordWrap = false; // // gbScan // diff --git a/McBitFont/Export.cs b/McBitFont/Export.cs index 71460b3..33a3490 100644 --- a/McBitFont/Export.cs +++ b/McBitFont/Export.cs @@ -81,6 +81,9 @@ namespace McBitFont { bool hdr = cbHeader.Checked; int lines = cbLines.SelectedIndex; int format = cbFormat.SelectedIndex; + bool mono = mainForm.monospaced; + + int imin, jmin, imax, jmax, idir, jdir; if (com) { //Header comments @@ -102,7 +105,7 @@ namespace McBitFont { } } - int imin, jmin, imax, jmax, idir, jdir; + //Figure out mins and maxes if (cbOrder.SelectedIndex == 0) { if (cbHDir.SelectedIndex == 0) { // Columns; Left to right @@ -192,7 +195,19 @@ namespace McBitFont { } // Array definition - if ( format == 0 || format == 1 ) output += "const " + dataType + " " + mainForm.prjName + "[] = {\n"; + if ( format == 0 || format == 1 ) output += "const " + dataType + " " + mainForm.prjName + "[]" + (format==1?"[]":"") + " = {\n"; + + // Should we add the meta header? + if (hdr) { + output += + " // Meta header\n" + + " " + (mono ? mainForm.frames.First().width.ToString() : "0") + ", // Font width in pixels; 0 - variable width\n" + + " " + mainForm.frames.First().height.ToString() + ", // Font height in pixels\n" + + " 0, // Font space (between symbols) in pixels\n" + + " " + mainForm.frames.First().code.ToString() + ", // First character code\n" + + " " + mainForm.frames.Last().code.ToString() + ", // Last character code\n" + ; + } // Brackets for 2D array definition string obracket = format == 1 ? "{ " : ""; @@ -202,9 +217,9 @@ namespace McBitFont { int numcount = 0; if (com) output += " // Data:\n"; + MainForm.FrameMiniature flast = mainForm.frames.Last(); foreach ( MainForm.FrameMiniature f in mainForm.frames) { // For each frame - string str; uint b = 0; // current number bits int t, x, y; // t - calculated bit number; x - actual x; y - actual y @@ -218,6 +233,49 @@ namespace McBitFont { output += " "; numcount = 0; } + if (!mono && mainForm.frames.Count > 1) { + // Not a single image; Variable width font - lets post the width as a 1st number + + // Should we post a prefix to the number? + pref = (!cbZeroes.Checked && ((f.width < 10 && nbase == 16) || (f.width < 2 && nbase == 2))) ? "" : prefix; + output += " " + pref + Convert.ToString(f.width, nbase).PadLeft(pad, '0') + "," + (lines == 1 ? " " : "\n"); + } + + //Figure out mins and maxes for variable width fonts + if (!mono) { + if (cbOrder.SelectedIndex == 0) { + if (cbHDir.SelectedIndex == 0) { + // Columns; Left to right + imax = f.width; + } else { + // Columns; Right to left + imin = f.width - 1; + } + if (cbVDir.SelectedIndex == 0) { + // Columns; Top to bottom + jmax = f.height; + } else { + // Columns; Bottom to top + jmin = f.height - 1; + } + } else { + if (cbHDir.SelectedIndex == 0) { + // Rows; Left to right + jmax = f.height; + } else { + // Rows; Right to left + jmin = f.height - 1; + } + if (cbVDir.SelectedIndex == 0) { + // Rows; Top to bottom + imax = f.width; + } else { + // Rows; Bottom to top + imin = f.width - 1; + } + } + } + for (int i = imin; i != imax; i += idir) { if (lines == 0) { // "Column/Row per line" - new line offset @@ -261,11 +319,12 @@ namespace McBitFont { } if (lines == 0) { // "Column/Row per line" - closing line - output += cbracket + (i+idir!=imax ? "," : "") + "\n"; + output += cbracket + ((i+idir==imax) && f.Equals(flast) ? "" : ",") + "\n"; } } if (lines == 1) { // "1 symbol per line" - closing line + if (!f.Equals(flast)) output += ","; if (com) { //...with a comment output += " // " + f.code.ToString() + " --> " + mainForm.decodeSymbol(f.code); @@ -286,6 +345,10 @@ namespace McBitFont { MessageBox.Show("Cannot pack the font meta header into 2D array!\nChoose 1D array or disable the meta header.\n(Setting array format to 1D array)", "No header in 2D array!", MessageBoxButtons.OK, MessageBoxIcon.Information); cbFormat.SelectedIndex = 0; } + if (!mainForm.monospaced && cbFormat.SelectedIndex == 1) { + MessageBox.Show("Cannot pack the variable width font into 2D array!\n(Setting array format to 1D array)", "2D arrays are for monospaced fonts only!", MessageBoxButtons.OK, MessageBoxIcon.Information); + cbFormat.SelectedIndex = 0; + } if (cbFormat.SelectedIndex == 1) { cbLines.SelectedIndex = 0; cbLines.Enabled = false; diff --git a/McBitFont/Form1.cs b/McBitFont/Form1.cs index f582e7d..df83efb 100644 --- a/McBitFont/Form1.cs +++ b/McBitFont/Form1.cs @@ -46,7 +46,7 @@ namespace McBitFont { private int pixelOffset = 5; private int gap; private int w, h; - bool monospaced = false; + public bool monospaced = false; bool modified = false; bool prjModified = false; public const string version = "1.0";