149 lines
3.7 KiB
JavaScript
149 lines
3.7 KiB
JavaScript
/**
|
|
* This file contains information and classes for the various kinds of styles
|
|
* used in TeX. It provides a generic `Style` class, which holds information
|
|
* about a specific style. It then provides instances of all the different kinds
|
|
* of styles possible, and provides functions to move between them and get
|
|
* information about them.
|
|
*/
|
|
|
|
const sigmas = require("./fontMetrics.js").sigmas;
|
|
|
|
const metrics = [{}, {}, {}];
|
|
for (const key in sigmas) {
|
|
if (sigmas.hasOwnProperty(key)) {
|
|
for (let i = 0; i < 3; i++) {
|
|
metrics[i][key] = sigmas[key][i];
|
|
}
|
|
}
|
|
}
|
|
for (let i = 0; i < 3; i++) {
|
|
metrics[i].emPerEx = sigmas.xHeight[i] / sigmas.quad[i];
|
|
}
|
|
|
|
/**
|
|
* The main style class. Contains a unique id for the style, a size (which is
|
|
* the same for cramped and uncramped version of a style), a cramped flag, and a
|
|
* size multiplier, which gives the size difference between a style and
|
|
* textstyle.
|
|
*/
|
|
function Style(id, size, multiplier, cramped) {
|
|
this.id = id;
|
|
this.size = size;
|
|
this.cramped = cramped;
|
|
this.sizeMultiplier = multiplier;
|
|
this.metrics = metrics[size > 0 ? size - 1 : 0];
|
|
}
|
|
|
|
/**
|
|
* Get the style of a superscript given a base in the current style.
|
|
*/
|
|
Style.prototype.sup = function() {
|
|
return styles[sup[this.id]];
|
|
};
|
|
|
|
/**
|
|
* Get the style of a subscript given a base in the current style.
|
|
*/
|
|
Style.prototype.sub = function() {
|
|
return styles[sub[this.id]];
|
|
};
|
|
|
|
/**
|
|
* Get the style of a fraction numerator given the fraction in the current
|
|
* style.
|
|
*/
|
|
Style.prototype.fracNum = function() {
|
|
return styles[fracNum[this.id]];
|
|
};
|
|
|
|
/**
|
|
* Get the style of a fraction denominator given the fraction in the current
|
|
* style.
|
|
*/
|
|
Style.prototype.fracDen = function() {
|
|
return styles[fracDen[this.id]];
|
|
};
|
|
|
|
/**
|
|
* Get the cramped version of a style (in particular, cramping a cramped style
|
|
* doesn't change the style).
|
|
*/
|
|
Style.prototype.cramp = function() {
|
|
return styles[cramp[this.id]];
|
|
};
|
|
|
|
/**
|
|
* HTML class name, like "displaystyle cramped"
|
|
*/
|
|
Style.prototype.cls = function() {
|
|
return sizeNames[this.size] + (this.cramped ? " cramped" : " uncramped");
|
|
};
|
|
|
|
/**
|
|
* HTML Reset class name, like "reset-textstyle"
|
|
*/
|
|
Style.prototype.reset = function() {
|
|
return resetNames[this.size];
|
|
};
|
|
|
|
/**
|
|
* Return if this style is tightly spaced (scriptstyle/scriptscriptstyle)
|
|
*/
|
|
Style.prototype.isTight = function() {
|
|
return this.size >= 2;
|
|
};
|
|
|
|
// IDs of the different styles
|
|
const D = 0;
|
|
const Dc = 1;
|
|
const T = 2;
|
|
const Tc = 3;
|
|
const S = 4;
|
|
const Sc = 5;
|
|
const SS = 6;
|
|
const SSc = 7;
|
|
|
|
// String names for the different sizes
|
|
const sizeNames = [
|
|
"displaystyle textstyle",
|
|
"textstyle",
|
|
"scriptstyle",
|
|
"scriptscriptstyle",
|
|
];
|
|
|
|
// Reset names for the different sizes
|
|
const resetNames = [
|
|
"reset-textstyle",
|
|
"reset-textstyle",
|
|
"reset-scriptstyle",
|
|
"reset-scriptscriptstyle",
|
|
];
|
|
|
|
// Instances of the different styles
|
|
const styles = [
|
|
new Style(D, 0, 1.0, false),
|
|
new Style(Dc, 0, 1.0, true),
|
|
new Style(T, 1, 1.0, false),
|
|
new Style(Tc, 1, 1.0, true),
|
|
new Style(S, 2, 0.7, false),
|
|
new Style(Sc, 2, 0.7, true),
|
|
new Style(SS, 3, 0.5, false),
|
|
new Style(SSc, 3, 0.5, true),
|
|
];
|
|
|
|
// Lookup tables for switching from one style to another
|
|
const sup = [S, Sc, S, Sc, SS, SSc, SS, SSc];
|
|
const sub = [Sc, Sc, Sc, Sc, SSc, SSc, SSc, SSc];
|
|
const fracNum = [T, Tc, S, Sc, SS, SSc, SS, SSc];
|
|
const fracDen = [Tc, Tc, Sc, Sc, SSc, SSc, SSc, SSc];
|
|
const cramp = [Dc, Dc, Tc, Tc, Sc, Sc, SSc, SSc];
|
|
|
|
// We only export some of the styles. Also, we don't export the `Style` class so
|
|
// no more styles can be generated.
|
|
module.exports = {
|
|
DISPLAY: styles[D],
|
|
TEXT: styles[T],
|
|
SCRIPT: styles[S],
|
|
SCRIPTSCRIPT: styles[SS],
|
|
};
|