Merge pull request #580 from kohler/muunits
Support "mu"-denominated sizes and \mkern
This commit is contained in:
commit
f742fbf9f2
|
@ -733,7 +733,7 @@ Parser.prototype.parseSizeGroup = function(optional) {
|
|||
number: +(match[1] + match[2]), // sign + magnitude, cast to number
|
||||
unit: match[3],
|
||||
};
|
||||
if (data.unit !== "em" && data.unit !== "ex") {
|
||||
if (data.unit !== "em" && data.unit !== "ex" && data.unit !== "mu") {
|
||||
throw new ParseError("Invalid unit: '" + data.unit + "'", res);
|
||||
}
|
||||
return new ParseFuncOrArgument(
|
||||
|
|
|
@ -543,6 +543,16 @@ groupTypes.genfrac = function(group, options) {
|
|||
options);
|
||||
};
|
||||
|
||||
var calculateSize = function(sizeValue, style) {
|
||||
var x = sizeValue.number;
|
||||
if (sizeValue.unit === "ex") {
|
||||
x *= style.metrics.emPerEx;
|
||||
} else if (sizeValue.unit === "mu") {
|
||||
x /= 18;
|
||||
}
|
||||
return x;
|
||||
};
|
||||
|
||||
groupTypes.array = function(group, options) {
|
||||
var r;
|
||||
var c;
|
||||
|
@ -589,18 +599,7 @@ groupTypes.array = function(group, options) {
|
|||
|
||||
var gap = 0;
|
||||
if (group.value.rowGaps[r]) {
|
||||
gap = group.value.rowGaps[r].value;
|
||||
switch (gap.unit) {
|
||||
case "em":
|
||||
gap = gap.number;
|
||||
break;
|
||||
case "ex":
|
||||
gap = gap.number * style.metrics.emPerEx;
|
||||
break;
|
||||
default:
|
||||
console.error("Can't handle unit " + gap.unit);
|
||||
gap = 0;
|
||||
}
|
||||
gap = calculateSize(group.value.rowGaps[r].value, style);
|
||||
if (gap > 0) { // \@argarraycr
|
||||
gap += arstrutDepth;
|
||||
if (depth < gap) {
|
||||
|
@ -1269,21 +1268,11 @@ groupTypes.rule = function(group, options) {
|
|||
// Calculate the shift, width, and height of the rule, and account for units
|
||||
var shift = 0;
|
||||
if (group.value.shift) {
|
||||
shift = group.value.shift.number;
|
||||
if (group.value.shift.unit === "ex") {
|
||||
shift *= style.metrics.xHeight;
|
||||
}
|
||||
shift = calculateSize(group.value.shift, style);
|
||||
}
|
||||
|
||||
var width = group.value.width.number;
|
||||
if (group.value.width.unit === "ex") {
|
||||
width *= style.metrics.xHeight;
|
||||
}
|
||||
|
||||
var height = group.value.height.number;
|
||||
if (group.value.height.unit === "ex") {
|
||||
height *= style.metrics.xHeight;
|
||||
}
|
||||
var width = calculateSize(group.value.width, style);
|
||||
var height = calculateSize(group.value.height, style);
|
||||
|
||||
// The sizes of rules are absolute, so make it larger if we are in a
|
||||
// smaller style.
|
||||
|
@ -1311,10 +1300,7 @@ groupTypes.kern = function(group, options) {
|
|||
|
||||
var dimension = 0;
|
||||
if (group.value.dimension) {
|
||||
dimension = group.value.dimension.number;
|
||||
if (group.value.dimension.unit === "ex") {
|
||||
dimension *= style.metrics.xHeight;
|
||||
}
|
||||
dimension = calculateSize(group.value.dimension, style);
|
||||
}
|
||||
|
||||
dimension /= style.sizeMultiplier;
|
||||
|
|
|
@ -205,7 +205,9 @@ defineFunction("\\rule", {
|
|||
};
|
||||
});
|
||||
|
||||
defineFunction("\\kern", {
|
||||
// TODO: In TeX, \mkern only accepts mu-units, and \kern does not accept
|
||||
// mu-units. In current KaTeX we relax this; both commands accept any unit.
|
||||
defineFunction(["\\kern", "\\mkern"], {
|
||||
numArgs: 1,
|
||||
argTypes: ["size"],
|
||||
}, function(context, args) {
|
||||
|
|
|
@ -977,15 +977,18 @@ describe("A rule parser", function() {
|
|||
describe("A kern parser", function() {
|
||||
var emKern = "\\kern{1em}";
|
||||
var exKern = "\\kern{1ex}";
|
||||
var muKern = "\\kern{1mu}";
|
||||
var badUnitRule = "\\kern{1px}";
|
||||
var noNumberRule = "\\kern{em}";
|
||||
|
||||
it("should list the correct units", function() {
|
||||
var emParse = getParsed(emKern)[0];
|
||||
var exParse = getParsed(exKern)[0];
|
||||
var muParse = getParsed(muKern)[0];
|
||||
|
||||
expect(emParse.value.dimension.unit).toEqual("em");
|
||||
expect(exParse.value.dimension.unit).toEqual("ex");
|
||||
expect(muParse.value.dimension.unit).toEqual("mu");
|
||||
});
|
||||
|
||||
it("should not parse invalid units", function() {
|
||||
|
@ -1007,15 +1010,18 @@ describe("A kern parser", function() {
|
|||
describe("A non-braced kern parser", function() {
|
||||
var emKern = "\\kern1em";
|
||||
var exKern = "\\kern 1 ex";
|
||||
var muKern = "\\kern 1mu";
|
||||
var badUnitRule = "\\kern1px";
|
||||
var noNumberRule = "\\kern em";
|
||||
|
||||
it("should list the correct units", function() {
|
||||
var emParse = getParsed(emKern)[0];
|
||||
var exParse = getParsed(exKern)[0];
|
||||
var muParse = getParsed(muKern)[0];
|
||||
|
||||
expect(emParse.value.dimension.unit).toEqual("em");
|
||||
expect(exParse.value.dimension.unit).toEqual("ex");
|
||||
expect(muParse.value.dimension.unit).toEqual("mu");
|
||||
});
|
||||
|
||||
it("should not parse invalid units", function() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user