|
| 1 | +/************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2017 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 | +/** |
| 19 | + * @fileoverview Implements the CHTMLmsubsup wrapper for the MmlMsubsup object |
| 20 | + * and the special cases CHTMLmsub and CHTMLmsup |
| 21 | + * |
| 22 | + * @author dpvc@mathjax.org (Davide Cervone) |
| 23 | + */ |
| 24 | + |
| 25 | +import {CHTMLWrapper} from '../Wrapper.js'; |
| 26 | +import {CHTMLscriptbase} from './scriptbase.js'; |
| 27 | +import {MmlMsubsup, MmlMsub, MmlMsup} from '../../../core/MmlTree/MmlNodes/msubsup.js'; |
| 28 | +import {MmlNode} from '../../../core/MmlTree/MmlNode.js'; |
| 29 | +import {BBox} from '../BBox.js'; |
| 30 | +import {StyleList} from '../CssStyles.js'; |
| 31 | + |
| 32 | +/*****************************************************************/ |
| 33 | +/* |
| 34 | + * The CHTMLmsub wrapper for the MmlMsub object |
| 35 | + */ |
| 36 | + |
| 37 | +export class CHTMLmsub extends CHTMLscriptbase { |
| 38 | + public static kind = MmlMsub.prototype.kind; |
| 39 | + |
| 40 | + /* |
| 41 | + * @override |
| 42 | + */ |
| 43 | + public get script() { |
| 44 | + return this.childNodes[(this.node as MmlMsub).sub]; |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + * Get the shift for the subscript |
| 49 | + * |
| 50 | + * @override |
| 51 | + */ |
| 52 | + protected getOffset(bbox: BBox, sbox: BBox) { |
| 53 | + return -this.getV(bbox, sbox); |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +/*****************************************************************/ |
| 59 | +/* |
| 60 | + * The CHTMLmsup wrapper for the MmlMsup object |
| 61 | + */ |
| 62 | + |
| 63 | +export class CHTMLmsup extends CHTMLscriptbase { |
| 64 | + public static kind = MmlMsup.prototype.kind; |
| 65 | + |
| 66 | + /* |
| 67 | + * @override |
| 68 | + */ |
| 69 | + public get script() { |
| 70 | + return this.childNodes[(this.node as MmlMsup).sup]; |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + * Get the shift for the superscript |
| 75 | + * |
| 76 | + * @override |
| 77 | + */ |
| 78 | + public getOffset(bbox: BBox, sbox: BBox) { |
| 79 | + return this.getU(bbox, sbox); |
| 80 | + } |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +/*****************************************************************/ |
| 85 | +/* |
| 86 | + * The CHTMLmsubsup wrapper for the MmlMsubsup object |
| 87 | + */ |
| 88 | + |
| 89 | +export class CHTMLmsubsup extends CHTMLscriptbase { |
| 90 | + public static kind = MmlMsubsup.prototype.kind; |
| 91 | + |
| 92 | + public static styles: StyleList = { |
| 93 | + 'mjx-script': { |
| 94 | + display: 'inline-block', |
| 95 | + 'padding-right': '.05em' // scriptspace |
| 96 | + }, |
| 97 | + 'mjx-script > *': { |
| 98 | + display: 'block' |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + /* |
| 103 | + * Cached values for the script offsets and separation (so if they are |
| 104 | + * computed in computeBBox(), they don't have to be recomputed for toCHTML()) |
| 105 | + */ |
| 106 | + protected UVQ: number[] = null; |
| 107 | + |
| 108 | + /* |
| 109 | + * @return{CHTMLWrapper} The wrapper for the subscript |
| 110 | + */ |
| 111 | + public get sub() { |
| 112 | + return this.childNodes[(this.node as MmlMsubsup).sub]; |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * @return{CHTMLWrapper} The wrapper for the superscript |
| 117 | + */ |
| 118 | + public get sup() { |
| 119 | + return this.childNodes[(this.node as MmlMsubsup).sup]; |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * @override |
| 124 | + */ |
| 125 | + public toCHTML(parent: HTMLElement) { |
| 126 | + this.chtml = this.standardCHTMLnode(parent); |
| 127 | + const [u, v, q] = this.getUVQ(this.base.getBBox(), this.sub.getBBox(), this.sup.getBBox()); |
| 128 | + const style = {'vertical-align': this.em(v)}; |
| 129 | + this.base.toCHTML(this.chtml); |
| 130 | + const stack = this.chtml.appendChild(this.html('mjx-script', {style})); |
| 131 | + this.sup.toCHTML(stack); |
| 132 | + stack.appendChild(this.html('mjx-spacer', {style: {'margin-top': this.em(q)}})); |
| 133 | + this.sub.toCHTML(stack); |
| 134 | + } |
| 135 | + |
| 136 | + /* |
| 137 | + * @override |
| 138 | + */ |
| 139 | + public computeBBox(bbox: BBox) { |
| 140 | + const basebox = this.base.getBBox(); |
| 141 | + const subbox = this.sub.getBBox(); |
| 142 | + const supbox = this.sup.getBBox(); |
| 143 | + bbox.empty(); |
| 144 | + bbox.append(basebox); |
| 145 | + const w = bbox.w; |
| 146 | + const [u, v, q] = this.getUVQ(basebox, subbox, supbox); |
| 147 | + bbox.combine(subbox, w, v); |
| 148 | + bbox.combine(supbox, w, u); |
| 149 | + bbox.w += this.font.params.scriptspace; |
| 150 | + bbox.clean(); |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + * Get the shift for the scripts and their separation (TeXBook Appendix G 18adef) |
| 155 | + * |
| 156 | + * @param{BBox} basebox The bounding box of the base |
| 157 | + * @param{BBox} subbox The bounding box of the superscript |
| 158 | + * @param{BBox} supbox The bounding box of the subscript |
| 159 | + * @return{number[]} The vertical offsets for super and subscripts, and the space between them |
| 160 | + */ |
| 161 | + protected getUVQ(basebox: BBox, subbox: BBox, supbox: BBox) { |
| 162 | + if (this.UVQ) return this.UVQ; |
| 163 | + const tex = this.font.params; |
| 164 | + const t = 3 * tex.rule_thickness; |
| 165 | + let [u, v] = (this.isCharBase() ? [0, 0] : [this.getU(basebox, supbox), |
| 166 | + Math.max(basebox.d + tex.sub_drop * subbox.rscale, tex.sub2)]); |
| 167 | + let q = (u - supbox.d * supbox.rscale) - (subbox.h * subbox.rscale - v); |
| 168 | + if (q < t) { |
| 169 | + v += t - q; |
| 170 | + const p = (4/5) * tex.x_height - (u - supbox.d * supbox.rscale); |
| 171 | + if (p > 0) { |
| 172 | + u += p; |
| 173 | + v -= p; |
| 174 | + } |
| 175 | + } |
| 176 | + u = Math.max(this.length2em(this.node.attributes.get('superscriptshift'), u), u); |
| 177 | + v = Math.max(this.length2em(this.node.attributes.get('subscriptshift'), v), v); |
| 178 | + q = (u - supbox.d * supbox.rscale) - (subbox.h * subbox.rscale - v); |
| 179 | + this.UVQ = [u, -v, q]; |
| 180 | + return this.UVQ; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments