Debian Codegraph @

unpacked/jax/output/CommonHTML/autoload/mmultiscripts.js

raw
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3
4 /*************************************************************
5 *
6 * MathJax/jax/output/CommonHTML/autoload/mmultiscripts.js
7 *
8 * Implements the CommonHTML output for <mmultiscripts> elements.
9 *
10 * ---------------------------------------------------------------------
11 *
12 * Copyright (c) 2015-2020 The MathJax Consortium
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26
27 MathJax.Hub.Register.StartupHook("CommonHTML Jax Ready",function () {
28 var VERSION = "2.7.9";
29 var MML = MathJax.ElementJax.mml,
30 CHTML = MathJax.OutputJax.CommonHTML;
31
32 MML.mmultiscripts.Augment({
33 toCommonHTML: function (node,options) {
34 var stretch = (options||{}).stretch;
35 if (!stretch) {
36 node = this.CHTMLcreateNode(node);
37 this.CHTMLhandleStyle(node);
38 this.CHTMLgetVariant();
39 this.CHTMLhandleScale(node);
40 }
41 CHTML.BBOX.empty(this.CHTML);
42
43 //
44 // Get base node
45 //
46 var base, bbox;
47 if (stretch) {
48 base = CHTML.getNode(node,"mjx-base");
49 } else {
50 this.CHTMLaddChild(node,0,{type:"mjx-base", noBBox:true, forceChild:true});
51 base = node.firstChild;
52 }
53 bbox = this.CHTMLbboxFor(0);
54 if (bbox.ic) {
55 bbox.R -= bbox.ic; // remove IC (added by mo and mi)
56 if (!stretch) base.style.marginRight = CHTML.Em(-bbox.ic);
57 delta = 1.3*bbox.ic + .05; // make faked IC be closer to expeted results
58 }
59
60 //
61 // Collect scripts into horizontal boxes and add them into the node
62 //
63 var BOX = {}, BBOX = {};
64 this.CHTMLgetScripts(BOX,BBOX,stretch,node);
65 var sub = BOX.sub, sup = BOX.sup, presub = BOX.presub, presup = BOX.presup;
66 var sbox = BBOX.sub, Sbox = BBOX.sup, pbox = BBOX.presub, Pbox = BBOX.presup;
67 if (!stretch) this.CHTMLaddBoxes(node,base,BOX);
68
69 //
70 // Get the initial values for the variables
71 //
72 var values = this.getValues("scriptlevel","scriptsizemultiplier");
73 var sscale = (this.Get("scriptlevel") < 3 ? values.scriptsizemultiplier : 1);
74 var ex = CHTML.TEX.x_height, s = CHTML.TEX.scriptspace;
75 var q = CHTML.TEX.sup_drop * sscale, r = CHTML.TEX.sub_drop * sscale;
76 var u = bbox.h - q, v = bbox.d + r, delta = 0, p;
77 var bmml = this.data[this.base];
78 if (bmml && (bmml.type === "mi" || bmml.type === "mo")) {
79 if (CHTML.isChar(bmml.data.join("")) && bbox.rscale === 1 && !bbox.sH &&
80 !bmml.Get("largeop")) {u = v = 0}
81 }
82 values = this.getValues("displaystyle","subscriptshift","superscriptshift","texprimestyle");
83 values.subscriptshift = (values.subscriptshift === "" ? 0 : this.CHTMLlength2em(values.subscriptshift));
84 values.superscriptshift = (values.superscriptshift === "" ? 0 : this.CHTMLlength2em(values.superscriptshift));
85
86 var dx = (presub ? s+pbox.w : presup ? s+Pbox.w-delta : 0);
87 this.CHTML.combine(bbox,dx,0); var x = this.CHTML.w;
88
89 //
90 // Place the scripts as needed
91 //
92 if (!sup && !presup) {
93 v = Math.max(v,CHTML.TEX.sub1,values.subscriptshift);
94 if (sub) v = Math.max(v,sbox.h-(4/5)*ex);
95 if (presub) v = Math.max(v,pbox.h-(4/5)*ex);
96 if (sub) this.CHTMLplaceSubOnly(sub,sbox,x,v,s);
97 if (presub) this.CHTMLplacePresubOnly(presub,pbox,v,s);
98 } else {
99 if (!sub && !presub) {
100 p = CHTML.TEX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
101 u = Math.max(u,p,values.superscriptshift);
102 if (sup) u = Math.max(u,Sbox.d+(1/4)*ex);
103 if (presup) u = Math.max(u,Pbox.d+(1/4)*ex);
104 if (sup) this.CHTMLplaceSupOnly(sup,Sbox,x,delta,u,s);
105 if (presup) this.CHTMLplacePresupOnly(presup,Pbox,delta,u,s);
106 } else {
107 v = Math.max(v,CHTML.TEX.sub2);
108 var t = CHTML.TEX.rule_thickness;
109 var h = (sbox||pbox).h, d = (Sbox||Pbox).d;
110 if (presub) h = Math.max(h,pbox.h);
111 if (presup) d = Math.max(d,Pbox.d);
112 if ((u - d) - (h - v) < 3*t) {
113 v = 3*t - u + d + h; q = (4/5)*ex - (u - d);
114 if (q > 0) {u += q; v -= q}
115 }
116 u = Math.max(u,values.superscriptshift);
117 v = Math.max(v,values.subscriptshift);
118 if (sup) {
119 if (sub) {this.CHTMLplaceSubSup(sub,sbox,sup,Sbox,x,delta,u,v,s)}
120 else {this.CHTMLplaceSupOnly(sup,Sbox,x,delta,u,s)}
121 } else if (sub) {this.CHTMLplaceSubOnly(sub,sbox,x,v,s)}
122 if (presup) {
123 if (presub) {this.CHTMLplacePresubPresup(presub,pbox,presup,Pbox,delta,u,v,s)}
124 else {this.CHTMLplacePresupOnly(presup,Pbox,delta,u,s)}
125 } else if (presub) {this.CHTMLplacePresubOnly(presub,pbox,v,s)}
126 }
127 }
128 this.CHTML.clean();
129 this.CHTMLhandleSpace(node);
130 this.CHTMLhandleBBox(node);
131 this.CHTMLhandleColor(node);
132 return node;
133 },
134 //
135 // Get the subscript, superscript, presubscript, and presuperscript
136 // boxes, with proper spacing, and computer their bounding boxes.
137 //
138 CHTMLgetScripts: function (BOX,BBOX,stretch,node) {
139 if (stretch) {
140 BOX.sub = CHTML.getNode(node,"mjx-sub");
141 BOX.sup = CHTML.getNode(node,"mjx-sup");
142 BOX.presub = CHTML.getNode(node,"mjx-presub");
143 BOX.presup = CHTML.getNode(node,"mjx-presup");
144 BBOX.sub = this.CHTMLbbox.sub;
145 BBOX.sup = this.CHTMLbbox.sup;
146 BBOX.presub = this.CHTMLbbox.presub;
147 BBOX.presup = this.CHTMLbbox.presup;
148 return;
149 }
150 this.CHTMLbbox = BBOX; // save for when stretched
151 var state = {i:1, w:0, BOX:BOX, BBOX:BBOX}, m = this.data.length;
152 var sub = "sub", sup = "sup";
153 while (state.i < m) {
154 if ((this.data[state.i]||{}).type === "mprescripts") {
155 state.i++; state.w = 0;
156 sub = "presub"; sup = "presup";
157 } else {
158 var sbox = this.CHTMLaddScript(sub,state,node);
159 var Sbox = this.CHTMLaddScript(sup,state,node);
160 var w = Math.max((sbox ? sbox.rscale*sbox.w : 0),(Sbox ? Sbox.rscale*Sbox.w : 0));
161 this.CHTMLpadScript(sub,w,sbox,state);
162 this.CHTMLpadScript(sup,w,Sbox,state);
163 state.w += w;
164 }
165 }
166 if (BBOX.sub) BBOX.sub.clean();
167 if (BBOX.sup) BBOX.sup.clean();
168 if (BBOX.presub) BBOX.presub.clean();
169 if (BBOX.presup) BBOX.presup.clean();
170 },
171 //
172 // Add a script to the proper box, creating the box if needed,
173 // and padding the box to account for any <none/> elements.
174 // Return the bounding box for the script for later use.
175 //
176 CHTMLaddScript: function (type,state,node) {
177 var BOX, BBOX, data = this.data[state.i];
178 if (data && data.type !== "none" && data.type !== "mprescripts") {
179 BOX = state.BOX[type];
180 if (!BOX) {
181 //
182 // Add the box to the node temporarily so that it is in the DOM
183 // (so that CHTMLnodeElement() can be used in the toCommonHTML() below).
184 // See issue #1480.
185 //
186 BOX = state.BOX[type] = CHTML.addElement(node,"mjx-"+type);
187 BBOX = state.BBOX[type] = CHTML.BBOX.empty();
188 if (state.w) {
189 BOX.style.paddingLeft = CHTML.Em(state.w);
190 BBOX.w = BBOX.r = state.w; BBOX.x = state.w;
191 }
192 }
193 data.toCommonHTML(BOX);
194 BBOX = data.CHTML;
195 }
196 if (data && data.type !== "mprescripts") state.i++;
197 return BBOX;
198 },
199 //
200 // Add padding to the script box to make match the width of the
201 // super- or subscript that is above or below it, and adjust the
202 // bounding box for the script row. If these are pre-scripts,
203 // right-justify the scripts, otherwise, left-justify them.
204 //
205 CHTMLpadScript: function (type,w,bbox,state) {
206 if (!bbox) bbox = {w:0, fake:1, rscale:1};
207 var BBOX = state.BBOX[type], dx = 0, dw = 0;
208 if (BBOX) {
209 if (bbox.rscale*bbox.w < w) {
210 var BOX = state.BOX[type]; dw = w-bbox.rscale*bbox.w;
211 var space = CHTML.Element("mjx-spacer",{style:{width:CHTML.Em(dw)}});
212 if (type.substr(0,3) === "pre" && !bbox.fake) {
213 BOX.insertBefore(space,BOX.lastChild);
214 dx = dw; dw = 0;
215 } else {
216 BOX.appendChild(space);
217 }
218 }
219 if (bbox.fake) {BBOX.w += dx} else {BBOX.combine(bbox,BBOX.w+dx,0)}
220 BBOX.w += dw;
221 }
222 },
223 //
224 // Add the boxes into the main node, creating stacks when needed
225 //
226 CHTMLaddBoxes: function (node,base,BOX) {
227 var sub = BOX.sub, sup = BOX.sup, presub = BOX.presub, presup = BOX.presup;
228 if (presub && presup) {
229 var prestack = CHTML.Element("mjx-prestack"); node.insertBefore(prestack,base);
230 prestack.appendChild(presup); prestack.appendChild(presub);
231 } else {
232 if (presub) node.insertBefore(presub,base);
233 if (presup) node.insertBefore(presup,base);
234 }
235 if (sub && sup) {
236 var stack = CHTML.addElement(node,"mjx-stack");
237 stack.appendChild(sup); stack.appendChild(sub);
238 } else {
239 if (sub) node.appendChild(sub);
240 if (sup) node.appendChild(sup);
241 }
242 },
243 //
244 // Handle positioning the various scripts
245 //
246 CHTMLplaceSubOnly: function (sub,sbox,x,v,s) {
247 sub.style.verticalAlign = CHTML.Em(-v);
248 sub.style.marginRight = CHTML.Em(s); sbox.w += s;
249 this.CHTML.combine(sbox,x,-v);
250 },
251 CHTMLplaceSupOnly: function (sup,Sbox,x,delta,u,s) {
252 sup.style.verticalAlign = CHTML.Em(u);
253 sup.style.paddingLeft = CHTML.Em(delta);
254 sup.style.paddingRight = CHTML.Em(s); Sbox.w += s;
255 this.CHTML.combine(Sbox,x+delta,u);
256 },
257 CHTMLplaceSubSup: function (sub,sbox,sup,Sbox,x,delta,u,v,s) {
258 sub.style.paddingRight = CHTML.Em(s); sbox.w += s;
259 sup.style.paddingBottom = CHTML.Em(u+v-Sbox.d-sbox.h);
260 sup.style.paddingLeft = CHTML.Em(delta+(Sbox.x||0));
261 sup.style.paddingRight = CHTML.Em(s); Sbox.w += s;
262 sup.parentNode.style.verticalAlign = CHTML.Em(-v);
263 this.CHTML.combine(sbox,x,-v);
264 this.CHTML.combine(Sbox,x+delta,u);
265 },
266 CHTMLplacePresubOnly: function (presub,pbox,v,s) {
267 presub.style.verticalAlign = CHTML.Em(-v);
268 presub.style.marginLeft = CHTML.Em(s);
269 this.CHTML.combine(pbox,s,-v);
270 },
271 CHTMLplacePresupOnly: function (presup,Pbox,delta,u,s) {
272 presup.style.verticalAlign = CHTML.Em(u);
273 presup.style.paddingLeft = CHTML.Em(s);
274 presup.style.paddingRight = CHTML.Em(-delta);
275 this.CHTML.combine(Pbox,s,u);
276 },
277 CHTMLplacePresubPresup: function (presub,pbox,presup,Pbox,delta,u,v,s) {
278 presub.style.paddingLeft = CHTML.Em(s);
279 presup.style.paddingBottom = CHTML.Em(u+v-Pbox.d-pbox.h);
280 presup.style.paddingLeft = CHTML.Em(delta+s+(Pbox.x||0));
281 presup.style.paddingRight = CHTML.Em(-delta);
282 presup.parentNode.style.verticalAlign = CHTML.Em(-v);
283 this.CHTML.combine(pbox,s,-v);
284 this.CHTML.combine(Pbox,s+delta,u);
285 },
286 //
287 // Handle stretchy bases
288 //
289 CHTMLstretchH: MML.mbase.CHTMLstretchH,
290 CHTMLstretchV: MML.mbase.CHTMLstretchV
291 });
292
293 MathJax.Hub.Startup.signal.Post("CommonHTML mmultiscripts Ready");
294 MathJax.Ajax.loadComplete(CHTML.autoloadDir+"/mmultiscripts.js");
295 });
296