Export form dummy. Syntax highlight in the text output box.

This commit is contained in:
Anton Mukhin
2023-05-05 16:55:34 +03:00
parent 8da1614ab2
commit 40f006a309
9 changed files with 681 additions and 111 deletions

89
McBitFont/Export.cs Normal file
View File

@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace McBitFont {
public partial class Export : Form {
public Export() {
InitializeComponent();
}
private void Export_Load(object sender, EventArgs e) {
var groupBoxes = Controls.OfType<GroupBox>();
foreach (var gb in groupBoxes) {
var comboBoxes = gb.Controls.OfType<ComboBox>();
foreach (var cb in comboBoxes) {
cb.SelectedIndex = 0;
}
}
}
private void cbOrder_SelectedIndexChanged(object sender, EventArgs e) {
switch (cbOrder.SelectedIndex) {
case 0: // Columns
cbBitOrder.Items[0] = "LSB Top";
cbBitOrder.Items[1] = "MSB Top";
cbLines.Items[0] = "1 Column per line";
break;
case 1: // Rows
cbBitOrder.Items[0] = "LSB Left";
cbBitOrder.Items[1] = "MSB Left";
cbLines.Items[0] = "1 Row per line";
break;
}
}
void ParseLine(string line) {
Regex r = new Regex("([ \\t{}():;])");
string[] tokens = r.Split(line);
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);
// 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.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);
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) {
txtOutput.SelectAll();
ParseText();
}
}
}