Fix all AMS mathord symbols (#618)

* Fix all AMS mathord symbols

* Fix \imath, \jmath, \pounds support

* Fix \mathit support

* Fix Greek capitals

* Default font to main (fix Unicode support)

* Now using correct \maltese

* Correct mathit documentation

* var -> const

* Add trailing commas

* Remove greekCapitals (no longer needed)
This commit is contained in:
Erik Demaine
2017-01-15 19:05:21 -05:00
committed by Kevin Barabash
parent f1c02226cc
commit d4aa6a7253
3 changed files with 56 additions and 45 deletions

View File

@@ -9,27 +9,28 @@ const fontMetrics = require("./fontMetrics");
const symbols = require("./symbols"); const symbols = require("./symbols");
const utils = require("./utils"); const utils = require("./utils");
const greekCapitals = [
"\\Gamma",
"\\Delta",
"\\Theta",
"\\Lambda",
"\\Xi",
"\\Pi",
"\\Sigma",
"\\Upsilon",
"\\Phi",
"\\Psi",
"\\Omega",
];
// The following have to be loaded from Main-Italic font, using class mainit // The following have to be loaded from Main-Italic font, using class mainit
const mainitLetters = [ const mainitLetters = [
"\u0131", // dotless i, \imath "\\imath", // dotless i
"\u0237", // dotless j, \jmath "\\jmath", // dotless j
"\u00a3", // \pounds "\\pounds", // pounds symbol
]; ];
/**
* Looks up the given symbol in fontMetrics, after applying any symbol
* replacements defined in symbol.js
*/
const lookupSymbol = function(value, fontFamily, mode) {
// Replace the value with its replaced value from symbol.js
if (symbols[mode][value] && symbols[mode][value].replace) {
value = symbols[mode][value].replace;
}
return {
value: value,
metrics: fontMetrics.getCharacterMetrics(value, fontFamily),
};
};
/** /**
* Makes a symbolNode after translation via the list of symbols in symbols.js. * Makes a symbolNode after translation via the list of symbols in symbols.js.
* Correctly pulls out metrics for the character, and optionally takes a list of * Correctly pulls out metrics for the character, and optionally takes a list of
@@ -40,12 +41,9 @@ const mainitLetters = [
* should if present come first in `classes`. * should if present come first in `classes`.
*/ */
const makeSymbol = function(value, fontFamily, mode, options, classes) { const makeSymbol = function(value, fontFamily, mode, options, classes) {
// Replace the value with its replaced value from symbol.js const lookup = lookupSymbol(value, fontFamily, mode);
if (symbols[mode][value] && symbols[mode][value].replace) { const metrics = lookup.metrics;
value = symbols[mode][value].replace; value = lookup.value;
}
const metrics = fontMetrics.getCharacterMetrics(value, fontFamily);
let symbolNode; let symbolNode;
if (metrics) { if (metrics) {
@@ -100,29 +98,44 @@ const mathsym = function(value, mode, options, classes) {
*/ */
const mathDefault = function(value, mode, options, classes, type) { const mathDefault = function(value, mode, options, classes, type) {
if (type === "mathord") { if (type === "mathord") {
return mathit(value, mode, options, classes); const fontLookup = mathit(value, mode, options, classes);
return makeSymbol(value, fontLookup.fontName, mode, options,
classes.concat([fontLookup.fontClass]));
} else if (type === "textord") { } else if (type === "textord") {
const font = symbols[mode][value] && symbols[mode][value].font;
if (font === "ams") {
return makeSymbol( return makeSymbol(
value, "Main-Regular", mode, options, classes.concat(["mathrm"])); value, "AMS-Regular", mode, options, classes.concat(["amsrm"]));
} else { // if (font === "main") {
return makeSymbol(
value, "Main-Regular", mode, options,
classes.concat(["mathrm"]));
}
} else { } else {
throw new Error("unexpected type: " + type + " in mathDefault"); throw new Error("unexpected type: " + type + " in mathDefault");
} }
}; };
/** /**
* Makes a symbol in the italic math font. * Determines which of the two font names (Main-Italic and Math-Italic) and
* corresponding style tags (mainit or mathit) to use for font "mathit",
* depending on the symbol. Use this function instead of fontMap for font
* "mathit".
*/ */
const mathit = function(value, mode, options, classes) { const mathit = function(value, mode, options, classes) {
if (/[0-9]/.test(value.charAt(0)) || if (/[0-9]/.test(value.charAt(0)) ||
// glyphs for \imath and \jmath do not exist in Math-Italic so we // glyphs for \imath and \jmath do not exist in Math-Italic so we
// need to use Main-Italic instead // need to use Main-Italic instead
utils.contains(mainitLetters, value) || utils.contains(mainitLetters, value)) {
utils.contains(greekCapitals, value)) { return {
return makeSymbol( fontName: "Main-Italic",
value, "Main-Italic", mode, options, classes.concat(["mainit"])); fontClass: "mainit",
};
} else { } else {
return makeSymbol( return {
value, "Math-Italic", mode, options, classes.concat(["mathit"])); fontName: "Math-Italic",
fontClass: "mathit",
};
} }
}; };
@@ -131,26 +144,24 @@ const mathit = function(value, mode, options, classes) {
*/ */
const makeOrd = function(group, options, type) { const makeOrd = function(group, options, type) {
const mode = group.mode; const mode = group.mode;
let value = group.value; const value = group.value;
if (symbols[mode][value] && symbols[mode][value].replace) {
value = symbols[mode][value].replace;
}
const classes = ["mord"]; const classes = ["mord"];
const font = options.font; const font = options.font;
if (font) { if (font) {
let fontLookup;
if (font === "mathit" || utils.contains(mainitLetters, value)) { if (font === "mathit" || utils.contains(mainitLetters, value)) {
return mathit(value, mode, options, classes); fontLookup = mathit(value, mode, options, classes);
} else { } else {
const fontName = fontMap[font].fontName; fontLookup = fontMap[font];
if (fontMetrics.getCharacterMetrics(value, fontName)) { }
return makeSymbol( if (lookupSymbol(value, fontLookup.fontName, mode).metrics) {
value, fontName, mode, options, classes.concat([font])); return makeSymbol(value, fontLookup.fontName, mode, options,
classes.concat([fontLookup.fontClass || font]));
} else { } else {
return mathDefault(value, mode, options, classes, type); return mathDefault(value, mode, options, classes, type);
} }
}
} else { } else {
return mathDefault(value, mode, options, classes, type); return mathDefault(value, mode, options, classes, type);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB