我希望能够在Windows窗体或WPF窗口的标题栏中显示带下标的文本。这样做的原因是简单的。我们的项目团队已经编写了分子编辑器:
我们不仅要显示其名称'ACME',还要显示以下内容:
ACME-编辑C 6 H 12 Cl
文本被下标(并且可能是上标),以及控件是显示在Windows Forms还是WPF主机中。
(由于你们问:)
最初的问题是询问如何在Form的标题中插入RichTextBox,以显示化学式。当然可以做到:您可以在此处查看DwmExtendFrameIntoClientArea和文档:Custom Window Frame Using DWM和许多SO questions。
但是在这里看起来像过头了。有一个更简单的选择:利用现有的Unicode SubScript符号(这是Unicode类别)来重现公式。
这些是印度阿拉伯数字的基本下标和SuperScript Unicode代码点:
char[] subScriptNumbers = {
'\u2080', '\u2081', '\u2082', '\u2083', '\u2084',
'\u2085', '\u2086', '\u2087', '\u2088', '\u2089'
};
char[] superScriptNumbers = {
'\u2070', '\u00B9', '\u00B2', '\u00B3', '\u2074',
'\u2075', '\u2076', '\u2077', '\u2078', '\u2079'
};
正如注释中所提示的,可以将简单公式:[C6H12Cl
转换为C₆H₁₂Cl
,将数字映射到SubScript范围中的相应Unicode值。例如:
this.Text = string.Concat("C6H12Cl".Select(c => char.IsDigit(c) ? subScriptNumbers[c-48] : c));
或者,因为SubScript代码点是顺序的(SuperScript的不是):
const int subScriptBase = 0x2080;
string chem = "C6H12Cl";
// Or this.Title, in WPF
this.Text = chem.Aggregate(new StringBuilder(), (sb, c) =>
sb.Append(char.IsDigit(c) ? (char)(subScriptBase + c - 48) : c)).ToString();
由于似乎有人感兴趣,所以我提出了一个稍微复杂一些的解析器(使用相同的逻辑),以生成不同种类的公式:
[此处显示的类可以使用简单的表示法(类似于Wikipedia所使用的标记的表示法)来转换SubScript / SuperScript数字或字母的序列:
SuperScript: [+:symbols] A[+:12] => A¹²
SubScript: [-:symbols] A[-:12] => A₁₂
Fraction: [f:symbols/symbols] A·[f:x/12] => A·ˣ⁄₁₂
例如:
string formula = "N[+:(x+2)] · H[+:3] · γLog[-:e] + δ· [f:n11/x]";
// Or this.Text, in WinForms
this.Title = UniSubSup.Parse(formula);
将打印:
N⁽ˣ⁺²⁾·H³·γLogₑ + δ·ⁿ¹¹⁄ₓ
Note1:markup中的空格不被处理,因此[+:(x+2)]
可以,而[+:(x + 2)]
则可以。
Note2:我没有包括所有字母因为是星期六和,因为不是所有字体都支持SubScript和/或SuperScript类别中的所有代码点。一些(很少)这样做。诸如fileformat.info的网站可以提供这些信息。
public class UniSubSup
{
const char joiner = '\u200D';
const char nonJoiner = '\u200C';
const char fraction = '\u2044';
const char solidusShort = '\u0337';
const char solidusLong = '\u0338';
internal static Dictionary<char, char> superScripts = new Dictionary<char, char>()
{
['0'] = '\u2070', ['1'] = '\u00B9', ['2'] = '\u00B2', ['3'] = '\u00B3',
['4'] = '\u2074', ['5'] = '\u2075', ['6'] = '\u2076', ['7'] = '\u2077',
['8'] = '\u2078', ['9'] = '\u2079',
['+'] = '\u207A', ['-'] = '\u207B', ['='] = '\u207C',
['('] = '\u207D', [')'] = '\u207E',
['e'] = '\u1D49', ['n'] = '\u207F', ['x'] = '\u02E3'
};
internal static Dictionary<char, char> subScripts = new Dictionary<char, char>()
{
['0'] = '\u2080', ['1'] = '\u2081', ['2'] = '\u2082', ['3'] = '\u2083',
['4'] = '\u2084', ['5'] = '\u2085', ['6'] = '\u2086', ['7'] = '\u2087',
['8'] = '\u2088', ['9'] = '\u2089',
['+'] = '\u208A', ['-'] = '\u208B', ['='] = '\u208C',
['('] = '\u208D', [')'] = '\u208E', ['/'] = '\u2044',
['e'] = '\u2091', ['n'] = '\u2099', ['x'] = '\u2093'
};
protected internal static string sub(string s) =>
s.Aggregate(new StringBuilder(), (sb, c) => sb.Append(subScripts[c])).ToString();
protected internal static string sup(string s) =>
s.Aggregate(new StringBuilder(), (sb, c) => sb.Append(superScripts[c])).ToString();
protected internal static string fract(string str)
{
var sb = new StringBuilder();
var parts = str.Split('/');
parts[0].Aggregate(sb, (s, c) => sb.Append(superScripts[c]));
sb.Append(fraction);
parts[1].Aggregate(sb, (s, c) => sb.Append(subScripts[c]));
return sb.ToString();
}
protected internal static Dictionary<string, Func<string, string>> actions =
new Dictionary<string, Func<string, string>>()
{
["-"] = (s) => sub(s),
["+"] = (s) => sup(s),
["f"] = (s) => fract(s),
};
static RegexOptions options = RegexOptions.Singleline | RegexOptions.Compiled;
public static string Parse(string input)
{
string pattern = @"\[(\D{1}):(\S\/?\S*?)\]";
var matches = Regex.Matches(input, pattern, options);
StringBuilder result = new StringBuilder(input);
foreach (Match m in matches)
{
result = result.Replace(m.Value, actions[m.Groups[1].Value](m.Groups[2].Value));
}
return result.ToString();
}
}