From 8202d6f24055b55503500e194eced4b2cbafa260 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Thu, 14 Mar 2024 08:00:08 -0400 Subject: [PATCH 1/2] Make tex font packages work with \require --- components/mjs/input/tex/extension.js | 20 +++++++++++++++++++ .../mjs/input/tex/extensions/bbm/bbm.js | 17 ++-------------- .../mjs/input/tex/extensions/bboldx/bboldx.js | 17 ++-------------- .../mjs/input/tex/extensions/dsfont/dsfont.js | 17 ++-------------- ts/input/tex/bboldx/BboldxConfiguration.ts | 12 ++++++----- ts/output/chtml.ts | 11 ++++++++++ ts/output/chtml/FontData.ts | 20 ++++++++++++++++++- ts/output/common.ts | 11 ++++++++++ ts/output/common/FontData.ts | 13 ++++++++---- 9 files changed, 83 insertions(+), 55 deletions(-) create mode 100644 components/mjs/input/tex/extension.js diff --git a/components/mjs/input/tex/extension.js b/components/mjs/input/tex/extension.js new file mode 100644 index 000000000..9631c73bf --- /dev/null +++ b/components/mjs/input/tex/extension.js @@ -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); + }); + } + }; + } +} diff --git a/components/mjs/input/tex/extensions/bbm/bbm.js b/components/mjs/input/tex/extensions/bbm/bbm.js index ce268183f..25dadc20d 100644 --- a/components/mjs/input/tex/extensions/bbm/bbm.js +++ b/components/mjs/input/tex/extensions/bbm/bbm.js @@ -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'); diff --git a/components/mjs/input/tex/extensions/bboldx/bboldx.js b/components/mjs/input/tex/extensions/bboldx/bboldx.js index 32276fa89..cc32d683f 100644 --- a/components/mjs/input/tex/extensions/bboldx/bboldx.js +++ b/components/mjs/input/tex/extensions/bboldx/bboldx.js @@ -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'); diff --git a/components/mjs/input/tex/extensions/dsfont/dsfont.js b/components/mjs/input/tex/extensions/dsfont/dsfont.js index 509910379..fce2c5d9b 100644 --- a/components/mjs/input/tex/extensions/dsfont/dsfont.js +++ b/components/mjs/input/tex/extensions/dsfont/dsfont.js @@ -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'); diff --git a/ts/input/tex/bboldx/BboldxConfiguration.ts b/ts/input/tex/bboldx/BboldxConfiguration.ts index bf8075154..9d6e3275e 100644 --- a/ts/input/tex/bboldx/BboldxConfiguration.ts +++ b/ts/input/tex/bboldx/BboldxConfiguration.ts @@ -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 }); diff --git a/ts/output/chtml.ts b/ts/output/chtml.ts index 7d1e26312..2042d711c 100644 --- a/ts/output/chtml.ts +++ b/ts/output/chtml.ts @@ -162,6 +162,17 @@ CommonOutputJax< this.wrapperUsage = new Usage(); } + /** + * @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 */ diff --git a/ts/output/chtml/FontData.ts b/ts/output/chtml/FontData.ts index 40ad93e41..3ed4810f1 100644 --- a/ts/output/chtml/FontData.ts +++ b/ts/output/chtml/FontData.ts @@ -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'; @@ -188,6 +188,24 @@ export class ChtmlFontData extends FontData, + prefix: string = '' + ): string[] { + super.addExtension(data, prefix); + if (data.fonts && this.options.adaptiveCSS) { + const css = {}; + const styles = new CssStyles(); + (this.constructor as typeof ChtmlFontData).addDynamicFontCss(css, data.fonts, data.fontURL); + styles.addStyles(css); + return styles.getStyleRules(); + } + return []; + } + /***********************************************************************/ /** diff --git a/ts/output/common.ts b/ts/output/common.ts index 9976ea4b8..5c7d0750a 100644 --- a/ts/output/common.ts +++ b/ts/output/common.ts @@ -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); + } + /*****************************************************************/ diff --git a/ts/output/common/FontData.ts b/ts/output/common/FontData.ts index b7ec95d9b..571c89d1e 100644 --- a/ts/output/common/FontData.ts +++ b/ts/output/common/FontData.ts @@ -293,6 +293,7 @@ export type DynamicFont = { files: DynamicFileList; sizeN: number; stretchN: number; + data: FontExtensionData }; /** @@ -623,7 +624,7 @@ export class FontData, D extends /** * The font extension dynamic data */ - protected static dynamicExtensions: DynamicFontMap = new Map(); + public static dynamicExtensions: DynamicFontMap = new Map(); /** * The font options @@ -792,7 +793,8 @@ export class FontData, 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 [ @@ -853,14 +855,16 @@ export class FontData, 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, prefix: string = '') { + public addExtension(data: FontExtensionData, 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); @@ -885,6 +889,7 @@ export class FontData, D extends if (data.ranges) { this.defineDynamicCharacters(dynamicFont.files); } + return []; } /** From 88bad3d3deae85ecbe1cfbc8abc98d7bc9c49477 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Mon, 18 Mar 2024 16:27:14 -0400 Subject: [PATCH 2/2] Change if-then as requested in PR review --- ts/output/chtml/FontData.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ts/output/chtml/FontData.ts b/ts/output/chtml/FontData.ts index 3ed4810f1..7bada91da 100644 --- a/ts/output/chtml/FontData.ts +++ b/ts/output/chtml/FontData.ts @@ -196,14 +196,14 @@ export class ChtmlFontData extends FontData