[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

@@ -369,3 +369,81 @@ exports[`An implicit group parser within optional groups should work with sizing
}
]
`;
exports[`Extending katex by new fonts and symbols Add new font class to new extended symbols 1`] = `
<span class="katex">
<span class="katex-mathml">
<math>
<semantics>
<mrow>
<mi mathvariant="normal">
۹
</mi>
<msup>
<mi mathvariant="normal">
۹
</mi>
<mrow>
<mi mathvariant="normal">
۱
</mi>
<mi mathvariant="normal">
۱
</mi>
</mrow>
</msup>
</mrow>
<annotation encoding="application/x-tex">
۹۹^{۱۱}
</annotation>
</semantics>
</math>
</span>
<span class="katex-html"
aria-hidden="true"
>
<span class="base">
<span class="strut"
style="height:0.84425em;vertical-align:0em;"
>
</span>
<span class="mord mockEasternArabicFont-Regular">
۹
</span>
<span class="mord">
<span class="mord mockEasternArabicFont-Regular">
۹
</span>
<span class="msupsub">
<span class="vlist-t">
<span class="vlist-r">
<span class="vlist"
style="height:0.84425em;"
>
<span style="top:-3.063em;margin-right:0.05em;">
<span class="pstrut"
style="height:2.7em;"
>
</span>
<span class="sizing reset-size6 size3 mtight">
<span class="mord mtight">
<span class="mord mockEasternArabicFont-Regular mtight">
۱
</span>
<span class="mord mockEasternArabicFont-Regular mtight">
۱
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
`;

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();
});
});