From e52f6859ec6e2f49b3af33c0934b08aed91a2820 Mon Sep 17 00:00:00 2001 From: Ron Kok Date: Fri, 29 Mar 2019 06:59:21 -0700 Subject: [PATCH] Improve MathML for \rule (#1912) * MathML for \rule * Pick up comments * Update rule.js --- src/functions/color.js | 3 ++- src/functions/rule.js | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/functions/color.js b/src/functions/color.js index 35d1c390..1fa81bd4 100644 --- a/src/functions/color.js +++ b/src/functions/color.js @@ -22,7 +22,8 @@ const htmlBuilder = (group, options) => { }; const mathmlBuilder = (group, options) => { - const inner = mml.buildExpression(group.body, options); + const inner = mml.buildExpression(group.body, + options.withColor(group.color)); const node = new mathMLTree.MathNode("mstyle", inner); diff --git a/src/functions/rule.js b/src/functions/rule.js index 8ec7bc2e..b1bbbfa9 100644 --- a/src/functions/rule.js +++ b/src/functions/rule.js @@ -30,13 +30,9 @@ defineFunction({ const rule = buildCommon.makeSpan(["mord", "rule"], [], options); // Calculate the shift, width, and height of the rule, and account for units - let shift = 0; - if (group.shift) { - shift = calculateSize(group.shift, options); - } - const width = calculateSize(group.width, options); const height = calculateSize(group.height, options); + const shift = (group.shift) ? calculateSize(group.shift, options) : 0; // Style the rule to the right size rule.style.borderRightWidth = width + "em"; @@ -55,10 +51,25 @@ defineFunction({ return rule; }, mathmlBuilder(group, options) { - // TODO(emily): Figure out if there's an actual way to draw black boxes - // in MathML. - const node = new mathMLTree.MathNode("mrow"); + const width = calculateSize(group.width, options); + const height = calculateSize(group.height, options); + const shift = (group.shift) ? calculateSize(group.shift, options) : 0; + const color = options.color && options.getColor() || "black"; - return node; + const rule = new mathMLTree.MathNode("mspace"); + rule.setAttribute("mathbackground", color); + rule.setAttribute("width", width + "em"); + rule.setAttribute("height", height + "em"); + + const wrapper = new mathMLTree.MathNode("mpadded", [rule]); + if (shift >= 0) { + wrapper.setAttribute("height", "+" + shift + "em"); + } else { + wrapper.setAttribute("height", shift + "em"); + wrapper.setAttribute("depth", "+" + (-shift) + "em"); + } + wrapper.setAttribute("voffset", shift + "em"); + + return wrapper; }, });