Skip to content

Commit

Permalink
(#63) TexEnvironment: move to the Shared assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 7, 2023
1 parent a13b407 commit e443c9c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 62 deletions.
3 changes: 2 additions & 1 deletion WpfMath.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bomless/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=roundtrippable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=stringified/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Windows.Media;
using WpfMath.Rendering;

namespace WpfMath;

Expand All @@ -7,8 +7,8 @@ public sealed record TexEnvironment(
TexStyle Style,
ITeXFont MathFont,
ITeXFont TextFont,
Brush? Background = null,
Brush? Foreground = null)
IPlatformBrush? Background = null,
IPlatformBrush? Foreground = null)
{
// ID of font that was last used.
private int lastFontId = TexFontUtilities.NoFontId;
Expand Down
11 changes: 11 additions & 0 deletions src/WpfMath.Tests/ApprovalTestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ open Newtonsoft.Json.Serialization
open WpfMath
open WpfMath.Atoms
open WpfMath.Fonts
open WpfMath.Rendering

type private BomlessFileWriter(data: string, ?extensionWithoutDot: string) =
inherit ApprovalTextWriter(data, defaultArg extensionWithoutDot "txt")
Expand Down Expand Up @@ -92,13 +93,23 @@ type private UniversalDoubleConverter() =
let stringified = value.ToString("0.0###############", CultureInfo.InvariantCulture)
writer.WriteRawValue stringified

type private WpfBrushConverter() =
inherit ReadOnlyJsonConverter<WpfBrush>()
override _.WriteJson(writer: JsonWriter, value: WpfBrush, _: JsonSerializer) =
let stringified =
match value.Get() with
| null -> null
| _ -> value.Get().ToString()
writer.WriteValue stringified

let private jsonSettings = JsonSerializerSettings(ContractResolver = InnerPropertyContractResolver(),
Formatting = Formatting.Indented,
Converters = [|
StringEnumConverter()
GlyphTypefaceConverter()
UniversalDoubleConverter()
WpfGlyphTypefaceConverter()
WpfBrushConverter()
|])

let private serialize o =
Expand Down
75 changes: 39 additions & 36 deletions src/WpfMath/Atoms/StyledAtom.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
using System.Windows.Media;
using WpfMath.Boxes;
using WpfMath.Rendering;

namespace WpfMath.Atoms
namespace WpfMath.Atoms;

/// <summary>Atom specifying graphical style.</summary>
internal record StyledAtom : Atom, IRow
{
// Atom specifying graphical style.
internal record StyledAtom : Atom, IRow
public StyledAtom(
SourceSpan? source,
Atom? atom,
IPlatformBrush? backgroundColor,
IPlatformBrush? foregroundColor)
: base(source)
{
public StyledAtom(SourceSpan? source, Atom? atom, Brush? backgroundColor, Brush? foregroundColor)
: base(source)
{
this.RowAtom = new RowAtom(source, atom);
this.Background = backgroundColor;
this.Foreground = foregroundColor;
}
this.RowAtom = new RowAtom(source, atom);
this.Background = backgroundColor;
this.Foreground = foregroundColor;
}

// RowAtom to which colors are applied.
public RowAtom RowAtom { get; init; }
// RowAtom to which colors are applied.
public RowAtom RowAtom { get; init; }

public Brush? Background { get; init; }
public IPlatformBrush? Background { get; init; }

public Brush? Foreground { get; init; }
public IPlatformBrush? Foreground { get; init; }

public Atom WithPreviousAtom(DummyAtom? previousAtom) =>
this with { RowAtom = (RowAtom) RowAtom.WithPreviousAtom(previousAtom) };
public Atom WithPreviousAtom(DummyAtom? previousAtom) =>
this with { RowAtom = (RowAtom) RowAtom.WithPreviousAtom(previousAtom) };

protected override Box CreateBoxCore(TexEnvironment environment)
{
var newEnvironment = environment with
{
Foreground = this.Foreground ?? environment.Foreground
};
var childBox = this.RowAtom.CreateBox(newEnvironment);
if (Background != null)
childBox.Background = Background;
return childBox;
}

public override TexAtomType GetLeftType()
protected override Box CreateBoxCore(TexEnvironment environment)
{
var newEnvironment = environment with
{
return this.RowAtom.GetLeftType();
}
Foreground = this.Foreground ?? environment.Foreground
};
var childBox = this.RowAtom.CreateBox(newEnvironment);
if (Background != null)
childBox.Background = Background;
return childBox;
}

public override TexAtomType GetRightType()
{
return this.RowAtom.GetRightType();
}
public override TexAtomType GetLeftType()
{
return this.RowAtom.GetLeftType();
}

public override TexAtomType GetRightType()
{
return this.RowAtom.GetRightType();
}
}
7 changes: 3 additions & 4 deletions src/WpfMath/Boxes/Box.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media;
using WpfMath.Rendering;

namespace WpfMath.Boxes
Expand All @@ -21,7 +20,7 @@ protected Box()
{
}

protected Box(Brush? foreground, Brush? background)
protected Box(IPlatformBrush? foreground, IPlatformBrush? background)
{
this.children = new List<Box>();
this.childrenReadOnly = new ReadOnlyCollection<Box>(this.children);
Expand All @@ -40,13 +39,13 @@ public SourceSpan? Source
set;
}

public Brush? Foreground
public IPlatformBrush? Foreground
{
get;
set;
}

public Brush? Background
public IPlatformBrush? Background
{
get;
set;
Expand Down
4 changes: 2 additions & 2 deletions src/WpfMath/Boxes/CharBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace WpfMath.Boxes
{
// Box representing single character.
/// <summary>Box representing single character.</summary>
internal class CharBox : Box
{
public CharBox(TexEnvironment environment, CharInfo charInfo)
Expand All @@ -24,7 +24,7 @@ public CharInfo Character

public override void RenderTo(IElementRenderer renderer, double x, double y)
{
var color = this.Foreground ?? Brushes.Black;
var color = this.Foreground.ToWpf() ?? Brushes.Black;
renderer.RenderCharacter(Character, x, y, color);
}

Expand Down
5 changes: 2 additions & 3 deletions src/WpfMath/Boxes/HorizontalBox.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Windows.Media;
using WpfMath.Rendering;

namespace WpfMath.Boxes
{
// Box containing horizontal stack of child boxes.
/// <summary>Box containing horizontal stack of child boxes.</summary>
internal class HorizontalBox : Box
{
private double childBoxesTotalWidth = 0.0;
Expand Down Expand Up @@ -38,7 +37,7 @@ public HorizontalBox(Box box)
this.Add(box);
}

public HorizontalBox(Brush? foreground, Brush? background)
public HorizontalBox(IPlatformBrush? foreground, IPlatformBrush? background)
: base(foreground, background)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/Boxes/HorizontalRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public HorizontalRule(TexEnvironment environment, double thickness, double width

public override void RenderTo(IElementRenderer renderer, double x, double y)
{
var color = this.Foreground ?? Brushes.Black;
var color = this.Foreground.ToWpf() ?? Brushes.Black;
var rectangle = new Rect(x, y - this.Height, this.Width, this.Height);
renderer.RenderRectangle(rectangle, color);
}
Expand Down
3 changes: 2 additions & 1 deletion src/WpfMath/Controls/FormulaControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Media;
using WpfMath.Boxes;
using WpfMath.Exceptions;
using WpfMath.Rendering;

namespace WpfMath.Controls
{
Expand Down Expand Up @@ -159,7 +160,7 @@ private void Render()
&& source.Start < selectionEnd
&& box is CharBox)
{
box.Background = selectionBrush;
box.Background = WpfBrush.FromBrush(selectionBrush);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/WpfMath/Rendering/BrushExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Media;

namespace WpfMath.Rendering;

internal static class BrushExtensions
{
public static Brush? ToWpf(this IPlatformBrush? brush) => ((WpfBrush?)brush)?.Get();
public static IPlatformBrush? ToPlatform(this Brush? brush) => brush == null ? null : WpfBrush.FromBrush(brush);
}
5 changes: 2 additions & 3 deletions src/WpfMath/Rendering/WpfBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ private WpfBrush(Brush brush) : base(brush)
{
}

public static WpfBrush FromBrush(Brush value) =>
new WpfBrush(value);
public static WpfBrush FromBrush(Brush value) => new(value);

public static WpfBrush FromColor(RgbaColor value) =>
new WpfBrush(
new(
new SolidColorBrush(
Color.FromArgb(value.A, value.R, value.G, value.B)));
}
2 changes: 1 addition & 1 deletion src/WpfMath/Rendering/WpfElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void RenderBackground(Box box, double x, double y)
if (box.Background != null)
{
_targetContext.DrawRectangle(
box.Background,
box.Background.ToWpf(),
null,
new Rect(_scale * x, _scale * (y - box.Height),
_scale * box.TotalWidth,
Expand Down
11 changes: 6 additions & 5 deletions src/WpfMath/TexFormula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using WpfMath.Atoms;
using WpfMath.Boxes;
using WpfMath.Fonts;
using WpfMath.Rendering;

namespace WpfMath
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public TexRenderer GetRenderer(TexStyle style, // TODO: Revise internal usages o
{
var mathFont = new DefaultTexFont(WpfMathFontProvider.Instance, scale);
var textFont = systemTextFontName == null ? (ITeXFont)mathFont : GetSystemFont(systemTextFontName, scale);
var environment = new TexEnvironment(style, mathFont, textFont, background, foreground);
var environment = new TexEnvironment(style, mathFont, textFont, WpfBrush.FromBrush(background), WpfBrush.FromBrush(foreground));
return new TexRenderer(CreateBox(environment), scale);
}

Expand Down Expand Up @@ -77,23 +78,23 @@ public void SetForeground(Brush brush)
{
if (this.RootAtom is StyledAtom sa)
{
this.RootAtom = sa with { Foreground = brush };
this.RootAtom = sa with { Foreground = brush.ToPlatform() };
}
else
{
this.RootAtom = new StyledAtom(this.RootAtom?.Source, this.RootAtom, null, brush);
RootAtom = new StyledAtom(RootAtom?.Source, RootAtom, null, brush.ToPlatform());
}
}

public void SetBackground(Brush brush)
{
if (this.RootAtom is StyledAtom sa)
{
this.RootAtom = sa with { Background = brush };
this.RootAtom = sa with { Background = brush.ToPlatform() };
}
else
{
this.RootAtom = new StyledAtom(this.RootAtom?.Source, this.RootAtom, brush, null);
this.RootAtom = new StyledAtom(this.RootAtom?.Source, this.RootAtom, brush.ToPlatform(), null);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
using WpfMath.Colors;
using WpfMath.Exceptions;
using WpfMath.Parsers;
using WpfMath.Rendering;
#if NET452
using WpfMath.Utils;
#endif

namespace WpfMath
{
Expand Down Expand Up @@ -547,7 +550,7 @@ private TexFormula ReadScript(

return new Tuple<AtomAppendMode, Atom?>(
AtomAppendMode.Add,
new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color)));
new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color).ToPlatform())); // TODO[#63]: Should read platform brush from color
}
case "colorbox":
{
Expand All @@ -559,7 +562,8 @@ private TexFormula ReadScript(

return new Tuple<AtomAppendMode, Atom?>(
AtomAppendMode.Add,
new StyledAtom(source, bodyFormula.RootAtom, new SolidColorBrush(color), null));
new StyledAtom(source, bodyFormula.RootAtom, new SolidColorBrush(color).ToPlatform(), null));
// TODO[#63]: ↑ Should read platform brush from color
}
}

Expand Down

0 comments on commit e443c9c

Please sign in to comment.