dea*_*ces 4 c# wpf superscript subscript winforms
我希望能够在 Windows 窗体或 WPF 窗口的标题栏中显示下标文本。这样做的原因是简单的。我们项目组写了一个分子编辑器:
我们不只是显示其名称“ACME”,而是希望显示如下内容:
ACME - 编辑 C 6 H 12 Cl
文本带有下标(也可能带有上标)的位置以及控件是否显示在 Windows 窗体或 WPF 主机中。
最初的问题是询问如何在表单的标题中插入 RichTextBox,以显示化学式。
当然可以做到:您可以查看DwmExtendFrameIntoClientArea和此处的文档:Custom Window Frame Using DWM和许多SO 问题。
但这里有点过头了。有一个更简单的替代方法:利用现有的 Unicode SubScript 符号(它是一个 Unicode 类别)来重现公式。
这些是印度-阿拉伯数字的基本 SubScript 和 SuperScript Unicode CodePoints:
char[] subScriptNumbers = {
'\u2080', '\u2081', '\u2082', '\u2083', '\u2084',
'\u2085', '\u2086', '\u2087', '\u2088', '\u2089'
};
char[] superScriptNumbers = {
'\u2070', '\u00B9', '\u00B2', '\u00B3', '\u2074',
'\u2075', '\u2076', '\u2077', '\u2078', '\u2079'
};
Run Code Online (Sandbox Code Playgroud)
正如评论中所暗示的,简单的公式:C6H12Cl可以转换为C?H??Cl,将数字映射到 SubScript 范围内的相应 Unicode 值。例如:
this.Text = string.Concat("C6H12Cl".Select(c => char.IsDigit(c) ? subScriptNumbers[c-48] : c));
Run Code Online (Sandbox Code Playgroud)
或者,由于 SubScript CodePoint 是连续的(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();
Run Code Online (Sandbox Code Playgroud)
由于似乎有人感兴趣,我提出了一个稍微复杂一点的解析器(使用相同的逻辑),以生成不同类型的公式:
此处显示的类可以使用简单的表示法(类似于维基百科使用的标记的表示法)转换 SubScript/SuperScript 数字或字母序列:
SuperScript: [+:symbols] A[+:12] => A¹²
SubScript: [-:symbols] A[-:12] => A??
Fraction: [f:symbols/symbols] A·[f:x/12] => A·????
Run Code Online (Sandbox Code Playgroud)
例如:
string formula = "N[+:(x+2)] · H[+:3] · ?Log[-:e] + ?· [f:n11/x]";
// Or this.Text, in WinForms
this.Title = UniSubSup.Parse(formula);
Run Code Online (Sandbox Code Playgroud)
将打印:
N???²?·H³·?Log? + ?·?¹¹??注意1:
当 标记包含空格时,它被跳过:因此[+:(x+2)]被解析,而[+:(x + 2)]不是(如果这些括号应该被忽略)。
注2:
我没有包括所有字母,因为并非所有字体都支持 SubScript 和 SuperScript 类别中的所有 CodePoint。相对常见的下标n( \u2099)(例如 Log n)在大多数字体类型中不可用(因为子/超级脚本是通过不同方式生成的)。
一些(极少数)字体确实有这些字形。像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';
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),
};
internal static string sub(string s) =>
s.Aggregate(new StringBuilder(), (sb, c) => sb.Append(subScripts[c])).ToString();
internal static string sup(string s) =>
s.Aggregate(new StringBuilder(), (sb, c) => sb.Append(superScripts[c])).ToString();
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();
}
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();
}
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'
};
}
Run Code Online (Sandbox Code Playgroud)