Skip to content

Commit

Permalink
Merge pull request #1075 from mathjax/require-font
Browse files Browse the repository at this point in the history
Make tex font packages work with \require
  • Loading branch information
dpvc authored Mar 18, 2024
2 parents 7a5dbdc + 88bad3d commit 5ed20c7
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 55 deletions.
20 changes: 20 additions & 0 deletions components/mjs/input/tex/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {combineDefaults} from '#js/components/global.js';

export function fontExtension(id, name, pkg = `@mathjax/${name}`) {
if (MathJax.config?.loader) {
const FONTPATH = (typeof document === 'undefined' ? pkg :
`https://cdn.jsdelivr.net/npm/${name}`);
const path = name.replace(/-font-extension$/, '-extension');
const extension = name.replace(/-font-extension$/, '');
combineDefaults(MathJax.config.loader, 'paths', {[path]: FONTPATH});
MathJax.config.loader[id] = {
checkReady() {
return MathJax.loader.load(
`[${path}]/${MathJax.config?.startup?.output || 'chtml'}`
).then(() => {
MathJax.startup.document?.outputJax?.addExtension(extension);
});
}
};
}
}
17 changes: 2 additions & 15 deletions components/mjs/input/tex/extensions/bbm/bbm.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import './lib/bbm.js';
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
import {fontExtension} from '../../extension.js';

const FONTPATH = (typeof document === 'undefined' ?
'@mathjax/mathjax-bbm-font-extension' :
'https://cdn.jsdelivr.net/npm/mathjax-bbm-font-extension');

if (MathJax.config?.loader) {
combineDefaults(MathJax.config.loader, 'paths', {
'mathjax-bbm-extension': FONTPATH
});
MathJax.config.loader['[tex]/bbm'] = {
checkReady() {
return MathJax.loader.load(`[mathjax-bbm-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
}
};
}
fontExtension('[tex]/bbm', 'mathjax-bbm-font-extension');
17 changes: 2 additions & 15 deletions components/mjs/input/tex/extensions/bboldx/bboldx.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import './lib/bboldx.js';
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
import {fontExtension} from '../../extension.js';

const FONTPATH = (typeof document === 'undefined' ?
'@mathjax/mathjax-bboldx-font-extension' :
'https://cdn.jsdelivr.net/npm/mathjax-bboldx-font-extension');

if (MathJax.config?.loader) {
combineDefaults(MathJax.config.loader, 'paths', {
'mathjax-bboldx-extension': FONTPATH
});
MathJax.config.loader['[tex]/bboldx'] = {
checkReady() {
return MathJax.loader.load(`[mathjax-bboldx-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
}
};
}
fontExtension('[tex]/bboldx', 'mathjax-bboldx-font-extension');
17 changes: 2 additions & 15 deletions components/mjs/input/tex/extensions/dsfont/dsfont.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import './lib/dsfont.js';
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
import {fontExtension} from '../../extension.js';

const FONTPATH = (typeof document === 'undefined' ?
'@mathjax/mathjax-dsfont-font-extension' :
'https://cdn.jsdelivr.net/npm/mathjax-dsfont-font-extension');

if (MathJax.config?.loader) {
combineDefaults(MathJax.config.loader, 'paths', {
'mathjax-dsfont-extension': FONTPATH
});
MathJax.config.loader['[tex]/dsfont'] = {
checkReady() {
return MathJax.loader.load(`[mathjax-dsfont-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
}
};
}
fontExtension('[tex]/dsfont', 'mathjax-dsfont-font-extension');
12 changes: 7 additions & 5 deletions ts/input/tex/bboldx/BboldxConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ export const BboldxConfiguration = Configuration.create('bboldx', {
bboldx: {
bfbb: false,
light: false
},
// add text macros by default to textmacros
textmacros: {
packages: {'[+]': ['text-bboldx']}
}
}
},
config(_config, jax) {
const {textConf, parseOptions} = jax.parseOptions.packageData.get('textmacros');
parseOptions.options.textmacros.packages.push('text-bboldx');
textConf.add('text-bboldx', jax, {});
},
priority: 3 // load before base, since we override \mathbb
});

11 changes: 11 additions & 0 deletions ts/output/chtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ CommonOutputJax<
this.wrapperUsage = new Usage<string>();
}

/**
* @override
*/
public addExtension(name: string): string[] {
const css = super.addExtension(name);
if (css.length && this.options.adaptiveCSS && this.chtmlStyles) {
this.adaptor.insertRules(this.chtmlStyles, css);
}
return [];
}

/**
* @override
*/
Expand Down
20 changes: 19 additions & 1 deletion ts/output/chtml/FontData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {CharMap, CharOptions, CharDataArray, VariantData,
DelimiterData, FontData, FontExtensionData, DIRECTION} from '../common/FontData.js';
import {Usage} from './Usage.js';
import {StringMap} from './Wrapper.js';
import {StyleList, StyleData} from '../../util/StyleList.js';
import {StyleList, StyleData, CssStyles} from '../../util/StyleList.js';
import {em} from '../../util/lengths.js';

export * from '../common/FontData.js';
Expand Down Expand Up @@ -188,6 +188,24 @@ export class ChtmlFontData extends FontData<ChtmlCharOptions, ChtmlVariantData,
data.fonts && this.addDynamicFontCss(this.defaultStyles, data.fonts, data.fontURL);
}

/**
* @override
*/
public addExtension(
data: ChtmlFontExtensionData<ChtmlCharOptions, ChtmlDelimiterData>,
prefix: string = ''
): string[] {
super.addExtension(data, prefix);
if (!data.fonts || !this.options.adaptiveCSS) {
return [];
}
const css = {};
const styles = new CssStyles();
(this.constructor as typeof ChtmlFontData).addDynamicFontCss(css, data.fonts, data.fontURL);
styles.addStyles(css);
return styles.getStyleRules();
}

/***********************************************************************/

/**
Expand Down
11 changes: 11 additions & 0 deletions ts/output/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ export abstract class CommonOutputJax<
}
}

/**
* Add a registered font extension to the output jax's font.
*
* @param {string} name The name of the extension to add to this output jax's font
* @return {string[]} New CSS rules needed for the font
*/
public addExtension(name: string): string[] {
const font = this.font.CLASS.dynamicExtensions.get(name);
return this.font.addExtension(font.data);
}


/*****************************************************************/

Expand Down
13 changes: 9 additions & 4 deletions ts/output/common/FontData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export type DynamicFont = {
files: DynamicFileList;
sizeN: number;
stretchN: number;
data: FontExtensionData<any, any>
};

/**
Expand Down Expand Up @@ -623,7 +624,7 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
/**
* The font extension dynamic data
*/
protected static dynamicExtensions: DynamicFontMap = new Map();
public static dynamicExtensions: DynamicFontMap = new Map();

/**
* The font options
Expand Down Expand Up @@ -792,7 +793,8 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
prefix: prefix,
files: this.defineDynamicFiles(data.ranges, data.name),
sizeN: this.defaultSizeVariants.length,
stretchN: this.defaultStretchVariants.length
stretchN: this.defaultStretchVariants.length,
data: data
};
this.dynamicExtensions.set(data.name, extension);
for (const [src, dst] of [
Expand Down Expand Up @@ -853,14 +855,16 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
*
* @param {FontExtensionData} data The data for the font extension to merge into this font.
* @param {string} prefix The [prefix] to add to all component names
* @return {string[]} The new CSS rules needed for this extension
*/
public addExtension(data: FontExtensionData<C, D>, prefix: string = '') {
public addExtension(data: FontExtensionData<C, D>, prefix: string = ''): string[] {
const dynamicFont = {
name: data.name,
prefix: prefix,
files: this.CLASS.defineDynamicFiles(data.ranges, prefix),
sizeN: this.sizeVariants.length,
stretchN: this.stretchVariants.length
stretchN: this.stretchVariants.length,
data: data
};
this.CLASS.dynamicExtensions.set(data.name, dynamicFont);

Expand All @@ -885,6 +889,7 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
if (data.ranges) {
this.defineDynamicCharacters(dynamicFont.files);
}
return [];
}

/**
Expand Down

0 comments on commit 5ed20c7

Please sign in to comment.