Skip to content

Commit a012354

Browse files
committed
Add support for matching web fonts in HTML-CSS. Change the names of the paramters that control it in NativeMML (so both have the same names). Add a global matchWebFonts option to control whether to do the loop to check for web fonts (off by default). Resolves more of issue #558.
1 parent c777547 commit a012354

File tree

5 files changed

+102
-20
lines changed

5 files changed

+102
-20
lines changed

unpacked/MathJax.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,10 @@ MathJax.Hub = {
17411741
showMathMenuMSIE: true, // separtely determine if MSIE should have math menu
17421742
// (since the code for that is a bit delicate)
17431743

1744+
matchWebFonts: false, // true means look for web fonts that may cause changes in the
1745+
// scaling factors for the math (off by default since it
1746+
// uses a loop every time math is rendered).
1747+
17441748
menuSettings: {
17451749
zoom: "None", // when to do MathZoom
17461750
CTRL: false, // require CTRL for MathZoom?
@@ -1906,6 +1910,7 @@ MathJax.Hub = {
19061910

19071911
takeAction: function (action,element,callback) {
19081912
var ec = this.elementCallback(element,callback);
1913+
console.log(element);
19091914
var queue = MathJax.Callback.Queue(["Clear",this.signal]);
19101915
for (var i = 0, m = ec.elements.length; i < m; i++) {
19111916
if (ec.elements[i]) {
@@ -2229,6 +2234,12 @@ MathJax.Hub = {
22292234
},
22302235

22312236
elementScripts: function (element) {
2237+
if (element instanceof Array) {
2238+
var scripts = [];
2239+
for (var i = 0, m = element.length; i < m; i++)
2240+
{scripts.push.apply(scripts,this.elementScripts(element[i]))}
2241+
return scripts;
2242+
}
22322243
if (typeof(element) === 'string') {element = document.getElementById(element)}
22332244
if (!document.body) {document.body = document.getElementsByTagName("body")[0]}
22342245
if (element == null) {element = document.body}

unpacked/jax/output/HTML-CSS/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ MathJax.OutputJax["HTML-CSS"] = MathJax.OutputJax({
4444
undefinedFamily: "STIXGeneral,'Arial Unicode MS',serif", // fonts to use for unknown unicode characters
4545
mtextFontInherit: false, // to make <mtext> be in page font rather than MathJax font
4646

47+
fontCheckDelay: 500, // initial delay for the first check for web fonts
48+
// (set to null to prevent the checks)
49+
fontCheckTimeout: 15 * 1000, // how long to keep looking for font changes (15 seconds)
50+
4751
EqnChunk: (MathJax.Hub.Browser.isMobile ? 10: 50),
4852
// number of equations to process before showing them
4953
EqnChunkFactor: 1.5, // chunk size is multiplied by this after each chunk

unpacked/jax/output/HTML-CSS/jax.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,15 +618,25 @@
618618
//
619619
state.HTMLCSSeqn += (state.i - state.HTMLCSSi); state.HTMLCSSi = state.i;
620620
if (state.HTMLCSSeqn >= state.HTMLCSSlast + state.HTMLCSSchunk) {
621-
this.postTranslate(state);
621+
this.postTranslate(state,true);
622622
state.HTMLCSSchunk = Math.floor(state.HTMLCSSchunk*this.config.EqnChunkFactor);
623623
state.HTMLCSSdelay = true; // delay if there are more scripts
624624
}
625625
}
626626
},
627627

628-
postTranslate: function (state) {
628+
postTranslate: function (state,partial) {
629629
var scripts = state.jax[this.id];
630+
if (!partial && HUB.config.matchWebFonts && this.config.matchFontHeight) {
631+
//
632+
// Check for changes in the web fonts that might affect the font
633+
// size for math elements. This is a periodic check that goes on
634+
// until a timeout is reached.
635+
//
636+
AJAX.timer.start(AJAX,["checkFonts",this,state.jax[this.id]],
637+
this.config.fontCheckDelay,this.config.fontCheckTimeout);
638+
639+
}
630640
if (!this.hideProcessedMath) return;
631641
//
632642
// Reveal this chunk of math
@@ -651,7 +661,7 @@
651661
}
652662
if (this.forceReflow) {
653663
// WebKit can misplace some elements that should wrap to the next line
654-
// but gets them right ona reflow, so force reflow by toggling a stylesheet
664+
// but gets them right on a reflow, so force reflow by toggling a stylesheet
655665
var sheet = (document.styleSheets||[])[0]||{};
656666
sheet.disabled = true; sheet.disabled = false;
657667
}
@@ -660,6 +670,57 @@
660670
//
661671
state.HTMLCSSlast = state.HTMLCSSeqn;
662672
},
673+
674+
checkFonts: function (check,scripts) {
675+
if (check.time(function () {})) return;
676+
var size = [], i, m;
677+
//
678+
// Add the elements used for testing ex and em sizes
679+
//
680+
for (i = 0, m = scripts.length; i < m; i++) {
681+
script = scripts[i];
682+
if (script.parentNode && script.MathJax.elementJax) {
683+
script.parentNode.insertBefore(this.EmExSpan.cloneNode(true),script);
684+
}
685+
}
686+
//
687+
// Check to see if anything has changed
688+
//
689+
for (i = 0, m = scripts.length; i < m; i++) {
690+
script = scripts[i]; if (!script.parentNode) continue;
691+
var jax = script.MathJax.elementJax; if (!jax) continue;
692+
var span = document.getElementById(jax.inputID+"-Frame");
693+
//
694+
// Check if ex or mex has changed
695+
//
696+
var test = script.previousSibling, div = test.previousSibling;
697+
var ex = test.firstChild.offsetHeight/60;
698+
var em = test.lastChild.lastChild.offsetHeight/60;
699+
if (ex === 0 || ex === "NaN") {ex = this.defaultEx; em = this.defaultEm}
700+
if (ex !== jax.HTMLCSS.ex || em !== jax.HTMLCSS.em) {
701+
var scale = ex/this.TeX.x_height/em;
702+
scale = Math.floor(Math.max(this.config.minScaleAdjust/100,scale)*this.config.scale);
703+
if (scale/100 !== jax.scale) {size.push(script); scripts[i] = {}}
704+
}
705+
}
706+
//
707+
// Remove markers
708+
//
709+
for (i = 0, m = scripts.length; i < m; i++) {
710+
script = scripts[i];
711+
if (script.parentNode && script.MathJax.elementJax) {
712+
script.parentNode.removeChild(script.previousSibling);
713+
}
714+
}
715+
//
716+
// Rerender the changed items
717+
//
718+
if (size.length) {MathJax.Hub.Queue(["Rerender",MathJax.Hub,[size],{}])}
719+
//
720+
// Try again later
721+
//
722+
setTimeout(check,check.delay);
723+
},
663724

664725
getJaxFromMath: function (math) {
665726
if (math.parentNode.className === "MathJax_Display") {math = math.parentNode}

unpacked/jax/output/NativeMML/config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ MathJax.OutputJax.NativeMML = MathJax.OutputJax({
3636
scale: 100, // scaling factor for all math
3737
minScaleAdjust: 50, // minimum scaling to adjust to surrounding text
3838
// (since the code for that is a bit delicate)
39-
widthCheckDelay: 500, // initial delay for the first width check for web fonts
40-
// (set to null to prevent the width checks)
41-
widthCheckTimeout: 15 * 1000, // how long to keep looking for width changes (15 seconds)
39+
40+
fontCheckDelay: 500, // initial delay for the first width check for web fonts
41+
// (set to null to prevent the width checks)
42+
fontCheckTimeout: 15 * 1000, // how long to keep looking for width changes (15 seconds)
4243

4344
styles: {
4445
"DIV.MathJax_MathML": {

unpacked/jax/output/NativeMML/jax.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,14 @@
327327
},
328328

329329
postTranslate: function (state) {
330-
if (!isMSIE && this.config.widthCheckDelay != null) {
330+
if (!isMSIE && HUB.config.matchWebFonts) {
331331
//
332332
// Check for changes in the web fonts that might affect the sizes
333333
// of math elements. This is a periodic check that goes on until
334334
// a timeout is reached.
335335
//
336-
AJAX.timer.start(AJAX,["checkWidths",this,state.jax[this.id]],
337-
this.config.widthCheckDelay,this.config.widthCheckTimeout);
336+
AJAX.timer.start(AJAX,["checkFonts",this,state.jax[this.id]],
337+
this.config.fontCheckDelay,this.config.fontCheckTimeout);
338338
}
339339
if (this.forceReflow) {
340340
//
@@ -355,36 +355,37 @@
355355
// sizes of these have been stored in the NativeMML object of the
356356
// element jax so that we can check for them here.
357357
//
358-
checkWidths: function (check,scripts) {
358+
checkFonts: function (check,scripts) {
359359
if (check.time(function () {})) return;
360-
var adjust = [], mtd = [], size = [], i, m;
360+
var adjust = [], mtd = [], size = [], i, m, script;
361361
//
362362
// Add the elements used for testing ex and em sizes
363363
//
364364
for (i = 0, m = scripts.length; i < m; i++) {
365-
if (!scripts[i].parentNode || !scripts[i].MathJax.elementJax) continue;
366-
scripts[i].parentNode.insertBefore(this.EmExSpan.cloneNode(true),scripts[i]);
365+
script = scripts[i];
366+
if (script.parentNode && script.MathJax.elementJax) {
367+
script.parentNode.insertBefore(this.EmExSpan.cloneNode(true),script);
368+
}
367369
}
368370
//
369371
// Check to see if anything has changed
370372
//
371373
for (i = 0, m = scripts.length; i < m; i++) {
372-
if (!scripts[i].parentNode) continue;
373-
var jax = scripts[i].MathJax.elementJax;
374-
if (!jax) continue;
374+
script = scripts[i]; if (!script.parentNode) continue;
375+
var jax = script.MathJax.elementJax; if (!jax) continue;
375376
var span = document.getElementById(jax.inputID+"-Frame");
376377
var math = span.getElementsByTagName("math")[0]; if (!math) continue;
377378
jax = jax.NativeMML;
378379
//
379380
// Check if ex or mex has changed
380381
//
381-
var test = scripts[i].previousSibling;
382+
var test = script.previousSibling;
382383
var ex = test.firstChild.offsetWidth/60;
383384
var mex = test.lastChild.offsetWidth/60;
384385
if (ex === 0 || ex === "NaN") {ex = this.defaultEx; mex = this.defaultMEx}
385386
var newEx = (ex !== jax.ex);
386387
if (newEx || mex != jax.mex) {
387-
scale = (this.config.matchFontHeight && mex > 1 ? ex/mex : 1);
388+
var scale = (this.config.matchFontHeight && mex > 1 ? ex/mex : 1);
388389
scale = Math.floor(Math.max(this.config.minScaleAdjust/100,scale) * this.config.scale);
389390
if (scale/100 !== jax.scale) {size.push([span.style,scale])}
390391
jax.scale = scale/100; jax.fontScale = scale+"%"; jax.ex = ex; jax.mex = mex;
@@ -414,8 +415,9 @@
414415
// Remove markers
415416
//
416417
for (i = 0, m = scripts.length; i < m; i++) {
417-
if (scripts[i].parentNode && scripts[i].MathJax.elementJax) {
418-
scripts[i].parentNode.removeChild(scripts[i].previousSibling);
418+
script = scripts[i];
419+
if (script.parentNode && script.MathJax.elementJax) {
420+
script.parentNode.removeChild(script.previousSibling);
419421
}
420422
}
421423
//
@@ -438,6 +440,9 @@
438440
style = style.replace(/(($|;)\s*min-width:).*?ex/,"$1 "+mtd[i][1].toFixed(3)+"ex");
439441
mtd[i][0].setAttribute("style",style);
440442
}
443+
//
444+
// Try again later
445+
//
441446
setTimeout(check,check.delay);
442447
},
443448

0 commit comments

Comments
 (0)