Fix large operator positioning causing incorrect atom ordering

When rendering large operators (e.g., sum, integral) with scripts in text
  mode, the operator glyph was incorrectly positioned after its subscripts
  and superscripts instead of before them. This caused expressions like
  \sum_{i=1}^{n} i = \frac{n(n+1)}{2} to render with the equals sign
  appearing visually misplaced.

  Root cause:
  The line-breaking refactoring introduced double-positioning of large
  operators. makeLargeOp() internally sets the operator position, advances
  currentPosition.x, and adds script displays. However, the calling code
  then overwrote the position and advanced currentPosition.x again, causing:
  - Double-advancement leading to incorrect width calculations
  - Scripts positioned before the operator instead of after

  Solution:
  Save and restore typesetter state before/after line break dimension checks,
  then call makeLargeOp() once at the correct position after handling line
  breaks and inter-element spacing.
This commit is contained in:
Nicolas Guillot
2025-11-14 10:32:10 +01:00
parent 8cf87ef703
commit 15269e87e5
3 changed files with 276 additions and 56 deletions

View File

@@ -258,9 +258,24 @@ public class MTMathUILabel : MTView {
let effectiveWidth = _preferredMaxLayoutWidth > 0 ? _preferredMaxLayoutWidth : bounds.size.width
let availableWidth = effectiveWidth - contentInsets.left - contentInsets.right
print("🔧 MTMathUILabel _layoutSubviews:")
print(" preferredMaxLayoutWidth: \(_preferredMaxLayoutWidth)")
print(" bounds.size.width: \(bounds.size.width)")
print(" effectiveWidth: \(effectiveWidth)")
print(" availableWidth: \(availableWidth)")
print(" LaTeX: \(_latex.prefix(60))...")
// print("Pre list = \(_mathList!)")
_displayList = MTTypesetter.createLineForMathList(_mathList, font: font, style: currentStyle, maxWidth: availableWidth)
_displayList!.textColor = textColor
print(" Display subDisplays count: \(_displayList!.subDisplays.count)")
for (index, subDisplay) in _displayList!.subDisplays.enumerated() {
print(" Display \(index): type=\(type(of: subDisplay)), x=\(subDisplay.position.x), width=\(subDisplay.width)")
if let lineDisplay = subDisplay as? MTCTLineDisplay {
print(" Content: '\(lineDisplay.attributedString?.string ?? "")'")
}
}
// print("Post list = \(_mathList!)")
var textX = CGFloat(0)
switch self.textAlignment {