From 2e2514b000f3bb5ef9b984d15ffc3787b7f30878 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Thu, 12 Jan 2023 00:18:31 +0100 Subject: [PATCH] (#63) WpfMath: rearrange the remaining types --- .../Rendering/GeometryHelper.cs | 0 .../Rendering/TeXFormulaExtensions.cs | 2 +- .../GeometryElementRendererTests.fs | 2 +- src/WpfMath/Fonts/WpfSystemFont.cs | 132 +++++++++++++++++ ...ushExtensions.cs => WpfBrushExtensions.cs} | 2 +- ...xtensions.cs => WpfRectangleExtensions.cs} | 2 +- src/WpfMath/SystemFont.cs | 134 ------------------ 7 files changed, 136 insertions(+), 138 deletions(-) rename src/{WpfMath => WpfMath.Shared}/Rendering/GeometryHelper.cs (100%) rename src/{WpfMath => WpfMath.Shared}/Rendering/TeXFormulaExtensions.cs (90%) create mode 100644 src/WpfMath/Fonts/WpfSystemFont.cs rename src/WpfMath/Rendering/{BrushExtensions.cs => WpfBrushExtensions.cs} (89%) rename src/WpfMath/Rendering/{RectangleExtensions.cs => WpfRectangleExtensions.cs} (80%) delete mode 100644 src/WpfMath/SystemFont.cs diff --git a/src/WpfMath/Rendering/GeometryHelper.cs b/src/WpfMath.Shared/Rendering/GeometryHelper.cs similarity index 100% rename from src/WpfMath/Rendering/GeometryHelper.cs rename to src/WpfMath.Shared/Rendering/GeometryHelper.cs diff --git a/src/WpfMath/Rendering/TeXFormulaExtensions.cs b/src/WpfMath.Shared/Rendering/TeXFormulaExtensions.cs similarity index 90% rename from src/WpfMath/Rendering/TeXFormulaExtensions.cs rename to src/WpfMath.Shared/Rendering/TeXFormulaExtensions.cs index 82839c65..e5cf536f 100644 --- a/src/WpfMath/Rendering/TeXFormulaExtensions.cs +++ b/src/WpfMath.Shared/Rendering/TeXFormulaExtensions.cs @@ -2,7 +2,7 @@ namespace WpfMath.Rendering; -public static class TeXFormulaExtensions // TODO: Move to the shared assembly. +public static class TeXFormulaExtensions { public static void RenderTo( // TODO: Tests for this method. this TexFormula formula, diff --git a/src/WpfMath.Tests/GeometryElementRendererTests.fs b/src/WpfMath.Tests/GeometryElementRendererTests.fs index 758a6ae1..4771eb9f 100644 --- a/src/WpfMath.Tests/GeometryElementRendererTests.fs +++ b/src/WpfMath.Tests/GeometryElementRendererTests.fs @@ -28,7 +28,7 @@ type GeometryElementRendererTests() = let font = DefaultTexFont(WpfMathFontProvider.Instance, 20.0) let environment = TexEnvironment(TexStyle.Display, font, font) let char = environment.MathFont.GetDefaultCharInfo('x', TexStyle.Display).Value - renderer.RenderCharacter(char, 0.0, 0.0, BrushExtensions.ToPlatform Brushes.Black) + renderer.RenderCharacter(char, 0.0, 0.0, WpfBrushExtensions.ToPlatform Brushes.Black) let group = Seq.exactlyOne geometry.Children :?> GeometryGroup Assert.IsType(Seq.exactlyOne group.Children) |> ignore diff --git a/src/WpfMath/Fonts/WpfSystemFont.cs b/src/WpfMath/Fonts/WpfSystemFont.cs new file mode 100644 index 00000000..84a13a37 --- /dev/null +++ b/src/WpfMath/Fonts/WpfSystemFont.cs @@ -0,0 +1,132 @@ +using System.Globalization; +using System.Windows; +using System.Windows.Media; +using WpfMath.Exceptions; +using WpfMath.Utils; + +namespace WpfMath.Fonts; + +internal class SystemFont : ITeXFont +{ + private readonly FontFamily fontFamily; + + public SystemFont(double size, FontFamily fontFamily) + { + this.fontFamily = fontFamily; + Size = size; + } + + public bool SupportsMetrics => false; + + public double Size { get; } + + public ExtensionChar GetExtension(CharInfo charInfo, TexStyle style) => + throw MethodNotSupported(nameof(GetExtension)); + + public CharFont? GetLigature(CharFont leftChar, CharFont rightChar) => null; + + public CharInfo GetNextLargerCharInfo(CharInfo charInfo, TexStyle style) => + throw MethodNotSupported(nameof(GetNextLargerCharInfo)); + + public Result GetDefaultCharInfo(char character, TexStyle style) => + Result.Error(MethodNotSupported(nameof(this.GetDefaultCharInfo))); + + public Result GetCharInfo(char character, string textStyle, TexStyle style) + { + var typeface = this.GetTypeface(); + if (!typeface.TryGetGlyphTypeface(out var glyphTypeface)) + { + return Result.Error(new TypeFaceNotFoundException( + $"Glyph typeface for font {this.fontFamily.BaseUri} was not found")); + } + + var metrics = GetFontMetrics(character, typeface); + return Result.Ok( + new CharInfo(character, new WpfGlyphTypeface(glyphTypeface), 1.0, TexFontUtilities.NoFontId, metrics)); + } + + public Result GetCharInfo(CharFont charFont, TexStyle style) => + Result.Error(MethodNotSupported(nameof(this.GetCharInfo))); + + public Result GetCharInfo(string name, TexStyle style) => + Result.Error(MethodNotSupported(nameof(GetCharInfo))); + + public double GetKern(CharFont leftChar, CharFont rightChar, TexStyle style) => 0.0; + + public double GetQuad(int fontId, TexStyle style) => throw MethodNotSupported(nameof(GetQuad)); + + public double GetSkew(CharFont charFont, TexStyle style) => throw MethodNotSupported(nameof(GetSkew)); + + public bool HasSpace(int fontId) => throw MethodNotSupported(nameof(HasSpace)); + + public bool HasNextLarger(CharInfo charInfo) => throw MethodNotSupported(nameof(HasNextLarger)); + + public bool IsExtensionChar(CharInfo charInfo) => throw MethodNotSupported(nameof(IsExtensionChar)); + + public int GetMuFontId() => throw MethodNotSupported(nameof(GetMuFontId)); + + public double GetXHeight(TexStyle style, int fontId) => throw MethodNotSupported(nameof(GetXHeight)); + + public double GetSpace(TexStyle style) => throw MethodNotSupported(nameof(GetSpace)); + + public double GetAxisHeight(TexStyle style) => throw MethodNotSupported(nameof(GetAxisHeight)); + + public double GetBigOpSpacing1(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing1)); + + public double GetBigOpSpacing2(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing2)); + + public double GetBigOpSpacing3(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing3)); + + public double GetBigOpSpacing4(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing4)); + + public double GetBigOpSpacing5(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing5)); + + public double GetSub1(TexStyle style) => throw MethodNotSupported(nameof(GetSub1)); + + public double GetSub2(TexStyle style) => throw MethodNotSupported(nameof(GetSub2)); + + public double GetSubDrop(TexStyle style) => throw MethodNotSupported(nameof(GetSubDrop)); + + public double GetSup1(TexStyle style) => throw MethodNotSupported(nameof(GetSup1)); + + public double GetSup2(TexStyle style) => throw MethodNotSupported(nameof(GetSup2)); + + public double GetSup3(TexStyle style) => throw MethodNotSupported(nameof(GetSup3)); + + public double GetSupDrop(TexStyle style) => throw MethodNotSupported(nameof(GetSupDrop)); + + public double GetNum1(TexStyle style) => throw MethodNotSupported(nameof(GetNum1)); + + public double GetNum2(TexStyle style) => throw MethodNotSupported(nameof(GetNum2)); + + public double GetNum3(TexStyle style) => throw MethodNotSupported(nameof(GetNum3)); + + public double GetDenom1(TexStyle style) => throw MethodNotSupported(nameof(GetDenom1)); + + public double GetDenom2(TexStyle style) => throw MethodNotSupported(nameof(GetDenom2)); + + public double GetDefaultLineThickness(TexStyle style) => throw MethodNotSupported(nameof(GetDefaultLineThickness)); + + private static TexNotSupportedException MethodNotSupported(string callerMethod) + { + return new TexNotSupportedException( + $"Call of method {callerMethod} on {nameof(SystemFont)} is not supported"); + } + + private TexFontMetrics GetFontMetrics(char c, Typeface typeface) + { + var formattedText = new FormattedText(c.ToString(), + CultureInfo.CurrentUICulture, + FlowDirection.LeftToRight, + typeface, + 1.0, + Brushes.Black +#if !NET452 + , 1 +#endif + ); + return new TexFontMetrics(formattedText.Width, formattedText.Height, 0.0, formattedText.Width, 1.0); + } + + private Typeface GetTypeface() => new Typeface(this.fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); // TODO[F]: Put into lazy field +} diff --git a/src/WpfMath/Rendering/BrushExtensions.cs b/src/WpfMath/Rendering/WpfBrushExtensions.cs similarity index 89% rename from src/WpfMath/Rendering/BrushExtensions.cs rename to src/WpfMath/Rendering/WpfBrushExtensions.cs index 43199582..ef5e7337 100644 --- a/src/WpfMath/Rendering/BrushExtensions.cs +++ b/src/WpfMath/Rendering/WpfBrushExtensions.cs @@ -3,7 +3,7 @@ namespace WpfMath.Rendering; -internal static class BrushExtensions +internal static class WpfBrushExtensions { public static Brush? ToWpf(this IPlatformBrush? brush) => ((WpfBrush?)brush)?.Value; [return: NotNullIfNotNull(nameof(brush))] diff --git a/src/WpfMath/Rendering/RectangleExtensions.cs b/src/WpfMath/Rendering/WpfRectangleExtensions.cs similarity index 80% rename from src/WpfMath/Rendering/RectangleExtensions.cs rename to src/WpfMath/Rendering/WpfRectangleExtensions.cs index 0170125c..da4a8531 100644 --- a/src/WpfMath/Rendering/RectangleExtensions.cs +++ b/src/WpfMath/Rendering/WpfRectangleExtensions.cs @@ -2,7 +2,7 @@ namespace WpfMath.Rendering; -internal static class RectangleExtensions +internal static class WpfRectangleExtensions { public static Rect ToWpf(this Rectangle rectangle) => new(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); diff --git a/src/WpfMath/SystemFont.cs b/src/WpfMath/SystemFont.cs deleted file mode 100644 index 165e9b95..00000000 --- a/src/WpfMath/SystemFont.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System.Globalization; -using System.Windows; -using System.Windows.Media; -using WpfMath.Exceptions; -using WpfMath.Fonts; -using WpfMath.Utils; - -namespace WpfMath -{ - internal class SystemFont : ITeXFont - { - private readonly FontFamily fontFamily; - - public SystemFont(double size, FontFamily fontFamily) - { - this.fontFamily = fontFamily; - Size = size; - } - - public bool SupportsMetrics => false; - - public double Size { get; } - - public ExtensionChar GetExtension(CharInfo charInfo, TexStyle style) => - throw MethodNotSupported(nameof(GetExtension)); - - public CharFont? GetLigature(CharFont leftChar, CharFont rightChar) => null; - - public CharInfo GetNextLargerCharInfo(CharInfo charInfo, TexStyle style) => - throw MethodNotSupported(nameof(GetNextLargerCharInfo)); - - public Result GetDefaultCharInfo(char character, TexStyle style) => - Result.Error(MethodNotSupported(nameof(this.GetDefaultCharInfo))); - - public Result GetCharInfo(char character, string textStyle, TexStyle style) - { - var typeface = this.GetTypeface(); - if (!typeface.TryGetGlyphTypeface(out var glyphTypeface)) - { - return Result.Error(new TypeFaceNotFoundException( - $"Glyph typeface for font {this.fontFamily.BaseUri} was not found")); - } - - var metrics = GetFontMetrics(character, typeface); - return Result.Ok( - new CharInfo(character, new WpfGlyphTypeface(glyphTypeface), 1.0, TexFontUtilities.NoFontId, metrics)); - } - - public Result GetCharInfo(CharFont charFont, TexStyle style) => - Result.Error(MethodNotSupported(nameof(this.GetCharInfo))); - - public Result GetCharInfo(string name, TexStyle style) => - Result.Error(MethodNotSupported(nameof(GetCharInfo))); - - public double GetKern(CharFont leftChar, CharFont rightChar, TexStyle style) => 0.0; - - public double GetQuad(int fontId, TexStyle style) => throw MethodNotSupported(nameof(GetQuad)); - - public double GetSkew(CharFont charFont, TexStyle style) => throw MethodNotSupported(nameof(GetSkew)); - - public bool HasSpace(int fontId) => throw MethodNotSupported(nameof(HasSpace)); - - public bool HasNextLarger(CharInfo charInfo) => throw MethodNotSupported(nameof(HasNextLarger)); - - public bool IsExtensionChar(CharInfo charInfo) => throw MethodNotSupported(nameof(IsExtensionChar)); - - public int GetMuFontId() => throw MethodNotSupported(nameof(GetMuFontId)); - - public double GetXHeight(TexStyle style, int fontId) => throw MethodNotSupported(nameof(GetXHeight)); - - public double GetSpace(TexStyle style) => throw MethodNotSupported(nameof(GetSpace)); - - public double GetAxisHeight(TexStyle style) => throw MethodNotSupported(nameof(GetAxisHeight)); - - public double GetBigOpSpacing1(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing1)); - - public double GetBigOpSpacing2(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing2)); - - public double GetBigOpSpacing3(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing3)); - - public double GetBigOpSpacing4(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing4)); - - public double GetBigOpSpacing5(TexStyle style) => throw MethodNotSupported(nameof(GetBigOpSpacing5)); - - public double GetSub1(TexStyle style) => throw MethodNotSupported(nameof(GetSub1)); - - public double GetSub2(TexStyle style) => throw MethodNotSupported(nameof(GetSub2)); - - public double GetSubDrop(TexStyle style) => throw MethodNotSupported(nameof(GetSubDrop)); - - public double GetSup1(TexStyle style) => throw MethodNotSupported(nameof(GetSup1)); - - public double GetSup2(TexStyle style) => throw MethodNotSupported(nameof(GetSup2)); - - public double GetSup3(TexStyle style) => throw MethodNotSupported(nameof(GetSup3)); - - public double GetSupDrop(TexStyle style) => throw MethodNotSupported(nameof(GetSupDrop)); - - public double GetNum1(TexStyle style) => throw MethodNotSupported(nameof(GetNum1)); - - public double GetNum2(TexStyle style) => throw MethodNotSupported(nameof(GetNum2)); - - public double GetNum3(TexStyle style) => throw MethodNotSupported(nameof(GetNum3)); - - public double GetDenom1(TexStyle style) => throw MethodNotSupported(nameof(GetDenom1)); - - public double GetDenom2(TexStyle style) => throw MethodNotSupported(nameof(GetDenom2)); - - public double GetDefaultLineThickness(TexStyle style) => throw MethodNotSupported(nameof(GetDefaultLineThickness)); - - private static TexNotSupportedException MethodNotSupported(string callerMethod) - { - return new TexNotSupportedException( - $"Call of method {callerMethod} on {nameof(SystemFont)} is not supported"); - } - - private TexFontMetrics GetFontMetrics(char c, Typeface typeface) - { - var formattedText = new FormattedText(c.ToString(), - CultureInfo.CurrentUICulture, - FlowDirection.LeftToRight, - typeface, - 1.0, - Brushes.Black -#if !NET452 - , 1 -#endif - ); - return new TexFontMetrics(formattedText.Width, formattedText.Height, 0.0, formattedText.Width, 1.0); - } - - private Typeface GetTypeface() => new Typeface(this.fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); // TODO[F]: Put into lazy field - } -}