Skip to content

Commit

Permalink
Initial changes to make Atoms structs (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Apr 6, 2018
1 parent d33430b commit bca8047
Show file tree
Hide file tree
Showing 25 changed files with 101 additions and 173 deletions.
56 changes: 22 additions & 34 deletions src/WpfMath/AccentedAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,63 @@

namespace WpfMath
{
// Atom representing base atom with accent above it.
internal class AccentedAtom : Atom
/// <summary>Atom representing base atom with accent above it.</summary>
internal readonly struct AccentedAtom : IAtom
{
public AccentedAtom(Atom baseAtom, string accentName, SourceSpan source)
public TexAtomType Type => TexAtomType.Ordinary;
public TexAtomType GetLeftType() => Type;
public TexAtomType GetRightType() => Type;

public SourceSpan Source { get; }

public AccentedAtom(IAtom baseAtom, string accentName, SourceSpan source)
{
this.BaseAtom = baseAtom;
this.AccentAtom = SymbolAtom.GetAtom(accentName, source);
this.Source = source;

if (this.AccentAtom.Type != TexAtomType.Accent)
throw new ArgumentException("The specified symbol name is not an accent.", "accent");
}

public AccentedAtom(Atom baseAtom, TexFormula accent)
public AccentedAtom(IAtom baseAtom, TexFormula accent)
{
var rootSymbol = accent.RootAtom as SymbolAtom;
var rootSymbol = accent.RootAtom as SymbolAtom?;
if (rootSymbol == null)
throw new ArgumentException("The formula for the accent is not a single symbol.", "accent");
this.AccentAtom = (SymbolAtom)rootSymbol;
this.AccentAtom = rootSymbol.Value;

if (this.AccentAtom.Type != TexAtomType.Accent)
throw new ArgumentException("The specified symbol name is not an accent.", "accent");
}

private AccentedAtom()
{
this.BaseAtom = null;
this.Source = null;
}

// Atom over which accent symbol is placed.
public Atom BaseAtom
{
get;
private set;
}
/// <summary>Atom over which accent symbol is placed.</summary>
public IAtom BaseAtom { get; }

// Atom representing accent symbol to place over base atom.
public SymbolAtom AccentAtom
{
get;
private set;
}

public override Atom Copy()
{
return CopyTo(new AccentedAtom()
{
BaseAtom = BaseAtom?.Copy(),
AccentAtom = (SymbolAtom)AccentAtom?.Copy()
});
}
public SymbolAtom AccentAtom { get; }

protected override Box CreateBoxCore(TexEnvironment environment)
public Box CreateBox(TexEnvironment environment)
{
CharSymbol GetBaseChar()
ICharSymbol GetBaseChar(IAtom baseAtom)
{
var baseAtom = BaseAtom;
while (baseAtom is AccentedAtom a)
{
baseAtom = a.BaseAtom;
}

return baseAtom as CharSymbol;
return baseAtom as ICharSymbol;
}

var texFont = environment.MathFont;
var style = environment.Style;

// Create box for base atom.
var baseBox = this.BaseAtom == null ? StrutBox.Empty : this.BaseAtom.CreateBox(environment.GetCrampedStyle());
var baseCharFont = GetBaseChar()?.GetCharFont(texFont);
var baseCharFont = GetBaseChar(BaseAtom)?.GetCharFont(texFont);
var skew = baseCharFont == null ? 0.0 : texFont.GetSkew(baseCharFont, style);

// Find character of best scale for accent symbol.
Expand Down
54 changes: 0 additions & 54 deletions src/WpfMath/Atom.cs

This file was deleted.

30 changes: 12 additions & 18 deletions src/WpfMath/BigDelimeterAtom.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
namespace WpfMath
{
// Atom representing big delimeter (e.g. brackets).
internal class BigDelimeterAtom : Atom
internal readonly struct BigDelimeterAtom : IAtom
{
public BigDelimeterAtom(Atom delimeterAtom, int size)
public TexAtomType Type => TexAtomType.Ordinary;
public TexAtomType GetLeftType() => Type;
public TexAtomType GetRightType() => Type;

public SourceSpan Source { get; }

public BigDelimeterAtom(IAtom delimeterAtom, int size)
{
this.DelimeterAtom = delimeterAtom;
this.Size = size;
this.Source = null;
}

public Atom DelimeterAtom
{
get;
private set;
}
public IAtom DelimeterAtom { get; }

public int Size
{
get;
private set;
}

public override Atom Copy()
{
return CopyTo(new BigDelimeterAtom(DelimeterAtom.Copy(), Size));
}
public int Size { get; }

protected override Box CreateBoxCore(TexEnvironment environment)
public Box CreateBox(TexEnvironment environment)
{
// TODO
var resultBox = (Box)null; // DelimiterFactory.CreateBox(this.DelimeterAtom, this.Size, environment);
Expand Down
4 changes: 2 additions & 2 deletions src/WpfMath/BigOperatorAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace WpfMath
{
// Atom representing big operator with optional limits.
internal class BigOperatorAtom : Atom
internal readonly struct BigOperatorAtom : IAtom
{
private static Box ChangeWidth(Box box, double maxWidth)
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public override Atom Copy()
return CopyTo(new BigOperatorAtom(BaseAtom?.Copy(), LowerLimitAtom?.Copy(), UpperLimitAtom?.Copy(), UseVerticalLimits));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/CharAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override Atom Copy()
return CopyTo(new CharAtom(Source, TextStyle));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var font = GetStyledFont(environment);
var charInfo = GetCharInfo(font, environment.Style);
Expand Down
25 changes: 5 additions & 20 deletions src/WpfMath/CharSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
namespace WpfMath
{
// Atom representing single character that can be marked as text symbol.
internal abstract class CharSymbol : Atom
/// <summary>Atom representing single character that can be marked as text symbol.</summary>
internal interface ICharSymbol : IAtom
{
public CharSymbol()
{
this.IsTextSymbol = false;
}

public bool IsTextSymbol
{
get;
set;
}

protected override Atom CopyTo(Atom atom)
{
((CharSymbol)atom).IsTextSymbol = IsTextSymbol;
return base.CopyTo(atom);
}
bool IsTextSymbol { get; }

/// <summary>Returns the preferred font to render this character.</summary>
public virtual ITeXFont GetStyledFont(TexEnvironment environment) => environment.MathFont;
ITeXFont GetStyledFont(TexEnvironment environment) => environment.MathFont;

public abstract CharFont GetCharFont(ITeXFont texFont);
CharFont GetCharFont(ITeXFont texFont);
}
}
2 changes: 1 addition & 1 deletion src/WpfMath/DummyAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CharFont GetCharFont(ITeXFont texFont)
return ((CharSymbol)this.Atom).GetCharFont(texFont);
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
if (this.IsTextSymbol)
((CharSymbol)this.Atom).IsTextSymbol = true;
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/FencedAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override Atom Copy()
return CopyTo(new FencedAtom(BaseAtom?.Copy(), (SymbolAtom)LeftDelimeter?.Copy(), (SymbolAtom)RightDelimeter?.Copy()));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/FixedCharAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override CharFont GetCharFont(ITeXFont texFont)
return this.CharFont;
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var charInfo = environment.MathFont.GetCharInfo(this.CharFont, environment.Style);
return new CharBox(environment, charInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/FractionAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override Atom Copy()
return CopyTo(atom);
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
Expand Down
19 changes: 19 additions & 0 deletions src/WpfMath/IAtom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace WpfMath
{
/// <summary>Atom (smallest unit) of TexFormula.</summary>
internal interface IAtom
{
TexAtomType Type { get; }

/// <summary>Gets type of leftmost child item.</summary>
TexAtomType GetLeftType();

/// <summary>Gets type of leftmost child item.</summary>
TexAtomType GetRightType();

/// <summary>Source of this atom. May be <c>null</c>.</summary>
SourceSpan Source { get; }

Box CreateBox(TexEnvironment environment);
}
}
2 changes: 1 addition & 1 deletion src/WpfMath/OverUnderDelimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override Atom Copy()
});
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
// Create boxes for base, delimeter, and script atoms.
var baseBox = this.BaseAtom == null ? StrutBox.Empty : this.BaseAtom.CreateBox(environment);
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/OverlinedAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override Atom Copy()
return CopyTo(new OverlinedAtom(BaseAtom?.Copy()));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
// Create box for base atom, in cramped style.
var baseBox = this.BaseAtom == null ? StrutBox.Empty : this.BaseAtom.CreateBox(environment.GetCrampedStyle());
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/PhantomAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override Atom Copy()
return CopyTo(new PhantomAtom(RowAtom?.Copy(), useWidth, useHeight, useDepth) { PreviousAtom = (DummyAtom)PreviousAtom?.Copy() });
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var resultBox = this.RowAtom.CreateBox(environment);
return new StrutBox((this.useWidth ? resultBox.Width : 0), (this.useHeight ? resultBox.Height : 0),
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/Radical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override Atom Copy()
return CopyTo(new Radical(BaseAtom?.Copy(), DegreeAtom?.Copy()));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/RowAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override Atom Copy()
return CopyTo(atom);
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
// Create result box.
var resultBox = new HorizontalBox(environment.Foreground, environment.Background);
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/ScriptsAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override Atom Copy()
return CopyTo(new ScriptsAtom(BaseAtom?.Copy(), SubscriptAtom?.Copy(), SuperscriptAtom?.Copy()));
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/SpaceAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override Atom Copy()
return CopyTo(atom);
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
if (isHardSpace)
return new StrutBox(environment.MathFont.GetSpace(environment.Style), 0, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/StyledAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override Atom Copy()
return CopyTo(new StyledAtom(RowAtom?.Copy(), Background, Foreground) { PreviousAtom = (DummyAtom)PreviousAtom?.Copy() });
}

protected override Box CreateBoxCore(TexEnvironment environment)
protected override Box CreateBox(TexEnvironment environment)
{
var newEnvironment = environment.Clone();
if (this.Background != null)
Expand Down
Loading

0 comments on commit bca8047

Please sign in to comment.