Skip to content

Commit

Permalink
(#63) WpfTeXFormulaExtensions: margins should be in pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 15, 2023
1 parent a7b1be9 commit 471cb70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/WpfMath.Shared/Rendering/IElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public interface IElementRenderer
/// <param name="box">The element that should be rendered.</param>
/// <param name="x">Logical X coordinate of the top left corner.</param>
/// <param name="y">Logical Y coordinate of the top left corner.</param>
/// <remarks>Should be called for every element of the formula (including nested ones).</remarks>
/// <remarks>
/// Usually this method should call <see cref="Box.RenderTo"/> with optional code common for all elements.
/// <para>Should be called for every element of the formula (including nested ones).</para>
/// <para>
/// Usually this method should call <see cref="Box.RenderTo"/> with optional code common for all elements.
/// </para>
/// </remarks>
void RenderElement(Box box, double x, double y);

Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/Rendering/GeometryElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void RenderTransformed(Box box, IEnumerable<Transformation> transforms, d
var scaledTransforms = transforms.Select(t => t.Scale(_scale));
ApplyTransformations(scaledTransforms, group);
var nestedRenderer = new GeometryElementRenderer(group, _scale);
nestedRenderer.RenderElement(box, x / _scale, y / _scale);
nestedRenderer.RenderElement(box, x, y);
_geometry.Children.Add(group);
}

Expand Down
25 changes: 18 additions & 7 deletions src/WpfMath/Rendering/WpfTeXFormulaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public static Geometry RenderToGeometry(
return geometry;
}

/// <summary>Renders the formula to a WPF bitmap.</summary>
/// <param name="scale">Formula text scale./</param>
/// <param name="x">A physical X coordinate of the top left corner in the resulting bitmap.</param>
/// <param name="y">A physical Y coordinate of the top left corner in the resulting bitmap.</param>
/// <param name="dpi">The resulting image DPI.</param>
public static BitmapSource RenderToBitmap(
this TexFormula formula,
TexEnvironment environment,
Expand Down Expand Up @@ -51,26 +56,32 @@ private static void RenderWithPositiveCoordinates(
double y)
{
using (var drawingContext = visual.RenderOpen())
formula.RenderTo(drawingContext, environment, scale, x, y);
formula.RenderTo(drawingContext, environment, scale, x / scale, y / scale);

var bounds = visual.ContentBounds;
if (bounds.X >= 0 && bounds.Y >= 0) return;
if (bounds is { X: >= 0, Y: >= 0 }) return;

using (var drawingContext = visual.RenderOpen())
{
drawingContext.PushTransform(
new TranslateTransform(Math.Max(0.0, -bounds.X), Math.Max(0.0, -bounds.Y)));
formula.RenderTo(drawingContext, environment, scale, x, y);
formula.RenderTo(drawingContext, environment, scale, x / scale, y / scale);
}
}

public static void RenderTo( // TODO: Tests for this method.
/// <summary>
/// Renders the <paramref name="formula"/> to the <paramref name="drawingContext"/>.
/// </summary>
/// <param name="scale">Formula text scale./</param>
/// <param name="x">Logical X coordinate of the top left corner of the formula.</param>
/// <param name="y">Logical Y coordinate of the top left corner of the formula.</param>
public static void RenderTo(
this TexFormula formula,
DrawingContext drawingContext,
TexEnvironment environment,
double scale,
double x,
double y)
double scale = 20.0,
double x = 0.0,
double y = 0.0)
{
formula.RenderTo(new WpfElementRenderer(drawingContext, scale), environment, x, y);
}
Expand Down

0 comments on commit 471cb70

Please sign in to comment.