ForNeVeR / ForNeVeR/xaml-math

Matrix should align its items with the baseline, not the total box

Open
#484 0 comments 0 reactions 0 assignees View on GitHub
help-wanted kind:bug
Dominant language
C#
Stars
719
Forks
113
Avg merge
6d 12h
Merged PRs (30d)
1

Description

Consider this formula:
```tex
\text{f(x)} = \cases{\text{1/3} & \text{if 0 ≤ x ≤ 1;} \\
\text{2/3} & \text{if 3 ≤ x ≤ 4;} \\
0..... & \colorbox{red}{\text{.elsewherge.}} \\
0..... & \colorbox{red}{\text{..}}}
```

Check how it looks currently:
![image](https://github.com/ForNeVeR/xaml-math/assets/92793/3b1d5ecf-64c1-4d57-80f8-c09e65779ec3)

As you can see, on line `elsewherge` the red box is high and the item itself is shifted up because of this.

On the line `..`, on contrary, the box is quite low and thus the item is also placed incorrectly.

Now, consider this patch that temporarily disables the character depth calculation for the system fonts:
```diff
Index: src/WpfMath/Fonts/WpfSystemFont.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/WpfMath/Fonts/WpfSystemFont.cs b/src/WpfMath/Fonts/WpfSystemFont.cs
--- a/src/WpfMath/Fonts/WpfSystemFont.cs (revision Staged)
+++ b/src/WpfMath/Fonts/WpfSystemFont.cs (date 1710012618138)
@@ -155,6 +155,6 @@
var scalingFactor = typeface.FontFamily.LineSpacing / typeface.FontFamily.Baseline;
height /= scalingFactor;

- return new TeXFontMetrics(formattedText.Width, height, depth, 0, 1.0);
+ return new TeXFontMetrics(formattedText.Width, height, 0, 0, 1.0);
}
}
```

After it, the `elsewherge` placement is nearly perfect:
![image](https://github.com/ForNeVeR/xaml-math/assets/92793/03a5b671-0250-4fd1-b4f6-e4136f9d2e1a)

So, the problem is that the characters properly report its baseline to the box model, but it cannot take it into account when placing the matrix items (I believe that we should just take `Height` instead of `TotalHeight` somewhere in the matrix calculation).

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by rendering the formula shown in the issue and inspect src/WpfMath/Fonts/WpfSystemFont.cs, especially the reported character metrics. Then trace the matrix layout calculation to determine how item baselines are positioned relative to their boxes. Done means the boxed characters align consistently with the surrounding matrix items without regressing other matrix rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, tex
Domain
computer-graphics, desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.