Skip to content

Measure and center the full bounds of multiline equations #239

Description

@vxdguy

Describe the bug

MathPainter generates an image that cuts off the right side of the equation.

To Reproduce

Compiled on Ubuntu 24.04.1
.NET 8.0.405

CSProj contents:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <DebugType>embedded</DebugType>
    <ReadyToRun>true</ReadyToRun>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="CSharpMath.SkiaSharp" Version="0.5.1" />
    <PackageReference Include="SkiaSharp" Version="3.116.1" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.1" />
  </ItemGroup>
</Project>

main.cs contents:

using CSharpMath.SkiaSharp;
using SkiaSharp;


// Render LaTeX to PNG
var painter = new MathPainter
{
    LaTeX = @"\begin{aligned}
\nabla \cdot \vec{\bf E} & = \frac {\rho} {\varepsilon_0} \\
\nabla \cdot \vec{\bf B} & = 0 \\
\nabla \times \vec{\bf E} &= - \frac{\partial\vec{\bf B}}{\partial t} \\
\nabla \times \vec{\bf B} & = \mu_0\vec{\bf J} + \mu_0\varepsilon_0 \frac{\partial\vec{\bf E}}{\partial t}
\end{aligned}",
    AntiAlias = true,
    TextColor = SKColors.Green,
    FontSize = 48,
    PaintStyle = CSharpMath.Rendering.FrontEnd.PaintStyle.Fill,
    GlyphBoxColor = (SKColors.Red, SKColors.Azure),
};

using var png = painter.DrawAsStream(format: SKEncodedImageFormat.Png, quality: 100); 

if (png == null)
{
    throw new InvalidOperationException("Failed to create PNG stream.");
}

using var file = File.OpenWrite("equation.png");

png.CopyTo(file);

Compile, execute, and open the resulting equation.png and you can see the image of the equation.

The canvas is not wide enough for the generated latex image.

Expected behavior

Width of canvas should be as large as the latex image.

Environment (please complete the following information):

  • Platform: CSharpMath.SkiaSharp
  • Package Version or Commit: 0.5.11
  • Device: Desktop PC, Ubuntu 24.04.1

Additional context

See attached image result.

Image

Maintainer triage (2026-08-30)

Reproduced on current master (8bda039) with the aligned example above and with #172's eqnarray example. DrawAsStream creates its surface from Measure, but later rows can extend past that measured/right-aligned bound and are clipped. The eqnarray reproduction also positions the shorter first row and longer second row as though the complete table bounds were not centered as one unit.

This issue now consolidates #172 and covers multiline table environments in both MathPainter and TextPainter.

Acceptance criteria:

  • Measure uses the union of every row/cell's horizontal bounds for aligned, split, array/matrix, and eqnarray-style displays.
  • Bounds include italic overhang and any negative/positive cell offsets, not only glyph advances or the first row.
  • DrawAsStream does not clip any row at either edge.
  • TextPainter centers the complete multiline display as one unit at finite and infinite canvas widths.
  • Add regression/golden-image tests using the reproductions from this issue and textPainter does not correctly center an equation which contains eqnarray #172.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area/RenderingTypesetting, measurement, fonts, text layout, and drawing.Type/BugA reproducible defect in CSharpMath.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions