multiline.js 12 KB

12345678910111213141516171819202122232425
  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/HTML-CSS/autoload/multiline.js
  6. *
  7. * Implements the HTML-CSS output for <mrow>'s that contain line breaks.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2010-2013 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. */MathJax.Hub.Register.StartupHook("HTML-CSS Jax Ready",function(){var l=MathJax.ElementJax.mml,m=MathJax.OutputJax["HTML-CSS"],n={newline:0,nobreak:1E6,goodbreak:[-200],badbreak:[200],auto:[0],toobig:800,nestfactor:400,spacefactor:-100,spaceoffset:2,spacelimit:1,fence:500,close:500},r={linebreakstyle:"after"};l.mbase.Augment({HTMLlinebreakPenalty:n,HTMLmultiline:function(a){for(var c=this;c.inferred||c.parent&&"mrow"===c.parent.type&&1===c.parent.data.length;)c=c.parent;var d="math"===c.type&&"block"===c.Get("display")||"mtd"===c.type;c.isMultiline=!0;var b=this.getValues("linebreak","linebreakstyle","lineleading","linebreakmultchar","indentalign","indentshift","indentalignfirst","indentshiftfirst","indentalignlast","indentshiftlast");b.linebreakstyle===l.LINEBREAKSTYLE.INFIXLINEBREAKSTYLE&&(b.linebreakstyle=this.Get("infixlinebreakstyle"));b.lineleading=m.length2em(b.lineleading,1,.5);this.HTMLremoveColor(a);for(var e=m.createStack(a),b={n:0,Y:0,scale:this.HTMLgetScale(),isTop:d,values:{},VALUES:b},g=this.HTMLgetAlign(b,{}),f=this.HTMLgetShift(b,{},g),k=[],h={index:[],penalty:n.nobreak,w:0,W:f,shift:f,scanW:f,nest:0},q=!1;this.HTMLbetterBreak(h,b)&&(h.scanW>=m.linebreakWidth||h.penalty==n.newline);)this.HTMLaddLine(e,k,h.index,b,h.values,q),k=h.index.slice(0),q=!0,g=this.HTMLgetAlign(b,h.values),f=this.HTMLgetShift(b,h.values,g),g===l.INDENTALIGN.CENTER&&(f=0),h.W=h.shift=h.scanW=f,h.penalty=n.nobreak;b.isLast=!0;this.HTMLaddLine(e,k,[],b,r,q);d&&(e.style.width="100%","math"===c.type&&(a.bbox.width="100%"));this.HTMLhandleSpace(a);this.HTMLhandleColor(a);a.bbox.isMultiline=!0;return a},HTMLbetterBreak:function(a,c){if(this.isToken)return!1;if(this.isEmbellished())return a.embellished=this,this.CoreMO().HTMLbetterBreak(a,c);if(this.linebreakContainer)return!1;var d=a.index.slice(0),b=a.index.shift(),e=this.data.length,g,f,k,h=0<a.index.length,l=!1;null==b&&(b=-1);h||(b++,a.W+=a.w,a.w=0);k=a.scanW=a.W;for(a.nest++;b<e&&a.scanW<1.33*m.linebreakWidth;){if(this.data[b]){if(this.data[b].HTMLbetterBreak(a,c)&&(l=!0,d=[b].concat(a.index),g=a.W,f=a.w,a.penalty===n.newline))return a.index=d,a.nest--,!0;k=h?a.scanW:this.HTMLaddWidth(b,a,k)}a.index=[];b++;h=!1}a.nest--;a.index=d;l&&(a.W=g,a.w=f);return l},HTMLaddWidth:function(a,c,d){this.data[a]&&(a=this.data[a].HTMLspanElement(),d+=a.bbox.w,a.style.paddingLeft&&(d+=m.unEm(a.style.paddingLeft)),a.style.paddingRight&&(d+=m.unEm(a.style.paddingRight)),c.W=c.scanW=d,c.w=0);return d},HTMLaddLine:function(a,c,d,b,e,g){line=m.createBox(a);line.bbox=this.HTMLemptyBBox({});b.first=g;b.last=!0;this.HTMLmoveLine(c,d,line,b,e);this.HTMLcleanBBox(line.bbox);a=this.HTMLgetAlign(b,e);if(c=this.HTMLgetShift(b,e,a))m.createBlank(line,c,a!==l.INDENTALIGN.RIGHT),line.bbox.w+=c,line.bbox.rw+=c;0<b.n&&(b.Y-=Math.max(m.FONTDATA.baselineskip*b.scale,b.d+line.bbox.h+(null==b.values.lineleading?b.VALUES:b.values).lineleading));m.alignBox(line,a,b.Y);b.d=line.bbox.d;b.values=e;b.n++},HTMLgetAlign:function(a,c){var d=a.values,b=a.VALUES,e;e=0===a.n?c.indentalignfirst||d.indentalignfirst||b.indentalignfirst:a.isLast?d.indentalignlast||b.indentalignlast:d.indentalign||b.indentalign;e===l.INDENTALIGN.INDENTALIGN&&(e=d.indentalign||b.indentalign);e===l.INDENTALIGN.AUTO&&(e=a.isTop?this.displayAlign:l.INDENTALIGN.LEFT);return e},HTMLgetShift:function(a,c,d){if(d===l.INDENTALIGN.CENTER)return 0;d=a.values;var b=a.VALUES;c=0===a.n?c.indentshiftfirst||d.indentshiftfirst||b.indentshiftfirst:a.isLast?d.indentshiftlast||b.indentshiftlast:d.indentshift||b.indentshift;c===l.INDENTSHIFT.INDENTSHIFT&&(c=d.indentshift||b.indentshift);if("auto"===c||""===c)c=a.isTSop?this.displayIndent:"0";return m.length2em(c,0)},HTMLmoveLine:function(a,c,d,b,e){var g=a[0],f=c[0];null==g&&(g=-1);null==f&&(f=this.data.length-1);if(g===f&&1<a.length)this.data[g].HTMLmoveSlice(a.slice(1),c.slice(1),d,b,e,"paddingLeft");else{var k=b.last;for(b.last=!1;g<f;)this.data[g]&&(1>=a.length?this.data[g].HTMLmoveSpan(d,b,e):this.data[g].HTMLmoveSlice(a.slice(1),[],d,b,e,"paddingLeft")),g++,b.first=!1,a=[];b.last=k;this.data[g]&&(1>=c.length?this.data[g].HTMLmoveSpan(d,b,e):this.data[g].HTMLmoveSlice([],c.slice(1),d,b,e,"paddingRight"))}},HTMLmoveSlice:function(a,c,d,b,e,g){this.HTMLremoveColor();var f=this.HTMLcreateSliceSpan(d);this.HTMLmoveLine(a,c,f,b,e);f.style[g]="";this.HTMLcombineBBoxes(f,d.bbox);this.HTMLcleanBBox(f.bbox);if(0===c.length)for(d=this.HTMLspanElement(),d.parentNode.removeChild(d),d.nextMathJaxSpan.id=d.id,a=0;d=d.nextMathJaxSpan;)if(c=this.HTMLhandleColor(d))c.id+="-MathJax-Continue-"+a,a++;return f},HTMLcreateSliceSpan:function(a){for(var c=this.HTMLspanElement(),d=0,b=c;b.nextMathJaxSpan;)b=b.nextMathJaxSpan,d++;c=c.cloneNode(!1);b.nextMathJaxSpan=c;c.nextMathJaxSpan=null;c.id+="-MathJax-Continue-"+d;c.bbox=this.HTMLemptyBBox({});return a.appendChild(c)},HTMLmoveSpan:function(a,c,d){if(!c.first&&!c.last||c.first&&c.values.linebreakstyle===l.LINEBREAKSTYLE.BEFORE||c.last&&d.linebreakstyle===l.LINEBREAKSTYLE.AFTER){(d=document.getElementById("MathJax-Color-"+this.spanID+m.idPostfix))&&a.appendChild(d);var b=this.HTMLspanElement();a.appendChild(b);c.last&&(b.style.paddingRight="");if(c.first||c.nextIsFirst)delete c.nextIsFirst,b.style.paddingLeft="",d&&(this.HTMLremoveColor(b),this.HTMLhandleColor(b));this.HTMLcombineBBoxes(this,a.bbox)}else c.first?c.nextIsFirst=!0:delete c.nextIsFirst}});l.mfenced.Augment({HTMLbetterBreak:function(a,c){var d=a.index.slice(0),b=a.index.shift(),e=this.data.length,g,f,k,h=0<a.index.length,l=!1;null==b&&(b=-1);h||(b++,a.W+=a.w,a.w=0);k=a.scanW=a.W;a.nest++;if(!this.dataI){this.dataI=[];this.data.open&&this.dataI.push("open");e&&this.dataI.push(0);for(var p=1;p<e;p++)this.data["sep"+p]&&this.dataI.push("sep"+p),this.dataI.push(p);this.data.close&&this.dataI.push("close")}for(e=this.dataI.length;b<e&&a.scanW<1.33*m.linebreakWidth;){p=this.dataI[b];if(this.data[p]){if(this.data[p].HTMLbetterBreak(a,c)&&(l=!0,d=[b].concat(a.index),g=a.W,f=a.w,a.penalty===n.newline))return a.index=d,a.nest--,!0;k=h?a.scanW:this.HTMLaddWidth(b,a,k)}a.index=[];b++;h=!1}a.nest--;a.index=d;l&&(a.W=g,a.w=f);return l},HTMLmoveLine:function(a,c,d,b,e){var g=a[0],f=c[0];null==g&&(g=-1);null==f&&(f=this.dataI.length-1);if(g===f&&1<a.length)this.data[this.dataI[g]].HTMLmoveSlice(a.slice(1),c.slice(1),d,b,e,"paddingLeft");else{var k=b.last;b.last=!1;for(var h=this.dataI[g];g<f;)this.data[h]&&(1>=a.length?this.data[h].HTMLmoveSpan(d,b,e):this.data[h].HTMLmoveSlice(a.slice(1),[],d,b,e,"paddingLeft")),g++,h=this.dataI[g],b.first=!1,a=[];b.last=k;this.data[h]&&(1>=c.length?this.data[h].HTMLmoveSpan(d,b,e):this.data[h].HTMLmoveSlice([],c.slice(1),d,b,e,"paddingRight"))}}});l.msubsup.Augment({HTMLbetterBreak:function(a,c){if(!this.data[this.base])return!1;var d=a.index.slice(0),b=a.index.shift(),e,g,f,k=0<a.index.length,h=!1;k||(a.W+=a.w,a.w=0);f=a.scanW=a.W;null==b&&(this.HTMLbaseW=this.data[this.base].HTMLspanElement().bbox.w,this.HTMLdw=this.HTMLspanElement().bbox.w-this.HTMLbaseW);this.data[this.base].HTMLbetterBreak(a,c)&&(h=!0,d=[this.base].concat(a.index),e=a.W,g=a.w,a.penalty===n.newline&&(h=k=!0));k||this.HTMLaddWidth(this.base,a,f);a.scanW+=this.HTMLdw;a.W=a.scanW;a.index=[];h&&(a.W=e,a.w=g,a.index=d);return h},HTMLmoveLine:function(a,c,d,b,e){this.data[this.base]&&(1<a.length?this.data[this.base].HTMLmoveSlice(a.slice(1),c.slice(1),d,b,e,"paddingLeft"):1>=c.length?this.data[this.base].HTMLmoveSpan(d,b,e):this.data[this.base].HTMLmoveSlice([],c.slice(1),d,b,e,"paddingRight"));if(0===c.length&&(a=this.data[this.sup]||this.data[this.sub])){a=a.HTMLspanElement().parentNode;c=a.parentNode;this.data[this.base]&&c.removeChild(c.firstChild);for(a=c.firstChild;a;a=a.nextSibling)a.style.left=m.Em(m.unEm(a.style.left)-this.HTMLbaseW);c.bbox.w-=this.HTMLbaseW;c.style.width=m.Em(c.bbox.w);this.HTMLcombineBBoxes(c,d.bbox);d.appendChild(c)}}});l.mo.Augment({HTMLbetterBreak:function(a,c){if(a.values&&a.values.id===this.spanID)return!1;var d=this.getValues("linebreak","linebreakstyle","lineleading","linebreakmultchar","indentalign","indentshift","indentalignfirst","indentshiftfirst","indentalignlast","indentshiftlast","texClass","fence");d.linebreakstyle===l.LINEBREAKSTYLE.INFIXLINEBREAKSTYLE&&(d.linebreakstyle=this.Get("infixlinebreakstyle"));d.texClass===l.TEXCLASS.OPEN&&a.nest++;d.texClass===l.TEXCLASS.CLOSE&&a.nest--;var b=a.scanW,e=a.embellished||this;delete a.embellished;var g=e.HTMLspanElement(),e=g.bbox.w;g.style.paddingLeft&&(e+=m.unEm(g.style.paddingLeft));d.linebreakstyle===l.LINEBREAKSTYLE.AFTER&&(b+=e,e=0);if(0===b-a.shift)return!1;g=m.linebreakWidth-b;if(0===c.n&&(d.indentshiftfirst!==c.VALUES.indentshiftfirst||d.indentalignfirst!==c.VALUES.indentalignfirst))var f=this.HTMLgetAlign(c,d),f=this.HTMLgetShift(c,d,f),g=g+(a.shift-f);f=Math.floor(g/m.linebreakWidth*1E3);0>f&&(f=n.toobig-3*f);d.fence&&(f+=n.fence);if(d.linebreakstyle===l.LINEBREAKSTYLE.AFTER&&d.texClass===l.TEXCLASS.OPEN||d.texClass===l.TEXCLASS.CLOSE)f+=n.close;var f=f+a.nest*n.nestfactor,k=n[d.linebreak||l.LINEBREAK.AUTO];k instanceof Array?f=Math.max(1,f+k[0]*a.nest):0<=g&&(f=k*a.nest);if(f>=a.penalty)return!1;a.penalty=f;a.values=d;a.W=b;a.w=e;d.lineleading=m.length2em(d.lineleading,c.VALUES.lineleading);d.id=this.spanID;return!0}});l.mspace.Augment({HTMLbetterBreak:function(a,c){if(a.values&&a.values.id===this.spanID)return!1;var d=this.getValues("linebreak"),b=d.linebreak;if(!b||this.hasDimAttr())b=l.LINEBREAK.AUTO;var e=a.scanW,g=this.HTMLspanElement(),f=g.bbox.w;g.style.paddingLeft&&(f+=m.unEm(g.style.paddingLeft));if(0===e-a.shift)return!1;var g=m.linebreakWidth-e,k=Math.floor(g/m.linebreakWidth*1E3);0>k&&(k=n.toobig-3*k);var k=k+a.nest*n.nestfactor,h=n[b];b===l.LINEBREAK.AUTO&&f>=n.spacelimit&&(h=[(f+n.spaceoffset)*n.spacefactor]);h instanceof Array?k=Math.max(1,k+h[0]*a.nest):0<=g&&(k=h*a.nest);if(k>=a.penalty)return!1;a.penalty=k;a.values=d;a.W=e;a.w=f;d.lineleading=c.VALUES.lineleading;d.linebreakstyle="before";d.id=this.spanID;return!0}});MathJax.Hub.Register.StartupHook("TeX mathchoice Ready",function(){l.TeXmathchoice.Augment({HTMLbetterBreak:function(a,c){return this.Core().HTMLbetterBreak(a,c)},HTMLmoveLine:function(a,c,d,b,e){return this.Core().HTMLmoveSlice(a,c,d,b,e)}})});l.maction.Augment({HTMLbetterBreak:function(a,c){return this.Core().HTMLbetterBreak(a,c)},HTMLmoveLine:function(a,c,d,b,e){return this.Core().HTMLmoveSlice(a,c,d,b,e)},HTMLmoveSlice:function(a,c,d,b,e,g){var f=document.getElementById("MathJax-HitBox-"+this.spanID+m.idPostfix);f&&f.parentNode.removeChild(f);f=this.SUPER(arguments).HTMLmoveSlice.apply(this,arguments);if(0===c.length){d=this.HTMLspanElement();for(var k=0;d;)this.HTMLhandleHitBox(d,"-Continue-"+k),d=d.nextMathJaxSpan,k++}return f}});l.semantics.Augment({HTMLbetterBreak:function(a,c){return this.data[0]?this.data[0].HTMLbetterBreak(a,c):!1},HTMLmoveLine:function(a,c,d,b,e){return this.data[0]?this.data[0].HTMLmoveSlice(a,c,d,b,e):null}});MathJax.Hub.Startup.signal.Post("HTML-CSS multiline Ready");MathJax.Ajax.loadComplete(m.autoloadDir+"/multiline.js")});