Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions components/mjs/output/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@ import {combineDefaults, combineWithMathJax} from '#js/components/global.js';
import {Package} from '#js/components/package.js';
import {hasWindow} from '#js/util/context.js';

export const FONTPATH = hasWindow ?
'https://cdn.jsdelivr.net/npm/@mathjax/%%FONT%%-font':
'@mathjax/%%FONT%%-font';
export const FONTPATH = hasWindow
? 'https://cdn.jsdelivr.net/npm/@mathjax/%%FONT%%-font'
: '@mathjax/%%FONT%%-font';

export function configFont(font, jax, config, extension = '') {
const path = (config.fontPath || (FONTPATH + extension));
const name = (font.match(/^[a-z]+:/) ? (font.match(/[^/:\\]*$/) || [jax])[0] : font);
combineDefaults(MathJax.config.loader, 'paths', {
[name+extension]: (name === font ? path.replace(/%%FONT%%/g, font) : font)
});
return `[${name}${extension}]`;
}

export function configExtensions(jax, config) {
const extensions = [];
for (const name of (config.fontExtensions || [])) {
const font = configFont(name, jax, config, '-extension');
const module = `${font}/${jax}`
extensions.push(module);
}
return extensions;
}

export const OutputUtil = {
config(jax, jaxClass, defaultFont, fontClass) {

Expand All @@ -20,15 +40,15 @@ export const OutputUtil = {
}

if (font.charAt(0) !== '[') {
const path = (config.fontPath || FONTPATH);
const name = (font.match(/^[a-z]+:/) ? (font.match(/[^/:\\]*$/) || [jax])[0] : font);
combineDefaults(MathJax.config.loader, 'paths', {
[name]: (name === font ? path.replace(/%%FONT%%/g, font) : font)
});
font = `[${name}]`;
font = configFont(font, jax, config);
}
const name = font.substring(1, font.length - 1);

const extensions = configExtensions(jax, config);
if (extensions.length) {
MathJax.loader.addPackageData(`${font}/${jax}`, {extraLoads: extensions});
}

if (name !== defaultFont || !fontClass) {

MathJax.loader.addPackageData(`output/${jax}`, {extraLoads: [`${font}/${jax}`]});
Expand Down
22 changes: 21 additions & 1 deletion ts/output/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export abstract class CommonOutputJax<
LinebreakVisitor: null, // The LinebreakVisitor to use
},
font: '', // the font component to load
fontExtensions: [], // the font extensions to load
htmlHDW: 'auto', // 'use', 'force', or 'ignore' data-mjx-hdw attributes
wrapperFactory: null, // The wrapper factory to use
fontData: null, // The FontData object to use
Expand Down Expand Up @@ -183,6 +184,15 @@ export abstract class CommonOutputJax<
},
};

/**
* The font to use for generic extensions
*/
public static genericFont: FontDataClass<
CharOptions,
VariantData<CharOptions>,
DelimiterData
>;

/**
* Used for collecting styles needed for the output jax
*/
Expand Down Expand Up @@ -305,6 +315,8 @@ export abstract class CommonOutputJax<
this.styleJson = this.options.styleJson || new StyleJsonSheet();
this.font = font || new fontClass(fontOptions);
this.font.setOptions({ mathmlSpacing: this.options.mathmlSpacing });
/* prettier-ignore */
(this.constructor as typeof CommonOutputJax<N, T, D, WW, WF, WC, CC, VV, DD, FD, FC>).genericFont = fontClass;
this.unknownCache = new Map();
const linebreaks = (this.options.linebreaks.LinebreakVisitor ||
LinebreakVisitor) as typeof Linebreaks;
Expand Down Expand Up @@ -349,9 +361,17 @@ export abstract class CommonOutputJax<
* @override
*/
public typeset(math: MathItem<N, T, D>, html: MathDocument<N, T, D>) {
/* prettier-ignore */
const CLASS = (this.constructor as typeof CommonOutputJax<N, T, D, WW, WF, WC, CC, VV, DD, FD, FC>);
const generic = CLASS.genericFont;
CLASS.genericFont = this.font.constructor as FontDataClass<CC, VV, DD>;
this.setDocument(html);
const node = this.createNode();
this.toDOM(math, node, html);
try {
this.toDOM(math, node, html);
} finally {
CLASS.genericFont = generic;
}
return node;
}

Expand Down