mtr.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the SVGmtr wrapper for the MmlMtr object
  19. * and SVGmlabeledtr for MmlMlabeledtr
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. import {SVGWrapper, SVGConstructor, Constructor} from '../Wrapper.js';
  24. import {CommonMtrMixin} from '../../common/Wrappers/mtr.js';
  25. import {CommonMlabeledtrMixin} from '../../common/Wrappers/mtr.js';
  26. import {SVGmtd} from './mtd.js';
  27. import {MmlMtr, MmlMlabeledtr} from '../../../core/MmlTree/MmlNodes/mtr.js';
  28. /**
  29. * The data needed for placeCell()
  30. */
  31. export type SizeData = {
  32. x: number,
  33. y: number,
  34. w: number,
  35. lSpace: number,
  36. rSpace: number,
  37. lLine: number,
  38. rLine: number
  39. };
  40. /*****************************************************************/
  41. /**
  42. * The SVGmtr wrapper for the MmlMtr object
  43. *
  44. * @template N The HTMLElement node class
  45. * @template T The Text node class
  46. * @template D The Document class
  47. */
  48. export class SVGmtr<N, T, D> extends
  49. CommonMtrMixin<SVGmtd<any, any, any>, SVGConstructor<any, any, any>>(SVGWrapper) {
  50. /**
  51. * The mtr wrapper
  52. */
  53. public static kind = MmlMtr.prototype.kind;
  54. /**
  55. * The height of the row
  56. */
  57. public H: number;
  58. /**
  59. * The depth of the row
  60. */
  61. public D: number;
  62. /**
  63. * The space above the row
  64. */
  65. public tSpace: number;
  66. /**
  67. * The space below the row
  68. */
  69. public bSpace: number;
  70. /**
  71. * The line space above the row
  72. */
  73. public tLine: number;
  74. /**
  75. * The line space below the row
  76. */
  77. public bLine: number;
  78. /**
  79. * @override
  80. */
  81. public toSVG(parent: N) {
  82. const svg = this.standardSVGnode(parent);
  83. this.placeCells(svg);
  84. this.placeColor();
  85. }
  86. /**
  87. * Set the location of the cell contents in the row and expand the cell background colors
  88. *
  89. * @param {N} svg The container for the table
  90. */
  91. protected placeCells(svg: N) {
  92. const cSpace = this.parent.getColumnHalfSpacing();
  93. const cLines = [this.parent.fLine, ...this.parent.cLines, this.parent.fLine];
  94. const cWidth = this.parent.getComputedWidths();
  95. const scale = 1 / this.getBBox().rscale;
  96. let x = cLines[0];
  97. for (let i = 0; i < this.numCells; i++) {
  98. const child = this.getChild(i);
  99. child.toSVG(svg);
  100. x += this.placeCell(child, {
  101. x: x, y: 0, lSpace: cSpace[i] * scale, rSpace: cSpace[i + 1] * scale, w: cWidth[i] * scale,
  102. lLine: cLines[i] * scale, rLine: cLines[i + 1] * scale
  103. });
  104. }
  105. }
  106. /**
  107. * @param {SVGmtd} cell The cell to place
  108. * @param {SizeData} sizes The positioning information
  109. * @return {number} The new x position
  110. */
  111. public placeCell(cell: SVGmtd<N, T, D>, sizes: SizeData): number {
  112. const {x, y, lSpace, w, rSpace, lLine, rLine} = sizes;
  113. const scale = 1 / this.getBBox().rscale;
  114. const [h, d] = [this.H * scale, this.D * scale];
  115. const [t, b] = [this.tSpace * scale, this.bSpace * scale];
  116. const [dx, dy] = cell.placeCell(x + lSpace, y, w, h, d);
  117. const W = lSpace + w + rSpace;
  118. cell.placeColor(-(dx + lSpace + lLine / 2), -(d + b + dy), W + (lLine + rLine) / 2, h + d + t + b);
  119. return W + rLine;
  120. }
  121. /**
  122. * Expand the backgound color to fill the entire row
  123. */
  124. protected placeColor() {
  125. const scale = 1 / this.getBBox().rscale;
  126. const adaptor = this.adaptor;
  127. const child = this.firstChild();
  128. if (child && adaptor.kind(child) === 'rect' && adaptor.getAttribute(child, 'data-bgcolor')) {
  129. const [TL, BL] = [(this.tLine / 2) * scale, (this.bLine / 2) * scale];
  130. const [TS, BS] = [this.tSpace * scale, this.bSpace * scale];
  131. const [H, D] = [this.H * scale, this.D * scale];
  132. adaptor.setAttribute(child, 'y', this.fixed(-(D + BS + BL)));
  133. adaptor.setAttribute(child, 'width', this.fixed(this.parent.getWidth() * scale));
  134. adaptor.setAttribute(child, 'height', this.fixed(TL + TS + H + D + BS + BL));
  135. }
  136. }
  137. }
  138. /*****************************************************************/
  139. /**
  140. * The SVGlabeledmtr wrapper for the MmlMlabeledtr object
  141. *
  142. * @template N The HTMLElement node class
  143. * @template T The Text node class
  144. * @template D The Document class
  145. */
  146. // @ts-ignore
  147. export class SVGmlabeledtr<N, T, D> extends
  148. CommonMlabeledtrMixin<SVGmtd<any, any, any>, Constructor<SVGmtr<any, any, any>>>(SVGmtr) {
  149. /**
  150. * The mlabeledtr wrapper
  151. */
  152. public static kind = MmlMlabeledtr.prototype.kind;
  153. /**
  154. * @override
  155. */
  156. public toSVG(parent: N) {
  157. super.toSVG(parent);
  158. const child = this.childNodes[0];
  159. if (child) {
  160. child.toSVG(this.parent.labels);
  161. }
  162. }
  163. }