From b19edf9579813b1ac2e5430155fb10a2aff49318 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Mon, 2 Jan 2023 23:08:54 +0100 Subject: [PATCH] (#63) TexEnvironment: convert to a public record --- src/WpfMath/Atoms/StyledAtom.cs | 7 +- src/WpfMath/TexEnvironment.cs | 132 +++++++------------------------- 2 files changed, 32 insertions(+), 107 deletions(-) diff --git a/src/WpfMath/Atoms/StyledAtom.cs b/src/WpfMath/Atoms/StyledAtom.cs index 938c12ca..e8b632b5 100644 --- a/src/WpfMath/Atoms/StyledAtom.cs +++ b/src/WpfMath/Atoms/StyledAtom.cs @@ -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; diff --git a/src/WpfMath/TexEnvironment.cs b/src/WpfMath/TexEnvironment.cs index 49f6b42b..4f7dede6 100644 --- a/src/WpfMath/TexEnvironment.cs +++ b/src/WpfMath/TexEnvironment.cs @@ -1,115 +1,39 @@ using System.Windows.Media; -namespace WpfMath +namespace WpfMath; + +/// Specifies current graphical parameters used to create boxes. +public sealed record TexEnvironment( + TexStyle Style, + ITeXFont MathFont, + ITeXFont TextFont, + Brush? Background = null, + Brush? Foreground = null) { - /// Specifies current graphical parameters used to create boxes. - 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) }; }