WIP: export text formats
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user