Skip to content

Commit

Permalink
Extracted calculation of values to be rendered from `OverUnderBox.Cre…
Browse files Browse the repository at this point in the history
…ateRenderChoices` into its own method, for a more generative approach
  • Loading branch information
Lehonti Ramos committed Oct 4, 2023
1 parent ef8d647 commit 2215e93
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/XamlMath.Shared/Boxes/OverUnderBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,27 @@ public OverUnderBox(Box baseBox, Box delimiterBox, Box? scriptBox, double kern,
// True to draw delimeter and script over base; false to draw under base.
public bool Over { get; }

public override void RenderTo(IElementRenderer renderer, double x, double y)
private record struct RenderChoices(
double TranslationX,
double TranslationY,
double YPosition);
private RenderChoices CreateRenderChoices(double x, double y)
{
renderer.RenderElement(this.BaseBox, x, y);

if (this.Over)
{
// Draw delimeter and script boxes over base box.
var centerY = y - this.BaseBox.Height - this.DelimiterBox.Width;
var translationX = x + this.DelimiterBox.Width / 2;
var translationY = centerY + this.DelimiterBox.Width / 2;

RenderDelimiter(translationX, translationY);
return new(

// Draw script box as superscript.
RenderScriptBox(centerY - this.Kern - this.ScriptBox!.Depth); // Nullable TODO: This probably needs null checking
TranslationX: translationX,
TranslationY: translationY,

// Draw script box as superscript.
YPosition: centerY - this.Kern - this.ScriptBox!.Depth // Nullable TODO: This probably needs null checking
);
}
else
{
Expand All @@ -58,11 +64,25 @@ public override void RenderTo(IElementRenderer renderer, double x, double y)
var translationX = x + this.DelimiterBox.Width / 2;
var translationY = centerY - this.DelimiterBox.Width / 2;

RenderDelimiter(translationX, translationY);
return new(

TranslationX: translationX,
TranslationY: translationY,

// Draw script box as subscript.
RenderScriptBox(centerY + this.Kern + this.ScriptBox!.Height); // Nullable TODO: This probably needs null checking
// Draw script box as subscript.
YPosition: centerY + this.Kern + this.ScriptBox!.Height // Nullable TODO: This probably needs null checking
);
}
}

public override void RenderTo(IElementRenderer renderer, double x, double y)
{
var renderChoices = CreateRenderChoices(x, y);

renderer.RenderElement(this.BaseBox, x, y);

RenderDelimiter(renderChoices.TranslationX, renderChoices.TranslationY);
RenderScriptBox(renderChoices.YPosition);

void RenderDelimiter(double translationX, double translationY)
{
Expand Down

0 comments on commit 2215e93

Please sign in to comment.