Skip to content

Commit 6dcfbdc

Browse files
committed
Use unique key for MathJax labels in bbox cache
1 parent 11e3bc7 commit 6dcfbdc

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/components/drawing/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,9 @@ drawing.textPointBBox = function (s) {
13461346
const math = d3.select(node.parentNode).select('g.text-math-group');
13471347
let bb;
13481348
if (math.size()) {
1349-
bb = drawing.bBox(math.node());
1349+
// The default key of the render has no font, so key it by its source `<text>` node, which has the font
1350+
const hash = textPointHash(node);
1351+
bb = drawing.bBox(math.node(), false, hash && hash + '~math');
13501352
// the MathJax svg is placed in the frame of the `<text>` node
13511353
bb.top = +math.select('svg').attr('y') - +s.attr('y');
13521354
bb.bottom = bb.top + bb.height;

‎test/jasmine/bundle_tests/mathjax_test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Plotly = require('../../../lib/index');
22
var Lib = require('../../../src/lib');
3+
const Drawing = require('../../../src/components/drawing');
34
var d3Select = require('../../strict-d3').select;
45

56
var createGraphDiv = require('../assets/create_graph_div');
@@ -252,6 +253,42 @@ describe('Test MathJax v' + mathjaxVersion + ':', function() {
252253
.then(done, done.fail);
253254
});
254255

256+
it('should measure a tex label at the font size of its own trace', (done) => {
257+
Plotly.newPlot(gd, [{
258+
mode: 'markers+text',
259+
x: [0.5],
260+
y: [0.5],
261+
text: ['$\\alpha$'],
262+
textposition: 'auto',
263+
textfont: {size: 10}
264+
}, {
265+
mode: 'markers+text',
266+
x: [1, 1.3, 1.6, 1.9, 2.2, 2.5],
267+
y: [2, 2.1, 2, 2.1, 2, 2.1],
268+
text: ['$\\alpha$', '$\\alpha$', '$\\alpha$', '$\\alpha$', '$\\alpha$', '$\\alpha$'],
269+
textposition: 'auto',
270+
textfont: {size: 40}
271+
}], Lib.extendDeep({}, layout, {showlegend: false, xaxis: {range: [0, 3]}, yaxis: {range: [0, 3]}}))
272+
.then(() => {
273+
const sizes = [];
274+
275+
d3Select(gd).selectAll('.textpoint').each(function() {
276+
const label = d3Select(this);
277+
const rect = label.select('.text-math-group').node().getBoundingClientRect();
278+
const measured = Drawing.textPointBBox(label.select('text'));
279+
280+
expect(Math.abs(measured.width - rect.width)).toBeLessThan(1, 'width of a tex label');
281+
expect(Math.abs(measured.height - rect.height)).toBeLessThan(1, 'height of a tex label');
282+
sizes.push(Math.round(rect.width));
283+
});
284+
285+
// the two font sizes give two sizes of the same string
286+
expect(sizes.length).toBe(7, 'tex labels');
287+
expect(sizes[1]).toBeGreaterThan(sizes[0] * 2, 'large label against small label');
288+
})
289+
.then(done, done.fail);
290+
});
291+
255292
it('should place *auto* labels by their rendered box and hide the whole label', function(done) {
256293
var n = 12;
257294
var x = [];

0 commit comments

Comments
 (0)