From c3ea9d07dd3e5a9e2f0c38f43017f6212ceb9c1c Mon Sep 17 00:00:00 2001 From: Caio Pizzol Date: Sat, 28 Mar 2026 09:07:45 -0300 Subject: [PATCH] fix(math): set displaystyle and Cambria Math font on MathML output (SD-2404) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Set displaystyle=true only on display math (m:oMathPara) per ECMA-376 spec — inline math uses cramped mode with smaller fractions and limits as subscripts. Set display=block for m:oMathPara. Use Cambria Math as preferred math font to match Word rendering. --- .../painters/dom/src/features/math/omml-to-mathml.test.ts | 4 ++++ .../painters/dom/src/features/math/omml-to-mathml.ts | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.test.ts b/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.test.ts index 6a31f8a695..606818faa9 100644 --- a/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.test.ts +++ b/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.test.ts @@ -30,6 +30,8 @@ describe('convertOmmlToMathml', () => { expect(result).not.toBeNull(); expect(result!.namespaceURI).toBe(MATHML_NS); expect(result!.localName).toBe('math'); + expect(result!.getAttribute('displaystyle')).toBeNull(); + expect(result!.getAttribute('display')).toBeNull(); // Should contain an for the identifier 'x' const mi = result!.querySelector('mi'); @@ -94,6 +96,8 @@ describe('convertOmmlToMathml', () => { const result = convertOmmlToMathml(omml, doc); expect(result).not.toBeNull(); expect(result!.localName).toBe('math'); + expect(result!.getAttribute('displaystyle')).toBe('true'); + expect(result!.getAttribute('display')).toBe('block'); // The m:oMathParaPr should be skipped (it ends with 'Pr') // The m:oMath child should produce content expect(result!.textContent).toBe('y'); diff --git a/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.ts b/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.ts index 958f74cd98..3dd25c3014 100644 --- a/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.ts +++ b/packages/layout-engine/painters/dom/src/features/math/omml-to-mathml.ts @@ -157,6 +157,12 @@ export function convertOmmlToMathml(ommlJson: unknown, doc: Document): Element | const root = ommlJson as OmmlJsonNode; const mathEl = doc.createElementNS(MATHML_NS, 'math'); + mathEl.setAttribute('style', 'font-family: "Cambria Math", math'); + + if (root.name === 'm:oMathPara') { + mathEl.setAttribute('display', 'block'); + mathEl.setAttribute('displaystyle', 'true'); + } // For m:oMathPara, iterate over child m:oMath elements // For m:oMath, process directly