Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions apps/mobile/src/MathLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const MONO = { ascent: 1.02, descent: 0.3 };
* gives that character no font (RCTNSAttributedStringFragmentFromFragment), so TextKit uses
* its default, Helvetica at 12 points (descender 0.23 em), at any text size. */
const VIEW_FONT_DESCENT = 2.76;
/** React Native rounds an inline view's origin to the pixel grid (ParagraphShadowNode's
* roundToPixel), moving it up to half a pixel: half a point at most. Room is left for that
* wherever a picture would otherwise touch the edge of what cuts it off. */
const PIXEL_ROUNDING = 0.5;

/** A font in a line of text: its size, and whether it is the code face. */
export type TextRun = { fontSize: number; mono?: boolean };
Expand Down Expand Up @@ -104,8 +108,9 @@ export function lineHeightFor(sizes: Size[], fontSize: number, line: number, fon
}

/** The fonts around an inline picture on iOS: those in the hard line it sits in (up to and
* including that line's newline), and every one in its Text. */
export type InlineFonts = { line: TextRun[]; text: TextRun[] };
* including that line's newline) and every one in its Text, and whether that line is the
* Text's last. */
export type InlineFonts = { line: TextRun[]; text: TextRun[]; last: boolean };

/** The largest descent and the tallest line height among some fonts, in points. */
function iosFonts(runs: TextRun[]) {
Expand All @@ -120,20 +125,41 @@ function iosFonts(runs: TextRun[]) {
* character's font is TextKit's default, not the text's. The text's baseline is its line's
* largest descent up from the bottom (TextKit), raised by half the lineHeight over the Text's
* tallest font (RCTApplyBaselineOffsetForRange, over the whole Text). A line with no text has
* only TextKit's font, so its pictures already sit on its baseline. The lift never takes a
* no descent: its baseline is its bottom. In a Text with no text at all that line is the whole
* Text, as tall as its pictures' boxes, so they come down by the descender onto it, or the
* Text would cut off their tops. A line with no text in a Text with text is laid out taller,
* with React Native's baseline offset, so its pictures stay where they are. The lift never takes a
* picture above its line's top: a picture taller than the line leaves above the baseline sits
* lower instead, as the line doesn't grow for it. A line known here is a hard line; when text
* wraps, a wrapped line holding only math is lifted as if it held the hard line's text, still
* inside its line. Android puts an inline view on the baseline itself. */
export function inlineLift(lineHeight: number, size: Size, fonts: InlineFonts, fontScale: number, platform: string = Platform.OS): number {
if (platform !== "ios" || !fonts.line.length) return 0;
if (platform !== "ios") return 0;
if (!fonts.line.length) return fonts.text.length ? 0 : -(VIEW_FONT_DESCENT + PIXEL_ROUNDING);
const { descent } = iosFonts(fonts.line);
const { tallest } = iosFonts(fonts.text);
const baseline = (descent + Math.max(0, (lineHeight - tallest) / 2)) * fontScale;
const room = (lineHeight - (size.height - size.depth)) * fontScale - VIEW_FONT_DESCENT;
const room = (lineHeight - (size.height - size.depth)) * fontScale - VIEW_FONT_DESCENT - PIXEL_ROUNDING;
return Math.max(0, Math.min(baseline - VIEW_FONT_DESCENT, room));
}

/** The room an iOS Text must leave below its last line for the depth of the pictures there,
* in points at the font scale. A Text cuts off what it holds at its frame (Text.js's default
* overflow: hidden), and a last line with no text has no descent to hold a depth. */
export function depthRoom(pictures: { size: Size; fonts: InlineFonts }[], fontScale: number, platform: string = Platform.OS): number {
if (platform !== "ios") return 0;
return Math.max(0, ...pictures.filter(({ fonts }) => fonts.last && !fonts.line.length)
.map(({ size, fonts }) => size.depth * fontScale - VIEW_FONT_DESCENT - inlineLift(0, size, fonts, fontScale, platform) + PIXEL_ROUNDING));
}

/** A Text's bottom padding with `room` added to what its style already gives. */
export function bottomPadding(style: StyleProp<TextStyle>, room: number): { paddingBottom?: number } {
if (room <= 0) return {};
const { paddingBottom, paddingVertical, padding } = StyleSheet.flatten(style) ?? {};
const given = [paddingBottom, paddingVertical, padding].find((value) => typeof value === "number") ?? 0;
return { paddingBottom: given + room };
}

/** A label with its math drawn as pictures, sized before it is drawn so the row never
* grows later. It is never reachable by a screen reader on its own: whatever holds it
* carries the spoken label. `style` must set fontSize and lineHeight. */
Expand Down Expand Up @@ -182,11 +208,16 @@ export function MathLabel({ pieces, style, color }: { pieces: LabelPiece[]; styl
return -1;
});
const text = lines.flat();
const pictures = block.map((_, at) => sizes[at] && { size: sizes[at], fonts: { line: lines[lineOf[at] as number] ?? [], text, last: lineOf[at] === lines.length - 1 } });
const below = depthRoom(pictures.filter((picture) => picture !== undefined), fontScale);
return (
<Text key={index} style={[style, { lineHeight: line }]}>
{block.map((piece, at) => piece.kind === "text" ? piece.text
: <MathPicture key={at} svg={piece.drawn.svg[c.mode]} drawn={piece.drawn} size={sizes[at] as Size} scale={fontScale} color={color} live={live} inline
lift={inlineLift(line, sizes[at] as Size, { line: lines[lineOf[at] as number] ?? [], text }, fontScale)} />)}
<Text key={index} style={[style, { lineHeight: line, ...bottomPadding(style, below) }]}>
{block.map((piece, at) => {
const picture = pictures[at];
if (piece.kind === "text") return piece.text;
return picture && <MathPicture key={at} svg={piece.drawn.svg[c.mode]} drawn={piece.drawn} size={picture.size} scale={fontScale} color={color} live={live} inline
lift={inlineLift(line, picture.size, picture.fonts, fontScale)} />;
})}
</Text>
);
})}
Expand Down
13 changes: 8 additions & 5 deletions apps/mobile/src/NativeMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Text, View, useWindowDimensions, type HostInstance, type LayoutChangeEv
import { drawingsOf, equationOf } from "./message-math";
import { useLivePictures } from "./math-window";
import type { LabelPiece } from "./choice-math";
import { fit, inlineLift, lineHeightFor, MathPicture, MATH_SCALE, MAX_LINES, useScreenReader, useSpeech, type InlineFonts, type Size, type TextRun } from "./MathLabel";
import { bottomPadding, depthRoom, fit, inlineLift, lineHeightFor, MathPicture, MATH_SCALE, MAX_LINES, useScreenReader, useSpeech, type InlineFonts, type Size, type TextRun } from "./MathLabel";
import { spokenLabel, spokenMath } from "./math-speech";
import { httpsLink, type MarkdownToken } from "./render-markdown";
import { fonts, radius, space, useTheme } from "./theme";
Expand Down Expand Up @@ -119,20 +119,22 @@ export const NativeMarkdown = memo(function NativeMarkdown({ tokens, openLink }:
};
walk(nodes);
const text = lines.flat();
return new Map([...lineOf].map(([token, at]) => [token, { line: lines[at] ?? [], text }]));
return new Map([...lineOf].map(([token, at]) => [token, { line: lines[at] ?? [], text, last: at === lines.length - 1 }]));
}

/** A line of text whose line height leaves room for the inline math in it. Inline math
* wider than `width` scales down. */
function line(key: number, nodes: Node[], style: { fontSize: number; lineHeight: number }, extra?: object, role?: "header", width = maxWidth) {
const sizes = mathSizes(nodes, style.fontSize, style.lineHeight, width);
const lineHeight = sizes.size ? lineHeightFor([...sizes.values()], style.fontSize, style.lineHeight, fontScale) : style.lineHeight;
const fonts = inlineFonts(nodes, sizes, style.fontSize);
const below = depthRoom([...fonts].map(([token, around]) => ({ size: sizes.get(token) as Size, fonts: around })), fontScale);
return (
<Live key={key} pictures={sizes.size}>
{(live, ref, onLayout) => (
<Text ref={ref} onLayout={onLayout} accessibilityRole={role} selectable style={[s.prose, style, extra, { lineHeight }]}
<Text ref={ref} onLayout={onLayout} accessibilityRole={role} selectable style={[s.prose, style, extra, { lineHeight, ...bottomPadding([s.prose, style, extra], below) }]}
accessibilityLabel={sizes.size ? spokenLabel(pieces(nodes, sizes), speech && live) : undefined}>
{inline(nodes, { sizes, live, fonts: inlineFonts(nodes, sizes, style.fontSize), lineHeight })}
{inline(nodes, { sizes, live, fonts, lineHeight })}
</Text>
)}
</Live>
Expand Down Expand Up @@ -166,8 +168,9 @@ export const NativeMarkdown = memo(function NativeMarkdown({ tokens, openLink }:
const drawn = size && pictures.get(token);
// Math that can't be drawn shows its source.
if (!size || !drawn) return token.markup === "$" ? `$${token.content}$` : `\\(${token.content}\\)`;
const around = math.fonts.get(token);
const picture = <MathPicture key={index} svg={drawn.svg[c.mode]} drawn={drawn} size={size} scale={fontScale} color={c.text} live={math.live} inline
lift={inlineLift(math.lineHeight, size, math.fonts.get(token) ?? { line: [], text: [] }, fontScale)} />;
lift={around ? inlineLift(math.lineHeight, size, around, fontScale) : 0} />;
// A period or comma right after the math never wraps away from it.
const after = nodes[index + 1]?.token;
return after?.type === "text" && CLINGS.test(after.content) ? [picture, JOIN] : picture;
Expand Down
43 changes: 42 additions & 1 deletion apps/mobile/src/choice-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,49 @@ const THEMES = Object.fromEntries((["dark", "light"] as const).map((mode) => {
return [mode, { text: colors.text, surfaces }];
})) as Record<ThemeMode, { text: string; surfaces: string[] }>;

/** The color of each visible glyph and the box drawn behind it, if any. */
/** Whether a space draws a rule: it has a background and a size. \rule{0pt}{1.2em}, a strut,
* only holds the line open. */
function drawnRule(node: MmlNode): boolean {
if (!node.isKind("mspace") || node.attributes?.getExplicit("mathbackground") === undefined) return false;
const size = (name: string) => parseFloat(String(node.attributes.getExplicit(name) ?? 0));
return size("width") > 0 && (size("height") > 0 || size("depth") > 0);
}

/** A \rule is a space MathJax fills with the \color in effect where the rule is parsed, or
* with black when there is none (BaseMethods' Rule). That black stands for the color around
* it: the nearest \color above it, which math inside \text doesn't see while it is parsed,
* or else the text's color. Any other fill was chosen, and is left alone. */
function rulesInTextColor(node: MmlNode, color?: string) {
const own = ["mathcolor", "color"].map((name) => node.attributes?.getExplicit(name)).find((value) => value !== undefined);
color = own === undefined ? color : String(own);
if (drawnRule(node) && String(node.attributes.getExplicit("mathbackground")).trim().toLowerCase() === "black") {
node.attributes.set("mathbackground", color ?? "currentColor");
}
if (!node.isToken) for (const child of children(node)) if (child) rulesInTextColor(child, color);
}

/** MathJax draws a rule as a rectangle marked as a background, like a \colorbox's, but it is
* the rule's ink: unmarked, a picture drawn without its colors recolors it instead of
* dropping it. */
function rulesAsInk(adaptor: LiteAdaptor, node: LiteElement) {
for (const child of adaptor.childNodes(node)) {
if (adaptor.kind(child) === "#text") continue;
const element = child as LiteElement;
if (adaptor.kind(element) === "rect" && adaptor.getAttribute(node, "data-mml-node") === "mspace") adaptor.removeAttribute(element, "data-bgcolor");
rulesAsInk(adaptor, element);
}
}

/** The color of each visible glyph or rule and the box drawn behind it, if any. */
function inks(node: MmlNode, color: string | undefined, behind: string | undefined, found: { color?: string; behind?: string }[]) {
if (node.isKind("mphantom")) return found;
const explicit = (names: string[]) => names.map((name) => node.attributes?.getExplicit(name)).find((value) => value !== undefined);
// A rule's background is its ink: currentColor is the text's color. A strut draws nothing.
if (drawnRule(node)) {
const rule = String(explicit(["mathbackground", "background"]));
found.push({ color: rule === "currentColor" ? undefined : rule, behind });
return found;
}
color = (explicit(["mathcolor", "color"]) as string | undefined) ?? color;
behind = (explicit(["mathbackground", "background"]) as string | undefined) ?? behind;
if (node.isToken) {
Expand Down Expand Up @@ -222,10 +261,12 @@ export function drawTex(tex: string, display: boolean): DrawnMath | undefined {
item.convert(document, STATE.COMPILED);
const root = item.root;
if (!trusted(root)) return;
rulesInTextColor(root);
item.convert(document);
const svg = adaptor.firstChild(item.typesetRoot) as LiteElement;
const [, minY = 0, width = 0, height = 0] = (adaptor.getAttribute(svg, "viewBox") ?? "").split(" ").map(Number);
for (const name of ["width", "height", "style"]) adaptor.removeAttribute(svg, name);
rulesAsInk(adaptor, svg);
const original = adaptor.outerHTML(svg);
if (/data-mml-node="merror"|data-mjx-error/.test(original) || /\s(?:style|id)=/.test(original)) return;
// The same picture without its groups draws far faster (svg-flatten.ts).
Expand Down
44 changes: 43 additions & 1 deletion apps/mobile/tests/choice-math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import { repairMathForDisplay } from "../../../src/core/math-delims.ts";
import { buildAnswer, type QuestionData } from "../../../src/core/question-core.ts";
import { drawTex, EXCLUDED_PACKAGES, hasMath, joinPieces, plainText, splitLabel, tagClassCount, type LabelPiece } from "../src/choice-math.ts";
import { drawTex, EXCLUDED_PACKAGES, flattening, hasMath, joinPieces, plainText, splitLabel, tagClassCount, type LabelPiece } from "../src/choice-math.ts";
import { spokenLabel } from "../src/math-speech.ts";

const LABEL = "No, $P\\to Q$ is blocked.";
Expand Down Expand Up @@ -113,6 +113,48 @@ test("invisible colors stay as source text", () => {
}
});

test("a rule is drawn in the text's color or its own \\color, and kept when a theme drops colors", () => {
// MathJax fills a \rule with black when no \color is in effect (#317).
const shapes = (svg: string) => svg.match(/<(?:path|rect)\b[^>]*>/g) ?? [];
const fills = (svg: string) => shapes(svg).map((shape) => /fill="([^"]*)"/.exec(shape)?.[1]);
for (const on of [true, false]) {
flattening.on = on;
try {
const plain = drawTex("\\rule{1em}{2em}", false)?.svg;
assert.ok(plain);
assert.deepEqual(fills(plain.dark), ["currentColor"], `flattened ${on}`);
assert.deepEqual(fills(plain.light), ["currentColor"], `flattened ${on}`);
const black = drawTex("\\color{black}{\\rule{1em}{1em}}", false)?.svg;
assert.ok(black);
assert.ok(fills(black.light).includes("black"), "black stands out on the light theme");
assert.ok(fills(black.dark).length > 0 && !fills(black.dark).includes("black"), "dropped to the text color on the dark theme, not dropped");
const red = drawTex("\\color{red}{\\rule{1em}{1em}}", false)?.svg;
assert.ok(red && fills(red.dark).includes("red") && fills(red.light).includes("red"));
// A glyph that doesn't stand out takes the whole picture to the text color, the rule too:
// the rule's right edge (x 1572), or its rectangle, is still drawn.
const beside = drawTex("\\color{navy}{x}\\rule{1em}{1em}", false)?.svg;
assert.ok(beside && fills(beside.dark).every((fill) => fill === undefined || fill === "currentColor") && /H1572V|<rect[^>]*width="1000"/.test(beside.dark), `flattened ${on}`);
} finally {
flattening.on = true;
}
}
// A rule takes the nearest \color above it, even from math inside \text, which MathJax
// parses apart and fills with black; a color written for the rule itself stays.
for (const tex of ["\\textcolor{red}{x\\text{$\\rule{1em}{1em}$}}", "\\text{\\color{red}\\rule[2pt]{1em}{1em}}",
"\\mmlToken{mspace}[width=\"1em\",height=\"1em\",mathbackground=\"red\"]{}"]) {
const svg = drawTex(tex, false)?.svg;
assert.ok(svg, tex);
assert.deepEqual([...new Set([...fills(svg.light), ...fills(svg.dark)])], ["red"], tex);
}
// A strut draws nothing, so it never decides whether colors stand out: black on yellow does.
const strut = drawTex("\\colorbox{yellow}{\\rule{0pt}{1.2em}\\color{black}{x}}", false)?.svg;
assert.ok(strut);
for (const svg of [strut.light, strut.dark]) assert.ok(fills(svg).includes("yellow") && fills(svg).includes("black"), svg.slice(0, 120));
// A box behind text is still a box, and still dropped where its color doesn't stand out.
const box = drawTex("\\colorbox{yellow}{x}", false)?.svg;
assert.ok(box && box.light.includes('<rect fill="yellow"') && !box.dark.includes("<rect"));
});

test("a color that doesn't stand out in a theme is dropped there, not shown as source", () => {
const colored = (tex: string) => { const drawn = drawTex(tex, false); assert.ok(drawn, tex); return drawn.svg; };
const keeps = (svg: string) => /fill="(?!currentColor|none)/.test(svg);
Expand Down
Loading
Loading