Debian Codegraph @

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