Skip to content

Commit

Permalink
Merge pull request #778 from mathjax/issue2298
Browse files Browse the repository at this point in the history
Add support for mglyph use of fontfamily/index.  (mathjax/MathJax#2298)
  • Loading branch information
dpvc authored Apr 6, 2022
2 parents f173491 + 8ac3e24 commit a800e03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
13 changes: 13 additions & 0 deletions ts/core/MmlTree/MmlNodes/mglyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class MmlMglyph extends AbstractMmlTokenNode {
...AbstractMmlTokenNode.defaults,
alt: '',
src: '',
index: '',
width: 'auto',
height: 'auto',
valign: '0em'
Expand All @@ -55,4 +56,16 @@ export class MmlMglyph extends AbstractMmlTokenNode {
return 'mglyph';
}

/**
* @override
*/
public verifyAttributes(options: PropertyList) {
const {src, fontfamily, index} = this.attributes.getList('src', 'fontfamily', 'index');
if (src === '' && (fontfamily === '' || index === '')) {
this.mError('mglyph must have either src or fontfamily and index attributes', options, true);
} else {
super.verifyAttributes(options);
}
}

}
5 changes: 5 additions & 0 deletions ts/output/chtml/Wrappers/mglyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import {CHTMLWrapper, CHTMLConstructor} from '../Wrapper.js';
import {CommonMglyphMixin} from '../../common/Wrappers/mglyph.js';
import {MmlMglyph} from '../../../core/MmlTree/MmlNodes/mglyph.js';
import {CHTMLTextNode} from './TextNode.js';
import {StyleList, StyleData} from '../../../util/StyleList.js';

/*****************************************************************/
Expand Down Expand Up @@ -59,6 +60,10 @@ CommonMglyphMixin<CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
*/
public toCHTML(parent: N) {
const chtml = this.standardCHTMLnode(parent);
if (this.charWrapper) {
(this.charWrapper as CHTMLTextNode<N, T, D>).toCHTML(chtml);
return;
}
const {src, alt} = this.node.attributes.getList('src', 'alt');
const styles: StyleData = {
width: this.em(this.width),
Expand Down
62 changes: 46 additions & 16 deletions ts/output/common/Wrappers/mglyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
import {CommonTextNode} from './TextNode.js';
import {TextNode} from '../../../core/MmlTree/MmlNode.js';
import {BBox} from '../../../util/BBox.js';

/*****************************************************************/
Expand All @@ -30,14 +32,29 @@ import {BBox} from '../../../util/BBox.js';
*/
export interface CommonMglyph extends AnyWrapper {
/**
* The image's width, height, and valign values converted to em's
* The image's width converted to em's
*/
width: number;
/**
* The image's height converted to em's
*/
height: number;
/*
* The image's valign values converted to em's
*/
valign: number;

/**
* TextNode used for deprecated fontfamily/index use case
*/
charWrapper: CommonTextNode;

/**
* Obtain the width, height, and valign.
* Note: Currently, the width and height must be specified explicitly, or they default to 1em
* Since loading the image may be asynchronous, it would require a restart.
* A future extension could implement this either by subclassing this object, or
* perhaps as a post-filter on the MathML input jax that adds the needed dimensions
*/
getParameters(): void;
}
Expand All @@ -58,18 +75,23 @@ export function CommonMglyphMixin<T extends WrapperConstructor>(Base: T): Mglyph
return class extends Base {

/**
* The image's width converted to em's
* @override
*/
public width: number;
/**
* The image's height converted to em's
* @override
*/
public height: number;
/**
* The image's valign values converted to em's
* @override
*/
public valign: number;

/**
* @override
*/
public charWrapper: CommonTextNode;

/**
* @override
* @constructor
Expand All @@ -80,26 +102,34 @@ export function CommonMglyphMixin<T extends WrapperConstructor>(Base: T): Mglyph
}

/**
* Obtain the width, height, and valign.
* Note: Currently, the width and height must be specified explicitly, or they default to 1em
* Since loading the image may be asynchronous, it would require a restart.
* A future extension could implement this either by subclassing this object, or
* perhaps as a post-filter on the MathML input jax that adds the needed dimensions
* @override
*/
public getParameters() {
const {width, height, valign} = this.node.attributes.getList('width', 'height', 'valign');
this.width = (width === 'auto' ? 1 : this.length2em(width));
this.height = (height === 'auto' ? 1 : this.length2em(height));
this.valign = this.length2em(valign || '0');
const {width, height, valign, src, index} =
this.node.attributes.getList('width', 'height', 'valign', 'src', 'index');
if (src) {
this.width = (width === 'auto' ? 1 : this.length2em(width));
this.height = (height === 'auto' ? 1 : this.length2em(height));
this.valign = this.length2em(valign || '0');
} else {
const text = String.fromCodePoint(parseInt(index as string));
const mmlFactory = this.node.factory;
this.charWrapper = this.wrap((mmlFactory.create('text') as TextNode).setText(text));
this.charWrapper.parent = this as any as AnyWrapper;
}
}

/**
* @override
*/
public computeBBox(bbox: BBox, _recompute: boolean = false) {
bbox.w = this.width;
bbox.h = this.height + this.valign;
bbox.d = -this.valign;
if (this.charWrapper) {
bbox.updateFrom(this.charWrapper.getBBox());
} else {
bbox.w = this.width;
bbox.h = this.height + this.valign;
bbox.d = -this.valign;
}
}

};
Expand Down
5 changes: 5 additions & 0 deletions ts/output/svg/Wrappers/mglyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import {SVGWrapper, SVGConstructor} from '../Wrapper.js';
import {CommonMglyphMixin} from '../../common/Wrappers/mglyph.js';
import {MmlMglyph} from '../../../core/MmlTree/MmlNodes/mglyph.js';
import {SVGTextNode} from './TextNode.js';
import {OptionList} from '../../../util/Options.js';

/*****************************************************************/
Expand All @@ -48,6 +49,10 @@ CommonMglyphMixin<SVGConstructor<any, any, any>>(SVGWrapper) {
*/
public toSVG(parent: N) {
const svg = this.standardSVGnode(parent);
if (this.charWrapper) {
(this.charWrapper as SVGTextNode<N, T, D>).toSVG(svg);
return;
}
const {src, alt} = this.node.attributes.getList('src', 'alt');
const h = this.fixed(this.height);
const w = this.fixed(this.width);
Expand Down

0 comments on commit a800e03

Please sign in to comment.