Skip to content

Commit 67bca32

Browse files
committed
Properly determine container width for shrink-wrapped elements, and try to handle the problem with full-width equations being too wide when the ex-size isn't measured accurately enough. Issue mathjax#1478.
1 parent c0c63b1 commit 67bca32

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

unpacked/jax/output/SVG/jax.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
"min-height": 0, "max-height":"none",
101101
padding:0, border: 0, margin: 0
102102
},
103+
".MathJax_SVG_LineBox": {
104+
display: "table-cell!important",
105+
width: "10000em!important",
106+
"min-width":0, "max-width":"none",
107+
padding:0, border:0, margin:0
108+
},
103109

104110
"#MathJax_SVG_Tooltip": {
105111
position: "absolute", left: 0, top: 0,
@@ -173,8 +179,7 @@
173179
);
174180

175181
// Used in preTranslate to get linebreak width
176-
this.linebreakSpan = HTML.Element("span",null,
177-
[["hr",{style: {width:"auto", size:1, padding:0, border:0, margin:0}}]]);
182+
this.linebreakSpan = HTML.Element("span",{className:"MathJax_SVG_LineBox"});
178183

179184
// Set up styles
180185
return AJAX.Styles(this.config.styles,["InitializeSVG",this]);
@@ -190,7 +195,7 @@
190195
document.body.appendChild(this.ExSpan);
191196
document.body.appendChild(this.linebreakSpan);
192197
this.defaultEx = this.ExSpan.firstChild.offsetHeight/60;
193-
this.defaultWidth = this.linebreakSpan.firstChild.offsetWidth;
198+
this.defaultWidth = this.linebreakSpan.offsetWidth;
194199
document.body.removeChild(this.linebreakSpan);
195200
document.body.removeChild(this.ExSpan);
196201
},
@@ -256,16 +261,15 @@
256261
test = script.previousSibling; div = test.previousSibling;
257262
jax = script.MathJax.elementJax; if (!jax) continue;
258263
ex = test.firstChild.offsetHeight/60;
259-
cwidth = div.previousSibling.firstChild.offsetWidth / this.config.scale * 100;
260-
if (relwidth) {maxwidth = cwidth}
264+
cwidth = Math.max(0,(div.previousSibling.offsetWidth-2) / this.config.scale * 100);
261265
if (ex === 0 || ex === "NaN") {
262266
// can't read width, so move to hidden div for processing
263267
// (this will cause a reflow for each math element that is hidden)
264268
this.hiddenDiv.appendChild(div);
265269
jax.SVG.isHidden = true;
266270
ex = this.defaultEx; cwidth = this.defaultWidth;
267-
if (relwidth) {maxwidth = cwidth}
268271
}
272+
if (relwidth) {maxwidth = cwidth}
269273
jax.SVG.ex = ex;
270274
jax.SVG.em = em = ex / SVG.TeX.x_height * 1000; // scale ex to x_height
271275
jax.SVG.cwidth = cwidth/em * 1000;
@@ -2089,13 +2093,14 @@
20892093
var style = svg.element.style, px = SVG.TeX.x_height/SVG.ex;
20902094
var H = (Math.ceil(svg.H/px)+1)*px+SVG.HFUZZ, // round to pixels and add padding
20912095
D = (Math.ceil(svg.D/px)+1)*px+SVG.DFUZZ;
2092-
svg.element.setAttribute("width",SVG.Ex(l+svg.w+r));
2096+
var w = l + svg.w + r;
2097+
svg.element.setAttribute("width",SVG.Ex(w));
20932098
svg.element.setAttribute("height",SVG.Ex(H+D));
20942099
style.verticalAlign = SVG.Ex(-D);
20952100
if (l) style.marginLeft = SVG.Ex(-l);
20962101
if (r) style.marginRight = SVG.Ex(-r);
20972102
svg.element.setAttribute("viewBox",SVG.Fixed(-l,1)+" "+SVG.Fixed(-H,1)+" "+
2098-
SVG.Fixed(l+svg.w+r,1)+" "+SVG.Fixed(H+D,1));
2103+
SVG.Fixed(w,1)+" "+SVG.Fixed(H+D,1));
20992104
//
21002105
// If there is extra height or depth, hide that
21012106
//
@@ -2105,6 +2110,12 @@
21052110
style.verticalAlign = SVG.Ex(-svg.d);
21062111
}
21072112
//
2113+
// The approximate ex can cause full-width equations to be too wide,
2114+
// so if they are close to full width, make sure they aren't too big.
2115+
//
2116+
if (Math.abs(w-SVG.cwidth) < 10)
2117+
style.maxWidth = SVG.Fixed(SVG.cwidth*SVG.em/1000);
2118+
//
21082119
// Add it to the MathJax span
21092120
//
21102121
var alttext = this.Get("alttext");
@@ -2131,7 +2142,7 @@
21312142
if (shift) {
21322143
HUB.Insert(style,({
21332144
left: {marginLeft: SVG.Ex(shift)},
2134-
right: {marginRight: SVG.Ex(-shift), marginLeft: SVG.Ex(Math.max(0,shift-(l+svg.w+r)))},
2145+
right: {marginRight: SVG.Ex(-shift), marginLeft: SVG.Ex(Math.max(0,shift-(w)))},
21352146
center: {marginLeft: SVG.Ex(shift), marginRight: SVG.Ex(-shift)}
21362147
})[values.indentalign]);
21372148
}

0 commit comments

Comments
 (0)