File size: 10,222 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
// @flow
import defineFunction, {normalizeArgument} from "../defineFunction";
import buildCommon from "../buildCommon";
import mathMLTree from "../mathMLTree";
import utils from "../utils";
import stretchy from "../stretchy";
import {assertNodeType} from "../parseNode";
import {assertSpan, assertSymbolDomNode} from "../domTree";
import {makeEm} from "../units";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
import type {ParseNode, AnyParseNode} from "../parseNode";
import type {HtmlBuilderSupSub, MathMLBuilder} from "../defineFunction";
// NOTE: Unlike most `htmlBuilder`s, this one handles not only "accent", but
// also "supsub" since an accent can affect super/subscripting.
export const htmlBuilder: HtmlBuilderSupSub<"accent"> = (grp, options) => {
// Accents are handled in the TeXbook pg. 443, rule 12.
let base: AnyParseNode;
let group: ParseNode<"accent">;
let supSubGroup;
if (grp && grp.type === "supsub") {
// If our base is a character box, and we have superscripts and
// subscripts, the supsub will defer to us. In particular, we want
// to attach the superscripts and subscripts to the inner body (so
// that the position of the superscripts and subscripts won't be
// affected by the height of the accent). We accomplish this by
// sticking the base of the accent into the base of the supsub, and
// rendering that, while keeping track of where the accent is.
// The real accent group is the base of the supsub group
group = assertNodeType(grp.base, "accent");
// The character box is the base of the accent group
base = group.base;
// Stick the character box into the base of the supsub group
grp.base = base;
// Rerender the supsub group with its new base, and store that
// result.
supSubGroup = assertSpan(html.buildGroup(grp, options));
// reset original base
grp.base = group;
} else {
group = assertNodeType(grp, "accent");
base = group.base;
}
// Build the base group
const body = html.buildGroup(base, options.havingCrampedStyle());
// Does the accent need to shift for the skew of a character?
const mustShift = group.isShifty && utils.isCharacterBox(base);
// Calculate the skew of the accent. This is based on the line "If the
// nucleus is not a single character, let s = 0; otherwise set s to the
// kern amount for the nucleus followed by the \skewchar of its font."
// Note that our skew metrics are just the kern between each character
// and the skewchar.
let skew = 0;
if (mustShift) {
// If the base is a character box, then we want the skew of the
// innermost character. To do that, we find the innermost character:
const baseChar = utils.getBaseElem(base);
// Then, we render its group to get the symbol inside it
const baseGroup = html.buildGroup(baseChar, options.havingCrampedStyle());
// Finally, we pull the skew off of the symbol.
skew = assertSymbolDomNode(baseGroup).skew;
// Note that we now throw away baseGroup, because the layers we
// removed with getBaseElem might contain things like \color which
// we can't get rid of.
// TODO(emily): Find a better way to get the skew
}
const accentBelow = group.label === "\\c";
// calculate the amount of space between the body and the accent
let clearance = accentBelow
? body.height + body.depth
: Math.min(
body.height,
options.fontMetrics().xHeight);
// Build the accent
let accentBody;
if (!group.isStretchy) {
let accent;
let width: number;
if (group.label === "\\vec") {
// Before version 0.9, \vec used the combining font glyph U+20D7.
// But browsers, especially Safari, are not consistent in how they
// render combining characters when not preceded by a character.
// So now we use an SVG.
// If Safari reforms, we should consider reverting to the glyph.
accent = buildCommon.staticSvg("vec", options);
width = buildCommon.svgData.vec[1];
} else {
accent = buildCommon.makeOrd({mode: group.mode, text: group.label},
options, "textord");
accent = assertSymbolDomNode(accent);
// Remove the italic correction of the accent, because it only serves to
// shift the accent over to a place we don't want.
accent.italic = 0;
width = accent.width;
if (accentBelow) {
clearance += accent.depth;
}
}
accentBody = buildCommon.makeSpan(["accent-body"], [accent]);
// "Full" accents expand the width of the resulting symbol to be
// at least the width of the accent, and overlap directly onto the
// character without any vertical offset.
const accentFull = (group.label === "\\textcircled");
if (accentFull) {
accentBody.classes.push('accent-full');
clearance = body.height;
}
// Shift the accent over by the skew.
let left = skew;
// CSS defines `.katex .accent .accent-body:not(.accent-full) { width: 0 }`
// so that the accent doesn't contribute to the bounding box.
// We need to shift the character by its width (effectively half
// its width) to compensate.
if (!accentFull) {
left -= width / 2;
}
accentBody.style.left = makeEm(left);
// \textcircled uses the \bigcirc glyph, so it needs some
// vertical adjustment to match LaTeX.
if (group.label === "\\textcircled") {
accentBody.style.top = ".2em";
}
accentBody = buildCommon.makeVList({
positionType: "firstBaseline",
children: [
{type: "elem", elem: body},
{type: "kern", size: -clearance},
{type: "elem", elem: accentBody},
],
}, options);
} else {
accentBody = stretchy.svgSpan(group, options);
accentBody = buildCommon.makeVList({
positionType: "firstBaseline",
children: [
{type: "elem", elem: body},
{
type: "elem",
elem: accentBody,
wrapperClasses: ["svg-align"],
wrapperStyle: skew > 0
? {
width: `calc(100% - ${makeEm(2 * skew)})`,
marginLeft: makeEm(2 * skew),
}
: undefined,
},
],
}, options);
}
const accentWrap =
buildCommon.makeSpan(["mord", "accent"], [accentBody], options);
if (supSubGroup) {
// Here, we replace the "base" child of the supsub with our newly
// generated accent.
supSubGroup.children[0] = accentWrap;
// Since we don't rerun the height calculation after replacing the
// accent, we manually recalculate height.
supSubGroup.height = Math.max(accentWrap.height, supSubGroup.height);
// Accents should always be ords, even when their innards are not.
supSubGroup.classes[0] = "mord";
return supSubGroup;
} else {
return accentWrap;
}
};
const mathmlBuilder: MathMLBuilder<"accent"> = (group, options) => {
const accentNode =
group.isStretchy ?
stretchy.mathMLnode(group.label) :
new mathMLTree.MathNode("mo", [mml.makeText(group.label, group.mode)]);
const node = new mathMLTree.MathNode(
"mover",
[mml.buildGroup(group.base, options), accentNode]);
node.setAttribute("accent", "true");
return node;
};
const NON_STRETCHY_ACCENT_REGEX = new RegExp([
"\\acute", "\\grave", "\\ddot", "\\tilde", "\\bar", "\\breve",
"\\check", "\\hat", "\\vec", "\\dot", "\\mathring",
].map(accent => `\\${accent}`).join("|"));
// Accents
defineFunction({
type: "accent",
names: [
"\\acute", "\\grave", "\\ddot", "\\tilde", "\\bar", "\\breve",
"\\check", "\\hat", "\\vec", "\\dot", "\\mathring", "\\widecheck",
"\\widehat", "\\widetilde", "\\overrightarrow", "\\overleftarrow",
"\\Overrightarrow", "\\overleftrightarrow", "\\overgroup",
"\\overlinesegment", "\\overleftharpoon", "\\overrightharpoon",
],
props: {
numArgs: 1,
},
handler: (context, args) => {
const base = normalizeArgument(args[0]);
const isStretchy = !NON_STRETCHY_ACCENT_REGEX.test(context.funcName);
const isShifty = !isStretchy ||
context.funcName === "\\widehat" ||
context.funcName === "\\widetilde" ||
context.funcName === "\\widecheck";
return {
type: "accent",
mode: context.parser.mode,
label: context.funcName,
isStretchy: isStretchy,
isShifty: isShifty,
base: base,
};
},
htmlBuilder,
mathmlBuilder,
});
// Text-mode accents
defineFunction({
type: "accent",
names: [
"\\'", "\\`", "\\^", "\\~", "\\=", "\\u", "\\.", '\\"',
"\\c", "\\r", "\\H", "\\v", "\\textcircled",
],
props: {
numArgs: 1,
allowedInText: true,
allowedInMath: true, // unless in strict mode
argTypes: ["primitive"],
},
handler: (context, args) => {
const base = args[0];
let mode = context.parser.mode;
if (mode === "math") {
context.parser.settings.reportNonstrict("mathVsTextAccents",
`LaTeX's accent ${context.funcName} works only in text mode`);
mode = "text";
}
return {
type: "accent",
mode: mode,
label: context.funcName,
isStretchy: false,
isShifty: true,
base: base,
};
},
htmlBuilder,
mathmlBuilder,
});
|