| |
| |
| |
| |
| |
|
|
| import {fontMap, makeSpan} from "./buildCommon"; |
| import {getCharacterMetrics} from "./fontMetrics"; |
| import ParseError from "./ParseError"; |
| import symbols, {ligatures} from "./symbols"; |
| import {_mathmlGroupBuilders as groupBuilders} from "./defineFunction"; |
| import {MathNode, TextNode} from "./mathMLTree"; |
|
|
| import type Options from "./Options"; |
| import type {DomSpan, HtmlDomNode} from "./domTree"; |
| import type {MathDomNode} from "./mathMLTree"; |
| import type {Mode} from "./types"; |
| import type {FontVariant, MathFont} from "./types/fonts"; |
| import type {AnyParseNode, SymbolParseNode} from "./types/nodes"; |
|
|
| const noVariantSymbols = new Set(["\\imath", "\\jmath"]); |
| const rowLikeTypes = new Set(["mrow", "mtable"]); |
|
|
| |
| |
| |
| |
| export const makeText = function( |
| text: string, |
| mode: Mode, |
| options?: Options, |
| ): TextNode { |
| if (symbols[mode][text] && symbols[mode][text].replace && |
| text.charCodeAt(0) !== 0xD835 && |
| !(ligatures.hasOwnProperty(text) && options && |
| ((options.fontFamily && options.fontFamily.slice(4, 6) === "tt") || |
| (options.font && options.font.slice(4, 6) === "tt")))) { |
| text = symbols[mode][text].replace!; |
| } |
|
|
| return new TextNode(text); |
| }; |
|
|
| |
| |
| |
| |
| export const makeRow = function(body: MathDomNode[]): MathDomNode { |
| if (body.length === 1) { |
| return body[0]; |
| } else { |
| return new MathNode("mrow", body); |
| } |
| }; |
|
|
| const mathFontVariants: Partial< |
| Record<MathFont, FontVariant | ((group: SymbolParseNode) => FontVariant)> |
| > = { |
| mathit: "italic", |
| boldsymbol: (group) => (group.type === "textord" ? "bold" : "bold-italic"), |
| mathbf: "bold", |
| mathbb: "double-struck", |
| mathsfit: "sans-serif-italic", |
| mathfrak: "fraktur", |
| mathscr: "script", |
| mathcal: "script", |
| mathsf: "sans-serif", |
| mathtt: "monospace", |
| }; |
|
|
| |
| |
| |
| export const getVariant = ( |
| group: SymbolParseNode, |
| options: Options, |
| ): FontVariant | null | undefined => { |
| |
| |
| |
| if (group.mode === "text") { |
| if (options.fontFamily === "texttt") { |
| return "monospace"; |
| } else if (options.fontFamily === "textsf") { |
| if (options.fontShape === "textit" && |
| options.fontWeight === "textbf") { |
| return "sans-serif-bold-italic"; |
| } else if (options.fontShape === "textit") { |
| return "sans-serif-italic"; |
| } else if (options.fontWeight === "textbf") { |
| return "bold-sans-serif"; |
| } else { |
| return "sans-serif"; |
| } |
| } else if (options.fontShape === "textit" && |
| options.fontWeight === "textbf") { |
| return "bold-italic"; |
| } else if (options.fontShape === "textit") { |
| return "italic"; |
| } else if (options.fontWeight === "textbf") { |
| return "bold"; |
| } |
| } |
|
|
| const font = options.font; |
| if (!font || font === "mathnormal") { |
| return null; |
| } |
|
|
| const mode = group.mode; |
| const mathVariant = mathFontVariants[font]; |
|
|
| if (mathVariant) { |
| return typeof mathVariant === "function" |
| ? mathVariant(group) |
| : mathVariant; |
| } |
|
|
| let text = group.text; |
| if (noVariantSymbols.has(text)) { |
| return null; |
| } |
|
|
| if (symbols[mode][text]) { |
| const replacement = symbols[mode][text].replace; |
| if (replacement) { |
| text = replacement; |
| } |
| } |
|
|
| const fontName = fontMap[font].fontName; |
| if (getCharacterMetrics(text, fontName, mode)) { |
| return fontMap[font].variant; |
| } |
|
|
| return null; |
| }; |
|
|
| |
| |
| |
| |
| |
| function isNumberPunctuation(group: MathNode | null | undefined): boolean { |
| if (!group) { |
| return false; |
| } |
| if (group.type === 'mi' && group.children.length === 1) { |
| const child = group.children[0]; |
| return child instanceof TextNode && child.text === '.'; |
| } else if (group.type === 'mo' && group.children.length === 1 && |
| group.getAttribute('separator') === 'true' && |
| group.getAttribute('lspace') === '0em' && |
| group.getAttribute('rspace') === '0em' |
| ) { |
| const child = group.children[0]; |
| return child instanceof TextNode && child.text === ','; |
| } else { |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| export const buildExpression = function( |
| expression: AnyParseNode[], |
| options: Options, |
| isOrdgroup?: boolean, |
| ): MathNode[] { |
| if (expression.length === 1) { |
| const group = buildGroup(expression[0], options); |
| if (isOrdgroup && group instanceof MathNode && group.type === "mo") { |
| |
| |
| group.setAttribute("lspace", "0em"); |
| group.setAttribute("rspace", "0em"); |
| } |
| return [group]; |
| } |
|
|
| const groups = []; |
| let lastGroup; |
| for (let i = 0; i < expression.length; i++) { |
| const group = buildGroup(expression[i], options); |
| if (group instanceof MathNode && lastGroup instanceof MathNode) { |
| |
| if (group.type === 'mtext' && lastGroup.type === 'mtext' |
| && group.getAttribute('mathvariant') === |
| lastGroup.getAttribute('mathvariant')) { |
| lastGroup.children.push(...group.children); |
| continue; |
| |
| } else if (group.type === 'mn' && lastGroup.type === 'mn') { |
| lastGroup.children.push(...group.children); |
| continue; |
| |
| } else if (isNumberPunctuation(group) && lastGroup.type === 'mn') { |
| lastGroup.children.push(...group.children); |
| continue; |
| |
| } else if (group.type === 'mn' && isNumberPunctuation(lastGroup)) { |
| group.children = [...lastGroup.children, ...group.children]; |
| groups.pop(); |
| |
| |
| } else if ((group.type === 'msup' || group.type === 'msub') && |
| group.children.length >= 1 && |
| (lastGroup.type === 'mn' || isNumberPunctuation(lastGroup)) |
| ) { |
| const base = group.children[0]; |
| if (base instanceof MathNode && base.type === 'mn') { |
| base.children = [...lastGroup.children, ...base.children]; |
| groups.pop(); |
| } |
| |
| } else if (lastGroup.type === 'mi' && lastGroup.children.length === 1) { |
| const lastChild = lastGroup.children[0]; |
| if (lastChild instanceof TextNode && lastChild.text === '\u0338' && |
| (group.type === 'mo' || group.type === 'mi' || |
| group.type === 'mn')) { |
| const child = group.children[0]; |
| if (child instanceof TextNode && child.text.length > 0) { |
| |
| child.text = child.text.slice(0, 1) + "\u0338" + |
| child.text.slice(1); |
| groups.pop(); |
| } |
| } |
| } |
| } |
| groups.push(group); |
| lastGroup = group; |
| } |
| return groups; |
| }; |
|
|
| |
| |
| |
| |
| export const buildExpressionRow = function( |
| expression: AnyParseNode[], |
| options: Options, |
| isOrdgroup?: boolean, |
| ): MathDomNode { |
| return makeRow(buildExpression(expression, options, isOrdgroup)); |
| }; |
|
|
| |
| |
| |
| |
| export const buildGroup = function( |
| group: AnyParseNode | null | undefined, |
| options: Options, |
| ): MathNode { |
| if (!group) { |
| return new MathNode("mrow"); |
| } |
|
|
| if (groupBuilders[group.type]) { |
| |
| |
| |
| return groupBuilders[group.type](group, options) as MathNode; |
| } else { |
| throw new ParseError( |
| "Got group of unknown type: '" + group.type + "'"); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export default function buildMathML( |
| tree: AnyParseNode[], |
| texExpression: string, |
| options: Options, |
| isDisplayMode: boolean, |
| forMathmlOnly: boolean, |
| ): DomSpan { |
| const expression = buildExpression(tree, options); |
|
|
| |
| |
| |
| |
|
|
| |
| |
| let wrapper; |
| if (expression.length === 1 && expression[0] instanceof MathNode && |
| rowLikeTypes.has(expression[0].type)) { |
| wrapper = expression[0]; |
| } else { |
| wrapper = new MathNode("mrow", expression); |
| } |
|
|
| |
| const annotation = new MathNode( |
| "annotation", [new TextNode(texExpression)]); |
|
|
| annotation.setAttribute("encoding", "application/x-tex"); |
|
|
| const semantics = new MathNode( |
| "semantics", [wrapper, annotation]); |
|
|
| const math = new MathNode("math", [semantics]); |
| math.setAttribute("xmlns", "http://www.w3.org/1998/Math/MathML"); |
| if (isDisplayMode) { |
| math.setAttribute("display", "block"); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| const wrapperClass = forMathmlOnly ? "katex" : "katex-mathml"; |
| return makeSpan([wrapperClass], [math as unknown as HtmlDomNode]); |
| } |
|
|