Skip to content

Commit 152d4bf

Browse files
Measure repositioned tex in its own units
repositionMathGroup now measures the typeset group with getBBox, which reports its size in the group's own units, instead of its client rect. The size no longer changes when the group or the graph is rotated or scaled, and it matches what positionText uses for plain text. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 406c21c commit 152d4bf

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎src/lib/svg_text_utils.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ function measureMath(newSvg, width0, height0) {
6565
return {width: w, height: h};
6666
}
6767

68+
/**
69+
* The size of a typeset group in its own units, which unlike its client rect
70+
* does not change when the group, or something above it, is rotated or scaled.
71+
*
72+
* @param {d3 selection} mathjaxGroup: the '<svgClass>-group' wrapper
73+
* @return {{width: number, height: number}}
74+
*/
75+
function getLocalSize(mathjaxGroup) {
76+
var bb = mathjaxGroup.node().getBBox();
77+
return {width: bb.width, height: bb.height};
78+
}
79+
6880
/**
6981
* Positions a MathJax-typeset group against its source <text> node's x/y
7082
* and text-anchor.
@@ -260,9 +272,8 @@ exports.convertToTspans = function(_context, gd, _callback) {
260272
* convertToTspans, to match its source <text> node's *current* x/y and
261273
* text-anchor.
262274
*
263-
* The size is measured from the page, so it changes with the rotation or
264-
* scale of whatever the group sits in. A caller that moves the text into such
265-
* a state should measure once beforehand and pass that size back in.
275+
* The size is measured in the group's own units, so it does not change with
276+
* the rotation or scale of whatever the group sits in.
266277
*
267278
* @param {d3 selection} _context: the source <text> element
268279
* @param {{width: number, height: number}} [size]: a size returned by an
@@ -279,7 +290,7 @@ exports.repositionMathGroup = function(_context, size) {
279290
var newSvg = mathjaxGroup.select('svg.' + svgClass);
280291
if(mathjaxGroup.empty() || newSvg.empty()) return null;
281292

282-
size = size || measureMath(newSvg);
293+
size = size || getLocalSize(mathjaxGroup);
283294
var textAnchor = _context.attr('text-anchor');
284295
var fontSize = parseInt(_context.node().style.fontSize, 10);
285296
positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, size);

0 commit comments

Comments
 (0)