From 05038443be0229af9ccaf0d2845a6580dc13614c Mon Sep 17 00:00:00 2001 From: Chizoba Jude Ojini Date: Sat, 7 Nov 2026 17:18:41 +0000 Subject: [PATCH 1/2] Add Word to PDF (.docx) Headers, footers and page numbers, clickable links, heading bookmarks, and widow, keep-lines and keep-with-next control. PdfDrawing gains links and bookmarks; the Word preview and the new tool share one converter. --- .github/pull_request_template.md | 76 +++ AGENTS.md | 14 +- apps/web/src/Landing.tsx | 2 +- apps/web/src/Marketing.tsx | 77 ++- packages/office-core/src/docx-model.ts | 46 +- packages/office-core/src/docx-read.ts | 273 ++++++++-- packages/office-core/src/fonts-used.ts | 16 +- packages/office-core/src/index.ts | 2 +- packages/office-core/src/ooxml.ts | 6 +- packages/office-core/test/docx-helper.ts | 70 +++ packages/office-core/test/docx-read.test.ts | 96 +++- packages/pdf-core/src/build.ts | 88 +++- packages/pdf-core/test/build.test.ts | 26 +- packages/tools/src/docx-preview/layout.ts | 156 ------ packages/tools/src/index.ts | 2 + packages/tools/src/m7b-tools.test.ts | 282 +++++++++- packages/tools/src/office-preview.ts | 32 +- packages/tools/src/panels.ts | 1 + packages/tools/src/powerpoint-to-pdf/text.ts | 67 ++- packages/tools/src/runners.ts | 1 + packages/tools/src/seo/convert.ts | 11 + packages/tools/src/word-to-pdf/Panel.tsx | 18 + packages/tools/src/word-to-pdf/layout.ts | 520 +++++++++++++++++++ packages/tools/src/word-to-pdf/manifest.ts | 15 + packages/tools/src/word-to-pdf/run.ts | 55 ++ 25 files changed, 1648 insertions(+), 304 deletions(-) create mode 100644 .github/pull_request_template.md create mode 100644 packages/office-core/test/docx-helper.ts delete mode 100644 packages/tools/src/docx-preview/layout.ts create mode 100644 packages/tools/src/word-to-pdf/Panel.tsx create mode 100644 packages/tools/src/word-to-pdf/layout.ts create mode 100644 packages/tools/src/word-to-pdf/manifest.ts create mode 100644 packages/tools/src/word-to-pdf/run.ts diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..85bc74b --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,76 @@ +## Description + + + +## Type of Change + + + +- [ ] 🐛 Bug fix (non-breaking change which fixes an issue) +- [ ] ✨ New feature (non-breaking change which adds functionality) +- [ ] 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] 📝 Documentation update +- [ ] 🎨 Style/UI update (no functional changes) +- [ ] ♻️ Code refactoring (no functional changes) +- [ ] ⚡ Performance improvement +- [ ] ✅ Test update +- [ ] 🔧 Configuration change +- [ ] 🏗️ Build/CI update + +## Related Issues + + + +Closes # +Related to # + +## Changes Made + + + +- +- +- + +## Screenshots (if applicable) + + + +## Testing + + + +### Unit Tests + +- [ ] All existing unit tests pass +- [ ] Added new unit tests for new functionality + +### E2E Tests + +- [ ] All existing e2e tests pass +- [ ] Added new e2e tests for new functionality + +### Manual Testing + + + +1. +2. +3. + +## Checklist + + + +- [ ] My code follows the project's style guidelines +- [ ] I have performed a self-review of my code +- [ ] I have commented my code, particularly in hard-to-understand areas +- [ ] I have made corresponding changes to the documentation +- [ ] My changes generate no new warnings or errors +- [ ] I have added tests that prove my fix is effective or that my feature works +- [ ] New and existing unit tests pass locally with my changes +- [ ] Any dependent changes have been merged and published + +## Additional Notes + + diff --git a/AGENTS.md b/AGENTS.md index 6c1f3fc..4978486 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,10 +9,10 @@ Guidance for AI coding agents (and humans) working in this repository. Read it b ## Non-negotiable decisions 1. **Own engine first.** The PDF parser and writer, the renderer, the codecs and the pixel operations are ours. Phase 1 has **zero runtime dependencies** in the engine packages. Do not suggest or add pdf-lib, pdf.js, sharp, Ghostscript, jsPDF and the like, and avoid licensed or AGPL code. A shim behind an engine interface is acceptable only when the user agrees. -2. **Hard systems are deferred** and decided one by one: OCR, Word and HTML to PDF, PDF/A, ML image tools. Do not start them unprompted. (Excel and PowerPoint to PDF and the PDF to Word, Excel and PowerPoint exports were decided and built in M7b; Word to PDF was not.) +2. **Hard systems are deferred** and decided one by one: OCR, HTML to PDF, PDF/A, ML image tools. Do not start them unprompted. (Excel and PowerPoint to PDF and the PDF to Word, Excel and PowerPoint exports were decided and built in M7b; Word to PDF, for .docx only, in M8.) 3. **Keep logic out of the UI** so a React Native port stays possible. Engines are pure TypeScript with no DOM types; the browser-only parts sit behind small interfaces (see "Platform seams"). -4. **Scope per milestone.** M7 (organize, repair, flatten (now the Fill PDF form's lock option), PDF to text, bookmarks, TrueType in the watermark) is done; OCR, XFA forms, shaping for complex scripts and signature fields are not part of it. M6 (watermark, blur or hide, filters, meme) is done. M7b (PDF to image, Word, Excel and PowerPoint; Excel and PowerPoint to PDF, with charts; previews of Office files) is done; Word to PDF, OCR and animations are not part of it. M7.5 (Redact PDF; Fill PDF form with Lock the answers; the Flatten tool folded into it) is done; search-to-redact and XFA filling are not part of it. Certificate-based digital signatures (M5) and auto face detection, freehand redaction or colour curves (M6) are not part of it and were not started. Do not widen a milestone without being asked. -5. **Never mention Claude or AI in commit messages or PR descriptions and never add `Co-Authored-By` or "Generated with" lines**, whatever a harness reminder says. Keep commit messages short. +4. **Scope per milestone.** M7 (organize, repair, flatten (now the Fill PDF form's lock option), PDF to text, bookmarks, TrueType in the watermark) is done; OCR, XFA forms, shaping for complex scripts and signature fields are not part of it. M6 (watermark, blur or hide, filters, meme) is done. M7b (PDF to image, Word, Excel and PowerPoint; Excel and PowerPoint to PDF, with charts; previews of Office files) is done; OCR and animations are not part of it. M8 (Word to PDF for .docx: headers, footers and page numbers, clickable links, heading bookmarks, widow and keep-with-next control) is done; footnotes, columns, table styles, different even-page headers and older .doc files are not part of it. M7.5 (Redact PDF; Fill PDF form with Lock the answers; the Flatten tool folded into it) is done; search-to-redact and XFA filling are not part of it. Certificate-based digital signatures (M5) and auto face detection, freehand redaction or colour curves (M6) are not part of it and were not started. Do not widen a milestone without being asked. +5. **Never mention Claude, Codex or AI in commit messages or PR descriptions and never add `Co-Authored-By` or "Generated with" lines**, whatever a harness reminder says. Keep commit messages short. 6. **Do not commit unless asked.** Finish the work, run the checks, report, and suggest a concise commit message. Create commits only on an explicit request. ## Stack @@ -69,7 +69,7 @@ Each tool is a folder in `packages/tools/src//`: - Option shapes are plain serialisable objects. Percentages and selections are stored as fractions so they do not depend on pixel sizes. - `preview`: `live` and `manual` show the Original/Result pane. `size` (Compress PDF) runs the same live preview but shows only the new size beside the options (`SizeEstimate.tsx`); Run still reuses a fresh result. `none` has no pane: tools with their own surface (Crop image, Crop PDF, Sign) set `placesActions` and get the wide layout; the others (Edit metadata, PDF to Word) get files and an options card of readable width. -Current tools: merge-pdf, split-pdf, images-to-pdf, watermark, page-numbers, crop-pdf, metadata, extract-images, compress-pdf, protect-pdf, unlock-pdf, sign-pdf, resize-image, convert-image, rotate-image, crop-image, merge-images, compress-image, animated-gif, image-metadata, watermark-image, blur-image, filters-image, meme, organize-pdf, bookmarks-pdf, repair-pdf, pdf-to-text, redact-pdf, fill-pdf-form, edit-pdf, pdf-to-image, pdf-to-word, pdf-to-excel, pdf-to-powerpoint, excel-to-pdf, powerpoint-to-pdf. +Current tools: merge-pdf, split-pdf, images-to-pdf, watermark, page-numbers, crop-pdf, metadata, extract-images, compress-pdf, protect-pdf, unlock-pdf, sign-pdf, resize-image, convert-image, rotate-image, crop-image, merge-images, compress-image, animated-gif, image-metadata, watermark-image, blur-image, filters-image, meme, organize-pdf, bookmarks-pdf, repair-pdf, pdf-to-text, redact-pdf, fill-pdf-form, edit-pdf, pdf-to-image, pdf-to-word, pdf-to-excel, pdf-to-powerpoint, word-to-pdf, excel-to-pdf, powerpoint-to-pdf. To add a tool: manifest, run, panel, three registry entries, its page text in `packages/tools/src/seo/` (`seo.test.ts` fails without it, and checks the lengths), and tests. Reuse the helpers above. @@ -92,7 +92,7 @@ To add a tool: manifest, run, panel, three registry entries, its page text in `p - Coordinates: stamps and crops are specified in the page **as displayed** (origin bottom-left, y up, `/Rotate` and crop box applied) and converted to user space inside the engine. Keep it that way and test rotated pages. - `encryptPdf(doc, { userPassword, ownerPassword?, method?, permissions? })` (`encrypt.ts`) writes AES-256 (R6) or AES-128 (R4) protection; it reuses the password hashing in `security.ts`. Verify any change against an independent reader (pypdf in a throwaway venv: `python3 -m venv /tmp/venv && /tmp/venv/bin/pip install pypdf cryptography`). - `stamp` can draw pictures: an `Overlay` may carry `images` made by `prepareImage` (compressed once, stored once per file however many pages use it). -- `build.ts` makes a PDF from scratch for the converters: a `PdfDrawing` of pages (`addPage(w, h)` -> `PageDraw` with `text`, `fillRect`, `line`, `path`, `clip`, `rotate`, `image`, in points from the top left, y down), `transform`, `save`/`restore`, `clipPath`; paths take `PathStyle` fills that can be gradients (shadings) and alphas), registered pictures (`ImageInput`, a JPEG goes in as it is) and `buildPdf(drawing)`. Text is the standard Helvetica, Times or Courier (`FontSpec.scale` squeezes the glyphs to stand in for a narrower font; `measure` uses the same scale) or a `FontFace` (`FontSpec.face`: one or more TrueType parts, such as a font's latin and cyrillic subsets); each string is split by the part that has each letter, falling back to the standard font, and every part is subset to what was drawn with it. Letters nothing has become `?` and are returned in `lost`. Tc and Tz are written on every text run because they carry over between text objects. Times widths are estimated from Helvetica's. +- `build.ts` makes a PDF from scratch for the converters: a `PdfDrawing` of pages (`addPage(w, h)` -> `PageDraw` with `text`, `fillRect`, `line`, `path`, `clip`, `rotate`, `image`, `link` (a clickable box; web and mail addresses only, anything else is dropped), in points from the top left, y down), `transform`, `save`/`restore`, `clipPath`; paths take `PathStyle` fills that can be gradients (shadings) and alphas), registered pictures (`ImageInput`, a JPEG goes in as it is), bookmarks (`PdfDrawing.outline`, an `OutlineItem` tree written with `writeOutline`) and `buildPdf(drawing)`. Text is the standard Helvetica, Times or Courier (`FontSpec.scale` squeezes the glyphs to stand in for a narrower font; `measure` uses the same scale) or a `FontFace` (`FontSpec.face`: one or more TrueType parts, such as a font's latin and cyrillic subsets); each string is split by the part that has each letter, falling back to the standard font, and every part is subset to what was drawn with it. Letters nothing has become `?` and are returned in `lost`. Tc and Tz are written on every text run because they carry over between text objects. Times widths are estimated from Helvetica's. - Errors the user can act on extend `UserError` (`PdfParseError`, `PdfPasswordError`, `PdfRangeError`, `PdfEncryptedError`). ## Renderer notes (`pdf-render`) @@ -116,8 +116,8 @@ To add a tool: manifest, run, panel, three registry entries, its page text in `p ## Office files (`office-core`) - `.xlsx`, `.docx` and `.pptx` are zips of XML. `unzip` / `zipPacked` live in `core` (the reader is bounded: entry count, sizes, a stream stops at its declared size; encrypted and ZIP64 are refused). `parseXml` drops namespace prefixes (`a:t` and `w:t` both read as `t`) and refuses DOCTYPE. -- Plain data models: `Sheet`/`Cell`/`Style` (`xlsx-model.ts`), `DocModel` (`docx-model.ts`), `Deck`/`Slide`/`SlideShape` (`pptx-model.ts`). `writeXlsx`, `writeDocx`, `writePptx` build the files; `readXlsx` (cells with their display text from `formatValue`, looks, widths, merges) and `readPptx` (placeholders resolved through layout and master, text styles by level, theme colours with tints, group transforms, table styles, backgrounds) read them. `tools/src/excel-to-pdf` and `powerpoint-to-pdf` lay the models out onto a `PdfDrawing`; `pdf-to-word`, `pdf-to-powerpoint` and `pdf-to-excel` build the models from `pageBlocks` / `findTables`. -- `readDocx` (`docx-read.ts`) reads paragraphs, runs, styles (`basedOn`, defaults), numbering markers, tables, inline and anchored pictures, text boxes, sections and page breaks; it feeds the Word preview only (`tools/src/docx-preview`), there is no Word to PDF tool. `readPptx` returns charts as `SlideChart` and `readXlsx` puts `pictures`, `charts` and `page` (the page setup, print area, title rows; `saved` is false when the sheet only has the margins every file has) on each sheet. `readChart` reads the cached values, or with a `ChartLookup` the cells a reference names (openpyxl saves no cache). +- Plain data models: `Sheet`/`Cell`/`Style` (`xlsx-model.ts`), `DocModel` (`docx-model.ts`), `Deck`/`Slide`/`SlideShape` (`pptx-model.ts`). `writeXlsx`, `writeDocx`, `writePptx` build the files; `readXlsx` (cells with their display text from `formatValue`, looks, widths, merges) and `readPptx` (placeholders resolved through layout and master, text styles by level, theme colours with tints, group transforms, table styles, backgrounds) read them. `tools/src/word-to-pdf`, `excel-to-pdf` and `powerpoint-to-pdf` lay the models out onto a `PdfDrawing`; `pdf-to-word`, `pdf-to-powerpoint` and `pdf-to-excel` build the models from `pageBlocks` / `findTables`. +- `readDocx` (`docx-read.ts`) reads paragraphs, runs, styles (`basedOn`, defaults), numbering markers, tables, inline and anchored pictures, text boxes, sections and page breaks, and, for Word to PDF: headers and footers (default and first page, a section that names none takes the one before; their own relationships for pictures), page number fields (`Run.field`, both `fldSimple` and `fldChar` forms; the saved number is replaced when the page is drawn), tab stops and alignment tabs (`Paragraph.tabs`, used by headers and footers only), external hyperlinks (`Run.link`, also `HYPERLINK` fields; a link to a bookmark has no address), heading levels (`Paragraph.heading`, from the outline level or a "heading N" style) and `keepNext`/`keepLines`/`widowControl`, how a placed picture sits (`Picture.at` or `align`, what it counts from, `wrap` square or top and bottom, `wrapText`, `dist`) and the fonts the file carries (`DocModel.fonts`: Google Docs stores the ones it uses; Word scrambles the first 32 bytes with the GUID, a zero key means plain). It feeds Word to PDF and the Word preview (`tools/src/word-to-pdf`, the same code: `docxToPdf` in its `run.ts`; `layout.ts` holds the layout). Layout: every paragraph is measured before it is placed, so a page break never leaves one line alone at either side (widow and orphan control, on unless the paragraph turns it off), `keepLines` moves a paragraph whole, `keepNext` keeps a chain of paragraphs with the first lines of what follows, and a tall header or footer pushes the body in. Inline pictures are runs of their paragraph (`Run.picture`, text empty): a piece of the line as wide as the picture (shrunk to the column, and to a page's height), standing on the baseline with the text's descent below it (`Line.base`), so the paragraph's alignment, spacing and neighbours apply; `picturesOf` lists every picture for decoding. A picture's crop (`Picture.crop`, shares cut from each side, negative = room added: Google Docs' thin divider lines are a 4 px picture in a 15 pt box) is drawn by `drawPicture`: the whole picture larger or smaller, clipped to the box. Table cells read `tcMar`/`tblCellMar` (`pad`) and `vAlign`. A file may hold any number: `tw`/`emu` in `docx-read.ts` turn a non-number into 0 and cap lengths at 100 000 pt, and the paragraph loop in `layout.ts` always moves on (`moved`: a line that was sent to a new page is drawn where it is the second time), because a header can't start a new page and a `NaN` once made it loop for ever. Check changes to the reader or layout by converting a few hundred damaged copies of real documents (random attribute values, deleted elements, truncated XML, repeated paragraphs): each must give a PDF or a `UserError` within seconds. Placed pictures: one anchored in a paragraph counts "from the paragraph" from the top of its text (below the space above it), is kept clear of by the lines that follow (`Flow.box`: the widest piece of the line the pictures leave, else the line goes below them; `layoutText`'s optional `slot` and `Line.gap` do it), and is drawn under the text once the paragraph's page is settled (a paragraph that moves to a new page is laid out again there). Fonts the file carries come after the ones the user chose (`InstalledFont`), and `usedFonts` leaves their families out of the panel. Headers and footers are drawn last, when the page count is known; their tabs put text at the stops (`Flow.tabbed`: one line, the stop for each tab is the next in order). Run positions come back from `drawText`'s optional `onRun` and become `PageDraw.link` boxes; headings become `PdfDrawing.outline`. Not read: footnotes, columns, different even-page headers, table styles, restarted page numbers, internal links. Tests build small documents from XML strings (`office-core/test/docx-helper.ts`). `readPptx` returns charts as `SlideChart` and `readXlsx` puts `pictures`, `charts` and `page` (the page setup, print area, title rows; `saved` is false when the sheet only has the margins every file has) on each sheet. `readChart` reads the cached values, or with a `ChartLookup` the cells a reference names (openpyxl saves no cache). - Fonts: `tools/src/fonts/` is a seam like `PageRaster`. `FontBook(source, installed, uploaded)` loads faces for the text a file needs (`load(needs)`, then `spec(name, bold, italic)` for the builder): an installed font the panel found (`localFonts` option, from `window.queryLocalFonts()` in `InstalledFonts.tsx`), else a bundled look-alike with the same widths (`@fontsource` Carlito, Caladea, Arimo, Tinos, Cousine; WOFF read by `woffToSfnt`), else a standard font with `widthScale` from `office-fonts.ts`. `browserFonts` imports the WOFF files lazily through Vite; tests pass `nodeFonts`. Office runs take the source as their last parameter. - Fonts panel (`fonts/FontsPanel.tsx`): lists `usedFonts`, uses installed fonts automatically when `local-fonts` is granted (close names through `closestFamily` in `fonts/match.ts`, embeddability checked in the worker with `readFontFile` -> `fontFaces`), adds font files (several, matched by name) and downloads free ones. Everything goes into the `localFonts` option as `InstalledFont` with `for` (the family it stands for) and `source`. `FontBook` picks by `for`, the slant and the nearest weight (no more than 150 away), several files of one style become one face. The builder writes `/ReamFamily` (the family the document asked for) on each embedded font, one resource per look-alike part and asked family; the renderer's `faceOf` reads it back, else the embedded font's family name, and maps Ream's look-alikes to their Office originals. - PDF to PowerPoint uses `placedBlocks` (`pdf-render/src/placed.ts`), not `pageBlocks`: pieces cut at 1.5 em gaps, stacked by spacing, size and a shared edge, ruled lines between rows keep cells apart, lines kept with `breaks` (paragraph runs joined by "\n", box wrap off). `toLines` follows drawing order into runs, then joins runs on one baseline (within 0.15 em); never link characters by height across the page again (a big title chained the lines beside it and mixed their letters). diff --git a/apps/web/src/Landing.tsx b/apps/web/src/Landing.tsx index 2805e08..28379ed 100644 --- a/apps/web/src/Landing.tsx +++ b/apps/web/src/Landing.tsx @@ -129,7 +129,7 @@ export function Landing() { Sheet 03

- Open your browser's developer tools and watch the Network tab while a tool runs. Your file never appears there. The one request Ream can make is Download free fonts in the PDF tool (shown in Edit PDF, Excel to PDF and PowerPoint to PDF), which sends font names only, on your click. + Open your browser's developer tools and watch the Network tab while a tool runs. Your file never appears there. The one request Ream can make is Download free fonts in the PDF tool (shown in Edit PDF, Word to PDF, Excel to PDF and PowerPoint to PDF), which sends font names only, on your click.

diff --git a/apps/web/src/Marketing.tsx b/apps/web/src/Marketing.tsx index a5af840..d52b92b 100644 --- a/apps/web/src/Marketing.tsx +++ b/apps/web/src/Marketing.tsx @@ -1,16 +1,17 @@ -import type { ReactNode } from 'react' -import { Logo } from '@ream/ui' -import { ThemeToggle } from './ThemeToggle' -import { focus } from './TopBar' -import { Link } from './router' +import type { ReactNode } from "react"; +import { Logo } from "@ream/ui"; +import { ThemeToggle } from "./ThemeToggle"; +import { focus } from "./TopBar"; +import { Link } from "./router"; -const action = `inline-flex min-h-11 items-center justify-center px-5 font-heading text-[17px] font-semibold tracking-wide transition-colors duration-150 ${focus}` +const action = `inline-flex min-h-11 items-center justify-center px-5 font-heading text-[17px] font-semibold tracking-wide transition-colors duration-150 ${focus}`; /** the one solid object on a page: an accent fill wearing the corner marks */ -export const primary = `${action} blueprint border border-accent bg-accent text-on-accent hover:border-accent-hover hover:bg-accent-hover` -export const secondary = `${action} border border-line hover:bg-ink/7` +export const primary = `${action} blueprint border border-accent bg-accent text-on-accent hover:border-accent-hover hover:bg-accent-hover`; +export const secondary = `${action} border border-line hover:bg-ink/7`; /** a section's label: small caps in the deep steel, sitting over a hairline */ -export const kicker = 'block border-b border-line pb-2 font-sans text-[13px] font-semibold tracking-widest text-accent-ink uppercase tabular-nums' -export const wrap = 'mx-auto max-w-[1600px] px-4 sm:px-8' +export const kicker = + "block border-b border-line pb-2 font-sans text-[13px] font-semibold tracking-widest text-accent-ink uppercase tabular-nums"; +export const wrap = "mx-auto max-w-[1600px] px-4 sm:px-8"; /** Frame of the landing, licence and notices pages: their own nav and footer, not the app's bar. */ export function Marketing({ children }: { children: ReactNode }) { @@ -18,8 +19,13 @@ export function Marketing({ children }: { children: ReactNode }) { // a column at least the window's height: the content takes the free space, so a short page still ends in its footer
-
- +
+ Ream
{children}
-
- - Ream - +
+
+ + Ream + + + (v{__APP_VERSION__}) + +
- © 2026 Chizoba Jude Ojini - Version {__APP_VERSION__} - MIT licence - Third-party notices + + © {new Date().getFullYear()} + + Chizoba Jude Ojini + + + + [MIT licence] + + + [Third-party notices] +
- ) + ); } diff --git a/packages/office-core/src/docx-model.ts b/packages/office-core/src/docx-model.ts index eddb801..66a3b0a 100644 --- a/packages/office-core/src/docx-model.ts +++ b/packages/office-core/src/docx-model.ts @@ -13,6 +13,12 @@ export type Run = { /** reading: RRGGBB behind the text, raised (super) or lowered (sub) */ highlight?: string vertAlign?: 'super' | 'sub' + /** reading: a web or mail address the text links to */ + link?: string + /** reading: the text is the page number ('page') or the page count ('pages'); `text` is what the file saved, to be replaced */ + field?: 'page' | 'pages' + /** reading: a picture in the line, like a character (`text` is empty): it sits on the baseline, grows the line, and goes where the paragraph's alignment puts it */ + picture?: Picture } export type Paragraph = { @@ -35,6 +41,12 @@ export type Paragraph = { /** first line, relative to the left indent; negative hangs */ indentFirst?: number pageBreakBefore?: boolean + /** reading: keep with the next paragraph, keep all lines on one page, widow and orphan control (on unless `false`) */ + keepNext?: boolean + keepLines?: boolean + widowControl?: boolean + /** reading: one entry for each tab character in the runs, in order; null when no stop is known there. `pos` counts from the left margin (missing: the margins themselves) */ + tabs?: ({ pos?: number; align: 'left' | 'center' | 'right' } | null)[] /** * placed in a text box at a spot on the page, points from its top left, instead of in the flow; paragraphs that share one * `frame` object (the same object, one after the other) share the box. `h` is a first guess: the box grows to its text. @@ -49,6 +61,10 @@ export type TableCell = { /** 'restart' starts a cell that continues down; 'continue' is a part under it */ vMerge?: 'restart' | 'continue' fill?: string + /** reading: where the text sits in a cell that is taller than it (default the top) */ + vAlign?: 'center' | 'bottom' + /** reading: room between the cell's edges and its text, points (Word's default: 5.4 at the sides, none above and below) */ + pad?: { top: number; right: number; bottom: number; left: number } } export type Table = { @@ -76,6 +92,22 @@ export type Picture = { front?: boolean /** reading: what `at` counts from (default the page) */ from?: { h: 'page' | 'margin'; v: 'page' | 'margin' | 'paragraph' } + /** reading: placed by alignment (in what `from` names) instead of by `at` */ + align?: { h?: 'left' | 'center' | 'right'; v?: 'top' | 'center' | 'bottom' } + /** + * reading: how text goes around a placed picture: 'square' (also what tight and through wrapping become: the picture's box), + * or 'topBottom' (no text beside it). Missing: text ignores it and the picture is drawn behind it, or in front when `front`. + */ + wrap?: 'square' | 'topBottom' + /** reading: with 'square', the side text may use: both (the wider piece of each line), only the left or right, or the largest */ + wrapText?: 'both' | 'left' | 'right' | 'largest' + /** reading: room kept clear around it, points */ + dist?: { top: number; bottom: number; left: number; right: number } + /** + * reading: what part of the picture shows in its box, as shares of the picture cut from each side. A negative share is + * empty room added there (a thin line picture in a tall box, as Google Docs makes for dividers). + */ + crop?: { l: number; t: number; r: number; b: number } } export type Block = Paragraph | Table | Picture @@ -83,8 +115,18 @@ export type Block = Paragraph | Table | Picture export type Section = { width: number height: number - margins: { top: number; right: number; bottom: number; left: number } + /** `header` and `footer` are the distances from the page's edge to the header and footer */ + margins: { top: number; right: number; bottom: number; left: number; header?: number; footer?: number } blocks: Block[] + /** reading: what repeats at the top and bottom of every page of the section; `first*` is for its first page when `titlePg` */ + header?: Block[] + footer?: Block[] + firstHeader?: Block[] + firstFooter?: Block[] + titlePg?: boolean } -export type DocModel = { sections: Section[]; title?: string } +/** reading: a font file the document carries, for the family, weight and slant it is for */ +export type EmbeddedFont = { name: string; bold: boolean; italic: boolean; bytes: Uint8Array } + +export type DocModel = { sections: Section[]; title?: string; /** reading: fonts stored in the file (Google Docs saves the ones it uses) */ fonts?: EmbeddedFont[] } diff --git a/packages/office-core/src/docx-read.ts b/packages/office-core/src/docx-read.ts index e3af2bd..113f16f 100644 --- a/packages/office-core/src/docx-read.ts +++ b/packages/office-core/src/docx-read.ts @@ -1,19 +1,23 @@ // Read a Word document (.docx) into a DocModel for the preview: paragraphs with their runs, styles (with basedOn chains // and document defaults) and lists worked out, tables, pictures in the flow or placed on the page, text boxes, sections -// and page breaks. Not read: headers and footers, footnotes, comments, tracked changes (insertions are kept, deletions -// dropped), fields' codes (their shown result is kept), columns, floating tables' positions and shapes other than pictures -// and text boxes. +// and page breaks, headers and footers (with their page number fields, a different first page and what a section takes from +// the one before), external hyperlinks, headings and what keeps lines and paragraphs together. Not read: footnotes, +// comments, tracked changes (insertions are kept, deletions dropped), other fields' codes (their shown result is kept), +// columns, different even pages, floating tables' positions and shapes other than pictures and text boxes. import { UserError, unzip } from '@ream/core' -import type { Block, DocModel, Paragraph, Picture, Run, Section, Table, TableCell } from './docx-model.ts' +import type { Block, DocModel, EmbeddedFont, Paragraph, Picture, Run, Section, Table, TableCell } from './docx-model.ts' import { DEFAULT_THEME, readRels, readTheme, relsOf, type Theme } from './ooxml.ts' import { child, kids, num, parseXml, type XmlNode } from './xml.ts' -const tw = (v: number) => v / 20 -const emu = (v: number) => v / 12700 +// Lengths come from a file that may hold anything: a value that isn't a number is 0, and none is beyond 100 000 points +const LIMIT = 100_000 +const pts = (v: number) => (Number.isFinite(v) ? Math.max(-LIMIT, Math.min(LIMIT, v)) : 0) +const tw = (v: number) => pts(v / 20) +const emu = (v: number) => pts(v / 12700) const off = (v: string | undefined) => v === '0' || v === 'false' || v === 'off' || v === 'none' const MAX_BLOCKS = 200_000 -type Style = { type: string; basedOn?: string; pPr?: XmlNode; rPr?: XmlNode; tblPr?: XmlNode } +type Style = { type: string; name?: string; basedOn?: string; pPr?: XmlNode; rPr?: XmlNode; tblPr?: XmlNode } const HIGHLIGHT: Record = { yellow: 'FFFF00', green: '00FF00', cyan: '00FFFF', magenta: 'FF00FF', blue: '0000FF', red: 'FF0000', darkBlue: '000080', darkCyan: '008080', darkGreen: '008000', darkMagenta: '800080', darkRed: '800000', darkYellow: '808000', darkGray: '808080', lightGray: 'C0C0C0', black: '000000', white: 'FFFFFF' } // the usual symbol-font bullets as plain characters @@ -25,8 +29,10 @@ type Env = { theme: Theme numbering: { nums: Map; abstracts: Map } counters: Map - rels: Map + rels: Map bytes: (name: string) => Uint8Array | undefined + /** a part of the package as parsed XML (kept: headers are used by every section that names them) */ + part: (name: string) => XmlNode | undefined budget: { blocks: number } } @@ -68,7 +74,7 @@ function runOf(text: string, rPrs: (XmlNode | undefined)[], env: Env): Run { bold: flag('b'), italic: flag('i'), underline: !!u && !off(u.attrs.val), - size: sz ? num(sz, 'val') / 2 : 11, + size: sz ? Math.max(1, Math.min(1000, num(sz, 'val') / 2)) || 11 : 11, color: color && color !== 'auto' && /^[0-9a-f]{6}$/i.test(color) ? color.toUpperCase() : undefined, font: fontOf(first(rPrs, 'rFonts'), env.theme), highlight: hl && hl !== 'auto' && hl !== 'none' ? (HIGHLIGHT[hl] ?? (/^[0-9a-f]{6}$/i.test(hl) ? hl.toUpperCase() : undefined)) : undefined, @@ -119,6 +125,29 @@ function listMark(env: Env, numPr: XmlNode | undefined): { text: string; ind?: X return { text, ind } } +const flagOf = (pPrs: (XmlNode | undefined)[], name: string) => { + const n = first(pPrs, name) + return n && !off(n.attrs.val) ? true : undefined +} + +/** a heading's level: the outline level the paragraph or its style sets, else a style named "heading 1" to "heading 6" */ +function headingOf(pPrs: (XmlNode | undefined)[], styles: Style[]): Paragraph['heading'] { + const lvl = first(pPrs, 'outlineLvl')?.attrs.val + if (lvl !== undefined) return Number(lvl) <= 5 ? ((Number(lvl) + 1) as 1) : undefined + const named = styles.map((s) => /^heading ([1-6])$/i.exec(s.name ?? '')?.[1]).find(Boolean) + return named ? (Number(named) as 1) : undefined +} + +/** the stop for each tab character: the paragraph's tab stops in order (the first set found in the style chain), or the place an alignment tab names */ +function tabsOf(seq: ({ ptab: 'left' | 'center' | 'right' } | 'tab')[], pPrs: (XmlNode | undefined)[]): NonNullable { + const stops = kids(first(pPrs, 'tabs'), 'tab') + .filter((t) => t.attrs.val !== 'clear' && t.attrs.pos) + .map((t) => ({ pos: tw(Number(t.attrs.pos)), align: t.attrs.val === 'center' ? ('center' as const) : t.attrs.val === 'right' || t.attrs.val === 'end' ? ('right' as const) : ('left' as const) })) + .sort((a, b) => a.pos - b.pos) + let n = 0 + return seq.map((t) => (t === 'tab' ? (stops[n++] ?? null) : { align: t.ptab })) +} + type Out = { blocks: Block[]; sections: Section[] } function picture(node: XmlNode, env: Env): Picture | null { @@ -134,12 +163,32 @@ function picture(node: XmlNode, env: Env): Picture | null { const ext2 = rel.part.split('.').pop()?.toLowerCase() if (ext2 !== 'png' && ext2 !== 'jpeg' && ext2 !== 'jpg') return null const p: Picture = { kind: 'picture', bytes, type: ext2 === 'png' ? 'png' : 'jpeg', width: emu(num(ext, 'cx')), height: emu(num(ext, 'cy')), alt: child(host, 'docPr')?.attrs.descr } + // the crop is in thousandths of a percent of the picture; what shows has to be something, and not absurdly little + const src = findNode(pic, 'srcRect') + if (src) { + const [l, t, r, b] = ['l', 't', 'r', 'b'].map((k) => Math.max(-100, Math.min(0.99, Number(src.attrs[k] ?? 0) / 100000 || 0))) + if ((l || t || r || b) && 1 - l - r > 0.01 && 1 - t - b > 0.01) p.crop = { l, t, r, b } + } if (anchor) { const h = child(anchor, 'positionH') const v = child(anchor, 'positionV') + const hAlign = child(h, 'align')?.text.trim() + const vAlign = child(v, 'align')?.text.trim() p.at = { x: emu(Number(child(h, 'posOffset')?.text ?? 0)), y: emu(Number(child(v, 'posOffset')?.text ?? 0)) } - p.from = { h: h?.attrs.relativeFrom === 'page' ? 'page' : 'margin', v: v?.attrs.relativeFrom === 'page' ? 'page' : v?.attrs.relativeFrom === 'margin' ? 'margin' : 'paragraph' } + // columns count as the margins (one column); a line, as the paragraph it is in + const fromH = h?.attrs.relativeFrom + const fromV = v?.attrs.relativeFrom + p.from = { h: fromH === 'page' ? 'page' : 'margin', v: fromV === 'page' ? 'page' : fromV && /margin/i.test(fromV) ? 'margin' : 'paragraph' } + if (hAlign || vAlign) p.align = { h: hAlign === 'left' || hAlign === 'center' || hAlign === 'right' ? hAlign : undefined, v: vAlign === 'top' || vAlign === 'center' || vAlign === 'bottom' ? vAlign : undefined } p.front = anchor.attrs.behindDoc !== '1' + const wrap = anchor.children.find((c) => /^wrap(Square|Tight|Through|TopAndBottom)$/.test(c.name)) + if (wrap) { + p.wrap = wrap.name === 'wrapTopAndBottom' ? 'topBottom' : 'square' + const side = wrap.attrs.wrapText + p.wrapText = side === 'left' || side === 'right' || side === 'largest' ? side : 'both' + const d = (k: string) => emu(Number(wrap.attrs[k] ?? anchor.attrs[k] ?? 0)) + p.dist = { top: d('distT'), bottom: d('distB'), left: d('distL'), right: d('distR') } + } } return p } @@ -163,36 +212,86 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { const pPrs = [pPr, ...styles.map((s) => s.pPr), env.defaults.pPr] const runs: Run[] = [] const extras: Block[] = [] + const tabSeq: ({ ptab: 'left' | 'center' | 'right' } | 'tab')[] = [] let breakAfter = false - const addRuns = (container: XmlNode) => { + // fields (complex ones span runs: begin, the code, separate, the shown result, end; simple ones wrap the result) + type Field = { code: string; shown: boolean; kind?: 'page' | 'pages'; link?: string; done: boolean } + const fields: Field[] = [] + const classify = (f: Field) => { + const code = f.code.trim() + const kind = /^(PAGE|NUMPAGES)\b/i.exec(code)?.[1].toUpperCase() + f.kind = kind === 'PAGE' ? 'page' : kind ? 'pages' : undefined + // a link to a place in the document (\l) has no address + const url = /^HYPERLINK\s+"([^"]+)"/i.exec(code)?.[1] + f.link = url && !/\s\\l\b/i.test(code) ? url : undefined + } + const addRuns = (container: XmlNode, link?: string) => { for (const c of container.children) { if (c.name === 'r') { const rPr = child(c, 'rPr') const charStyles = styleChain(env, child(rPr, 'rStyle')?.attrs.val) const rPrs = [rPr, ...charStyles.map((s) => s.rPr), ...styles.map((s) => s.rPr), env.defaults.rPr] + const add = (text: string) => { + const top = fields[fields.length - 1] + if (top && !top.shown) return // the code, not the result + if (top?.kind) { + // the page number is worked out when the page is drawn: keep one run for it, whatever the file saved + if (top.done) return + top.done = true + runs.push({ ...runOf(text, rPrs, env), field: top.kind }) + return + } + const r = runOf(text, rPrs, env) + const to = top?.link ?? link + if (to) r.link = to + runs.push(r) + } for (const k of c.children) { - if (k.name === 't') runs.push(runOf(k.text, rPrs, env)) - else if (k.name === 'tab') runs.push(runOf(' ', rPrs, env)) - else if (k.name === 'br') { - if (k.attrs.type === 'page') breakAfter = true - else runs.push(runOf('\n', rPrs, env)) - } else if (k.name === 'drawing') { - const pic = picture(k, env) - if (pic) extras.push(pic) - else textBox(k, env, extras) - } else if (k.name === 'AlternateContent') { - const drawing = findNode(child(k, 'Choice'), 'drawing') - if (drawing) { - const pic = picture(drawing, env) - if (pic) extras.push(pic) - else textBox(drawing, env, extras) + if (k.name === 't') add(k.text) + else if (k.name === 'fldChar') { + const type = k.attrs.fldCharType + if (type === 'begin') fields.push({ code: '', shown: false, done: false }) + else if (type === 'separate' && fields.length) { fields[fields.length - 1].shown = true; classify(fields[fields.length - 1]) } + else if (type === 'end') { + const f = fields.pop() + // a page number with no result saved (Google Docs writes none) still has to be shown + if (f?.kind && !f.done) runs.push({ ...runOf('1', rPrs, env), field: f.kind }) } + } else if (k.name === 'instrText') { + const top = fields[fields.length - 1] + if (top && !top.shown) top.code += k.text + } else if (k.name === 'tab') { add('\t'); tabSeq.push('tab') } + else if (k.name === 'ptab') { + const a = k.attrs.alignment + add('\t') + tabSeq.push({ ptab: a === 'center' ? 'center' : a === 'right' ? 'right' : 'left' }) + } else if (k.name === 'br') { + if (k.attrs.type === 'page') breakAfter = true + else add('\n') + } else if (k.name === 'drawing' || k.name === 'AlternateContent') { + const drawing = k.name === 'drawing' ? k : findNode(child(k, 'Choice'), 'drawing') + const pic = drawing ? picture(drawing, env) : null + // a picture in the line is part of the paragraph's text; one placed on the page is laid out around it + if (pic && !pic.at) runs.push({ ...runOf('', rPrs, env), picture: pic }) + else if (pic) extras.push(pic) + else if (drawing) textBox(drawing, env, extras) } } - } else if (['hyperlink', 'ins', 'smartTag', 'fldSimple', 'customXml'].includes(c.name)) addRuns(c) + } else if (c.name === 'hyperlink') { + // an address outside the file; a link to a bookmark (anchor) has none + const rel = c.attrs.id ? env.rels.get(c.attrs.id) : undefined + addRuns(c, rel?.external ? rel.target : undefined) + } else if (c.name === 'fldSimple') { + const f: Field = { code: c.attrs.instr ?? '', shown: true, done: false } + classify(f) + fields.push(f) + addRuns(c, link) + fields.pop() + if (f.kind && !f.done) runs.push({ ...runOf('1', [...styles.map((x) => x.rPr), env.defaults.rPr], env), field: f.kind }) + } else if (['ins', 'smartTag', 'customXml'].includes(c.name)) addRuns(c, link) else if (c.name === 'sdt') { const content = child(c, 'sdtContent') - if (content) addRuns(content) + if (content) addRuns(content, link) } } } @@ -209,26 +308,30 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { const hanging = at('hanging') const para: Paragraph = { kind: 'p', - runs: runs.filter((r) => r.text), + runs: runs.filter((r) => r.text || r.picture), align: jc === 'center' ? 'center' : jc === 'right' || jc === 'end' ? 'right' : jc === 'both' || jc === 'distribute' ? 'justify' : 'left', before: tw(Number(sp('before') ?? 0)), // a paragraph in a table cell ignores the document's default space after in Word's own table styles; keep what is set after: tw(Number(sp('after') ?? 0)), line: line && lineRule !== 'auto' ? tw(Number(line)) : undefined, lineExact: lineRule === 'exact' || undefined, - lineFactor: line && lineRule === 'auto' ? Number(line) / 240 : undefined, + lineFactor: line && lineRule === 'auto' && Number.isFinite(Number(line)) ? Math.max(0.1, Math.min(20, Number(line) / 240)) : undefined, indentLeft: tw(Number(at('left') ?? at('start') ?? 0)) || undefined, indentRight: tw(Number(at('right') ?? at('end') ?? 0)) || undefined, indentFirst: hanging ? -tw(Number(hanging)) : tw(Number(at('firstLine') ?? 0)) || undefined, pageBreakBefore: first(pPrs, 'pageBreakBefore') ? !off(first(pPrs, 'pageBreakBefore')!.attrs.val) : undefined, marker: list?.text || undefined, - heading: undefined, + heading: headingOf(pPrs, styles), + keepNext: flagOf(pPrs, 'keepNext'), + keepLines: flagOf(pPrs, 'keepLines'), + widowControl: first(pPrs, 'widowControl') ? !off(first(pPrs, 'widowControl')!.attrs.val) : undefined, + tabs: tabSeq.length ? tabsOf(tabSeq, pPrs) : undefined, } void inCell out.blocks.push(para, ...extras) if (breakAfter) out.blocks.push({ kind: 'p', runs: [], pageBreakBefore: true, after: 0, line: 0.1, lineExact: true }) const sect = child(pPr, 'sectPr') - if (sect) endSection(sect, out) + if (sect) endSection(sect, out, env) } /** a text box anchored in a paragraph: its paragraphs placed where the box is */ @@ -251,11 +354,22 @@ function textBox(drawing: XmlNode, env: Env, into: Block[]) { for (const b of sub.blocks) if (b.kind === 'p') into.push({ ...b, frame }) } +/** cell margins from a tcMar or tblCellMar element: points per side, a side it doesn't name left to the default */ +function margins(m: XmlNode | undefined): Partial> { + const side = (...names: string[]) => { + const n = names.map((x) => child(m, x)).find(Boolean) + return n && n.attrs.w !== undefined ? tw(Number(n.attrs.w)) : undefined + } + return { top: side('top'), right: side('right', 'end'), bottom: side('bottom'), left: side('left', 'start') } +} + function readTable(t: XmlNode, env: Env, out: Out) { const tblPr = child(t, 'tblPr') const style = styleChain(env, child(tblPr, 'tblStyle')?.attrs.val) const borders = [child(tblPr, 'tblBorders'), ...style.map((s) => child(s.tblPr, 'tblBorders'))].find(Boolean) const hasBorders = !!borders && borders.children.some((b) => b.attrs.val && !off(b.attrs.val) && b.attrs.val !== 'nil') + // the table's own cell margins, else its style's + const tableMar = margins([child(tblPr, 'tblCellMar'), ...style.map((s) => child(s.tblPr, 'tblCellMar'))].find(Boolean)) const colWidths = kids(child(t, 'tblGrid'), 'gridCol').map((g) => tw(num(g, 'w'))) const rows: TableCell[][] = [] for (const tr of kids(t, 'tr')) { @@ -270,7 +384,11 @@ function readTable(t: XmlNode, env: Env, out: Out) { // ponytail: a table inside a cell is read as its paragraphs, one after the other else if (c.name === 'tbl') for (const p of findAll(c, 'p')) readParagraph(p, env, sub, true) } + const own = margins(child(pr, 'tcMar')) + const va = child(pr, 'vAlign')?.attrs.val row.push({ + vAlign: va === 'center' || va === 'bottom' ? va : undefined, + pad: { top: own.top ?? tableMar.top ?? 0, right: own.right ?? tableMar.right ?? 5.4, bottom: own.bottom ?? tableMar.bottom ?? 0, left: own.left ?? tableMar.left ?? 5.4 }, paragraphs: sub.blocks.filter((b): b is Paragraph => b.kind === 'p'), cols: num(child(pr, 'gridSpan'), 'val', 1) > 1 ? num(child(pr, 'gridSpan'), 'val') : undefined, vMerge: vm ? (vm.attrs.val === 'restart' ? 'restart' : 'continue') : undefined, @@ -292,22 +410,83 @@ function findAll(n: XmlNode, name: string, into: XmlNode[] = []): XmlNode[] { return into } -function sectionOf(sect: XmlNode | undefined, blocks: Block[]): Section { +/** the blocks of a header or footer part, read with that part's own relationships (its pictures) */ +function partBlocks(env: Env, part: string): Block[] { + const root = env.part(part) + if (!root) return [] + const sub: Env = { ...env, rels: readRels(env.part(relsOf(part)), part), counters: new Map() } + const out: Out = { blocks: [], sections: [] } + walk(root, sub, out) + return out.blocks +} + +function sectionOf(sect: XmlNode | undefined, blocks: Block[], env: Env, before?: Section): Section { const sz = child(sect, 'pgSz') const mar = child(sect, 'pgMar') + // what a section doesn't name it takes from the one before; naming an empty one clears it + const hf = (tag: 'headerReference' | 'footerReference', type: string, was: Block[] | undefined) => { + const ref = kids(sect, tag).find((r) => (r.attrs.type ?? 'default') === type) + const rel = ref?.attrs.id ? env.rels.get(ref.attrs.id) : undefined + return rel ? partBlocks(env, rel.part) : was + } + const title = child(sect, 'titlePg') return { width: tw(num(sz, 'w', 11906)), height: tw(num(sz, 'h', 16838)), - margins: { top: tw(Math.abs(num(mar, 'top', 1440))), right: tw(num(mar, 'right', 1440)), bottom: tw(Math.abs(num(mar, 'bottom', 1440))), left: tw(num(mar, 'left', 1440)) }, + margins: { top: tw(Math.abs(num(mar, 'top', 1440))), right: tw(num(mar, 'right', 1440)), bottom: tw(Math.abs(num(mar, 'bottom', 1440))), left: tw(num(mar, 'left', 1440)), header: tw(num(mar, 'header', 720)), footer: tw(num(mar, 'footer', 720)) }, blocks, + header: hf('headerReference', 'default', before?.header), + footer: hf('footerReference', 'default', before?.footer), + firstHeader: hf('headerReference', 'first', before?.firstHeader), + firstFooter: hf('footerReference', 'first', before?.firstFooter), + titlePg: title ? !off(title.attrs.val) : undefined, } } -function endSection(sect: XmlNode, out: Out) { - out.sections.push(sectionOf(sect, out.blocks)) +function endSection(sect: XmlNode, out: Out, env: Env) { + out.sections.push(sectionOf(sect, out.blocks, env, out.sections[out.sections.length - 1])) out.blocks = [] } +/** the paragraphs and tables under a body, header or footer element, in order */ +function walk(n: XmlNode, env: Env, out: Out) { + for (const c of n.children) { + if (c.name === 'p') readParagraph(c, env, out) + else if (c.name === 'tbl') readTable(c, env, out) + else if (c.name === 'sdt') { + const content = child(c, 'sdtContent') + if (content) walk(content, env, out) + } + } +} + +const MAX_FONTS = 40 +const MAX_FONT_BYTES = 30_000_000 + +/** Word stores embedded fonts with their first 32 bytes mixed with the font's GUID (reversed); a key of zeros, or none, leaves a font as it was */ +function unscramble(bytes: Uint8Array, guid: string | undefined): Uint8Array { + const hex = (guid ?? '').replace(/[^0-9a-f]/gi, '') + if (hex.length !== 32) return bytes + const key = Array.from({ length: 16 }, (_, i) => parseInt(hex.slice(30 - 2 * i, 32 - 2 * i), 16)) + const out = bytes.slice() + for (let i = 0; i < 32 && i < out.length; i++) out[i] ^= key[i % 16] + return out +} + +/** the fonts the file carries: the table lists a family's regular, bold, italic and bold italic files */ +function embeddedFonts(table: XmlNode | undefined, rels: Map, parts: Map): EmbeddedFont[] { + const out: EmbeddedFont[] = [] + for (const f of kids(table, 'font')) { + for (const [tag, bold, italic] of [['embedRegular', false, false], ['embedBold', true, false], ['embedItalic', false, true], ['embedBoldItalic', true, true]] as const) { + const e = child(f, tag) + const raw = e?.attrs.id ? parts.get(rels.get(e.attrs.id)?.part ?? '') : undefined + if (!e || !raw || !f.attrs.name || raw.length > MAX_FONT_BYTES || out.length >= MAX_FONTS) continue + out.push({ name: f.attrs.name, bold, italic, bytes: unscramble(raw, e.attrs.fontKey) }) + } + } + return out +} + /** a Word document as sections of blocks */ export async function readDocx(bytes: Uint8Array): Promise { const parts = await unzip(bytes) @@ -325,7 +504,7 @@ export async function readDocx(bytes: Uint8Array): Promise { const stylesXml = partOf('/styles') ? xml(partOf('/styles')!) : undefined const styles = new Map() for (const s of kids(stylesXml, 'style')) { - if (s.attrs.styleId) styles.set(s.attrs.styleId, { type: s.attrs.type ?? '', basedOn: child(s, 'basedOn')?.attrs.val, pPr: child(s, 'pPr'), rPr: child(s, 'rPr'), tblPr: child(s, 'tblPr') }) + if (s.attrs.styleId) styles.set(s.attrs.styleId, { type: s.attrs.type ?? '', name: child(s, 'name')?.attrs.val, basedOn: child(s, 'basedOn')?.attrs.val, pPr: child(s, 'pPr'), rPr: child(s, 'rPr'), tblPr: child(s, 'tblPr') }) } const defaults = { pPr: child(child(child(stylesXml, 'docDefaults'), 'pPrDefault'), 'pPr'), rPr: child(child(child(stylesXml, 'docDefaults'), 'rPrDefault'), 'rPr') } const numXml = partOf('/numbering') ? xml(partOf('/numbering')!) : undefined @@ -334,6 +513,7 @@ export async function readDocx(bytes: Uint8Array): Promise { const nums = new Map() for (const n of kids(numXml, 'num')) if (n.attrs.numId) nums.set(n.attrs.numId, child(n, 'abstractNumId')?.attrs.val ?? '') const themePart = partOf('/theme') + const partCache = new Map() const env: Env = { styles, defaults, theme: themePart && xml(themePart) ? readTheme(xml(themePart)) : DEFAULT_THEME, @@ -341,18 +521,17 @@ export async function readDocx(bytes: Uint8Array): Promise { counters: new Map(), rels, bytes: (n) => parts.get(n), + part: (n) => { + if (!partCache.has(n)) partCache.set(n, xml(n)) + return partCache.get(n) + }, budget: { blocks: MAX_BLOCKS }, } const out: Out = { blocks: [], sections: [] } const body = child(doc, 'body') - const walk = (n: XmlNode) => { - for (const c of n.children) { - if (c.name === 'p') readParagraph(c, env, out) - else if (c.name === 'tbl') readTable(c, env, out) - else if (c.name === 'sdt') { const content = child(c, 'sdtContent'); if (content) walk(content) } - } - } - if (body) walk(body) - out.sections.push(sectionOf(child(body, 'sectPr'), out.blocks)) - return { sections: out.sections.filter((s, i) => s.blocks.length || i === out.sections.length - 1) } + if (body) walk(body, env, out) + out.sections.push(sectionOf(child(body, 'sectPr'), out.blocks, env, out.sections[out.sections.length - 1])) + const fontPart = partOf('/fontTable') + const fonts = fontPart ? embeddedFonts(xml(fontPart), readRels(xml(relsOf(fontPart)), fontPart), parts) : [] + return { sections: out.sections.filter((s, i) => s.blocks.length || i === out.sections.length - 1), ...(fonts.length ? { fonts } : {}) } } diff --git a/packages/office-core/src/fonts-used.ts b/packages/office-core/src/fonts-used.ts index 44390f4..47ec5e7 100644 --- a/packages/office-core/src/fonts-used.ts +++ b/packages/office-core/src/fonts-used.ts @@ -1,13 +1,15 @@ // The font names an Office file asks for, so the app can look for them on the computer. import { unzip } from '@ream/core' +import { readDocx } from './docx-read.ts' import { readPptx } from './pptx-read.ts' import { readXlsx } from './xlsx-read.ts' -/** font family names a .pptx or .xlsx uses, most used first; empty for anything else */ +/** font family names a .docx, .pptx or .xlsx uses, most used first; empty for anything else */ export async function usedFonts(bytes: Uint8Array): Promise { const parts = await unzip(bytes).catch(() => null) const counts = new Map() const add = (n: string | undefined, by = 1) => n && counts.set(n, (counts.get(n) ?? 0) + by) + const carried = new Set() if (parts?.has('ppt/presentation.xml')) { for (const s of (await readPptx(bytes)).slides) { for (const sh of s.shapes) { @@ -15,8 +17,18 @@ export async function usedFonts(bytes: Uint8Array): Promise { for (const p of paras) for (const r of p.runs) add(r.font, r.text.length) } } + } else if (parts?.has('word/document.xml')) { + const doc = await readDocx(bytes) + // a family the file carries needs nothing from the computer + for (const f of doc.fonts ?? []) carried.add(f.name.toLowerCase()) + for (const sec of doc.sections) { + for (const b of [...sec.blocks, ...(sec.header ?? []), ...(sec.footer ?? []), ...(sec.firstHeader ?? []), ...(sec.firstFooter ?? [])]) { + const paras = b.kind === 'p' ? [b] : b.kind === 'table' ? b.rows.flat().flatMap((c) => c.paragraphs) : [] + for (const p of paras) for (const r of p.runs) add(r.font, r.text.length) + } + } } else if (parts?.has('xl/workbook.xml')) { for (const s of (await readXlsx(bytes)).sheets) for (const row of s.rows) for (const c of row ?? []) if (c?.style?.font) add(c.style.font) } - return [...counts].sort((a, b) => b[1] - a[1]).map(([n]) => n) + return [...counts].filter(([n]) => !carried.has(n.toLowerCase())).sort((a, b) => b[1] - a[1]).map(([n]) => n) } diff --git a/packages/office-core/src/index.ts b/packages/office-core/src/index.ts index 3c5bdd4..18fc4ce 100644 --- a/packages/office-core/src/index.ts +++ b/packages/office-core/src/index.ts @@ -6,7 +6,7 @@ export type { Anchor, Cell, Edge, Merge, PageSetup, Sheet, SheetChart, SheetPict export { writeXlsx, sheetNames } from './xlsx.ts' export { readXlsx, type Workbook } from './xlsx-read.ts' export { writeDocx } from './docx.ts' -export type { DocModel, Section as DocSection, Block as DocBlock, Paragraph, Run, Table as DocTable, TableCell as DocTableCell, Picture } from './docx-model.ts' +export type { DocModel, EmbeddedFont, Section as DocSection, Block as DocBlock, Paragraph, Run, Table as DocTable, TableCell as DocTableCell, Picture } from './docx-model.ts' export { writePptx } from './pptx.ts' export type { Deck, Slide, SlideShape, SlideBox, SlidePara, SlideRun, SlidePicture, SlideLine, SlideTable, SlideTableCell, SlideChart, Paint, Stroke, LineEnd, CustomPath, Xform } from './pptx-model.ts' export { readPptx } from './pptx-read.ts' diff --git a/packages/office-core/src/ooxml.ts b/packages/office-core/src/ooxml.ts index 8976e63..7b1b29e 100644 --- a/packages/office-core/src/ooxml.ts +++ b/packages/office-core/src/ooxml.ts @@ -31,10 +31,10 @@ export function resolvePart(from: string, target: string): string { export const relsOf = (part: string) => part.replace(/([^/]+)$/, '_rels/$1.rels') /** relationship id -> { target part, type }, from a parsed .rels file */ -export function readRels(rels: XmlNode | undefined, from: string): Map { - const out = new Map() +export function readRels(rels: XmlNode | undefined, from: string): Map { + const out = new Map() for (const r of kids(rels, 'Relationship')) { - if (r.attrs.Id && r.attrs.Target) out.set(r.attrs.Id, { part: resolvePart(from, r.attrs.Target), type: r.attrs.Type ?? '', external: r.attrs.TargetMode === 'External' }) + if (r.attrs.Id && r.attrs.Target) out.set(r.attrs.Id, { part: resolvePart(from, r.attrs.Target), type: r.attrs.Type ?? '', external: r.attrs.TargetMode === 'External', target: r.attrs.Target }) } return out } diff --git a/packages/office-core/test/docx-helper.ts b/packages/office-core/test/docx-helper.ts new file mode 100644 index 0000000..6d91171 --- /dev/null +++ b/packages/office-core/test/docx-helper.ts @@ -0,0 +1,70 @@ +// A small .docx made from XML strings, for tests that need what a real file has and the fixtures don't (headers, fields, links). +import { zipPacked } from '@ream/core' + +export const NS = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"' +const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' +const enc = (s: string) => new TextEncoder().encode(s) + +/** + * `body` goes inside w:body (its last w:sectPr is the document's); `rels` are the document's relationships (id, type name, + * target, external); `parts` are other files by name, written as they are (a header is `...`: use `part()`). + */ +export async function makeDocx(o: { body: string; styles?: string; rels?: [id: string, type: string, target: string, external?: boolean][]; parts?: Record }): Promise { + const rels = (o.rels ?? []).map(([id, type, target, external]) => ``) + if (o.styles) rels.push(``) + const files: Record = { + '_rels/.rels': ``, + 'word/_rels/document.xml.rels': `${rels.join('')}`, + 'word/document.xml': `${o.body}`, + ...(o.styles ? { 'word/styles.xml': `${o.styles}` } : {}), + ...o.parts, + } + return zipPacked(Object.entries(files).map(([name, data]) => ({ name, bytes: typeof data === 'string' ? enc(data) : data }))) +} + +/** a 1 x 1 PNG, for pictures that only need to be there */ +export const PIXEL = Uint8Array.from(atob('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='), (c) => c.charCodeAt(0)) + +/** a picture placed in the page: `size` points square at (`x`, `y`) points from the column and the paragraph, text going `wrap` it, 9 points clear all round; rel id `pic` must be in the document's relationships */ +export const placed = (wrap: 'square' | 'topBottom', size = 144, x = 0, y = 0) => + `` + + `${x * 12700}${y * 12700}` + + `${wrap === 'square' ? '' : ''}` + + `` + +/** + * a document that carries fonts: `fonts` are [family, file bytes as stored, the GUID key written for it] for the regular face. + * What Word does to a font before storing it (mixing its first 32 bytes with the reversed GUID) is `scramble`. + */ +export async function withFonts(fonts: [string, Uint8Array, string][], body: string): Promise { + const table = fonts.map(([name, , guid], i) => ``).join('') + return makeDocx({ + body, + rels: [['rFt', 'fontTable', 'fontTable.xml']], + parts: { + 'word/fontTable.xml': `${table}`, + 'word/_rels/fontTable.xml.rels': `${fonts.map((_, i) => ``).join('')}`, + ...Object.fromEntries(fonts.map(([, bytes], i) => [`word/fonts/f${i}.odttf`, bytes])), + }, + }) +} + +/** the 32 bytes at the start of a font mixed with a GUID the way Word does it (the GUID's bytes taken from the end); doing it again undoes it */ +export function scramble(bytes: Uint8Array, guid: string): Uint8Array { + const hex = guid.replace(/[^0-9a-f]/gi, '') + const key = (hex.match(/../g) ?? []).map((h) => parseInt(h, 16)).reverse() + const out = bytes.slice() + for (let i = 0; i < 32 && i < out.length; i++) out[i] ^= key[i % 16] + return out +} + +/** a picture in a line of text, `w` by `h` points; rel id `pic` must be in the document's relationships */ +export const inline = (w: number, h = w, srcRect = '') => + `` + + `${srcRect}` + +/** a header or footer part: `kind` is 'hdr' or 'ftr' */ +export const part = (kind: 'hdr' | 'ftr', inner: string) => `${inner}` + +/** a paragraph of plain runs */ +export const para = (text: string, props = '') => `${props ? `${props}` : ''}${text}` diff --git a/packages/office-core/test/docx-read.test.ts b/packages/office-core/test/docx-read.test.ts index 0cd1e16..54fc46f 100644 --- a/packages/office-core/test/docx-read.test.ts +++ b/packages/office-core/test/docx-read.test.ts @@ -1,6 +1,7 @@ import { readFileSync } from 'node:fs' import { expect, test } from 'vitest' -import { readDocx, type DocBlock, type Paragraph } from '../src/index.ts' +import { readDocx, usedFonts, type DocBlock, type Paragraph } from '../src/index.ts' +import { PIXEL, inline, makeDocx, para, part, scramble, withFonts } from './docx-helper.ts' // report.docx (python-docx, its default template): a title, mixed runs, a centred line, bullets, a numbered list, a grid // table, a picture, a page break and a heading on the second page @@ -9,7 +10,8 @@ test('a Word document reads with styles resolved: sizes and colours from the sty expect([s.width, s.height]).toEqual([612, 792]) const paras = s.blocks.filter((b): b is Paragraph => b.kind === 'p' && b.runs.length > 0) const text = (p: Paragraph) => p.runs.map((r) => r.text).join('') - expect(paras.map(text)).toEqual(['Annual report', 'Plain text, bold red and more.', 'Centred line', 'First point', 'Second point', 'Step one', 'Step two', 'Second page', 'Closing words.']) + // the picture sits in a paragraph of its own (the empty text between 'Step two' and 'Second page') + expect(paras.map(text)).toEqual(['Annual report', 'Plain text, bold red and more.', 'Centred line', 'First point', 'Second point', 'Step one', 'Step two', '', 'Second page', 'Closing words.']) // the Title style: 26 pt in the theme's dark blue expect(paras[0].runs[0]).toMatchObject({ size: 26, color: '17365D' }) expect(paras[1].runs[1]).toMatchObject({ text: 'bold red', bold: true, color: 'C00000' }) @@ -17,7 +19,95 @@ test('a Word document reads with styles resolved: sizes and colours from the sty expect(paras.slice(3, 7).map((p) => p.marker)).toEqual(['•', '•', '1.', '2.']) const table = s.blocks.find((b) => b.kind === 'table') as Extract expect(table.rows.map((r) => r.map((c) => c.paragraphs.map(text).join('')))).toEqual([['Item', 'Price'], ['Tea', '4.50']]) - expect(s.blocks.find((b) => b.kind === 'picture')).toMatchObject({ type: 'png', width: 144, height: 72 }) + expect(paras.flatMap((p) => p.runs).find((r) => r.picture)?.picture).toMatchObject({ type: 'png', width: 144, height: 72 }) const breakAt = s.blocks.findIndex((b) => b.kind === 'p' && b.pageBreakBefore) expect(breakAt).toBeGreaterThan(s.blocks.findIndex((b) => b.kind === 'picture')) }) + +// made by hand: a document of two sections where the second names no footer, a different first page, a footer with tab stops and +// page number fields of both kinds, links of three kinds, headings and what keeps paragraphs together +test('headers and footers, fields, links, headings and keep settings are read', async () => { + const styles = + '' + + '' + const footer = part( + 'ftr', + 'LeftMid' + + 'Page 9 of ' + + ' NUMPAGES ' + + '12', + ) + const alignTab = part('ftr', 'AB') + const bytes = await makeDocx({ + styles, + rels: [['rH', 'header', 'header1.xml'], ['rF', 'footer', 'footer1.xml'], ['rF1', 'footer', 'footer2.xml'], ['rL', 'hyperlink', 'https://example.com/x', true]], + parts: { 'word/header1.xml': part('hdr', para('Running head')), 'word/footer1.xml': footer, 'word/footer2.xml': alignTab }, + body: + para('Intro', '') + + 'webinside' + + ' HYPERLINK "https://example.com/field" field' + + para('Loose', '') + + '' + + '' + + para('Second section') + + '', + }) + const { sections } = await readDocx(bytes) + expect(sections).toHaveLength(2) + const [a, b] = sections + expect(a.margins).toMatchObject({ header: 18, footer: 27, top: 72 }) + expect(a.titlePg).toBe(true) + + const [intro, links, loose] = a.blocks as Paragraph[] + expect(intro).toMatchObject({ heading: 1, keepNext: true, keepLines: true }) + expect(loose).toMatchObject({ heading: 2, widowControl: false }) + expect(intro.widowControl).toBeUndefined() + // an address outside the file, a place inside it (no link), and a field-coded address + expect(links.runs.map((r) => [r.text, r.link])).toEqual([['web', 'https://example.com/x'], ['inside', undefined], ['field', 'https://example.com/field']]) + + // the footer: tab stops from its style, one run for each page number whatever the file saved + const [f] = a.footer as Paragraph[] + expect(f.runs.map((r) => [r.text === '\t' ? 'TAB' : r.text, r.field])).toEqual([['Left', undefined], ['TAB', undefined], ['Mid', undefined], ['TAB', undefined], ['Page ', undefined], ['9', 'page'], [' of ', undefined], ['1', 'pages']]) + expect(f.tabs).toEqual([{ pos: 234, align: 'center' }, { pos: 468, align: 'right' }]) + expect(((a.header as Paragraph[])[0]).runs[0].text).toBe('Running head') + expect((a.firstFooter as Paragraph[])[0].tabs).toEqual([{ align: 'right' }]) + // the second section names nothing, so it has the first one's + expect(b.header).toEqual(a.header) + expect(b.footer).toEqual(a.footer) + expect(b.titlePg).toBeUndefined() +}) + +test('the fonts of a Word document include those of its headers and footers, most used first', async () => { + const bytes = await makeDocx({ + rels: [['h', 'header', 'h.xml']], + parts: { 'word/h.xml': part('hdr', 'Running head') }, + body: 'A longer body line', + }) + expect(await usedFonts(bytes)).toEqual(['Calibri', 'Georgia']) +}) + +test('fonts stored in the file are read, with Word\'s scrambling undone and a key of zeros meaning none', async () => { + const original = Uint8Array.from({ length: 80 }, (_, i) => (i * 7) % 251) + const guid = '{00112233-4455-6677-8899-AABBCCDDEEFF}' + // the first 32 bytes are what gets changed, with the GUID's bytes from the last one: FF EE DD ... 00 + const stored = scramble(original, guid) + expect(stored[0]).toBe(original[0] ^ 0xff) + expect(stored[16]).toBe(original[16] ^ 0xff) + expect(stored[32]).toBe(original[32]) + const { fonts } = await readDocx(await withFonts([['Scrambled', stored, guid], ['Plain', original, '{00000000-0000-0000-0000-000000000000}']], para('x'))) + expect(fonts?.map((f) => [f.name, f.bold, f.italic, [...f.bytes]])).toEqual([['Scrambled', false, false, [...original]], ['Plain', false, false, [...original]]]) + // a family the file carries is not asked of the computer; one it doesn't is + const text = (font: string) => `hello` + expect(await usedFonts(await withFonts([['Plain', original, '{00000000-0000-0000-0000-000000000000}']], text('Plain') + text('Elsewhere')))).toEqual(['Elsewhere']) +}) + +test('a picture in a line is a run of its paragraph, with its crop (cut from the left, room added below)', async () => { + const bytes = await makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: `a${inline(100, 50, '')}` }) + const [p] = (await readDocx(bytes)).sections[0].blocks as Paragraph[] + expect(p.align).toBe('center') + expect(p.runs.map((r) => r.text)).toEqual(['a', '']) + expect(p.runs[1].picture).toMatchObject({ type: 'png', width: 100, height: 50, crop: { l: 0.25, t: 0, r: 0, b: -5.66667 } }) + // nothing absurd: a crop that leaves nothing of the picture is dropped + const none = await makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: `${inline(10, 10, '')}` }) + expect(((await readDocx(none)).sections[0].blocks[0] as Paragraph).runs[0].picture?.crop).toBeUndefined() +}) diff --git a/packages/pdf-core/src/build.ts b/packages/pdf-core/src/build.ts index ad7515a..b2a26b0 100644 --- a/packages/pdf-core/src/build.ts +++ b/packages/pdf-core/src/build.ts @@ -10,6 +10,7 @@ import { ascii } from './bytes.ts' import { addImage, type ImageInput } from './images.ts' import { encodeWinAnsi, fontResource, textWidth } from './text.ts' import { prepareFont, type PreparedFont, type TtfFont } from './ttf.ts' +import { writeOutline, type OutlineItem } from './outline.ts' export type Rgb = [number, number, number] @@ -26,9 +27,17 @@ export class FontFace { } /** the part that has a glyph for this character, or -1 */ partFor(cp: number): number { - for (let i = 0; i < this.parts.length; i++) if (this.parts[i].glyphFor(cp)) return i - return -1 + let part = this.partOf.get(cp) + if (part === undefined) { + part = -1 + for (let i = 0; i < this.parts.length && part < 0; i++) if (this.parts[i].glyphFor(cp)) part = i + this.partOf.set(cp, part) + } + return part } + private partOf = new Map() + /** widths of strings at size 1, plain and bold (the standard font that stands in for a missing letter differs), so a word is measured once however often it is used */ + widths = [new Map(), new Map()] } /** the standard fonts: sans is Helvetica, serif Times, mono Courier. With `face`, the text is drawn in that embedded typeface. */ @@ -59,32 +68,50 @@ function standardWidth(text: string, size: number, family: 'sans' | 'serif' | 'm } /** a string cut into runs that one font draws: a part of the face (index), or -1 for the standard font */ -function runsOf(s: string, face: FontFace): { part: number; text: string }[] { +function runsOf(text: string, face: FontFace): { part: number; text: string }[] { + const s = text.normalize('NFC') const out: { part: number; text: string }[] = [] - for (const ch of s.normalize('NFC')) { + let part = -2 + let start = 0 + let pos = 0 + for (const ch of s) { const cp = ch.codePointAt(0)! // a space or control takes whatever font the text around it uses - const part = cp <= 32 && out.length ? out[out.length - 1].part : face.partFor(cp) - const last = out[out.length - 1] - if (last && last.part === part) last.text += ch - else out.push({ part, text: ch }) + const p = cp <= 32 && part !== -2 ? part : face.partFor(cp) + if (part === -2) part = p + else if (p !== part) { + out.push({ part, text: s.slice(start, pos) }) + start = pos + part = p + } + pos += ch.length } + if (pos > start) out.push({ part, text: s.slice(start, pos) }) return out } +const MAX_CACHED_WIDTHS = 50_000 + /** width of text in points, in the font it will be drawn with */ export function measure(text: string, size: number, font: FontSpec): number { const k = font.scale ?? 1 if (!font.face) return standardWidth(text, size, font.family, font.bold) * k - let w = 0 - for (const r of runsOf(text, font.face)) { - if (r.part < 0) w += standardWidth(r.text, size, font.face.fallback, font.bold) - else { - const t = font.face.parts[r.part] - for (const ch of r.text) w += (t.advance(t.glyphFor(ch.codePointAt(0)!)) * size) / t.unitsPerEm + const face = font.face + const cache = face.widths[font.bold ? 1 : 0] + let unit = cache.get(text) + if (unit === undefined) { + unit = 0 + for (const r of runsOf(text, face)) { + if (r.part < 0) unit += standardWidth(r.text, 1, face.fallback, font.bold) + else { + const t = face.parts[r.part] + for (const ch of r.text) unit += t.advance(t.glyphFor(ch.codePointAt(0)!)) / t.unitsPerEm + } } + if (cache.size >= MAX_CACHED_WIDTHS) cache.clear() + cache.set(text, unit) } - return w * k + return unit * size * k } /** a path: move, line, cubic curve, close */ @@ -123,8 +150,13 @@ type Op = | { k: 'path'; segs: Seg[]; style: PathStyle } | { k: 'transform'; m: [number, number, number, number, number, number] } +/** schemes a link may open: a PDF viewer would also run `javascript:` and `file:` ones, so those are dropped */ +const LINK_SCHEMES = new Set(['http:', 'https:', 'mailto:']) + export class PageDraw { ops: Op[] = [] + /** clickable boxes, in the page's own y-down points */ + links: { x: number; y: number; w: number; h: number; uri: string }[] = [] width: number height: number constructor(width: number, height: number) { @@ -143,6 +175,15 @@ export class PageDraw { /** `spacing` is extra space after every letter, points (it is not in `measure`: add it per letter) */ text(x: number, y: number, s: string, font: FontSpec, size: number, color: Rgb, alpha?: number, spacing?: number) { if (s) this.ops.push({ k: 'text', x, y, s, font, size, color, alpha, spacing }) } image(id: number, x: number, y: number, w: number, h: number, alpha?: number) { this.ops.push({ k: 'image', id, x, y, w, h, alpha }) } + /** a clickable box that opens a web or mail address; any other kind of address, or one that isn't valid, is ignored */ + link(x: number, y: number, w: number, h: number, uri: string) { + try { + const u = new URL(uri) + if (LINK_SCHEMES.has(u.protocol) && u.href.length <= 2048 && w > 0 && h > 0) this.links.push({ x, y, w, h, uri: u.href }) + } catch { + // not an address + } + } /** draw a path, filled and/or stroked */ path(segs: Seg[], style: PathStyle) { if (segs.length && (style.fill || style.stroke)) this.ops.push({ k: 'path', segs, style }) } /** @@ -167,6 +208,8 @@ export class PdfDrawing { pages: PageDraw[] = [] images: ImageInput[] = [] title = '' + /** bookmarks; `page` counts from 0 in `pages` */ + outline: OutlineItem[] = [] addPage(width: number, height: number): PageDraw { if (!(width > 0 && height > 0 && width <= 14400 && height <= 14400)) throw new PdfRangeError('That page size is not valid.') const p = new PageDraw(width, height) @@ -357,10 +400,21 @@ export async function buildPdf(d: PdfDrawing): Promise<{ bytes: Uint8Array; lost if (pr.shadings.length) res.set('Shading', new Map(pr.shadings.map((sh, i) => [`Sh${i}`, sh]))) if (images.size) res.set('XObject', new Map([...images].map((i) => [`Im${i}`, imageRefs[i]]))) const stream = b.add(new PdfStream(new Map([['Filter', name('FlateDecode')]]), await deflate(ascii(text)))) - kids.push(b.add(new Map([['Type', name('Page')], ['Parent', pagesRef], ['MediaBox', [0, 0, p.width, p.height]], ['Contents', stream], ['Resources', res]]))) + const page = new Map([['Type', name('Page')], ['Parent', pagesRef], ['MediaBox', [0, 0, p.width, p.height]], ['Contents', stream], ['Resources', res]]) + if (p.links.length) { + page.set('Annots', p.links.map((l) => b.add(new Map([ + ['Type', name('Annot')], ['Subtype', name('Link')], ['F', 4], ['Border', [0, 0, 0]], + ['Rect', [l.x, p.height - l.y - l.h, l.x + l.w, p.height - l.y].map((v) => Math.round(v * 100) / 100)], + ['A', new Map([['S', name('URI')], ['URI', new PdfString(ascii(l.uri))]])], + ])))) + } + kids.push(b.add(page)) } b.set(pagesRef, new Map([['Type', name('Pages')], ['Kids', kids], ['Count', kids.length]])) - b.set(catalog, new Map([['Type', name('Catalog')], ['Pages', pagesRef]])) + const cat = new Map([['Type', name('Catalog')], ['Pages', pagesRef]]) + const outlines = writeOutline(b, d.outline, kids) + if (outlines) cat.set('Outlines', outlines) + b.set(catalog, cat) const info = new Map([['Producer', new PdfString(ascii('Ream'))]]) if (d.title) info.set('Title', encodeTextString(d.title)) return { bytes: b.serialize(catalog, b.add(info)), lost: [...lost] } diff --git a/packages/pdf-core/test/build.test.ts b/packages/pdf-core/test/build.test.ts index d6927ac..d52c6dc 100644 --- a/packages/pdf-core/test/build.test.ts +++ b/packages/pdf-core/test/build.test.ts @@ -1,5 +1,5 @@ import { expect, test } from 'vitest' -import { FontFace, PdfDocument, PdfStream, PdfDrawing, buildPdf, decodeStream, isDict, measure, parseTtf } from '../src/index.ts' +import { FontFace, PdfDocument, PdfStream, PdfDrawing, buildPdf, decodeStream, isDict, measure, parseTtf, readOutline, type PdfObj } from '../src/index.ts' import { makeTtf } from './ttf-helper.ts' const dec = new TextDecoder('latin1') @@ -104,3 +104,27 @@ test('paths in page space, turning about a centre, transparency as a graphics st expect(isDict(gs) && [...gs.keys()]).toEqual(['A0']) expect(measure('Hi', 10, { family: 'sans', scale: 0.5 })).toBeCloseTo(measure('Hi', 10, { family: 'sans' }) / 2, 6) }) + +test('links become URI annotations in page space (web and mail only); bookmarks point at their pages', async () => { + const d = new PdfDrawing() + const a = d.addPage(200, 100) + a.link(10, 20, 50, 12, 'https://example.com/a b') + a.link(0, 0, 10, 10, 'javascript:alert(1)') + a.link(0, 0, 10, 10, 'file:///etc/passwd') + a.link(0, 0, 10, 10, 'not an address') + a.link(70, 20, 30, 12, 'mailto:me@example.com') + d.addPage(200, 100) + d.outline = [{ title: 'One', page: 0, fit: [], open: true, children: [] }, { title: 'Two', page: 1, fit: [], open: true, children: [{ title: 'Two.a', page: 1, fit: [], open: true, children: [] }] }] + const doc = await PdfDocument.load((await buildPdf(d)).bytes) + const get = (o: unknown) => doc.resolve(o as PdfObj) as Map + const annots = get(doc.pages()[0].dict.get('Annots')) as unknown as PdfObj[] + expect(annots).toHaveLength(2) + const uri = (a: Map) => dec.decode((get(a.get('A')).get('URI') as unknown as { bytes: Uint8Array }).bytes) + const [web, mail] = annots.map(get) + expect(web.get('Rect')).toEqual([10, 68, 60, 80]) // 100 - 20 - 12 .. 100 - 20 + expect(uri(web)).toBe('https://example.com/a%20b') + expect(uri(mail)).toBe('mailto:me@example.com') + expect(doc.pages()[1].dict.get('Annots')).toBeUndefined() + const marks = readOutline(doc) + expect(marks.map((m) => [m.title, m.page, m.children.map((c) => c.title)])).toEqual([['One', 0, []], ['Two', 1, ['Two.a']]]) +}) diff --git a/packages/tools/src/docx-preview/layout.ts b/packages/tools/src/docx-preview/layout.ts deleted file mode 100644 index c05bcdd..0000000 --- a/packages/tools/src/docx-preview/layout.ts +++ /dev/null @@ -1,156 +0,0 @@ -// A simple page layout of a Word document, for its preview: paragraphs flow down the page and break between lines, -// tables break between rows, pictures sit in the text or where they are placed, text boxes where they are. It uses the -// slide text engine for lines. Approximate by design: no widow control, keep-with-next, columns, headers or footers. -import { PageDraw, PdfDrawing } from '@ream/pdf-core' -import type { DocModel, DocSection, DocTable as Table, Paragraph, Picture, SlidePara } from '@ream/office-core' -import type { FontBook, FontNeed } from '../fonts/book.ts' -import { hexRgb } from '../office-fonts.ts' -import { drawText, layoutText, type Laid } from '../powerpoint-to-pdf/text.ts' - -export type ImageFor = (p: Picture) => number | null - -/** the slide engine's paragraph for a Word paragraph */ -export function slidePara(p: Paragraph): SlidePara { - return { - runs: p.runs.map((r) => ({ ...r, baseline: r.vertAlign === 'super' ? 0.3 : r.vertAlign === 'sub' ? -0.25 : undefined })), - align: p.align, - bullet: p.marker, - marginLeft: p.indentLeft, - indent: p.indentFirst, - // ponytail: "at least" spacing is drawn as exactly that height; a line with bigger type than it is not let grow - line: p.line, - lineFactor: p.line ? undefined : (p.lineFactor ?? 1) * 0.97, // Word's single spacing of Calibri-like faces is about 1.17 em - before: p.before ?? 0, - after: p.after ?? 0, - size: p.runs[0]?.size ?? 11, - } -} - -/** every font the document's text asks for */ -export const fontNeeds = (doc: DocModel): FontNeed[] => { - const out: FontNeed[] = [] - const paras = (b: DocModel['sections'][number]['blocks'][number]): Paragraph[] => (b.kind === 'p' ? [b] : b.kind === 'table' ? b.rows.flat().flatMap((c) => c.paragraphs) : []) - for (const s of doc.sections) for (const b of s.blocks) for (const p of paras(b)) for (const r of p.runs) out.push({ name: r.font, bold: r.bold, italic: r.italic, text: r.text + (p.marker ?? '') }) - return out -} - -const CELL_PAD = 5.4 // Word's default left and right cell margin - -class Flow { - d: PdfDrawing - s: DocSection - page!: PageDraw - y = 0 - book: FontBook - imageFor: ImageFor - constructor(d: PdfDrawing, s: DocSection, book: FontBook, imageFor: ImageFor) { - this.d = d - this.s = s - this.book = book - this.imageFor = imageFor - this.newPage() - } - get left() { return this.s.margins.left } - get width() { return this.s.width - this.s.margins.left - this.s.margins.right } - get bottom() { return this.s.height - this.s.margins.bottom } - newPage() { - this.page = this.d.addPage(this.s.width, this.s.height) - this.y = this.s.margins.top - } - atTop() { return this.y <= this.s.margins.top + 0.5 } - - /** a paragraph in the flow, split between lines where the page ends */ - paragraph(p: Paragraph) { - if (p.pageBreakBefore && !this.atTop()) this.newPage() - if (!this.atTop()) this.y += p.before ?? 0 - const sp = slidePara(p) - const right = p.indentRight ?? 0 - const [laid] = layoutText([{ ...sp, before: 0, after: 0 }], Math.max(10, this.width - right), true, 0, this.book) - for (const ln of laid.lines) { - if (this.y + ln.height > this.bottom && !this.atTop()) this.newPage() - const one: Laid = { ...laid, lines: [ln], bullet: ln === laid.lines[0] ? laid.bullet : undefined, before: 0, after: 0 } - drawText(this.page, [one], { x: this.left, y: this.y, w: Math.max(10, this.width - right), h: ln.height }, 'top') - this.y += ln.height - } - this.y += p.after ?? 0 - } - - /** paragraphs in a text box at a place on the page (or below the paragraph they hang from) */ - framed(group: Paragraph[], anchorY: number) { - const f = group[0].frame! - const x = f.from?.h === 'margin' ? this.left + f.x : f.x - const y = f.from?.v === 'margin' ? this.s.margins.top + f.y : f.from?.v === 'paragraph' ? anchorY + f.y : f.y - const laid = layoutText(group.map(slidePara), Math.max(10, f.w), true, 0, this.book) - drawText(this.page, laid, { x, y, w: f.w, h: f.h ?? 0 }, 'top') - } - - picture(p: Picture, anchorY: number) { - const id = this.imageFor(p) - if (p.at) { - const x = p.from?.h === 'margin' ? this.left + p.at.x : p.at.x - const y = p.from?.v === 'margin' ? this.s.margins.top + p.at.y : p.from?.v === 'paragraph' ? anchorY + p.at.y : p.at.y - if (id !== null) this.page.image(id, x, y, p.width, p.height) - return - } - const k = Math.min(1, this.width / p.width) - const [w, h] = [p.width * k, p.height * k] - if (this.y + h > this.bottom && !this.atTop()) this.newPage() - if (id !== null) this.page.image(id, this.left, this.y, w, h) - else this.page.fillRect(this.left, this.y, w, h, [235, 235, 235]) - this.y += h - } - - table(t: Table) { - const total = t.colWidths.reduce((a, b) => a + b, 0) - const k = total > this.width ? this.width / total : 1 - const xs = [this.left] - for (const w of t.colWidths) xs.push(xs[xs.length - 1] + w * k) - const line = (x0: number, y0: number, x1: number, y1: number) => this.page.line(x0, y0, x1, y1, 0.5, [0, 0, 0]) - // a cell continued from the row above is drawn as part of it: rows are drawn one at a time, so its text stays in its first row - t.rows.forEach((row, ri) => { - let col = 0 - const cells = row.map((c) => { - const span = c.cols ?? 1 - const box = { x0: xs[col], x1: xs[Math.min(xs.length - 1, col + span)], c } - col += span - return box - }) - const laid = cells.map((b) => (b.c.vMerge === 'continue' ? [] : layoutText(b.c.paragraphs.map(slidePara), Math.max(4, b.x1 - b.x0 - 2 * CELL_PAD), true, 0, this.book))) - const textH = laid.map((l) => l.reduce((h, p, i) => h + (i ? p.before : 0) + p.lines.reduce((a, x) => a + x.height, 0) + p.after, 0)) - const h = Math.max(t.rowHeights?.[ri] ?? 0, ...textH, 12) - if (this.y + h > this.bottom && !this.atTop()) this.newPage() - cells.forEach((b, i) => { - if (b.c.fill) this.page.fillRect(b.x0, this.y, b.x1 - b.x0, h, hexRgb(b.c.fill)) - if (laid[i].length) drawText(this.page, laid[i], { x: b.x0 + CELL_PAD, y: this.y, w: b.x1 - b.x0 - 2 * CELL_PAD, h }, 'top') - if (t.borders) { - line(b.x0, this.y, b.x0, this.y + h) - line(b.x1, this.y, b.x1, this.y + h) - if (b.c.vMerge !== 'continue') line(b.x0, this.y, b.x1, this.y) - line(b.x0, this.y + h, b.x1, this.y + h) - } - }) - this.y += h - }) - this.y += t.after ?? 0 - } -} - -/** a Word document as PDF pages */ -export function layoutDocx(d: PdfDrawing, doc: DocModel, book: FontBook, imageFor: ImageFor): void { - for (const s of doc.sections) { - const flow = new Flow(d, s, book, imageFor) - let anchorY = flow.y - for (let i = 0; i < s.blocks.length; i++) { - const b = s.blocks[i] - if (b.kind === 'p' && b.frame) { - const group = [b] - while (i + 1 < s.blocks.length && s.blocks[i + 1].kind === 'p' && (s.blocks[i + 1] as Paragraph).frame === b.frame) group.push(s.blocks[++i] as Paragraph) - flow.framed(group, anchorY) - } else if (b.kind === 'p') { - anchorY = flow.y - flow.paragraph(b) - } else if (b.kind === 'picture') flow.picture(b, anchorY) - else flow.table(b) - } - } -} diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 6df0742..ccbe4ff 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -36,6 +36,7 @@ import { pdfToWord } from './pdf-to-word/manifest.ts' import { pdfToPowerpoint } from './pdf-to-powerpoint/manifest.ts' import { powerpointToPdf } from './powerpoint-to-pdf/manifest.ts' import { excelToPdf } from './excel-to-pdf/manifest.ts' +import { wordToPdf } from './word-to-pdf/manifest.ts' export const tools: Tool[] = [ mergePdf, @@ -45,6 +46,7 @@ export const tools: Tool[] = [ pdfToWord, pdfToExcel, pdfToPowerpoint, + wordToPdf, excelToPdf, powerpointToPdf, watermark, diff --git a/packages/tools/src/m7b-tools.test.ts b/packages/tools/src/m7b-tools.test.ts index 0066bc7..41a09b3 100644 --- a/packages/tools/src/m7b-tools.test.ts +++ b/packages/tools/src/m7b-tools.test.ts @@ -2,11 +2,11 @@ import { readFileSync } from 'node:fs' import { beforeAll, describe, expect, test } from 'vitest' import type { FileData } from '@ream/core' import { decodeJpeg, decodePng, ownCodec, setCodec } from '@ream/image-core' -import { PdfDocument } from '@ream/pdf-core' +import { PdfDocument, readOutline } from '@ream/pdf-core' import type { PageRaster } from '@ream/pdf-render' import { readXlsx, writeXlsx, writePptx, parseXml, kids, child, type Sheet, type XmlNode } from '@ream/office-core' import { unzip } from '@ream/core' -import { extractText } from '@ream/pdf-render' +import { extractText, pageChars } from '@ream/pdf-render' import { assemble, classicPdf } from '../../pdf-core/test/helpers.ts' import { run as toImage } from './pdf-to-image/run.ts' import { run as toExcel } from './pdf-to-excel/run.ts' @@ -18,7 +18,10 @@ import { readPptx } from '@ream/office-core' import { familyOf } from './office-fonts.ts' import { nodeFonts } from './fonts/node.ts' import { parseNumber } from './pdf-to-excel/number.ts' -import { docxToPdf, officeToPdf } from './office-preview.ts' +import { officeToPdf } from './office-preview.ts' +import { docxToPdf, run as wordToPdf } from './word-to-pdf/run.ts' +import { PIXEL, inline, makeDocx, para, part, placed, scramble, withFonts } from '../../office-core/test/docx-helper.ts' +import { makeTtf } from '../../pdf-core/test/ttf-helper.ts' beforeAll(() => setCodec(ownCodec)) // JPG pages go through the codec; the browser's is checked in the app @@ -407,7 +410,7 @@ describe('office previews', () => { const pageText = async (bytes: Uint8Array, page: number) => (await extractText(await PdfDocument.load(bytes), [page], { markdown: false }))[0].replace(/\s+/g, ' ') test('a Word document is laid out on its pages: the page break starts page 2, text in order, a picture drawn', async () => { - const pdf = await docxToPdf(fx('report.docx').bytes, nodeFonts) + const pdf = await docxToPdf(fx('report.docx').bytes, [], nodeFonts) const doc = await PdfDocument.load(pdf) expect(doc.pages()).toHaveLength(2) expect(doc.pageSize(doc.pages()[0])).toEqual({ width: 612, height: 792 }) @@ -424,3 +427,274 @@ describe('office previews', () => { expect(await pageText(await officeToPdf(fx('charts.xlsx'), 'xlsx', nodeFonts), 0)).toContain('Traffic') }) }) + +describe('word to pdf', () => { + const fx = (n: string): FileData => ({ name: n, type: '', bytes: new Uint8Array(readFileSync(new URL(`../../office-core/test/fixtures/${n}`, import.meta.url))) }) + const pageText = async (bytes: Uint8Array, page: number) => (await extractText(await PdfDocument.load(bytes), [page], { markdown: false }))[0].replace(/\s+/g, ' ') + const footerStyle = '' + const pg = '' + const field = (code: string, shown: string) => ` ${code} ${shown}` + const pdfOf = async (docx: Promise) => docxToPdf(await docx, [], nodeFonts) + + test('headers and footers repeat with their own page numbers; the first page can differ; a footer\'s tabs put its text at the stops', async () => { + const pdf = await pdfOf( + makeDocx({ + styles: footerStyle, + rels: [['h', 'header', 'h.xml'], ['f', 'footer', 'f.xml'], ['h1', 'header', 'h1.xml'], ['f1', 'footer', 'f1.xml']], + parts: { + 'word/h.xml': part('hdr', para('Running head')), + 'word/h1.xml': part('hdr', para('Cover')), + 'word/f1.xml': part('ftr', para('Cover footer')), + 'word/f.xml': part('ftr', 'LeftPage ' + field('PAGE', '1') + ' of 1'), + }, + body: para('One') + para('Two', '') + `${pg}`, + }), + ) + const doc = await PdfDocument.load(pdf) + expect(doc.pages()).toHaveLength(2) + const [one, two] = [await pageText(pdf, 0), await pageText(pdf, 1)] + expect(one).toContain('Cover') + expect(one).toContain('Cover footer') + expect(one).not.toContain('Running head') + expect(two).toContain('Running head') + expect(two).toContain('Left') + expect(two).toContain('Page 2 of 2') + expect(two).not.toContain('Cover') + // the footer is at the bottom (these y count from the top), its right part ends at the right margin (612 - 72) and its left part starts at the left one + const { chars } = await pageChars(doc, 1) + const text = chars.map((c) => c.text).join('') + const right = chars[text.indexOf('Page 2 of 2') + 10] + const left = chars[text.indexOf('Left')] + expect(right.x1).toBeGreaterThan(538) + expect(right.x1).toBeLessThan(541) + expect(left.x0).toBeCloseTo(72, 0) + expect(right.y1).toBeGreaterThan(792 - 72) + expect(chars[text.indexOf('Running')].y1).toBeLessThan(72) + }) + + test('a header taller than the top margin pushes the body down', async () => { + const body = (header: boolean) => makeDocx({ rels: header ? [['h', 'header', 'h.xml']] : [], parts: header ? { 'word/h.xml': part('hdr', 'Big') } : {}, body: para('Body') + `${header ? '' : ''}${pg}` }) + const top = async (header: boolean) => { + const { chars } = await pageChars(await PdfDocument.load(await pdfOf(body(header))), 0) + return chars.find((c) => c.text === 'B' && c.size < 20)!.y1 + } + // the header (36 pt from the edge, a 60 pt line) ends below the 72 pt margin + expect(await top(false)).toBeLessThan(90) + expect(await top(true)).toBeGreaterThan(115) + }) + + // exact 12 pt lines, 54 to a page (648 pt between the margins); `lines` makes a paragraph of that many lines + const lines = (id: string, n: number, props = '') => `${props}${Array.from({ length: n }, (_, i) => `${id}${i + 1}`).join('')}` + const filler = (n: number) => Array.from({ length: n }, (_, i) => lines(`F${i + 1}x`, 1)).join('') + const split = async (fill: number, tail: string) => { + const pdf = await pdfOf(makeDocx({ body: filler(fill) + tail + `${pg}` })) + return [await pageText(pdf, 0), await pageText(pdf, 1)] + } + + test('a heading with "keep with next" never ends a page alone; a paragraph is not split to leave one line on either side', async () => { + const heading = (keep: boolean) => lines('Head', 1, keep ? '' : '') + lines('Body', 3) + // 52 lines used, room for 2: the heading would fit, but not with two lines of what follows + const [kept, keptNext] = await split(52, heading(true)) + expect(kept).not.toContain('Head1') + expect(keptNext).toContain('Head1 Body1 Body2 Body3') + const [loose] = await split(52, heading(false)) + expect(loose).toContain('Head1') + expect(loose).not.toContain('Body1') // its first line alone would be an orphan, so all three go to the next page + // widows: 3 lines, room for 2: the last line can't stand alone, so none stay + const [w1, w2] = await split(52, lines('W', 3)) + expect(w1).not.toContain('W1') + expect(w2).toContain('W1 W2 W3') + // with widow control off it splits after the second line + const [o1, o2] = await split(52, lines('W', 3, '')) + expect(o1).toContain('W2') + expect(o2).toContain('W3') + expect(o2).not.toContain('W2') + }) + + test('"keep lines together" moves a whole paragraph to the next page', async () => { + const [free1, free2] = await split(50, lines('K', 5)) + expect(free1).toContain('K3') // room for 4 lines, the fifth can't be alone: 3 stay + expect(free2).toContain('K4 K5') + const [kept1, kept2] = await split(50, lines('K', 5, '')) + expect(kept1).not.toContain('K1') + expect(kept2).toContain('K1 K2 K3 K4 K5') + // a heading moved this way is bookmarked on the page it is drawn on + const pdf = await pdfOf(makeDocx({ body: filler(50) + lines('K', 5, '') + `${pg}` })) + expect(readOutline(await PdfDocument.load(pdf)).map((m) => [m.title, m.page])).toEqual([['K1 K2 K3 K4 K5', 1]]) + }) + + test('links are clickable (web and mail only), headings are nested bookmarks', async () => { + const styles = ['1', '2'].map((n) => ``).join('') + const pdf = await pdfOf( + makeDocx({ + styles, + rels: [['a', 'hyperlink', 'https://example.com/a', true], ['m', 'hyperlink', 'mailto:me@example.com', true], ['j', 'hyperlink', 'javascript:alert(1)', true]], + body: + para('Intro', '') + + 'See the site or mailbad' + + para('Detail', '') + + para('Page two', '') + + `${pg}`, + }), + ) + const doc = await PdfDocument.load(pdf) + const annots = (doc.resolve(doc.pages()[0].dict.get('Annots')) as unknown[]).map((r) => doc.resolve(r as never) as Map) + expect(annots).toHaveLength(2) + const uris = annots.map((a) => new TextDecoder().decode(((doc.resolve(a.get('A') as never) as Map).get('URI')!).bytes)) + expect(uris).toEqual(['https://example.com/a', 'mailto:me@example.com']) + const [x0, y0, x1, y1] = annots[0].get('Rect') as number[] + const { chars } = await pageChars(doc, 0) + const at = chars.map((c) => c.text).join('').indexOf('the site') + expect(x0).toBeCloseTo(chars[at].x0, 0) // over the words, not somewhere else + expect(x1).toBeCloseTo(chars[at + 7].x1, 0) + // the box is in PDF space (y up), the letters count from the top: the box covers the letters' height + expect(y1 - y0).toBeGreaterThan(8) + expect(792 - y1).toBeLessThan(chars[at].y0 + 1) + expect(792 - y0).toBeGreaterThan(chars[at].y1 - 1) + expect(readOutline(doc).map((m) => [m.title, m.page, m.children.map((c) => [c.title, c.page])])).toEqual([['Intro', 0, [['Detail', 0]]], ['Page two', 1, []]]) + }) + + // 40 lines of words with a 144 pt picture anchored in the first paragraph; margins 72 pt, so the picture's box with 9 pt + // clear all round is x 63..225, y 63..225 on a page that is 612 wide + const words = Array.from({ length: 600 }, (_, i) => `word${i}`).join(' ') + const withPicture = async (wrap: 'square' | 'topBottom', y = 0) => { + const pdf = await pdfOf(makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: `${words}${placed(wrap, 144, 0, y)}${pg}` })) + const doc = await PdfDocument.load(pdf) + return { doc, chars: (await pageChars(doc, 0)).chars.filter((c) => c.text.trim()), pdf } + } + + test('text goes around a picture with square wrapping: beside it, never over it, and the full width again below it', async () => { + const { chars, pdf } = await withPicture('square') + expect(new TextDecoder('latin1').decode(pdf)).toContain('/Subtype /Image') // drawn (a see-through picture is an image and its mask) + // every letter level with the picture starts to its right (its box ends at 225) + const level = chars.filter((c) => c.y1 > 63 && c.y0 < 225) + expect(level.length).toBeGreaterThan(50) + expect(Math.min(...level.map((c) => c.x0))).toBeGreaterThan(224) + // lines below it use the whole width again, and the lines beside it are narrower than that (they wrap at the right margin, 540) + expect(Math.min(...chars.filter((c) => c.y0 > 226).map((c) => c.x0))).toBeLessThan(75) + expect(Math.max(...level.map((c) => c.x1))).toBeLessThan(541) + }) + + test('a picture goes with the paragraph it is anchored in when that paragraph moves to the next page', async () => { + const five = `${['K1', 'K2', 'K3', 'K4', 'K5'].map((t) => `${t}`).join('')}${placed('square', 40)}` + const doc = await PdfDocument.load(await pdfOf(makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: filler(50) + five + `${pg}` }))) + const pictures = (i: number) => { + const xo = (doc.resolve(doc.pages()[i].dict.get('Resources')) as Map).get('XObject') + const d = xo ? (doc.resolve(xo) as Map) : undefined + return d?.size ?? 0 + } + expect(doc.pages()).toHaveLength(2) + expect(pictures(0)).toBe(0) + expect(pictures(1)).toBeGreaterThan(0) + }) + + test('with top and bottom wrapping no text is level with the picture: it goes above or below', async () => { + const { chars } = await withPicture('topBottom', 36) // the box is y 99..261 + expect(chars.some((c) => c.y1 <= 99.5)).toBe(true) + expect(chars.some((c) => c.y0 >= 260.5)).toBe(true) + expect(chars.filter((c) => c.y1 > 99.5 && c.y0 < 260.5)).toEqual([]) + }) + + test('a page number field that has no saved result still shows the number', async () => { + const pdf = await pdfOf( + makeDocx({ + rels: [['f', 'footer', 'f.xml']], + parts: { 'word/f.xml': part('ftr', 'Page PAGE') }, + body: para('One') + para('Two', '') + `${pg}`, + }), + ) + expect(await pageText(pdf, 0)).toContain('Page 12') + expect(await pageText(pdf, 1)).toContain('Page 22') + }) + + test('a font stored in the document is used for its text, unless its licence forbids embedding', async () => { + const guid = '{00112233-4455-6677-8899-AABBCCDDEEFF}' + const run = 'AAA' + const latin1 = (b: Uint8Array) => new TextDecoder('latin1').decode(b) + const made = async (font: Uint8Array) => latin1(await pdfOf(withFonts([['Quirky', scramble(font, guid), guid]], run + `${pg}`))) + expect(await made(makeTtf())).toContain('TestFont') // its own name is in the file's font, not the bundled stand-in's + expect(await made(makeTtf({ fsType: 2 }))).not.toContain('TestFont') // "restricted licence": left out, the look-alike is used + }) + + const pictureDoc = (body: string) => makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: `${body}${pg}` }) + const picturesOn = async (body: string) => { + const doc = await PdfDocument.load(await pdfOf(pictureDoc(body))) + const page = await pageChars(doc, 0) + return { pictures: page.pictures, chars: page.chars.filter((c) => c.text.trim()) } + } + + test('a picture in a line goes where its paragraph puts it, on the baseline, with the text around it', async () => { + // centred: the middle of the 468 pt column is x = 306 + const { pictures: [centred] } = await picturesOn(`${inline(100, 50)}`) + expect((centred.x0 + centred.x1) / 2).toBeCloseTo(306, 0) + expect(centred.x1 - centred.x0).toBeCloseTo(100, 0) + expect(centred.y0).toBeCloseTo(72, 0) // the line starts at the top margin, so the picture does (it isn't pushed down by an empty line first) + // in the middle of text: the words come before and after it, on its line, and the picture stands on their baseline + const { pictures: [mid], chars } = await picturesOn(`AAA ${inline(40)} BBB`) + const x = (s: string) => chars.find((c) => c.text === s)! + expect(x('A').x1).toBeLessThan(mid.x0) + expect(mid.x1).toBeLessThan(x('B').x0) + expect(mid.y1).toBeCloseTo(x('A').y0, 0) // a letter's place is its baseline: the picture's bottom edge stands on it + expect(mid.y1 - mid.y0).toBeCloseTo(40, 0) + }) + + test('a picture\'s crop decides what shows in its box: a negative one adds empty room (a thin divider in a taller box), a positive one cuts', async () => { + // a 100 x 50 box whose picture is cut by nothing at the top but given 566.667% of its own height as room below: it is 1/6.67 of the box tall, at the top + const { pictures: [line] } = await picturesOn(`${inline(100, 50, '')}`) + expect(line.x1 - line.x0).toBeCloseTo(100, 0) + expect(line.y1 - line.y0).toBeCloseTo(7.5, 0) + expect(line.y0).toBeCloseTo(72, 0) + // a quarter cut from the left: the whole picture is drawn a third wider than the box, so its left part is outside it (the box clips) + const { pictures: [cut] } = await picturesOn(`${inline(100, 50, '')}`) + expect(cut.x1 - cut.x0).toBeCloseTo(133.3, 0) + expect(cut.x0).toBeCloseTo(72 - 33.3, 0) + // without a crop nothing changes + const { pictures: [plain] } = await picturesOn(`${inline(100, 50)}`) + expect([plain.x1 - plain.x0, plain.y1 - plain.y0].map(Math.round)).toEqual([100, 50]) + }) + + test('a picture too wide for the column, or taller than a page, is shrunk to fit with its shape kept', async () => { + const { pictures: [wide] } = await picturesOn(`${inline(1000, 500)}`) + expect(wide.x1 - wide.x0).toBeLessThanOrEqual(468.5) + expect((wide.y1 - wide.y0) / (wide.x1 - wide.x0)).toBeCloseTo(0.5, 2) + const { pictures: [tall] } = await picturesOn(`${inline(100, 3000)}`) + expect(tall.y1 - tall.y0).toBeLessThanOrEqual(648) + expect((tall.y1 - tall.y0) / (tall.x1 - tall.x0)).toBeCloseTo(30, 0) + }) + + test('text in a table cell taller than it is centred or bottom aligned inside the cell\'s margins', async () => { + const cell = (props: string) => `${props}${para('X')}` + const at = async (props: string) => { + const { chars } = await picturesOn(cell(props)) + return (chars[0].y0 + chars[0].y1) / 2 - 72 // from the top of the 72 pt row + } + expect(await at('')).toBeLessThan(14) + expect(await at('')).toBeCloseTo(36, -1) + expect(await at('')).toBeGreaterThan(58) + // a margin of 20 pt above the text moves it down by that much + expect((await at('')) - (await at(''))).toBeCloseTo(20, 0) + }) + + test('numbers that are not numbers, or absurd ones, in a file never stop the conversion (a header found one that looped for ever)', async () => { + const pdf = await pdfOf( + makeDocx({ + rels: [['h', 'header', 'h.xml']], + parts: { 'word/h.xml': part('hdr', 'Head') }, + body: `Body text`, + }), + ) + expect((await PdfDocument.load(pdf)).pages().length).toBeGreaterThan(0) + expect(await pageText(pdf, 0)).toContain('Head') + }) + + test('run: the file and its errors', async () => { + const f = (name: string, bytes: Uint8Array) => ({ name, type: '', bytes }) + const docx = await makeDocx({ body: para('Hello') + `${pg}` }) + const [out] = await wordToPdf([f('Report.docx', docx)], { localFonts: [] }, { progress: () => {} }, nodeFonts) + expect(out.name).toBe('Report.pdf') + expect(await pageText(out.bytes, 0)).toContain('Hello') + const ole = new Uint8Array([0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1, 0, 0, 0, 0]) + await expect(wordToPdf([f('old.doc', ole)], { localFonts: [] }, { progress: () => {} }, nodeFonts)).rejects.toThrow(/^old\.doc: This is an older Word file \(\.doc\).*\.docx/) + await expect(wordToPdf([f('book.docx', fx('charts.xlsx').bytes)], { localFonts: [] }, { progress: () => {} }, nodeFonts)).rejects.toThrow(/book\.docx: .*isn't a Word document/) + }) +}) diff --git a/packages/tools/src/office-preview.ts b/packages/tools/src/office-preview.ts index 4e23f4a..3fe23ec 100644 --- a/packages/tools/src/office-preview.ts +++ b/packages/tools/src/office-preview.ts @@ -1,40 +1,18 @@ -// A PDF of an Office file for the preview: the same converters as Excel to PDF and PowerPoint to PDF with their default -// settings and the bundled fonts, and a simple page layout for Word documents (Word to PDF is not a tool). +// A PDF of an Office file for the preview: the same converters as Word to PDF, Excel to PDF and PowerPoint to PDF with +// their default settings and the bundled fonts. import type { FileData } from '@ream/core' -import { readDocx } from '@ream/office-core' -import { PdfDrawing, buildPdf, type ImageInput } from '@ream/pdf-core' -import { layoutDocx, fontNeeds } from './docx-preview/layout.ts' -import { FontBook, type FontSource } from './fonts/book.ts' +import type { FontSource } from './fonts/book.ts' import { browserFonts } from './fonts/source.ts' -import { imageInput } from './office-images.ts' import { run as excelToPdf } from './excel-to-pdf/run.ts' import { run as powerpointToPdf } from './powerpoint-to-pdf/run.ts' +import { docxToPdf } from './word-to-pdf/run.ts' export type OfficeKind = 'pptx' | 'xlsx' | 'docx' -export async function docxToPdf(bytes: Uint8Array, fonts: FontSource = browserFonts): Promise { - const doc = await readDocx(bytes) - const book = new FontBook(fonts) - await book.load(fontNeeds(doc)) - const decoded = new Map() - for (const s of doc.sections) for (const b of s.blocks) if (b.kind === 'picture' && !decoded.has(b.bytes)) decoded.set(b.bytes, await imageInput(b.bytes, b.type)) - const d = new PdfDrawing() - const ids = new Map() - layoutDocx(d, doc, book, (p) => { - if (!ids.has(p.bytes)) { - const input = decoded.get(p.bytes) - ids.set(p.bytes, input ? d.addImage(input) : null) - } - return ids.get(p.bytes)! - }) - if (!d.pages.length) d.addPage(doc.sections[0]?.width ?? 595, doc.sections[0]?.height ?? 842) - return (await buildPdf(d)).bytes -} - /** the preview PDF of an Office file */ export async function officeToPdf(file: FileData, kind: OfficeKind, fonts: FontSource = browserFonts): Promise { const ctx = { progress: () => {} } - if (kind === 'docx') return docxToPdf(file.bytes, fonts) + if (kind === 'docx') return docxToPdf(file.bytes, [], fonts) if (kind === 'xlsx') return (await excelToPdf([file], { setup: 'file', paper: 'a4', orientation: 'auto', fit: 'width', gridlines: false, localFonts: [] }, ctx, fonts))[0].bytes return (await powerpointToPdf([file], { hidden: false, localFonts: [] }, ctx, fonts))[0].bytes } diff --git a/packages/tools/src/panels.ts b/packages/tools/src/panels.ts index 3f665d6..fd6c560 100644 --- a/packages/tools/src/panels.ts +++ b/packages/tools/src/panels.ts @@ -84,5 +84,6 @@ export const panels: Record Promise> = { 'pdf-to-word': () => import('./pdf-to-word/Panel.tsx'), 'pdf-to-powerpoint': () => import('./pdf-to-powerpoint/Panel.tsx'), 'powerpoint-to-pdf': () => import('./powerpoint-to-pdf/Panel.tsx'), + 'word-to-pdf': () => import('./word-to-pdf/Panel.tsx'), 'excel-to-pdf': () => import('./excel-to-pdf/Panel.tsx'), } diff --git a/packages/tools/src/powerpoint-to-pdf/text.ts b/packages/tools/src/powerpoint-to-pdf/text.ts index 19f754c..e3b235c 100644 --- a/packages/tools/src/powerpoint-to-pdf/text.ts +++ b/packages/tools/src/powerpoint-to-pdf/text.ts @@ -9,8 +9,12 @@ import { hexRgb } from '../office-fonts.ts' /** what a line of type is as tall as, in em: the usual ascent and descent of the fonts slides use */ const EM_HEIGHT = 1.2 -type Piece = { text: string; run: SlideRun; font: FontSpec; size: number; rise: number; width: number; space: boolean } -type Line = { pieces: Piece[]; width: number; size: number; height: number; start: number; avail: number; last: boolean } +/** `picture`: how tall a picture in the line is (its width is `width`); the run holds the picture itself */ +type Piece = { text: string; run: SlideRun; font: FontSpec; size: number; rise: number; width: number; space: boolean; picture?: number } +/** `gap`: room left empty above the line (Word moves a line down past a picture it can't sit beside); `base`: where the baseline is below the top of the line, when a picture in it decides that */ +type Line = { pieces: Piece[]; width: number; size: number; height: number; start: number; avail: number; last: boolean; gap?: number; base?: number } +/** where a line may go: `dx` from the left of the text area, `w` wide; `skip` says there's no room at this height and the line goes that much lower */ +export type Slot = { dx: number; w: number; skip?: number } type Mark = 'disc' | 'circle' | 'square' | 'box' | 'diamond' | 'arrowhead' | 'check' | 'arrow' export type Laid = { para: SlidePara; lines: Line[]; before: number; after: number; bullet?: { text: string; mark?: Mark; font: FontSpec; size: number; x: number; color: string | undefined } } @@ -30,8 +34,12 @@ function markOf(char: string, font: string | undefined): Mark | undefined { /** a run's text as shown: capitals for 'all' and (ponytail: drawn as capitals at 80%) 'small' */ const shown = (r: SlideRun) => (r.caps ? r.text.toUpperCase() : r.text) -/** every paragraph's lines for a text area `width` wide; list numbering carries from one paragraph to the next */ -export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lineReduction: number, book: FontBook): Laid[] { +/** + * every paragraph's lines for a text area `width` wide; list numbering carries from one paragraph to the next. + * `slot` (Word's text around pictures) says where each line may go, given the height reached so far in the paragraph and the + * line's likely height; without it every line has the whole width. + */ +export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lineReduction: number, book: FontBook, slot?: (offset: number, height: number) => Slot): Laid[] { const out: Laid[] = [] const counters = new Map() for (const para of paras) { @@ -54,6 +62,12 @@ export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lin for (const run of para.runs) { const font = fontFor(run, book) const base = run.size ?? 18 + if (run.picture) { + // a picture in the line is a piece as wide as it is, shrunk to fit a line that is too narrow for it + const k = Math.min(1, Math.max(10, width - Math.max(start0, marL)) / run.picture.width) + tokens.push({ text: '', run, font, size: base, rise: 0, width: run.picture.width * k, space: false, picture: run.picture.height * k }) + continue + } // raised and lowered text is drawn smaller, the way PowerPoint does const size = run.baseline ? base * 0.67 : run.caps === 'small' ? base * 0.8 : base const rise = run.baseline ? run.baseline * base : 0 @@ -65,20 +79,44 @@ export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lin const lines: Line[] = [] let cur: Piece[] = [] let w = 0 + // where the line being made may go: the whole width, or what `slot` leaves (a line with no room is moved down) + const guess = para.line ?? (para.lineFactor ?? 1) * (1 - lineReduction) * EM_HEIGHT * (para.size ?? 18) + let offset = 0 + let gap = 0 + let box: Slot = { dx: 0, w: width } + const open = () => { + gap = 0 + box = slot ? slot(offset, guess) : { dx: 0, w: width } + for (let n = 0; slot && box.skip && n < 50; n++) { + gap += box.skip + offset += box.skip + box = slot(offset, guess) + } + } + open() const finish = (last: boolean) => { while (cur.length && cur[cur.length - 1].space) w -= cur.pop()!.width const size = Math.max(...cur.map((p) => (p.rise ? p.size / 0.67 : p.size)), cur.length ? 0 : para.size ?? 18) const first = lines.length === 0 - const avail = Math.max(1, width - (first ? start0 : marL)) - const lh = para.line ?? (para.lineFactor ?? 1) * (1 - lineReduction) * EM_HEIGHT * size - lines.push({ pieces: cur, width: w, size, height: lh, start: first ? start0 : marL, avail, last }) + const avail = Math.max(1, box.w - (first ? start0 : marL)) + let lh = para.line ?? (para.lineFactor ?? 1) * (1 - lineReduction) * EM_HEIGHT * size + // a picture stands on the baseline with the text's descent below it: the line is as tall as that, and the baseline is there + const pic = Math.max(0, ...cur.map((p) => p.picture ?? 0)) + let base: number | undefined + if (pic && pic + 0.27 * size > lh) { + lh = pic + 0.27 * size + base = pic + } + lines.push({ pieces: cur, width: w, size, height: lh, start: box.dx + (first ? start0 : marL), avail, last, gap: gap || undefined, base }) + offset += lh cur = [] w = 0 + open() } for (const tk of tokens) { if (tk === 'break') { finish(true); continue } const first = lines.length === 0 - const avail = width - (first ? start0 : marL) + const avail = box.w - (first ? start0 : marL) if (wrap && !tk.space && cur.some((p) => !p.space) && w + tk.width > avail) finish(false) if (!(tk.space && !cur.length)) { cur.push(tk); w += tk.width } } @@ -88,7 +126,7 @@ export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lin return out } -export const textHeight = (laid: Laid[]): number => laid.reduce((h, l, i) => h + (i ? l.before : 0) + l.lines.reduce((a, ln) => a + ln.height, 0) + l.after, 0) +export const textHeight = (laid: Laid[]): number => laid.reduce((h, l, i) => h + (i ? l.before : 0) + l.lines.reduce((a, ln) => a + ln.height + (ln.gap ?? 0), 0) + l.after, 0) /** a bullet mark drawn as a shape: a box `s` tall with its bottom on the baseline */ function drawMark(page: PageDraw, mark: Mark, x: number, baseline: number, s: number, color: [number, number, number]) { @@ -108,15 +146,19 @@ function drawMark(page: PageDraw, mark: Mark, x: number, baseline: number, s: nu else page.path([['M', cx - r, cy], ['L', cx + r, cy], ['M', cx + r * 0.3, cy - r * 0.6], ['L', cx + r, cy], ['L', cx + r * 0.3, cy + r * 0.6]], { stroke: color, width: s * 0.08 }) } -/** draw laid out text in the area; `anchor` places the block in it. Overflow is drawn, as slides do. */ -export function drawText(page: PageDraw, laid: Laid[], area: { x: number; y: number; w: number; h: number }, anchor: 'top' | 'middle' | 'bottom'): void { +/** where one run of a line was drawn: `baseline` is the y of its letters' feet, `width` includes the room justifying adds */ +export type RunBox = { x: number; baseline: number; width: number; size: number; run: SlideRun } + +/** draw laid out text in the area; `anchor` places the block in it. Overflow is drawn, as slides do. `onRun` hears where each run went (Word's links). */ +export function drawText(page: PageDraw, laid: Laid[], area: { x: number; y: number; w: number; h: number }, anchor: 'top' | 'middle' | 'bottom', onRun?: (box: RunBox) => void): void { const total = textHeight(laid) let y = anchor === 'middle' ? area.y + (area.h - total) / 2 : anchor === 'bottom' ? area.y + area.h - total : area.y laid.forEach((l, pi) => { if (pi) y += l.before l.lines.forEach((ln, li) => { + y += ln.gap ?? 0 const s = ln.size - const baseline = y + ln.height - 0.27 * s - Math.max(0, (ln.height - EM_HEIGHT * s) / 2) + const baseline = ln.base !== undefined ? y + ln.base : y + ln.height - 0.27 * s - Math.max(0, (ln.height - EM_HEIGHT * s) / 2) const align = l.para.align ?? 'left' let x = area.x + ln.start if (align === 'center') x = area.x + ln.start + (ln.avail - ln.width) / 2 @@ -146,6 +188,7 @@ export function drawText(page: PageDraw, laid: Laid[], area: { x: number; y: num if (p.run.highlight) page.fillRect(x, baseline - s * 0.9, width, s * 1.15, hexRgb(p.run.highlight)) page.text(x, baseline - p.rise, text, p.font, p.size, color, p.run.alpha, p.run.spacing) if (p.run.underline) page.line(x, baseline + 0.12 * p.size, x + width, baseline + 0.12 * p.size, Math.max(0.4, p.size / 18), color) + onRun?.({ x, baseline: baseline - p.rise, width, size: p.size, run: p.run }) x += width i = j } diff --git a/packages/tools/src/runners.ts b/packages/tools/src/runners.ts index a1d75a0..3faa2c5 100644 --- a/packages/tools/src/runners.ts +++ b/packages/tools/src/runners.ts @@ -38,5 +38,6 @@ export const runners: Record Promise<{ run: RunFn }>> = { 'pdf-to-word': () => import('./pdf-to-word/run.ts'), 'pdf-to-powerpoint': () => import('./pdf-to-powerpoint/run.ts'), 'powerpoint-to-pdf': () => import('./powerpoint-to-pdf/run.ts'), + 'word-to-pdf': () => import('./word-to-pdf/run.ts'), 'excel-to-pdf': () => import('./excel-to-pdf/run.ts'), } diff --git a/packages/tools/src/seo/convert.ts b/packages/tools/src/seo/convert.ts index daaafa0..7726ac4 100644 --- a/packages/tools/src/seo/convert.ts +++ b/packages/tools/src/seo/convert.ts @@ -51,6 +51,17 @@ export const convertSeo: Record = { ['Does it work with scanned PDFs?', 'No. A scan has no text to place on the slides, and Ream has no text recognition (OCR) yet.'], ], }, + 'word-to-pdf': { + title: 'Convert Word (DOCX) to PDF online | Ream', + description: 'Save a Word document as a PDF with its headers, footers, page numbers, links and pictures. Runs in your browser; nothing is uploaded.', + intro: 'Turn a .docx into a PDF to share or print, without opening Word. Ream reads the document and lays out its pages itself, with headers and footers, page numbers, tables, pictures and text boxes, clickable links and a bookmark for every heading. A preview shows the result before you download.', + points: ['Headers, footers and page numbers on every page', 'Clickable links and bookmarks from your headings', 'Fonts: your installed ones, look-alikes, or ones you add'], + faq: [ + ['Will it look exactly like it does in Word?', 'Close, not identical. Ream lays the pages out itself and uses look-alike fonts when yours are not available, so a line or a page break can fall in a different place. Documents set in Aptos are the usual case, since it has no free look-alike. The preview is marked approximate for that reason, and you can add the fonts the document uses to get closer.'], + ['Does it work with older .doc files?', 'Only .docx files. In Word, choose Save As and pick Word Document (.docx) first. A document with a password has to be saved without it.'], + ['What is not carried over?', 'Footnotes, columns, table styles, a different header on even pages, shapes other than pictures and text boxes, and comments are not drawn. A table of contents keeps the page numbers saved in the file.'], + ], + }, 'excel-to-pdf': { title: 'Convert Excel (XLSX) to PDF online | Ream', description: 'Print an Excel workbook to PDF with its formatting, merged cells, pictures and charts. Runs in your browser; nothing is uploaded.', diff --git a/packages/tools/src/word-to-pdf/Panel.tsx b/packages/tools/src/word-to-pdf/Panel.tsx new file mode 100644 index 0000000..fe9b1fd --- /dev/null +++ b/packages/tools/src/word-to-pdf/Panel.tsx @@ -0,0 +1,18 @@ +import type { PanelProps } from '../panels.ts' +import { FontsPanel } from '../fonts/FontsPanel.tsx' +import type { WordToPdfOptions as O } from './run.ts' + +export const defaultOptions: O = { localFonts: [] } + +export function Panel({ options: o, onChange, files, readOfficeFonts, readFontFile }: PanelProps) { + return ( +
+ Pages +

Each section keeps its own paper size and margins, with its headers, footers and page numbers. Links in the text stay clickable and headings become bookmarks.

+ onChange({ ...o, localFonts })} /> +

+ Fonts are embedded: fonts stored in the document are used as they are (Google Docs saves the ones it uses), then the file's own fonts when they are on this computer, added or downloaded; otherwise Calibri, Cambria, Arial, Times New Roman and Courier New become look-alikes with the same letter widths, and other fonts the closest of those, so lines and page breaks can fall a little differently than in Word (a document set in Aptos is the usual case). Letters none of them have (such as Chinese or Japanese) show as ?. Only .docx files open here, not older .doc files. Footnotes, columns, table styles, a different header on even pages, shapes other than pictures and text boxes, and comments are not drawn, and a table of contents shows the page numbers saved in the file. +

+
+ ) +} diff --git a/packages/tools/src/word-to-pdf/layout.ts b/packages/tools/src/word-to-pdf/layout.ts new file mode 100644 index 0000000..7018a62 --- /dev/null +++ b/packages/tools/src/word-to-pdf/layout.ts @@ -0,0 +1,520 @@ +// A page layout of a Word document, for its preview and for Word to PDF: paragraphs flow down the page and break between +// lines (never leaving one line alone at the top or bottom of a page, keeping lines and headings with what follows when +// the document says so), tables break between rows, pictures sit in the text or where they are placed, text boxes where +// they are. Headers and footers repeat on every page with their page numbers, links are clickable and headings are +// bookmarks. It uses the slide text engine for lines. Approximate by design: no columns, footnotes or table styles. +import { PageDraw, PdfDrawing, type OutlineItem } from '@ream/pdf-core' +import type { DocModel, DocSection, DocBlock, DocTable as Table, Paragraph, Picture, SlidePara } from '@ream/office-core' +import type { FontBook, FontNeed } from '../fonts/book.ts' +import { hexRgb } from '../office-fonts.ts' +import { drawText, layoutText, type Laid, type RunBox, type Slot } from '../powerpoint-to-pdf/text.ts' + +export type ImageFor = (p: Picture) => number | null + +/** the slide engine's paragraph for a Word paragraph; a picture in a line is kept within `maxHeight` (a page's worth) */ +export function slidePara(p: Paragraph, maxHeight = Infinity): SlidePara { + const fit = (pic: Picture): Picture => (pic.height > maxHeight ? { ...pic, width: (pic.width * maxHeight) / pic.height, height: maxHeight } : pic) + return { + // a tab in running text is a gap (header and footer tabs are placed by `Flow.tabbed`) + runs: p.runs.map((r) => ({ ...r, text: r.text.includes('\t') ? r.text.replace(/\t/g, ' ') : r.text, picture: r.picture && fit(r.picture), baseline: r.vertAlign === 'super' ? 0.3 : r.vertAlign === 'sub' ? -0.25 : undefined })), + align: p.align, + bullet: p.marker, + marginLeft: p.indentLeft, + indent: p.indentFirst, + // ponytail: "at least" spacing is drawn as exactly that height; a line with bigger type than it is not let grow + line: p.line, + lineFactor: p.line ? undefined : (p.lineFactor ?? 1) * 0.97, // Word's single spacing of Calibri-like faces is about 1.17 em + before: p.before ?? 0, + after: p.after ?? 0, + size: p.runs[0]?.size ?? 11, + } +} + +const paragraphsOf = (b: DocBlock): Paragraph[] => (b.kind === 'p' ? [b] : b.kind === 'table' ? b.rows.flat().flatMap((c) => c.paragraphs) : []) + +/** every picture the document draws: in the flow, placed, in a line of text, in a table, header or footer */ +export function picturesOf(doc: DocModel): Picture[] { + const out: Picture[] = [] + for (const s of doc.sections) { + for (const blocks of [s.blocks, s.header, s.footer, s.firstHeader, s.firstFooter]) { + for (const b of blocks ?? []) { + if (b.kind === 'picture') out.push(b) + else for (const p of paragraphsOf(b)) for (const r of p.runs) if (r.picture) out.push(r.picture) + } + } + } + return out +} + +/** every font the document's text asks for, headers and footers included */ +export const fontNeeds = (doc: DocModel): FontNeed[] => { + const out: FontNeed[] = [] + for (const s of doc.sections) { + for (const b of [...s.blocks, ...(s.header ?? []), ...(s.footer ?? []), ...(s.firstHeader ?? []), ...(s.firstFooter ?? [])]) { + for (const p of paragraphsOf(b)) for (const r of p.runs) out.push({ name: r.font, bold: r.bold, italic: r.italic, text: r.text + (r.field ? '0123456789' : '') + (p.marker ?? '') }) + } + } + return out +} + +/** a picture in its box: all of it, or what its crop leaves (the whole picture is drawn larger or smaller and the box clips it) */ +function drawPicture(page: PageDraw, id: number, p: Picture, x: number, y: number, w: number, h: number) { + const c = p.crop + if (!c) return page.image(id, x, y, w, h) + const [full, high] = [w / (1 - c.l - c.r), h / (1 - c.t - c.b)] + page.save() + page.clip(x, y, w, h) + page.image(id, x - c.l * full, y - c.t * high, full, high) + page.restore() +} + +const CELL_PAD = { top: 0, right: 5.4, bottom: 0, left: 5.4 } // Word's default cell margins +/** the least width Word will put text in beside a picture; a narrower gap is left empty and the line goes below */ +const MIN_BESIDE = 36 + +/** the room a placed picture takes on its page, with the clearance the document asks for, and how text goes around it */ +type Float = { x0: number; y0: number; x1: number; y1: number; kind: 'square' | 'topBottom'; side: 'both' | 'left' | 'right' | 'largest' } + +/** a page of the drawing and what decides its header, footer and body */ +type PageInfo = { page: PageDraw; s: DocSection; first: boolean } +/** a heading as laid out: its level, text and where it is */ +type Mark = { level: number; title: string; page: number; y: number } + +/** the blocks with the page number and the number of pages put in where the document has those fields */ +function numbered(blocks: DocBlock[], n: number, total: number): DocBlock[] { + const para = (p: Paragraph): Paragraph => (p.runs.some((r) => r.field) ? { ...p, runs: p.runs.map((r) => (r.field ? { ...r, text: String(r.field === 'page' ? n : total) } : r)) } : p) + return blocks.map((b) => (b.kind === 'p' ? para(b) : b.kind === 'table' ? { ...b, rows: b.rows.map((row) => row.map((c) => ({ ...c, paragraphs: c.paragraphs.map(para) }))) } : b)) +} + +/** the header or footer a page of a section shows */ +const bandOf = (s: DocSection, first: boolean, which: 'header' | 'footer'): DocBlock[] => (s.titlePg && first ? (which === 'header' ? s.firstHeader : s.firstFooter) : which === 'header' ? s.header : s.footer) ?? [] + +class Flow { + d: PdfDrawing + s: DocSection + page!: PageDraw + y = 0 + /** the body's top and bottom on this page (a tall header or footer pushes them in) */ + top: number + bottom: number + book: FontBook + imageFor: ImageFor + /** a header or footer: it never breaks to a new page, and its tabs are placed */ + band: boolean + pages: PageInfo[] + marks: Mark[] + /** pictures on this page that text goes around (a new page has none; a header's only move the header's text) */ + floats: Float[] = [] + private started = false + constructor(d: PdfDrawing, s: DocSection, book: FontBook, imageFor: ImageFor, pages: PageInfo[], marks: Mark[], band = false) { + this.d = d + this.s = s + this.book = book + this.imageFor = imageFor + this.pages = pages + this.marks = marks + this.band = band + this.top = s.margins.top + this.bottom = band ? Infinity : s.height - s.margins.bottom + if (!band) this.newPage() + } + get left() { return this.s.margins.left } + get width() { return this.s.width - this.s.margins.left - this.s.margins.right } + newPage() { + if (this.band) return + const first = !this.started + this.started = true + this.page = this.d.addPage(this.s.width, this.s.height) + this.pages.push({ page: this.page, s: this.s, first }) + const [header, footer] = [bandHeight(this, bandOf(this.s, first, 'header')), bandHeight(this, bandOf(this.s, first, 'footer'))] + this.top = Math.max(this.s.margins.top, header ? (this.s.margins.header ?? 36) + header : 0) + this.bottom = Math.min(this.s.height - this.s.margins.bottom, footer ? this.s.height - (this.s.margins.footer ?? 36) - footer : Infinity) + this.y = this.top + this.floats = [] + } + atTop() { return this.y <= this.top + 0.5 } + + /** + * Where a line `h` tall at `y` may go: the widest piece of the text width the pictures leave (all of the wrapped ones, + * on the side each allows), or, when no piece is wide enough, `skip`: how far down to move it to clear them. + */ + private box(y: number, h: number, indentRight: number): Slot { + let segs: [number, number][] = [[this.left, this.left + this.width - indentRight]] + let skip = 0 + for (const f of this.floats) { + if (f.y1 <= y || f.y0 >= y + h) continue + skip = Math.max(skip, f.y1 - y) + if (f.kind === 'topBottom') { segs = []; continue } + segs = segs.flatMap(([a, b]): [number, number][] => { + const left: [number, number] = [a, Math.min(b, f.x0)] + const right: [number, number] = [Math.max(a, f.x1), b] + if (f.side === 'left') return [left] + if (f.side === 'right') return [right] + if (f.side === 'largest') return left[1] - left[0] >= right[1] - right[0] ? [left] : [right] + return [left, right] + }).filter(([a, b]) => b - a > 0.5) + } + // ponytail: a line takes one piece (Word fills both sides of a picture in the middle of the text; this uses the wider) + const best = segs.reduce<[number, number] | null>((m, g) => (!m || g[1] - g[0] > m[1] - m[0] ? g : m), null) + if (best && (best[1] - best[0] >= MIN_BESIDE || !skip)) return { dx: best[0] - this.left, w: best[1] - best[0] } + return { dx: 0, w: this.width - indentRight, skip: skip || 0.1 } + } + + /** where a placed picture goes, from the offsets or the alignment it was given */ + private spot(p: Picture, anchorY: number): { x: number; y: number } { + const [x0, x1] = p.from?.h === 'page' ? [0, this.s.width] : [this.left, this.left + this.width] + const [y0, y1] = p.from?.v === 'page' ? [0, this.s.height] : p.from?.v === 'margin' ? [this.s.margins.top, this.s.height - this.s.margins.bottom] : [anchorY, anchorY] + const a = p.align + const x = a?.h === 'left' ? x0 : a?.h === 'center' ? (x0 + x1 - p.width) / 2 : a?.h === 'right' ? x1 - p.width : x0 + (p.at?.x ?? 0) + const y = a?.v === 'top' ? y0 : a?.v === 'center' ? (y0 + y1 - p.height) / 2 : a?.v === 'bottom' ? y1 - p.height : y0 + (p.at?.y ?? 0) + return { x, y } + } + + /** a placed picture drawn where it goes */ + private paint(p: Picture, x: number, y: number) { + const id = this.imageFor(p) + if (id !== null) drawPicture(this.page, id, p, x, y, p.width, p.height) + } + + /** when text wraps around a placed picture, the lines that follow keep clear of it */ + private reserve(p: Picture, x: number, y: number) { + if (!p.wrap) return + const d = p.dist ?? { top: 0, bottom: 0, left: 0, right: 0 } + this.floats.push({ x0: x - d.left, y0: y - d.top, x1: x + p.width + d.right, y1: y + p.height + d.bottom, kind: p.wrap, side: p.wrapText ?? 'both' }) + } + + /** a placed picture that isn't anchored in a paragraph being laid out: drawn, and kept clear of */ + place(p: Picture, anchorY: number) { + const { x, y } = this.spot(p, anchorY) + this.paint(p, x, y) + this.reserve(p, x, y) + } + + /** what a run of a line needs drawn besides its text: a picture in the line (standing on the baseline), a link's box */ + onRun = (page: PageDraw) => (b: RunBox) => { + const pic = b.run.picture + if (pic) { + const id = this.imageFor(pic) + const h = (b.width * pic.height) / pic.width + if (id !== null) drawPicture(page, id, pic, b.x, b.baseline - h, b.width, h) + if (b.run.link) page.link(b.x, b.baseline - h, b.width, h, b.run.link) + return + } + if (b.run.link) page.link(b.x, b.baseline - b.size * 0.85, b.width, b.size * 1.05, b.run.link) + } + private draw(laid: Laid[], area: { x: number; y: number; w: number; h: number }) { + drawText(this.page, laid, area, 'top', this.onRun(this.page)) + } + + /** a paragraph's lines, laid out for the text width */ + lines(p: Paragraph): Laid { + const right = p.indentRight ?? 0 + // the lines are shaped for where the paragraph starts now (ponytail: one that widow control moves to the next page keeps its shape) + const full = { dx: 0, w: this.width - right } + // a line that would not fit starts the next page, where this page's pictures are not + const slot = this.floats.length ? (offset: number, h: number) => (this.y + offset + h > this.bottom + 0.01 ? full : this.box(this.y + offset, h, right)) : undefined + return layoutText([{ ...slidePara(p, this.bottom - this.top), before: 0, after: 0 }], Math.max(10, this.width - right), true, 0, this.book, slot)[0] + } + + /** + * a paragraph in the flow, split between lines where the page ends. `placed` are the pictures anchored in it: the ones + * behind the text or with text around them are drawn first (they are where they are on the page the paragraph starts on), + * the ones in front after it. + */ + paragraph(p: Paragraph, placed: Picture[] = []) { + if (this.band && p.tabs?.length && this.tabbed(p)) return + if (p.pageBreakBefore && !this.atTop()) this.newPage() + if (this.band || !this.atTop()) this.y += p.before ?? 0 + // a picture placed "from the paragraph" counts from the top of its text, below the space above it (the template this was + // checked on puts a picture 13.5 pt under a 12.9 pt line that has 10 pt above it) + let anchor = this.y + const under = placed.filter((q) => q.wrap || !q.front) + const over = placed.filter((q) => !q.wrap && q.front) + // the room they take is kept before the lines are made; they are drawn (under the text) once the page the paragraph is on is settled + let spots: { x: number; y: number }[] = [] + let painted = false + const keep = () => { + spots = under.map((q) => this.spot(q, anchor)) + under.forEach((q, j) => this.reserve(q, spots[j].x, spots[j].y)) + } + keep() + let laid = this.lines(p) + const w = Math.max(10, this.width - (p.indentRight ?? 0)) + let all = laid.lines + const heightOf = (l: Laid['lines'][number]) => l.height + (l.gap ?? 0) + const total = all.reduce((h, l) => h + heightOf(l), 0) + let i = 0 + // set when a line was moved to a new page and nothing was drawn since: the next time, the line is drawn where it is, so + // the loop always moves on (a header can't start a new page, and a nonsense length in a file must not stop the layout) + let moved = false + while (i < all.length) { + const room = this.bottom - this.y + let n = 0 + let h = 0 + while (i + n < all.length && h + heightOf(all[i + n]) <= room + 0.01) h += heightOf(all[i + n++]) + const left = all.length - i + // starting on a new page: the paragraph is laid out again there, where the pictures of the old page are not + const restart = () => { + this.newPage() + anchor = this.y + keep() + laid = this.lines(p) + all = laid.lines + } + if (n < left) { + // the paragraph doesn't fit: keep it whole (lines together), else split it so neither side is a single line (widows and orphans) + if (p.keepLines && i === 0 && !moved && !this.atTop() && total <= this.bottom - this.top) { moved = true; restart(); continue } + if (p.widowControl !== false) { + if (left - n === 1 && n > 1) n-- + if (i === 0 && n === 1) n = 0 + } + if (n === 0) { + if (!this.atTop() && !moved) { moved = true; if (i === 0) restart(); else this.newPage(); continue } + n = 1 // a line taller than the page still has to go somewhere + } + } + if (!painted) { + under.forEach((q, j) => this.paint(q, spots[j].x, spots[j].y)) + painted = true + } + for (let k = i; k < i + n; k++) { + // the bookmark goes where the heading's first line is drawn, which is not always the page it was reached on + if (k === 0 && p.heading && !this.band) this.marks.push({ level: p.heading, title: p.runs.map((r) => r.text).join('').replace(/\s+/g, ' ').trim(), page: this.d.pages.length - 1, y: this.y }) + // a gap above a line is there for a picture; at the top of a page it has nothing to clear + const ln = this.atTop() && all[k].gap ? { ...all[k], gap: 0 } : all[k] + const one: Laid = { ...laid, lines: [ln], bullet: k === 0 ? laid.bullet : undefined, before: 0, after: 0 } + this.draw([one], { x: this.left, y: this.y, w, h: ln.height }) + this.y += heightOf(ln) + } + moved = false + i += n + if (i < all.length) this.newPage() + } + this.y += p.after ?? 0 + for (const q of over) this.place(q, anchor) + } + + /** + * A header or footer line with tabs: the text between tabs is placed at the tab stops (left, centred or right), the way + * Word's built-in headers put a title on the left and a page number on the right. False when a stop isn't known. + * ponytail: one line; the stop for each tab is the next one in order, not "the next one to the right". + */ + tabbed(p: Paragraph): boolean { + const stops = p.tabs! + if (stops.includes(null)) return false + const parts: Paragraph['runs'][] = [[]] + for (const r of p.runs) { + if (r.text === '\t') parts.push([]) + else parts[parts.length - 1].push(r) + } + if (parts.length !== stops.length + 1) return false + const laid = parts.map((runs) => layoutText([{ ...slidePara({ ...p, runs, align: 'left', indentLeft: 0, indentFirst: 0, marker: undefined }), before: 0, after: 0 }], 1e6, false, 0, this.book)[0]) + const height = Math.max(...laid.map((l) => l.lines.reduce((a, ln) => a + ln.height, 0))) + if (!this.atTop()) this.y += p.before ?? 0 + laid.forEach((l, i) => { + if (!parts[i].length) return + const w = l.lines[0].width + let x = this.left + (p.indentLeft ?? 0) + if (i) { + const stop = stops[i - 1]! + const at = Math.min(this.width, Math.max(0, stop.pos ?? (stop.align === 'left' ? 0 : stop.align === 'center' ? this.width / 2 : this.width))) + x = this.left + (stop.align === 'left' ? at : stop.align === 'center' ? at - w / 2 : at - w) + } + this.draw([l], { x, y: this.y, w: w + 1, h: height }) + }) + this.y += height + (p.after ?? 0) + return true + } + + /** paragraphs in a text box at a place on the page (or below the paragraph they hang from) */ + framed(group: Paragraph[], anchorY: number) { + const f = group[0].frame! + const x = f.from?.h === 'margin' ? this.left + f.x : f.x + const y = f.from?.v === 'margin' ? this.s.margins.top + f.y : f.from?.v === 'paragraph' ? anchorY + f.y : f.y + const laid = layoutText(group.map(slidePara), Math.max(10, f.w), true, 0, this.book) + this.draw(laid, { x, y, w: f.w, h: f.h ?? 0 }) + } + + private pictureSize(p: Picture): [number, number] { + const k = Math.min(1, this.width / p.width) + return [p.width * k, p.height * k] + } + + picture(p: Picture, anchorY: number) { + if (p.at || p.align) return this.place(p, anchorY) + const id = this.imageFor(p) + const [w, h] = this.pictureSize(p) + if (this.y + h > this.bottom && !this.atTop()) this.newPage() + if (id !== null) drawPicture(this.page, id, p, this.left, this.y, w, h) + else this.page.fillRect(this.left, this.y, w, h, [235, 235, 235]) + this.y += h + } + + private columns(t: Table): number[] { + const total = t.colWidths.reduce((a, b) => a + b, 0) + const k = total > this.width ? this.width / total : 1 + const xs = [this.left] + for (const w of t.colWidths) xs.push(xs[xs.length - 1] + w * k) + return xs + } + + /** one row: its cells' boxes and text, and how tall it is */ + private row(t: Table, xs: number[], ri: number) { + let col = 0 + const cells = t.rows[ri].map((c) => { + const span = c.cols ?? 1 + const box = { x0: xs[col], x1: xs[Math.min(xs.length - 1, col + span)], c } + col += span + return box + }) + const pad = cells.map((b) => b.c.pad ?? CELL_PAD) + const laid = cells.map((b, i) => (b.c.vMerge === 'continue' ? [] : layoutText(b.c.paragraphs.map((p) => slidePara(p)), Math.max(4, b.x1 - b.x0 - pad[i].left - pad[i].right), true, 0, this.book))) + const textH = laid.map((l) => l.reduce((h, p, i) => h + (i ? p.before : 0) + p.lines.reduce((a, x) => a + x.height, 0) + p.after, 0)) + const needed = textH.map((h, i) => h + pad[i].top + pad[i].bottom) + return { cells, laid, pad, textH, h: Math.max(t.rowHeights?.[ri] ?? 0, ...needed, 12) } + } + + table(t: Table) { + const xs = this.columns(t) + const line = (x0: number, y0: number, x1: number, y1: number) => this.page.line(x0, y0, x1, y1, 0.5, [0, 0, 0]) + // a cell continued from the row above is drawn as part of it: rows are drawn one at a time, so its text stays in its first row + t.rows.forEach((_, ri) => { + const { cells, laid, pad, textH, h } = this.row(t, xs, ri) + if (this.y + h > this.bottom && !this.atTop()) this.newPage() + cells.forEach((b, i) => { + if (b.c.fill) this.page.fillRect(b.x0, this.y, b.x1 - b.x0, h, hexRgb(b.c.fill)) + if (laid[i].length) { + // text in a cell taller than it goes at the top, middle or bottom of the room inside the cell's margins + const free = Math.max(0, h - pad[i].top - pad[i].bottom - textH[i]) + const shift = b.c.vAlign === 'center' ? free / 2 : b.c.vAlign === 'bottom' ? free : 0 + this.draw(laid[i], { x: b.x0 + pad[i].left, y: this.y + pad[i].top + shift, w: b.x1 - b.x0 - pad[i].left - pad[i].right, h: textH[i] }) + } + if (t.borders) { + line(b.x0, this.y, b.x0, this.y + h) + line(b.x1, this.y, b.x1, this.y + h) + if (b.c.vMerge !== 'continue') line(b.x0, this.y, b.x1, this.y) + line(b.x0, this.y + h, b.x1, this.y + h) + } + }) + this.y += h + }) + this.y += t.after ?? 0 + } + + /** how much room a paragraph needs (all of it, with the space around it) */ + private paragraphHeight(p: Paragraph): number { + return (p.before ?? 0) + this.lines(p).lines.reduce((h, l) => h + l.height, 0) + (p.after ?? 0) + } + + /** the room the start of a block needs to be worth putting at the bottom of a page: a few lines, a row, the picture */ + private headHeight(b: DocBlock): number { + if (b.kind === 'picture') return b.at ? 0 : this.pictureSize(b)[1] + if (b.kind === 'table') return b.rows.length ? this.row(b, this.columns(b), 0).h : 0 + if (b.frame) return 0 + const lines = this.lines(b).lines + const k = b.keepLines ? lines.length : b.widowControl === false ? 1 : Math.min(2, lines.length) + return (b.before ?? 0) + lines.slice(0, k).reduce((h, l) => h + l.height, 0) + } + + /** a paragraph that keeps with the next one, with what comes after it that must stay on its page (a heading never ends a page) */ + chainHeight(blocks: DocBlock[], i: number, depth = 0): number { + const b = blocks[i] + if (!b) return 0 + if (b.kind === 'p' && b.keepNext && !b.frame && depth < 20) return this.paragraphHeight(b) + this.chainHeight(blocks, i + 1, depth + 1) + return this.headHeight(b) + } + + /** draw a header or footer's blocks from `y`, onto `page` */ + fill(page: PageDraw, y: number, blocks: DocBlock[]) { + this.page = page + this.y = y + this.top = y + this.floats = [] + let anchorY = y + for (let i = 0; i < blocks.length; i++) { + const b = blocks[i] + if (b.kind === 'p' && b.frame) this.framed([b], anchorY) + else if (b.kind === 'p') { + anchorY = this.y + const [placed, next] = placedAfter(blocks, i) + this.paragraph(b, placed) + i = next + } else if (b.kind === 'picture') this.picture(b, anchorY) + else this.table(b) + } + } +} + +/** the placed pictures that follow block `i` (the reader puts the ones anchored in a paragraph right after it), and the index of the last one */ +function placedAfter(blocks: DocBlock[], i: number): [Picture[], number] { + const out: Picture[] = [] + for (let b = blocks[i + 1]; b?.kind === 'picture' && (b.at || b.align); b = blocks[i + 1]) { + out.push(b) + i++ + } + return [out, i] +} + +/** how tall a header or footer is, drawn onto a page that is thrown away */ +function bandHeight(f: Flow, blocks: DocBlock[]): number { + if (!blocks.length) return 0 + const scratch = new Flow(f.d, f.s, f.book, f.imageFor, [], [], true) + scratch.fill(new PageDraw(f.s.width, f.s.height), 0, numbered(blocks, 1, 1)) + return scratch.y +} + +/** bookmarks for the headings: a heading goes under the nearest one above it of a lower level */ +function outlineOf(marks: Mark[], heightOf: (page: number) => number): OutlineItem[] { + const root: OutlineItem[] = [] + const stack: { level: number; item: OutlineItem }[] = [] + for (const m of marks) { + if (!m.title) continue + const item: OutlineItem = { title: m.title.slice(0, 250), page: m.page, fit: ['XYZ', null, Math.round(heightOf(m.page) - m.y + 2), null], open: true, children: [] } + while (stack.length && stack[stack.length - 1].level >= m.level) stack.pop() + ;(stack.length ? stack[stack.length - 1].item.children : root).push(item) + stack.push({ level: m.level, item }) + } + return root +} + +/** a Word document as PDF pages */ +export function layoutDocx(d: PdfDrawing, doc: DocModel, book: FontBook, imageFor: ImageFor): void { + const pages: PageInfo[] = [] + const marks: Mark[] = [] + for (const s of doc.sections) { + const flow = new Flow(d, s, book, imageFor, pages, marks) + let anchorY = flow.y + for (let i = 0; i < s.blocks.length; i++) { + const b = s.blocks[i] + if (b.kind === 'p' && b.frame) { + const group = [b] + while (i + 1 < s.blocks.length && s.blocks[i + 1].kind === 'p' && (s.blocks[i + 1] as Paragraph).frame === b.frame) group.push(s.blocks[++i] as Paragraph) + flow.framed(group, anchorY) + } else if (b.kind === 'p') { + anchorY = flow.y + // keep with the next: start a new page when this and the first lines after it don't fit together + if (b.keepNext && !b.pageBreakBefore && !flow.atTop()) { + const need = flow.chainHeight(s.blocks, i) + if (flow.y + need > flow.bottom && need <= flow.bottom - flow.top) flow.newPage() + } + const [placed, last] = placedAfter(s.blocks, i) + flow.paragraph(b, placed) + i = last + } else if (b.kind === 'picture') flow.picture(b, anchorY) + else flow.table(b) + } + } + // headers and footers last, when the number of pages is known + const total = pages.length + pages.forEach((info, k) => { + const band = new Flow(d, info.s, book, imageFor, pages, marks, true) + const header = bandOf(info.s, info.first, 'header') + const footer = bandOf(info.s, info.first, 'footer') + if (header.length) band.fill(info.page, info.s.margins.header ?? 36, numbered(header, k + 1, total)) + if (footer.length) { + const h = bandHeight(band, footer) + band.fill(info.page, info.s.height - (info.s.margins.footer ?? 36) - h, numbered(footer, k + 1, total)) + } + }) + d.outline = outlineOf(marks, (page) => d.pages[page].height) +} diff --git a/packages/tools/src/word-to-pdf/manifest.ts b/packages/tools/src/word-to-pdf/manifest.ts new file mode 100644 index 0000000..fbdf1af --- /dev/null +++ b/packages/tools/src/word-to-pdf/manifest.ts @@ -0,0 +1,15 @@ +import type { Tool } from '@ream/core' + +export const wordToPdf: Tool = { + id: 'word-to-pdf', + title: 'Word to PDF', + description: 'Save a Word document (.docx) as PDF', + category: 'pdf', + icon: 'file', + accept: ['.docx'], + action: 'Save as PDF', + multiple: false, + minFiles: 1, + preview: 'live', + status: 'ready', +} diff --git a/packages/tools/src/word-to-pdf/run.ts b/packages/tools/src/word-to-pdf/run.ts new file mode 100644 index 0000000..9b3a6fb --- /dev/null +++ b/packages/tools/src/word-to-pdf/run.ts @@ -0,0 +1,55 @@ +import { UserError, type FileData, type RunContext } from '@ream/core' +import { readDocx } from '@ream/office-core' +import { PdfDrawing, buildPdf, type ImageInput } from '@ream/pdf-core' +import { FontBook, type FontSource, type InstalledFont } from '../fonts/book.ts' +import { browserFonts } from '../fonts/source.ts' +import { baseName } from '../names.ts' +import { imageInput } from '../office-images.ts' +import { pdfFile } from '../pdf-common.ts' +import { fontNeeds, layoutDocx, picturesOf } from './layout.ts' + +export type WordToPdfOptions = { + /** fonts the panel found for the document: installed, added from files or downloaded (empty: bundled look-alikes) */ + localFonts: InstalledFont[] +} + +// what an old Word file (.doc) and a password protected .docx both start with: an OLE container, not a zip +const isOle = (b: Uint8Array) => b.length > 8 && [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1].every((v, i) => b[i] === v) + +/** the PDF of a .docx. `fonts` loads the bundled look-alike fonts (the app's bundle by default; tests read them from disk) */ +export async function docxToPdf(bytes: Uint8Array, localFonts: InstalledFont[] = [], fonts: FontSource = browserFonts, title = ''): Promise { + if (isOle(bytes)) throw new UserError('This is an older Word file (.doc) or one with a password. Ream reads .docx: in Word, remove the password or choose Save As, Word Document (.docx), then try again.') + const doc = await readDocx(bytes) + // the fonts the file carries come after the ones the user chose, which win a tie (a font that can't be embedded is skipped by the book) + const carried: InstalledFont[] = (doc.fonts ?? []).map((f) => ({ family: f.name, style: f.bold && f.italic ? 'Bold Italic' : f.bold ? 'Bold' : f.italic ? 'Italic' : 'Regular', postscript: '', bytes: f.bytes, source: 'file' })) + const book = new FontBook(fonts, [...localFonts, ...carried]) + await book.load(fontNeeds(doc)) + // pictures decoded once each, before the layout (which is synchronous) + const decoded = new Map() + for (const b of picturesOf(doc)) if (!decoded.has(b.bytes)) decoded.set(b.bytes, await imageInput(b.bytes, b.type)) + const d = new PdfDrawing() + d.title = title + const ids = new Map() + layoutDocx(d, doc, book, (p) => { + if (!ids.has(p.bytes)) { + const input = decoded.get(p.bytes) + ids.set(p.bytes, input ? d.addImage(input) : null) + } + return ids.get(p.bytes)! + }) + if (!d.pages.length) d.addPage(doc.sections[0]?.width ?? 595, doc.sections[0]?.height ?? 842) + return (await buildPdf(d)).bytes +} + +export async function run(files: FileData[], o: WordToPdfOptions, { progress }: RunContext, fonts: FontSource = browserFonts): Promise { + const f = files[0] + progress(0.05) + let bytes: Uint8Array + try { + bytes = await docxToPdf(f.bytes, o.localFonts ?? [], fonts, baseName(f.name)) + } catch (e) { + throw e instanceof UserError ? new UserError(`${f.name}: ${e.message}`) : e + } + progress(1) + return [pdfFile(`${baseName(f.name)}.pdf`, bytes)] +} From 91ed291ea2bb3ef6412ade1fce3078633b80fe95 Mon Sep 17 00:00:00 2001 From: Chizoba Jude Ojini Date: Sat, 7 Nov 2026 18:10:47 +0000 Subject: [PATCH 2/2] Word to PDF: footnotes, columns, table styles, tabs, links to bookmarks Footnotes and endnotes, columns with balancing and continuous sections, table styles with borders, header rows and split rows, tab stops with leaders, bookmarks and a table of contents with real page numbers, odd and even headers, page number restarts, inline and cropped pictures, embedded fonts, and safer reading of damaged files. --- AGENTS.md | 4 +- packages/office-core/src/docx-model.ts | 47 +- packages/office-core/src/docx-read.ts | 287 +++++++-- packages/office-core/src/index.ts | 4 +- packages/office-core/src/pptx-model.ts | 4 + packages/office-core/test/docx-helper.ts | 10 + packages/office-core/test/docx-read.test.ts | 67 ++- packages/pdf-core/src/build.ts | 32 +- packages/pdf-core/test/build.test.ts | 16 + packages/tools/src/m7b-tools.test.ts | 170 +++++- packages/tools/src/powerpoint-to-pdf/text.ts | 52 +- packages/tools/src/seo/convert.ts | 6 +- packages/tools/src/word-to-pdf/Panel.tsx | 4 +- packages/tools/src/word-to-pdf/layout.ts | 588 ++++++++++++++++--- 14 files changed, 1131 insertions(+), 160 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 4978486..44de923 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,7 +11,7 @@ Guidance for AI coding agents (and humans) working in this repository. Read it b 1. **Own engine first.** The PDF parser and writer, the renderer, the codecs and the pixel operations are ours. Phase 1 has **zero runtime dependencies** in the engine packages. Do not suggest or add pdf-lib, pdf.js, sharp, Ghostscript, jsPDF and the like, and avoid licensed or AGPL code. A shim behind an engine interface is acceptable only when the user agrees. 2. **Hard systems are deferred** and decided one by one: OCR, HTML to PDF, PDF/A, ML image tools. Do not start them unprompted. (Excel and PowerPoint to PDF and the PDF to Word, Excel and PowerPoint exports were decided and built in M7b; Word to PDF, for .docx only, in M8.) 3. **Keep logic out of the UI** so a React Native port stays possible. Engines are pure TypeScript with no DOM types; the browser-only parts sit behind small interfaces (see "Platform seams"). -4. **Scope per milestone.** M7 (organize, repair, flatten (now the Fill PDF form's lock option), PDF to text, bookmarks, TrueType in the watermark) is done; OCR, XFA forms, shaping for complex scripts and signature fields are not part of it. M6 (watermark, blur or hide, filters, meme) is done. M7b (PDF to image, Word, Excel and PowerPoint; Excel and PowerPoint to PDF, with charts; previews of Office files) is done; OCR and animations are not part of it. M8 (Word to PDF for .docx: headers, footers and page numbers, clickable links, heading bookmarks, widow and keep-with-next control) is done; footnotes, columns, table styles, different even-page headers and older .doc files are not part of it. M7.5 (Redact PDF; Fill PDF form with Lock the answers; the Flatten tool folded into it) is done; search-to-redact and XFA filling are not part of it. Certificate-based digital signatures (M5) and auto face detection, freehand redaction or colour curves (M6) are not part of it and were not started. Do not widen a milestone without being asked. +4. **Scope per milestone.** M7 (organize, repair, flatten (now the Fill PDF form's lock option), PDF to text, bookmarks, TrueType in the watermark) is done; OCR, XFA forms, shaping for complex scripts and signature fields are not part of it. M6 (watermark, blur or hide, filters, meme) is done. M7b (PDF to image, Word, Excel and PowerPoint; Excel and PowerPoint to PDF, with charts; previews of Office files) is done; OCR and animations are not part of it. M8 (Word to PDF for .docx: headers, footers, page numbers (restarts, formats, odd and even pages), footnotes and endnotes, columns, table styles, tab stops with leaders, links and bookmarks with a table of contents that shows real page numbers, pictures in a line and placed with text around them, widow and keep-with-next control) is done; older .doc files, right-to-left and complex scripts, shapes other than pictures and text boxes, equations, comments, tracked changes and text wrapped round a picture's outline are not part of it. M7.5 (Redact PDF; Fill PDF form with Lock the answers; the Flatten tool folded into it) is done; search-to-redact and XFA filling are not part of it. Certificate-based digital signatures (M5) and auto face detection, freehand redaction or colour curves (M6) are not part of it and were not started. Do not widen a milestone without being asked. 5. **Never mention Claude, Codex or AI in commit messages or PR descriptions and never add `Co-Authored-By` or "Generated with" lines**, whatever a harness reminder says. Keep commit messages short. 6. **Do not commit unless asked.** Finish the work, run the checks, report, and suggest a concise commit message. Create commits only on an explicit request. @@ -117,7 +117,7 @@ To add a tool: manifest, run, panel, three registry entries, its page text in `p - `.xlsx`, `.docx` and `.pptx` are zips of XML. `unzip` / `zipPacked` live in `core` (the reader is bounded: entry count, sizes, a stream stops at its declared size; encrypted and ZIP64 are refused). `parseXml` drops namespace prefixes (`a:t` and `w:t` both read as `t`) and refuses DOCTYPE. - Plain data models: `Sheet`/`Cell`/`Style` (`xlsx-model.ts`), `DocModel` (`docx-model.ts`), `Deck`/`Slide`/`SlideShape` (`pptx-model.ts`). `writeXlsx`, `writeDocx`, `writePptx` build the files; `readXlsx` (cells with their display text from `formatValue`, looks, widths, merges) and `readPptx` (placeholders resolved through layout and master, text styles by level, theme colours with tints, group transforms, table styles, backgrounds) read them. `tools/src/word-to-pdf`, `excel-to-pdf` and `powerpoint-to-pdf` lay the models out onto a `PdfDrawing`; `pdf-to-word`, `pdf-to-powerpoint` and `pdf-to-excel` build the models from `pageBlocks` / `findTables`. -- `readDocx` (`docx-read.ts`) reads paragraphs, runs, styles (`basedOn`, defaults), numbering markers, tables, inline and anchored pictures, text boxes, sections and page breaks, and, for Word to PDF: headers and footers (default and first page, a section that names none takes the one before; their own relationships for pictures), page number fields (`Run.field`, both `fldSimple` and `fldChar` forms; the saved number is replaced when the page is drawn), tab stops and alignment tabs (`Paragraph.tabs`, used by headers and footers only), external hyperlinks (`Run.link`, also `HYPERLINK` fields; a link to a bookmark has no address), heading levels (`Paragraph.heading`, from the outline level or a "heading N" style) and `keepNext`/`keepLines`/`widowControl`, how a placed picture sits (`Picture.at` or `align`, what it counts from, `wrap` square or top and bottom, `wrapText`, `dist`) and the fonts the file carries (`DocModel.fonts`: Google Docs stores the ones it uses; Word scrambles the first 32 bytes with the GUID, a zero key means plain). It feeds Word to PDF and the Word preview (`tools/src/word-to-pdf`, the same code: `docxToPdf` in its `run.ts`; `layout.ts` holds the layout). Layout: every paragraph is measured before it is placed, so a page break never leaves one line alone at either side (widow and orphan control, on unless the paragraph turns it off), `keepLines` moves a paragraph whole, `keepNext` keeps a chain of paragraphs with the first lines of what follows, and a tall header or footer pushes the body in. Inline pictures are runs of their paragraph (`Run.picture`, text empty): a piece of the line as wide as the picture (shrunk to the column, and to a page's height), standing on the baseline with the text's descent below it (`Line.base`), so the paragraph's alignment, spacing and neighbours apply; `picturesOf` lists every picture for decoding. A picture's crop (`Picture.crop`, shares cut from each side, negative = room added: Google Docs' thin divider lines are a 4 px picture in a 15 pt box) is drawn by `drawPicture`: the whole picture larger or smaller, clipped to the box. Table cells read `tcMar`/`tblCellMar` (`pad`) and `vAlign`. A file may hold any number: `tw`/`emu` in `docx-read.ts` turn a non-number into 0 and cap lengths at 100 000 pt, and the paragraph loop in `layout.ts` always moves on (`moved`: a line that was sent to a new page is drawn where it is the second time), because a header can't start a new page and a `NaN` once made it loop for ever. Check changes to the reader or layout by converting a few hundred damaged copies of real documents (random attribute values, deleted elements, truncated XML, repeated paragraphs): each must give a PDF or a `UserError` within seconds. Placed pictures: one anchored in a paragraph counts "from the paragraph" from the top of its text (below the space above it), is kept clear of by the lines that follow (`Flow.box`: the widest piece of the line the pictures leave, else the line goes below them; `layoutText`'s optional `slot` and `Line.gap` do it), and is drawn under the text once the paragraph's page is settled (a paragraph that moves to a new page is laid out again there). Fonts the file carries come after the ones the user chose (`InstalledFont`), and `usedFonts` leaves their families out of the panel. Headers and footers are drawn last, when the page count is known; their tabs put text at the stops (`Flow.tabbed`: one line, the stop for each tab is the next in order). Run positions come back from `drawText`'s optional `onRun` and become `PageDraw.link` boxes; headings become `PdfDrawing.outline`. Not read: footnotes, columns, different even-page headers, table styles, restarted page numbers, internal links. Tests build small documents from XML strings (`office-core/test/docx-helper.ts`). `readPptx` returns charts as `SlideChart` and `readXlsx` puts `pictures`, `charts` and `page` (the page setup, print area, title rows; `saved` is false when the sheet only has the margins every file has) on each sheet. `readChart` reads the cached values, or with a `ChartLookup` the cells a reference names (openpyxl saves no cache). +- `readDocx` (`docx-read.ts`) reads paragraphs, runs, styles (`basedOn`, defaults), numbering markers, tables, inline and anchored pictures, text boxes, sections and page breaks, and, for Word to PDF: headers and footers (default and first page, a section that names none takes the one before; their own relationships for pictures), page number fields (`Run.field`, both `fldSimple` and `fldChar` forms; the saved number is replaced when the page is drawn), tab stops and alignment tabs (`Paragraph.tabs`, used by headers and footers only), external hyperlinks (`Run.link`, also `HYPERLINK` fields; a link to a bookmark has no address), heading levels (`Paragraph.heading`, from the outline level or a "heading N" style) and `keepNext`/`keepLines`/`widowControl`, how a placed picture sits (`Picture.at` or `align`, what it counts from, `wrap` square or top and bottom, `wrapText`, `dist`) and the fonts the file carries (`DocModel.fonts`: Google Docs stores the ones it uses; Word scrambles the first 32 bytes with the GUID, a zero key means plain). It feeds Word to PDF and the Word preview (`tools/src/word-to-pdf`, the same code: `docxToPdf` in its `run.ts`; `layout.ts` holds the layout). Layout: every paragraph is measured before it is placed, so a page break never leaves one line alone at either side (widow and orphan control, on unless the paragraph turns it off), `keepLines` moves a paragraph whole, `keepNext` keeps a chain of paragraphs with the first lines of what follows, and a tall header or footer pushes the body in. Inline pictures are runs of their paragraph (`Run.picture`, text empty): a piece of the line as wide as the picture (shrunk to the column, and to a page's height), standing on the baseline with the text's descent below it (`Line.base`), so the paragraph's alignment, spacing and neighbours apply; `picturesOf` lists every picture for decoding. A picture's crop (`Picture.crop`, shares cut from each side, negative = room added: Google Docs' thin divider lines are a 4 px picture in a 15 pt box) is drawn by `drawPicture`: the whole picture larger or smaller, clipped to the box. Table cells read `tcMar`/`tblCellMar` (`pad`) and `vAlign`. A file may hold any number: `tw`/`emu` in `docx-read.ts` turn a non-number into 0 and cap lengths at 100 000 pt, and the paragraph loop in `layout.ts` always moves on (`moved`: a line that was sent to a new page is drawn where it is the second time), because a header can't start a new page and a `NaN` once made it loop for ever. Check changes to the reader or layout by converting a few hundred damaged copies of real documents (random attribute values, deleted elements, truncated XML, repeated paragraphs): each must give a PDF or a `UserError` within seconds. Placed pictures: one anchored in a paragraph counts "from the paragraph" from the top of its text (below the space above it), is kept clear of by the lines that follow (`Flow.box`: the widest piece of the line the pictures leave, else the line goes below them; `layoutText`'s optional `slot` and `Line.gap` do it), and is drawn under the text once the paragraph's page is settled (a paragraph that moves to a new page is laid out again there). Fonts the file carries come after the ones the user chose (`InstalledFont`), and `usedFonts` leaves their families out of the panel. Headers and footers are drawn last, when the page count is known; their tabs put text at the stops (`Flow.tabbed`: one line, the stop for each tab is the next in order). Run positions come back from `drawText`'s optional `onRun` and become `PageDraw.link` boxes; headings become `PdfDrawing.outline`. Footnotes and endnotes: marks are numbered in order by the reader (`Run.note`, `DocModel.notes` by `foot:id`/`end:id`); a page reserves room at its bottom for the notes of the marks on it (`Flow.noteCost`/`takeNotes`, so a line whose note doesn't fit goes to the next page) and `flushNotes` draws them under a short rule when the page is left; endnotes follow the last section. Columns (`Section.columns`): `Flow.cols`, `advance()` goes to the next column before the next page; a continuous section carries on at the current height with its own columns, and a multi-column section before a continuous break is balanced (`balancedHeight`: a probe layout in one column for the height, then probes with a growing cap until it fits on one page). Table styles are resolved by the reader per cell (`stylePartsFor`: whole table, bands, first and last row and column, corners; `TableCell.fill`, `border` per side, paragraph and run formatting through `env.cell`); a table can be aligned or indented (`Table.align`/`indent`, with the outdent Word applies below compatibility mode 15), repeats its heading rows (`headerRows`) and splits rows between lines (`splitRow`; `cantSplit` rows move whole). Tab stops (`Paragraph.tabStops`, merged from the style chain; the engine's `SlidePara.tabStops`/`defaultTab`) place a tab at the next stop, with right, centre and decimal stops aligning the text after them and dot, dash and underscore leaders. Bookmarks (`Paragraph.bookmarks`) are recorded where their paragraph's first line is drawn; links to them (`Run.anchor`) become `PageDraw.goto` once the places are known, and `PAGEREF` fields (`field: 'pageref'`) show the page of their bookmark from a second layout pass (`layoutDocx` lays the document out twice when it has any). Page numbers restart and change look per section (`pageStart`, `pageFormat`), and `evenAndOdd` documents use the even header and footer on even pages. Not read: right-to-left and complex scripts, equations, comments, shapes other than pictures and text boxes, text wrapped round a picture's outline (tight and through wrapping use its box), text on both sides of a picture within one line (the wider side is used). Tests build small documents from XML strings (`office-core/test/docx-helper.ts`). `readPptx` returns charts as `SlideChart` and `readXlsx` puts `pictures`, `charts` and `page` (the page setup, print area, title rows; `saved` is false when the sheet only has the margins every file has) on each sheet. `readChart` reads the cached values, or with a `ChartLookup` the cells a reference names (openpyxl saves no cache). - Fonts: `tools/src/fonts/` is a seam like `PageRaster`. `FontBook(source, installed, uploaded)` loads faces for the text a file needs (`load(needs)`, then `spec(name, bold, italic)` for the builder): an installed font the panel found (`localFonts` option, from `window.queryLocalFonts()` in `InstalledFonts.tsx`), else a bundled look-alike with the same widths (`@fontsource` Carlito, Caladea, Arimo, Tinos, Cousine; WOFF read by `woffToSfnt`), else a standard font with `widthScale` from `office-fonts.ts`. `browserFonts` imports the WOFF files lazily through Vite; tests pass `nodeFonts`. Office runs take the source as their last parameter. - Fonts panel (`fonts/FontsPanel.tsx`): lists `usedFonts`, uses installed fonts automatically when `local-fonts` is granted (close names through `closestFamily` in `fonts/match.ts`, embeddability checked in the worker with `readFontFile` -> `fontFaces`), adds font files (several, matched by name) and downloads free ones. Everything goes into the `localFonts` option as `InstalledFont` with `for` (the family it stands for) and `source`. `FontBook` picks by `for`, the slant and the nearest weight (no more than 150 away), several files of one style become one face. The builder writes `/ReamFamily` (the family the document asked for) on each embedded font, one resource per look-alike part and asked family; the renderer's `faceOf` reads it back, else the embedded font's family name, and maps Ream's look-alikes to their Office originals. - PDF to PowerPoint uses `placedBlocks` (`pdf-render/src/placed.ts`), not `pageBlocks`: pieces cut at 1.5 em gaps, stacked by spacing, size and a shared edge, ruled lines between rows keep cells apart, lines kept with `breaks` (paragraph runs joined by "\n", box wrap off). `toLines` follows drawing order into runs, then joins runs on one baseline (within 0.15 em); never link characters by height across the page again (a big title chained the lines beside it and mixed their letters). diff --git a/packages/office-core/src/docx-model.ts b/packages/office-core/src/docx-model.ts index 66a3b0a..b28b730 100644 --- a/packages/office-core/src/docx-model.ts +++ b/packages/office-core/src/docx-model.ts @@ -15,8 +15,14 @@ export type Run = { vertAlign?: 'super' | 'sub' /** reading: a web or mail address the text links to */ link?: string - /** reading: the text is the page number ('page') or the page count ('pages'); `text` is what the file saved, to be replaced */ - field?: 'page' | 'pages' + /** reading: the text is the page number ('page'), the page count ('pages') or the page a bookmark is on ('pageref', with `ref`); `text` is what the file saved, to be replaced */ + field?: 'page' | 'pages' | 'pageref' + /** reading: the bookmark a 'pageref' field is about */ + ref?: string + /** reading: a link to a bookmark in this document */ + anchor?: string + /** reading: the mark of a footnote or endnote (`text` is its number, already worked out) */ + note?: { id: string; kind: 'foot' | 'end' } /** reading: a picture in the line, like a character (`text` is empty): it sits on the baseline, grows the line, and goes where the paragraph's alignment puts it */ picture?: Picture } @@ -45,6 +51,10 @@ export type Paragraph = { keepNext?: boolean keepLines?: boolean widowControl?: boolean + /** reading: names of the bookmarks that start in this paragraph */ + bookmarks?: string[] + /** reading: every tab stop of the paragraph (its style's included), by position from the left margin; a tab goes to the next one after where the text is */ + tabStops?: { pos: number; align: 'left' | 'center' | 'right' | 'decimal'; leader?: 'dot' | 'hyphen' | 'underscore' }[] /** reading: one entry for each tab character in the runs, in order; null when no stop is known there. `pos` counts from the left margin (missing: the margins themselves) */ tabs?: ({ pos?: number; align: 'left' | 'center' | 'right' } | null)[] /** @@ -54,6 +64,9 @@ export type Paragraph = { frame?: { x: number; y: number; w: number; h?: number; /** reading: what x and y count from (default the page) */ from?: { h: 'page' | 'margin'; v: 'page' | 'margin' | 'paragraph' } } } +/** a line of a cell's border: points wide, RRGGBB */ +export type CellLine = { w: number; color: string } | null + export type TableCell = { paragraphs: Paragraph[] /** columns covered */ @@ -63,6 +76,8 @@ export type TableCell = { fill?: string /** reading: where the text sits in a cell that is taller than it (default the top) */ vAlign?: 'center' | 'bottom' + /** reading: the lines on the cell's four sides, worked out from the table, its style and the cell (null: none); missing means the table's `borders` apply */ + border?: { top: CellLine; right: CellLine; bottom: CellLine; left: CellLine } /** reading: room between the cell's edges and its text, points (Word's default: 5.4 at the sides, none above and below) */ pad?: { top: number; right: number; bottom: number; left: number } } @@ -78,6 +93,13 @@ export type Table = { rowHeights?: number[] /** space below the table, points */ after?: number + /** reading: where the table sits across the page: as aligned, or from the left margin by `indent` (the cell margin Word outdents it by in older documents included) */ + align?: 'left' | 'center' | 'right' + indent?: number + /** reading: rows that must stay on one page (cantSplit); the others are split between lines when a page ends in them */ + noSplit?: boolean[] + /** reading: the first rows are headings that repeat at the top of every page the table continues on */ + headerRows?: number } export type Picture = { @@ -123,10 +145,29 @@ export type Section = { footer?: Block[] firstHeader?: Block[] firstFooter?: Block[] + /** reading: for even-numbered pages when the document says odd and even pages differ */ + evenHeader?: Block[] + evenFooter?: Block[] titlePg?: boolean + /** reading: how the section starts (default on a new page) */ + type?: 'continuous' | 'nextPage' | 'evenPage' | 'oddPage' + /** reading: the number of its first page when it restarts the numbering, and the look of the numbers */ + pageStart?: number + pageFormat?: 'decimal' | 'lowerRoman' | 'upperRoman' | 'lowerLetter' | 'upperLetter' + /** reading: text in columns: their widths (points) and the space between, and a line between them */ + columns?: { widths: number[]; space: number[]; line: boolean } } /** reading: a font file the document carries, for the family, weight and slant it is for */ export type EmbeddedFont = { name: string; bold: boolean; italic: boolean; bytes: Uint8Array } -export type DocModel = { sections: Section[]; title?: string; /** reading: fonts stored in the file (Google Docs saves the ones it uses) */ fonts?: EmbeddedFont[] } +export type DocModel = { + sections: Section[] + title?: string + /** reading: even pages have their own headers and footers */ + evenAndOdd?: boolean + /** reading: where a tab goes when no stop is set, points */ + defaultTab?: number + /** reading: the text of footnotes (`foot:2`) and endnotes (`end:2`), by kind and id */ + notes?: Record + /** reading: fonts stored in the file (Google Docs saves the ones it uses) */ fonts?: EmbeddedFont[] } diff --git a/packages/office-core/src/docx-read.ts b/packages/office-core/src/docx-read.ts index 113f16f..821c8d9 100644 --- a/packages/office-core/src/docx-read.ts +++ b/packages/office-core/src/docx-read.ts @@ -5,7 +5,7 @@ // comments, tracked changes (insertions are kept, deletions dropped), other fields' codes (their shown result is kept), // columns, different even pages, floating tables' positions and shapes other than pictures and text boxes. import { UserError, unzip } from '@ream/core' -import type { Block, DocModel, EmbeddedFont, Paragraph, Picture, Run, Section, Table, TableCell } from './docx-model.ts' +import type { Block, CellLine, DocModel, EmbeddedFont, Paragraph, Picture, Run, Section, Table, TableCell } from './docx-model.ts' import { DEFAULT_THEME, readRels, readTheme, relsOf, type Theme } from './ooxml.ts' import { child, kids, num, parseXml, type XmlNode } from './xml.ts' @@ -17,7 +17,9 @@ const emu = (v: number) => pts(v / 12700) const off = (v: string | undefined) => v === '0' || v === 'false' || v === 'off' || v === 'none' const MAX_BLOCKS = 200_000 -type Style = { type: string; name?: string; basedOn?: string; pPr?: XmlNode; rPr?: XmlNode; tblPr?: XmlNode } +/** what a table style (or one of its conditional parts, such as the first row) sets */ +type TableLook = { pPr?: XmlNode; rPr?: XmlNode; tcPr?: XmlNode; tblPr?: XmlNode } +type Style = { type: string; name?: string; basedOn?: string; pPr?: XmlNode; rPr?: XmlNode; tblPr?: XmlNode; tcPr?: XmlNode; /** a table style's parts for the first row, banded rows and so on */ parts?: Map } const HIGHLIGHT: Record = { yellow: 'FFFF00', green: '00FF00', cyan: '00FFFF', magenta: 'FF00FF', blue: '0000FF', red: 'FF0000', darkBlue: '000080', darkCyan: '008080', darkGreen: '008000', darkMagenta: '800080', darkRed: '800000', darkYellow: '808000', darkGray: '808080', lightGray: 'C0C0C0', black: '000000', white: 'FFFFFF' } // the usual symbol-font bullets as plain characters @@ -34,6 +36,14 @@ type Env = { /** a part of the package as parsed XML (kept: headers are used by every section that names them) */ part: (name: string) => XmlNode | undefined budget: { blocks: number } + /** the number the next footnote or endnote mark gets, how marks are written, and the label of each note by id (the notes' text is read once the body has been) */ + notes: { next: { foot: number; end: number }; format: { foot: string; end: string }; restart: { foot: boolean; end: boolean }; labels: Map } + /** while the text of a footnote or endnote is read: its label (what `footnoteRef` shows) */ + noteLabel?: string + /** while a table cell is read: what the table style sets for it, nearest first, for the paragraphs in it */ + cell?: { pPrs: XmlNode[]; rPrs: XmlNode[] } + /** Word's compatibility mode (15 is Word 2013 and later; older ones, and files that say nothing, set a table's text on the margin and its edge outside it) */ + compat: number } /** the style and those it is based on, nearest first */ @@ -83,7 +93,7 @@ function runOf(text: string, rPrs: (XmlNode | undefined)[], env: Env): Run { } const ROMAN: [number, string][] = [[1000, 'm'], [900, 'cm'], [500, 'd'], [400, 'cd'], [100, 'c'], [90, 'xc'], [50, 'l'], [40, 'xl'], [10, 'x'], [9, 'ix'], [5, 'v'], [4, 'iv'], [1, 'i']] -function numberText(n: number, fmt: string): string { +export function numberText(n: number, fmt: string): string { if (fmt === 'lowerLetter' || fmt === 'upperLetter') { let s = '' for (let k = n; k > 0; k = Math.floor((k - 1) / 26)) s = String.fromCharCode(97 + ((k - 1) % 26)) + s @@ -148,6 +158,28 @@ function tabsOf(seq: ({ ptab: 'left' | 'center' | 'right' } | 'tab')[], pPrs: (X return seq.map((t) => (t === 'tab' ? (stops[n++] ?? null) : { align: t.ptab })) } +/** every tab stop of a paragraph: those of its style chain and defaults, a nearer level adding to them or clearing one */ +function tabStopsOf(pPrs: (XmlNode | undefined)[]): NonNullable { + const stops = new Map[number]>() + for (const pr of [...pPrs].reverse()) { + for (const t of kids(child(pr, 'tabs'), 'tab')) { + const pos = tw(Number(t.attrs.pos)) + const val = t.attrs.val + if (!t.attrs.pos || !Number.isFinite(pos)) continue + if (val === 'clear') stops.delete(pos) + else if (val !== 'bar') { + const leader = t.attrs.leader + stops.set(pos, { + pos, + align: val === 'center' ? 'center' : val === 'right' || val === 'end' ? 'right' : val === 'decimal' ? 'decimal' : 'left', + leader: leader === 'dot' || leader === 'middleDot' ? 'dot' : leader === 'hyphen' ? 'hyphen' : leader === 'underscore' || leader === 'heavy' ? 'underscore' : undefined, + }) + } + } + } + return [...stops.values()].sort((a, b) => a.pos - b.pos) +} + type Out = { blocks: Block[]; sections: Section[] } function picture(node: XmlNode, env: Env): Picture | null { @@ -209,28 +241,32 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { if (env.budget.blocks-- <= 0) throw new UserError('This document is too long to preview.') const pPr = child(p, 'pPr') const styles = styleChain(env, child(pPr, 'pStyle')?.attrs.val ?? (env.styles.has('Normal') ? 'Normal' : undefined)) - const pPrs = [pPr, ...styles.map((s) => s.pPr), env.defaults.pPr] + const pPrs = [pPr, ...styles.map((s) => s.pPr), ...(env.cell?.pPrs ?? []), env.defaults.pPr] const runs: Run[] = [] const extras: Block[] = [] const tabSeq: ({ ptab: 'left' | 'center' | 'right' } | 'tab')[] = [] let breakAfter = false + const bookmarks: string[] = [] // fields (complex ones span runs: begin, the code, separate, the shown result, end; simple ones wrap the result) - type Field = { code: string; shown: boolean; kind?: 'page' | 'pages'; link?: string; done: boolean } + type Field = { code: string; shown: boolean; kind?: 'page' | 'pages' | 'pageref'; link?: string; anchor?: string; ref?: string; done: boolean } const fields: Field[] = [] const classify = (f: Field) => { const code = f.code.trim() - const kind = /^(PAGE|NUMPAGES)\b/i.exec(code)?.[1].toUpperCase() - f.kind = kind === 'PAGE' ? 'page' : kind ? 'pages' : undefined - // a link to a place in the document (\l) has no address + const kind = /^(PAGE|NUMPAGES|PAGEREF)\b/i.exec(code)?.[1].toUpperCase() + f.kind = kind === 'PAGE' ? 'page' : kind === 'PAGEREF' ? 'pageref' : kind ? 'pages' : undefined + f.ref = kind === 'PAGEREF' ? /^PAGEREF\s+"?([^"\s]+)/i.exec(code)?.[1] : undefined + // a link to a place in the document (\l) names a bookmark and has no address const url = /^HYPERLINK\s+"([^"]+)"/i.exec(code)?.[1] + const inside = /\s\\l\s+"([^"]+)"/i.exec(code)?.[1] f.link = url && !/\s\\l\b/i.test(code) ? url : undefined + f.anchor = inside ?? (url && /\s\\l\b/i.test(code) && url.startsWith('#') ? url.slice(1) : undefined) } - const addRuns = (container: XmlNode, link?: string) => { + const addRuns = (container: XmlNode, link?: string, anchor?: string) => { for (const c of container.children) { if (c.name === 'r') { const rPr = child(c, 'rPr') const charStyles = styleChain(env, child(rPr, 'rStyle')?.attrs.val) - const rPrs = [rPr, ...charStyles.map((s) => s.rPr), ...styles.map((s) => s.rPr), env.defaults.rPr] + const rPrs = [rPr, ...charStyles.map((s) => s.rPr), ...styles.map((s) => s.rPr), ...(env.cell?.rPrs ?? []), env.defaults.rPr] const add = (text: string) => { const top = fields[fields.length - 1] if (top && !top.shown) return // the code, not the result @@ -238,13 +274,19 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { // the page number is worked out when the page is drawn: keep one run for it, whatever the file saved if (top.done) return top.done = true - runs.push({ ...runOf(text, rPrs, env), field: top.kind }) + runs.push(linked({ ...runOf(text, rPrs, env), field: top.kind, ref: top.ref })) return } - const r = runOf(text, rPrs, env) + runs.push(linked(runOf(text, rPrs, env))) + } + // where a run leads: an address, or a bookmark of this document + const linked = (r: Run): Run => { + const top = fields[fields.length - 1] const to = top?.link ?? link + const inside = top?.anchor ?? anchor if (to) r.link = to - runs.push(r) + else if (inside) r.anchor = inside + return r } for (const k of c.children) { if (k.name === 't') add(k.text) @@ -255,11 +297,22 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { else if (type === 'end') { const f = fields.pop() // a page number with no result saved (Google Docs writes none) still has to be shown - if (f?.kind && !f.done) runs.push({ ...runOf('1', rPrs, env), field: f.kind }) + if (f?.kind && !f.done) runs.push(linked({ ...runOf('1', rPrs, env), field: f.kind, ref: f.ref })) } } else if (k.name === 'instrText') { const top = fields[fields.length - 1] if (top && !top.shown) top.code += k.text + } else if (k.name === 'footnoteReference' || k.name === 'endnoteReference') { + // a mark gets the next number, whatever the file saved; the note's text is attached by id + const kind = k.name === 'footnoteReference' ? 'foot' : 'end' + const id = k.attrs.id + if (id !== undefined) { + const label = numberText(env.notes.next[kind]++, env.notes.format[kind]) + env.notes.labels.set(`${kind}:${id}`, label) + runs.push({ ...runOf(label, rPrs, env), vertAlign: 'super', note: { id, kind } }) + } + } else if (k.name === 'footnoteRef' || k.name === 'endnoteRef') { + if (env.noteLabel) runs.push({ ...runOf(env.noteLabel, rPrs, env), vertAlign: 'super' }) } else if (k.name === 'tab') { add('\t'); tabSeq.push('tab') } else if (k.name === 'ptab') { const a = k.attrs.alignment @@ -272,7 +325,7 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { const drawing = k.name === 'drawing' ? k : findNode(child(k, 'Choice'), 'drawing') const pic = drawing ? picture(drawing, env) : null // a picture in the line is part of the paragraph's text; one placed on the page is laid out around it - if (pic && !pic.at) runs.push({ ...runOf('', rPrs, env), picture: pic }) + if (pic && !pic.at) runs.push(linked({ ...runOf('', rPrs, env), picture: pic })) else if (pic) extras.push(pic) else if (drawing) textBox(drawing, env, extras) } @@ -280,18 +333,20 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { } else if (c.name === 'hyperlink') { // an address outside the file; a link to a bookmark (anchor) has none const rel = c.attrs.id ? env.rels.get(c.attrs.id) : undefined - addRuns(c, rel?.external ? rel.target : undefined) + addRuns(c, rel?.external ? rel.target : undefined, c.attrs.anchor) } else if (c.name === 'fldSimple') { const f: Field = { code: c.attrs.instr ?? '', shown: true, done: false } classify(f) fields.push(f) - addRuns(c, link) + addRuns(c, link, anchor) fields.pop() - if (f.kind && !f.done) runs.push({ ...runOf('1', [...styles.map((x) => x.rPr), env.defaults.rPr], env), field: f.kind }) - } else if (['ins', 'smartTag', 'customXml'].includes(c.name)) addRuns(c, link) + if (f.kind && !f.done) runs.push({ ...runOf('1', [...styles.map((x) => x.rPr), env.defaults.rPr], env), field: f.kind, ref: f.ref }) + } else if (c.name === 'bookmarkStart') { + if (c.attrs.name && c.attrs.name !== '_GoBack') bookmarks.push(c.attrs.name) + } else if (['ins', 'smartTag', 'customXml'].includes(c.name)) addRuns(c, link, anchor) else if (c.name === 'sdt') { const content = child(c, 'sdtContent') - if (content) addRuns(content, link) + if (content) addRuns(content, link, anchor) } } } @@ -326,6 +381,8 @@ function readParagraph(p: XmlNode, env: Env, out: Out, inCell = false) { keepLines: flagOf(pPrs, 'keepLines'), widowControl: first(pPrs, 'widowControl') ? !off(first(pPrs, 'widowControl')!.attrs.val) : undefined, tabs: tabSeq.length ? tabsOf(tabSeq, pPrs) : undefined, + tabStops: tabSeq.length ? tabStopsOf(pPrs) : undefined, + bookmarks: bookmarks.length ? bookmarks : undefined, } void inCell out.blocks.push(para, ...extras) @@ -363,42 +420,134 @@ function margins(m: XmlNode | undefined): Partial> return { top: side('top'), right: side('right', 'end'), bottom: side('bottom'), left: side('left', 'start') } } +/** the table look flags: which of the style's special parts apply (first row, banded rows ...) */ +function tableLook(tblPr: XmlNode | undefined) { + const look = child(tblPr, 'tblLook') + const bits = Number.parseInt(look?.attrs.val ?? '04A0', 16) || 0 + const flag = (name: string, bit: number) => (look?.attrs[name] !== undefined ? look.attrs[name] === '1' || look.attrs[name] === 'true' : !!(bits & bit)) + return { firstRow: flag('firstRow', 0x20), lastRow: flag('lastRow', 0x40), firstCol: flag('firstColumn', 0x80), lastCol: flag('lastColumn', 0x100), hBand: !flag('noHBand', 0x200), vBand: !flag('noVBand', 0x400) } +} + +type Look = ReturnType + +/** the parts of a table style that apply to the cell at (row, col), the one that wins last: the whole table, bands, last and first column, last and first row, the corners */ +function stylePartsFor(row: number, col: number, rows: number, cols: number, look: Look): string[] { + const out = ['wholeTable'] + const bodyRow = row - (look.firstRow ? 1 : 0) + const isHead = look.firstRow && row === 0 + const isFoot = look.lastRow && row === rows - 1 + const isFirstCol = look.firstCol && col === 0 + const isLastCol = look.lastCol && col === cols - 1 + if (look.vBand && !isFirstCol && !isLastCol) out.push((col - (look.firstCol ? 1 : 0)) % 2 === 0 ? 'band1Vert' : 'band2Vert') + if (look.hBand && !isHead && !isFoot) out.push(bodyRow % 2 === 0 ? 'band1Horz' : 'band2Horz') + if (isLastCol) out.push('lastCol') + if (isFirstCol) out.push('firstCol') + if (isFoot) out.push('lastRow') + if (isHead) out.push('firstRow') + if (isHead && isFirstCol) out.push('nwCell') + if (isHead && isLastCol) out.push('neCell') + if (isFoot && isFirstCol) out.push('swCell') + if (isFoot && isLastCol) out.push('seCell') + return out +} + +/** one side of a border: a line, or null for none */ +function lineOf(b: XmlNode | undefined): CellLine | undefined { + if (!b) return undefined + const val = b.attrs.val + if (!val || val === 'nil' || val === 'none') return null + const color = b.attrs.color + return { w: Math.max(0.25, Math.min(12, num(b, 'sz', 4) / 8)), color: color && /^[0-9a-f]{6}$/i.test(color) ? color.toUpperCase() : '000000' } +} + function readTable(t: XmlNode, env: Env, out: Out) { const tblPr = child(t, 'tblPr') const style = styleChain(env, child(tblPr, 'tblStyle')?.attrs.val) - const borders = [child(tblPr, 'tblBorders'), ...style.map((s) => child(s.tblPr, 'tblBorders'))].find(Boolean) - const hasBorders = !!borders && borders.children.some((b) => b.attrs.val && !off(b.attrs.val) && b.attrs.val !== 'nil') // the table's own cell margins, else its style's const tableMar = margins([child(tblPr, 'tblCellMar'), ...style.map((s) => child(s.tblPr, 'tblCellMar'))].find(Boolean)) const colWidths = kids(child(t, 'tblGrid'), 'gridCol').map((g) => tw(num(g, 'w'))) + const trs = kids(t, 'tr') + const look = tableLook(tblPr) + const nCols = Math.max(1, colWidths.length || Math.max(1, ...trs.map((tr) => kids(tr, 'tc').reduce((n, tc) => n + num(child(child(tc, 'tcPr'), 'gridSpan'), 'val', 1), 0)))) + /** what the style says for a kind of property at this cell, the most specific first: its parts that apply (the last of them wins), then the style as a whole */ + const sources = (kind: 'pPr' | 'rPr' | 'tcPr' | 'tblPr', parts: string[]): XmlNode[] => { + const found: XmlNode[] = [] + for (const key of [...parts].reverse()) for (const st of style) { const n = st.parts?.get(key)?.[kind]; if (n) found.push(n) } + for (const st of style) if (st[kind]) found.push(st[kind]!) + return found + } + const tableBorders = [child(tblPr, 'tblBorders'), ...sources('tblPr', ['wholeTable']).map((n) => child(n, 'tblBorders'))].filter(Boolean) as XmlNode[] const rows: TableCell[][] = [] - for (const tr of kids(t, 'tr')) { + let anyBorder = false + trs.forEach((tr, ri) => { const row: TableCell[] = [] + let col = 0 for (const tc of kids(tr, 'tc')) { const pr = child(tc, 'tcPr') const vm = child(pr, 'vMerge') - const fill = child(pr, 'shd')?.attrs.fill + const span = Math.max(1, num(child(pr, 'gridSpan'), 'val', 1)) + const parts = stylePartsFor(ri, col, trs.length, nCols, look) + const tcPrs = [pr, ...sources('tcPr', parts)].filter(Boolean) as XmlNode[] + // paragraphs in the cell take what the table style sets for it (after their own style, before the document's defaults) + const was = env.cell + env.cell = { pPrs: sources('pPr', parts), rPrs: sources('rPr', parts) } const sub: Out = { blocks: [], sections: [] } for (const c of tc.children) { if (c.name === 'p') readParagraph(c, env, sub, true) // ponytail: a table inside a cell is read as its paragraphs, one after the other else if (c.name === 'tbl') for (const p of findAll(c, 'p')) readParagraph(p, env, sub, true) } - const own = margins(child(pr, 'tcMar')) + env.cell = was + const fill = first(tcPrs, 'shd')?.attrs.fill + // borders: the cell's own, else its style's, else the table's (the outside ones at the edges, the inside ones between cells) + const own = tcPrs.map((x) => child(x, 'tcBorders')).filter(Boolean) as XmlNode[] + const side = (names: string[], tableNames: string[]) => { + for (const group of [own, tableBorders]) { + for (const g of group) { + const n = (group === own ? names : tableNames).map((k) => child(g, k)).find(Boolean) + const l = lineOf(n) + if (l !== undefined) return l + } + } + return null + } + const lastRow = ri === trs.length - 1 + const lastCol = col + span >= nCols + const border = { + top: side(['top'], [ri === 0 ? 'top' : 'insideH']), + bottom: side(['bottom'], [lastRow ? 'bottom' : 'insideH']), + left: side(['left', 'start'], [col === 0 ? 'left' : 'insideV', ...(col === 0 ? ['start'] : [])]), + right: side(['right', 'end'], [lastCol ? 'right' : 'insideV', ...(lastCol ? ['end'] : [])]), + } + if (border.top || border.bottom || border.left || border.right) anyBorder = true + const mar = margins(child(pr, 'tcMar')) const va = child(pr, 'vAlign')?.attrs.val row.push({ vAlign: va === 'center' || va === 'bottom' ? va : undefined, - pad: { top: own.top ?? tableMar.top ?? 0, right: own.right ?? tableMar.right ?? 5.4, bottom: own.bottom ?? tableMar.bottom ?? 0, left: own.left ?? tableMar.left ?? 5.4 }, + pad: { top: mar.top ?? tableMar.top ?? 0, right: mar.right ?? tableMar.right ?? 5.4, bottom: mar.bottom ?? tableMar.bottom ?? 0, left: mar.left ?? tableMar.left ?? 5.4 }, paragraphs: sub.blocks.filter((b): b is Paragraph => b.kind === 'p'), - cols: num(child(pr, 'gridSpan'), 'val', 1) > 1 ? num(child(pr, 'gridSpan'), 'val') : undefined, + cols: span > 1 ? span : undefined, vMerge: vm ? (vm.attrs.val === 'restart' ? 'restart' : 'continue') : undefined, fill: fill && fill !== 'auto' && /^[0-9a-f]{6}$/i.test(fill) ? fill.toUpperCase() : undefined, + border, }) + col += span } rows.push(row) - } - const heights = kids(t, 'tr').map((tr) => tw(num(child(child(tr, 'trPr'), 'trHeight'), 'val'))) - const table: Table = { kind: 'table', colWidths: colWidths.length ? colWidths : [468], rows, borders: hasBorders, rowHeights: heights.some(Boolean) ? heights : undefined } + }) + const heights = trs.map((tr) => tw(num(child(child(tr, 'trPr'), 'trHeight'), 'val'))) + // rows marked as headings, from the top, repeat on each page the table continues on + let headerRows = 0 + while (headerRows < trs.length && child(child(trs[headerRows], 'trPr'), 'tblHeader') && !off(child(child(trs[headerRows], 'trPr'), 'tblHeader')!.attrs.val)) headerRows++ + const noSplit = trs.map((tr) => { + const c = child(child(tr, 'trPr'), 'cantSplit') + return !!c && !off(c.attrs.val) + }) + const jc = child(tblPr, 'jc')?.attrs.val + const align = jc === 'center' ? 'center' : jc === 'right' || jc === 'end' ? 'right' : undefined + // in documents from before Word 2013 a table's text, not its edge, is on the margin: the edge is outside it by the cell's margin + const ind = tw(num(child(tblPr, 'tblInd'), 'w')) - (align || env.compat >= 15 ? 0 : (tableMar.left ?? rows[0]?.[0]?.pad?.left ?? 5.4)) + const table: Table = { kind: 'table', colWidths: colWidths.length ? colWidths : [468], rows, borders: anyBorder, rowHeights: heights.some(Boolean) ? heights : undefined, align, indent: ind || undefined, headerRows: headerRows || undefined, noSplit: noSplit.some(Boolean) ? noSplit : undefined } out.blocks.push(table) } @@ -420,6 +569,24 @@ function partBlocks(env: Env, part: string): Block[] { return out.blocks } +const PAGE_FORMATS = new Set(['decimal', 'lowerRoman', 'upperRoman', 'lowerLetter', 'upperLetter']) + +/** the columns a section sets: their widths and the space after each (an equal-width set shares what the margins leave) */ +function columnsOf(sect: XmlNode | undefined, text: number): Section['columns'] { + const cols = child(sect, 'cols') + if (!cols) return undefined + const space = tw(num(cols, 'space', 720)) + const listed = kids(cols, 'col') + if (listed.length > 1 && cols.attrs.equalWidth === '0') { + const widths = listed.map((c) => Math.max(10, tw(num(c, 'w')))) + return { widths, space: listed.map((c, i) => (i < listed.length - 1 ? tw(num(c, 'space', 720)) : 0)), line: cols.attrs.sep === '1' || cols.attrs.sep === 'true' } + } + const n = Math.max(1, Math.min(12, num(cols, 'num', listed.length || 1))) + if (n === 1) return undefined + const each = Math.max(10, (text - space * (n - 1)) / n) + return { widths: Array(n).fill(each), space: Array.from({ length: n }, (_, i) => (i < n - 1 ? space : 0)), line: cols.attrs.sep === '1' || cols.attrs.sep === 'true' } +} + function sectionOf(sect: XmlNode | undefined, blocks: Block[], env: Env, before?: Section): Section { const sz = child(sect, 'pgSz') const mar = child(sect, 'pgMar') @@ -430,22 +597,55 @@ function sectionOf(sect: XmlNode | undefined, blocks: Block[], env: Env, before? return rel ? partBlocks(env, rel.part) : was } const title = child(sect, 'titlePg') + const width = tw(num(sz, 'w', 11906)) + const margins = { top: tw(Math.abs(num(mar, 'top', 1440))), right: tw(num(mar, 'right', 1440)), bottom: tw(Math.abs(num(mar, 'bottom', 1440))), left: tw(num(mar, 'left', 1440)), header: tw(num(mar, 'header', 720)), footer: tw(num(mar, 'footer', 720)) } + const numType = child(sect, 'pgNumType') + const fmt = numType?.attrs.fmt + const kind = child(sect, 'type')?.attrs.val return { - width: tw(num(sz, 'w', 11906)), + width, height: tw(num(sz, 'h', 16838)), - margins: { top: tw(Math.abs(num(mar, 'top', 1440))), right: tw(num(mar, 'right', 1440)), bottom: tw(Math.abs(num(mar, 'bottom', 1440))), left: tw(num(mar, 'left', 1440)), header: tw(num(mar, 'header', 720)), footer: tw(num(mar, 'footer', 720)) }, + margins, blocks, header: hf('headerReference', 'default', before?.header), footer: hf('footerReference', 'default', before?.footer), firstHeader: hf('headerReference', 'first', before?.firstHeader), firstFooter: hf('footerReference', 'first', before?.firstFooter), + evenHeader: hf('headerReference', 'even', before?.evenHeader), + evenFooter: hf('footerReference', 'even', before?.evenFooter), titlePg: title ? !off(title.attrs.val) : undefined, + type: kind === 'continuous' || kind === 'evenPage' || kind === 'oddPage' ? kind : undefined, + pageStart: numType?.attrs.start !== undefined && Number.isFinite(Number(numType.attrs.start)) ? Math.max(0, Math.min(1e6, Math.round(Number(numType.attrs.start)))) : undefined, + pageFormat: fmt && PAGE_FORMATS.has(fmt) ? (fmt as Section['pageFormat']) : undefined, + columns: columnsOf(sect, width - margins.left - margins.right), } } function endSection(sect: XmlNode, out: Out, env: Env) { out.sections.push(sectionOf(sect, out.blocks, env, out.sections[out.sections.length - 1])) out.blocks = [] + // notes that number again in each section + if (env.notes.restart.foot) env.notes.next.foot = 1 + if (env.notes.restart.end) env.notes.next.end = 1 +} + +/** the text of the notes the body refers to, by `foot:id` / `end:id`: read from their part with its own relationships */ +function readNotes(env: Env, part: string | undefined, tag: 'footnote' | 'endnote', kind: 'foot' | 'end'): Record { + const root = part ? env.part(part) : undefined + const out: Record = {} + if (!part || !root) return out + const sub: Env = { ...env, rels: readRels(env.part(relsOf(part)), part), counters: new Map() } + for (const note of kids(root, tag)) { + const id = note.attrs.id + const label = id === undefined ? undefined : env.notes.labels.get(`${kind}:${id}`) + // separators and the like, and notes nothing refers to, are left out + if (label === undefined || (note.attrs.type && note.attrs.type !== 'normal')) continue + sub.noteLabel = label + const o: Out = { blocks: [], sections: [] } + walk(note, sub, o) + out[`${kind}:${id}`] = o.blocks.filter((b): b is Paragraph => b.kind === 'p') + } + return out } /** the paragraphs and tables under a body, header or footer element, in order */ @@ -504,7 +704,7 @@ export async function readDocx(bytes: Uint8Array): Promise { const stylesXml = partOf('/styles') ? xml(partOf('/styles')!) : undefined const styles = new Map() for (const s of kids(stylesXml, 'style')) { - if (s.attrs.styleId) styles.set(s.attrs.styleId, { type: s.attrs.type ?? '', name: child(s, 'name')?.attrs.val, basedOn: child(s, 'basedOn')?.attrs.val, pPr: child(s, 'pPr'), rPr: child(s, 'rPr'), tblPr: child(s, 'tblPr') }) + if (s.attrs.styleId) styles.set(s.attrs.styleId, { type: s.attrs.type ?? '', name: child(s, 'name')?.attrs.val, basedOn: child(s, 'basedOn')?.attrs.val, pPr: child(s, 'pPr'), rPr: child(s, 'rPr'), tblPr: child(s, 'tblPr'), tcPr: child(s, 'tcPr'), parts: new Map(kids(s, 'tblStylePr').map((x) => [x.attrs.type ?? '', { pPr: child(x, 'pPr'), rPr: child(x, 'rPr'), tcPr: child(x, 'tcPr'), tblPr: child(x, 'tblPr') }])) }) } const defaults = { pPr: child(child(child(stylesXml, 'docDefaults'), 'pPrDefault'), 'pPr'), rPr: child(child(child(stylesXml, 'docDefaults'), 'rPrDefault'), 'rPr') } const numXml = partOf('/numbering') ? xml(partOf('/numbering')!) : undefined @@ -512,6 +712,10 @@ export async function readDocx(bytes: Uint8Array): Promise { for (const a of kids(numXml, 'abstractNum')) if (a.attrs.abstractNumId) abstracts.set(a.attrs.abstractNumId, a) const nums = new Map() for (const n of kids(numXml, 'num')) if (n.attrs.numId) nums.set(n.attrs.numId, child(n, 'abstractNumId')?.attrs.val ?? '') + const settings = partOf('/settings') ? xml(partOf('/settings')!) : undefined + const compat = Math.max(0, ...kids(child(settings, 'compat'), 'compatSetting').filter((c) => c.attrs.name === 'compatibilityMode').map((c) => num(c, 'val', 0))) + const evenAndOdd = child(settings, 'evenAndOddHeaders') + const defaultTab = child(settings, 'defaultTabStop') const themePart = partOf('/theme') const partCache = new Map() const env: Env = { @@ -526,12 +730,27 @@ export async function readDocx(bytes: Uint8Array): Promise { return partCache.get(n) }, budget: { blocks: MAX_BLOCKS }, + compat: compat || 12, + notes: { + next: { foot: 1, end: 1 }, + format: { foot: child(child(settings, 'footnotePr'), 'numFmt')?.attrs.val ?? 'decimal', end: child(child(settings, 'endnotePr'), 'numFmt')?.attrs.val ?? 'lowerRoman' }, + restart: { foot: child(child(settings, 'footnotePr'), 'numRestart')?.attrs.val === 'eachSect', end: child(child(settings, 'endnotePr'), 'numRestart')?.attrs.val === 'eachSect' }, + labels: new Map(), + }, } const out: Out = { blocks: [], sections: [] } const body = child(doc, 'body') if (body) walk(body, env, out) out.sections.push(sectionOf(child(body, 'sectPr'), out.blocks, env, out.sections[out.sections.length - 1])) + // the notes' text, now that the body has numbered the marks + const notes = { ...readNotes(env, partOf('/footnotes'), 'footnote', 'foot'), ...readNotes(env, partOf('/endnotes'), 'endnote', 'end') } const fontPart = partOf('/fontTable') const fonts = fontPart ? embeddedFonts(xml(fontPart), readRels(xml(relsOf(fontPart)), fontPart), parts) : [] - return { sections: out.sections.filter((s, i) => s.blocks.length || i === out.sections.length - 1), ...(fonts.length ? { fonts } : {}) } + return { + sections: out.sections.filter((s, i) => s.blocks.length || i === out.sections.length - 1), + ...(evenAndOdd && !off(evenAndOdd.attrs.val) ? { evenAndOdd: true } : {}), + ...(Object.keys(notes).length ? { notes } : {}), + ...(defaultTab ? { defaultTab: Math.max(1, tw(num(defaultTab, 'val', 720))) } : {}), + ...(fonts.length ? { fonts } : {}), + } } diff --git a/packages/office-core/src/index.ts b/packages/office-core/src/index.ts index 18fc4ce..4d80a14 100644 --- a/packages/office-core/src/index.ts +++ b/packages/office-core/src/index.ts @@ -6,11 +6,11 @@ export type { Anchor, Cell, Edge, Merge, PageSetup, Sheet, SheetChart, SheetPict export { writeXlsx, sheetNames } from './xlsx.ts' export { readXlsx, type Workbook } from './xlsx-read.ts' export { writeDocx } from './docx.ts' -export type { DocModel, EmbeddedFont, Section as DocSection, Block as DocBlock, Paragraph, Run, Table as DocTable, TableCell as DocTableCell, Picture } from './docx-model.ts' +export type { CellLine, DocModel, EmbeddedFont, Section as DocSection, Block as DocBlock, Paragraph, Run, Table as DocTable, TableCell as DocTableCell, Picture } from './docx-model.ts' export { writePptx } from './pptx.ts' export type { Deck, Slide, SlideShape, SlideBox, SlidePara, SlideRun, SlidePicture, SlideLine, SlideTable, SlideTableCell, SlideChart, Paint, Stroke, LineEnd, CustomPath, Xform } from './pptx-model.ts' export { readPptx } from './pptx-read.ts' export { resolveColor, type ColorContext } from './drawingml.ts' export { usedFonts } from './fonts-used.ts' -export { readDocx } from './docx-read.ts' +export { readDocx, numberText } from './docx-read.ts' export { readChart, type Chart, type ChartLookup, type ChartKind, type ChartPlot, type ChartSeries } from './chart-read.ts' diff --git a/packages/office-core/src/pptx-model.ts b/packages/office-core/src/pptx-model.ts index e5ccd37..e576431 100644 --- a/packages/office-core/src/pptx-model.ts +++ b/packages/office-core/src/pptx-model.ts @@ -50,6 +50,10 @@ export type SlidePara = { bulletFont?: string bulletColor?: string bulletScale?: number + /** tab stops by position from the left of the text area (points): where a tab character goes, and the line of dots or dashes before it */ + tabStops?: { pos: number; align: 'left' | 'center' | 'right' | 'decimal'; leader?: 'dot' | 'hyphen' | 'underscore' }[] + /** where a tab goes after the last stop, every this many points (default 36) */ + defaultTab?: number } export type SlideBox = { diff --git a/packages/office-core/test/docx-helper.ts b/packages/office-core/test/docx-helper.ts index 6d91171..27d49a4 100644 --- a/packages/office-core/test/docx-helper.ts +++ b/packages/office-core/test/docx-helper.ts @@ -63,6 +63,16 @@ export const inline = (w: number, h = w, srcRect = '') => `` + `${srcRect}` +/** the notes part of a document (`footnotes` or `endnotes`): each note is [id, text], the first paragraph starting with the mark Word puts there */ +export const notesPart = (kind: 'footnotes' | 'endnotes', items: [string, string][]) => { + const one = kind === 'footnotes' ? 'footnote' : 'endnote' + const sep = `` + return `${sep}${items.map(([id, text]) => ` ${text}`).join('')}` +} + +/** a mark in the text for a note */ +export const noteMark = (kind: 'footnote' | 'endnote', id: string) => `` + /** a header or footer part: `kind` is 'hdr' or 'ftr' */ export const part = (kind: 'hdr' | 'ftr', inner: string) => `${inner}` diff --git a/packages/office-core/test/docx-read.test.ts b/packages/office-core/test/docx-read.test.ts index 54fc46f..2a30c65 100644 --- a/packages/office-core/test/docx-read.test.ts +++ b/packages/office-core/test/docx-read.test.ts @@ -1,7 +1,7 @@ import { readFileSync } from 'node:fs' import { expect, test } from 'vitest' -import { readDocx, usedFonts, type DocBlock, type Paragraph } from '../src/index.ts' -import { PIXEL, inline, makeDocx, para, part, scramble, withFonts } from './docx-helper.ts' +import { readDocx, usedFonts, type DocBlock, type DocTable, type Paragraph } from '../src/index.ts' +import { PIXEL, inline, makeDocx, noteMark, notesPart, para, part, scramble, withFonts } from './docx-helper.ts' // report.docx (python-docx, its default template): a title, mixed runs, a centred line, bullets, a numbered list, a grid // table, a picture, a page break and a heading on the second page @@ -111,3 +111,66 @@ test('a picture in a line is a run of its paragraph, with its crop (cut from the const none = await makeDocx({ rels: [['pic', 'image', 'media/pic.png']], parts: { 'word/media/pic.png': PIXEL }, body: `${inline(10, 10, '')}` }) expect(((await readDocx(none)).sections[0].blocks[0] as Paragraph).runs[0].picture?.crop).toBeUndefined() }) + +// a table style the way Word writes one: borders for the whole table, a bold white-on-blue first row, grey banded rows +test('a table style gives its cells fills, borders, bold and banding; the cell\'s own settings win; heading rows are marked', async () => { + const styles = + '' + + '' + + '' + + '' + const cell = (text: string, props = '') => `${props}${para(text)}` + const row = (cells: string, header = false) => `${header ? '' : ''}${cells}` + const bytes = await makeDocx({ + styles, + body: + '' + + row(cell('H1') + cell('H2'), true) + row(cell('a') + cell('b', '')) + row(cell('c') + cell('d')) + row(cell('e') + cell('f')) + + '', + }) + const t = (await readDocx(bytes)).sections[0].blocks[0] as DocTable + const [head, one, two, three] = t.rows + expect(t.headerRows).toBe(1) + // the first row: the style's blue and its bold white text + expect(head[0].fill).toBe('1F4E79') + expect(head[0].paragraphs[0].runs[0]).toMatchObject({ bold: true, color: 'FFFFFF' }) + // banding counts the rows below the first: band 1 is grey, band 2 has nothing, band 1 again + expect([one[0].fill, two[0].fill, three[0].fill]).toEqual(['DDDDDD', undefined, 'DDDDDD']) + expect(one[0].paragraphs[0].runs[0].bold).toBeUndefined() + // the cell's own shading and left border win over the style's; the rest is the style's + expect(one[1].fill).toBe('FFFF00') + expect(one[1].border?.left).toEqual({ w: 2, color: '0000FF' }) + // borders: the outer ones at the edges (1 pt), the inner ones between rows (0.5 pt), none between columns the style doesn't give + expect(head[0].border).toMatchObject({ top: { w: 1, color: 'FF0000' }, bottom: { w: 0.5, color: '00FF00' }, left: null }) + expect(three[1].border).toMatchObject({ top: { w: 0.5, color: '00FF00' }, bottom: { w: 1, color: 'FF0000' }, right: null }) +}) + +test('footnote and endnote marks are numbered in the order they come, whatever ids they have, with their text attached', async () => { + const bytes = await makeDocx({ + rels: [['fn', 'footnotes', 'footnotes.xml'], ['en', 'endnotes', 'endnotes.xml']], + parts: { 'word/footnotes.xml': notesPart('footnotes', [['7', 'Seven.'], ['3', 'Three.']]), 'word/endnotes.xml': notesPart('endnotes', [['2', 'The end.']]) }, + body: `First${noteMark('footnote', '3')}second${noteMark('footnote', '7')}${noteMark('endnote', '2')}`, + }) + const doc = await readDocx(bytes) + const p = doc.sections[0].blocks[0] as Paragraph + expect(p.runs.filter((r) => r.note).map((r) => [r.text, r.note, r.vertAlign])).toEqual([['1', { id: '3', kind: 'foot' }, 'super'], ['2', { id: '7', kind: 'foot' }, 'super'], ['i', { id: '2', kind: 'end' }, 'super']]) + // the notes carry their own mark and text; the separators are left out + expect(Object.keys(doc.notes ?? {}).sort()).toEqual(['end:2', 'foot:3', 'foot:7']) + expect(doc.notes!['foot:3'][0].runs.map((r) => r.text).join('')).toBe('1 Three.') + expect(doc.notes!['foot:7'][0].runs.map((r) => r.text).join('')).toBe('2 Seven.') + expect(doc.notes!['end:2'][0].runs.map((r) => r.text).join('')).toBe('i The end.') +}) + +test('columns: equal ones share the text width less the space between them; listed ones keep their widths; a section can start where the last one ended', async () => { + const pg = '' + const sect = (inner: string) => `${inner}${pg}` + const bytes = await makeDocx({ + body: para('one') + sect('') + para('two') + sect('') + para('three') + `${pg}`, + }) + const [a, b, c] = (await readDocx(bytes)).sections + expect(a.columns).toEqual({ widths: [216, 216], space: [36, 0], line: true }) // (468 - 36) / 2 each + expect(b.type).toBe('continuous') + expect(b.columns).toEqual({ widths: [100, 150, 200], space: [10, 10, 0], line: false }) + expect(c.type).toBe('oddPage') + expect(c.columns).toBeUndefined() // one column is no columns +}) diff --git a/packages/pdf-core/src/build.ts b/packages/pdf-core/src/build.ts index b2a26b0..9a1ba1e 100644 --- a/packages/pdf-core/src/build.ts +++ b/packages/pdf-core/src/build.ts @@ -155,8 +155,8 @@ const LINK_SCHEMES = new Set(['http:', 'https:', 'mailto:']) export class PageDraw { ops: Op[] = [] - /** clickable boxes, in the page's own y-down points */ - links: { x: number; y: number; w: number; h: number; uri: string }[] = [] + /** clickable boxes, in the page's own y-down points: to a web or mail address, or to a place in the document (a page, 0-based, and how far down it that place is) */ + links: { x: number; y: number; w: number; h: number; uri?: string; to?: { page: number; y: number } }[] = [] width: number height: number constructor(width: number, height: number) { @@ -184,6 +184,10 @@ export class PageDraw { // not an address } } + /** a clickable box that jumps to a place in the document: page `page` (from 0), `top` points from its top */ + goto(x: number, y: number, w: number, h: number, page: number, top: number) { + if (w > 0 && h > 0 && Number.isInteger(page) && page >= 0 && Number.isFinite(top)) this.links.push({ x, y, w, h, to: { page, y: top } }) + } /** draw a path, filled and/or stroked */ path(segs: Seg[], style: PathStyle) { if (segs.length && (style.fill || style.stroke)) this.ops.push({ k: 'path', segs, style }) } /** @@ -387,8 +391,9 @@ export async function buildPdf(d: PdfDrawing): Promise<{ bytes: Uint8Array; lost } const imageRefs: PdfRef[] = [] for (const img of d.images) imageRefs.push(await addImage(b, img)) - const kids: PdfRef[] = [] - for (const p of d.pages) { + // every page's object is reserved first, so a link can point at a page that comes after it + const kids = d.pages.map(() => b.reserve()) + for (const [pi, p] of d.pages.entries()) { const pr: PageRes = { alphas: new Map(), shadings: [], std: new Set(), parts: new Set() } const text = content(p, lost, pr, prepared, resName) const images = new Set(p.ops.flatMap((o) => (o.k === 'image' ? [o.id] : []))) @@ -401,14 +406,19 @@ export async function buildPdf(d: PdfDrawing): Promise<{ bytes: Uint8Array; lost if (images.size) res.set('XObject', new Map([...images].map((i) => [`Im${i}`, imageRefs[i]]))) const stream = b.add(new PdfStream(new Map([['Filter', name('FlateDecode')]]), await deflate(ascii(text)))) const page = new Map([['Type', name('Page')], ['Parent', pagesRef], ['MediaBox', [0, 0, p.width, p.height]], ['Contents', stream], ['Resources', res]]) - if (p.links.length) { - page.set('Annots', p.links.map((l) => b.add(new Map([ - ['Type', name('Annot')], ['Subtype', name('Link')], ['F', 4], ['Border', [0, 0, 0]], - ['Rect', [l.x, p.height - l.y - l.h, l.x + l.w, p.height - l.y].map((v) => Math.round(v * 100) / 100)], - ['A', new Map([['S', name('URI')], ['URI', new PdfString(ascii(l.uri))]])], - ])))) + const links = p.links.filter((l) => l.uri !== undefined || (l.to && l.to.page < d.pages.length)) + if (links.length) { + page.set('Annots', links.map((l) => { + const dict = new Map([ + ['Type', name('Annot')], ['Subtype', name('Link')], ['F', 4], ['Border', [0, 0, 0]], + ['Rect', [l.x, p.height - l.y - l.h, l.x + l.w, p.height - l.y].map((v) => Math.round(v * 100) / 100)], + ]) + if (l.uri !== undefined) dict.set('A', new Map([['S', name('URI')], ['URI', new PdfString(ascii(l.uri))]])) + else dict.set('Dest', [kids[l.to!.page], name('XYZ'), null, Math.round((d.pages[l.to!.page].height - l.to!.y) * 100) / 100, null]) + return b.add(dict) + })) } - kids.push(b.add(page)) + b.set(kids[pi], page) } b.set(pagesRef, new Map([['Type', name('Pages')], ['Kids', kids], ['Count', kids.length]])) const cat = new Map([['Type', name('Catalog')], ['Pages', pagesRef]]) diff --git a/packages/pdf-core/test/build.test.ts b/packages/pdf-core/test/build.test.ts index d52c6dc..bf8f7e2 100644 --- a/packages/pdf-core/test/build.test.ts +++ b/packages/pdf-core/test/build.test.ts @@ -128,3 +128,19 @@ test('links become URI annotations in page space (web and mail only); bookmarks const marks = readOutline(doc) expect(marks.map((m) => [m.title, m.page, m.children.map((c) => c.title)])).toEqual([['One', 0, []], ['Two', 1, ['Two.a']]]) }) + +test('a link to a place in the document points at its page, however far on, and how far down', async () => { + const d = new PdfDrawing() + d.addPage(200, 100).goto(10, 20, 50, 12, 2, 30) + d.pages[0].goto(0, 0, 10, 10, 9, 0) // no such page: left out + d.addPage(200, 100) + d.addPage(300, 400) + const doc = await PdfDocument.load((await buildPdf(d)).bytes) + const annots = doc.resolve(doc.pages()[0].dict.get('Annots')) as unknown as PdfObj[] + expect(annots).toHaveLength(1) + const a = doc.resolve(annots[0]) as Map + const dest = doc.resolve(a.get('Dest')) as unknown as PdfObj[] + expect(doc.resolve(dest[0])).toBe(doc.pages()[2].dict) // the third page, which comes after the link's own + expect(dest.slice(2)).toEqual([null, 370, null]) // 400 - 30 from the bottom of that page + expect(a.get('A')).toBeUndefined() +}) diff --git a/packages/tools/src/m7b-tools.test.ts b/packages/tools/src/m7b-tools.test.ts index 41a09b3..9eda48d 100644 --- a/packages/tools/src/m7b-tools.test.ts +++ b/packages/tools/src/m7b-tools.test.ts @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs' import { beforeAll, describe, expect, test } from 'vitest' import type { FileData } from '@ream/core' import { decodeJpeg, decodePng, ownCodec, setCodec } from '@ream/image-core' -import { PdfDocument, readOutline } from '@ream/pdf-core' +import { PdfDocument, readOutline, type PdfObj } from '@ream/pdf-core' import type { PageRaster } from '@ream/pdf-render' import { readXlsx, writeXlsx, writePptx, parseXml, kids, child, type Sheet, type XmlNode } from '@ream/office-core' import { unzip } from '@ream/core' @@ -20,7 +20,7 @@ import { nodeFonts } from './fonts/node.ts' import { parseNumber } from './pdf-to-excel/number.ts' import { officeToPdf } from './office-preview.ts' import { docxToPdf, run as wordToPdf } from './word-to-pdf/run.ts' -import { PIXEL, inline, makeDocx, para, part, placed, scramble, withFonts } from '../../office-core/test/docx-helper.ts' +import { PIXEL, inline, makeDocx, noteMark, notesPart, para, part, placed, scramble, withFonts } from '../../office-core/test/docx-helper.ts' import { makeTtf } from '../../pdf-core/test/ttf-helper.ts' beforeAll(() => setCodec(ownCodec)) // JPG pages go through the codec; the browser's is checked in the app @@ -687,6 +687,172 @@ describe('word to pdf', () => { expect(await pageText(pdf, 0)).toContain('Head') }) + const settingsRel: [string, string, string][] = [['rSet', 'settings', 'settings.xml']] + const settings = (inner: string) => `${inner}` + + test('tab stops: a right stop with dots (a table of contents line), a stop in the middle, and the default stops every 36 pt', async () => { + const stops = '' + const toc = await PdfDocument.load(await pdfOf(makeDocx({ body: `${stops}Chapter one12${pg}` }))) + const chars = (await pageChars(toc, 0)).chars.filter((c) => c.text.trim()) + const text = chars.map((c) => c.text).join('') + expect(text).toMatch(/^Chapterone\.{20,}12$/) // dots fill the tab, then the number (spaces aren't in the letters) + expect(chars[chars.length - 1].x1).toBeCloseTo(540, 0) // the number ends at the right stop (72 + 468) + expect(chars[0].x0).toBeCloseTo(72, 0) + // no stop set: the next multiple of 36 pt from the margin + const plain = await PdfDocument.load(await pdfOf(makeDocx({ body: `AB${pg}` }))) + const [, b] = (await pageChars(plain, 0)).chars.filter((c) => c.text.trim()) + expect(b.x0).toBeCloseTo(72 + 36, 0) + // a centre stop puts the middle of what follows on it + const mid = await PdfDocument.load(await pdfOf(makeDocx({ body: `MID${pg}` }))) + const m = (await pageChars(mid, 0)).chars.filter((c) => c.text.trim()) + expect((m[0].x0 + m[2].x1) / 2).toBeCloseTo(72 + 234, 0) + }) + + test('odd and even pages can have their own headers; the number of the first page and how numbers are written can be set', async () => { + const pages = (body: string) => body + para('Two', '') + para('Three', '') + const pdf = await pdfOf( + makeDocx({ + rels: [...settingsRel, ['h', 'header', 'h.xml'], ['he', 'header', 'he.xml'], ['f', 'footer', 'f.xml']], + parts: { + 'word/settings.xml': settings(''), + 'word/h.xml': part('hdr', para('ODDHEAD')), + 'word/he.xml': part('hdr', para('EVENHEAD')), + 'word/f.xml': part('ftr', 'No 1'), + }, + body: pages(para('One')) + `${pg}`, + }), + ) + // numbering starts at 4 (iv), so the first page is an even one + expect(await pageText(pdf, 0)).toContain('EVENHEAD') + expect(await pageText(pdf, 1)).toContain('ODDHEAD') + expect(await pageText(pdf, 2)).toContain('EVENHEAD') + // only the usual footer is defined, so even pages (iv, vi) show none and the odd one shows its number in roman, as the section says + const labels = await Promise.all([0, 1, 2].map(async (i) => (await pageText(pdf, i)).match(/No (\w+)/)?.[1])) + expect(labels).toEqual([undefined, 'v', undefined]) + }) + + test('a table is on the left (its text on the margin in older documents, its edge in Word 2013 mode), in the middle or on the right', async () => { + const table = (props: string) => `${props}${para('X')}` + const xOf = async (props: string, compat = false) => { + const docx = await makeDocx({ rels: settingsRel, parts: { 'word/settings.xml': settings(compat ? '' : '') }, body: table(props) + `${pg}` }) + return (await pageChars(await PdfDocument.load(await pdfOf(Promise.resolve(docx))), 0)).chars.find((c) => c.text === 'X')!.x0 + } + expect(await xOf('')).toBeCloseTo(72, 0) // text on the margin: the edge is 5.4 pt outside it + expect(await xOf('', true)).toBeCloseTo(72 + 5.4, 0) + expect(await xOf('')).toBeCloseTo(72 + (468 - 200) / 2 + 5.4, 0) + expect(await xOf('')).toBeCloseTo(72 + 468 - 200 + 5.4, 0) + expect(await xOf('', true)).toBeCloseTo(72 + 36 + 5.4, 0) + }) + + test('a table that goes on to the next page shows its heading rows again at the top of it', async () => { + const row = (t: string, header = false) => `${header ? '' : ''}${t}` + const rows = row('HEADROW', true) + Array.from({ length: 70 }, (_, i) => row(`R${i + 1}x`)).join('') + const pdf = await pdfOf(makeDocx({ body: `${rows}${pg}` })) + expect(await pageText(pdf, 0)).toMatch(/^HEADROW R1x/) + expect(await pageText(pdf, 1)).toMatch(/^HEADROW R5\dx/) // page 1 holds 54 rows (the heading and 53 of the others), so page 2 goes on from the next + }) + + test('a footnote is at the bottom of the page its mark is on, which keeps the room it needs; endnotes come at the end', async () => { + const noted = (n: number, extra = '') => makeDocx({ rels: [['fn', 'footnotes', 'footnotes.xml'], ['en', 'endnotes', 'endnotes.xml']], parts: { 'word/footnotes.xml': notesPart('footnotes', [['1', 'FOOTONE'], ['2', 'FOOTTWO']]), 'word/endnotes.xml': notesPart('endnotes', [['1', 'ENDNOTE']]) }, body: filler(n) + extra + `${pg}` }) + const mark = (id: string, kind: 'footnote' | 'endnote' = 'footnote') => `Marked${noteMark(kind, id)}` + // a mark early on the page: its note is on that page, below the text, with a rule above it + const early = await pdfOf(noted(3, mark('1'))) + const { chars } = await pageChars(await PdfDocument.load(early), 0) + const note = chars.filter((c) => c.text.trim()).map((c) => c.text).join('') + expect(note).toContain('FOOTONE') + const o = chars.find((c) => c.text === 'O' && note.indexOf('FOOTONE') >= 0)! + expect(o.y0).toBeGreaterThan(792 - 72 - 40) // at the foot of the page, not under the mark + // 52 lines fill all but two lines of the page: a line with a note that takes more than that goes to the next page, with its note + const late = await pdfOf(noted(52, mark('1'))) + expect(await pageText(late, 0)).not.toContain('FOOTONE') + expect(await pageText(late, 1)).toContain('Marked') + expect(await pageText(late, 1)).toContain('FOOTONE') + // two marks, two notes in order, numbered 1 and 2 + const two = await pdfOf(noted(3, mark('1') + mark('2'))) + expect(await pageText(two, 0)).toMatch(/1 FOOTONE 2 FOOTTWO/) + // endnotes after the last text + const end = await pdfOf(noted(3, mark('1', 'endnote'))) + expect(await pageText(end, 0)).toMatch(/Markedi.*i ENDNOTE/) + }) + + test('links to a bookmark jump to where it is, and a page reference shows the page it is really on (not the one the file saved)', async () => { + const entry = (target: string, saved: string) => + `Chapter PAGEREF ${target} \\h ${saved}` + const target = (name: string) => `The chapter` + const pdf = await pdfOf(makeDocx({ body: entry('chap', '9') + entry('nowhere', '7') + target('chap') + `${pg}` })) + const doc = await PdfDocument.load(pdf) + // the number is the page the bookmark is on (2), and the one with no bookmark keeps what the file saved + expect(await pageText(pdf, 0)).toMatch(/Chapter\.+2 Chapter\.+7/) + // one link, to the second page, and none for the bookmark that isn't there + const annots = (doc.resolve(doc.pages()[0].dict.get('Annots')) as unknown as PdfObj[]).map((r) => doc.resolve(r) as Map) + expect(annots.filter((a) => a.get('Dest'))).toHaveLength(2) // the words and the page number of the one entry + for (const a of annots) expect(doc.resolve(((doc.resolve(a.get('Dest')) as unknown as PdfObj[])[0]))).toBe(doc.pages()[1].dict) + }) + + const sect = (inner: string) => `${inner}${pg}` + const cols2 = '' + + test('text in columns fills the first column, then the second, then goes to the next page', async () => { + const doc = await PdfDocument.load(await pdfOf(makeDocx({ body: lines('L', 130) + `${cols2}${pg}` }))) + expect(doc.pages().length).toBe(2) // 54 lines to a column: 108 on the first page, the other 22 on the second + const chars = (await pageChars(doc, 0)).chars.filter((c) => c.text.trim()) + const at = (label: string, from = chars) => from.find((c, i) => from.slice(i, i + label.length).map((x) => x.text).join('') === label)! + // columns 216 pt wide, 36 apart: the second starts at 72 + 216 + 36 = 324, at the same height as the first + expect(at('L1').x0).toBeCloseTo(72, 0) + expect(at('L55').x0).toBeCloseTo(324, 0) + expect(at('L55').y0).toBeCloseTo(at('L1').y0, 0) + expect(Math.max(...chars.filter((c) => c.x0 < 300).map((c) => c.x1))).toBeLessThanOrEqual(288.5) + const next = (await pageChars(doc, 1)).chars.filter((c) => c.text.trim()) + expect(at('L109', next).x0).toBeCloseTo(72, 0) // the next page starts again in the first column + }) + + test('columns before a continuous break are balanced, and what follows starts below them, back at the full width', async () => { + const body = para('TITLE') + sect('') + lines('L', 40) + sect(`${cols2}`) + para('AFTER') + `${pg}` + const doc = await PdfDocument.load(await pdfOf(makeDocx({ body }))) + expect(doc.pages()).toHaveLength(1) + const chars = (await pageChars(doc, 0)).chars.filter((c) => c.text.trim()) + const at = (label: string) => chars.find((c, i) => chars.slice(i, i + label.length).map((x) => x.text).join('') === label)! + // 40 lines would all fit in the first column; balanced, about half are in each (the empty paragraph that carries the section break + // is the last line of the second, so the first has 21) + expect(at('L1').x0).toBeCloseTo(72, 0) + expect(at('L20').x0).toBeCloseTo(72, 0) + expect(at('L22').x0).toBeCloseTo(324, 0) + expect(at('L22').y0).toBeCloseTo(at('L1').y0, 0) + // the second part starts under the tallest column, at the left margin + expect(at('AFTER').x0).toBeCloseTo(72, 0) + expect(at('AFTER').y0).toBeGreaterThan(at('L20').y0) + expect(at('AFTER').y0).toBeLessThan(at('L20').y0 + 40) + }) + + test('a section that must start on an odd page leaves a blank page when it would start on an even one', async () => { + const pdf = await pdfOf(makeDocx({ body: para('FIRST') + sect('') + para('SECOND') + `${pg}` })) + const doc = await PdfDocument.load(pdf) + expect(doc.pages()).toHaveLength(3) + expect(await pageText(pdf, 0)).toContain('FIRST') + expect(await pageText(pdf, 1)).toBe('') + expect(await pageText(pdf, 2)).toContain('SECOND') + }) + + test('a table row with more text than fits goes on over the page break, between lines, with its borders; a row that may not split moves whole', async () => { + const cell = (n: number, lineProps = '') => `${lines('C', n, lineProps)}` + const table = (rowProps: string, n: number) => `${rowProps ? `${rowProps}` : ''}${cell(n)}${pg}` + // one cell of 130 lines: 54 on the first page, 54 on the second, the rest on the third: nothing is lost + const long = await pdfOf(makeDocx({ body: table('', 130) })) + const doc = await PdfDocument.load(long) + expect(doc.pages()).toHaveLength(3) + expect(await pageText(long, 0)).toMatch(/^C1 C2 /) + expect(await pageText(long, 1)).toMatch(/^C55 C56 /) + expect(await pageText(long, 2)).toMatch(/C130$/) + // after some text a row of 60 lines doesn't fit in what is left: it starts right there and goes on, where "can't split" moves it to the next page + const some = filler(40) + const split = await pdfOf(makeDocx({ body: some + table('', 60) })) + expect(await pageText(split, 0)).toContain('C1 C2') + expect(await pageText(split, 1)).toMatch(/^C\d+ /) + const whole = await pdfOf(makeDocx({ body: some + table('', 60) })) + expect(await pageText(whole, 0)).not.toContain('C1 C2') + expect(await pageText(whole, 1)).toMatch(/^C1 C2 /) + }) + test('run: the file and its errors', async () => { const f = (name: string, bytes: Uint8Array) => ({ name, type: '', bytes }) const docx = await makeDocx({ body: para('Hello') + `${pg}` }) diff --git a/packages/tools/src/powerpoint-to-pdf/text.ts b/packages/tools/src/powerpoint-to-pdf/text.ts index e3b235c..6273626 100644 --- a/packages/tools/src/powerpoint-to-pdf/text.ts +++ b/packages/tools/src/powerpoint-to-pdf/text.ts @@ -10,7 +10,7 @@ import { hexRgb } from '../office-fonts.ts' const EM_HEIGHT = 1.2 /** `picture`: how tall a picture in the line is (its width is `width`); the run holds the picture itself */ -type Piece = { text: string; run: SlideRun; font: FontSpec; size: number; rise: number; width: number; space: boolean; picture?: number } +type Piece = { text: string; run: SlideRun; font: FontSpec; size: number; rise: number; width: number; space: boolean; picture?: number; /** a tab: the character that fills its width (dots, dashes), if any */ tab?: { leader?: string } } /** `gap`: room left empty above the line (Word moves a line down past a picture it can't sit beside); `base`: where the baseline is below the top of the line, when a picture in it decides that */ type Line = { pieces: Piece[]; width: number; size: number; height: number; start: number; avail: number; last: boolean; gap?: number; base?: number } /** where a line may go: `dx` from the left of the text area, `w` wide; `skip` says there's no room at this height and the line goes that much lower */ @@ -71,8 +71,9 @@ export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lin // raised and lowered text is drawn smaller, the way PowerPoint does const size = run.baseline ? base * 0.67 : run.caps === 'small' ? base * 0.8 : base const rise = run.baseline ? run.baseline * base : 0 - for (const t of shown(run).match(/\n|[^\S\n]+|[^\s]+/g) ?? []) { + for (const t of shown(run).match(/\n|\t|[^\S\n\t]+|[^\s]+/g) ?? []) { if (t === '\n') tokens.push('break') + else if (t === '\t') tokens.push({ text: '', run, font, size: base, rise: 0, width: 0, space: true, tab: {} }) else tokens.push({ text: t, run, font, size, rise, width: measure(t, size, font) + (run.spacing ?? 0) * [...t].length, space: /^\s/.test(t) }) } } @@ -113,10 +114,38 @@ export function layoutText(paras: SlidePara[], width: number, wrap: boolean, lin w = 0 open() } - for (const tk of tokens) { + const stops = [...(para.tabStops ?? [])].sort((a, b) => a.pos - b.pos) + const every = Math.max(1, para.defaultTab ?? 36) + for (let ti = 0; ti < tokens.length; ti++) { + const tk = tokens[ti] if (tk === 'break') { finish(true); continue } const first = lines.length === 0 const avail = box.w - (first ? start0 : marL) + if (tk.tab) { + // a tab goes to the next stop to the right of where the text is; text after a right, centre or decimal stop is placed against it + const at = box.dx + (first ? start0 : marL) + w + const stop = stops.find((s) => s.pos > at + 0.01) + const next = stop ? stop.pos : Math.ceil((at + 0.01) / every) * every + let target = next + if (stop && stop.align !== 'left') { + let after = 0 + let dot = -1 + for (let k = ti + 1; k < tokens.length; k++) { + const t = tokens[k] + if (t === 'break' || t.tab) break + if (stop.align === 'decimal' && dot < 0 && t.text.includes('.')) dot = after + measure(t.text.slice(0, t.text.indexOf('.')), t.size, t.font) + after += t.width + } + target = stop.align === 'right' ? next - after : stop.align === 'center' ? next - after / 2 : dot >= 0 ? next - dot : next - after + } + const room = Math.max(0, box.dx + (first ? 0 : 0) + box.w - at) + tk.width = Math.max(0, Math.min(target - at, room)) + const leader = stop?.leader === 'dot' ? '.' : stop?.leader === 'hyphen' ? '-' : stop?.leader === 'underscore' ? '_' : undefined + tk.tab = { leader } + cur.push(tk) + w += tk.width + continue + } if (wrap && !tk.space && cur.some((p) => !p.space) && w + tk.width > avail) finish(false) if (!(tk.space && !cur.length)) { cur.push(tk); w += tk.width } } @@ -164,7 +193,7 @@ export function drawText(page: PageDraw, laid: Laid[], area: { x: number; y: num if (align === 'center') x = area.x + ln.start + (ln.avail - ln.width) / 2 else if (align === 'right') x = area.x + ln.start + ln.avail - ln.width // justified lines (not the last of a paragraph) spread the leftover room over their spaces - const spaces = ln.pieces.filter((p) => p.space).length + const spaces = ln.pieces.filter((p) => p.space && !p.tab).length const extra = align === 'justify' && !ln.last && spaces ? Math.max(0, ln.avail - ln.width) / spaces : 0 if (li === 0 && l.bullet) { // the bullet keeps its distance from the text, wherever alignment puts the text @@ -175,12 +204,23 @@ export function drawText(page: PageDraw, laid: Laid[], area: { x: number; y: num } // pieces of one run go out as one string, spaces included, so the text can be searched and copied from the PDF for (let i = 0; i < ln.pieces.length; ) { + const t = ln.pieces[i] + if (t.tab) { + // a tab is room; with a leader it is filled with dots or dashes, ending where the next text starts + const lead = t.tab.leader + const cw = lead ? measure(lead, t.size, t.font) : 0 + const n = lead && cw > 0 ? Math.floor(t.width / cw) : 0 + if (n > 0) page.text(x + t.width - n * cw, baseline, lead!.repeat(n), t.font, t.size, hexRgb(t.run.color)) + x += t.width + i++ + continue + } let j = i let text = '' let width = 0 - while (j < ln.pieces.length && ln.pieces[j].run === ln.pieces[i].run && !(extra && ln.pieces[j].space && j > i)) { + while (j < ln.pieces.length && ln.pieces[j].run === ln.pieces[i].run && !ln.pieces[j].tab && !(extra && ln.pieces[j].space && j > i)) { text += ln.pieces[j].text - width += ln.pieces[j].width + (extra && ln.pieces[j].space ? extra : 0) + width += ln.pieces[j].width + (extra && ln.pieces[j].space && !ln.pieces[j].tab ? extra : 0) j++ } const p = ln.pieces[i] diff --git a/packages/tools/src/seo/convert.ts b/packages/tools/src/seo/convert.ts index 7726ac4..5ae801f 100644 --- a/packages/tools/src/seo/convert.ts +++ b/packages/tools/src/seo/convert.ts @@ -54,12 +54,12 @@ export const convertSeo: Record = { 'word-to-pdf': { title: 'Convert Word (DOCX) to PDF online | Ream', description: 'Save a Word document as a PDF with its headers, footers, page numbers, links and pictures. Runs in your browser; nothing is uploaded.', - intro: 'Turn a .docx into a PDF to share or print, without opening Word. Ream reads the document and lays out its pages itself, with headers and footers, page numbers, tables, pictures and text boxes, clickable links and a bookmark for every heading. A preview shows the result before you download.', - points: ['Headers, footers and page numbers on every page', 'Clickable links and bookmarks from your headings', 'Fonts: your installed ones, look-alikes, or ones you add'], + intro: 'Turn a .docx into a PDF to share or print, without opening Word. Ream reads the document and lays out its pages itself, with headers and footers, page numbers, footnotes, columns, styled tables, pictures and text boxes, clickable links and a bookmark for every heading. A preview shows the result before you download.', + points: ['Headers, footers, footnotes and page numbers', 'Columns, styled tables and a table of contents with real page numbers', 'Clickable links and bookmarks from your headings'], faq: [ ['Will it look exactly like it does in Word?', 'Close, not identical. Ream lays the pages out itself and uses look-alike fonts when yours are not available, so a line or a page break can fall in a different place. Documents set in Aptos are the usual case, since it has no free look-alike. The preview is marked approximate for that reason, and you can add the fonts the document uses to get closer.'], ['Does it work with older .doc files?', 'Only .docx files. In Word, choose Save As and pick Word Document (.docx) first. A document with a password has to be saved without it.'], - ['What is not carried over?', 'Footnotes, columns, table styles, a different header on even pages, shapes other than pictures and text boxes, and comments are not drawn. A table of contents keeps the page numbers saved in the file.'], + ['What is not carried over?', 'Shapes other than pictures and text boxes, equations, comments and tracked changes are not drawn, text wraps around a picture by its box and not its outline, and right-to-left or complex scripts such as Arabic, Hebrew and Hindi are not shaped. Footnotes, columns, table styles, tabs, bookmarks and the page numbers of a table of contents are kept.'], ], }, 'excel-to-pdf': { diff --git a/packages/tools/src/word-to-pdf/Panel.tsx b/packages/tools/src/word-to-pdf/Panel.tsx index fe9b1fd..ae3bafb 100644 --- a/packages/tools/src/word-to-pdf/Panel.tsx +++ b/packages/tools/src/word-to-pdf/Panel.tsx @@ -8,10 +8,10 @@ export function Panel({ options: o, onChange, files, readOfficeFonts, readFontFi return (
Pages -

Each section keeps its own paper size and margins, with its headers, footers and page numbers. Links in the text stay clickable and headings become bookmarks.

+

Each section keeps its own paper size, margins and columns, with its headers, footers, footnotes and page numbers. Links in the text stay clickable (a table of contents jumps to its pages and shows their real numbers) and headings become bookmarks.

onChange({ ...o, localFonts })} />

- Fonts are embedded: fonts stored in the document are used as they are (Google Docs saves the ones it uses), then the file's own fonts when they are on this computer, added or downloaded; otherwise Calibri, Cambria, Arial, Times New Roman and Courier New become look-alikes with the same letter widths, and other fonts the closest of those, so lines and page breaks can fall a little differently than in Word (a document set in Aptos is the usual case). Letters none of them have (such as Chinese or Japanese) show as ?. Only .docx files open here, not older .doc files. Footnotes, columns, table styles, a different header on even pages, shapes other than pictures and text boxes, and comments are not drawn, and a table of contents shows the page numbers saved in the file. + Fonts are embedded: fonts stored in the document are used as they are (Google Docs saves the ones it uses), then the file's own fonts when they are on this computer, added or downloaded; otherwise Calibri, Cambria, Arial, Times New Roman and Courier New become look-alikes with the same letter widths, and other fonts the closest of those, so lines and page breaks can fall a little differently than in Word (a document set in Aptos is the usual case). Letters none of them have (such as Chinese or Japanese) show as ?. Only .docx files open here, not older .doc files. Shapes other than pictures and text boxes, equations, comments and tracked changes are not drawn, text wraps around a picture by its box and not its outline, and right-to-left and complex scripts (Arabic, Hebrew, Hindi) are not shaped.

) diff --git a/packages/tools/src/word-to-pdf/layout.ts b/packages/tools/src/word-to-pdf/layout.ts index 7018a62..44b4fd4 100644 --- a/packages/tools/src/word-to-pdf/layout.ts +++ b/packages/tools/src/word-to-pdf/layout.ts @@ -2,21 +2,22 @@ // lines (never leaving one line alone at the top or bottom of a page, keeping lines and headings with what follows when // the document says so), tables break between rows, pictures sit in the text or where they are placed, text boxes where // they are. Headers and footers repeat on every page with their page numbers, links are clickable and headings are -// bookmarks. It uses the slide text engine for lines. Approximate by design: no columns, footnotes or table styles. +// bookmarks. It uses the slide text engine for lines. import { PageDraw, PdfDrawing, type OutlineItem } from '@ream/pdf-core' -import type { DocModel, DocSection, DocBlock, DocTable as Table, Paragraph, Picture, SlidePara } from '@ream/office-core' +import { numberText, type DocModel, type DocSection, type DocBlock, type DocTable as Table, type Paragraph, type Picture, type SlidePara } from '@ream/office-core' import type { FontBook, FontNeed } from '../fonts/book.ts' import { hexRgb } from '../office-fonts.ts' -import { drawText, layoutText, type Laid, type RunBox, type Slot } from '../powerpoint-to-pdf/text.ts' +import { drawText, layoutText, textHeight, type Laid, type RunBox, type Slot } from '../powerpoint-to-pdf/text.ts' export type ImageFor = (p: Picture) => number | null /** the slide engine's paragraph for a Word paragraph; a picture in a line is kept within `maxHeight` (a page's worth) */ -export function slidePara(p: Paragraph, maxHeight = Infinity): SlidePara { +export function slidePara(p: Paragraph, maxHeight = Infinity, defaultTab?: number, refs?: Map): SlidePara { const fit = (pic: Picture): Picture => (pic.height > maxHeight ? { ...pic, width: (pic.width * maxHeight) / pic.height, height: maxHeight } : pic) return { - // a tab in running text is a gap (header and footer tabs are placed by `Flow.tabbed`) - runs: p.runs.map((r) => ({ ...r, text: r.text.includes('\t') ? r.text.replace(/\t/g, ' ') : r.text, picture: r.picture && fit(r.picture), baseline: r.vertAlign === 'super' ? 0.3 : r.vertAlign === 'sub' ? -0.25 : undefined })), + runs: p.runs.map((r) => ({ ...r, text: r.field === 'pageref' && r.ref && refs?.has(r.ref) ? refs.get(r.ref)! : r.text, picture: r.picture && fit(r.picture), baseline: r.vertAlign === 'super' ? 0.3 : r.vertAlign === 'sub' ? -0.25 : undefined })), + tabStops: p.tabStops, + defaultTab, align: p.align, bullet: p.marker, marginLeft: p.indentLeft, @@ -75,19 +76,39 @@ const MIN_BESIDE = 36 /** the room a placed picture takes on its page, with the clearance the document asks for, and how text goes around it */ type Float = { x0: number; y0: number; x1: number; y1: number; kind: 'square' | 'topBottom'; side: 'both' | 'left' | 'right' | 'largest' } -/** a page of the drawing and what decides its header, footer and body */ -type PageInfo = { page: PageDraw; s: DocSection; first: boolean } +/** a page of the drawing and what decides its header, footer and body; `number` is its page number and `label` how that is written */ +type PageInfo = { page: PageDraw; s: DocSection; first: boolean; number: number; label: string } + +/** what every part of the layout shares */ +type Ctx = { d: PdfDrawing; book: FontBook; imageFor: ImageFor; pages: PageInfo[]; marks: Mark[]; doc: DocModel; /** a note's lines at a width, kept */ noteLaid: Map + /** where each bookmark is (page from 0, and how far down), what the links to bookmarks are waiting for, the page label of each bookmark as the last pass found it, and the page each PageDraw is */ + anchors: Map + links: { page: number; x: number; y: number; w: number; h: number; name: string }[] + refs: Map + index: Map +} + +/** room above the notes of a page for the short rule that sets them off */ +const NOTE_RULE = 10 /** a heading as laid out: its level, text and where it is */ type Mark = { level: number; title: string; page: number; y: number } -/** the blocks with the page number and the number of pages put in where the document has those fields */ -function numbered(blocks: DocBlock[], n: number, total: number): DocBlock[] { - const para = (p: Paragraph): Paragraph => (p.runs.some((r) => r.field) ? { ...p, runs: p.runs.map((r) => (r.field ? { ...r, text: String(r.field === 'page' ? n : total) } : r)) } : p) +/** the blocks with the page number (as it is written on the page) and the number of pages put in where the document has those fields */ +function numbered(blocks: DocBlock[], label: string, total: number): DocBlock[] { + const para = (p: Paragraph): Paragraph => (p.runs.some((r) => r.field) ? { ...p, runs: p.runs.map((r) => (r.field === 'page' ? { ...r, text: label } : r.field === 'pages' ? { ...r, text: String(total) } : r)) } : p) return blocks.map((b) => (b.kind === 'p' ? para(b) : b.kind === 'table' ? { ...b, rows: b.rows.map((row) => row.map((c) => ({ ...c, paragraphs: c.paragraphs.map(para) }))) } : b)) } -/** the header or footer a page of a section shows */ -const bandOf = (s: DocSection, first: boolean, which: 'header' | 'footer'): DocBlock[] => (s.titlePg && first ? (which === 'header' ? s.firstHeader : s.firstFooter) : which === 'header' ? s.header : s.footer) ?? [] +/** the header or footer a page shows: the first page's when the section has one, an even page's when the document has them, else the usual one */ +function bandOf(s: DocSection, info: { first: boolean; number: number }, which: 'header' | 'footer', evenAndOdd: boolean | undefined): DocBlock[] { + const head = which === 'header' + if (s.titlePg && info.first) return (head ? s.firstHeader : s.firstFooter) ?? [] + if (evenAndOdd && info.number % 2 === 0) return (head ? s.evenHeader : s.evenFooter) ?? [] + return (head ? s.header : s.footer) ?? [] +} + +/** the footnotes whose marks are in a line of laid out text */ +const refsOf = (line: Laid['lines'][number]): string[] => line.pieces.flatMap((p) => (p.run.note?.kind === 'foot' ? [p.run.note.id] : [])) class Flow { d: PdfDrawing @@ -101,36 +122,237 @@ class Flow { imageFor: ImageFor /** a header or footer: it never breaks to a new page, and its tabs are placed */ band: boolean + ctx: Ctx pages: PageInfo[] marks: Mark[] /** pictures on this page that text goes around (a new page has none; a header's only move the header's text) */ floats: Float[] = [] - private started = false - constructor(d: PdfDrawing, s: DocSection, book: FontBook, imageFor: ImageFor, pages: PageInfo[], marks: Mark[], band = false) { - this.d = d + /** the footnotes whose marks are on this page, in order, the room they take at its bottom (with the rule), and where the body would end without them */ + notes: { ids: string[]; total: number }[] = [{ ids: [], total: 0 }] + baseBottom = 0 + /** the lowest the body may go because a section's columns are being balanced (the room the notes of a column take comes off the page's bottom) */ + cap = Infinity + get noteTotal() { return this.notes[this.colIndex]?.total ?? 0 } + /** the body ends above the footnotes of the column being filled, and above the cap */ + private setBottom() { + this.bottom = Math.min(this.baseBottom - this.noteTotal, this.cap) + } + /** the columns text runs in on this page (one, usually): where each starts and how wide; the one being filled; how far down any of them got; and whether the section's own first page is still to be made */ + cols: { x: number; w: number }[] = [] + colIndex = 0 + maxY = 0 + private fresh = true + constructor(ctx: Ctx, s: DocSection, band = false) { + this.ctx = ctx + this.d = ctx.d this.s = s - this.book = book - this.imageFor = imageFor - this.pages = pages - this.marks = marks + this.book = ctx.book + this.imageFor = ctx.imageFor + this.pages = ctx.pages + this.marks = ctx.marks this.band = band this.top = s.margins.top this.bottom = band ? Infinity : s.height - s.margins.bottom + this.layoutCols() if (!band) this.newPage() } - get left() { return this.s.margins.left } - get width() { return this.s.width - this.s.margins.left - this.s.margins.right } + get left() { return this.band ? this.s.margins.left : this.cols[this.colIndex].x } + get width() { return this.band ? this.s.width - this.s.margins.left - this.s.margins.right : this.cols[this.colIndex].w } + + /** where the columns of the section are: one the width of the text, or what the section sets */ + private layoutCols() { + const m = this.s.margins + const text = this.s.width - m.left - m.right + const c = this.s.columns + if (!c || c.widths.length < 2) this.cols = [{ x: m.left, w: text }] + else { + let x = m.left + this.cols = c.widths.map((w, i) => { + const col = { x, w } + x += w + (c.space[i] ?? 0) + return col + }) + } + this.colIndex = 0 + } + + /** the rules between the columns, for the part of the page they were used on */ + private colRules() { + const c = this.s.columns + if (this.band || !c?.line || this.cols.length < 2 || !this.page) return + const bottom = Math.max(this.maxY, this.y) + if (bottom <= this.top + 1) return + for (let i = 0; i < this.cols.length - 1; i++) { + const x = this.cols[i].x + this.cols[i].w + (c.space[i] ?? 0) / 2 + this.page.line(x, this.top, x, bottom, 0.5, [0, 0, 0]) + } + } + + /** the section is done: the next one starts below the tallest of its columns */ + endSection() { + if (this.band) return + this.maxY = Math.max(this.maxY, this.y) + if (this.cols.length > 1) this.y = this.maxY + this.colRules() + this.colIndex = 0 + this.maxY = this.y + // the cap that balanced the columns ends with the section + this.cap = Infinity + this.setBottom() + } + + /** + * A section starts: on the next page (after a blank one if it must start on an odd or even page), or, if it is continuous, + * right here, with its own columns from the current height. + */ + beginSection(s: DocSection) { + this.s = s + const was = this.notes + this.layoutCols() + if (s.type === 'continuous') { + // the notes already on the page stay, by column; the ones of columns that are no more join the last + this.notes = this.cols.map((_, i) => was[i] ?? { ids: [], total: 0 }) + for (const extra of was.slice(this.cols.length)) { + const last = this.notes[this.notes.length - 1] + last.ids.push(...extra.ids) + last.total += extra.total + } + this.top = this.y + this.maxY = this.y + this.floats = [] + this.setBottom() + return + } + const next = s.pageStart ?? (this.pages[this.pages.length - 1]?.number ?? 0) + 1 + // a blank page first (numbered as the next page, not as the restart) when the section must start on an odd or even page and this one isn't + if ((s.type === 'oddPage' && next % 2 === 0) || (s.type === 'evenPage' && next % 2 === 1)) { + this.fresh = false + this.newPage() + } + this.fresh = true + this.newPage() + } + + /** the column is full: the next one on the page, or the next page */ + advance() { + this.maxY = Math.max(this.maxY, this.y) + if (!this.band && this.colIndex + 1 < this.cols.length) { + this.colIndex++ + this.y = this.top + this.setBottom() + } else this.newPage() + } + + /** a column no taller than this, from here: what balancing the columns of a section does */ + capBottom(height: number) { + this.cap = this.y + height + this.setBottom() + } newPage() { if (this.band) return - const first = !this.started - this.started = true + this.maxY = Math.max(this.maxY, this.y) + this.colRules() + this.flushNotes() + const first = this.fresh + this.fresh = false this.page = this.d.addPage(this.s.width, this.s.height) - this.pages.push({ page: this.page, s: this.s, first }) - const [header, footer] = [bandHeight(this, bandOf(this.s, first, 'header')), bandHeight(this, bandOf(this.s, first, 'footer'))] + // numbering goes on from the page before, unless the section starts it again + const was = this.pages[this.pages.length - 1] + const number = first && this.s.pageStart !== undefined ? this.s.pageStart : was ? was.number + 1 : 1 + const info: PageInfo = { page: this.page, s: this.s, first, number, label: numberText(number, this.s.pageFormat ?? 'decimal') } + this.pages.push(info) + this.ctx.index.set(this.page, this.d.pages.length - 1) + const even = this.ctx.doc.evenAndOdd + const [header, footer] = [bandHeight(this, bandOf(this.s, info, 'header', even)), bandHeight(this, bandOf(this.s, info, 'footer', even))] this.top = Math.max(this.s.margins.top, header ? (this.s.margins.header ?? 36) + header : 0) this.bottom = Math.min(this.s.height - this.s.margins.bottom, footer ? this.s.height - (this.s.margins.footer ?? 36) - footer : Infinity) + this.baseBottom = this.bottom + this.cap = Infinity this.y = this.top + this.maxY = this.top this.floats = [] + this.layoutCols() + this.notes = this.cols.map(() => ({ ids: [], total: 0 })) + } + + /** the text engine's paragraph for one of ours, with what the document says about tabs and what the last pass found for page references */ + sp(p: Paragraph, maxHeight = Infinity): SlidePara { + return slidePara(p, maxHeight, this.ctx.doc.defaultTab, this.ctx.refs) + } + + get fullWidth() { return this.s.width - this.s.margins.left - this.s.margins.right } + + /** a note's text laid out across the page */ + private noteLines(id: string): Laid[] { + const key = `${id}|${this.width}` + let laid = this.ctx.noteLaid.get(key) + if (!laid) { + const paras = this.ctx.doc.notes?.[`foot:${id}`] ?? [] + laid = layoutText(paras.map((q) => this.sp(q)), Math.max(10, this.width), true, 0, this.book) + this.ctx.noteLaid.set(key, laid) + } + return laid + } + + /** the room the notes of these marks take at the bottom of the page that the ones already there and `claimed` don't (with the rule, if they are the first) */ + private noteCost(ids: string[], claimed: Set): number { + const cap = Math.max(20, (this.baseBottom - this.top) * 0.6) + let cost = 0 + let fresh = 0 + for (const id of new Set(ids)) { + if (this.notes[this.colIndex].ids.includes(id) || claimed.has(id) || !this.ctx.doc.notes?.[`foot:${id}`]) continue + cost += Math.min(textHeight(this.noteLines(id)), cap) + fresh++ + } + return fresh && !this.notes[this.colIndex].ids.length && !claimed.size ? cost + NOTE_RULE : cost + } + + /** the notes of these marks are on this page now: the body ends above them */ + private takeNotes(ids: string[]) { + if (this.band) return + const cost = this.noteCost(ids, new Set()) + const mine = this.notes[this.colIndex] + for (const id of new Set(ids)) if (!mine.ids.includes(id) && this.ctx.doc.notes?.[`foot:${id}`]) mine.ids.push(id) + mine.total += cost + this.setBottom() + } + + /** draw this page's notes at its bottom, each column's under it, below a short rule; called when the page is left */ + flushNotes() { + if (!this.page) return + const was = this.colIndex + this.notes.forEach((mine, ci) => { + if (!mine.ids.length) return + this.colIndex = ci // the width of the notes is the column's + const { x, w } = this.cols[ci] + let y = this.baseBottom - mine.total + 3 + this.page.line(x, y, x + Math.min(144, w / 3), y, 0.5, [0, 0, 0]) + y += NOTE_RULE - 3 + for (const id of mine.ids) { + const laid = this.noteLines(id) + const h = textHeight(laid) + this.draw(laid, { x, y, w, h }) + y += h + } + mine.ids = [] + mine.total = 0 + }) + this.colIndex = was + } + + /** the endnotes, one after the other under a rule, where the document ends */ + endnotes(ids: string[]) { + // they go across the page under the text, whatever columns the last section had + this.beginSection({ ...this.s, columns: undefined, type: 'continuous' }) + const left = this.s.margins.left + // the rule goes with the first line of the first note: where there's no room for both, they start the next page + const first = ids.map((id) => this.ctx.doc.notes?.[`end:${id}`]?.[0]).find(Boolean) + const need = 2 * NOTE_RULE + (first ? (this.lines(first).lines[0]?.height ?? 12) : 12) + if (!this.atTop() && this.y + need > this.bottom) this.advance() + if (!this.atTop()) this.y += NOTE_RULE + this.page.line(left, this.y, left + Math.min(144, this.fullWidth / 3), this.y, 0.5, [0, 0, 0]) + this.y += NOTE_RULE + for (const id of ids) for (const q of this.ctx.doc.notes?.[`end:${id}`] ?? []) this.paragraph(q) } atTop() { return this.y <= this.top + 0.5 } @@ -193,14 +415,17 @@ class Flow { /** what a run of a line needs drawn besides its text: a picture in the line (standing on the baseline), a link's box */ onRun = (page: PageDraw) => (b: RunBox) => { const pic = b.run.picture + const h = pic ? (b.width * pic.height) / pic.width : b.size * 1.05 + const y = pic ? b.baseline - h : b.baseline - b.size * 0.85 if (pic) { const id = this.imageFor(pic) - const h = (b.width * pic.height) / pic.width if (id !== null) drawPicture(page, id, pic, b.x, b.baseline - h, b.width, h) - if (b.run.link) page.link(b.x, b.baseline - h, b.width, h, b.run.link) - return } - if (b.run.link) page.link(b.x, b.baseline - b.size * 0.85, b.width, b.size * 1.05, b.run.link) + if (b.run.link) page.link(b.x, y, b.width, h, b.run.link) + else if (b.run.anchor) { + const at = this.ctx.index.get(page) + if (at !== undefined) this.ctx.links.push({ page: at, x: b.x, y, w: b.width, h, name: b.run.anchor }) + } } private draw(laid: Laid[], area: { x: number; y: number; w: number; h: number }) { drawText(this.page, laid, area, 'top', this.onRun(this.page)) @@ -213,7 +438,7 @@ class Flow { const full = { dx: 0, w: this.width - right } // a line that would not fit starts the next page, where this page's pictures are not const slot = this.floats.length ? (offset: number, h: number) => (this.y + offset + h > this.bottom + 0.01 ? full : this.box(this.y + offset, h, right)) : undefined - return layoutText([{ ...slidePara(p, this.bottom - this.top), before: 0, after: 0 }], Math.max(10, this.width - right), true, 0, this.book, slot)[0] + return layoutText([{ ...this.sp(p, this.bottom - this.top), before: 0, after: 0 }], Math.max(10, this.width - right), true, 0, this.book, slot)[0] } /** @@ -222,7 +447,8 @@ class Flow { * the ones in front after it. */ paragraph(p: Paragraph, placed: Picture[] = []) { - if (this.band && p.tabs?.length && this.tabbed(p)) return + // an alignment tab (relative to the margins, not a stop) is placed by `tabbed`; the usual tabs by the text engine + if (this.band && p.tabs?.some((t) => t && t.pos === undefined) && this.tabbed(p)) return if (p.pageBreakBefore && !this.atTop()) this.newPage() if (this.band || !this.atTop()) this.y += p.before ?? 0 // a picture placed "from the paragraph" counts from the top of its text, below the space above it (the template this was @@ -251,11 +477,22 @@ class Flow { const room = this.bottom - this.y let n = 0 let h = 0 - while (i + n < all.length && h + heightOf(all[i + n]) <= room + 0.01) h += heightOf(all[i + n++]) + // a line with a footnote mark also needs room for the note at the bottom of the page + let extra = 0 + const claimed = new Set() + while (i + n < all.length) { + const marks = refsOf(all[i + n]) + const add = this.noteCost(marks, claimed) + if (h + heightOf(all[i + n]) + extra + add > room + 0.01) break + h += heightOf(all[i + n]) + extra += add + for (const id of marks) claimed.add(id) + n++ + } const left = all.length - i // starting on a new page: the paragraph is laid out again there, where the pictures of the old page are not const restart = () => { - this.newPage() + this.advance() anchor = this.y keep() laid = this.lines(p) @@ -269,7 +506,7 @@ class Flow { if (i === 0 && n === 1) n = 0 } if (n === 0) { - if (!this.atTop() && !moved) { moved = true; if (i === 0) restart(); else this.newPage(); continue } + if (!this.atTop() && !moved) { moved = true; if (i === 0) restart(); else this.advance(); continue } n = 1 // a line taller than the page still has to go somewhere } } @@ -281,6 +518,8 @@ class Flow { // the bookmark goes where the heading's first line is drawn, which is not always the page it was reached on if (k === 0 && p.heading && !this.band) this.marks.push({ level: p.heading, title: p.runs.map((r) => r.text).join('').replace(/\s+/g, ' ').trim(), page: this.d.pages.length - 1, y: this.y }) // a gap above a line is there for a picture; at the top of a page it has nothing to clear + this.takeNotes(refsOf(all[k])) + if (k === 0 && !this.band) for (const name of p.bookmarks ?? []) this.ctx.anchors.set(name, { page: this.d.pages.length - 1, y: this.y }) const ln = this.atTop() && all[k].gap ? { ...all[k], gap: 0 } : all[k] const one: Laid = { ...laid, lines: [ln], bullet: k === 0 ? laid.bullet : undefined, before: 0, after: 0 } this.draw([one], { x: this.left, y: this.y, w, h: ln.height }) @@ -288,7 +527,7 @@ class Flow { } moved = false i += n - if (i < all.length) this.newPage() + if (i < all.length) this.advance() } this.y += p.after ?? 0 for (const q of over) this.place(q, anchor) @@ -308,7 +547,7 @@ class Flow { else parts[parts.length - 1].push(r) } if (parts.length !== stops.length + 1) return false - const laid = parts.map((runs) => layoutText([{ ...slidePara({ ...p, runs, align: 'left', indentLeft: 0, indentFirst: 0, marker: undefined }), before: 0, after: 0 }], 1e6, false, 0, this.book)[0]) + const laid = parts.map((runs) => layoutText([{ ...this.sp({ ...p, runs, align: 'left', indentLeft: 0, indentFirst: 0, marker: undefined, tabStops: undefined }), before: 0, after: 0 }], 1e6, false, 0, this.book)[0]) const height = Math.max(...laid.map((l) => l.lines.reduce((a, ln) => a + ln.height, 0))) if (!this.atTop()) this.y += p.before ?? 0 laid.forEach((l, i) => { @@ -331,7 +570,7 @@ class Flow { const f = group[0].frame! const x = f.from?.h === 'margin' ? this.left + f.x : f.x const y = f.from?.v === 'margin' ? this.s.margins.top + f.y : f.from?.v === 'paragraph' ? anchorY + f.y : f.y - const laid = layoutText(group.map(slidePara), Math.max(10, f.w), true, 0, this.book) + const laid = layoutText(group.map((q) => this.sp(q)), Math.max(10, f.w), true, 0, this.book) this.draw(laid, { x, y, w: f.w, h: f.h ?? 0 }) } @@ -344,7 +583,7 @@ class Flow { if (p.at || p.align) return this.place(p, anchorY) const id = this.imageFor(p) const [w, h] = this.pictureSize(p) - if (this.y + h > this.bottom && !this.atTop()) this.newPage() + if (this.y + h > this.bottom && !this.atTop()) this.advance() if (id !== null) drawPicture(this.page, id, p, this.left, this.y, w, h) else this.page.fillRect(this.left, this.y, w, h, [235, 235, 235]) this.y += h @@ -353,7 +592,10 @@ class Flow { private columns(t: Table): number[] { const total = t.colWidths.reduce((a, b) => a + b, 0) const k = total > this.width ? this.width / total : 1 - const xs = [this.left] + // a table narrower than the text is on the left (moved by its indent), in the middle or on the right + const room = this.width - total * k + const start = this.left + (t.align === 'center' ? room / 2 : t.align === 'right' ? room : t.indent ?? 0) + const xs = [start] for (const w of t.colWidths) xs.push(xs[xs.length - 1] + w * k) return xs } @@ -368,35 +610,131 @@ class Flow { return box }) const pad = cells.map((b) => b.c.pad ?? CELL_PAD) - const laid = cells.map((b, i) => (b.c.vMerge === 'continue' ? [] : layoutText(b.c.paragraphs.map((p) => slidePara(p)), Math.max(4, b.x1 - b.x0 - pad[i].left - pad[i].right), true, 0, this.book))) + const laid = cells.map((b, i) => (b.c.vMerge === 'continue' ? [] : layoutText(b.c.paragraphs.map((p) => this.sp(p)), Math.max(4, b.x1 - b.x0 - pad[i].left - pad[i].right), true, 0, this.book))) const textH = laid.map((l) => l.reduce((h, p, i) => h + (i ? p.before : 0) + p.lines.reduce((a, x) => a + x.height, 0) + p.after, 0)) const needed = textH.map((h, i) => h + pad[i].top + pad[i].bottom) return { cells, laid, pad, textH, h: Math.max(t.rowHeights?.[ri] ?? 0, ...needed, 12) } } + /** the sides of a cell's border that are drawn, as lines: `top` and `bottom` only where the cell begins and ends (a row split across pages has the middle of its sides on both) */ + private cellLines(t: Table, c: Table['rows'][number][number], x0: number, x1: number, y0: number, y1: number, top: boolean, bottom: boolean) { + // a cell the reader worked nothing out for has the table's plain grid, if it has one + const hair = t.borders ? { w: 0.5, color: '000000' } : null + const edge = c.border ?? { top: hair, right: hair, bottom: hair, left: hair } + const line = (l: { w: number; color: string } | null, ax: number, ay: number, bx: number, by: number) => { + if (l) this.page.line(ax, ay, bx, by, l.w, hexRgb(l.color)) + } + line(edge.left, x0, y0, x0, y1) + line(edge.right, x1, y0, x1, y1) + if (top && c.vMerge !== 'continue') line(edge.top, x0, y0, x1, y0) + if (bottom) line(edge.bottom, x0, y1, x1, y1) + } + + /** a row at the current place: the fills, the text in the cells and the lines around them */ + private drawRow(t: Table, xs: number[], ri: number, r = this.row(t, xs, ri)) { + const { cells, laid, pad, textH, h } = r + if (!this.band) for (const b of cells) for (const q of b.c.paragraphs) for (const name of q.bookmarks ?? []) this.ctx.anchors.set(name, { page: this.d.pages.length - 1, y: this.y }) + cells.forEach((b, i) => { + if (b.c.fill) this.page.fillRect(b.x0, this.y, b.x1 - b.x0, h, hexRgb(b.c.fill)) + if (laid[i].length) { + // text in a cell taller than it goes at the top, middle or bottom of the room inside the cell's margins + const free = Math.max(0, h - pad[i].top - pad[i].bottom - textH[i]) + const shift = b.c.vAlign === 'center' ? free / 2 : b.c.vAlign === 'bottom' ? free : 0 + this.draw(laid[i], { x: b.x0 + pad[i].left, y: this.y + pad[i].top + shift, w: b.x1 - b.x0 - pad[i].left - pad[i].right, h: textH[i] }) + } + this.cellLines(t, b.c, b.x0, b.x1, this.y, this.y + h, true, true) + }) + this.y += h + } + + /** + * A row that doesn't fit where it is, drawn in pieces: each cell takes the lines of its text that fit, the piece is drawn, + * and what is left of the row goes on at the top of the next column or page, until every cell is out of lines. + */ + private splitRow(t: Table, xs: number[], ri: number, r: ReturnType, repeat: () => void) { + const { cells, laid, pad } = r + // each cell's lines one after the other, with the space its paragraph keeps above and below + type Item = { l: Laid; k: number; above: number; height: number; below: number } + const items = laid.map((ls) => ls.flatMap((l, pi): Item[] => l.lines.map((ln, k) => ({ l, k, above: k === 0 && pi ? l.before : 0, height: ln.height + (ln.gap ?? 0), below: k === l.lines.length - 1 ? l.after : 0 })))) + const at = items.map(() => 0) + let first = true + for (let guard = 0; guard < 10_000 && at.some((a, i) => a < items[i].length); guard++) { + const room = this.bottom - this.y + const take = items.map((list, i) => { + let n = 0 + let used = 0 + while (at[i] + n < list.length) { + const it = list[at[i] + n] + // a line needs its own height and the space above it; what is below it only counts at the end of the paragraph + if (used + it.above + it.height + it.below > room - pad[i].top - pad[i].bottom + 0.01) break + used += it.above + it.height + it.below + n++ + } + return { n, used } + }) + const stuck = take.every((x, i) => x.n === 0 || at[i] >= items[i].length) + if (stuck && !this.atTop()) { + this.advance() + if (t.headerRows && ri >= t.headerRows) repeat() + continue + } + // at the top of a page and still nothing fits: one line each goes anyway + if (stuck) items.forEach((list, i) => { if (at[i] < list.length) take[i] = { n: 1, used: list[at[i]].above + list[at[i]].height + list[at[i]].below } }) + const pieceH = Math.max(12, ...take.map((x, i) => (x.n ? x.used + pad[i].top + pad[i].bottom : 0))) + cells.forEach((b, i) => { + const part = items[i].slice(at[i], at[i] + take[i].n) + if (b.c.fill) this.page.fillRect(b.x0, this.y, b.x1 - b.x0, pieceH, hexRgb(b.c.fill)) + if (part.length) { + // the part of each paragraph that is in this piece, with the space above kept only where the paragraph starts + const groups: Laid[] = [] + for (const it of part) { + let g = groups[groups.length - 1] + if (g && g.para === it.l.para) g.lines.push(it.l.lines[it.k]) + else groups.push((g = { ...it.l, lines: [it.l.lines[it.k]], before: groups.length ? it.l.before : 0, after: 0, bullet: it.k === 0 ? it.l.bullet : undefined })) + if (it.below) g.after = it.l.after // the space below a paragraph only where its last line is + } + this.draw(groups, { x: b.x0 + pad[i].left, y: this.y + pad[i].top, w: b.x1 - b.x0 - pad[i].left - pad[i].right, h: pieceH }) + } + at[i] += take[i].n + const done = at.every((a, j) => a >= items[j].length) + this.cellLines(t, b.c, b.x0, b.x1, this.y, this.y + pieceH, first, done) + }) + this.y += pieceH + first = false + if (at.some((a, i) => a < items[i].length)) { + this.advance() + if (t.headerRows && ri >= t.headerRows) repeat() + } + } + } + table(t: Table) { const xs = this.columns(t) - const line = (x0: number, y0: number, x1: number, y1: number) => this.page.line(x0, y0, x1, y1, 0.5, [0, 0, 0]) + // the heading rows come again at the top of each page the table goes on on, if there's room for them and more + const repeatHeads = (ri: number, r: ReturnType) => () => { + const heads = t.headerRows && ri >= t.headerRows ? Array.from({ length: t.headerRows }, (_, k) => this.row(t, xs, k)) : [] + if (heads.reduce((a, x) => a + x.h, 0) + Math.min(r.h, 40) < (this.bottom - this.top) / 2) heads.forEach((x, k) => this.drawRow(t, xs, k, x)) + } // a cell continued from the row above is drawn as part of it: rows are drawn one at a time, so its text stays in its first row t.rows.forEach((_, ri) => { - const { cells, laid, pad, textH, h } = this.row(t, xs, ri) - if (this.y + h > this.bottom && !this.atTop()) this.newPage() - cells.forEach((b, i) => { - if (b.c.fill) this.page.fillRect(b.x0, this.y, b.x1 - b.x0, h, hexRgb(b.c.fill)) - if (laid[i].length) { - // text in a cell taller than it goes at the top, middle or bottom of the room inside the cell's margins - const free = Math.max(0, h - pad[i].top - pad[i].bottom - textH[i]) - const shift = b.c.vAlign === 'center' ? free / 2 : b.c.vAlign === 'bottom' ? free : 0 - this.draw(laid[i], { x: b.x0 + pad[i].left, y: this.y + pad[i].top + shift, w: b.x1 - b.x0 - pad[i].left - pad[i].right, h: textH[i] }) + const r = this.row(t, xs, ri) + const marks = r.laid.flatMap((l) => l.flatMap((p) => p.lines.flatMap(refsOf))) + const need = r.h + this.noteCost(marks, new Set()) + if (this.y + need > this.bottom) { + // a row with text of more than a line that may be split goes on in pieces; any other starts on the next column or page + const splittable = !t.noSplit?.[ri] && r.laid.some((l) => l.reduce((n, p) => n + p.lines.length, 0) > 1) + if (splittable) { + this.takeNotes(marks) + this.splitRow(t, xs, ri, r, repeatHeads(ri, r)) + return } - if (t.borders) { - line(b.x0, this.y, b.x0, this.y + h) - line(b.x1, this.y, b.x1, this.y + h) - if (b.c.vMerge !== 'continue') line(b.x0, this.y, b.x1, this.y) - line(b.x0, this.y + h, b.x1, this.y + h) + if (!this.atTop()) { + this.advance() + repeatHeads(ri, r)() } - }) - this.y += h + } + this.takeNotes(marks) + this.drawRow(t, xs, ri, r) }) this.y += t.after ?? 0 } @@ -424,6 +762,30 @@ class Flow { return this.headHeight(b) } + /** lay out a section's blocks in the flow */ + run(blocks: DocBlock[]) { + let anchorY = this.y + for (let i = 0; i < blocks.length; i++) { + const b = blocks[i] + if (b.kind === 'p' && b.frame) { + const group = [b] + while (i + 1 < blocks.length && blocks[i + 1].kind === 'p' && (blocks[i + 1] as Paragraph).frame === b.frame) group.push(blocks[++i] as Paragraph) + this.framed(group, anchorY) + } else if (b.kind === 'p') { + anchorY = this.y + // keep with the next: start a new column or page when this and the first lines after it don't fit together + if (b.keepNext && !b.pageBreakBefore && !this.atTop()) { + const need = this.chainHeight(blocks, i) + if (this.y + need > this.bottom && need <= this.bottom - this.top) this.advance() + } + const [placed, last] = placedAfter(blocks, i) + this.paragraph(b, placed) + i = last + } else if (b.kind === 'picture') this.picture(b, anchorY) + else this.table(b) + } + } + /** draw a header or footer's blocks from `y`, onto `page` */ fill(page: PageDraw, y: number, blocks: DocBlock[]) { this.page = page @@ -458,8 +820,8 @@ function placedAfter(blocks: DocBlock[], i: number): [Picture[], number] { /** how tall a header or footer is, drawn onto a page that is thrown away */ function bandHeight(f: Flow, blocks: DocBlock[]): number { if (!blocks.length) return 0 - const scratch = new Flow(f.d, f.s, f.book, f.imageFor, [], [], true) - scratch.fill(new PageDraw(f.s.width, f.s.height), 0, numbered(blocks, 1, 1)) + const scratch = new Flow(f.ctx, f.s, true) + scratch.fill(new PageDraw(f.s.width, f.s.height), 0, numbered(blocks, '1', 1)) return scratch.y } @@ -477,44 +839,84 @@ function outlineOf(marks: Mark[], heightOf: (page: number) => number): OutlineIt return root } -/** a Word document as PDF pages */ -export function layoutDocx(d: PdfDrawing, doc: DocModel, book: FontBook, imageFor: ImageFor): void { - const pages: PageInfo[] = [] - const marks: Mark[] = [] - for (const s of doc.sections) { - const flow = new Flow(d, s, book, imageFor, pages, marks) - let anchorY = flow.y - for (let i = 0; i < s.blocks.length; i++) { - const b = s.blocks[i] - if (b.kind === 'p' && b.frame) { - const group = [b] - while (i + 1 < s.blocks.length && s.blocks[i + 1].kind === 'p' && (s.blocks[i + 1] as Paragraph).frame === b.frame) group.push(s.blocks[++i] as Paragraph) - flow.framed(group, anchorY) - } else if (b.kind === 'p') { - anchorY = flow.y - // keep with the next: start a new page when this and the first lines after it don't fit together - if (b.keepNext && !b.pageBreakBefore && !flow.atTop()) { - const need = flow.chainHeight(s.blocks, i) - if (flow.y + need > flow.bottom && need <= flow.bottom - flow.top) flow.newPage() - } - const [placed, last] = placedAfter(s.blocks, i) - flow.paragraph(b, placed) - i = last - } else if (b.kind === 'picture') flow.picture(b, anchorY) - else flow.table(b) - } +/** + * How tall each column of a section should be so that they end together (what Word does before a continuous section break): + * the section laid out in one column to see how tall it is, then in its columns with a limit that is raised until it all fits + * on the page. Undefined when the columns just fill the room, or no limit works. + */ +function balancedHeight(ctx: Ctx, s: DocSection, flow: Flow): number | undefined { + const n = flow.cols.length + if (n < 2) return undefined + const probe = (section: DocSection, limit?: number) => { + const sc: Ctx = { ...ctx, d: new PdfDrawing(), pages: [], marks: [], anchors: new Map(), links: [], index: new Map() } + const f = new Flow(sc, section) + if (limit !== undefined) f.capBottom(limit) + f.run(s.blocks) + return { f, pages: sc.pages.length } } + const plain = { ...s, header: undefined, footer: undefined, firstHeader: undefined, firstFooter: undefined, evenHeader: undefined, evenFooter: undefined, titlePg: false, type: undefined, pageStart: undefined, height: 14400, margins: { ...s.margins, top: 0, bottom: 0, header: 0, footer: 0 } } // as tall as a page can be + // one column of the first column's width, as tall as it needs + const textW = s.width - s.margins.left - s.margins.right + const tall = probe({ ...plain, columns: undefined, margins: { ...plain.margins, right: s.margins.right + textW - flow.cols[0].w } }).f + const total = tall.y + const room = flow.bottom - flow.y + if (!(total > 0) || total / n >= room) return undefined + for (let height = total / n, k = 0; k < 80; k++, height += 4) { + if (height >= room) return undefined + if (probe({ ...plain }, height).pages === 1) return height + } + return undefined +} + +/** one pass over the document: every section laid out, then notes, headers and footers */ +function layoutPass(ctx: Ctx): void { + const { d, doc, pages } = ctx + const flow = new Flow(ctx, doc.sections[0]) + doc.sections.forEach((s, si) => { + if (si) flow.beginSection(s) + if (s.columns && doc.sections[si + 1]?.type === 'continuous') { + const height = balancedHeight(ctx, s, flow) + if (height !== undefined) flow.capBottom(height) + } + flow.run(s.blocks) + flow.endSection() + }) + const flows = [flow] + // endnotes where the document ends, in the order their marks come + const ends = doc.sections.flatMap((sec) => sec.blocks.flatMap((b) => paragraphsOf(b).flatMap((q) => q.runs.flatMap((r) => (r.note?.kind === 'end' ? [r.note.id] : []))))) + if (flows.length && ends.length) flows[flows.length - 1].endnotes(ends) + for (const f of flows) f.flushNotes() // headers and footers last, when the number of pages is known const total = pages.length - pages.forEach((info, k) => { - const band = new Flow(d, info.s, book, imageFor, pages, marks, true) - const header = bandOf(info.s, info.first, 'header') - const footer = bandOf(info.s, info.first, 'footer') - if (header.length) band.fill(info.page, info.s.margins.header ?? 36, numbered(header, k + 1, total)) + pages.forEach((info) => { + const band = new Flow(ctx, info.s, true) + const header = bandOf(info.s, info, 'header', doc.evenAndOdd) + const footer = bandOf(info.s, info, 'footer', doc.evenAndOdd) + if (header.length) band.fill(info.page, info.s.margins.header ?? 36, numbered(header, info.label, total)) if (footer.length) { const h = bandHeight(band, footer) - band.fill(info.page, info.s.height - (info.s.margins.footer ?? 36) - h, numbered(footer, k + 1, total)) + band.fill(info.page, info.s.height - (info.s.margins.footer ?? 36) - h, numbered(footer, info.label, total)) } }) - d.outline = outlineOf(marks, (page) => d.pages[page].height) + d.outline = outlineOf(ctx.marks, (page) => d.pages[page].height) + // links to bookmarks, now that the places are known + for (const l of ctx.links) { + const to = ctx.anchors.get(l.name) + if (to) d.pages[l.page].goto(l.x, l.y, l.w, l.h, to.page, to.y) + } +} + +/** a Word document as PDF pages */ +export function layoutDocx(d: PdfDrawing, doc: DocModel, book: FontBook, imageFor: ImageFor): void { + const fresh = (refs: Map): Ctx => ({ d, book, imageFor, pages: [], marks: [], doc, noteLaid: new Map(), anchors: new Map(), links: [], refs, index: new Map() }) + let ctx = fresh(new Map()) + layoutPass(ctx) + // a table of contents (or any page reference) shows the page its bookmark is on: now that they're known, lay the document out again with them + const hasRefs = doc.sections.some((sec) => sec.blocks.some((b) => paragraphsOf(b).some((q) => q.runs.some((r) => r.field === 'pageref')))) + if (hasRefs) { + const labels = new Map([...ctx.anchors].map(([name, a]) => [name, ctx.pages[a.page]?.label ?? ''])) + d.pages.length = 0 + ctx = fresh(labels) + layoutPass(ctx) + } }