Began building lexer/parser, implemented greek and basic spacing

This commit is contained in:
2017-05-24 01:51:56 -04:00
parent c588975b86
commit e1e62e466a
12 changed files with 436 additions and 165 deletions

View File

@@ -1 +1,2 @@
# WpfTex
# WpfTex

View File

@@ -1,148 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using LatexEditor.Fonts;
using System.Windows.Media.TextFormatting;
namespace LatexEditor
{
[ContentProperty("Content")]
public class LatexDocument : FrameworkElement
public class LatexDocument
{
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(string), typeof(LatexDocument),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
[Category("LaTeX")]
[Description("LaTeX markup")]
public string Content
{
get => (string)GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public static readonly DependencyProperty FontSizeProperty =
DependencyProperty.Register("FontSize", typeof(double), typeof(LatexDocument),
new FrameworkPropertyMetadata(20d, FrameworkPropertyMetadataOptions.AffectsRender));
[Category("LaTeX")]
[Description("LaTeX font scaling")]
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
protected List<GlyphInfo> Parse(string latex)
{
var x = 0d;
var y = 0d;
var font = CmFont.SerifItalic;
var size = FontSize;
var glyphs = new List<GlyphInfo>(latex.Length);
var state = new Stack<string>();
state.Push("-");
while (latex.Length > 0)
{
var s = state.Peek();
if (s == "cmd")
{
state.Pop();
var m = Regex.Match(latex, @"([a-z]+)(?=[^a-z]|$)", RegexOptions.IgnoreCase);
if (m.Index == 0)
{
var cmd = latex.Substring(0, m.Groups[1].Length);
switch (cmd)
{
case "it":
font = CmFont.SerifItalic;
break;
case "sym":
font = CmFont.Symbols;
break;
case "rm":
font = CmFont.Serif;
break;
case "sup":
size = FontSize * 0.4;
y = FontSize * (1 - 0.4);
break;
case "sub":
size = FontSize * 0.5;
y = -FontSize * 0.5 * 0.5;
break;
default:
continue;
}
latex = latex.Substring(m.Length).TrimStart(' ');
}
else
{
latex = latex.Substring(1);
}
}
else
{
if (latex.StartsWith("\\"))
{
latex = latex.Substring(1);
state.Push("cmd");
}
else
{
var chr = latex[0];
latex = latex.Substring(1);
var gtf = font.GlyphTypeface();
var point = new Point(x, y);
var gi = new GlyphInfo(font, chr, size, point);
x += gtf.AdvanceWidths[gi.Index] * size;
glyphs.Add(gi);
}
}
}
return glyphs;
}
protected override void OnRender(DrawingContext dc)
{
var text = Content;
if (string.IsNullOrEmpty(text)) return;
var glyphInfoList = Parse(text);
var gtfGroups = glyphInfoList.GroupBy(gi => gi.Gtf);
foreach (var gtfGroup in gtfGroups)
{
var gtf = gtfGroup.Key;
var sizeGroups = gtfGroup.GroupBy(gi => gi.Size);
foreach (var sizeGroup in sizeGroups)
{
var size = sizeGroup.Key;
var glyphs = sizeGroup.Select(gi => gi.Index).ToList();
var advanceWidths = new double[glyphs.Count];
var offsets = sizeGroup.Select(gi => gi.Offset).ToList();
var gr = new GlyphRun(gtf, 0, false, size,
glyphs, new Point(0, FontSize), advanceWidths,
offsets, null, null, null, null, null);
dc.DrawGlyphRun(Brushes.Black, gr);
}
}
}
}
}

View File

@@ -34,6 +34,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="JetBrains.Annotations, Version=10.2.1.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.2.1\lib\net\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
@@ -66,12 +69,17 @@
<Compile Include="Fonts\CmFont.cs" />
<Compile Include="Fonts\GlyphInfo.cs" />
<Compile Include="LatexDocument.cs" />
<Compile Include="LatexViewer.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Parser\LatexParser.cs" />
<Compile Include="Parser\Lexer.cs" />
<Compile Include="Parser\Token.cs" />
<Compile Include="Parser\TokenDescriptor.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@@ -87,6 +95,7 @@
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<Resource Include="Fonts\cm\cmmi10.ttf" />
@@ -150,6 +159,7 @@
<Resource Include="Fonts\cmun\cmunvi.ttf" />
<Resource Include="Fonts\cmun\cmunvt.ttf" />
<None Include="Fonts\cmun\Fontmap.CMU" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using LatexEditor.Fonts;
using LatexEditor.Parser;
namespace LatexEditor
{
[ContentProperty("Content")]
public class LatexViewer : FrameworkElement
{
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(string), typeof(LatexViewer),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
[Category("LaTeX")]
[Description("LaTeX markup")]
public string Content
{
get => (string) GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public static readonly DependencyProperty FontSizeProperty =
DependencyProperty.Register("FontSize", typeof(double), typeof(LatexViewer),
new FrameworkPropertyMetadata(20d, FrameworkPropertyMetadataOptions.AffectsRender));
[Category("LaTeX")]
[Description("LaTeX font scaling")]
public double FontSize
{
get => (double) GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
protected List<GlyphInfo> Parse(string latex)
{
var x = 0d;
var y = 0d;
var font = CmFont.SerifItalic;
var size = FontSize;
var glyphs = new List<GlyphInfo>(latex.Length);
var state = new Stack<string>();
state.Push("-");
while (latex.Length > 0)
{
var s = state.Peek();
if (s == "cmd")
{
state.Pop();
var m = Regex.Match(latex, @"([a-z]+)(?=[^a-z]|$)", RegexOptions.IgnoreCase);
if (m.Index == 0)
{
var cmd = latex.Substring(0, m.Groups[1].Length);
switch (cmd)
{
case "it":
font = CmFont.SerifItalic;
break;
case "sym":
font = CmFont.Symbols;
break;
case "rm":
font = CmFont.Serif;
break;
case "sup":
size = FontSize * 0.4;
y = FontSize * (1 - 0.4);
break;
case "sub":
size = FontSize * 0.5;
y = -FontSize * 0.5 * 0.5;
break;
default:
continue;
}
latex = latex.Substring(m.Length).TrimStart(' ');
}
else
{
latex = latex.Substring(1);
}
}
else
{
if (latex.StartsWith("\\"))
{
latex = latex.Substring(1);
state.Push("cmd");
}
else
{
var chr = latex[0];
latex = latex.Substring(1);
var gtf = font.GlyphTypeface();
var point = new Point(x, y);
var gi = new GlyphInfo(font, chr, size, point);
x += gtf.AdvanceWidths[gi.Index] * size;
glyphs.Add(gi);
}
}
}
return glyphs;
}
protected override void OnRender(DrawingContext dc)
{
Debug.WriteLine(dc, nameof(LatexViewer));
Debug.WriteLine(dc.GetType(), nameof(LatexViewer));
if (string.IsNullOrEmpty(Content)) return;
var glyphInfoList = LatexParser.Parse(Content);
var gtfGroups = glyphInfoList.GroupBy(gi => gi.Gtf);
foreach (var gtfGroup in gtfGroups)
{
var gtf = gtfGroup.Key;
var sizeGroups = gtfGroup.GroupBy(gi => gi.Size);
foreach (var sizeGroup in sizeGroups)
{
var size = sizeGroup.Key;
var glyphs = sizeGroup.Select(gi => gi.Index).ToList();
var advanceWidths = new double[glyphs.Count];
var offsets = sizeGroup.Select(gi => gi.Offset).ToList();
var gr = new GlyphRun(gtf, 0, false, size,
glyphs, new Point(0, FontSize), advanceWidths,
offsets, null, null, null, null, null);
dc.DrawGlyphRun(Brushes.Black, gr);
}
}
}
}
}

View File

@@ -7,8 +7,22 @@
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:LatexDocument>
\sym h \it x, y \sym i
</local:LatexDocument>
<local:LatexViewer>
<!-- e^{\pi i}=-1\{\textrm{in the complex plane}-->
A \ \alpha \quad N \ \nu \\
B \ \beta \quad \Xi \ \xi \\
\Gamma \ \gamma \quad O \ \omicron \\
\Delta \ \delta \quad \Pi \ \pi \\
E \ \epsilon \quad P \ \rho \\
Z \ \zeta \quad \Sigma \ \sigma \\
H \ \eta \quad T \ \tau \\
\Theta \ \theta \quad Y \ \upsilon \\
I \ \iota \quad \Phi \ \phi \\
K \ \kappa \quad X \ \chi \\
\Lambda \ \lambda \quad \Psi \ \psi \\
M \ \mu \quad \Omega \ \omega
</local:LatexViewer>
</Grid>
</Window>

View File

@@ -0,0 +1,125 @@
using System.Collections.Generic;
using System.Windows;
using LatexEditor.Fonts;
namespace LatexEditor.Parser
{
public static class LatexParser
{
private static readonly Lexer LatexLexer = new Lexer()
{
new TokenDescriptor("whitespace", @"\s+"),
new TokenDescriptor("escape", @"\\([\#\$\%\^\&_\{\}\~\\])", 1),
new TokenDescriptor("command", @"\\([^\d\s\\]+|[\ ])", 1),
new TokenDescriptor("command", @"[\#\$\%\^\&_]"),
new TokenDescriptor("number", @"\d"),
new TokenDescriptor("open", @"\{"),
new TokenDescriptor("close", @"\}"),
new TokenDescriptor("letter", @"[\S]"), // nearly catch-all for uncaptured symbols
};
private static readonly Dictionary<string, int> greekLetters = new Dictionary<string, int>
{
["alpha"] = 0x03b1,
["beta"] = 0x03b2,
["gamma"] = 0x03b3,
["delta"] = 0x03b4,
["epsilon"] = 0x03b5,
["zeta"] = 0x03b6,
["eta"] = 0x03b7,
["theta"] = 0x03b8,
["iota"] = 0x03b9,
["kappa"] = 0x03ba,
["lambda"] = 0x03bb,
["mu"] = 0x03bc,
["nu"] = 0x03bd,
["xi"] = 0x03be,
["omicron"] = 0x03bf,
["pi"] = 0x03c0,
["rho"] = 0x03c1,
["sigma"] = 0x03c2,
["tau"] = 0x03c4,
["upsilon"] = 0x03c5,
["phi"] = 0x03c6,
["chi"] = 0x03c7,
["psi"] = 0x03c8,
["omega"] = 0x03c9,
["Gamma"] = 0x0393,
["Delta"] = 0x0394,
["Theta"] = 0x0398,
["Lambda"] = 0x039b,
["Xi"] = 0x039e,
["Pi"] = 0x03a0,
["Sigma"] = 0x03a3,
["Phi"] = 0x03a6,
["Psi"] = 0x03a8,
["Omega"] = 0x03a9,
};
private static readonly Dictionary<string, double> spaces = new Dictionary<string, double>
{
[","] = 0.16666,
["!"] = -0.16666,
[">"] = .3,
[":"] = .3,
[";"] = .4,
[" "] = .5,
["quad"] = 1,
["qquad"] = 4,
};
public static IEnumerable<GlyphInfo> Parse(string latex) => Parse(LatexLexer.Tokenize(latex));
private static IEnumerable<GlyphInfo> Parse(IEnumerable<Token> tokens)
{
var tokenQ = new Queue<Token>(tokens);
var glyphs = new List<GlyphInfo>();
const double size = 20;
var x = 0d;
var y = 0d;
void AddGlyph(CmFont font, int c)
{
glyphs.Add(new GlyphInfo(font, c, size, new Point(x * size, y * size)));
var gtf = font.GlyphTypeface();
var idx = gtf.CharacterToGlyphMap[c];
x += gtf.AdvanceWidths[idx];
}
while (tokenQ.Count > 0)
{
var token = tokenQ.Dequeue();
var val = token.Value;
switch (token.TokenName)
{
case "letter":
AddGlyph(CmFont.SerifItalic, val[0]);
break;
case "number":
AddGlyph(CmFont.Serif, val[0]);
break;
case "command":
if (greekLetters.ContainsKey(val))
AddGlyph(CmFont.SerifItalic, greekLetters[val]);
else if (spaces.ContainsKey(val))
x += spaces[val];
else
foreach (var c in token.CapturedString)
AddGlyph(CmFont.SerifItalic, c);
break;
case "escape":
if (val == "\\")
{
x = 0;
y -= 1;
}
break;
}
}
return glyphs;
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
namespace LatexEditor.Parser
{
public class Lexer : IEnumerable<TokenDescriptor>
{
public List<TokenDescriptor> TokenDescriptors { get; }
public Lexer() : this(new List<TokenDescriptor>())
{
}
public Lexer(List<TokenDescriptor> tokens)
{
TokenDescriptors = tokens;
}
public IEnumerable<Token> Tokenize(string text)
{
while (!string.IsNullOrEmpty(text))
{
Token cap = null;
foreach (var ltd in TokenDescriptors)
if (ltd.TryBreakString(ref text, out cap))
break;
if (cap == null)
text = text.Substring(1);
else
yield return cap;
}
}
public void Add(TokenDescriptor td) => TokenDescriptors.Add(td);
public IEnumerator<TokenDescriptor> GetEnumerator()
{
return TokenDescriptors.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) TokenDescriptors).GetEnumerator();
}
}
}

View File

@@ -0,0 +1,21 @@
namespace LatexEditor.Parser
{
public class Token
{
public string TokenName { get; }
public string Value { get; }
public string CapturedString { get; }
public Token(string tokenName, string value, string capturedString)
{
TokenName = tokenName;
Value = value;
CapturedString = capturedString;
}
public override string ToString()
{
return $"\"{Value}\" ({TokenName})";
}
}
}

View File

@@ -0,0 +1,34 @@
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace LatexEditor.Parser
{
public class TokenDescriptor
{
public string TokenName { get; }
public int CaptureGroup { get; }
public Regex Regex { get; }
public TokenDescriptor(string tokenName, [RegexPattern, NotNull] string pattern, int captureGroup = 0)
{
TokenName = tokenName;
CaptureGroup = captureGroup;
Regex = new Regex("^" + pattern);
}
public bool TryBreakString(ref string text, out Token capture)
{
var match = Regex.Match(text);
if (!match.Success)
{
capture = null;
return false;
}
capture = new Token(TokenName, match.Groups[CaptureGroup].Value, match.Value);
text = text.Substring(match.Length);
return true;
}
}
}

View File

@@ -8,10 +8,10 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace LatexEditor.Properties
{
namespace LatexEditor.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
@@ -22,48 +22,40 @@ namespace LatexEditor.Properties
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("LatexEditor.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set
{
set {
resourceCulture = value;
}
}

View File

@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
<Profile TokenName="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="JetBrains.Annotations" version="10.2.1" targetFramework="net452" />
</packages>