Repository navigation
feat: the host supplies font files, and documents are laid out once they are loaded - #39
Open
pierreneter wants to merge 2 commits into
Open
pierreneter wants to merge 2 commits into
pierreneter wants to merge 2 commits into
Conversation
…hey are loaded DocViewer's `fonts` (and the `fonts` attribute / property of <office-viewer>): font files by URL or as bytes, under a document's own font name or the metric-compatible fallback cssFont() names after it (Carlito for Calibri, Caladea, Liberation). They are added to the page's fonts with the FontFace API, shared by the viewers that declare them, and fetched only when a document's text is set in their family. The DOCX view measures blocks once the fonts they use are loaded (a font that fails is reported to onError, one slower than 3 s is not waited for), and measures them again when a font arrives late, the page's own web fonts included, keeping the block in view and the selection. XLSX fits its numbers again; printing uses the same files. Fonts are never taken from a document. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #30: plan item 4, fonts. Items 1 to 3 (splitting blocks across pages, keep rules) are a separate branch, and the
exactlayout mode is later work.The host can give the viewer font files. The views load them with
FontFace, and the DOCX view measures and paginates a block only once the fonts it uses are ready, or have failed or timed out.API
The new
fontsprop onDocViewertakes a list ofFontSource:srcis a URL string, anArrayBuffer, anArrayBufferViewor aBlob. All of these can be sent to a worker, so the same list can later be handed to the worker for theexactmode.fontsas an attribute (a JSON list of fonts by URL) or as a property (bytes too).FontSourceis exported from the package root and fromcore.docs/api.mdhas an example with the OFL fallbacks: Carlito, Caladea, and Liberation Sans, Serif and Mono.How fonts are matched
Matching uses the stacks
cssFont()already builds:Carlitoserves documents in Calibri, but only where Calibri is not installed.Calibri) also works, and then it wins over an installed Calibri.Loading
document.fontswhen no viewer declares it any more.<style>injection.[office-engine]warning, and a load error also callsonError.@font-face(loadingdone), which today leaves stale heights.XLSX and PPTX
XLSX: when the declared fonts named by the styles are ready, or a late font arrives, the cache of digit widths is cleared and numbers are fitted to their columns again. The grid does not wait for fonts before it first paints.
PPTX: unchanged. The browser lays slide text out again when the font arrives.
Security
Font URLs come only from the host. A crafted DOCX with embedded-font references to an external URL, an internal
.odttfand a font name that looks likeurl()causes no request, and the name stays one quoted family name. This is covered ine2e/security.spec.tsandtest/security.test.ts.Tests
test/fonts.test.ts(14 tests):FontFace: sharing, release, timeout then late arrival, errors,Blobversus buffer.test/font-builder.tsbuilds a TrueType font of a few KB at test time, with very wide glyphs, so no binary is committed.e2e/fonts.spec.ts(10 tests in Chromium):Carlitodeclaration is fetched for a Calibri document;onErrorand one warning;@font-facefrom the page triggers a re-measure;Also new: the embed attribute and property in
e2e/embed.spec.ts.On
main, both late@font-facetests fail with stale layout. The tests that use the new prop fail there too.Results
In the Linux test VM with Bun 1.4.4.
bun testThen these e2e specs, all with 0 fail:
e2e/fonts.spec.tse2e/docx.spec.tse2e/docx-page.spec.tse2e/viewer.spec.tse2e/embed.spec.tse2e/sdk.spec.tse2e/security.spec.tse2e/xlsx.spec.tse2e/pptx.spec.tse2e/docx-hf.spec.tse2e/docx-qa2.spec.tse2e/docx-table.spec.tse2e/docx-track.spec.tse2e/file-menu.spec.tse2e/comments.spec.tsAfter the rebase onto
af19290, which only changespackage.json, I re-ran typecheck and the font unit tests.Not done
fontTable.xmlembeds nor PPTXembeddedFontLst.DocxViewandXlsxViewused withoutDocViewerget no host fonts. They still measure again when the page's own fonts arrive late.@font-facefonts are still absent there, as before.Merging with items 1 to 3
The
DocxView.tsxchanges are in one place:fontsReadygate on the measuring container;paginate.tsandrender.tsxare not touched. The scroll anchor'sblockTopsums block heights from the page's start; once paragraphs are split into fragments it has to account for them.🤖 Generated with Claude Code