Skip to content

Commit

Permalink
(#63) TexEnvironment: convert to a public record
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 7, 2023
1 parent 9901e61 commit b19edf9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 107 deletions.
7 changes: 4 additions & 3 deletions src/WpfMath/Atoms/StyledAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public Atom WithPreviousAtom(DummyAtom? previousAtom) =>

protected override Box CreateBoxCore(TexEnvironment environment)
{
var newEnvironment = environment.Clone();
if (this.Foreground != null)
newEnvironment.Foreground = this.Foreground;
var newEnvironment = environment with
{
Foreground = this.Foreground ?? environment.Foreground
};
var childBox = this.RowAtom.CreateBox(newEnvironment);
if (Background != null)
childBox.Background = Background;
Expand Down
132 changes: 28 additions & 104 deletions src/WpfMath/TexEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,39 @@
using System.Windows.Media;

namespace WpfMath
namespace WpfMath;

/// <summary>Specifies current graphical parameters used to create boxes.</summary>
public sealed record TexEnvironment(
TexStyle Style,
ITeXFont MathFont,
ITeXFont TextFont,
Brush? Background = null,
Brush? Foreground = null)
{
/// <summary>Specifies current graphical parameters used to create boxes.</summary>
public sealed class TexEnvironment
{
// ID of font that was last used.
private int lastFontId = TexFontUtilities.NoFontId;

internal TexEnvironment(
TexStyle style,
ITeXFont mathFont,
ITeXFont textFont,
Brush? background = null,
Brush? foreground = null)
{
if (style == TexStyle.Display || style == TexStyle.Text ||
style == TexStyle.Script || style == TexStyle.ScriptScript)
this.Style = style;
else
this.Style = TexStyle.Display;

this.MathFont = mathFont;
TextFont = textFont;
this.Background = background;
this.Foreground = foreground;
}

internal TexStyle Style
{
get;
private set;
}

internal ITeXFont MathFont
{
get;
private set;
}

internal ITeXFont TextFont { get; }

internal Brush? Background
{
get;
set;
}
// ID of font that was last used.
private int lastFontId = TexFontUtilities.NoFontId;

internal Brush? Foreground
{
get;
set;
}

internal int LastFontId
{
get { return this.lastFontId == TexFontUtilities.NoFontId ? this.MathFont.GetMuFontId() : this.lastFontId; }
set { this.lastFontId = value; }
}

internal TexEnvironment GetCrampedStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = (int)this.Style % 2 == 1 ? this.Style : this.Style + 1;
return newEnvironment;
}

internal TexEnvironment GetNumeratorStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = this.Style + 2 - 2 * ((int)this.Style / 6);
return newEnvironment;
}
internal int LastFontId
{
get { return this.lastFontId == TexFontUtilities.NoFontId ? this.MathFont.GetMuFontId() : this.lastFontId; }
set { this.lastFontId = value; }
}

internal TexEnvironment GetDenominatorStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = (TexStyle)(2 * ((int)this.Style / 2) + 1 + 2 - 2 * ((int)this.Style / 6));
return newEnvironment;
}
internal TexEnvironment GetCrampedStyle() =>
this with { Style = (int)this.Style % 2 == 1 ? this.Style : this.Style + 1 };

internal TexEnvironment GetRootStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = TexStyle.ScriptScript;
return newEnvironment;
}
internal TexEnvironment GetNumeratorStyle() =>
this with { Style = this.Style + 2 - 2 * ((int)this.Style / 6) };

internal TexEnvironment GetSubscriptStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = (TexStyle)(2 * ((int)this.Style / 4) + 4 + 1);
return newEnvironment;
}
internal TexEnvironment GetDenominatorStyle() =>
this with { Style = (TexStyle)(2 * ((int)this.Style / 2) + 1 + 2 - 2 * ((int)this.Style / 6)) };

internal TexEnvironment GetSuperscriptStyle()
{
var newEnvironment = Clone();
newEnvironment.Style = (TexStyle)(2 * ((int)this.Style / 4) + 4 + ((int)this.Style % 2));
return newEnvironment;
}
internal TexEnvironment GetRootStyle() =>
this with { Style = TexStyle.ScriptScript };

internal TexEnvironment Clone()
{
return new TexEnvironment(Style, MathFont, TextFont, Background, Foreground);
}
internal TexEnvironment GetSubscriptStyle() =>
this with { Style = (TexStyle)(2 * ((int)this.Style / 4) + 4 + 1) };

internal void Reset()
{
this.Background = null;
this.Foreground = null;
}
}
internal TexEnvironment GetSuperscriptStyle() =>
this with { Style = (TexStyle)(2 * ((int)this.Style / 4) + 4 + (int)this.Style % 2) };
}

0 comments on commit b19edf9

Please sign in to comment.