From 005162c9ac466d3c22a71c90544098a4a848a51e Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Thu, 9 Nov 2023 16:10:51 -0500 Subject: [PATCH] Better measuring of text for text that is in a native font rather than MathJax fonts --- ts/output/chtml.ts | 12 ++++++++---- ts/output/svg.ts | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ts/output/chtml.ts b/ts/output/chtml.ts index 9f41a5d15..f8f846552 100644 --- a/ts/output/chtml.ts +++ b/ts/output/chtml.ts @@ -286,8 +286,7 @@ CommonOutputJax< // and call to getBBox().w in TextNode.ts) // if (width !== null) { - const metrics = this.math.metrics; - styles.width = Math.round(width * metrics.em * metrics.scale * rscale) + 'px'; + styles.width = this.fixed(width * this.math.metrics.scale * rscale) + 'em'; } // return this.html('mjx-utext', {variant: variant, style: styles}, [this.text(text)]); @@ -308,11 +307,16 @@ CommonOutputJax< // adaptor.setStyle(text, 'font-family', adaptor.getStyle(text, 'font-family').replace(/MJXZERO, /g, '')); // - const style = {position: 'absolute', 'white-space': 'nowrap'}; + const em = this.math.metrics.em; + const style = { + position: 'absolute', top: 0, left: 0, + 'white-space': 'nowrap', + 'font-size': this.fixed(em, 3) + 'px' + }; const node = this.html('mjx-measure-text', {style}, [text]); adaptor.append(adaptor.parent(this.math.start.node), this.container); adaptor.append(this.container, node); - let w = adaptor.nodeSize(text, this.math.metrics.em)[0] / this.math.metrics.scale; + let w = adaptor.nodeSize(text, em)[0]; adaptor.remove(this.container); adaptor.remove(node); return {w: w, h: .75, d: .2}; diff --git a/ts/output/svg.ts b/ts/output/svg.ts index fdd58ff54..6f09dc319 100644 --- a/ts/output/svg.ts +++ b/ts/output/svg.ts @@ -488,7 +488,7 @@ CommonOutputJax< const ex = this.fixed(this.font.params.x_height * 1000, 1); const svg = this.svg('svg', { position: 'absolute', visibility: 'hidden', - width: '1ex', height: '1ex', + width: '1ex', height: '1ex', top: 0, left: 0, viewBox: [0, 0, ex, ex].join(' ') }, [text]); adaptor.append(adaptor.body(adaptor.document), svg);