mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-10 13:38:39 +00:00
Port unit.js to @flow. (#855)
* Port unit.js to @flow. * Removed unnecesary types.
This commit is contained in:
committed by
Kevin Barabash
parent
d46ca811c1
commit
3818105868
@@ -4,7 +4,7 @@ import environments from "./environments";
|
|||||||
import MacroExpander from "./MacroExpander";
|
import MacroExpander from "./MacroExpander";
|
||||||
import symbols from "./symbols";
|
import symbols from "./symbols";
|
||||||
import utils from "./utils";
|
import utils from "./utils";
|
||||||
import units from "./units";
|
import { validUnit } from "./units";
|
||||||
import { cjkRegex } from "./unicodeRegexes";
|
import { cjkRegex } from "./unicodeRegexes";
|
||||||
import ParseNode from "./ParseNode";
|
import ParseNode from "./ParseNode";
|
||||||
import ParseError from "./ParseError";
|
import ParseError from "./ParseError";
|
||||||
@@ -813,7 +813,7 @@ class Parser {
|
|||||||
number: +(match[1] + match[2]), // sign + magnitude, cast to number
|
number: +(match[1] + match[2]), // sign + magnitude, cast to number
|
||||||
unit: match[3],
|
unit: match[3],
|
||||||
};
|
};
|
||||||
if (!units.validUnit(data)) {
|
if (!validUnit(data)) {
|
||||||
throw new ParseError("Invalid unit: '" + data.unit + "'", res);
|
throw new ParseError("Invalid unit: '" + data.unit + "'", res);
|
||||||
}
|
}
|
||||||
return new ParseFuncOrArgument(
|
return new ParseFuncOrArgument(
|
||||||
|
@@ -13,7 +13,7 @@ import Style from "./Style";
|
|||||||
import buildCommon, { makeSpan } from "./buildCommon";
|
import buildCommon, { makeSpan } from "./buildCommon";
|
||||||
import delimiter from "./delimiter";
|
import delimiter from "./delimiter";
|
||||||
import domTree from "./domTree";
|
import domTree from "./domTree";
|
||||||
import units from "./units";
|
import { calculateSize } from "./units";
|
||||||
import utils from "./utils";
|
import utils from "./utils";
|
||||||
import stretchy from "./stretchy";
|
import stretchy from "./stretchy";
|
||||||
|
|
||||||
@@ -628,7 +628,7 @@ groupTypes.array = function(group, options) {
|
|||||||
|
|
||||||
let gap = 0;
|
let gap = 0;
|
||||||
if (group.value.rowGaps[r]) {
|
if (group.value.rowGaps[r]) {
|
||||||
gap = units.calculateSize(group.value.rowGaps[r].value, options);
|
gap = calculateSize(group.value.rowGaps[r].value, options);
|
||||||
if (gap > 0) { // \@argarraycr
|
if (gap > 0) { // \@argarraycr
|
||||||
gap += arstrutDepth;
|
gap += arstrutDepth;
|
||||||
if (depth < gap) {
|
if (depth < gap) {
|
||||||
@@ -1267,11 +1267,11 @@ groupTypes.rule = function(group, options) {
|
|||||||
// Calculate the shift, width, and height of the rule, and account for units
|
// Calculate the shift, width, and height of the rule, and account for units
|
||||||
let shift = 0;
|
let shift = 0;
|
||||||
if (group.value.shift) {
|
if (group.value.shift) {
|
||||||
shift = units.calculateSize(group.value.shift, options);
|
shift = calculateSize(group.value.shift, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
const width = units.calculateSize(group.value.width, options);
|
const width = calculateSize(group.value.width, options);
|
||||||
const height = units.calculateSize(group.value.height, options);
|
const height = calculateSize(group.value.height, options);
|
||||||
|
|
||||||
// Style the rule to the right size
|
// Style the rule to the right size
|
||||||
rule.style.borderRightWidth = width + "em";
|
rule.style.borderRightWidth = width + "em";
|
||||||
@@ -1295,7 +1295,7 @@ groupTypes.kern = function(group, options) {
|
|||||||
const rule = makeSpan(["mord", "rule"], [], options);
|
const rule = makeSpan(["mord", "rule"], [], options);
|
||||||
|
|
||||||
if (group.value.dimension) {
|
if (group.value.dimension) {
|
||||||
const dimension = units.calculateSize(group.value.dimension, options);
|
const dimension = calculateSize(group.value.dimension, options);
|
||||||
rule.style.marginLeft = dimension + "em";
|
rule.style.marginLeft = dimension + "em";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1642,7 +1642,7 @@ groupTypes.raisebox = function(group, options) {
|
|||||||
}],
|
}],
|
||||||
size: 6, // simulate \normalsize
|
size: 6, // simulate \normalsize
|
||||||
}}, options);
|
}}, options);
|
||||||
const dy = units.calculateSize(group.value.dy.value, options);
|
const dy = calculateSize(group.value.dy.value, options);
|
||||||
return buildCommon.makeVList([{
|
return buildCommon.makeVList([{
|
||||||
type: "elem",
|
type: "elem",
|
||||||
elem: body,
|
elem: body,
|
||||||
|
17
src/units.js
17
src/units.js
@@ -1,4 +1,4 @@
|
|||||||
/* eslint no-console:0 */
|
// @flow
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This file does conversion between units. In particular, it provides
|
* This file does conversion between units. In particular, it provides
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import ParseError from "./ParseError";
|
import ParseError from "./ParseError";
|
||||||
|
import Options from "./Options";
|
||||||
|
|
||||||
// This table gives the number of TeX pts in one of each *absolute* TeX unit.
|
// This table gives the number of TeX pts in one of each *absolute* TeX unit.
|
||||||
// Thus, multiplying a length by this number converts the length from units
|
// Thus, multiplying a length by this number converts the length from units
|
||||||
@@ -36,12 +37,14 @@ const relativeUnit = {
|
|||||||
"mu": true,
|
"mu": true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Measurement = {number: number, unit: string};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine whether the specified unit (either a string defining the unit
|
* Determine whether the specified unit (either a string defining the unit
|
||||||
* or a "size" parse node containing a unit field) is valid.
|
* or a "size" parse node containing a unit field) is valid.
|
||||||
*/
|
*/
|
||||||
const validUnit = function(unit) {
|
export const validUnit = function(unit: string | Measurement): boolean {
|
||||||
if (unit.unit) {
|
if (typeof unit !== "string") {
|
||||||
unit = unit.unit;
|
unit = unit.unit;
|
||||||
}
|
}
|
||||||
return (unit in ptPerUnit || unit in relativeUnit || unit === "ex");
|
return (unit in ptPerUnit || unit in relativeUnit || unit === "ex");
|
||||||
@@ -52,7 +55,8 @@ const validUnit = function(unit) {
|
|||||||
* as parsed by functions.js argType "size") into a CSS em value for the
|
* as parsed by functions.js argType "size") into a CSS em value for the
|
||||||
* current style/scale. `options` gives the current options.
|
* current style/scale. `options` gives the current options.
|
||||||
*/
|
*/
|
||||||
const calculateSize = function(sizeValue, options) {
|
export const calculateSize = function(
|
||||||
|
sizeValue: Measurement, options: Options): number {
|
||||||
let scale;
|
let scale;
|
||||||
if (sizeValue.unit in ptPerUnit) {
|
if (sizeValue.unit in ptPerUnit) {
|
||||||
// Absolute units
|
// Absolute units
|
||||||
@@ -92,8 +96,3 @@ const calculateSize = function(sizeValue, options) {
|
|||||||
}
|
}
|
||||||
return Math.min(sizeValue.number * scale, options.maxSize);
|
return Math.min(sizeValue.number * scale, options.maxSize);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
validUnit: validUnit,
|
|
||||||
calculateSize: calculateSize,
|
|
||||||
};
|
|
||||||
|
Reference in New Issue
Block a user