Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <text> 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;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions test/image/compare_pixels_collections.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"map_predefined-styles1"
],
"mathjax3": [
"legend_mathjax_fontsize",
"legend_mathjax_title_and_items",
"mathjax",
"parcats_grid_subplots",
Expand Down
44 changes: 44 additions & 0 deletions test/image/mocks/legend_mathjax_fontsize.json
Original file line number Diff line number Diff line change
@@ -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]
}
]
}