Skip to content

Commit 82802ef

Browse files
authored
Merge pull request #799 from mathjax/handle-borders
Handle border and padding CSS in CHTML and SVG output
2 parents 4faec27 + 5979969 commit 82802ef

File tree

25 files changed

+234
-88
lines changed

25 files changed

+234
-88
lines changed

ts/output/chtml/Wrappers/math.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ CommonMathMixin<CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
113113
if (this.bbox.pwidth === BBox.fullWidth) {
114114
adaptor.setAttribute(parent, 'width', 'full');
115115
if (this.jax.table) {
116-
let {L, w, R} = this.jax.table.getBBox();
116+
let {L, w, R} = this.jax.table.getOuterBBox();
117117
if (align === 'right') {
118118
R = Math.max(R || -shift, -shift);
119119
} else if (align === 'left') {

ts/output/chtml/Wrappers/msqrt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class CHTMLmsqrt<N, T, D> extends CommonMsqrtMixin<CHTMLConstructor<any,
7878
// Get the parameters for the spacing of the parts
7979
//
8080
const sbox = surd.getBBox();
81-
const bbox = base.getBBox();
81+
const bbox = base.getOuterBBox();
8282
const [ , q] = this.getPQ(sbox);
8383
const t = this.font.params.rule_thickness;
8484
const H = bbox.h + q + t;

ts/output/chtml/Wrappers/munderover.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ CommonMunderMixin<CHTMLWrapper<any, any, any>, Constructor<CHTMLmsub<any, any, a
8585
) as N;
8686
this.baseChild.toCHTML(base);
8787
this.scriptChild.toCHTML(under);
88-
const basebox = this.baseChild.getBBox();
89-
const underbox = this.scriptChild.getBBox();
88+
const basebox = this.baseChild.getOuterBBox();
89+
const underbox = this.scriptChild.getOuterBBox();
9090
const k = this.getUnderKV(basebox, underbox)[0];
9191
const delta = (this.isLineBelow ? 0 : this.getDelta(true));
9292
this.adaptor.setStyle(under, 'paddingTop', this.em(k));
@@ -140,8 +140,8 @@ CommonMoverMixin<CHTMLWrapper<any, any, any>, Constructor<CHTMLmsup<any, any, an
140140
const base = this.adaptor.append(this.chtml, this.html('mjx-base')) as N;
141141
this.scriptChild.toCHTML(over);
142142
this.baseChild.toCHTML(base);
143-
const overbox = this.scriptChild.getBBox();
144-
const basebox = this.baseChild.getBBox();
143+
const overbox = this.scriptChild.getOuterBBox();
144+
const basebox = this.baseChild.getOuterBBox();
145145
this.adjustBaseHeight(base, basebox);
146146
const k = this.getOverKU(basebox, overbox)[0];
147147
const delta = (this.isLineAbove ? 0 : this.getDelta());
@@ -207,9 +207,9 @@ CommonMunderoverMixin<CHTMLWrapper<any, any, any>, Constructor<CHTMLmsubsup<any,
207207
this.overChild.toCHTML(over);
208208
this.baseChild.toCHTML(base);
209209
this.underChild.toCHTML(under);
210-
const overbox = this.overChild.getBBox();
211-
const basebox = this.baseChild.getBBox();
212-
const underbox = this.underChild.getBBox();
210+
const overbox = this.overChild.getOuterBBox();
211+
const basebox = this.baseChild.getOuterBBox();
212+
const underbox = this.underChild.getOuterBBox();
213213
this.adjustBaseHeight(base, basebox);
214214
const ok = this.getOverKU(basebox, overbox)[0];
215215
const uk = this.getUnderKV(basebox, underbox)[0];

ts/output/common/OutputJax.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export abstract class CommonOutputJax<
273273
this.math = math;
274274
math.root.setTeXclass(null);
275275
this.nodeMap = new Map<MmlNode, W>();
276-
let bbox = this.factory.wrap(math.root).getBBox();
276+
let bbox = this.factory.wrap(math.root).getOuterBBox();
277277
this.nodeMap = null;
278278
return bbox;
279279
}

ts/output/common/Wrapper.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,34 @@ export class CommonWrapper<
314314
return bbox;
315315
}
316316

317+
/**
318+
* Return the wrapped node's bounding box that includes borders and padding
319+
*
320+
* @param {boolean} save Whether to cache the bbox or not (used for stretchy elements)
321+
* @return {BBox} The computed bounding box
322+
*/
323+
public getOuterBBox(save: boolean = true): BBox {
324+
const bbox = this.getBBox(save);
325+
if (!this.styles) return bbox;
326+
const obox = new BBox();
327+
Object.assign(obox, bbox);
328+
for (const [name, side] of BBox.StyleAdjust) {
329+
const x = this.styles.get(name);
330+
if (x) {
331+
(obox as any)[side] += this.length2em(x, 1, obox.rscale);
332+
}
333+
}
334+
return obox;
335+
}
336+
317337
/**
318338
* @param {BBox} bbox The bounding box to modify (either this.bbox, or an empty one)
319339
* @param {boolean} recompute True if we are recomputing due to changes in children that have percentage widths
320340
*/
321341
protected computeBBox(bbox: BBox, recompute: boolean = false) {
322342
bbox.empty();
323343
for (const child of this.childNodes) {
324-
bbox.append(child.getBBox());
344+
bbox.append(child.getOuterBBox());
325345
}
326346
bbox.clean();
327347
if (this.fixesPWidth && this.setChildPWidths(recompute)) {
@@ -348,7 +368,7 @@ export class CommonWrapper<
348368
}
349369
let changed = false;
350370
for (const child of this.childNodes) {
351-
const cbox = child.getBBox();
371+
const cbox = child.getOuterBBox();
352372
if (cbox.pwidth && child.setChildPWidths(recompute, w === null ? cbox.w : w, clear)) {
353373
changed = true;
354374
}

ts/output/common/Wrappers/maction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export function CommonMactionMixin<
184184
* @override
185185
*/
186186
public computeBBox(bbox: BBox, recompute: boolean = false) {
187-
bbox.updateFrom(this.selected.getBBox());
187+
bbox.updateFrom(this.selected.getOuterBBox());
188188
this.selected.setChildPWidths(recompute);
189189
}
190190

ts/output/common/Wrappers/mfenced.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function CommonMfencedMixin<T extends WrapperConstructor>(Base: T): Mfenc
135135
* @override
136136
*/
137137
public computeBBox(bbox: BBox, recompute: boolean = false) {
138-
bbox.updateFrom(this.mrow.getBBox());
138+
bbox.updateFrom(this.mrow.getOuterBBox());
139139
this.setChildPWidths(recompute);
140140
}
141141

ts/output/common/Wrappers/mfrac.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracCo
159159
* @param {number} t The thickness of the line
160160
*/
161161
public getFractionBBox(bbox: BBox, display: boolean, t: number) {
162-
const nbox = this.childNodes[0].getBBox();
163-
const dbox = this.childNodes[1].getBBox();
162+
const nbox = this.childNodes[0].getOuterBBox();
163+
const dbox = this.childNodes[1].getOuterBBox();
164164
const tex = this.font.params;
165165
const a = tex.axis_height;
166166
const {T, u, v} = this.getTUV(display, t);
@@ -204,8 +204,8 @@ export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracCo
204204
* the separation between the two, and the bboxes themselves.
205205
*/
206206
public getUVQ(display: boolean): {u: number, v: number, q: number, nbox: BBox, dbox: BBox} {
207-
const nbox = this.childNodes[0].getBBox() as BBox;
208-
const dbox = this.childNodes[1].getBBox() as BBox;
207+
const nbox = this.childNodes[0].getOuterBBox();
208+
const dbox = this.childNodes[1].getOuterBBox();
209209
const tex = this.font.params;
210210
//
211211
// Initial offsets (u, v)
@@ -234,7 +234,7 @@ export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracCo
234234
*/
235235
public getBevelledBBox(bbox: BBox, display: boolean) {
236236
const {u, v, delta, nbox, dbox} = this.getBevelData(display);
237-
const lbox = this.bevel.getBBox();
237+
const lbox = this.bevel.getOuterBBox();
238238
bbox.combine(nbox, 0, u);
239239
bbox.combine(lbox, bbox.w - delta / 2, 0);
240240
bbox.combine(dbox, bbox.w - delta / 2, v);
@@ -249,8 +249,8 @@ export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracCo
249249
public getBevelData(display: boolean): {
250250
H: number, delta: number, u: number, v: number, nbox: BBox, dbox: BBox
251251
} {
252-
const nbox = this.childNodes[0].getBBox() as BBox;
253-
const dbox = this.childNodes[1].getBBox() as BBox;
252+
const nbox = this.childNodes[0].getOuterBBox();
253+
const dbox = this.childNodes[1].getOuterBBox();
254254
const delta = (display ? .4 : .15);
255255
const H = Math.max(nbox.scale * (nbox.h + nbox.d), dbox.scale * (dbox.h + dbox.d)) + 2 * delta;
256256
const a = this.font.params.axis_height;
@@ -282,7 +282,7 @@ export function CommonMfracMixin<T extends WrapperConstructor>(Base: T): MfracCo
282282
public getWrapWidth(i: number) {
283283
const attributes = this.node.attributes;
284284
if (attributes.get('bevelled')) {
285-
return this.childNodes[i].getBBox().w;
285+
return this.childNodes[i].getOuterBBox().w;
286286
}
287287
const w = this.getBBox().w;
288288
const thickness = this.length2em(attributes.get('linethickness'));

ts/output/common/Wrappers/mmultiscripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export function CommonMmultiscriptsMixin<
254254
if (child.node.isKind('mprescripts')) {
255255
script = 'psubList';
256256
} else {
257-
lists[script].push(child.getBBox());
257+
lists[script].push(child.getOuterBBox());
258258
script = NextScript[script];
259259
}
260260
}

ts/output/common/Wrappers/mroot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function CommonMrootMixin<T extends MsqrtConstructor>(Base: T): MrootCons
6666
* @override
6767
*/
6868
public combineRootBBox(BBOX: BBox, sbox: BBox, H: number) {
69-
const bbox = this.childNodes[this.root].getBBox();
69+
const bbox = this.childNodes[this.root].getOuterBBox();
7070
const h = this.getRootDimens(sbox, H)[1];
7171
BBOX.combine(bbox, 0, h);
7272
}
@@ -76,7 +76,7 @@ export function CommonMrootMixin<T extends MsqrtConstructor>(Base: T): MrootCons
7676
*/
7777
public getRootDimens(sbox: BBox, H: number) {
7878
const surd = this.childNodes[this.surd] as CommonMo;
79-
const bbox = this.childNodes[this.root].getBBox();
79+
const bbox = this.childNodes[this.root].getOuterBBox();
8080
const offset = (surd.size < 0 ? .5 : .6) * sbox.w;
8181
const {w, rscale} = bbox;
8282
const W = Math.max(w, offset / rscale);

0 commit comments

Comments
 (0)