mtable.js 10 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/mtable.js
  6. *
  7. * Implements the HTML-CSS output for <mtable> elements.
  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,c=MathJax.OutputJax["HTML-CSS"];L.mtable.Augment({toHTML:function(x){x=this.HTMLcreateSpan(x);if(0===this.data.length)return x;var g=this.getValues("columnalign","rowalign","columnspacing","rowspacing","columnwidth","equalcolumns","equalrows","columnlines","rowlines","frame","framespacing","align","useHeight","width","side","minlabelspacing"),G=g.width.match(/%$/),t=c.createStack(x),E=this.HTMLgetScale(),H=this.HTMLgetMu(x),h=[],m=[],k=[],d=[],n=[],a,b,u=-1,f,I,p,r,w,A,l=[],M=c.FONTDATA.baselineskip*E*g.useHeight,q,J=c.FONTDATA.lineH*E,N=c.FONTDATA.lineD*E;a=0;for(f=this.data.length;a<f;a++)for(r=this.data[a],p="mlabeledtr"===r.type?-1:0,d[a]=[],h[a]=m[a]=0,b=p,I=r.data.length+p;b<I;b++)null==k[b]&&(b>u&&(u=b),n[b]=c.createStack(c.createBox(t)),k[b]=-c.BIGDIMEN),d[a][b]=c.createBox(n[b]),l.push(r.data[b-p].toHTML(d[a][b]));c.MeasureSpans(l);a=0;for(f=this.data.length;a<f;a++)for(r=this.data[a],b=p="mlabeledtr"===r.type?-1:0,I=r.data.length+p;b<I;b++){w=r.data[b-p];w.isMultiline&&(d[a][b].style.width="100%");if(w.isEmbellished()&&(A=w.CoreMO(),l=A.Get("minsize",!0))){var B=A.HTMLspanElement().bbox;if(A.HTMLcanStretch("Vertical")){if(q=B.h+B.d)l=c.length2em(l,H,q),l*B.h/q>h[a]&&(h[a]=l*B.h/q),l*B.d/q>m[a]&&(m[a]=l*B.d/q)}else A.HTMLcanStretch("Horizontal")&&(l=c.length2em(l,H,B.w),l>k[b]&&(k[b]=l))}d[a][b].bbox.h>h[a]&&(h[a]=d[a][b].bbox.h);d[a][b].bbox.d>m[a]&&(m[a]=d[a][b].bbox.d);d[a][b].bbox.w>k[b]&&(k[b]=d[a][b].bbox.w)}h[0]+m[0]&&(h[0]=Math.max(h[0],J));h[d.length-1]+m[d.length-1]&&(m[d.length-1]=Math.max(m[d.length-1],N));var C=MathJax.Hub.SplitList,l=C(g.columnspacing),J=C(g.rowspacing);I=C(g.columnalign);var e=C(g.rowalign),B=C(g.columnlines),N=C(g.rowlines),y=C(g.columnwidth),F=[];a=0;for(f=l.length;a<f;a++)l[a]=c.length2em(l[a],H);a=0;for(f=J.length;a<f;a++)J[a]=c.length2em(J[a],H);for(;l.length<u;)l.push(l[l.length-1]);for(;I.length<=u;)I.push(I[I.length-1]);for(;B.length<u;)B.push(B[B.length-1]);for(;y.length<=u;)y.push(y[y.length-1]);for(;J.length<d.length;)J.push(J[J.length-1]);for(;e.length<=d.length;)e.push(e[e.length-1]);for(;N.length<d.length;)N.push(N[N.length-1]);n[-1]&&(I[-1]="l"===g.side.substr(0,1)?"left":"right",l[-1]=-k[-1]);a=0;for(f=d.length;a<f;a++)if(r=this.data[a],F[a]=[],r.rowalign&&(e[a]=r.rowalign),r.columnalign)for(F[a]=C(r.columnalign);F[a].length<=u;)F[a].push(F[a][F[a].length-1]);if(g.equalrows){r=Math.max.apply(Math,h);var O=Math.max.apply(Math,m);a=0;for(f=d.length;a<f;a++)p=(r+O-(h[a]+m[a]))/2,h[a]+=p,m[a]+=p}q=h[0]+m[d.length-1];a=0;for(f=d.length-1;a<f;a++)q+=Math.max(h[a]+m[a]?M:0,m[a]+h[a+1]+J[a]);a=O=0;r=q;if("none"!==g.frame||(g.columnlines+g.rowlines).match(/solid|dashed/))r=C(g.framespacing),2!=r.length&&(r=C(this.defaults.framespacing)),O=c.length2em(r[0],H),a=c.length2em(r[1],H),r=q+2*a;b="";"string"!==typeof g.align&&(g.align=String(g.align));g.align.match(/(top|bottom|center|baseline|axis)( +(-?\d+))?/)?(b=RegExp.$3,g.align=RegExp.$1):g.align=this.defaults.align;if(""!==b)for(b=parseInt(b),0>b&&(b=d.length+1+b),1>b?b=1:b>d.length&&(b=d.length),C=0,E=-(q+a)+h[0],a=0,f=b-1;a<f;a++)b=Math.max(h[a]+m[a]?M:0,m[a]+h[a+1]+J[a]),C+=b,E+=b;else C={top:-(h[0]+a),bottom:q+a-h[0],center:q/2-h[0],baseline:q/2-h[0],axis:q/2+c.TeX.axis_height*E-h[0]}[g.align],E={top:-(q+2*a),bottom:0,center:-(q/2+a),baseline:-(q/2+a),axis:c.TeX.axis_height*E-q/2-a}[g.align];var v,D=0,z=0;q=A=w=0;b=[];p=[];var Q=1;if(g.equalcolumns&&"auto"!==g.width)if(G){v=(100/(u+1)).toFixed(2).replace(/\.?0+$/,"")+"%";a=0;for(f=Math.min(u+1,y.length);a<f;a++)y[a]=v;v=0;D=1;q=u+1;a=0;for(f=Math.min(u+1,l.length);a<f;a++)v+=l[a]}else{v=c.length2em(g.width,H);a=0;for(f=Math.min(u+1,l.length);a<f;a++)v-=l[a];v/=u+1;a=0;for(f=Math.min(u+1,y.length);a<f;a++)k[a]=v}else{a=0;for(f=Math.min(u+1,y.length);a<f;a++)"auto"===y[a]?z+=k[a]:"fit"===y[a]?(p[q]=a,q++,z+=k[a]):y[a].match(/%$/)?(b[A]=a,A++,w+=k[a],D+=c.length2em(y[a],H,1)):(k[a]=c.length2em(y[a],H),z+=k[a]);if(G){a=v=0;for(f=Math.min(u,l.length);a<f;a++)v+=l[a];.98<D&&(Q=.98/D,D=.98)}else{if("auto"===g.width).98<D?(Q=w/(z+w),v=z+w):v=z/(1-D);else for(v=c.length2em(g.width,H),a=0,f=Math.min(u+1,l.length);a<f;a++)v-=l[a];a=0;for(f=b.length;a<f;a++)k[b[a]]=c.length2em(y[b[a]],H,v*Q),z+=k[b[a]];if(.01<Math.abs(v-z))if(q&&v>z)for(v=(v-z)/q,a=0,f=p.length;a<f;a++)k[p[a]]+=v;else for(v/=z,b=0;b<=u;b++)k[b]*=v;if(g.equalcolumns)for(a=Math.max.apply(Math,k),b=0;b<=u;b++)k[b]=a}}var P=C;for(b=p=n[-1]?-1:0;b<=u;b++){a=0;for(f=d.length;a<f;a++)d[a][b]&&(p="mlabeledtr"===this.data[a].type?-1:0,w=this.data[a].data[b-p],w.HTMLcanStretch("Horizontal")?d[a][b].bbox=w.HTMLstretchH(n[b],k[b]).bbox:w.HTMLcanStretch("Vertical")&&(A=w.CoreMO(),p=A.symmetric,A.symmetric=!1,d[a][b].bbox=w.HTMLstretchV(n[b],h[a],m[a]).bbox,d[a][b].HH=null,A.symmetric=p),A=w.rowalign||this.data[a].rowalign||e[a],p={top:h[a]-d[a][b].bbox.h,bottom:d[a][b].bbox.d-m[a],center:(h[a]-m[a]-(d[a][b].bbox.h-d[a][b].bbox.d))/2,baseline:0,axis:0}[A]||0,A=w.columnalign||F[a][b]||I[b],c.alignBox(d[a][b],A,P+p)),a<d.length-1&&(P-=Math.max(h[a]+m[a]?M:0,m[a]+h[a+1]+J[a]));P=C}if(G)for(a=c.createBox(t),a.style.left=a.style.top=0,a.style.right=c.Em(v+2*O),a.style.display="inline-block",a.style.height="0px",c.msieRelativeWidthBug&&(a=c.createBox(a),a.style.position="relative",a.style.height="1em",a.style.width="100%",a.bbox=t.bbox),f=0,F=O,q?(p=100*(1-D)/q,z/=q):(p=100*(1-D)/(u+1),z/=u+1),b=0;b<=u;b++)c.placeBox(n[b].parentNode,0,0),n[b].style.position="relative",n[b].style.left=c.Em(F),n[b].style.width="100%",n[b].parentNode.parentNode.removeChild(n[b].parentNode),e=c.createBox(a),c.addBox(e,n[b]),n[b]=e,D=e.style,D.display="inline-block",D.left=f+"%",y[b].match(/%$/)?(w=parseFloat(y[b])*Q,0===q?(D.width=p+w+"%",f+=p+w,e=c.createBox(e),c.addBox(e,n[b].firstChild),e.style.left=0,e.style.right=c.Em(z),F-=z):(D.width=w+"%",f+=w)):"fit"===y[b]||0===q?(D.width=p+"%",e=c.createBox(e),c.addBox(e,n[b].firstChild),e.style.left=0,e.style.right=c.Em(z-k[b]),F+=k[b]-z,f+=p):(D.width=c.Em(k[b]),F+=k[b]),c.msieRelativeWidthBug&&(c.addText(e.firstChild,c.NBSP),e.firstChild.style.position="relative"),F+=l[b],"none"!==B[b]&&b<u&&-1!==b&&(e=c.createBox(a),e.style.left=f+"%",e=c.createRule(e,r,0,1.25/c.em),e.style.position="absolute",e.bbox={h:r,d:0,w:0,rw:1.25/c.em,lw:0},e.parentNode.bbox=t.bbox,c.placeBox(e,F-l[b]/2,E,!0),e.style.borderStyle=B[b]);else for(a=O,b=0;b<=u;b++)n[b].bbox.width||c.setStackWidth(n[b],k[b]),"auto"!==y[b]&&"fit"!==y[b]&&(n[b].bbox.width=k[b],n[b].bbox.isFixed=!0),c.placeBox(n[b].parentNode,a,0),a+=k[b]+l[b],"none"!==B[b]&&b<u&&-1!==b&&(e=c.createRule(t,r,0,1.25/c.em),c.addBox(t,e),e.bbox={h:r,d:0,w:0,rw:1.25/c.em,lw:0},c.placeBox(e,a-l[b]/2,E,!0),e.style.borderStyle=B[b]);t.bbox.d=-E;t.bbox.h=r+E;c.setStackWidth(t,t.bbox.w+O);var k=t.bbox.w,K;"none"!==g.frame&&(K=c.createFrame(t,r,0,k,1.25/c.em,g.frame),c.addBox(t,K),c.placeBox(K,0,E,!0),G&&(K.style.width="100%"));P=C;a=0;for(f=d.length-1;a<f;a++)p=Math.max(M,m[a]+h[a+1]+J[a]),"none"!==N[a]&&(e=c.createRule(t,1.25/c.em,0,k),c.addBox(t,e),e.bbox={h:1.25/c.em,d:0,w:k,rw:k,lw:0},c.placeBox(e,0,P-m[a]-(p-m[a]-h[a+1])/2,!0),"dashed"===N[a]||G)&&(e.style.borderTop=e.style.height+" "+N[a],e.style.height=0,e.style.width=e.style.borderLeftWidth,e.style.borderLeft="",G&&(e.style.width="100%")),P-=p;G&&(x.bbox.width=g.width,t.style.width="100%");n[-1]&&(h=t.bbox.w,d=this.getValues("indentalignfirst","indentshiftfirst","indentalign","indentshift"),d.indentalignfirst!==L.INDENTALIGN.INDENTALIGN&&(d.indentalign=d.indentalignfirst),d.indentalign===L.INDENTALIGN.AUTO&&(d.indentalign=this.displayAlign),d.indentshiftfirst!==L.INDENTSHIFT.INDENTSHIFT&&(d.indentshift=d.indentshiftfirst),"auto"===d.indentshift&&(d.indentshift=this.displayIndent),M=c.createStack(x,!1,"100%"),c.addBox(M,t),c.alignBox(t,d.indentalign,0),d.indentshift&&d.indentalign!==L.INDENTALIGN.CENTER&&(m=c.length2em(d.indentshift,H),h+=m,t.style[d.indentalign]=c.Em(m)),n[-1].parentNode.parentNode.removeChild(n[-1].parentNode),c.addBox(M,n[-1]),c.alignBox(n[-1],I[-1],0),c.msieRelativeWidthBug&&(t.style.top=n[-1].style.top=""),G&&(t.style.width=g.width,x.bbox.width="100%"),m=c.length2em(g.minlabelspacing,H),n[-1].style.marginRight=n[-1].style.marginLeft=c.Em(m),d.indentalign===L.INDENTALIGN.CENTER?h+=4*m+2*n[-1].bbox.w:d.indentalign!==I[-1]&&(h+=2*m+n[-1].bbox.w),x.style.minWidth=x.bbox.minWidth=M.style.minWidth=M.bbox.minWidth=c.Em(h));G||this.HTMLhandleSpace(x);(g=this.HTMLhandleColor(x))&&G&&(K||(K=c.createFrame(t,r,0,k,0,"none"),c.addBox(t,K),c.placeBox(K,0,E,!0),K.style.width="100%"),K.style.backgroundColor=g.style.backgroundColor,K.parentNode.insertBefore(K,K.parentNode.firstChild),g.parentNode.removeChild(g));return x},HTMLhandleSpace:function(x){x.bbox.keepPadding=!0;x.bbox.exact=!0;this.hasFrame||null!=x.bbox.width||(x.style.paddingLeft=x.style.paddingRight=c.Em(1/6));this.SUPER(arguments).HTMLhandleSpace.call(this,x)}});L.mtd.Augment({toHTML:function(c,g,G){c=this.HTMLcreateSpan(c);if(this.data[0]){var t=this.data[0].toHTML(c);null!=G?t=this.data[0].HTMLstretchV(c,g,G):null!=g&&(t=this.data[0].HTMLstretchH(c,g));c.bbox=t.bbox}this.HTMLhandleSpace(c);this.HTMLhandleColor(c);return c},HTMLstretchH:L.mbase.HTMLstretchH,HTMLstretchV:L.mbase.HTMLstretchV});MathJax.Hub.Startup.signal.Post("HTML-CSS mtable Ready");MathJax.Ajax.loadComplete(c.autoloadDir+"/mtable.js")});