1 | // @flow
|
2 | /**
|
3 | * This file contains information and classes for the various kinds of styles
|
4 | * used in TeX. It provides a generic `Style` class, which holds information
|
5 | * about a specific style. It then provides instances of all the different kinds
|
6 | * of styles possible, and provides functions to move between them and get
|
7 | * information about them.
|
8 | */
|
9 |
|
10 | /**
|
11 | * The main style class. Contains a unique id for the style, a size (which is
|
12 | * the same for cramped and uncramped version of a style), and a cramped flag.
|
13 | */
|
14 | class Style implements StyleInterface {
|
15 | id: number;
|
16 | size: number;
|
17 | cramped: boolean;
|
18 |
|
19 | constructor(id: number, size: number, cramped: boolean) {
|
20 | this.id = id;
|
21 | this.size = size;
|
22 | this.cramped = cramped;
|
23 | }
|
24 |
|
25 | /**
|
26 | * Get the style of a superscript given a base in the current style.
|
27 | */
|
28 | sup(): Style {
|
29 | return styles[sup[this.id]];
|
30 | }
|
31 |
|
32 | /**
|
33 | * Get the style of a subscript given a base in the current style.
|
34 | */
|
35 | sub(): Style {
|
36 | return styles[sub[this.id]];
|
37 | }
|
38 |
|
39 | /**
|
40 | * Get the style of a fraction numerator given the fraction in the current
|
41 | * style.
|
42 | */
|
43 | fracNum(): Style {
|
44 | return styles[fracNum[this.id]];
|
45 | }
|
46 |
|
47 | /**
|
48 | * Get the style of a fraction denominator given the fraction in the current
|
49 | * style.
|
50 | */
|
51 | fracDen(): Style {
|
52 | return styles[fracDen[this.id]];
|
53 | }
|
54 |
|
55 | /**
|
56 | * Get the cramped version of a style (in particular, cramping a cramped style
|
57 | * doesn't change the style).
|
58 | */
|
59 | cramp(): Style {
|
60 | return styles[cramp[this.id]];
|
61 | }
|
62 |
|
63 | /**
|
64 | * Get a text or display version of this style.
|
65 | */
|
66 | text(): Style {
|
67 | return styles[text[this.id]];
|
68 | }
|
69 |
|
70 | /**
|
71 | * Return true if this style is tightly spaced (scriptstyle/scriptscriptstyle)
|
72 | */
|
73 | isTight(): boolean {
|
74 | return this.size >= 2;
|
75 | }
|
76 | }
|
77 |
|
78 | // Export an interface for type checking, but don't expose the implementation.
|
79 | // This way, no more styles can be generated.
|
80 | export interface StyleInterface {
|
81 | id: number;
|
82 | size: number;
|
83 | cramped: boolean;
|
84 |
|
85 | sup(): StyleInterface;
|
86 | sub(): StyleInterface;
|
87 | fracNum(): StyleInterface;
|
88 | fracDen(): StyleInterface;
|
89 | cramp(): StyleInterface;
|
90 | text(): StyleInterface;
|
91 | isTight(): boolean;
|
92 | }
|
93 |
|
94 | // IDs of the different styles
|
95 | const D = 0;
|
96 | const Dc = 1;
|
97 | const T = 2;
|
98 | const Tc = 3;
|
99 | const S = 4;
|
100 | const Sc = 5;
|
101 | const SS = 6;
|
102 | const SSc = 7;
|
103 |
|
104 | // Instances of the different styles
|
105 | const styles = [
|
106 | new Style(D, 0, false),
|
107 | new Style(Dc, 0, true),
|
108 | new Style(T, 1, false),
|
109 | new Style(Tc, 1, true),
|
110 | new Style(S, 2, false),
|
111 | new Style(Sc, 2, true),
|
112 | new Style(SS, 3, false),
|
113 | new Style(SSc, 3, true),
|
114 | ];
|
115 |
|
116 | // Lookup tables for switching from one style to another
|
117 | const sup = [S, Sc, S, Sc, SS, SSc, SS, SSc];
|
118 | const sub = [Sc, Sc, Sc, Sc, SSc, SSc, SSc, SSc];
|
119 | const fracNum = [T, Tc, S, Sc, SS, SSc, SS, SSc];
|
120 | const fracDen = [Tc, Tc, Sc, Sc, SSc, SSc, SSc, SSc];
|
121 | const cramp = [Dc, Dc, Tc, Tc, Sc, Sc, SSc, SSc];
|
122 | const text = [D, Dc, T, Tc, T, Tc, T, Tc];
|
123 |
|
124 | // We only export some of the styles.
|
125 | export default {
|
126 | DISPLAY: styles[D],
|
127 | TEXT: styles[T],
|
128 | SCRIPT: styles[S],
|
129 | SCRIPTSCRIPT: styles[SS],
|
130 | };
|