[plugin system] Add a utility function (setFontMetrics) to extend builtin fontMetrics (#1269)

* Add and expose addFontMetrics function

* fix typo

* recreate package.json-lock

* Add setFontMetrics function to change the defualt metricMap - change getFontMetrics to getGlobalMetrics

* use new setFontMetrics on main KaTeX object

* fix package-lock.json error by rebuilding it

* Add appropriate tests

* update the snapshot
This commit is contained in:
Hossein Saniei
2018-06-10 05:12:35 +04:30
committed by Kevin Barabash
parent 251283ffc1
commit 8b1e1b4886
10 changed files with 453 additions and 264 deletions

View File

@@ -2,6 +2,7 @@
/* global expect: false */
/* global it: false */
/* global describe: false */
/* global beforeAll: false */
import buildMathML from "../src/buildMathML";
import buildTree from "../src/buildTree";
@@ -3191,3 +3192,39 @@ describe("Internal __* interface", function() {
expect(tree.toMarkup()).toEqual(renderedSansMathML);
});
});
describe("Extending katex by new fonts and symbols", function() {
beforeAll(() => {
const fontName = "mockEasternArabicFont";
// add eastern arabic numbers to symbols table
// these symbols are ۰۱۲۳۴۵۶۷۸۹ and ٠١٢٣٤٥٦٧٨٩
for (let number = 0; number <= 9; number++) {
const persianNum = String.fromCharCode(0x0660 + number);
katex.__defineSymbol(
"math", fontName, "textord", persianNum, persianNum);
const arabicNum = String.fromCharCode(0x06F0 + number);
katex.__defineSymbol(
"math", fontName, "textord", arabicNum, arabicNum);
}
});
it("should throw on rendering new symbols with no font metrics", () => {
// Lets parse 99^11 in eastern arabic
const errorMessage = "Font metrics not found for font: mockEasternArabicFont-Regular.";
expect(() => {
katex.__renderToDomTree("۹۹^{۱۱}", strictSettings);
}).toThrow(errorMessage);
});
it("should add font metrics to metrics map and render successfully", () => {
const mockMetrics = {};
// mock font metrics for the symbols that we added previously
for (let number = 0; number <= 9; number++) {
mockMetrics[0x0660 + number] = [-0.00244140625, 0.6875, 0, 0];
mockMetrics[0x06F0 + number] = [-0.00244140625, 0.6875, 0, 0];
}
katex.__setFontMetrics('mockEasternArabicFont-Regular', mockMetrics);
expect("۹۹^{۱۱}").toBuild();
});
it("Add new font class to new extended symbols", () => {
expect(katex.renderToString("۹۹^{۱۱}")).toMatchSnapshot();
});
});