multiline.js 27 KB

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