mtable.js 7.2 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/SVG/autoload/mtable.js
  6. *
  7. * Implements the SVG output for <mtable> elements.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2011-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("SVG Jax Ready",function(){var H=MathJax.ElementJax.mml,h=MathJax.OutputJax.SVG,F=h.BBOX;H.mtable.Augment({toSVG:function(f){this.SVGgetStyles();f=this.SVG();if(0===this.data.length)return this.SVGsaveData(f),f;var d=this.getValues("columnalign","rowalign","columnspacing","rowspacing","columnwidth","equalcolumns","equalrows","columnlines","rowlines","frame","framespacing","align","useHeight","width","side","minlabelspacing");d.width.match(/%$/)&&(f.width=d.width=Math.floor(h.cwidth*parseFloat(d.width)/100)+"px");var x=this.SVGgetScale(),v=this.SVGgetMu(f),c=[],l=[],n=[],k=[],C=[],a,b,t=-1,e,A,m,y,g,q,J=h.FONTDATA.baselineskip*x*d.useHeight,z=h.FONTDATA.lineH*x,D=h.FONTDATA.lineD*x;a=0;for(e=this.data.length;a<e;a++)for(y=this.data[a],m="mlabeledtr"===y.type?-1:0,k[a]=[],c[a]=l[a]=0,b=m,A=y.data.length+m;b<A;b++){null==n[b]&&(b>t&&(t=b),C[b]=F.G(),n[b]=-h.BIGDIMEN);g=y.data[b-m];k[a][b]=g.toSVG();if(g.isEmbellished()){q=g.CoreMO();var p=q.Get("minsize",!0);if(p)if(q.SVGcanStretch("Vertical")){if(g=q.SVGdata.h+q.SVGdata.d)p=h.length2em(p,v,g),p*q.SVGdata.h/g>c[b]&&(c[b]=p*q.SVGdata.h/g),p*q.SVGdata.d/g>l[b]&&(l[b]=p*q.SVGdata.d/g)}else q.SVGcanStretch("Horizontal")&&(p=h.length2em(p,v,q.SVGdata.w),p>n[b]&&(n[b]=p))}k[a][b].h>c[a]&&(c[a]=k[a][b].h);k[a][b].d>l[a]&&(l[a]=k[a][b].d);k[a][b].w>n[b]&&(n[b]=k[a][b].w)}c[0]+l[0]&&(c[0]=Math.max(c[0],z));c[k.length-1]+l[k.length-1]&&(l[k.length-1]=Math.max(l[k.length-1],D));var w=MathJax.Hub.SplitList,p=w(d.columnspacing),z=w(d.rowspacing);A=w(d.columnalign);var u=w(d.rowalign),I=w(d.columnlines),D=w(d.rowlines);b=w(d.columnwidth);var G=[];a=0;for(e=p.length;a<e;a++)p[a]=h.length2em(p[a],v);a=0;for(e=z.length;a<e;a++)z[a]=h.length2em(z[a],v);for(;p.length<t;)p.push(p[p.length-1]);for(;A.length<=t;)A.push(A[A.length-1]);for(;I.length<t;)I.push(I[I.length-1]);for(;b.length<=t;)b.push(b[b.length-1]);for(;z.length<k.length;)z.push(z[z.length-1]);for(;u.length<=k.length;)u.push(u[u.length-1]);for(;D.length<k.length;)D.push(D[D.length-1]);C[-1]&&(A[-1]="l"===d.side.substr(0,1)?"left":"right",p[-1]=-n[-1]);a=0;for(e=k.length;a<e;a++)if(y=this.data[a],G[a]=[],y.rowalign&&(u[a]=y.rowalign),y.columnalign)for(G[a]=w(y.columnalign);G[a].length<=t;)G[a].push(G[a][G[a].length-1]);if(d.equalrows){y=Math.max.apply(Math,c);var E=Math.max.apply(Math,l);a=0;for(e=k.length;a<e;a++)m=(y+E-(c[a]+l[a]))/2,c[a]+=m,l[a]+=m}g=c[0]+l[k.length-1];a=0;for(e=k.length-1;a<e;a++)g+=Math.max(c[a]+l[a]?J:0,l[a]+c[a+1]+z[a]);a=y=0;E=g;if("none"!==d.frame||(d.columnlines+d.rowlines).match(/solid|dashed/))a=w(d.framespacing),2!=a.length&&(a=w(this.defaults.framespacing)),y=h.length2em(a[0],v),a=h.length2em(a[1],v),E=g+2*a;e="";"string"!==typeof d.align&&(d.align=String(d.align));d.align.match(/(top|bottom|center|baseline|axis)( +(-?\d+))?/)?(e=RegExp.$3,d.align=RegExp.$1):d.align=this.defaults.align;if(""!==e)for(e=parseInt(e),0>e&&(e=k.length+1+e),1>e?e=1:e>k.length&&(e=k.length),w=0,x=-(g+a)+c[0],a=0,--e;a<e;a++)m=Math.max(c[a]+l[a]?J:0,l[a]+c[a+1]+z[a]),w+=m,x+=m;else w={top:-(c[0]+a),bottom:g+a-c[0],center:g/2-c[0],baseline:g/2-c[0],axis:g/2+h.TeX.axis_height*x-c[0]}[d.align],x={top:-(g+2*a),bottom:0,center:-(g/2+a),baseline:-(g/2+a),axis:h.TeX.axis_height*x-g/2-a}[d.align];var r,K=m=r=0,L=0;g=0;q=[];var B=[],M=1;if(d.equalcolumns&&"auto"!==d.width){r=h.length2em(d.width,v);a=0;for(e=Math.min(t+1,p.length);a<e;a++)r-=p[a];r/=t+1;a=0;for(e=Math.min(t+1,b.length);a<e;a++)n[a]=r}else{a=0;for(e=Math.min(t+1,b.length);a<e;a++)"auto"===b[a]?m+=n[a]:"fit"===b[a]?(B[g]=a,g++,m+=n[a]):b[a].match(/%$/)?(q[L]=a,L++,K+=n[a],r+=h.length2em(b[a],v,1)):(n[a]=h.length2em(b[a],v),m+=n[a]);if("auto"===d.width).98<r?(M=K/(m+K),r=m+K):r=m/(1-r);else for(r=h.length2em(d.width,v),a=0,e=Math.min(t+1,p.length);a<e;a++)r-=p[a];a=0;for(e=q.length;a<e;a++)n[q[a]]=h.length2em(b[q[a]],v,r*M),m+=n[q[a]];if(.01<Math.abs(r-m))if(g&&r>m)for(r=(r-m)/g,a=0,e=B.length;a<e;a++)n[B[a]]+=r;else for(r/=m,b=0;b<=t;b++)n[b]*=r;if(d.equalcolumns)for(a=Math.max.apply(Math,n),b=0;b<=t;b++)n[b]=a}B=w;for(b=m=C[-1]?-1:0;b<=t;b++){C[b].w=n[b];a=0;for(e=k.length;a<e;a++)k[a][b]&&(m="mlabeledtr"===this.data[a].type?-1:0,g=this.data[a].data[b-m],g.SVGcanStretch("Horizontal")?k[a][b]=g.SVGstretchH(n[b]):g.SVGcanStretch("Vertical")&&(q=g.CoreMO(),m=q.symmetric,q.symmetric=!1,k[a][b]=g.SVGstretchV(c[a],l[a]),q.symmetric=m),q=g.rowalign||this.data[a].rowalign||u[a],m={top:c[a]-k[a][b].h,bottom:k[a][b].d-l[a],center:(c[a]-l[a]-(k[a][b].h-k[a][b].d))/2,baseline:0,axis:0}[q]||0,q=g.columnalign||G[a][b]||A[b],C[b].Align(k[a][b],q,0,B+m)),a<k.length-1&&(B-=Math.max(c[a]+l[a]?J:0,l[a]+c[a+1]+z[a]));B=w}u=1.5*h.em;a=y-u/2;for(b=0;b<=t;b++)f.Add(C[b],a,0),a+=n[b]+p[b],"none"!==I[b]&&b<t&&-1!==b&&f.Add(F.VLINE(E,u,I[b]),a-p[b]/2,x);f.w+=y;f.d=-x;f.h=E+x;n=f.w;"none"!==d.frame&&(f.Add(F.HLINE(n,u,d.frame),0,x+E-u),f.Add(F.HLINE(n,u,d.frame),0,x),f.Add(F.VLINE(E,u,d.frame),0,x),f.Add(F.VLINE(E,u,d.frame),n-u,x));B=w-u/2;a=0;for(e=k.length-1;a<e;a++)m=Math.max(J,l[a]+c[a+1]+z[a]),"none"!==D[a]&&f.Add(F.HLINE(n,u,D[a]),0,B-l[a]-(m-l[a]-c[a+1])/2),B-=m;f.Clean();this.SVGhandleSpace(f);this.SVGhandleColor(f);C[-1]&&(c=this.getValues("indentalignfirst","indentshiftfirst","indentalign","indentshift"),c.indentalignfirst!==H.INDENTALIGN.INDENTALIGN&&(c.indentalign=c.indentalignfirst),c.indentalign===H.INDENTALIGN.AUTO&&(c.indentalign=this.displayAlign),c.indentshiftfirst!==H.INDENTSHIFT.INDENTSHIFT&&(c.indentshift=c.indentshiftfirst),"auto"===c.indentshift&&(c.indentshift=this.displayIndent),l=c.indentshift?h.length2em(c.indentshift,v):0,d=h.length2em(d.minlabelspacing,v),v=f,f=this.SVG(),c.indentalign===H.INDENTALIGN.CENTER?(f.w=f.r=h.length2em(h.cwidth+"px"),l=0,f.hasIndent=!0):A[-1]!==c.indentalign?(f.w=f.r=h.length2em(h.cwidth+"px")-l-d,l=d=0):(f.w=f.r=v.w+l,f.hasIndent=!0),f.Align(v,c.indentalign,l,0),f.Align(C[-1],A[-1],d,0));this.SVGsaveData(f);return f},SVGhandleSpace:function(f){this.hasFrame||f.width||(f.x=f.X=167);this.SUPER(arguments).SVGhandleSpace.call(this,f)}});H.mtd.Augment({toSVG:function(f,d){var h=this.svg=this.SVG();this.data[0]&&(h.Add(this.SVGdataStretched(0,f,d)),h.Clean());this.SVGhandleColor(h);this.SVGsaveData(h);return h}});MathJax.Hub.Startup.signal.Post("SVG mtable Ready");MathJax.Ajax.loadComplete(h.autoloadDir+"/mtable.js")});