diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index 901f77ec98b..51ed6d34e28 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -770,7 +770,42 @@ function computeTextDimensions(g, gd, legendObj, aTitle) { var height, width; if(mathjaxNode) { - var mathjaxBB = Drawing.bBox(mathjaxNode); + var mathjaxBB; + var mjMathJaxVersion = svgTextUtils.mathJaxMajorVersion(); + var mjInnerSvg = mathjaxNode.querySelector && mathjaxNode.querySelector('svg'); + var mjViewBox = mjInnerSvg && mjInnerSvg.getAttribute('viewBox'); + // MathJax v3/v4 (both built on the same MathJax-src engine) always + // draw into an internal coordinate system where 1000 units equal + // 1em (see output/svg.ts `createSVG` in + // https://github.com/mathjax/MathJax-src, and confirmation from a + // MathJax maintainer in mathjax/MathJax#2003). The viewBox's 3rd + // and 4th numbers are that em-relative width and height (height + // already includes the depth below the baseline), so converting + // them with the *typeset* em size in px - the same font.size we + // asked MathJax to typeset with (see + // `parseInt(_context.node().style.fontSize, 10)` in + // convertToTspans) - reconstructs the exact rendered pixel box. + // This sidesteps getBoundingClientRect() on a detached/cloned copy + // of the node (Drawing.bBox), which is what produces the + // too-small legend sizing seen in gh-559: unlike font.size, a + // computed style read here isn't reliable, since this group + // doesn't inherit the font-size set on the original node (a + // sibling, not an ancestor, of this math group). + // + // MathJax v2's (legacy, pre-MathJax-src) SVG output does NOT use + // the same 1000-units-per-em convention for its viewBox - verified + // empirically to be off by ~5-10% on the height - so for v2 (or + // any unrecognized/future version) we keep using Drawing.bBox(). + if(mjViewBox && (mjMathJaxVersion === 3 || mjMathJaxVersion === 4)) { + var vbParts = mjViewBox.trim().split(/[\s,]+/).map(Number); + var vbWidth = (vbParts[2] / 1000) * font.size; + var vbHeight = (vbParts[3] / 1000) * font.size; + mathjaxBB = (vbParts.length === 4 && isFinite(vbWidth) && isFinite(vbHeight) && vbWidth > 0 && vbHeight > 0) + ? {width: vbWidth, height: vbHeight} + : Drawing.bBox(mathjaxNode); + } else { + mathjaxBB = Drawing.bBox(mathjaxNode); + } height = mathjaxBB.height; width = mathjaxBB.width; diff --git a/src/lib/svg_text_utils.js b/src/lib/svg_text_utils.js index 21a0aa17beb..b9db93e9012 100644 --- a/src/lib/svg_text_utils.js +++ b/src/lib/svg_text_utils.js @@ -323,6 +323,18 @@ function isMathJaxVersionSupported() { return false; } +/* + * The major version number of the MathJax global, or null if MathJax isn't + * loaded. Exposed so other modules (e.g. legend/draw.js) can special-case + * behavior that depends on which MathJax engine actually rendered a given + * node, without each needing to parse `MathJax.version` themselves. + */ +exports.mathJaxMajorVersion = function() { + return (typeof MathJax !== 'undefined' && MathJax.version) ? + parseInt(MathJax.version.split('.')[0]) : + null; +}; + function texToSVG(_texString, _config, _callback) { const MathJaxVersion = mathJaxMajorVersion(); diff --git a/test/image/compare_pixels_collections.json b/test/image/compare_pixels_collections.json index b850cd904b8..665c75bac7d 100644 --- a/test/image/compare_pixels_collections.json +++ b/test/image/compare_pixels_collections.json @@ -41,6 +41,7 @@ "map_predefined-styles1" ], "mathjax3": [ + "legend_mathjax_fontsize", "legend_mathjax_title_and_items", "mathjax", "parcats_grid_subplots", diff --git a/test/image/mocks/legend_mathjax_fontsize.json b/test/image/mocks/legend_mathjax_fontsize.json new file mode 100644 index 00000000000..190a658fa90 --- /dev/null +++ b/test/image/mocks/legend_mathjax_fontsize.json @@ -0,0 +1,44 @@ +{ + "layout": { + "legend": { + "title": { + "text": "$\\varphi = \\frac{1+\\sqrt{5}}{2}$", + "font": { + "size": 26 + } + }, + "font": { + "size": 9 + }, + "orientation": "v", + "bordercolor": "gray", + "borderwidth": 2, + "bgcolor": "rgba(255,255,0,0.5)" + }, + "margin": { + "t": 25, + "b": 25, + "r": 25, + "l": 25 + }, + "width": 500, + "height": 300 + }, + "data": [ + { + "name": "$\\alpha_{1c} = 352$", + "x": [1, 2], + "y": [0.5, 0.5] + }, + { + "name": "${\\frac {1}{2}}=0.5$", + "x": [2, 3], + "y": [0.6, 0.6] + }, + { + "name": "$x^2 + y^2 = z^2$", + "x": [3, 5], + "y": [0.7, 0.7] + } + ] +}