Skip to content

Commit

Permalink
Use CharAtoms to render all text (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 3, 2018
1 parent 83e4bea commit cefd15d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/WpfMath.Tests/ParserTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ let ``Delimiter with scripts should be parsed properly`` () =
<| @"\left(2+2\right)_a^b"
<| (formula <| scripts (fenced (openBrace "lbrack") ``2+2`` (closeBrace "rbrack")) (char 'a') (char 'b'))

let ``\text doesn't create any SymbolAtoms``() =
assertParseResult
<| @"\text{2+2}"
<| (formula <| row [char '2'; char '+'; char '2'])

[<Fact>]
let ``\sqrt{} should throw a TexParseException``() =
assertParseThrows<TexParseException> @"\sqrt{}"
Expand Down
13 changes: 6 additions & 7 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static bool IsWhiteSpace(char ch)
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
}

private static bool ShouldSkipWhiteSpace(string style) => style != "text";
private static bool ShouldSkipWhiteSpace(string style) => style != TexUtilities.TextStyleName;

public TexFormula Parse(string value, string textStyle = null)
{
Expand Down Expand Up @@ -232,7 +232,7 @@ private TexFormula Parse(string value, ref int position, bool allowClosingDelimi
formula,
value,
ref position,
ConvertCharacter(formula, value, ref position, ch),
ConvertCharacter(formula, ref position, ch),
skipWhiteSpace);
formula.Add(scriptsAtom);
}
Expand Down Expand Up @@ -604,15 +604,15 @@ private Atom AttachScripts(TexFormula formula, string value, ref int position, A
}
}

private Atom ConvertCharacter(TexFormula formula, string value, ref int position, char character)
private Atom ConvertCharacter(TexFormula formula, ref int position, char character)
{
position++;
if (IsSymbol(character))
if (IsSymbol(character) && formula.TextStyle != TexUtilities.TextStyleName)
{
// Character is symbol.
var symbolName = symbols.ElementAtOrDefault(character);
if (string.IsNullOrEmpty(symbolName))
throw new TexParseException("Unknown character : '" + character.ToString() + "'");
throw new TexParseException($"Unknown character : '{character}'");

try
{
Expand All @@ -626,9 +626,8 @@ private Atom ConvertCharacter(TexFormula formula, string value, ref int position
+ (string)symbolName + "'!", e);
}
}
else
else // Character is alpha-numeric or should be rendered as text.
{
// Character is alpha-numeric.
return new CharAtom(character, formula.TextStyle);
}
}
Expand Down

0 comments on commit cefd15d

Please sign in to comment.