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
2 changes: 2 additions & 0 deletions Common/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ interface RenderEquationResult {
* @public
*/
interface ClientRenderOptions extends CommonRenderOptions {
supportsRasterScale?: boolean;
rasterScale?: number;
rangeId: string;
equation: string;
equationLinkEncoded: string;
Expand Down
23 changes: 17 additions & 6 deletions Docs/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ function clientRenderComplete(equations: { options: DocsClientRenderOptions, ren
}

const equationBlob = Utilities.newBlob(Utilities.base64Decode(equation.renderedEquationB64), "image/png");
const result = placeImage(rangeElements[0], equationBlob, mathjaxRenderer, equation.options.equationLinkEncoded, equation.options.size, equation.options.delim, equation.options.customAltText);
const result = placeImage(rangeElements[0], equationBlob, mathjaxRenderer, equation.options.equationLinkEncoded, equation.options.size, equation.options.delim, equation.options.customAltText, equation.options.rasterScale);

if (result.status === DocsEquationRenderStatus.Success) {
c++;
Expand Down Expand Up @@ -1014,6 +1014,7 @@ function buildClientRenderResponse(
const clientRenderOptions: DocsClientRenderOptions = {
...coloredRenderOptions,
size,
supportsRasterScale: true,
rangeId: namedRange.getId(),
equation: clientEquation,
equationLinkEncoded: encodeURIComponent(clientEquation),
Expand Down Expand Up @@ -1140,7 +1141,8 @@ function placeImage(
equation: string,
size: number,
delim: AutoLatexCommon.Delimiter,
customAltText?: string
customAltText?: string,
rasterScale?: number
) {
// GET VARIABLES
let textElement = startElement.getElement().asText();
Expand Down Expand Up @@ -1217,7 +1219,7 @@ function placeImage(
try {
// Configure and size the image while the source equation is still present. Only
// commit the text replacement after all image-side operations have succeeded.
result = repairImage(paragraph, childIndex, size, renderer, delim, renderedEquation, equation, customAltText);
result = repairImage(paragraph, childIndex, size, renderer, delim, renderedEquation, equation, customAltText, rasterScale);
} catch (err) {
// REASON: If image configuration fails, remove the uncommitted image and leave the
// original equation untouched. Cleanup itself is best-effort during a Docs outage.
Expand All @@ -1244,7 +1246,8 @@ function repairImage(
delim: AutoLatexCommon.Delimiter,
resp: GoogleAppsScript.Base.Blob,
equationOriginal: string,
customAltText?: string
customAltText?: string,
rasterScale?: number
): DocsEquationRenderResult {
let attemptsToSetImageUrl = 3;
Common.reportDeltaTime(552); // 3 seconds!! inserting an inline image takes time
Expand Down Expand Up @@ -1317,9 +1320,10 @@ function repairImage(
//C [75.4, 79.6] on width and height ratio
multiple = size / 76.0;
else if (renderer[5] === "MathJax")
// The MathJax renderer returns scaled equations. We scale down by 5 (resolution), and 1.26 is just for consistency with other renderers.
// The MathJax renderer reports its actual raster scale. Missing metadata
// means an old 5x sidebar; 1.26 preserves consistency with other renderers.
// TODO: When MathJax supports changing font, switch to a font that's more similar to CodeCogs
multiple = 1.26 / 5;
multiple = getMathJaxDisplayScale(rasterScale);

Common.reportDeltaTime(595);
Common.sizeImage(getDocsApp(), paragraph, childIndex + 1, Math.round(height * multiple), Math.round(width * multiple));
Expand Down Expand Up @@ -1576,3 +1580,10 @@ function editEquations(sizeRaw: string, delimiter: string, renderer: string = "a
successCount: cursorResult === Common.DerenderResult.Success ? 1 : 0
};
}

// REASON: local to each server bundle so a Common-library version mismatch
// cannot break image sizing. Old sidebars omit the metadata and rendered at 5x.
function getMathJaxDisplayScale(rasterScale?: number) {
const scale = typeof rasterScale === "number" && Number.isFinite(rasterScale) && rasterScale > 0 && rasterScale <= 12 ? rasterScale : 5;
return 1.26 / scale;
}
30 changes: 25 additions & 5 deletions SidebarMathJaxShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ interface SharedMathJaxApi {
}

interface SharedMathJaxRenderOptions {
// Server capability and successful-client response metadata, respectively.
supportsRasterScale?: boolean;
rasterScale?: number;
equation: string;
inline: boolean;
size: number;
Expand Down Expand Up @@ -283,21 +286,34 @@ async function renderEquationPngWithMathJax(
});
}

// measure at the requested font size, then rasterize at 5x for quality
// Measure at the requested font size, then oversample without changing layout.
// REASON: 5x looked softer than CodeCogs. 12x gives 2.4x more pixels per
// dimension. Only opt in when the server can undo the actual scale; old
// servers and already-open old sidebars must retain their original sizing.
svg.classList.add("mathjax-equation-hidden-render");
svg.style.fontSize = `${renderOptions.size}px`;
document.body.appendChild(svg);
const width = svg.clientWidth * 5;
const height = svg.clientHeight * 5;
const layoutWidth = svg.clientWidth;
const layoutHeight = svg.clientHeight;
svg.remove();
// REASON: a whitespace-only / empty equation (e.g. a lone "\r") typesets to a 0x0 SVG. A
// zero-size OffscreenCanvas then throws an opaque IndexSizeError in convertToBlob ("The size
// of OffscreenCanvas is zero"), which surfaced in prod as "MathJax failed to render 1
// equation(s)". Docs skips these upstream in findPos, but Slides/Sheets share this renderer,
// so fail fast here with a clear, self-explaining message instead of the canvas crash.
if (width <= 0 || height <= 0) {
if (!Number.isFinite(layoutWidth) || !Number.isFinite(layoutHeight) || layoutWidth <= 0 || layoutHeight <= 0) {
throw new Error("Empty equation (zero-size render); nothing to rasterize.");
}
// REASON: bound each RGBA canvas to ~32 MB, including very wide/tall
// equations. Four concurrent jobs otherwise multiply the 12x memory cost.
const desiredScale = renderOptions.supportsRasterScale === true ? 12 : 5;
const rasterScale = Math.min(desiredScale, 4096 / layoutWidth, 4096 / layoutHeight,
Math.sqrt(8000000 / (layoutWidth * layoutHeight)));
if (!renderOptions.supportsRasterScale && rasterScale < 5) {
throw new Error("Equation is too large to render safely. Split it into smaller equations.");
}
const width = Math.max(1, Math.floor(layoutWidth * rasterScale));
const height = Math.max(1, Math.floor(layoutHeight * rasterScale));
svg.setAttribute("width", `${width}px`);
svg.setAttribute("height", `${height}px`);

Expand Down Expand Up @@ -345,7 +361,11 @@ async function renderEquationPngWithMathJax(
}
}, "image/png");
});
return await withMathJaxTimeout(pngExport, equationTimeoutMs, "creating the equation image");
const png = await withMathJaxTimeout(pngExport, equationTimeoutMs, "creating the equation image");
// The caller sends the same options object back with the PNG. Set this only
// after successful export; an old sidebar never supplies it (server uses 5).
renderOptions.rasterScale = rasterScale;
return png;
} finally {
URL.revokeObjectURL(svgUrl);
}
Expand Down
13 changes: 12 additions & 1 deletion Slides/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ interface DerenderData {
}

interface SlidesClientRenderOptions {
supportsRasterScale?: boolean;
rasterScale?: number;
size: number;
inline: boolean;
r: number;
Expand Down Expand Up @@ -443,6 +445,7 @@ function findAllClientRenderEquationsInTextElement(
delim: renderOptions.delim,
equation: clientEquation,
equationLinkEncoded: encodeURIComponent(clientEquation),
supportsRasterScale: true,
slideId: slide.getObjectId(),
pageElementId: getTargetObjectId(textElement),
tableRow: isTableCell(textElement) ? textElement.getRowIndex() : undefined,
Expand Down Expand Up @@ -865,6 +868,7 @@ function findClientRenderEquationInTextElement(
delim: renderOptions.delim,
equation: clientEquation,
equationLinkEncoded: encodeURIComponent(clientEquation),
supportsRasterScale: true,
slideId: slide.getObjectId(),
pageElementId: getTargetObjectId(textElement),
tableRow: isTableCell(textElement) ? textElement.getRowIndex() : undefined,
Expand Down Expand Up @@ -1277,7 +1281,7 @@ function placeImageAndFillSpaces(
const mathJaxRenderer = Common.getRenderer(Common.rendererIds.MATHJAX);

const image = target.slide.insertImage(renderedEquation);
resize(image, 1.26 / 5, textHorizontalAlignment, textVerticalAlignment, bounds, pos);
resize(image, getMathJaxDisplayScale(options.rasterScale), textHorizontalAlignment, textVerticalAlignment, bounds, pos);
const imageWidth = image.getWidth();

// Replace the equation source with spaces sized to the rendered image (real font's space width).
Expand Down Expand Up @@ -1670,3 +1674,10 @@ function editEquations(sizeRaw: string, delimiter: string, renderer: string = "a
return { result: SlidesDerenderResult.CursorNotFound, successCount: 0 };
}
}

// REASON: local to each server bundle so a Common-library version mismatch
// cannot break image sizing. Old sidebars omit the metadata and rendered at 5x.
function getMathJaxDisplayScale(rasterScale?: number) {
const scale = typeof rasterScale === "number" && Number.isFinite(rasterScale) && rasterScale > 0 && rasterScale <= 12 ? rasterScale : 5;
return 1.26 / scale;
}
2 changes: 2 additions & 0 deletions Slides/Sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function reportMathJaxClientError(context: string, error: unknown, extra: Record
}

interface SlidesClientRenderOptions {
supportsRasterScale?: boolean;
rasterScale?: number;
size: number;
inline: boolean;
r: number;
Expand Down
27 changes: 21 additions & 6 deletions tests/browser/render-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const css = fs.readFileSync(path.join(root, 'Docs/ALEStylesheet.html'), 'utf8').
const samples = ['1+1=2', String.raw`\text{Work} = F \times d`, String.raw`\frac{1+1}{2}=1`, String.raw`\sum_{i=1}^{n} i = \frac{n(n+1)}{2}`];
const glyphs = [['2B', '3D', '32'], ['3D', 'D7', '1D451'], ['2B', '3D', '31'], ['2211', '3D', '32']];
const clientCases = cases.filter(c => c.renderer === 'auto' || c.renderer === 'mathjax');
const records = [], visuals = [];
const records = [], visuals = [], resolutionChecks = [];

async function main() {
fs.mkdirSync(out, { recursive: true });
Expand Down Expand Up @@ -70,7 +70,7 @@ async function main() {
MathJax.tex2svgPromise = async (...args) => {
const result = await realTypeset(...args);
window.lastTypeset = {
fragmentCount: result.querySelectorAll('svg').length,
fragmentCount: [...result.children].filter(child => child.tagName.toLowerCase() === 'svg').length,
glyphs: [...result.querySelectorAll('[data-c]')].map(n => n.getAttribute('data-c')),
errors: [...result.querySelectorAll('[data-mjx-error]')].map(n => n.getAttribute('data-mjx-error')),
};
Expand All @@ -89,9 +89,23 @@ async function main() {
}
const bytes = new Uint8Array(await blob.arrayBuffer());
const b64 = btoa(Array.from(bytes, b => String.fromCharCode(b)).join(''));
return { ...window.lastTypeset, width: bitmap.width, height: bitmap.height, ink, left, right, b64, mime: blob.type };
return { rasterScale: options.rasterScale || 5, ...window.lastTypeset, width: bitmap.width, height: bitmap.height, ink, left, right, b64, mime: blob.type };
};
});
// Real PNG A/B: equal layout, more pixels, including the reported angle
// and multi-row cases. The legacy request models a server without scale support.
for (const equation of [String.raw`m\angle 2 = 75^\circ`,
String.raw`\begin{cases}K &: a<x\le b\\K &: a<x\le b\\0 &: \text{otherwise}\end{cases}`]) {
const options = { equation, inline: false, size: 11, r: 0, g: 0, b: 0 };
const before = await page.evaluate(p => window.renderPng(p), options);
const after = await page.evaluate(p => window.renderPng(p), { ...options, supportsRasterScale: true });
assert.equal(after.rasterScale, 12, 'new client must report higher resolution');
assert.ok(after.width >= before.width * 2.3 && after.height >= before.height * 2.3);
assert.ok(Math.abs(after.width / after.rasterScale - before.width / 5) < 1);
assert.ok(Math.abs(after.height / after.rasterScale - before.height / 5) < 1);
assert.deepEqual(after.errors, []);
resolutionChecks.push({ equation, before: [before.width, before.height], after: [after.width, after.height] });
}
const pngBySettings = new Map();
for (const c of clientCases) {
for (let sample = 0; sample < samples.length; sample++) {
Expand All @@ -102,6 +116,7 @@ async function main() {
assert.deepEqual(result.errors, []);
for (const glyph of glyphs[sample]) assert.ok(result.glyphs.includes(glyph), `${samples[sample]} missing ${glyph}; got ${result.glyphs}`);
assert.equal(result.mime, 'image/png');
assert.equal(result.rasterScale, 12, 'both apps and renderer preferences negotiate high resolution');
assert.ok(result.ink > 100 && result.width > 0 && result.height > 0, 'PNG contains visible ink');
assert.ok(result.right - result.left > result.width / 2, 'ink reaches across the image');
const hash = crypto.createHash('sha256').update(Buffer.from(result.b64, 'base64')).digest('hex');
Expand All @@ -110,7 +125,7 @@ async function main() {
const key = `${c.app}/${c.mode}/${sample}`;
if (pngBySettings.has(key)) assert.equal(hash, pngBySettings.get(key), 'delimiters/preference must not change pixels');
else pngBySettings.set(key, hash);
records.push({ ...c, sample: samples[sample], delimiterId: p.delim[6], width: result.width, height: result.height, pngSha256: hash });
records.push({ ...c, sample: samples[sample], delimiterId: p.delim[6], width: result.width, height: result.height, rasterScale: result.rasterScale, pngSha256: hash });
if (c.renderer === 'mathjax' && c.delimiter === '$$') visuals.push({ app: c.app, mode: c.mode, sample, ...result });
}
}
Expand All @@ -129,12 +144,12 @@ async function main() {
const escapeHtml = s => s.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
await gallery.setContent(`<!doctype html><html><head><meta charset="utf-8"><style>
body{font:18px system-ui;margin:40px;color:#162536;background:#f5f7fa}h1{font-size:30px;margin-bottom:10px}p{line-height:1.5;color:#475569}table{border-collapse:collapse;background:white;width:100%;table-layout:fixed}th,td{border:1px solid #cbd5e1;padding:24px;text-align:left}th{background:#e8eef5}code{font-size:14px;overflow-wrap:anywhere}img{display:block;max-width:100%}.note{font-size:14px}
</style></head><body><h1>${app} equation rendering · Auto vs Inline size</h1><p>Actual PNG output from the production renderer in Chromium.<br>Automatic and MathJax renderer choices produced identical pixels across every delimiter.</p><table><thead><tr><th>Source equation</th><th>Automatic size</th><th>Inline size</th></tr></thead><tbody>${samples.map((eq, sample) => `<tr><td><code>${escapeHtml(eq)}</code></td>${['smart', 'inline'].map(mode => { const v = cells.find(v => v.sample === sample && v.mode === mode); return `<td><img alt="${escapeHtml(eq)}" src="data:image/png;base64,${v.b64}" width="${v.width / 5 * 2}"></td>`; }).join('')}</tr>`).join('')}</tbody></table><p class="note">2× viewing scale · Source font: ${app === 'Docs' ? 11 : 18}px · Delimiters tested: $$, $ (Beta), \\[ \\], \\( \\), All.<br>Local rendering evidence; not a screenshot of the deployed Google ${app} add-on. External renderer availability and Google insertion are separate checks.<br>Renderer SHA-256: ${rendererSha256.slice(0, 20)} · Base revision: ${revision.slice(0, 7)}</p></body></html>`);
</style></head><body><h1>${app} equation rendering · Auto vs Inline size</h1><p>Actual PNG output from the production renderer in Chromium.<br>Automatic and MathJax renderer choices produced identical pixels across every delimiter.</p><table><thead><tr><th>Source equation</th><th>Automatic size</th><th>Inline size</th></tr></thead><tbody>${samples.map((eq, sample) => `<tr><td><code>${escapeHtml(eq)}</code></td>${['smart', 'inline'].map(mode => { const v = cells.find(v => v.sample === sample && v.mode === mode); return `<td><img alt="${escapeHtml(eq)}" src="data:image/png;base64,${v.b64}" width="${v.width / v.rasterScale * 2}"></td>`; }).join('')}</tr>`).join('')}</tbody></table><p class="note">2× viewing scale · Source font: ${app === 'Docs' ? 11 : 18}px · Delimiters tested: $$, $ (Beta), \\[ \\], \\( \\), All.<br>Local rendering evidence; not a screenshot of the deployed Google ${app} add-on. External renderer availability and Google insertion are separate checks.<br>Renderer SHA-256: ${rendererSha256.slice(0, 20)} · Base revision: ${revision.slice(0, 7)}</p></body></html>`);
await gallery.locator('img').evaluateAll(imgs => Promise.all(imgs.map(img => img.decode())));
await gallery.screenshot({ path: path.join(out, `${app.toLowerCase()}-auto-inline.png`), fullPage: true });
await gallery.close();
}
fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify({ revision, rendererSha256, browser: browser.version(), matrixCases: cases.length, browserCases: clientCases.length, pngRenders: records.length, records }, null, 2));
fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify({ revision, rendererSha256, browser: browser.version(), matrixCases: cases.length, browserCases: clientCases.length, pngRenders: records.length, resolutionChecks, records }, null, 2));
console.log(`PASS: ${clientCases.length} browser settings, ${records.length} real PNG renders. Screenshots: ${out}`);
} finally {
if (browser) await browser.close();
Expand Down
Loading
Loading