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