Debian Codegraph @

unpacked/jax/output/CommonHTML/autoload/multiline.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/multiline.js
7 *
8 * Implements the CommonHTML output for <mrow>'s that contain line breaks.
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 CONFIG = MathJax.Hub.config,
31 CHTML = MathJax.OutputJax.CommonHTML;
32 //
33 // Fake node used for testing end-of-line potential breakpoint
34 //
35 var MO = MML.mo().With({CHTML: CHTML.BBOX.empty()});
36
37 //
38 // Penalties for the various line breaks
39 //
40 var PENALTY = {
41 newline: 0,
42 nobreak: 1000000,
43 goodbreak: [-200],
44 badbreak: [+200],
45 auto: [0],
46
47 maxwidth: 1.33, // stop looking for breaks after this time the line-break width
48 toobig: 800,
49 nestfactor: 400,
50 spacefactor: -100,
51 spaceoffset: 2,
52 spacelimit: 1, // spaces larger than this get a penalty boost
53 fence: 500,
54 close: 500
55 };
56
57 var ENDVALUES = {linebreakstyle: "after"};
58
59
60 /**************************************************************************/
61
62 MML.mbase.Augment({
63 CHTMLlinebreakPenalty: PENALTY,
64
65 /****************************************************************/
66 //
67 // Handle breaking an mrow into separate lines
68 //
69 CHTMLmultiline: function (node) {
70
71 //
72 // Find the parent element and mark it as multiline
73 //
74 var parent = this;
75 while (parent.inferred || (parent.parent && parent.parent.type === "mrow" &&
76 parent.parent.isEmbellished())) {parent = parent.parent}
77 var isTop = ((parent.type === "math" && parent.Get("display") === "block") ||
78 parent.type === "mtd");
79 parent.isMultiline = true;
80
81 //
82 // Default values for the line-breaking parameters
83 //
84 var VALUES = this.getValues(
85 "linebreak","linebreakstyle","lineleading","linebreakmultchar",
86 "indentalign","indentshift",
87 "indentalignfirst","indentshiftfirst",
88 "indentalignlast","indentshiftlast"
89 );
90 if (VALUES.linebreakstyle === MML.LINEBREAKSTYLE.INFIXLINEBREAKSTYLE)
91 VALUES.linebreakstyle = this.Get("infixlinebreakstyle");
92 VALUES.lineleading = this.CHTMLlength2em(VALUES.lineleading,0.5);
93
94 //
95 // Break the math at its best line breaks
96 //
97 CHTML.BBOX.empty(this.CHTML);
98 var stack = CHTML.addElement(node,"mjx-stack");
99 var state = {
100 BBOX: this.CHTML,
101 n: 0, Y: 0,
102 scale: (this.CHTML.scale||1),
103 isTop: isTop,
104 values: {},
105 VALUES: VALUES
106 },
107 align = this.CHTMLgetAlign(state,{}),
108 shift = this.CHTMLgetShift(state,{},align),
109 start = [],
110 end = {
111 index:[], penalty:PENALTY.nobreak,
112 w:0, W:shift, shift:shift, scanW:shift,
113 nest: 0
114 },
115 broken = false;
116
117 while (this.CHTMLbetterBreak(end,state,true) &&
118 (end.scanW >= CHTML.linebreakWidth || end.penalty === PENALTY.newline)) {
119 this.CHTMLaddLine(stack,start,end.index,state,end.values,broken);
120 start = end.index.slice(0); broken = true;
121 align = this.CHTMLgetAlign(state,end.values);
122 shift = this.CHTMLgetShift(state,end.values,align);
123 end.W = end.shift = end.scanW = shift; end.penalty = PENALTY.nobreak;
124 }
125 state.isLast = true;
126 this.CHTMLaddLine(stack,start,[],state,ENDVALUES,broken);
127
128 if (parent.type === "math") {
129 node.style.width = stack.style.width = this.CHTML.pwidth = "100%";
130 }
131 this.CHTML.mwidth = CHTML.Em(this.CHTML.w);
132 this.CHTML.isMultiline = parent.CHTML.isMultiline = true;
133 stack.style.verticalAlign = CHTML.Em(state.d - this.CHTML.d);
134
135 return node;
136 },
137
138 /****************************************************************/
139 //
140 // Locate the next linebreak that is better than the current one
141 //
142 CHTMLbetterBreak: function (info,state,toplevel) {
143 if (this.isToken) return false; // FIXME: handle breaking of token elements
144 if (this.isEmbellished()) {
145 info.embellished = this;
146 return this.CoreMO().CHTMLbetterBreak(info,state);
147 }
148 if (this.linebreakContainer) return false;
149 //
150 // Get the current breakpoint position and other data
151 //
152 var index = info.index.slice(0), i = info.index.shift(),
153 m = this.data.length, W, w, scanW, broken = (info.index.length > 0), better = false;
154 if (i == null) i = -1; if (!broken) {i++; info.W += info.w; info.w = 0}
155 scanW = info.scanW = info.W; info.nest++;
156 //
157 // Look through the line for breakpoints,
158 // (as long as we are not too far past the breaking width)
159 //
160 while (i < m && (info.scanW < PENALTY.maxwidth*CHTML.linebreakWidth || info.w === 0)) {
161 if (this.data[i]) {
162 if (this.data[i].CHTMLbetterBreak(info,state)) {
163 better = true; index = [i].concat(info.index); W = info.W; w = info.w;
164 if (info.penalty === PENALTY.newline) {
165 info.index = index;
166 if (info.nest) {info.nest--}
167 return true;
168 }
169 }
170 scanW = (broken ? info.scanW : this.CHTMLaddWidth(i,info,scanW));
171 }
172 info.index = []; i++; broken = false;
173 }
174 //
175 // Check if end-of-line is a better breakpoint
176 //
177 if (toplevel && better) {
178 MO.parent = this.parent; MO.inherit = this.inherit;
179 if (MO.CHTMLbetterBreak(info,state)) {better = false; index = info.index}
180 }
181 if (info.nest) {info.nest--}
182 info.index = index;
183 if (better) {info.W = W; info.w = w}
184 return better;
185 },
186 CHTMLaddWidth: function (i,info,scanW) {
187 if (this.data[i]) {
188 var bbox = this.data[i].CHTML;
189 scanW += (bbox.w + (bbox.L||0) + (bbox.R||0)) * (bbox.scale || 1);
190 info.W = info.scanW = scanW; info.w = 0;
191 }
192 return scanW;
193 },
194
195 /****************************************************************/
196 //
197 // Create a new line and move the required elements into it
198 // Position it using proper alignment and indenting
199 //
200 CHTMLaddLine: function (stack,start,end,state,values,broken) {
201 //
202 // Create a box for the line, with empty BBox
203 // fill it with the proper elements,
204 // and clean up the bbox
205 //
206 var block = CHTML.addElement(stack,"mjx-block",{},[["mjx-box"]]), line = block.firstChild;
207 var bbox = state.bbox = CHTML.BBOX.empty();
208 state.first = broken; state.last = true;
209 this.CHTMLmoveLine(start,end,line,state,values);
210 bbox.clean();
211 //
212 // Get the alignment and shift values
213 //
214 var align = this.CHTMLgetAlign(state,values),
215 shift = this.CHTMLgetShift(state,values,align,true);
216 //
217 // Set the Y offset based on previous depth, leading, and current height
218 //
219 var dY = 0;
220 if (state.n > 0) {
221 var LHD = CHTML.FONTDATA.baselineskip;
222 var leading = (state.values.lineleading == null ? state.VALUES : state.values).lineleading * state.scale;
223 var Y = state.Y;
224 state.Y -= Math.max(LHD,state.d + bbox.h + leading);
225 dY = Y - state.Y - state.d - bbox.h;
226 }
227 //
228 // Place the new line
229 //
230 if (shift) line.style.margin = "0 "+CHTML.Em(-shift)+" 0 "+CHTML.Em(shift);
231 if (align !== MML.INDENTALIGN.LEFT) block.style.textAlign = align;
232 if (dY) block.style.paddingTop = CHTML.Em(dY);
233 state.BBOX.combine(bbox,shift,state.Y);
234 //
235 // Save the values needed for the future
236 //
237 state.d = state.bbox.d; state.values = values; state.n++;
238 },
239
240 /****************************************************************/
241 //
242 // Get alignment and shift values from the given data
243 //
244 CHTMLgetAlign: function (state,values) {
245 var cur = values, prev = state.values, def = state.VALUES, align;
246 if (state.n === 0) align = cur.indentalignfirst || prev.indentalignfirst || def.indentalignfirst;
247 else if (state.isLast) align = prev.indentalignlast || def.indentalignlast;
248 else align = prev.indentalign || def.indentalign;
249 if (align === MML.INDENTALIGN.INDENTALIGN) align = prev.indentalign || def.indentalign;
250 if (align === MML.INDENTALIGN.AUTO) align = (state.isTop ? CONFIG.displayAlign : MML.INDENTALIGN.LEFT);
251 return align;
252 },
253 CHTMLgetShift: function (state,values,align,noadjust) {
254 var cur = values, prev = state.values, def = state.VALUES, shift;
255 if (state.n === 0) shift = cur.indentshiftfirst || prev.indentshiftfirst || def.indentshiftfirst;
256 else if (state.isLast) shift = prev.indentshiftlast || def.indentshiftlast;
257 else shift = prev.indentshift || def.indentshift;
258 if (shift === MML.INDENTSHIFT.INDENTSHIFT) shift = prev.indentshift || def.indentshift;
259 if (shift === "auto" || shift === "") shift = "0";
260 shift = this.CHTMLlength2em(shift,CHTML.cwidth);
261 if (state.isTop && CONFIG.displayIndent !== "0") {
262 var indent = this.CHTMLlength2em(CONFIG.displayIndent,CHTML.cwidth);
263 shift += (align === MML.INDENTALIGN.RIGHT ? -indent : indent);
264 }
265 return (align === MML.INDENTALIGN.RIGHT && !noadjust ? -shift : shift);
266 },
267
268 /****************************************************************/
269 //
270 // Move the selected elements into the new line's box,
271 // moving whole items when possible, and parts of ones
272 // that are split by a line break.
273 //
274 CHTMLmoveLine: function (start,end,node,state,values) {
275 var i = start[0], j = end[0];
276 if (i == null) i = -1; if (j == null) j = this.data.length-1;
277 if (i === j && start.length > 1) {
278 //
279 // If starting and ending in the same element move the subpiece to the new line
280 //
281 this.data[i].CHTMLmoveSlice(start.slice(1),end.slice(1),node,state,values,"marginLeft");
282 } else {
283 //
284 // Otherwise, move the remainder of the initial item
285 // and any others up to the last one
286 //
287 var last = state.last; state.last = false;
288 while (i < j) {
289 if (this.data[i]) {
290 if (start.length <= 1) this.data[i].CHTMLmoveNode(node,state,values);
291 else this.data[i].CHTMLmoveSlice(start.slice(1),[],node,state,values,"marginLeft");
292 }
293 i++; state.first = false; start = [];
294 }
295 //
296 // If the last item is complete, move it,
297 // otherwise move the first part of it up to the split
298 //
299 state.last = last;
300 if (this.data[i]) {
301 if (end.length <= 1) this.data[i].CHTMLmoveNode(node,state,values);
302 else this.data[i].CHTMLmoveSlice([],end.slice(1),node,state,values,"marginRight");
303 }
304 }
305 },
306
307 /****************************************************************/
308 //
309 // Split an element and copy the selected items into the new part
310 //
311 CHTMLmoveSlice: function (start,end,node,state,values,margin) {
312 //
313 // Create a new box for the slice of the element
314 // Move the selected portion into the slice
315 // If it is the last slice
316 // Remove the original (now empty) node
317 // Rename the Continue-0 node with the original name (for CHTMLnodeElement)
318 //
319 var slice = this.CHTMLcreateSliceNode(node);
320 this.CHTMLmoveLine(start,end,slice,state,values);
321 if (slice.style[margin]) slice.style[margin] = "";
322 if (this.CHTML.L) {
323 if (margin !== "marginLeft") state.bbox.w += this.CHTML.L;
324 else slice.className = slice.className.replace(/ MJXc-space\d/,"");
325 }
326 if (this.CHTML.R && margin !== "marginRight") state.bbox.w += this.CHTML.R;
327 if (end.length === 0) {
328 node = this.CHTMLnodeElement();
329 if (this.href) node = node.parentNode;
330 node.parentNode.removeChild(node);
331 node.nextMathJaxNode.id = node.id;
332 }
333 return slice;
334 },
335
336 /****************************************************************/
337 //
338 // Create a new node for an element that is split in two
339 // Clone the original and update its ID.
340 // Link the old node to the new one so we can find it later
341 //
342 CHTMLcreateSliceNode: function (node) {
343 var NODE = this.CHTMLnodeElement(), n = 0;
344 if (this.href) NODE = NODE.parentNode;
345 var LAST = NODE; while (LAST.nextMathJaxNode) {LAST = LAST.nextMathJaxNode; n++}
346 var SLICE = NODE.cloneNode(false); LAST.nextMathJaxNode = SLICE; SLICE.nextMathJaxNode = null;
347 SLICE.id += "-MJX-Continue-"+n;
348 return node.appendChild(SLICE);
349 },
350
351 /****************************************************************/
352 //
353 // Move an element from its original node to its new location in
354 // a split element or the new line's node
355 //
356 CHTMLmoveNode: function (line,state,values) {
357 // FIXME: handle linebreakstyle === "duplicate"
358 // FIXME: handle linebreakmultchar
359 if (!(state.first || state.last) ||
360 (state.first && state.values.linebreakstyle === MML.LINEBREAKSTYLE.BEFORE) ||
361 (state.last && values.linebreakstyle === MML.LINEBREAKSTYLE.AFTER)) {
362 //
363 // Move node
364 //
365 var node = this.CHTMLnodeElement();
366 if (this.href) node = node.parentNode;
367 line.appendChild(node);
368 if (this.CHTML.pwidth && !line.style.width) line.style.width = this.CHTML.pwidth;
369 //
370 // If it is last, remove right margin
371 // If it is first, remove left margin
372 //
373 if (state.last) node.style.marginRight = "";
374 if (state.first || state.nextIsFirst) {
375 node.style.marginLeft = ""; this.CHTML.L = 0;
376 node.className = node.className.replace(/ MJXc-space\d/,"");
377 }
378 if (state.first && this.CHTML.w === 0) state.nextIsFirst = true;
379 else delete state.nextIsFirst;
380 //
381 // Update bounding box
382 //
383 state.bbox.combine(this.CHTML,state.bbox.w,0);
384 }
385 }
386 });
387
388 /**************************************************************************/
389
390 MML.mfenced.Augment({
391 CHTMLbetterBreak: function (info,state) {
392 //
393 // Get the current breakpoint position and other data
394 //
395 var index = info.index.slice(0), i = info.index.shift(),
396 m = this.data.length, W, w, scanW, broken = (info.index.length > 0), better = false;
397 if (i == null) i = -1; if (!broken) {i++; info.W += info.w; info.w = 0}
398 scanW = info.scanW = info.W; info.nest++;
399 //
400 // Create indices that include the delimiters and separators
401 //
402 if (!this.dataI) {
403 this.dataI = [];
404 if (this.data.open) this.dataI.push("open");
405 if (m) this.dataI.push(0);
406 for (var j = 1; j < m; j++) {
407 if (this.data["sep"+j]) this.dataI.push("sep"+j);
408 this.dataI.push(j);
409 }
410 if (this.data.close) this.dataI.push("close");
411 }
412 m = this.dataI.length;
413 //
414 // Look through the line for breakpoints, including the open, close, and separators
415 // (as long as we are not too far past the breaking width)
416 //
417 while (i < m && (info.scanW < PENALTY.maxwidth*CHTML.linebreakWidth || info.w === 0)) {
418 var k = this.dataI[i];
419 if (this.data[k]) {
420 if (this.data[k].CHTMLbetterBreak(info,state)) {
421 better = true; index = [i].concat(info.index); W = info.W; w = info.w;
422 if (info.penalty === PENALTY.newline) {
423 info.index = index;
424 if (info.nest) info.nest--;
425 return true;
426 }
427 }
428 scanW = (broken ? info.scanW : this.CHTMLaddWidth(i,info,scanW));
429 }
430 info.index = []; i++; broken = false;
431 }
432 if (info.nest) info.nest--;
433 info.index = index;
434 if (better) {info.W = W; info.w = w}
435 return better;
436 },
437
438 CHTMLmoveLine: function (start,end,node,state,values) {
439 var i = start[0], j = end[0];
440 if (i == null) i = -1; if (j == null) j = this.dataI.length-1;
441 if (i === j && start.length > 1) {
442 //
443 // If starting and ending in the same element move the subpiece to the new line
444 //
445 this.data[this.dataI[i]].CHTMLmoveSlice(start.slice(1),end.slice(1),node,state,values,"marginLeft");
446 } else {
447 //
448 // Otherwise, move the remainder of the initial item
449 // and any others (including open and separators) up to the last one
450 //
451 var last = state.last; state.last = false; var k = this.dataI[i];
452 while (i < j) {
453 if (this.data[k]) {
454 if (start.length <= 1) this.data[k].CHTMLmoveNode(node,state,values);
455 else this.data[k].CHTMLmoveSlice(start.slice(1),[],node,state,values,"marginLeft");
456 }
457 i++; k = this.dataI[i]; state.first = false; start = [];
458 }
459 //
460 // If the last item is complete, move it
461 //
462 state.last = last;
463 if (this.data[k]) {
464 if (end.length <= 1) this.data[k].CHTMLmoveNode(node,state,values);
465 else this.data[k].CHTMLmoveSlice([],end.slice(1),node,state,values,"marginRight");
466 }
467 }
468 }
469
470 });
471
472 /**************************************************************************/
473
474 MML.msubsup.Augment({
475 CHTMLbetterBreak: function (info,state) {
476 if (!this.data[this.base]) {return false}
477 //
478 // Get the current breakpoint position and other data
479 //
480 var index = info.index.slice(0), i = info.index.shift(),
481 W, w, scanW, broken = (info.index.length > 0), better = false;
482 if (!broken) {info.W += info.w; info.w = 0}
483 scanW = info.scanW = info.W;
484 //
485 // Record the width of the base and the super- and subscripts
486 //
487 if (i == null) {
488 this.CHTML.baseW = this.data[this.base].CHTML.w;
489 this.CHTML.dw = this.CHTML.w - this.CHTML.baseW;
490 }
491 //
492 // Check if the base can be broken
493 //
494 if (this.data[this.base].CHTMLbetterBreak(info,state)) {
495 better = true; index = [this.base].concat(info.index); W = info.W; w = info.w;
496 if (info.penalty === PENALTY.newline) better = broken = true;
497 }
498 //
499 // Add in the base if it is unbroken, and add the scripts
500 //
501 if (!broken) this.CHTMLaddWidth(this.base,info,scanW);
502 info.scanW += this.CHTML.dw; info.W = info.scanW;
503 info.index = []; if (better) {info.W = W; info.w = w; info.index = index}
504 return better;
505 },
506
507 CHTMLmoveLine: function (start,end,node,state,values) {
508 //
509 // Move the proper part of the base
510 //
511 if (this.data[this.base]) {
512 var base = CHTML.addElement(node,"mjx-base");
513 var ic = this.data[this.base].CHTML.ic;
514 if (ic) base.style.marginRight = CHTML.Em(-ic);
515 if (start.length > 1) {
516 this.data[this.base].CHTMLmoveSlice(start.slice(1),end.slice(1),base,state,values,"marginLeft");
517 } else {
518 if (end.length <= 1) this.data[this.base].CHTMLmoveNode(base,state,values);
519 else this.data[this.base].CHTMLmoveSlice([],end.slice(1),base,state,values,"marginRight");
520 }
521 }
522 //
523 // If this is the end, check for super and subscripts, and move those
524 // by moving the elements that contains them. Adjust the bounding box
525 // to include the super and subscripts.
526 //
527 if (end.length === 0) {
528 var NODE = this.CHTMLnodeElement(),
529 stack = CHTML.getNode(NODE,"mjx-stack"),
530 sup = CHTML.getNode(NODE,"mjx-sup"),
531 sub = CHTML.getNode(NODE,"mjx-sub");
532 if (stack) node.appendChild(stack);
533 else if (sup) node.appendChild(sup);
534 else if (sub) node.appendChild(sub);
535 var w = state.bbox.w, bbox;
536 if (sup) {
537 bbox = this.data[this.sup].CHTML;
538 state.bbox.combine(bbox,w,bbox.Y);
539 }
540 if (sub) {
541 bbox = this.data[this.sub].CHTML;
542 state.bbox.combine(bbox,w,bbox.Y);
543 }
544 }
545 }
546
547 });
548
549 /**************************************************************************/
550
551 MML.mmultiscripts.Augment({
552 CHTMLbetterBreak: function (info,state) {
553 if (!this.data[this.base]) return false;
554 //
555 // Get the current breakpoint position and other data
556 //
557 var index = info.index.slice(0); info.index.shift();
558 var W, w, scanW, broken = (info.index.length > 0), better = false;
559 if (!broken) {info.W += info.w; info.w = 0}
560 info.scanW = info.W;
561 //
562 // Get the bounding boxes and the width of the scripts
563 //
564 var bbox = this.CHTML, base = this.data[this.base].CHTML;
565 var dw = bbox.w - base.w - (bbox.X||0);
566 //
567 // Add in the width of the prescripts
568 //
569 info.scanW += bbox.X||0; scanW = info.scanW;
570 //
571 // Check if the base can be broken
572 //
573 if (this.data[this.base].CHTMLbetterBreak(info,state)) {
574 better = true; index = [this.base].concat(info.index); W = info.W; w = info.w;
575 if (info.penalty === PENALTY.newline) better = broken = true;
576 }
577 //
578 // Add in the base if it is unbroken, and add the scripts
579 //
580 if (!broken) this.CHTMLaddWidth(this.base,info,scanW);
581 info.scanW += dw; info.W = info.scanW;
582 info.index = []; if (better) {info.W = W; info.w = w; info.index = index}
583 return better;
584 },
585
586 CHTMLmoveLine: function (start,end,node,state,values) {
587 var NODE, BOX = this.CHTMLbbox, w;
588 //
589 // If this is the start, move the prescripts, if any.
590 //
591 if (start.length < 1) {
592 NODE = this.CHTMLnodeElement();
593 var prestack = CHTML.getNode(NODE,"mjx-prestack"),
594 presup = CHTML.getNode(NODE,"mjx-presup"),
595 presub = CHTML.getNode(NODE,"mjx-presub");
596 if (prestack) node.appendChild(prestack);
597 else if (presup) node.appendChild(presup);
598 else if (presub) node.appendChild(presub);
599 w = state.bbox.w;
600 if (presup) state.bbox.combine(BOX.presup,w+BOX.presup.X,BOX.presup.Y);
601 if (presub) state.bbox.combine(BOX.presub,w+BOX.presub.X,BOX.presub.Y);
602 }
603 //
604 // Move the proper part of the base
605 //
606 if (this.data[this.base]) {
607 var base = CHTML.addElement(node,"mjx-base");
608 if (start.length > 1) {
609 this.data[this.base].CHTMLmoveSlice(start.slice(1),end.slice(1),base,state,values,"marginLeft");
610 } else {
611 if (end.length <= 1) this.data[this.base].CHTMLmoveNode(base,state,values);
612 else this.data[this.base].CHTMLmoveSlice([],end.slice(1),base,state,values,"marginRight");
613 }
614 }
615 //
616 // If this is the end, check for super and subscripts, and move those
617 // by moving the elements that contains them. Adjust the bounding box
618 // to include the super and subscripts.
619 //
620 if (end.length === 0) {
621 NODE = this.CHTMLnodeElement();
622 var stack = CHTML.getNode(NODE,"mjx-stack"),
623 sup = CHTML.getNode(NODE,"mjx-sup"),
624 sub = CHTML.getNode(NODE,"mjx-sub");
625 if (stack) node.appendChild(stack);
626 else if (sup) node.appendChild(sup);
627 else if (sub) node.appendChild(sub);
628 w = state.bbox.w;
629 if (sup) state.bbox.combine(BOX.sup,w,BOX.sup.Y);
630 if (sub) state.bbox.combine(BOX.sub,w,BOX.sub.Y);
631 }
632 }
633
634 });
635
636 /**************************************************************************/
637
638 MML.mo.Augment({
639 //
640 // Override the method for checking line breaks to properly handle <mo>
641 //
642 CHTMLbetterBreak: function (info,state) {
643 if (info.values && info.values.id === this.CHTMLnodeID) return false;
644 var values = this.getValues(
645 "linebreak","linebreakstyle","lineleading","linebreakmultchar",
646 "indentalign","indentshift",
647 "indentalignfirst","indentshiftfirst",
648 "indentalignlast","indentshiftlast",
649 "texClass", "fence"
650 );
651 if (values.linebreakstyle === MML.LINEBREAKSTYLE.INFIXLINEBREAKSTYLE)
652 values.linebreakstyle = this.Get("infixlinebreakstyle");
653 //
654 // Adjust nesting by TeX class (helps output that does not include
655 // mrows for nesting, but can leave these unbalanced.
656 //
657 if (values.texClass === MML.TEXCLASS.OPEN) info.nest++;
658 if (values.texClass === MML.TEXCLASS.CLOSE && info.nest) info.nest--;
659 //
660 // Get the default penalty for this location
661 //
662 var W = info.scanW; delete info.embellished;
663 var w = this.CHTML.w + (this.CHTML.L||0) + (this.CHTML.R||0);
664 if (values.linebreakstyle === MML.LINEBREAKSTYLE.AFTER) {W += w; w = 0}
665 if (W - info.shift === 0 && values.linebreak !== MML.LINEBREAK.NEWLINE)
666 return false; // don't break at zero width (FIXME?)
667 var offset = CHTML.linebreakWidth - W;
668 // Adjust offest for explicit first-line indent and align
669 if (state.n === 0 && (values.indentshiftfirst !== state.VALUES.indentshiftfirst ||
670 values.indentalignfirst !== state.VALUES.indentalignfirst)) {
671 var align = this.CHTMLgetAlign(state,values),
672 shift = this.CHTMLgetShift(state,values,align);
673 offset += (info.shift - shift);
674 }
675 //
676 var penalty = Math.floor(offset / CHTML.linebreakWidth * 1000);
677 if (penalty < 0) penalty = PENALTY.toobig - 3*penalty;
678 if (values.fence) penalty += PENALTY.fence;
679 if ((values.linebreakstyle === MML.LINEBREAKSTYLE.AFTER &&
680 values.texClass === MML.TEXCLASS.OPEN) ||
681 values.texClass === MML.TEXCLASS.CLOSE) penalty += PENALTY.close;
682 penalty += info.nest * PENALTY.nestfactor;
683 //
684 // Get the penalty for this type of break and
685 // use it to modify the default penalty
686 //
687 var linebreak = PENALTY[values.linebreak||MML.LINEBREAK.AUTO]||0;
688 if (!MathJax.Object.isArray(linebreak)) {
689 // for breaks past the width, keep original penalty for newline
690 if (linebreak || offset >= 0) {penalty = linebreak * info.nest}
691 } else {penalty = Math.max(1,penalty + linebreak[0] * info.nest)}
692 //
693 // If the penalty is no better than the current one, return false
694 // Otherwise save the data for this breakpoint and return true
695 //
696 if (penalty >= info.penalty) return false;
697 info.penalty = penalty; info.values = values; info.W = W; info.w = w;
698 values.lineleading = this.CHTMLlength2em(values.lineleading,state.VALUES.lineleading);
699 values.id = this.CHTMLnodeID;
700 return true;
701 }
702 });
703
704 /**************************************************************************/
705
706 MML.mspace.Augment({
707 //
708 // Override the method for checking line breaks to properly handle <mspace>
709 //
710 CHTMLbetterBreak: function (info,state) {
711 if (info.values && info.values.id === this.CHTMLnodeID) return false;
712 var values = this.getValues("linebreak");
713 var linebreakValue = values.linebreak;
714 if (!linebreakValue || this.hasDimAttr()) {
715 // The MathML spec says that the linebreak attribute should be ignored
716 // if any dimensional attribute is set.
717 linebreakValue = MML.LINEBREAK.AUTO;
718 }
719 //
720 // Get the default penalty for this location
721 //
722 var W = info.scanW, w = this.CHTML.w + (this.CHTML.L||0) + (this.CHTML.R||0);
723 if (W - info.shift === 0) return false; // don't break at zero width (FIXME?)
724 var offset = CHTML.linebreakWidth - W;
725 //
726 var penalty = Math.floor(offset / CHTML.linebreakWidth * 1000);
727 if (penalty < 0) penalty = PENALTY.toobig - 3*penalty;
728 penalty += info.nest * PENALTY.nestfactor;
729 //
730 // Get the penalty for this type of break and
731 // use it to modify the default penalty
732 //
733 var linebreak = PENALTY[linebreakValue]||0;
734 if (linebreakValue === MML.LINEBREAK.AUTO && w >= PENALTY.spacelimit &&
735 !this.mathbackground && !this.background)
736 linebreak = [(w+PENALTY.spaceoffset)*PENALTY.spacefactor];
737 if (!MathJax.Object.isArray(linebreak)) {
738 // for breaks past the width, keep original penalty for newline
739 if (linebreak || offset >= 0) {penalty = linebreak * info.nest}
740 } else {penalty = Math.max(1,penalty + linebreak[0] * info.nest)}
741 //
742 // If the penalty is no better than the current one, return false
743 // Otherwise save the data for this breakpoint and return true
744 //
745 if (penalty >= info.penalty) return false;
746 info.penalty = penalty; info.values = values; info.W = W; info.w = w;
747 values.lineleading = state.VALUES.lineleading;
748 values.linebreakstyle = "before"; values.id = this.CHTMLnodeID;
749 return true;
750 }
751 });
752
753 //
754 // Hook into the mathchoice extension
755 //
756 MathJax.Hub.Register.StartupHook("TeX mathchoice Ready",function () {
757 MML.TeXmathchoice.Augment({
758 CHTMLbetterBreak: function (info,state) {
759 return this.Core().CHTMLbetterBreak(info,state);
760 },
761 CHTMLmoveLine: function (start,end,node,state,values) {
762 return this.Core().CHTMLmoveSlice(start,end,node,state,values);
763 }
764 });
765 });
766
767 //
768 // Have maction process only the selected item
769 //
770 MML.maction.Augment({
771 CHTMLbetterBreak: function (info,state) {
772 return this.Core().CHTMLbetterBreak(info,state);
773 },
774 CHTMLmoveLine: function (start,end,node,state,values) {
775 return this.Core().CHTMLmoveSlice(start,end,node,state,values);
776 }
777 });
778
779 //
780 // Have semantics only do the first element
781 // (FIXME: do we need to do anything special about annotation-xml?)
782 //
783 MML.semantics.Augment({
784 CHTMLbetterBreak: function (info,state) {
785 return (this.data[0] ? this.data[0].CHTMLbetterBreak(info,state) : false);
786 },
787 CHTMLmoveLine: function (start,end,node,state,values) {
788 return (this.data[0] ? this.data[0].CHTMLmoveSlice(start,end,node,state,values) : null);
789 }
790 });
791
792 /**************************************************************************/
793
794 MathJax.Hub.Startup.signal.Post("CommonHTML multiline Ready");
795 MathJax.Ajax.loadComplete(CHTML.autoloadDir+"/multiline.js");
796
797 });