mtable.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. */
  25. MathJax.Hub.Register.StartupHook("SVG Jax Ready",function () {
  26. var VERSION = "2.2";
  27. var MML = MathJax.ElementJax.mml,
  28. SVG = MathJax.OutputJax.SVG,
  29. BBOX = SVG.BBOX;
  30. MML.mtable.Augment({
  31. toSVG: function (span) {
  32. this.SVGgetStyles();
  33. var svg = this.SVG();
  34. if (this.data.length === 0) {this.SVGsaveData(svg);return svg}
  35. var values = this.getValues("columnalign","rowalign","columnspacing","rowspacing",
  36. "columnwidth","equalcolumns","equalrows",
  37. "columnlines","rowlines","frame","framespacing",
  38. "align","useHeight","width","side","minlabelspacing");
  39. // Handle relative width as fixed width in relation to container
  40. if (values.width.match(/%$/))
  41. {svg.width = values.width = Math.floor(SVG.cwidth*parseFloat(values.width)/100)+"px"}
  42. var scale = this.SVGgetScale(), mu = this.SVGgetMu(svg);
  43. var LABEL = -1;
  44. var H = [], D = [], W = [], A = [], C = [], i, j, J = -1, m, M, s, row, cell, mo;
  45. var LHD = SVG.FONTDATA.baselineskip * scale * values.useHeight, HD,
  46. LH = SVG.FONTDATA.lineH * scale, LD = SVG.FONTDATA.lineD * scale;
  47. //
  48. // Create cells and measure columns and rows
  49. //
  50. for (i = 0, m = this.data.length; i < m; i++) {
  51. row = this.data[i]; s = (row.type === "mlabeledtr" ? LABEL : 0);
  52. A[i] = []; H[i] = D[i] = 0;
  53. for (j = s, M = row.data.length + s; j < M; j++) {
  54. if (W[j] == null) {
  55. if (j > J) {J = j}
  56. C[j] = BBOX.G();
  57. W[j] = -SVG.BIGDIMEN;
  58. }
  59. cell = row.data[j-s];
  60. A[i][j] = cell.toSVG();
  61. // if (row.data[j-s].isMultiline) {A[i][j].style.width = "100%"}
  62. if (cell.isEmbellished()) {
  63. mo = cell.CoreMO();
  64. var min = mo.Get("minsize",true);
  65. if (min) {
  66. if (mo.SVGcanStretch("Vertical")) {
  67. HD = mo.SVGdata.h + mo.SVGdata.d;
  68. if (HD) {
  69. min = SVG.length2em(min,mu,HD);
  70. if (min*mo.SVGdata.h/HD > H[j]) {H[j] = min*mo.SVGdata.h/HD}
  71. if (min*mo.SVGdata.d/HD > D[j]) {D[j] = min*mo.SVGdata.d/HD}
  72. }
  73. } else if (mo.SVGcanStretch("Horizontal")) {
  74. min = SVG.length2em(min,mu,mo.SVGdata.w);
  75. if (min > W[j]) {W[j] = min}
  76. }
  77. }
  78. }
  79. if (A[i][j].h > H[i]) {H[i] = A[i][j].h}
  80. if (A[i][j].d > D[i]) {D[i] = A[i][j].d}
  81. if (A[i][j].w > W[j]) {W[j] = A[i][j].w}
  82. }
  83. }
  84. if (H[0]+D[0]) {H[0] = Math.max(H[0],LH)}
  85. if (H[A.length-1]+D[A.length-1]) {D[A.length-1] = Math.max(D[A.length-1],LD)}
  86. //
  87. // Determine spacing and alignment
  88. //
  89. var SPLIT = MathJax.Hub.SplitList;
  90. var CSPACE = SPLIT(values.columnspacing),
  91. RSPACE = SPLIT(values.rowspacing),
  92. CALIGN = SPLIT(values.columnalign),
  93. RALIGN = SPLIT(values.rowalign),
  94. CLINES = SPLIT(values.columnlines),
  95. RLINES = SPLIT(values.rowlines),
  96. CWIDTH = SPLIT(values.columnwidth),
  97. RCALIGN = [];
  98. for (i = 0, m = CSPACE.length; i < m; i++) {CSPACE[i] = SVG.length2em(CSPACE[i],mu)}
  99. for (i = 0, m = RSPACE.length; i < m; i++) {RSPACE[i] = SVG.length2em(RSPACE[i],mu)}
  100. while (CSPACE.length < J) {CSPACE.push(CSPACE[CSPACE.length-1])}
  101. while (CALIGN.length <= J) {CALIGN.push(CALIGN[CALIGN.length-1])}
  102. while (CLINES.length < J) {CLINES.push(CLINES[CLINES.length-1])}
  103. while (CWIDTH.length <= J) {CWIDTH.push(CWIDTH[CWIDTH.length-1])}
  104. while (RSPACE.length < A.length) {RSPACE.push(RSPACE[RSPACE.length-1])}
  105. while (RALIGN.length <= A.length) {RALIGN.push(RALIGN[RALIGN.length-1])}
  106. while (RLINES.length < A.length) {RLINES.push(RLINES[RLINES.length-1])}
  107. if (C[LABEL]) {
  108. CALIGN[LABEL] = (values.side.substr(0,1) === "l" ? "left" : "right");
  109. CSPACE[LABEL] = -W[LABEL];
  110. }
  111. //
  112. // Override row data
  113. //
  114. for (i = 0, m = A.length; i < m; i++) {
  115. row = this.data[i]; RCALIGN[i] = [];
  116. if (row.rowalign) {RALIGN[i] = row.rowalign}
  117. if (row.columnalign) {
  118. RCALIGN[i] = SPLIT(row.columnalign);
  119. while (RCALIGN[i].length <= J) {RCALIGN[i].push(RCALIGN[i][RCALIGN[i].length-1])}
  120. }
  121. }
  122. //
  123. // Handle equal heights
  124. //
  125. if (values.equalrows) {
  126. // FIXME: should really be based on row align (below is for baseline)
  127. var Hm = Math.max.apply(Math,H), Dm = Math.max.apply(Math,D);
  128. for (i = 0, m = A.length; i < m; i++)
  129. {s = ((Hm + Dm) - (H[i] + D[i])) / 2; H[i] += s; D[i] += s}
  130. }
  131. // FIXME: do background colors for entire cell (include half the intercolumn space?)
  132. //
  133. // Determine array total height
  134. //
  135. HD = H[0] + D[A.length-1];
  136. for (i = 0, m = A.length-1; i < m; i++)
  137. {HD += Math.max((H[i]+D[i] ? LHD : 0),D[i]+H[i+1]+RSPACE[i])}
  138. //
  139. // Determine frame and line sizes
  140. //
  141. var fx = 0, fy = 0, fW, fH = HD;
  142. if (values.frame !== "none" ||
  143. (values.columnlines+values.rowlines).match(/solid|dashed/)) {
  144. var frameSpacing = SPLIT(values.framespacing);
  145. if (frameSpacing.length != 2) {
  146. // invalid attribute value: use the default.
  147. frameSpacing = SPLIT(this.defaults.framespacing);
  148. }
  149. fx = SVG.length2em(frameSpacing[0],mu);
  150. fy = SVG.length2em(frameSpacing[1],mu);
  151. fH = HD + 2*fy; // fW waits until svg.w is determined
  152. }
  153. //
  154. // Compute alignment
  155. //
  156. var Y, fY, n = "";
  157. if (typeof(values.align) !== "string") {values.align = String(values.align)}
  158. if (values.align.match(/(top|bottom|center|baseline|axis)( +(-?\d+))?/))
  159. {n = RegExp.$3; values.align = RegExp.$1} else {values.align = this.defaults.align}
  160. if (n !== "") {
  161. //
  162. // Find the height of the given row
  163. //
  164. n = parseInt(n);
  165. if (n < 0) {n = A.length + 1 + n}
  166. if (n < 1) {n = 1} else if (n > A.length) {n = A.length}
  167. Y = 0; fY = -(HD + fy) + H[0];
  168. for (i = 0, m = n-1; i < m; i++) {
  169. // FIXME: Should handle values.align for final row
  170. var dY = Math.max((H[i]+D[i] ? LHD : 0),D[i]+H[i+1]+RSPACE[i]);
  171. Y += dY; fY += dY;
  172. }
  173. } else {
  174. Y = ({
  175. top: -(H[0] + fy),
  176. bottom: HD + fy - H[0],
  177. center: HD/2 - H[0],
  178. baseline: HD/2 - H[0],
  179. axis: HD/2 + SVG.TeX.axis_height*scale - H[0]
  180. })[values.align];
  181. fY = ({
  182. top: -(HD + 2*fy),
  183. bottom: 0,
  184. center: -(HD/2 + fy),
  185. baseline: -(HD/2 + fy),
  186. axis: SVG.TeX.axis_height*scale - HD/2 - fy
  187. })[values.align];
  188. }
  189. var WW, WP = 0, Wt = 0, Wp = 0, p = 0, f = 0, P = [], F = [], Wf = 1;
  190. //
  191. if (values.equalcolumns && values.width !== "auto") {
  192. //
  193. // Handle equalcolumns for percent-width and fixed-width tables
  194. //
  195. // Get total width minus column spacing
  196. WW = SVG.length2em(values.width,mu);
  197. for (i = 0, m = Math.min(J+1,CSPACE.length); i < m; i++) {WW -= CSPACE[i]}
  198. // Determine individual column widths
  199. WW /= J+1;
  200. for (i = 0, m = Math.min(J+1,CWIDTH.length); i < m; i++) {W[i] = WW}
  201. } else {
  202. //
  203. // Get column widths for fit and percentage columns
  204. //
  205. // Calculate the natural widths and percentage widths,
  206. // while keeping track of the fit and percentage columns
  207. for(i = 0, m = Math.min(J+1,CWIDTH.length); i < m; i++) {
  208. if (CWIDTH[i] === "auto") {Wt += W[i]}
  209. else if (CWIDTH[i] === "fit") {F[f] = i; f++; Wt += W[i]}
  210. else if (CWIDTH[i].match(/%$/))
  211. {P[p] = i; p++; Wp += W[i]; WP += SVG.length2em(CWIDTH[i],mu,1)}
  212. else {W[i] = SVG.length2em(CWIDTH[i],mu); Wt += W[i]}
  213. }
  214. // Get the full width (excluding inter-column spacing)
  215. if (values.width === "auto") {
  216. if (WP > .98) {Wf = Wp/(Wt+Wp); WW = Wt + Wp} else {WW = Wt / (1-WP)}
  217. } else {
  218. WW = SVG.length2em(values.width,mu);
  219. for (i = 0, m = Math.min(J+1,CSPACE.length); i < m; i++) {WW -= CSPACE[i]}
  220. }
  221. // Determine the relative column widths
  222. for (i = 0, m = P.length; i < m; i++) {
  223. W[P[i]] = SVG.length2em(CWIDTH[P[i]],mu,WW*Wf); Wt += W[P[i]];
  224. }
  225. // Stretch fit columns, if any, otherwise stretch (or shrink) everything
  226. if (Math.abs(WW - Wt) > .01) {
  227. if (f && WW > Wt) {
  228. WW = (WW - Wt) / f; for (i = 0, m = F.length; i < m; i++) {W[F[i]] += WW}
  229. } else {WW = WW/Wt; for (j = 0; j <= J; j++) {W[j] *= WW}}
  230. }
  231. //
  232. // Handle equal columns
  233. //
  234. if (values.equalcolumns) {
  235. var Wm = Math.max.apply(Math,W);
  236. for (j = 0; j <= J; j++) {W[j] = Wm}
  237. }
  238. }
  239. //
  240. // Lay out array columns
  241. //
  242. var y = Y, dy, align; s = (C[LABEL] ? LABEL : 0);
  243. for (j = s; j <= J; j++) {
  244. C[j].w = W[j];
  245. for (i = 0, m = A.length; i < m; i++) {
  246. if (A[i][j]) {
  247. s = (this.data[i].type === "mlabeledtr" ? LABEL : 0);
  248. cell = this.data[i].data[j-s];
  249. if (cell.SVGcanStretch("Horizontal")) {
  250. A[i][j] = cell.SVGstretchH(W[j]);
  251. } else if (cell.SVGcanStretch("Vertical")) {
  252. mo = cell.CoreMO();
  253. var symmetric = mo.symmetric; mo.symmetric = false;
  254. A[i][j] = cell.SVGstretchV(H[i],D[i]);
  255. mo.symmetric = symmetric;
  256. }
  257. align = cell.rowalign||this.data[i].rowalign||RALIGN[i];
  258. dy = ({top: H[i] - A[i][j].h,
  259. bottom: A[i][j].d - D[i],
  260. center: ((H[i]-D[i]) - (A[i][j].h-A[i][j].d))/2,
  261. baseline: 0, axis: 0})[align] || 0; // FIXME: handle axis better?
  262. align = (cell.columnalign||RCALIGN[i][j]||CALIGN[j])
  263. C[j].Align(A[i][j],align,0,y+dy);
  264. }
  265. if (i < A.length-1) {y -= Math.max((H[i]+D[i] ? LHD : 0),D[i]+H[i+1]+RSPACE[i])}
  266. }
  267. y = Y;
  268. }
  269. //
  270. // Place the columns and add column lines
  271. //
  272. var lw = 1.5*SVG.em;
  273. var x = fx - lw/2;
  274. for (j = 0; j <= J; j++) {
  275. svg.Add(C[j],x,0); x += W[j] + CSPACE[j];
  276. if (CLINES[j] !== "none" && j < J && j !== LABEL)
  277. {svg.Add(BBOX.VLINE(fH,lw,CLINES[j]),x-CSPACE[j]/2,fY)}
  278. }
  279. svg.w += fx; svg.d = -fY; svg.h = fH+fY;
  280. fW = svg.w;
  281. //
  282. // Add frame
  283. //
  284. if (values.frame !== "none") {
  285. svg.Add(BBOX.HLINE(fW,lw,values.frame),0,fY+fH-lw);
  286. svg.Add(BBOX.HLINE(fW,lw,values.frame),0,fY);
  287. svg.Add(BBOX.VLINE(fH,lw,values.frame),0,fY);
  288. svg.Add(BBOX.VLINE(fH,lw,values.frame),fW-lw,fY);
  289. }
  290. //
  291. // Add row lines
  292. //
  293. y = Y - lw/2;
  294. for (i = 0, m = A.length-1; i < m; i++) {
  295. dy = Math.max(LHD,D[i]+H[i+1]+RSPACE[i]);
  296. if (RLINES[i] !== "none")
  297. {svg.Add(BBOX.HLINE(fW,lw,RLINES[i]),0,y-D[i]-(dy-D[i]-H[i+1])/2)}
  298. y -= dy;
  299. }
  300. //
  301. // Finish the table
  302. //
  303. svg.Clean();
  304. this.SVGhandleSpace(svg);
  305. this.SVGhandleColor(svg);
  306. //
  307. // Place the labels, if any
  308. //
  309. if (C[LABEL]) {
  310. var indent = this.getValues("indentalignfirst","indentshiftfirst","indentalign","indentshift");
  311. if (indent.indentalignfirst !== MML.INDENTALIGN.INDENTALIGN) {indent.indentalign = indent.indentalignfirst}
  312. if (indent.indentalign === MML.INDENTALIGN.AUTO) {indent.indentalign = this.displayAlign}
  313. if (indent.indentshiftfirst !== MML.INDENTSHIFT.INDENTSHIFT) {indent.indentshift = indent.indentshiftfirst}
  314. if (indent.indentshift === "auto") {indent.indentshift = this.displayIndent}
  315. var shift = (indent.indentshift ? SVG.length2em(indent.indentshift,mu) : 0);
  316. var labelshift = SVG.length2em(values.minlabelspacing,mu);
  317. var eqn = svg; svg = this.SVG();
  318. if (indent.indentalign === MML.INDENTALIGN.CENTER) {
  319. svg.w = svg.r = SVG.length2em(SVG.cwidth+"px"); shift = 0; svg.hasIndent = true;
  320. } else if (CALIGN[LABEL] !== indent.indentalign) {
  321. svg.w = svg.r = SVG.length2em(SVG.cwidth+"px") - shift - labelshift;
  322. shift = labelshift = 0;
  323. } else {
  324. svg.w = svg.r = eqn.w + shift;
  325. svg.hasIndent = true;
  326. }
  327. svg.Align(eqn,indent.indentalign,shift,0);
  328. svg.Align(C[LABEL],CALIGN[LABEL],labelshift,0);
  329. }
  330. this.SVGsaveData(svg);
  331. return svg;
  332. },
  333. SVGhandleSpace: function (svg) {
  334. if (!this.hasFrame && !svg.width) {svg.x = svg.X = 167}
  335. this.SUPER(arguments).SVGhandleSpace.call(this,svg);
  336. }
  337. });
  338. MML.mtd.Augment({
  339. toSVG: function (HW,D) {
  340. var svg = this.svg = this.SVG();
  341. if (this.data[0]) {
  342. svg.Add(this.SVGdataStretched(0,HW,D));
  343. svg.Clean();
  344. }
  345. this.SVGhandleColor(svg);
  346. this.SVGsaveData(svg);
  347. return svg;
  348. }
  349. });
  350. MathJax.Hub.Startup.signal.Post("SVG mtable Ready");
  351. MathJax.Ajax.loadComplete(SVG.autoloadDir+"/mtable.js");
  352. });