Skip to content

Add fontExtensions configuration option. (mathjax/MathJax#3410) #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions 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,14 @@ 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);
CLASS.genericFont = generic;
return node;
}

Expand Down