mrow.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-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 CommonMrow wrapper minin for the MmlMrow object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. import {CommonMo} from './mo.js';
  24. import {BBox} from '../../../util/BBox.js';
  25. import {DIRECTION} from '../FontData.js';
  26. /*****************************************************************/
  27. /**
  28. * The CommonMrow interface
  29. */
  30. export interface CommonMrow extends AnyWrapper {
  31. /**
  32. * Handle vertical stretching of children to match height of
  33. * other nodes in the row.
  34. */
  35. stretchChildren(): void;
  36. }
  37. /**
  38. * Shorthand for the CommonMrow constructor
  39. */
  40. export type MrowConstructor = Constructor<CommonMrow>;
  41. /*****************************************************************/
  42. /**
  43. * The CommonMrow wrapper mixin for the MmlMrow object
  44. *
  45. * @template T The Wrapper class constructor type
  46. */
  47. export function CommonMrowMixin<T extends WrapperConstructor>(Base: T): MrowConstructor & T {
  48. return class extends Base {
  49. /**
  50. * @override
  51. */
  52. get fixesPWidth() {
  53. return false;
  54. }
  55. /**
  56. * @override
  57. * @constructor
  58. */
  59. constructor(...args: any[]) {
  60. super(...args);
  61. this.stretchChildren();
  62. for (const child of this.childNodes) {
  63. if (child.bbox.pwidth) {
  64. this.bbox.pwidth = BBox.fullWidth;
  65. break;
  66. }
  67. }
  68. }
  69. /**
  70. * Handle vertical stretching of children to match height of
  71. * other nodes in the row.
  72. */
  73. public stretchChildren() {
  74. let stretchy: AnyWrapper[] = [];
  75. //
  76. // Locate and count the stretchy children
  77. //
  78. for (const child of this.childNodes) {
  79. if (child.canStretch(DIRECTION.Vertical)) {
  80. stretchy.push(child);
  81. }
  82. }
  83. let count = stretchy.length;
  84. let nodeCount = this.childNodes.length;
  85. if (count && nodeCount > 1) {
  86. let H = 0, D = 0;
  87. //
  88. // If all the children are stretchy, find the largest one,
  89. // otherwise, find the height and depth of the non-stretchy
  90. // children.
  91. //
  92. let all = (count > 1 && count === nodeCount);
  93. for (const child of this.childNodes) {
  94. const noStretch = (child.stretch.dir === DIRECTION.None);
  95. if (all || noStretch) {
  96. let {h, d, rscale} = child.getOuterBBox(noStretch);
  97. h *= rscale;
  98. d *= rscale;
  99. if (h > H) H = h;
  100. if (d > D) D = d;
  101. }
  102. }
  103. //
  104. // Stretch the stretchable children
  105. //
  106. for (const child of stretchy) {
  107. (child.coreMO() as CommonMo).getStretchedVariant([H, D]);
  108. }
  109. }
  110. }
  111. };
  112. }
  113. /*****************************************************************/
  114. /*****************************************************************/
  115. /**
  116. * The CommonInferredMrow interface
  117. */
  118. export interface CommonInferredMrow extends CommonMrow {
  119. }
  120. /**
  121. * Shorthand for the CommonInferredMrow constructor
  122. */
  123. export type InferredMrowConstructor = Constructor<CommonInferredMrow>;
  124. /*****************************************************************/
  125. /**
  126. * The CommonInferredMrow wrapper mixin for the MmlInferredMrow object
  127. *
  128. * @template T The Wrapper class constructor type
  129. */
  130. export function CommonInferredMrowMixin<T extends MrowConstructor>(Base: T): InferredMrowConstructor & T {
  131. return class extends Base {
  132. /**
  133. * Since inferred rows don't produce a container span, we can't
  134. * set a font-size for it, so we inherit the parent scale
  135. *
  136. * @override
  137. */
  138. public getScale() {
  139. this.bbox.scale = this.parent.bbox.scale;
  140. this.bbox.rscale = 1;
  141. }
  142. };
  143. }