Skip to content

Commit 8202d6f

Browse files
committed
Make tex font packages work with \require
1 parent c0071b9 commit 8202d6f

File tree

9 files changed

+83
-55
lines changed

9 files changed

+83
-55
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {combineDefaults} from '#js/components/global.js';
2+
3+
export function fontExtension(id, name, pkg = `@mathjax/${name}`) {
4+
if (MathJax.config?.loader) {
5+
const FONTPATH = (typeof document === 'undefined' ? pkg :
6+
`https://cdn.jsdelivr.net/npm/${name}`);
7+
const path = name.replace(/-font-extension$/, '-extension');
8+
const extension = name.replace(/-font-extension$/, '');
9+
combineDefaults(MathJax.config.loader, 'paths', {[path]: FONTPATH});
10+
MathJax.config.loader[id] = {
11+
checkReady() {
12+
return MathJax.loader.load(
13+
`[${path}]/${MathJax.config?.startup?.output || 'chtml'}`
14+
).then(() => {
15+
MathJax.startup.document?.outputJax?.addExtension(extension);
16+
});
17+
}
18+
};
19+
}
20+
}
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
import './lib/bbm.js';
2-
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
2+
import {fontExtension} from '../../extension.js';
33

4-
const FONTPATH = (typeof document === 'undefined' ?
5-
'@mathjax/mathjax-bbm-font-extension' :
6-
'https://cdn.jsdelivr.net/npm/mathjax-bbm-font-extension');
7-
8-
if (MathJax.config?.loader) {
9-
combineDefaults(MathJax.config.loader, 'paths', {
10-
'mathjax-bbm-extension': FONTPATH
11-
});
12-
MathJax.config.loader['[tex]/bbm'] = {
13-
checkReady() {
14-
return MathJax.loader.load(`[mathjax-bbm-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
15-
}
16-
};
17-
}
4+
fontExtension('[tex]/bbm', 'mathjax-bbm-font-extension');
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
import './lib/bboldx.js';
2-
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
2+
import {fontExtension} from '../../extension.js';
33

4-
const FONTPATH = (typeof document === 'undefined' ?
5-
'@mathjax/mathjax-bboldx-font-extension' :
6-
'https://cdn.jsdelivr.net/npm/mathjax-bboldx-font-extension');
7-
8-
if (MathJax.config?.loader) {
9-
combineDefaults(MathJax.config.loader, 'paths', {
10-
'mathjax-bboldx-extension': FONTPATH
11-
});
12-
MathJax.config.loader['[tex]/bboldx'] = {
13-
checkReady() {
14-
return MathJax.loader.load(`[mathjax-bboldx-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
15-
}
16-
};
17-
}
4+
fontExtension('[tex]/bboldx', 'mathjax-bboldx-font-extension');
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
import './lib/dsfont.js';
2-
import {MathJax, combineDefaults} from 'mathjax-full/js/components/global.js';
2+
import {fontExtension} from '../../extension.js';
33

4-
const FONTPATH = (typeof document === 'undefined' ?
5-
'@mathjax/mathjax-dsfont-font-extension' :
6-
'https://cdn.jsdelivr.net/npm/mathjax-dsfont-font-extension');
7-
8-
if (MathJax.config?.loader) {
9-
combineDefaults(MathJax.config.loader, 'paths', {
10-
'mathjax-dsfont-extension': FONTPATH
11-
});
12-
MathJax.config.loader['[tex]/dsfont'] = {
13-
checkReady() {
14-
return MathJax.loader.load(`[mathjax-dsfont-extension]/${MathJax.config?.startup?.output || 'chtml'}`);
15-
}
16-
};
17-
}
4+
fontExtension('[tex]/dsfont', 'mathjax-dsfont-font-extension');

ts/input/tex/bboldx/BboldxConfiguration.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ export const BboldxConfiguration = Configuration.create('bboldx', {
5252
bboldx: {
5353
bfbb: false,
5454
light: false
55-
},
56-
// add text macros by default to textmacros
57-
textmacros: {
58-
packages: {'[+]': ['text-bboldx']}
5955
}
60-
}
56+
},
57+
config(_config, jax) {
58+
const {textConf, parseOptions} = jax.parseOptions.packageData.get('textmacros');
59+
parseOptions.options.textmacros.packages.push('text-bboldx');
60+
textConf.add('text-bboldx', jax, {});
61+
},
62+
priority: 3 // load before base, since we override \mathbb
6163
});
6264

ts/output/chtml.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ CommonOutputJax<
162162
this.wrapperUsage = new Usage<string>();
163163
}
164164

165+
/**
166+
* @override
167+
*/
168+
public addExtension(name: string): string[] {
169+
const css = super.addExtension(name);
170+
if (css.length && this.options.adaptiveCSS && this.chtmlStyles) {
171+
this.adaptor.insertRules(this.chtmlStyles, css);
172+
}
173+
return [];
174+
}
175+
165176
/**
166177
* @override
167178
*/

ts/output/chtml/FontData.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {CharMap, CharOptions, CharDataArray, VariantData,
2525
DelimiterData, FontData, FontExtensionData, DIRECTION} from '../common/FontData.js';
2626
import {Usage} from './Usage.js';
2727
import {StringMap} from './Wrapper.js';
28-
import {StyleList, StyleData} from '../../util/StyleList.js';
28+
import {StyleList, StyleData, CssStyles} from '../../util/StyleList.js';
2929
import {em} from '../../util/lengths.js';
3030

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

191+
/**
192+
* @override
193+
*/
194+
public addExtension(
195+
data: ChtmlFontExtensionData<ChtmlCharOptions, ChtmlDelimiterData>,
196+
prefix: string = ''
197+
): string[] {
198+
super.addExtension(data, prefix);
199+
if (data.fonts && this.options.adaptiveCSS) {
200+
const css = {};
201+
const styles = new CssStyles();
202+
(this.constructor as typeof ChtmlFontData).addDynamicFontCss(css, data.fonts, data.fontURL);
203+
styles.addStyles(css);
204+
return styles.getStyleRules();
205+
}
206+
return [];
207+
}
208+
191209
/***********************************************************************/
192210

193211
/**

ts/output/common.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ export abstract class CommonOutputJax<
266266
}
267267
}
268268

269+
/**
270+
* Add a registered font extension to the output jax's font.
271+
*
272+
* @param {string} name The name of the extension to add to this output jax's font
273+
* @return {string[]} New CSS rules needed for the font
274+
*/
275+
public addExtension(name: string): string[] {
276+
const font = this.font.CLASS.dynamicExtensions.get(name);
277+
return this.font.addExtension(font.data);
278+
}
279+
269280

270281
/*****************************************************************/
271282

ts/output/common/FontData.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export type DynamicFont = {
293293
files: DynamicFileList;
294294
sizeN: number;
295295
stretchN: number;
296+
data: FontExtensionData<any, any>
296297
};
297298

298299
/**
@@ -623,7 +624,7 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
623624
/**
624625
* The font extension dynamic data
625626
*/
626-
protected static dynamicExtensions: DynamicFontMap = new Map();
627+
public static dynamicExtensions: DynamicFontMap = new Map();
627628

628629
/**
629630
* The font options
@@ -792,7 +793,8 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
792793
prefix: prefix,
793794
files: this.defineDynamicFiles(data.ranges, data.name),
794795
sizeN: this.defaultSizeVariants.length,
795-
stretchN: this.defaultStretchVariants.length
796+
stretchN: this.defaultStretchVariants.length,
797+
data: data
796798
};
797799
this.dynamicExtensions.set(data.name, extension);
798800
for (const [src, dst] of [
@@ -853,14 +855,16 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
853855
*
854856
* @param {FontExtensionData} data The data for the font extension to merge into this font.
855857
* @param {string} prefix The [prefix] to add to all component names
858+
* @return {string[]} The new CSS rules needed for this extension
856859
*/
857-
public addExtension(data: FontExtensionData<C, D>, prefix: string = '') {
860+
public addExtension(data: FontExtensionData<C, D>, prefix: string = ''): string[] {
858861
const dynamicFont = {
859862
name: data.name,
860863
prefix: prefix,
861864
files: this.CLASS.defineDynamicFiles(data.ranges, prefix),
862865
sizeN: this.sizeVariants.length,
863-
stretchN: this.stretchVariants.length
866+
stretchN: this.stretchVariants.length,
867+
data: data
864868
};
865869
this.CLASS.dynamicExtensions.set(data.name, dynamicFont);
866870

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

890895
/**

0 commit comments

Comments
 (0)