diff --git a/README.md b/README.md index b057e26..59bad95 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Also online at [p10node.github.io/office-engine](https://p10node.github.io/offic - **File menu**: new, open (also DOC / XLS / PPT / RTF / OpenDocument / Pages / Keynote / Numbers / Visio / CSV / text / Markdown / HTML / SVG / diagrams), import CSV, rename, download as PDF / HTML / text / CSV, document details, print - **Comments**: threads with replies, resolve, edit / delete, @mentions; on DOCX text, XLSX cells and PPTX slides, stored in the file the way Word, Excel and PowerPoint store them - **Real-time collaboration**: live edits, avatars, collaborators' cursors, offline edits sync on reconnect +- **Fonts**: hand the viewer font files (the OFL fallbacks Carlito, Caladea, Liberation, or the originals) and documents are laid out once they are loaded, so lines and pages break as with the fonts the document names ([API](docs/api.md#fonts)) - **Dark mode and theming**: `theme="dark"` or `"auto"` (follows the OS), or a theme object with design tokens for colors, type and shape ([Theming](docs/theming.md)); documents keep their own colors - **Embed anywhere**: React component, hook, vanilla `mount()`, `` web component, single ` ``` -``: attributes `src`, `name`, `editable`, `toolbar="false"`, `thumbnails="false"`, `save="false"`, `download="false"`, `file-menu="false"`, `open="false"`, `capture-select-all="false"`, `theme="dark"` (or `"auto"`), `collab` / `room` / `user` / `base` (real-time collaboration; `user` also writes the comments), `comments="false"`; properties `.file = File` and `.icons` (map or resolver, as `icons` above); events `load`, `error`, `comment` (`detail`: a `CommentEvent`). +``: attributes `src`, `name`, `editable`, `toolbar="false"`, `thumbnails="false"`, `save="false"`, `download="false"`, `file-menu="false"`, `open="false"`, `capture-select-all="false"`, `theme="dark"` (or `"auto"`), `collab` / `room` / `user` / `base` (real-time collaboration; `user` also writes the comments), `comments="false"`, `fonts` (a JSON list of `FontSource` with URLs: `fonts='[{"family":"Carlito","src":"/fonts/Carlito-Regular.ttf"}]'`); properties `.file = File`, `.icons` (map or resolver, as `icons` above) and `.fonts` (as `fonts` above, bytes too; it wins over the attribute); events `load`, `error`, `comment` (`detail`: a `CommentEvent`). ## Core API (no React) diff --git a/docs/development.md b/docs/development.md index b20388e..291ba3b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -54,7 +54,8 @@ The app reads `DocViewer` props from the query string (`?src=samples/sample.docx | `file-menu.spec.ts` | every File entry: New, Open (conversions), Import, Save, Rename, Download as, Details, Print | | `permissions.spec.ts` | `allowSave` / `allowDownload` combinations, image download, Ctrl+S / Ctrl+P | | `color.spec.ts` | custom colors (system picker, recent list, persistence) in the 3 formats | -| `embed.spec.ts` | `` (shadow DOM, attributes, `file`) and `mount()` | +| `embed.spec.ts` | `` (shadow DOM, attributes, `file`, `fonts`) and `mount()` | +| `fonts.spec.ts` | host fonts (a generated test font): loaded before measuring, timeout, failure, late web fonts | | `collab.spec.ts` | two editors on `scripts/collab-server.ts`: live edits, presence, File menu in a room | `bun test` only runs `test/` (`bunfig.toml`); `bun run test:e2e` runs `e2e/`. diff --git a/docs/features.md b/docs/features.md index c7ca46b..3d68739 100644 --- a/docs/features.md +++ b/docs/features.md @@ -9,6 +9,7 @@ - DOCX: blocks (paragraph/table) stream in → offscreen height measurement in time slices → pagination → only visible pages render - XLSX: rows stream in, 2D virtualized grid (only cells in viewport render), freeze panes, merge, number format, sheet tabs - PPTX: slides parsed on demand when scrolled into view, virtualized thumbnails +- **Fonts**: the host can supply font files (`fonts`: URLs or bytes, loaded with the `FontFace` API when a document uses the family); a declared fallback serves the fonts named before it (Carlito for Calibri, Caladea for Cambria, Liberation for Arial, Times New Roman and Courier New). DOCX pages are measured once those fonts are loaded (at most 3 s, not past a failure; a font that comes later has the pages measured again); a late web font of the page also has the pages measured again; XLSX fits its numbers again. No font file ships with the library, and fonts embedded in a document are not read. See [API](api.md#fonts) ## Format support diff --git a/e2e/app/main.tsx b/e2e/app/main.tsx index be08f97..a26b673 100644 --- a/e2e/app/main.tsx +++ b/e2e/app/main.tsx @@ -3,6 +3,7 @@ * ?src=samples/sample.docx&editable=1&allowSave=0&onSave=1&items=1&close=1&collab=ws://…&room=r * comments: &me=Ann&meId=u1 (author), &people=1 (mentions: Ann, Bob Stone, Cid), &comments=0 (hidden), &cmOpen=1 * &icons=1: every icon from a host resolver, as `` + * &fonts=[{"family":"Carlito","src":"/fonts/carlito.ttf"}]: the host's fonts (JSON) */ import { useState } from 'react' import { createRoot } from 'react-dom/client' @@ -71,6 +72,7 @@ const initial: DocViewerProps = { ) : undefined, + fonts: q.get('fonts') ? JSON.parse(q.get('fonts')!) : undefined, collab: q.get('collab') ? { transport: q.get('collab')!, room: q.get('room') ?? 'e2e', base: Number(q.get('base')) || 0, user: { name: q.get('user') ?? 'Tester' } } : undefined, } diff --git a/e2e/embed.spec.ts b/e2e/embed.spec.ts index d7d38f3..2a465c8 100644 --- a/e2e/embed.spec.ts +++ b/e2e/embed.spec.ts @@ -1,4 +1,6 @@ import { describe, expect, test } from 'bun:test' +import { parasToDocx } from '../src/core/convert' +import { testFont } from '../test/font-builder' import { useBrowser, viewer } from './harness' useBrowser() @@ -40,6 +42,44 @@ describe('embedding without React', () => { await v.page.locator('#wc .de-grid').waitFor() }) + test(': fonts attribute (URLs), fonts property (bytes too)', async () => { + const font = Buffer.from(testFont({ family: 'Wide Test' })) + const docx = Buffer.from(await (await parasToDocx([{ runs: [{ text: 'Wide', set: { font: 'Wide Test' } }] }])).arrayBuffer()) + const asked: string[] = [] + const v = await viewer( + {}, + { + page: 'embed.html', + setup: (v) => + v.ctx.route('**/gen/**', (r) => { + const name = new URL(r.request().url()).pathname.split('/').pop()! + asked.push(name) + return r.fulfill({ body: name.endsWith('.docx') ? docx : font }) + }), + }, + ) + const wc = v.page.locator('#wc') + await wc.locator('.de-page-inner').first().waitFor() + const faces = () => v.page.evaluate(() => [...document.fonts].map((f) => `${f.family.replace(/"/g, '')} ${f.status}`)) + await v.page.evaluate(() => { + const el = document.getElementById('wc')! + el.setAttribute('fonts', JSON.stringify([{ family: 'Wide Test', src: '/gen/wide.ttf' }])) + el.setAttribute('src', 'gen/wide.docx') + }) + await v.page.waitForFunction(() => [...document.fonts].some((f) => f.status === 'loaded')) + expect(asked).toEqual(['wide.docx', 'wide.ttf']) + expect(await faces()).toEqual(['Wide Test loaded']) + // the property wins over the attribute: the bytes, and the file at the URL is let go + await v.page.evaluate((bytes) => { + const el = document.getElementById('wc') as HTMLElement & { fonts?: unknown } + el.fonts = [{ family: 'Wide Test', src: new Uint8Array(bytes).buffer, weight: 700 }] + }, [...font]) + await v.page.waitForFunction(() => [...document.fonts].length === 1 && [...document.fonts][0].weight === '700') + expect(await faces()).toEqual(['Wide Test loaded']) + expect(asked).toEqual(['wide.docx', 'wide.ttf']) + expect(v.errors).toEqual([]) + }) + test('mount(): options, update(), destroy()', async () => { const v = await viewer({}, { page: 'embed.html' }) const host = v.page.locator('#mounted') diff --git a/e2e/fonts.spec.ts b/e2e/fonts.spec.ts new file mode 100644 index 0000000..849dc11 --- /dev/null +++ b/e2e/fonts.spec.ts @@ -0,0 +1,300 @@ +/** + * The host's fonts in Chromium: a generated TrueType font whose letters are 2 em wide (test/font-builder.ts) and a + * document set in it. With the font, its text takes about four times the lines, so the page count tells which font + * the document was laid out with. + */ +import { describe, expect, test } from 'bun:test' +import { parasToDocx, rowsToXlsx } from '../src/core/convert' +import { testFont } from '../test/font-builder' +import { useBrowser, viewer, type Params, type Viewer } from './harness' + +useBrowser() + +declare global { + interface Window { + __frames: { n: number; status: string; font: string }[] + } +} + +const FAMILY = 'Wide Test' +const FONT = Buffer.from(testFont({ family: FAMILY })) +const SENTENCE = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' + +/** a one-word paragraph, then 24 of a few lines, all in `font` */ +async function docx(font: string) { + const paras = [{ runs: [{ text: 'Wide', set: { font } }] }, ...Array.from({ length: 24 }, (_, i) => ({ runs: [{ text: `${i + 1}. ${SENTENCE} ${SENTENCE}`, set: { font } }] }))] + return Buffer.from(await (await parasToDocx(paras)).arrayBuffer()) +} +const DOCS: Partial>> = { + 'wide.docx': docx(FAMILY), + 'calibri.docx': docx('Calibri'), + // a number in a column 18 characters wide, in Calibri (the workbook's font) + 'pi.xlsx': rowsToXlsx([['3.14159265358979']]).then(async (b) => Buffer.from(await b.arrayBuffer())), +} + +const fontsParam = (src: string) => JSON.stringify([{ family: FAMILY, src }]) + +/** + * Serves the document and the fonts under /gen/ (a font after `delay` ms, or with `status`), records what is asked + * for there and, every frame, how many paragraphs page 1 shows, the status text and the state of the test font. + */ +function serve(fonts: Record = {}) { + const requested: string[] = [] + const warnings: string[] = [] + /** every font the page fetched, from anywhere */ + const fontRequests: string[] = [] + const setup = async (v: Viewer) => { + v.page.on('console', (m) => void (m.type() === 'warning' && warnings.push(m.text()))) + v.page.on('request', (r) => void (r.resourceType() === 'font' && fontRequests.push(new URL(r.url()).pathname))) + await v.ctx.route('**/gen/**', async (route) => { + const name = new URL(route.request().url()).pathname.split('/').pop()! + requested.push(name) + try { + if (DOCS[name]) return await route.fulfill({ body: await DOCS[name], contentType: 'application/octet-stream' }) + const how = fonts[name] ?? {} + if (how.delay) await new Promise((r) => setTimeout(r, how.delay)) + if (how.status) return await route.fulfill({ status: how.status, body: 'missing' }) + await route.fulfill({ body: FONT, contentType: 'font/ttf' }) + } catch { + // the page is gone (the test ended before a slow font was sent) + } + }) + await v.page.addInitScript(() => { + window.__frames = [] + const rec = () => { + const page1 = document.querySelector('.de-page[data-page="1"]') + const face = [...document.fonts].find((f) => f.family.replace(/["']/g, '') === 'Wide Test') + window.__frames.push({ n: page1 ? page1.querySelectorAll('[data-pa]').length : 0, status: document.querySelector('.de-bar .de-status')?.textContent ?? '', font: face?.status ?? 'none' }) + requestAnimationFrame(rec) + } + requestAnimationFrame(rec) + }) + } + return { requested, warnings, fontRequests, setup } +} + +const status = (v: Viewer) => v.page.evaluate(() => document.querySelector('.de-bar .de-status')?.textContent ?? '') + +/** the page count once the document is loaded and measured (and stays so for a moment) */ +async function pages(v: Viewer, until: (n: number) => boolean = () => true): Promise { + let last = '' + for (let i = 0; i < 200; i++) { + const s = await status(v) + const m = /^Page \d+ of (\d+) · [\d,]+ words$/.exec(s) + if (m && s === last && until(Number(m[1]))) return Number(m[1]) + last = s + await v.page.waitForTimeout(150) + } + throw new Error(`not laid out: ${last}`) +} + +const open = async (params: Params, fonts?: Record) => { + const s = serve(fonts) + const v = await viewer({ src: 'gen/wide.docx', ...params }, { setup: s.setup }) + return { v, ...s } +} + +/** the page count in whatever the browser has for the family: no font declared */ +let plain: Promise | undefined +const fallbackPages = () => + (plain ??= (async () => { + const { v } = await open({}) + return pages(v) + })()) + +describe('fonts the host supplies', () => { + test('none declared: nothing is fetched, the text is in a font the browser has', async () => { + const { v, requested, fontRequests } = await open({}) + const n = await pages(v) + expect(requested).toEqual(['wide.docx']) + expect(fontRequests).toEqual([]) + expect(await v.page.evaluate(() => document.fonts.size)).toBe(0) + expect(n).toBeGreaterThan(0) + // a font declared for a family the document does not use is not fetched either + const other = await open({ src: 'samples/sample.docx', fonts: fontsParam('/gen/unused.ttf') }) + await pages(other.v) + expect(other.requested).toEqual([]) + expect(v.errors).toEqual([]) + }) + + test('declared: the document is measured with it, once it is there', async () => { + const fallback = await fallbackPages() + const { v, requested, fontRequests } = await open({ fonts: fontsParam('/gen/wide.ttf') }, { 'wide.ttf': { delay: 1200 } }) + const n = await pages(v) + expect(fontRequests).toEqual(['/gen/wide.ttf']) + // about four times the lines: far more pages than in the browser's own font + expect(n).toBeGreaterThan(fallback * 2) + expect(requested).toEqual(['wide.docx', 'wide.ttf']) + + // the text is in it: the family resolves to the file, a one-word run is 8 em wide and 1 em tall (about 2:1 otherwise) + expect(await v.page.evaluate((f) => document.fonts.check(`16px "${f}"`), FAMILY)).toBe(true) + const ratio = await v.page.evaluate(() => { + const span = [...document.querySelectorAll('.de-page [data-pa="0"] span')].find((s) => s.textContent === 'Wide')! + const r = span.getBoundingClientRect() + return r.width / r.height + }) + expect(ratio).toBeGreaterThan(7) + expect(ratio).toBeLessThan(9) + + // nothing was laid out before it arrived: from the first frame that shows page 1, the font is there and page 1 + // never holds more paragraphs than it ends with (the browser's narrower font would have fitted more first) + const frames = await v.page.evaluate(() => window.__frames) + const shown = frames.filter((f) => f.n > 0) + expect(shown.length).toBeGreaterThan(10) + expect(shown.filter((f) => f.font !== 'loaded')).toEqual([]) + const final = shown.at(-1)!.n + expect(shown.filter((f) => f.n > final)).toEqual([]) + const counts = frames.map((f) => /of (\d+)/.exec(f.status)?.[1]).filter((x) => x !== undefined).map(Number) + expect(counts.every((c, i) => i === 0 || c >= counts[i - 1])).toBe(true) + expect(v.errors).toEqual([]) + }) + + test('as bytes: the same layout, nothing fetched', async () => { + const fallback = await fallbackPages() + const { v, requested } = await open({ src: '' }) + await v.page.evaluate( + ([bytes, family]) => window.__setProps({ src: 'gen/wide.docx', fonts: [{ family: family as string, src: new Uint8Array(bytes as number[]).buffer }] }), + [[...FONT], FAMILY] as const, + ) + await v.waitEvent('load') + const n = await pages(v, (x) => x > fallback * 2) + expect(n).toBeGreaterThan(fallback * 2) + expect(requested).toEqual(['wide.docx']) + expect(v.errors).toEqual([]) + }) + + test('declared under the name of a fallback: fetched for the documents in the original (Carlito for Calibri)', async () => { + const { v, requested } = await open({ src: 'gen/calibri.docx', fonts: JSON.stringify([{ family: 'Carlito', src: '/gen/carlito.ttf' }]) }) + await pages(v) + expect(requested).toEqual(['calibri.docx', 'carlito.ttf']) + // (whether the page shows it depends on a Calibri installed here: a font of that name comes first) + expect(await v.page.evaluate(() => [...document.fonts].find((f) => f.family === 'Carlito')?.status)).toBe('loaded') + expect(v.errors).toEqual([]) + }) + + test('a font that cannot be loaded: reported, and the document is shown in the next font', async () => { + const fallback = await fallbackPages() + const { v, warnings } = await open({ fonts: fontsParam('/gen/missing.ttf') }, { 'missing.ttf': { status: 404 } }) + expect(await pages(v)).toBe(fallback) + const errors = await v.events('error') + expect(errors).toHaveLength(1) + expect(errors[0].message).toStartWith(`Font "${FAMILY}" (/gen/missing.ttf) could not be loaded`) + expect(warnings.filter((w) => w.startsWith(`[office-engine] Font "${FAMILY}"`))).toHaveLength(1) + expect(v.errors).toEqual([]) + }) + + test('a slow font: laid out without it after 3 s, again when it arrives, the text in view kept in view', async () => { + const fallback = await fallbackPages() + expect(fallback).toBeGreaterThan(1) + const { v, warnings } = await open({ fonts: fontsParam('/gen/slow.ttf') }, { 'slow.ttf': { delay: 7000 } }) + expect(await pages(v)).toBe(fallback) + // shown while the font was still on its way + const frames = await v.page.evaluate(() => window.__frames) + expect(frames.find((f) => f.n > 0)?.font).toBe('loading') + expect(warnings.some((w) => /^\[office-engine\] font "Wide Test" \(\/gen\/slow\.ttf\) is still loading after 3 s/.test(w))).toBe(true) + + // the reader is on page 2: its first paragraph near the top of the view + await v.page.evaluate(() => { + const sc = document.querySelector('.de-scroll')! + const page1 = document.querySelector('.de-page[data-page="1"]')! + sc.scrollTop = page1.offsetTop + page1.offsetHeight + 16 + 30 + }) + await v.page.waitForTimeout(300) + const before = await v.page.evaluate(() => { + const sc = document.querySelector('.de-scroll')! + const el = document.querySelector('.de-page[data-page="2"] [data-pa]')! + return { pa: el.dataset.pa!, y: el.getBoundingClientRect().top - sc.getBoundingClientRect().top, scroll: sc.scrollTop } + }) + expect(before.y).toBeGreaterThan(0) + + // the font arrives: measured again, many more pages, and that paragraph where it was on screen + const n = await pages(v, (x) => x > fallback * 2) + expect(n).toBeGreaterThan(fallback * 2) + const after = await v.page.evaluate((pa) => { + const sc = document.querySelector('.de-scroll')! + const el = document.querySelector(`[data-pa="${pa}"]`) + return el ? { y: el.getBoundingClientRect().top - sc.getBoundingClientRect().top, scroll: sc.scrollTop } : null + }, before.pa) + expect(after).not.toBeNull() + expect(Math.abs(after!.y - before.y)).toBeLessThan(3) + expect(after!.scroll).toBeGreaterThan(before.scroll) + expect(v.errors).toEqual([]) + }) + + test('editing: the selection is kept when a late font moves its paragraph to another page', async () => { + const fallback = await fallbackPages() + const { v } = await open({ editable: true, fonts: fontsParam('/gen/late.ttf') }, { 'late.ttf': { delay: 5000 } }) + await v.page.locator('.de-ebar').first().waitFor() + expect(await pages(v)).toBe(fallback) + // paragraph 6: on page 1 in the browser's font, further on in the test font + const pageOf = () => v.page.evaluate(() => document.querySelector('[data-pa="6"]')?.closest('.de-page')?.dataset.page) + expect(await pageOf()).toBe('1') + await v.selectText('6. Lorem') + await pages(v, (x) => x > fallback * 2) + expect(await pageOf()).not.toBe('1') + expect(await v.page.evaluate(() => getSelection()?.toString())).toBe('6. Lorem') + await v.page.keyboard.type('Six:') + await v.page.waitForTimeout(300) + expect(await v.page.locator('[data-pa="6"]').innerText()).toStartWith('Six: ipsum dolor') + expect(v.errors).toEqual([]) + }) + + test("a font of the page's own (@font-face) that arrives late: the document is measured again", async () => { + const fallback = await fallbackPages() + const s = serve({ 'css.ttf': { delay: 1500 } }) + const v = await viewer( + { src: 'gen/wide.docx' }, + { + setup: async (v) => { + await s.setup(v) + await v.page.addInitScript(() => { + const style = document.createElement('style') + style.textContent = '@font-face { font-family: "Wide Test"; src: url(/gen/css.ttf) format("truetype"); }' + document.addEventListener('DOMContentLoaded', () => document.head.append(style)) + }) + }, + }, + ) + const n = await pages(v, (x) => x > fallback * 2) + expect(n).toBeGreaterThan(fallback * 2) + expect(s.requested).toEqual(['wide.docx', 'css.ttf']) + expect(v.errors).toEqual([]) + }) + + /** a number shown in a font 2 em a digit: what fits of it in its column is a few characters */ + const fitted = async (v: Viewer) => { + for (let i = 0; i < 100; i++) { + const t = await v.shown('A1') + if (t.length <= 5) return t + await v.page.waitForTimeout(150) + } + return v.shown('A1') + } + + test('xlsx: a number is fitted to its column again when the declared font arrives', async () => { + const { v, requested } = await open({ src: 'gen/pi.xlsx', fonts: JSON.stringify([{ family: 'Calibri', src: '/gen/calibri.ttf' }]) }, { 'calibri.ttf': { delay: 800 } }) + expect(await fitted(v)).toMatch(/^3\.\d{0,3}$/) + expect(requested).toEqual(['pi.xlsx', 'calibri.ttf']) + expect(v.errors).toEqual([]) + }) + + test("xlsx: ...and when a font of the page's own arrives (the widths measured before were the fallback's)", async () => { + const s = serve({ 'css-calibri.ttf': { delay: 800 } }) + const v = await viewer( + { src: 'gen/pi.xlsx' }, + { + setup: async (v) => { + await s.setup(v) + await v.page.addInitScript(() => { + const style = document.createElement('style') + style.textContent = '@font-face { font-family: Calibri; src: url(/gen/css-calibri.ttf) format("truetype"); }' + document.addEventListener('DOMContentLoaded', () => document.head.append(style)) + }) + }, + }, + ) + expect(await fitted(v)).toMatch(/^3\.\d{0,3}$/) + expect(s.requested).toEqual(['pi.xlsx', 'css-calibri.ttf']) + expect(v.errors).toEqual([]) + }) +}) diff --git a/e2e/harness.ts b/e2e/harness.ts index c2e8ce6..567be40 100644 --- a/e2e/harness.ts +++ b/e2e/harness.ts @@ -55,13 +55,14 @@ export function useBrowser() { export type Params = Record -/** Opens the fixture with DocViewer props from `params` and waits for the document. */ -export async function viewer(params: Params, opts: { wait?: boolean; page?: string; viewport?: { width: number; height: number } } = {}) { +/** Opens the fixture with DocViewer props from `params` and waits for the document; `setup` runs before the page loads (routes, init scripts). */ +export async function viewer(params: Params, opts: { wait?: boolean; page?: string; viewport?: { width: number; height: number }; setup?: (v: Viewer) => Promise } = {}) { if (!shared) throw new Error('useBrowser() missing') const ctx = await shared.browser.newContext({ viewport: opts.viewport ?? { width: 1600, height: 1000 }, acceptDownloads: true }) open.add(ctx) const page = await ctx.newPage() const v = new Viewer(page, ctx) + await opts.setup?.(v) // a missing sample is served as the fixture's index.html and fails later as "not a ZIP": say so (tests of a bad file pass wait: false) const sample = typeof params.src === 'string' && params.src.startsWith('samples/') ? params.src.slice('samples/'.length) : '' if (opts.wait !== false && sample && !existsSync(`${SAMPLES}/${sample}`)) throw new Error(`${params.src} is missing: large-* samples are generated (python3 scripts/fixtures.py --large-deck)`) diff --git a/e2e/security.spec.ts b/e2e/security.spec.ts index 96e4971..d21d4f8 100644 --- a/e2e/security.spec.ts +++ b/e2e/security.spec.ts @@ -3,7 +3,8 @@ * browser request nothing, and a link that would run script is not opened. */ import { describe, expect, test } from 'bun:test' -import { PROBE_HOST, probeDocx, probePptx, probeVisio, probeXlsx, scriptLinkDocx } from '../test/probes' +import { PROBE_HOST, probeDocx, probeFontsDocx, probePptx, probeVisio, probeXlsx, scriptLinkDocx } from '../test/probes' +import { testFont } from '../test/font-builder' import { plainToOffice } from '../src/core/plain' import { READY, useBrowser, viewer, type Viewer } from './harness' @@ -85,6 +86,27 @@ describe('a crafted file', () => { }) } + test('docx: a font is fetched only from where the host said, never from the file', async () => { + const asked: string[] = [] + const font = Buffer.from(testFont({ family: 'Probe Font' })) + const v = await viewer( + { src: 'samples/sample.xlsx', fonts: JSON.stringify([{ family: 'Probe Font', src: '/host-fonts/probe.ttf' }]) }, + { setup: (v) => v.ctx.route('**/host-fonts/**', (r) => (asked.push(new URL(r.request().url()).pathname), r.fulfill({ body: font, contentType: 'font/ttf' }))) }, + ) + const requests = await outbound(v) + // the workbook is not set in it: not fetched + expect(asked).toEqual([]) + // a document that embeds "Probe Font" (one file of it at an address) and names a font like CSS: the host's file + await openBytes(v, 'fonts.docx', await probeFontsDocx()) + await v.page.getByText('probe css font').first().waitFor() + await v.page.waitForTimeout(1000) + expect(requests).toEqual([]) + expect(asked).toEqual(['/host-fonts/probe.ttf']) + expect(await v.page.evaluate(() => [...document.fonts].map((f) => `${f.family.replace(/"/g, '')} ${f.status}`))).toEqual(['Probe Font loaded']) + // (the name that looks like CSS is in the pages' styles, as one quoted family name: requested by nobody) + expect(v.errors).toEqual([]) + }) + test('docx: "Open link" does not open a link that would run script', async () => { const v = await viewer({ src: 'samples/sample.docx', editable: true }) const item = v.page.locator('.de-ctx button', { hasText: 'Open link' }) diff --git a/src/core/fonts.ts b/src/core/fonts.ts index c5c2be2..228b146 100644 --- a/src/core/fonts.ts +++ b/src/core/fonts.ts @@ -1,4 +1,5 @@ import { dict } from './dict' +import type { Block, DocxMeta } from './docx/types' /** Maps Office font names to CSS stacks with metric-compatible fallbacks. */ const ALIASES: Record = dict({ @@ -31,3 +32,110 @@ export function cssFont(name: string | undefined): string | undefined { cache.set(name, v) return v } + +/** + * A font file the host supplies (DocViewer's `fonts`). Fonts are never taken from a document: its font names only + * pick among these. Plain data (a URL string or the bytes), so the same list can be handed to a worker. + */ +export interface FontSource { + /** + * The family text asks for: a document's own name (`Calibri`), used before an installed font of that name, or the + * metric-compatible one the stack names after it (`Carlito`, `Caladea`, `Liberation Sans`…), used where the + * original is not installed. + */ + family: string + /** The file (TTF, OTF, WOFF, WOFF2): a URL, resolved against the page, or its bytes. */ + src: string | ArrayBuffer | ArrayBufferView | Blob + /** CSS font-weight of the file: `400`, `700`, `'bold'`, `'100 900'` (a variable font). Default normal. */ + weight?: number | string + /** CSS font-style of the file: `'normal'`, `'italic'`, `'oblique'`. Default normal. */ + style?: string + /** CSS unicode-range: the characters the file covers, when a family comes in several files. */ + unicodeRange?: string +} + +/** a family name as CSS compares it: unquoted, spaces collapsed, ASCII case folded */ +export function familyKey(name: string): string { + return name + .trim() + .replace(/^(["'])(.*)\1$/, '$2') + .replace(/\s+/g, ' ') + .replace(/[A-Z]+/g, (c) => c.toLowerCase()) +} + +const lists = new Map() + +/** the families (by `familyKey`) of a CSS font-family list, such as the stacks `cssFont()` builds */ +export function fontFamilies(stack: string): string[] { + let v = lists.get(stack) + if (v) return v + v = [] + let cur = '' + let quote = '' + const end = () => { + const k = familyKey(cur) + if (k) v!.push(k) + cur = '' + } + for (const ch of stack) { + if (quote) { + if (ch === quote) quote = '' + else cur += ch + } else if (ch === '"' || ch === "'") quote = ch + else if (ch === ',') end() + else cur += ch + } + end() + if (lists.size > 1000) lists.clear() + lists.set(stack, v) + return v +} + +/** + * The declared fonts text in these stacks can use: those of a family a stack names. With the stacks `cssFont()` + * builds, a font declared as `Carlito` serves a document in Calibri, and one declared as `Calibri` too. + */ +export function fontsFor(fonts: readonly T[], stacks: Iterable): T[] { + if (!fonts.length) return [] + const named = new Set() + for (const s of stacks) for (const f of fontFamilies(s)) named.add(f) + return fonts.filter((f) => named.has(familyKey(f.family))) +} + +/** the FontFace descriptors of a declared font (what it leaves out is CSS's default) */ +export function faceDescriptors(f: Pick): FontFaceDescriptors { + const d: FontFaceDescriptors = {} + if (f.weight !== undefined && f.weight !== '') d.weight = String(f.weight) + if (f.style) d.style = f.style + if (f.unicodeRange) d.unicodeRange = f.unicodeRange + return d +} + +/** the family name given to a FontFace: without what `cssFont()` leaves out of a name, so the two agree */ +export const faceFamily = (family: string) => family.trim().replace(/^(["'])(.*)\1$/, '$2').replace(/["\\\u0000-\u001f]/g, '') + +/** `url("…")` for a FontFace source: the address quoted, what would end the quote or the value escaped */ +export const cssUrl = (url: string) => `url("${url.replace(/["\\\n\r\f]/g, (c) => `\\${c.charCodeAt(0).toString(16)} `)}")` + +/** the CSS font stacks text of these DOCX blocks is set in: runs, list markers, empty paragraphs, cells, text boxes */ +export function docxFonts(blocks: Iterable, into = new Set()): Set { + const walk = (b: Block) => { + if (b.t === 'p') { + if (b.markFont) into.add(b.markFont) + if (b.marker?.s.font) into.add(b.marker.s.font) + for (const x of b.runs) { + if (x.t === 'box') x.blocks.forEach(walk) + else if ((x.t === 'text' || x.t === 'field' || x.t === 'ref') && x.s.font) into.add(x.s.font) + } + } else if (b.t === 'tbl') for (const r of b.rows) for (const c of r.cells) c.blocks.forEach(walk) + } + for (const b of blocks) walk(b) + return into +} + +/** …and of what every page shows: the body's default font, headers and footers */ +export function docxPageFonts(meta: DocxMeta, into = new Set()): Set { + if (meta.font) into.add(meta.font) + for (const part of [meta.header, meta.footer, meta.firstHeader, meta.firstFooter, meta.evenHeader, meta.evenFooter]) if (part) docxFonts(part, into) + return into +} diff --git a/src/core/index.ts b/src/core/index.ts index b07624c..04bed92 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,5 +1,6 @@ import './default-worker' export * from './source' +export type { FontSource } from './fonts' export { ZipArchive } from './zip' export { SaxParser, TreeBuilder, parseXml, parseXmlStream, type XEl, type SaxHandler } from './xml' export { OpcPackage, type DocKind } from './opc' diff --git a/src/embed.ts b/src/embed.ts index 925e128..a483d6e 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -3,6 +3,7 @@ import { createRoot, type Root } from 'react-dom/client' import './core/default-worker' import { DocViewer, type DocViewerProps } from './react/DocViewer' import type { Icons } from './react/edit/Toolbar' +import type { FontSource } from './core/fonts' export type MountOptions = Omit @@ -36,15 +37,17 @@ export function mount(el: HTMLElement, options: MountOptions): MountedViewer { * `` * Rendered inside a shadow root so host page CSS cannot leak in. * Attributes: src, name, editable, toolbar / thumbnails / save / download / file-menu / open / capture-select-all ("false" to turn off), - * theme ("light" | "dark" | "auto"); + * theme ("light" | "dark" | "auto"), fonts (a JSON list of `{ family, src, weight?, style?, unicodeRange? }` with URLs); * real-time collaboration: collab (hub WebSocket URL), room, user (display name), base (snapshot seq `src` was opened at). - * Properties: `file` (File/Blob/ArrayBuffer) for local data; `icons` (map or resolver, as DocViewer's `icons`). + * Properties: `file` (File/Blob/ArrayBuffer) for local data; `icons` (map or resolver, as DocViewer's `icons`); + * `fonts` (as DocViewer's `fonts`, bytes too; over the attribute). */ export class OfficeViewerElement extends HTMLElement { - static observedAttributes = ['src', 'name', 'toolbar', 'thumbnails', 'editable', 'save', 'download', 'file-menu', 'open', 'capture-select-all', 'theme', 'collab', 'room', 'user', 'base', 'comments'] + static observedAttributes = ['src', 'name', 'toolbar', 'thumbnails', 'editable', 'save', 'download', 'file-menu', 'open', 'capture-select-all', 'theme', 'collab', 'room', 'user', 'base', 'comments', 'fonts'] private viewer?: MountedViewer private _file?: Blob | ArrayBuffer private _icons?: Icons + private _fonts?: readonly FontSource[] // one function each for the element's life: a new one with every update would run the viewer's load effect again (a `load` event per update) private readonly onLoad: NonNullable = (client) => this.dispatchEvent(new CustomEvent('load', { detail: { kind: client.kind, name: client.name } })) private readonly onError: NonNullable = (error) => this.dispatchEvent(new CustomEvent('error', { detail: error })) @@ -66,6 +69,14 @@ export class OfficeViewerElement extends HTMLElement { this.sync() } + get fonts() { + return this._fonts + } + set fonts(v: readonly FontSource[] | undefined) { + this._fonts = v + this.sync() + } + connectedCallback() { if (this.viewer) return const shadow = this.shadowRoot ?? this.attachShadow({ mode: 'open' }) @@ -100,6 +111,7 @@ export class OfficeViewerElement extends HTMLElement { captureSelectAll: this.getAttribute('capture-select-all') !== 'false', theme: THEMES.find((t) => t === this.getAttribute('theme')), icons: this._icons, + fonts: this._fonts ?? fontsAttr(this.getAttribute('fonts')), collab: this.getAttribute('collab') && this.getAttribute('room') ? { @@ -126,6 +138,19 @@ export class OfficeViewerElement extends HTMLElement { } } +/** the `fonts` attribute: a JSON list of fonts by URL; anything else is ignored, with a warning */ +function fontsAttr(v: string | null): FontSource[] | undefined { + if (!v) return undefined + try { + const list = JSON.parse(v) as unknown + if (Array.isArray(list)) return list.filter((f): f is FontSource => !!f && typeof f.family === 'string' && typeof f.src === 'string') + } catch { + // reported below + } + console.warn('[office-engine] the fonts attribute is not a JSON list of { family, src } objects; it is ignored') + return undefined +} + export function defineElement(tag = 'office-viewer') { if (typeof customElements !== 'undefined' && !customElements.get(tag)) customElements.define(tag, OfficeViewerElement) } diff --git a/src/index.ts b/src/index.ts index ec48d5b..cfa57e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ export { IconsProvider, ICON_NAMES, type IconName, type Icons } from './react/ed export { DocumentClient, setDefaultWorker, type OpenOptions } from './core/worker/client' export type { DocSource } from './core/source' +export type { FontSource } from './core/fonts' export type { DocKind } from './core/opc' export type * from './core/docx/types' export type * from './core/xlsx/types' diff --git a/src/react/DocViewer.tsx b/src/react/DocViewer.tsx index 59ca633..80fd573 100644 --- a/src/react/DocViewer.tsx +++ b/src/react/DocViewer.tsx @@ -26,6 +26,8 @@ import type { CollabOptions } from '../collab' import { CollabBar, PeerOverlay, useCollab } from '../collab/react' import { CommentsContext, CommentsCtl, useCommentsState, type CommentOptions } from './comments/Comments' import { COMMENT_KEYS, CommentsPanel } from './comments/CommentsPanel' +import type { FontSource } from '../core/fonts' +import { FontsContext, useHostFonts } from './common/fonts' export interface DocViewerProps { /** @@ -112,6 +114,15 @@ export interface DocViewerProps { * Each is rendered in `span.de-icon`, sized by font-size (1em). */ icons?: Icons + /** + * Font files the host supplies (the library ships none): `{ family, src, weight?, style?, unicodeRange? }`, `src` a URL + * or the bytes. A file is loaded when a document's text names its family, directly (`Calibri`) or as the fallback + * the font list puts after it (`Carlito` for Calibri, `Caladea` for Cambria, `Liberation Sans / Serif / Mono` for + * Arial, Times New Roman, Courier New); documents are laid out once it is there. One that fails is reported to + * `onError`, one slower than 3 s is waited for no longer: the text is shown in the next font of its list, and laid + * out again when it arrives. + */ + fonts?: readonly FontSource[] } const EXT: Record = { docx: '.docx', xlsx: '.xlsx', pptx: '.pptx' } @@ -164,6 +175,7 @@ function fmtBytes(n: number) { export function DocViewer(props: DocViewerProps) { const { src, name, className, style, toolbar = true, toolbarExtra, zoom, thumbnails, headers, credentials, worker, styleTarget, theme, onLoad, onError } = props const root = useThemeRoot(theme, style) + const fonts = useHostFonts(props.fonts, onError) const { allowEdit = true, allowSave = true, allowDownload = true, fileMenu = true, allowOpen = true, captureSelectAll = true, onSave, onDirtyChange } = props const [editing, setEditing] = useState(!!props.editable) useEffect(() => { @@ -691,7 +703,9 @@ export function DocViewer(props: DocViewerProps) {
- {body} + + {body} +
diff --git a/src/react/common/fonts.ts b/src/react/common/fonts.ts new file mode 100644 index 0000000..7032a82 --- /dev/null +++ b/src/react/common/fonts.ts @@ -0,0 +1,280 @@ +/** + * The host's fonts (DocViewer's `fonts`): FontFace objects in the page's font set while a viewer declares them, + * shared by the viewers that declare the same file. The views ask for the ones their text names and wait for them + * before measuring (DOCX), or measure again once they are there (XLSX). + */ +import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react' +import { cssUrl, faceDescriptors, faceFamily, familyKey, fontFamilies, fontsFor, type FontSource } from '../../core/fonts' +import { useForceUpdate, useLatest } from './hooks' + +/** `timeout`: how long a view waits for a font before it lays the text out without it (a browser's own block period) */ +export const FONT_LIMITS = { timeout: 3000 } + +type State = 'idle' | 'loading' | 'loaded' | 'error' | 'timeout' + +/** one declared font file, shared by every viewer on the page that declares it */ +export interface HostFace { + key: string + src: FontSource + users: number + state: State + face?: FontFace + /** what the FontFace was made from: the CSS url() or the bytes (a print frame makes its own) */ + data?: string | BufferSource + error?: Error +} + +const faces = new Map() +const listeners = new Set<(f: HostFace, removed: boolean) => void>() +const emit = (f: HostFace, removed = false) => { + for (const l of [...listeners]) l(f, removed) +} + +/** the declaration (`fontKey`) each FontFace made for the host's fonts is for */ +const hostFaces = new WeakMap() + +const fontSet = (): FontFaceSet | undefined => (typeof document !== 'undefined' && typeof FontFace !== 'undefined' ? document.fonts : undefined) + +const bytesIds = new WeakMap() +let nextBytes = 0 + +/** what tells two declarations of a face apart: family, descriptors and file (its URL, or the bytes object itself) */ +export function fontKey(f: FontSource): string { + const d = faceDescriptors(f) + let file: string + if (typeof f.src === 'string') file = 'u:' + f.src + else { + let id = bytesIds.get(f.src) + if (id === undefined) bytesIds.set(f.src, (id = ++nextBytes)) + file = 'b:' + id + } + return [familyKey(f.family), d.weight ?? '', d.style ?? '', d.unicodeRange ?? '', file].join('|') +} + +const describe = (f: HostFace) => `"${f.src.family}"${typeof f.src.src === 'string' ? ` (${f.src.src})` : ''}` + +function fail(f: HostFace, e: unknown) { + if (f.state === 'error') return + f.state = 'error' + const why = e instanceof Error && e.message ? `: ${e.message}` : '' + f.error = new Error(`Font ${describe(f)} could not be loaded${why}`) + console.warn(`[office-engine] ${f.error.message}; text in it is shown in the next font of its list`) + emit(f) +} + +/** the FontFace, in the page's font set: from then on the browser also fetches it itself when text in it is shown */ +function attach(f: HostFace, data: string | ArrayBuffer | ArrayBufferView) { + // (a view over a SharedArrayBuffer is refused by FontFace itself, as a failed font) + const set = fontSet() + if (!set) return + try { + f.data = typeof data === 'string' ? cssUrl(new URL(data, document.baseURI).href) : (data as BufferSource) + f.face = new FontFace(faceFamily(f.src.family), f.data, faceDescriptors(f.src)) + hostFaces.set(f.face, f.key) + set.add(f.face) + } catch (e) { + f.face = undefined + fail(f, e) + } +} + +/** the face of a declaration, held by one more viewer: the first one adds it to the page's font set */ +export function acquireFont(src: FontSource): HostFace { + const key = fontKey(src) + let f = faces.get(key) + if (!f) faces.set(key, (f = { key, src, users: 0, state: 'idle' })) + // a Blob's bytes are read when the font is first needed + if (f.users++ === 0 && !f.face && !(typeof Blob !== 'undefined' && src.src instanceof Blob)) attach(f, src.src as string | ArrayBuffer | ArrayBufferView) + emit(f) + return f +} + +/** a viewer lets the face go: it leaves the page's font set when no viewer holds it */ +export function releaseFont(f: HostFace) { + f.users-- + // later: a viewer that declares it again at once (an effect run again) keeps the same face, not a second download + queueMicrotask(() => { + if (f.users > 0 || faces.get(f.key) !== f) return + faces.delete(f.key) + if (f.face) fontSet()?.delete(f.face) + emit(f, true) + }) +} + +/** starts loading a face once; it settles as loaded, failed, or timed out (and may still arrive later) */ +export function loadFont(f: HostFace) { + if (f.state !== 'idle' || !fontSet()) return + f.state = 'loading' + const timer = setTimeout(() => { + if (f.state !== 'loading') return + f.state = 'timeout' + console.warn(`[office-engine] font ${describe(f)} is still loading after ${FONT_LIMITS.timeout / 1000} s: the text is laid out without it, and again once it arrives`) + emit(f) + }, FONT_LIMITS.timeout) + const src = f.src.src + const made = f.face + ? Promise.resolve(f.face) + : src instanceof Blob + ? src.arrayBuffer().then((bytes) => { + attach(f, bytes) + if (!f.face) throw f.error + return f.face + }) + : Promise.reject(new Error('not a font file')) + made + .then((face) => face.load()) + .then( + () => { + clearTimeout(timer) + f.state = 'loaded' + emit(f) + }, + (e) => { + clearTimeout(timer) + fail(f, e) + }, + ) +} + +const NONE: readonly FontSource[] = [] + +/** the fonts the host declared, for the views inside a viewer */ +export const FontsContext = createContext(NONE) + +/** + * The host's fonts while the viewer is shown: in the page's font set (nothing is fetched until text needs one), + * released when no viewer declares them any more. A font that cannot be loaded is reported to `onError`. + */ +export function useHostFonts(fonts: readonly FontSource[] | undefined, onError?: (error: Error) => void): readonly FontSource[] { + const given = fonts ?? NONE + const valid = given.filter((f) => !!f && typeof f.family === 'string' && !!f.family.trim() && (typeof f.src === 'string' || (typeof f.src === 'object' && f.src !== null))) + // the same fonts in a new array (a host's inline list) are not another declaration + const key = valid.map(fontKey).join('\n') + const list = useMemo(() => { + if (valid.length < given.length) console.warn('[office-engine] a font without a family name or a src is ignored') + return valid.length ? valid : NONE + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [key, valid.length < given.length]) + const errorTo = useLatest(onError) + useEffect(() => { + if (!list.length) return + const held = new Set(list.map(acquireFont)) + const told = new Set() + const tell = (f: HostFace) => { + if (f.state !== 'error' || !held.has(f) || told.has(f)) return + told.add(f) + errorTo.current?.(f.error!) + } + held.forEach(tell) + const off = onFonts(tell) + return () => { + off() + held.forEach(releaseFont) + } + }, [list, errorTo]) + return list +} + +/** called when a face is held, settles or is let go */ +export function onFonts(fn: (f: HostFace, removed: boolean) => void) { + listeners.add(fn) + return () => void listeners.delete(fn) +} + +export interface DocFonts { + /** + * Whether text in these CSS font stacks can be measured: the host's fonts they name have loaded, failed or timed + * out. Starts loading the ones that have not; the view renders again when they settle. + */ + ready(stacks: Iterable): boolean + /** + * Grows when a font arrives that text measured before was laid out without: one that timed out, one the page + * loads through CSS, a host font taken away. What was measured is measured again. + */ + gen: number +} + +/** the fonts of one view's document: what it waits for, and when it measures again */ +export function useDocFonts(): DocFonts { + const declared = useContext(FontsContext) + const [, rerender] = useForceUpdate() + const [gen, setGen] = useState(0) + /** + * `watch`: the families its text is set in; `late`: faces it declared and measured text without; `seen`: faces it + * did not declare (the page's own @font-face, another viewer's) that arrived; `mine`: what it declared + */ + const ref = useRef<{ watch: Set; late: WeakSet; seen: WeakSet; mine: Set } | null>(null) + const st = (ref.current ??= { watch: new Set(), late: new WeakSet(), seen: new WeakSet(), mine: new Set() }) + st.mine = useMemo(() => new Set(declared.map(fontKey)), [declared]) + useEffect(() => { + const set = fontSet() + const { watch, late, seen } = st + const again = () => setGen((g) => g + 1) + // the browser loaded a font this view did not ask for that text here is set in (the browser fetches such a font + // when text first needs it): that text was laid out while it was missing + const done = (e: Event) => { + let hit = false + for (const face of (e as FontFaceSetLoadEvent).fontfaces ?? []) { + const key = hostFaces.get(face) + if ((key && st.mine.has(key)) || seen.has(face) || !watch.has(familyKey(face.family))) continue + seen.add(face) + hit = true + } + if (hit) again() + } + set?.addEventListener('loadingdone', done) + const off = onFonts((f, removed) => { + // a host font that arrived after text was measured without it, or one taken away from text laid out with it + if (f.face && !removed && f.state === 'loaded' && late.has(f.face)) { + late.delete(f.face) + again() + } else if (removed && f.state === 'loaded' && watch.has(familyKey(f.src.family))) again() + else rerender() + }) + return () => { + set?.removeEventListener('loadingdone', done) + off() + } + }, [st, rerender]) + return { + gen, + ready(stacks) { + const list = [...stacks] + for (const s of list) for (const fam of fontFamilies(s)) st.watch.add(fam) + if (!declared.length || !fontSet()) return true + let ok = true + for (const d of fontsFor(declared, list)) { + const f = faces.get(fontKey(d)) + // declared a moment ago: the viewer holds it after this render, and that renders again + if (!f) { + ok = false + continue + } + loadFont(f) + if (f.state === 'idle' || f.state === 'loading') ok = false + // what is measured now is measured without it: when it arrives, it is measured again + else if (f.state === 'timeout' && f.face) st.late.add(f.face) + } + return ok + }, + } +} + +/** + * The host's fonts the page has loaded, in another document too (the print frame): its text breaks as the page's + * does. They start loading; the document's `fonts.ready` tells when they are there. + */ +export function copyFonts(doc: Document) { + const W = doc.defaultView as (Window & typeof globalThis) | null + if (!W?.FontFace || !doc.fonts) return + for (const f of faces.values()) { + if (f.state !== 'loaded' || f.data === undefined) continue + try { + const face = new W.FontFace(faceFamily(f.src.family), f.data, faceDescriptors(f.src)) + doc.fonts.add(face) + face.load().catch(() => {}) + } catch { + // as on the page: the text is shown in the next font of its list + } + } +} diff --git a/src/react/docx/DocxView.tsx b/src/react/docx/DocxView.tsx index 358f61b..221c700 100644 --- a/src/react/docx/DocxView.tsx +++ b/src/react/docx/DocxView.tsx @@ -25,6 +25,8 @@ import { mergeSource } from '../../core/plainmerge' import { docxBodyHtml, docxToHtml, docxToMarkdown, docxToText, pictureData } from '../file/text' import { BlockView, contextual, type RenderCtx } from './render' import { pageHF, paginate, type PageSlice } from './paginate' +import { docxFonts, docxPageFonts } from '../../core/fonts' +import { useDocFonts } from '../common/fonts' const PX = 4 / 3 const GAP = 16 @@ -169,12 +171,23 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp } const tailEnd = Math.min(total, st.frontier + batch) for (let i = st.frontier; i < tailEnd; i++) queue.push({ b: st.blocks[i], i }) + // ---- fonts: blocks are measured once the host's fonts they are set in are ready (loaded, failed or timed out). A + // font that arrives later (one that timed out, the page's own web font) has the measured blocks measured again in + // slices, their old heights kept meanwhile: the pages stay, and so do the caret and the page in view ---- + const fonts = useDocFonts() + const again = useRef({ gen: fonts.gen, next: -1 }) + if (again.current.gen !== fonts.gen) again.current = { gen: fonts.gen, next: st.frontier > 0 ? 0 : -1 } + const againFrom = again.current.next + const againTo = st.frontier + const againEnd = againFrom < 0 ? -1 : Math.min(againTo, againFrom + batch) + for (let i = Math.max(0, againFrom); i < againEnd; i++) if (!st.dirty.has(st.blocks[i])) queue.push({ b: st.blocks[i], i }) + const fontsReady = !queue.length || !meta || fonts.ready(docxFonts(queue.map((q) => q.b), docxPageFonts(meta))) const renderStart = performance.now() const measureCtx = useMemo(() => ({ client, page: -1, pages: 0, measuring: true, editable: false }), [client]) useIsoLayoutEffect(() => { const el = measureRef.current - if (!el || !queue.length || !meta) return + if (!el || !queue.length || !meta || !fontsReady) return const hs = Array.from(el.children).map((c) => (c as HTMLElement).getBoundingClientRect().height) const dt = performance.now() - renderStart const id = setTimeout(() => { @@ -183,6 +196,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp st.dirty.delete(q.b) }) if (tailEnd > st.frontier) st.frontier = tailEnd + if (againEnd >= 0) again.current.next = againEnd >= againTo ? -1 : againEnd setBatch((b) => (dt < 10 ? Math.min(b * 2, 1500) : dt > 30 ? Math.max(10, b >> 1) : b)) bump() }, 0) @@ -215,6 +229,33 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp const first = tops.length ? lowerIndex(tops, scroll.y - pageH * z) : 0 const last = tops.length ? lowerIndex(tops, scroll.y + size.h + 200) : -1 + // measured again for a font: the first block in view stays where it is on screen, until the measuring is done or the + // reader scrolls. Placed by the model (blocks stacked from the top margin): the text on screen may already be in the + // new font while the pages are not + const anchor = useRef<{ gen: number; bi: number; dy: number; y: number } | null>(null) + useIsoLayoutEffect(() => { + const sc = scrollRef.current + const a = anchor.current + if (!sc || !tops.length) return + const blockTop = (pi: number, bi: number) => { + let y = tops[pi] + page.mt * PX * z + for (let i = pages[pi].start; i < bi; i++) y += (st.heights.get(st.blocks[i]) ?? 0) * z + return y + } + if (again.current.next === 0 && a?.gen !== fonts.gen) { + // (nothing measured again yet: these are the pages the reader sees) + const y = sc.scrollTop + const pi = lowerIndex(tops, y) + let bi = pages[pi].start + while (bi < pages[pi].end - 1 && blockTop(pi, bi + 1) <= y) bi++ + anchor.current = { gen: fonts.gen, bi, dy: blockTop(pi, bi) - y, y } + } else if (a && Math.abs(sc.scrollTop - a.y) <= 1) { + const pi = pages.findIndex((p) => a.bi >= p.start && a.bi < p.end) + if (pi >= 0) sc.scrollTop = Math.max(0, blockTop(pi, a.bi) - a.dy) + a.y = sc.scrollTop + if (again.current.next < 0) anchor.current = null + } else anchor.current = null + }) const pageCount = complete ? pages.length : 0 // Both ends of the selection stay mounted, so the DOM selection survives scrolling. A removed page moves its end of // the range to the pages container, and a page mounted again there lands after that point: outside a range that ends on it. @@ -790,7 +831,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp } }} > - {meta && queue.length > 0 && ( + {meta && queue.length > 0 && fontsReady && (
{queue.map(({ b, i }) => ( diff --git a/src/react/file/print.tsx b/src/react/file/print.tsx index f1ca520..7d21de5 100644 --- a/src/react/file/print.tsx +++ b/src/react/file/print.tsx @@ -1,6 +1,7 @@ import type { ReactNode } from 'react' import { createRoot } from 'react-dom/client' import { stylesText } from '../injectStyles' +import { copyFonts } from '../common/fonts' /** Content laid out for paper: PDF / print and standalone HTML. */ export interface Printable { @@ -191,6 +192,8 @@ async function mount(p: Printable, title: string): Promise { frame.remove() } try { + // the host's fonts, as the pages were laid out with them (waited for with the document's own fonts) + copyFonts(doc) await settle(doc, host) fitText(host) } catch (e) { diff --git a/src/react/xlsx/XlsxView.tsx b/src/react/xlsx/XlsxView.tsx index beee293..45098cd 100644 --- a/src/react/xlsx/XlsxView.tsx +++ b/src/react/xlsx/XlsxView.tsx @@ -19,7 +19,8 @@ import { XlsxToolbar, type MergeMode } from './XlsxToolbar' import { Icon } from '../edit/Toolbar' import { Axis } from './axis' import { drawingBox, drawingEl, drawingExtent, useSheetDrawings } from './drawings' -import { fitNumber, fontPx } from './fit' +import { fitNumber, fontPx, forgetWidths } from './fit' +import { useDocFonts } from '../common/fonts' import { htmlToTsv } from './html' import { fontOf, rotatedHeight, textWidth, wrapHeight } from './measure' import { sheetPrintable } from './print' @@ -754,6 +755,17 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet }: const styles = meta.styles const basePx = fontPx(meta.baseFont.fontSize, 14.67) + // the host's fonts the workbook is set in are loaded: numbers fitted to their columns before one arrived (or before + // the page's own web font did) are fitted again + const fonts = useDocFonts() + // eslint-disable-next-line react-hooks/exhaustive-deps + const stacks = useMemo(() => new Set([meta.baseFont, ...styles].map((x) => x?.fontFamily).filter((f): f is string => typeof f === 'string')), [styles.length]) + const fontsKey = `${fonts.ready(stacks)}|${fonts.gen}` + const lastFonts = useRef(fontsKey) + if (lastFonts.current !== fontsKey) { + lastFonts.current = fontsKey + forgetWidths() + } const mergeAt = (r: number, c: number) => s.merges.find((m) => r >= m.r0 && r <= m.r1 && c >= m.c0 && c <= m.c1) // A row that says no height shows its turned text whole: Excel fits such rows when it opens the file. In the view diff --git a/src/react/xlsx/fit.ts b/src/react/xlsx/fit.ts index 9b2ee3f..453e4e6 100644 --- a/src/react/xlsx/fit.ts +++ b/src/react/xlsx/fit.ts @@ -14,6 +14,9 @@ let ctx: CanvasRenderingContext2D | null | undefined /** widths of the characters of numbers, by font (a CSS font) */ const widths = new Map>() +/** a font arrived: the widths measured before it are the fallback's */ +export const forgetWidths = () => widths.clear() + /** * Width in px of a number in `font`: the sum of its characters' (digits and signs are not kerned), each measured once. * Undefined where there is no canvas. diff --git a/test/font-builder.ts b/test/font-builder.ts new file mode 100644 index 0000000..a1fdeb8 --- /dev/null +++ b/test/font-builder.ts @@ -0,0 +1,175 @@ +/** + * Writes a tiny TrueType font for tests: printable ASCII as one box glyph each, every one of them `advance` units wide + * (2 em by default: text in it is about four times as wide as in an ordinary font), the space `space` units wide. + * Valid as browsers check fonts (OpenType Sanitizer): head, hhea, maxp, OS/2, hmtx, cmap (format 4), loca, glyf, + * name, post, with their checksums. A few kilobytes, made when a test runs: no font file in the repository. + */ + +export interface TestFontOptions { + family: string + /** advance width of the printable characters, in units of 1000 per em */ + advance?: number + /** advance width of the space */ + space?: number +} + +const UPEM = 1000 +const ASCENT = 800 +const DESCENT = 200 +const FIRST = 0x20 +const LAST = 0x7e + +class Out { + b: number[] = [] + u8(v: number) { + this.b.push(v & 0xff) + return this + } + u16(v: number) { + return this.u8(v >> 8).u8(v) + } + i16(v: number) { + return this.u16(v < 0 ? v + 0x10000 : v) + } + u32(v: number) { + return this.u16((v >>> 16) & 0xffff).u16(v & 0xffff) + } + tag(s: string) { + for (const c of s) this.u8(c.charCodeAt(0)) + return this + } + zeros(n: number) { + for (let i = 0; i < n; i++) this.u8(0) + return this + } +} + +const table = (fill: (o: Out) => void) => { + const o = new Out() + fill(o) + return o.b +} + +function checksum(bytes: number[], from = 0, length = bytes.length): number { + let sum = 0 + for (let i = 0; i < length; i += 4) { + const at = from + i + sum = (sum + (((bytes[at] ?? 0) << 24) | ((bytes[at + 1] ?? 0) << 16) | ((bytes[at + 2] ?? 0) << 8) | (bytes[at + 3] ?? 0))) >>> 0 + } + return sum +} + +/** the font's bytes */ +export function testFont({ family, advance = 2000, space = 500 }: TestFontOptions): Uint8Array { + // glyph 0 .notdef (a box), 1 space (empty), then one box per printable character after the space + const count = LAST - FIRST + 2 + const box = table((o) => { + o.i16(1).i16(50).i16(0).i16(advance - 50).i16(ASCENT - 100) + o.u16(3).u16(0) + for (let i = 0; i < 4; i++) o.u8(1) + for (const dx of [50, 0, advance - 100, 0]) o.i16(dx) + for (const dy of [0, ASCENT - 100, 0, -(ASCENT - 100)]) o.i16(dy) + o.zeros(2) + }) + const glyphs = [box, [], ...Array.from({ length: count - 2 }, () => box)] + const glyf = glyphs.flat() + const loca = table((o) => { + let at = 0 + o.u16(0) + for (const g of glyphs) o.u16((at += g.length) / 2) + }) + const head = table((o) => { + o.u16(1).u16(0).u32(0x00010000).u32(0).u32(0x5f0f3cf5).u16(0x0003).u16(UPEM) + o.zeros(16) + o.i16(50).i16(0).i16(advance - 50).i16(ASCENT - 100) + o.u16(0).u16(8).i16(2).i16(0).i16(0) + }) + const hhea = table((o) => { + o.u16(1).u16(0).i16(ASCENT).i16(-DESCENT).i16(0).u16(Math.max(advance, space)) + o.i16(50).i16(50).i16(advance - 50).i16(1).i16(0).i16(0) + o.zeros(8).i16(0).u16(count) + }) + const maxp = table((o) => { + o.u32(0x00010000).u16(count).u16(4).u16(1).u16(0).u16(0).u16(2) + o.zeros(16) + }) + const os2 = table((o) => { + o.u16(4).i16(advance).u16(400).u16(5).u16(0) + o.i16(650).i16(600).i16(0).i16(75).i16(650).i16(600).i16(0).i16(350).i16(50).i16(300).i16(0) + o.zeros(10) + o.u32(1).u32(0).u32(0).u32(0) + o.tag('NONE').u16(0x0040).u16(FIRST).u16(LAST) + o.i16(ASCENT).i16(-DESCENT).i16(0).u16(ASCENT).u16(DESCENT) + o.u32(1).u32(0) + o.i16(500).i16(ASCENT - 100).u16(0).u16(0x20).u16(0) + }) + const hmtx = table((o) => { + o.u16(advance).i16(50) + o.u16(space).i16(0) + for (let i = 2; i < count; i++) o.u16(advance).i16(50) + }) + // format 4: the space and the printable characters as one segment (glyph = code - 0x1f), then the closing one + const cmap = table((o) => { + o.u16(0).u16(1).u16(3).u16(1).u32(12) + o.u16(4).u16(32).u16(0).u16(4).u16(4).u16(1).u16(0) + o.u16(LAST).u16(0xffff).u16(0) + o.u16(FIRST).u16(0xffff) + o.i16(1 - FIRST).i16(1) + o.u16(0).u16(0) + }) + const ps = family.replace(/[^A-Za-z0-9-]/g, '') || 'TestFont' + const names: [number, string][] = [ + [1, family], + [2, 'Regular'], + [3, `${ps}-test`], + [4, family], + [5, 'Version 1.000'], + [6, ps], + ] + const name = table((o) => { + o.u16(0).u16(names.length).u16(6 + 12 * names.length) + let at = 0 + for (const [id, text] of names) { + o.u16(3).u16(1).u16(0x0409).u16(id).u16(text.length * 2).u16(at) + at += text.length * 2 + } + for (const [, text] of names) for (const c of text) o.u16(c.charCodeAt(0)) + }) + const post = table((o) => { + o.u32(0x00030000).u32(0).i16(-100).i16(50).u32(0) + o.zeros(16) + }) + + const tables: [string, number[]][] = [ + ['OS/2', os2], + ['cmap', cmap], + ['glyf', glyf], + ['head', head], + ['hhea', hhea], + ['hmtx', hmtx], + ['loca', loca], + ['maxp', maxp], + ['name', name], + ['post', post], + ] + const n = tables.length + const sel = Math.floor(Math.log2(n)) + const out = new Out() + out.u32(0x00010000).u16(n).u16(16 << sel).u16(sel).u16(n * 16 - (16 << sel)) + let offset = 12 + 16 * n + const placed: { at: number; bytes: number[] }[] = [] + for (const [tag, bytes] of tables) { + out.tag(tag).u32(checksum(bytes)).u32(offset).u32(bytes.length) + placed.push({ at: offset, bytes }) + offset += (bytes.length + 3) & ~3 + } + for (const { bytes } of placed) { + out.b.push(...bytes) + out.zeros(((bytes.length + 3) & ~3) - bytes.length) + } + // head.checkSumAdjustment: the whole file sums to 0xB1B0AFBA + const headAt = placed[3].at + 8 + const adjust = (0xb1b0afba - checksum(out.b)) >>> 0 + for (let k = 0; k < 4; k++) out.b[headAt + k] = (adjust >>> (24 - 8 * k)) & 0xff + return Uint8Array.from(out.b) +} diff --git a/test/fonts.test.ts b/test/fonts.test.ts new file mode 100644 index 0000000..c1ab526 --- /dev/null +++ b/test/fonts.test.ts @@ -0,0 +1,278 @@ +/** + * The host's fonts: which declared files a document's text can use (through the fallbacks `cssFont()` names), the + * FontFace each becomes, and the page-wide set of them that viewers share (load, timeout, failure, release). + */ +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' +import type { Block, DocxMeta, Paragraph } from '../src/core/docx/types' +import { cssFont, cssUrl, docxFonts, docxPageFonts, faceDescriptors, faceFamily, familyKey, fontFamilies, fontsFor, type FontSource } from '../src/core/fonts' +import { acquireFont, copyFonts, FONT_LIMITS, fontKey, loadFont, onFonts, releaseFont, type HostFace } from '../src/react/common/fonts' +import { testFont } from './font-builder' + +describe('font stacks and declared fonts', () => { + test('the families of a stack, as CSS compares them', () => { + expect(fontFamilies(cssFont('Calibri')!)).toEqual(['calibri', 'carlito', 'sans-serif']) + expect(fontFamilies(cssFont('Times New Roman')!)).toEqual(['times new roman', 'liberation serif', 'tinos', 'serif']) + expect(fontFamilies(`'A, B',C , "Two Words" , serif`)).toEqual(['a, b', 'c', 'two words', 'serif']) + expect(fontFamilies('')).toEqual([]) + expect(familyKey(' "Liberation Sans" ')).toBe('liberation sans') + // ASCII case only, as CSS + expect(familyKey('ÉLAN Sans')).toBe('Élan sans') + }) + + const OFL: FontSource[] = [ + { family: 'Carlito', src: '/fonts/Carlito-Regular.ttf' }, + { family: 'Carlito', src: '/fonts/Carlito-Bold.ttf', weight: 700 }, + { family: 'Caladea', src: '/fonts/Caladea-Regular.ttf' }, + { family: 'Liberation Sans', src: '/fonts/LiberationSans-Regular.ttf' }, + { family: 'Liberation Serif', src: '/fonts/LiberationSerif-Regular.ttf' }, + { family: 'Liberation Mono', src: '/fonts/LiberationMono-Regular.ttf' }, + ] + const used = (...names: string[]) => fontsFor(OFL, names.map((n) => cssFont(n)!)).map((f) => f.src) + + test('a fallback declared serves the documents set in the original, through the font list', () => { + expect(used('Calibri')).toEqual(['/fonts/Carlito-Regular.ttf', '/fonts/Carlito-Bold.ttf']) + expect(used('Calibri Light')).toEqual(['/fonts/Carlito-Regular.ttf', '/fonts/Carlito-Bold.ttf']) + expect(used('Cambria')).toEqual(['/fonts/Caladea-Regular.ttf']) + expect(used('Arial')).toEqual(['/fonts/LiberationSans-Regular.ttf']) + expect(used('Helvetica')).toEqual(['/fonts/LiberationSans-Regular.ttf']) + expect(used('Times New Roman')).toEqual(['/fonts/LiberationSerif-Regular.ttf']) + expect(used('Courier New')).toEqual(['/fonts/LiberationMono-Regular.ttf']) + expect(used('Arial', 'Cambria')).toEqual(['/fonts/Caladea-Regular.ttf', '/fonts/LiberationSans-Regular.ttf']) + // nothing names them: nothing to load + expect(used('Georgia', 'Wingdings')).toEqual([]) + expect(fontsFor([], [cssFont('Calibri')!])).toEqual([]) + }) + + test('a font declared under the name documents use, or the fallback name in any case', () => { + expect(fontsFor([{ family: 'Calibri', src: 'c.woff2' }], [cssFont('Calibri')!])).toHaveLength(1) + expect(fontsFor([{ family: 'carlito', src: 'c.woff2' }], [cssFont('CALIBRI')!])).toHaveLength(1) + expect(fontsFor([{ family: '"Calibri"', src: 'c.woff2' }], [cssFont('Calibri')!])).toHaveLength(1) + // another document's font: not this one + expect(fontsFor([{ family: 'Calibri', src: 'c.woff2' }], [cssFont('Aptos')!])).toHaveLength(0) + }) + + test('descriptors: what is given, as CSS takes it', () => { + expect(faceDescriptors({})).toEqual({}) + expect(faceDescriptors({ weight: 700, style: 'italic' })).toEqual({ weight: '700', style: 'italic' }) + expect(faceDescriptors({ weight: '100 900', unicodeRange: 'U+0000-00FF, U+0131' })).toEqual({ weight: '100 900', unicodeRange: 'U+0000-00FF, U+0131' }) + expect(faceDescriptors({ weight: 'bold', style: '' })).toEqual({ weight: 'bold' }) + expect(faceFamily(' "Liberation Sans" ')).toBe('Liberation Sans') + expect(faceFamily('Evil"\\\nName')).toBe('EvilName') + }) + + test('a URL is quoted for url() without what would end it', () => { + expect(cssUrl('https://cdn.example/f.woff2')).toBe('url("https://cdn.example/f.woff2")') + expect(cssUrl('a") , url("b\\c\nd')).toBe('url("a\\22 ) , url(\\22 b\\5c c\\a d")') + }) + + test('the declarations of one face share a key; another file, weight or style does not', () => { + const bytes = new Uint8Array(4) + expect(fontKey({ family: 'Carlito', src: '/f.ttf' })).toBe(fontKey({ family: ' carlito ', src: '/f.ttf' })) + expect(fontKey({ family: 'Carlito', src: '/f.ttf' })).not.toBe(fontKey({ family: 'Carlito', src: '/g.ttf' })) + expect(fontKey({ family: 'Carlito', src: '/f.ttf' })).not.toBe(fontKey({ family: 'Carlito', src: '/f.ttf', weight: 700 })) + expect(fontKey({ family: 'Carlito', src: '/f.ttf', style: 'italic' })).not.toBe(fontKey({ family: 'Carlito', src: '/f.ttf' })) + expect(fontKey({ family: 'Carlito', src: bytes })).toBe(fontKey({ family: 'Carlito', src: bytes })) + expect(fontKey({ family: 'Carlito', src: bytes })).not.toBe(fontKey({ family: 'Carlito', src: new Uint8Array(4) })) + }) + + test('the stacks a DOCX shows: runs, markers, empty paragraphs, cells, text boxes, headers and footers', () => { + const p = (font: string | undefined, more: Partial = {}): Paragraph => ({ t: 'p', ps: {}, runs: [{ t: 'text', text: 'x', s: { font } }], ...more }) + const blocks: Block[] = [ + p('"A", sans-serif'), + p(undefined, { marker: { text: '•', s: { font: '"Symbol", sans-serif' }, tab: true }, markFont: '"Mark", serif' }), + { t: 'p', ps: {}, runs: [{ t: 'box', w: 10, h: 10, blocks: [p('"Boxed", serif')] }, { t: 'field', kind: 'PAGE', s: { font: '"Field", serif' } }] }, + { t: 'tbl', cols: [10], rows: [{ cells: [{ blocks: [p('"Cell", serif')], colSpan: 1, rowSpan: 1, borders: {}, pad: [0, 0, 0, 0] }] }] } as unknown as Block, + ] + expect([...docxFonts(blocks)].sort()).toEqual(['"A", sans-serif', '"Boxed", serif', '"Cell", serif', '"Field", serif', '"Mark", serif', '"Symbol", sans-serif']) + const meta = { page: { w: 1, h: 1, mt: 0, mr: 0, mb: 0, ml: 0, header: 0, footer: 0 }, font: '"Body", serif', header: [p('"Head", serif')], evenFooter: [p('"Foot", serif')] } as DocxMeta + expect([...docxPageFonts(meta)].sort()).toEqual(['"Body", serif', '"Foot", serif', '"Head", serif']) + }) +}) + +// ---- the page-wide set, with a FontFace and a font set that behave as a browser's + +class FakeFace { + static made: FakeFace[] = [] + status = 'unloaded' + settle?: { ok: () => void; fail: (e: Error) => void } + constructor( + readonly family: string, + readonly source: string | BufferSource, + readonly desc: FontFaceDescriptors, + ) { + if (desc.weight === 'heavy') throw new SyntaxError("Failed to construct 'FontFace': Failed to set 'heavy' as a property value.") + FakeFace.made.push(this) + } + load() { + this.status = 'loading' + return new Promise((ok, fail) => { + this.settle = { + ok: () => { + this.status = 'loaded' + ok(this) + }, + fail: (e) => { + this.status = 'error' + fail(e) + }, + } + }) + } +} + +describe('the fonts of the page', () => { + const set = new Set() + const warnings: string[] = [] + const saved = { document: globalThis.document, FontFace: (globalThis as { FontFace?: unknown }).FontFace, warn: console.warn, timeout: FONT_LIMITS.timeout } + const tick = (ms = 0) => new Promise((r) => setTimeout(r, ms)) + + beforeEach(() => { + set.clear() + warnings.length = 0 + FakeFace.made = [] + Object.assign(globalThis, { + FontFace: FakeFace, + document: { baseURI: 'https://host.example/app/', fonts: { add: (f: FakeFace) => set.add(f), delete: (f: FakeFace) => set.delete(f), addEventListener() {}, removeEventListener() {} } }, + }) + console.warn = (...a: unknown[]) => void warnings.push(a.map(String).join(' ')) + }) + afterEach(() => { + Object.assign(globalThis, { document: saved.document, FontFace: saved.FontFace }) + console.warn = saved.warn + FONT_LIMITS.timeout = saved.timeout + }) + + test('viewers declaring one file share one face; it leaves the page with the last of them', async () => { + const events: [string, boolean][] = [] + const off = onFonts((f, removed) => events.push([f.state, removed])) + const a = acquireFont({ family: 'Carlito', src: 'fonts/carlito.ttf' }) + const b = acquireFont({ family: 'carlito', src: 'fonts/carlito.ttf' }) + expect(b).toBe(a) + expect(FakeFace.made).toHaveLength(1) + // resolved against the page; nothing fetched yet (the face is not loaded until text needs it) + expect(FakeFace.made[0].source).toBe('url("https://host.example/app/fonts/carlito.ttf")') + expect(FakeFace.made[0].family).toBe('Carlito') + expect(FakeFace.made[0].status).toBe('unloaded') + expect([...set]).toEqual(FakeFace.made) + releaseFont(a) + await tick() + expect(set.size).toBe(1) + releaseFont(b) + await tick() + expect(set.size).toBe(0) + expect(events.at(-1)).toEqual(['idle', true]) + // let go and held again at once (an effect run twice): the same face, not a second download + const c = acquireFont({ family: 'Carlito', src: 'fonts/carlito.ttf' }) + releaseFont(c) + const d = acquireFont({ family: 'Carlito', src: 'fonts/carlito.ttf' }) + await tick() + expect(d).toBe(c) + expect(set.size).toBe(1) + releaseFont(d) + off() + }) + + test('loaded once, however many ask', async () => { + const f = acquireFont({ family: 'Carlito', src: '/c.ttf', weight: 700, style: 'italic' }) + expect(FakeFace.made[0].desc).toEqual({ weight: '700', style: 'italic' }) + loadFont(f) + loadFont(f) + expect(f.state).toBe('loading') + await tick() + FakeFace.made[0].settle!.ok() + await tick() + expect(f.state).toBe('loaded') + loadFont(f) + expect(f.state).toBe('loaded') + releaseFont(f) + }) + + test('a slow font times out and arrives later; a failing one says which and why', async () => { + FONT_LIMITS.timeout = 20 + const slow = acquireFont({ family: 'Slow', src: '/slow.ttf' }) + loadFont(slow) + await tick(40) + expect(slow.state).toBe('timeout') + expect(warnings.join('\n')).toMatch(/^\[office-engine\] font "Slow" \(\/slow\.ttf\) is still loading after 0\.02 s/) + FakeFace.made[0].settle!.ok() + await tick() + expect(slow.state).toBe('loaded') + + const bad = acquireFont({ family: 'Bad', src: '/missing.ttf' }) + loadFont(bad) + await tick() + FakeFace.made[1].settle!.fail(new Error('A network error occurred.')) + await tick() + expect(bad.state).toBe('error') + expect(bad.error?.message).toBe('Font "Bad" (/missing.ttf) could not be loaded: A network error occurred.') + expect(warnings.at(-1)).toStartWith('[office-engine] Font "Bad" (/missing.ttf) could not be loaded') + releaseFont(slow) + releaseFont(bad) + }) + + test('the print frame gets the loaded faces of its own, from the same file', async () => { + const f = acquireFont({ family: 'Carlito', src: '/c.ttf', weight: 700 }) + const idle = acquireFont({ family: 'Caladea', src: '/d.ttf' }) + loadFont(f) + await tick() + FakeFace.made[0].settle!.ok() + await tick() + const added: FakeFace[] = [] + const frame = { defaultView: { FontFace: FakeFace }, fonts: { add: (x: FakeFace) => added.push(x) } } as unknown as Document + copyFonts(frame) + // the loaded one only, as a face of the frame's (and loading there) + expect(added).toHaveLength(1) + expect(added[0]).not.toBe(FakeFace.made[0]) + expect([added[0].family, added[0].source, added[0].desc, added[0].status]).toEqual(['Carlito', 'url("https://host.example/c.ttf")', { weight: '700' }, 'loading']) + releaseFont(f) + releaseFont(idle) + }) + + test('descriptors a FontFace refuses: a failed font, not an exception', () => { + const f: HostFace = acquireFont({ family: 'Heavy', src: '/h.ttf', weight: 'heavy' }) + expect(f.state).toBe('error') + expect(f.face).toBeUndefined() + loadFont(f) + expect(f.state).toBe('error') + releaseFont(f) + }) + + test('bytes: a Blob is read when first needed, a buffer is used as it is', async () => { + const bytes = testFont({ family: 'Wide Test' }) + const blob = acquireFont({ family: 'Wide Test', src: new Blob([bytes]) }) + expect(FakeFace.made).toHaveLength(0) + loadFont(blob) + await tick(10) + expect(FakeFace.made).toHaveLength(1) + expect(new Uint8Array(FakeFace.made[0].source as ArrayBuffer)).toEqual(bytes) + FakeFace.made[0].settle!.ok() + await tick() + expect(blob.state).toBe('loaded') + const view = acquireFont({ family: 'Wide Test', src: bytes, weight: 700 }) + expect(FakeFace.made[1].source).toBe(bytes) + releaseFont(blob) + releaseFont(view) + }) +}) + +describe('the test font', () => { + test('a TrueType file with its tables in order and checksums that add up', () => { + const f = testFont({ family: 'Wide Test' }) + const dv = new DataView(f.buffer) + expect(dv.getUint32(0)).toBe(0x00010000) + const n = dv.getUint16(4) + const tags: string[] = [] + let sum = 0 + for (let i = 0; i < f.length; i += 4) sum = (sum + dv.getUint32(i)) >>> 0 + for (let i = 0; i < n; i++) { + const at = 12 + 16 * i + tags.push(String.fromCharCode(...f.subarray(at, at + 4))) + const off = dv.getUint32(at + 8) + expect(off % 4).toBe(0) + expect(off + dv.getUint32(at + 12)).toBeLessThanOrEqual(f.length) + } + expect(tags).toEqual(['OS/2', 'cmap', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'name', 'post']) + expect(sum).toBe(0xb1b0afba) + expect(f.length).toBeLessThan(8000) + }) +}) diff --git a/test/probes.ts b/test/probes.ts index c190ebd..5fa2f44 100644 --- a/test/probes.ts +++ b/test/probes.ts @@ -176,6 +176,33 @@ export function scriptLinkDocx(): Promise { }) } +/** a font name a CSS parser would read as a list with a url() in it, if it were not quoted */ +export const CSS_FONT_NAME = `Probe"), url(https://${PROBE_HOST}/css-font.woff2), local("x` + +/** + * Fonts in a document: `word/fontTable.xml` embeds "Probe Font" (one file at an address outside the package, one + * inside it), and runs are set in it and in a name that looks like CSS. + */ +export async function probeFontsDocx(): Promise { + const zip = await ZipArchive.open(new BlobSource(new Blob([await Bun.file(`${SAMPLES}/sample.docx`).arrayBuffer()]))) + const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' + const key = 'w:fontKey="{00000000-0000-0000-0000-000000000000}"' + const attr = (s: string) => s.replace(/&/g, '&').replace(/"/g, '"').replace(/ `${text}` + const changes = new Map([ + ['word/fontTable.xml', before(await zip.text('word/fontTable.xml'), '', ``)], + [ + 'word/_rels/fontTable.xml.rels', + `` + + `` + + ``, + ], + ['word/fonts/font2.odttf', 'not a font'], + ['word/document.xml', after(await zip.text('word/document.xml'), '', run('Probe Font', 'probe font') + run(CSS_FONT_NAME, 'probe css font'))], + ]) + return rebuildZip(zip, changes) +} + // ---- Visio drawings: built whole (test/visio-builder.ts), shown as slides const blobOf = async (bytes: Promise>) => new Blob([await bytes]) diff --git a/test/security.test.ts b/test/security.test.ts index 492c1b4..e79e822 100644 --- a/test/security.test.ts +++ b/test/security.test.ts @@ -5,7 +5,8 @@ */ import { afterEach, describe, expect, test } from 'bun:test' import { colorOf, cssColor, cssPaint, customIndexed, drawingColor, fileHex, officeHex, sheetColor } from '../src/core/color' -import { cssFont } from '../src/core/fonts' +import { cssFont, docxFonts, familyKey, fontFamilies, fontsFor } from '../src/core/fonts' +import type { Block } from '../src/core/docx/types' import { parseRaw } from '../src/core/raw' import { BlobSource, BufferSource } from '../src/core/source' import { parseTheme } from '../src/core/theme' @@ -18,7 +19,7 @@ import { isSharedCall } from '../src/collab/docx' import { CollabSession, type Adapter } from '../src/collab/session' import { MemoryTransport } from '../src/collab/transport' import { PEER_COLORS } from '../src/collab/protocol' -import { INHERITED, inheritedDocx, inheritedPptx, inheritedVisio, inheritedXlsx, PROBE_HOST, probeDocx, probePptx, probeVisio, probeXlsx } from './probes' +import { CSS_FONT_NAME, INHERITED, inheritedDocx, inheritedPptx, inheritedVisio, inheritedXlsx, PROBE_HOST, probeDocx, probeFontsDocx, probePptx, probeVisio, probeXlsx } from './probes' const S = (n: string) => `${import.meta.dir}/../playground/public/samples/${n}` const URL_COLOR = 'fff url(https://probe.invalid/x.png)' @@ -155,6 +156,21 @@ describe('colors given to CSS', () => { }) } + test('docx: the fonts a file embeds or names never reach the views as something to fetch', async () => { + const all = await models(await probeFontsDocx()) + const json = JSON.stringify(all) + // embedded fonts (word/fontTable.xml) are not read: neither their address nor their part is handed on + expect(json).not.toContain('embedded-font') + expect(json).not.toContain('odttf') + // a name that looks like CSS is one quoted family of its stack, followed by the generic one + const stacks = new Set() + docxFonts(all.filter(Array.isArray).flat() as Block[], stacks) + const css = [...stacks].find((s) => s.includes('css-font'))! + expect(css).toBe(cssFont(CSS_FONT_NAME)!) + expect(fontFamilies(css)).toEqual([familyKey(CSS_FONT_NAME.replace(/"/g, '')), 'sans-serif']) + expect(fontsFor([{ family: 'Probe Font', src: '/host.ttf' }], stacks)).toHaveLength(1) + }) + test('an edit writes a color into the file only when it is one', async () => { const bad = '#fff"/>