From 14c8099abb424fedc04a83f89644396da1dbcad3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Jun 2020 00:34:16 +0530 Subject: [PATCH] Port the in-browser viewer to use MathJax v3 --- resources/pdf-mathjax-loader.js | 1 + src/pyj/read_book/mathjax.pyj | 69 ++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/resources/pdf-mathjax-loader.js b/resources/pdf-mathjax-loader.js index bb941b807c..5597a605f7 100644 --- a/resources/pdf-mathjax-loader.js +++ b/resources/pdf-mathjax-loader.js @@ -15,6 +15,7 @@ if (for_pdf_renderer) document.title = "mathjax-load-complete"; else document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done")); } + // also do any changes in mathjax.pyj for the in-browser reader window.MathJax = {}; window.MathJax.options = { renderActions: { diff --git a/src/pyj/read_book/mathjax.pyj b/src/pyj/read_book/mathjax.pyj index b91c720a72..50cdf09ad3 100644 --- a/src/pyj/read_book/mathjax.pyj +++ b/src/pyj/read_book/mathjax.pyj @@ -24,12 +24,69 @@ def postprocess(link_uid): a.setAttribute('data-' + link_uid, JSON.stringify({'frag':href[1:]})) -def apply_mathjax(mathjax_files, link_uid, proceed): - document.documentElement.addEventListener("calibre-mathjax-typeset-done", def(ev): - postprocess(link_uid) - proceed() - ) +def load_mathjax(src): script = E.script(type='text/javascript') script.async = True - script.src = f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/mathjax/startup.js' + script.src = src document.head.appendChild(script) + + +def apply_mathjax(mathjax_files, link_uid, proceed): + if runtime.is_standalone_viewer: + document.documentElement.addEventListener("calibre-mathjax-typeset-done", def(ev): + postprocess(link_uid) + proceed() + ) + load_mathjax(f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/mathjax/startup.js') + return + window.MathJax = v'{}' + window.MathJax.startup = { + 'ready': def (): + window.MathJax.startup.defaultReady() + # monkeypatch the method responsible for mapping font URLs + # to use our blob URLs, see https://github.com/mathjax/MathJax/issues/2458 + window.MathJax.startup.output.font.addFontURLs = def (styles, fonts, url): + base = url.partition('/')[2] + for name in fonts: + clone = v'{}' + font = fonts[name] + for key in Object.keys(font): + clone[key] = font[key] + font_name = clone.src.partition('/')[2].partition('"')[0] + full_name = base + '/' + font_name + src = get_url(mathjax_files, full_name) + clone.src = clone.src.replace(/".+?"/, f'"{src}"') + styles[name] = clone + window.MathJax.startup.promise.then(def(): + postprocess(link_uid) + proceed() + ) + , + } + + # also do any changes in pdf-mathjax-loader.js for the standalone + # viewer/editor/pdf outpu + window.MathJax.loader = { + 'load': v"['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml']", + 'require': def (url): + return new Promise(def (resolve, reject): + name = url.partition('/')[2] + script = document.createElement('script') + script.charset = 'UTF-8' + script.onload = def (): + resolve(url) + script.onerror = def (): + reject(url) + script.src = get_url(mathjax_files, name) + document.head.appendChild(script) + ) + , + } + + for s in document.scripts: + if s.type is 'text/x-mathjax-config': + es = document.createElement('script') + es.text = s.text + document.head.appendChild(es) + document.head.removeChild(es) + load_mathjax(get_url(mathjax_files, 'startup.js'))