Working on export

This commit is contained in:
Anton Mukhin
2023-05-11 13:50:59 +03:00
parent 04bace7267
commit fc60991d85
43 changed files with 344 additions and 66 deletions

View File

@@ -14,6 +14,9 @@ namespace McBitFont {
private MainForm mainForm;
//private List<string> comments = new List<string>();
private Dictionary<string, string> comments = new Dictionary<string, string>();
public Export(object sender) {
InitializeComponent();
@@ -28,6 +31,31 @@ namespace McBitFont {
cb.SelectedIndex = 0;
}
}
comments.Add("header",
"// File generated by McBitFont " + MainForm.version + "\n" +
"// made by Anton Mukhin (mcfly@mcflyer.ru)\n" +
"//\n" +
"//\n"
);
comments.Add("scan_order", "// Scan order: ");
comments.Add("scan_hdir", "// Horizontal direction: ");
comments.Add("scan_vdir", "// Vertical direction: ");
comments.Add("num_bit_order", "// Bit order: ");
comments.Add("num_base", "// Numbers base: ");
comments.Add("num_size", "// Numbers size: ");
comments.Add("text_format", "// Text format: ");
comments.Add("text_lines", "// Numbers per line: ");
comments.Add("font_header_map",
"// Font header map:\n" +
"// width; // Font width in pixels\n" +
"// height; // Font height in pixels\n" +
"// space; // Font space in pixels\n" +
"// first; // First character code\n" +
"// last; // Last character code\n"
);
}
private void cbOrder_SelectedIndexChanged(object sender, EventArgs e) {
@@ -49,29 +77,38 @@ namespace McBitFont {
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("Lucida Console", (float)9.75, FontStyle.Regular);
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.Green;
txtOutput.SelectionFont = new Font("Lucida Console", (float)9.75, FontStyle.Italic);
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.
string[] keywords = { "public", "void", "using", "static", "class", "array", "char", "uint8_t", "uint16_t", "uint32_t", "byte" };
for (int i = 0; i < keywords.Length; i++) {
if (keywords[i] == token) {
// Apply alternative color and font to highlight keyword.
txtOutput.SelectionColor = Color.Blue;
txtOutput.SelectionFont = new Font("Lucida Console", (float)9.75, FontStyle.Bold);
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;
}
}
@@ -87,8 +124,32 @@ namespace McBitFont {
}
private void btnGenerate_Click(object sender, EventArgs e) {
//string output = "";
txtOutput.Clear();
bool com = cbComments.Checked;
bool hdr = cbHeader.Checked;
int lines = cbLines.SelectedIndex;
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");
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");
txtOutput.AppendText(comments["text_format"] + cbFormat.Text + "\n");
txtOutput.AppendText(comments["text_lines"] + cbLines.Text + "\n\n");
if (hdr) {
// comment about meta header map
txtOutput.AppendText(comments["font_header_map"] + "\n\n");
}
}
int imin, jmin, imax, jmax, idir, jdir;
if (cbOrder.SelectedIndex == 0) {
if (cbHDir.SelectedIndex == 0) {
@@ -140,15 +201,19 @@ namespace McBitFont {
ushort bits = 8;
string dataType = "uint8_t";
switch (cbNumSize.SelectedIndex) {
case 0:
bits = 8;
dataType = "uint8_t";
break;
case 1:
bits = 16;
dataType = "uint16_t";
break;
case 2:
bits = 32;
dataType = "uint32_t";
break;
}
@@ -174,13 +239,23 @@ namespace McBitFont {
break;
}
//Array definition
txtOutput.AppendText("const " + dataType + " " + mainForm.prjName + "[] = {\n");
if (com) txtOutput.AppendText(" // Data:\n");
foreach ( MainForm.FrameMiniature f in mainForm.frames) {
// For each frame
string str;
uint b = 0;
int t, x, y;
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
txtOutput.AppendText(" // " + f.code.ToString() + " --> \n");
}
for (int i = imin; i != imax; i += idir) {
str = "";
str = " ";
for (int j = jmin; j != jmax; j += jdir) {
if (jdir < 0) t = jmin - j;
else t = j;
@@ -203,7 +278,7 @@ namespace McBitFont {
}
if (((t + 1) % bits == 0) || j + jdir == jmax) {
if (str.Length > 0) str += " ";
if (str.Length > 2) str += " ";
pref = (!cbZeroes.Checked && ((b < 10 && nbase == 16) || (b < 2 && nbase == 2))) ? "" : prefix;
str += pref + Convert.ToString(b, nbase).PadLeft(pad, '0') + ',';
}
@@ -213,9 +288,20 @@ namespace McBitFont {
}
txtOutput.AppendText(Environment.NewLine);
}
// Close array definition
txtOutput.AppendText("};\n");
txtOutput.SelectAll();
ParseText();
txtOutput.SelectionStart = 0;
txtOutput.ScrollToCaret();
}
private void cbFormat_SelectedIndexChanged(object sender, EventArgs e) {
if (cbFormat.SelectedIndex == 1 && cbHeader.Checked) {
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;
}
}
}
}