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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,29 @@ const shaper: Shaper = {
- **Subsets**: `subsetFont` keeps the glyphs given and what they are made of, renumbered from 0 (glyph 0 stays), with a character map for the `chars` passed as a third argument. TrueType outlines are copied; PostScript ones (CFF, also CID-keyed) keep their charstrings and subroutines, and come as an OTF and as the bare CFF table (`cff`) a PDF embeds. `font.embeddable` is false for a font whose license forbids embedding.
- **Limits**: `FONT_LIMITS` (a file of 64 MB, 128 MB unpacked, 2,000,000 layout records). A file that is not a font, is damaged or is past a limit throws a `FontError` that says which. Not read: fonts embedded in a document, CFF2 outlines, the other instances of a variable font, bitmap and color glyphs. WOFF2 needs a runtime whose `DecompressionStream` unpacks Brotli: Node 26, Bun 1.4 and Firefox 157 do, Chromium 156 does not. There a WOFF2 file is refused with a `FontError` that says so (`font:error`, `onError`) and its text goes to the next font of the list, so declare TTF, OTF or WOFF files for text that has to be measured in Chrome.

### PDF output

A presentation is written as a PDF file by the library itself: no browser, no print dialog, no dependency, in the worker or on a server (`new Engine(emit)`). Its text is set in the font files of `fonts.set` and embedded as subsets, with the text of their glyphs, so it can be selected, copied and searched. Spreadsheets (#47) and documents (#48) follow.

```ts
await doc.call('fonts.set', fonts) // the files text is set in (see "Reading font files")
const pdf = await doc.call('pdf') // a Blob, application/pdf: every slide but the hidden ones
await doc.call('pdf', { slides: [0, 2], title: 'Q3' }) // these slides; hidden ones only with `hidden: true`, even when named
doc.on('warning', ({ text }) => {}) // what could not be drawn as it is, once each
const svg = await doc.call('pptx.svg', 0) // a slide as an SVG picture, drawn as the PDF draws it

// without a document: pages of drawing commands, and the backends that paint them
import { slidePage, writePdf, pageToSvg, svgItems, FontBook } from '@p10node/office-engine/core'
const page = await slidePage(slideModel, { fonts: new FontBook(fonts), part: async (name) => bytesOf(name) })
const bytes = await writePdf([page], { title: 'Deck', onWarning: console.warn })
```

- **Pages**: a slide is a page at the slide's size; shapes, preset and custom outlines, solid and gradient fills, picture fills, outlines with their dashes and arrowheads, flips and rotation, pictures with their crop and shape, tables (rows grow to their text), charts (the viewer's chart, as shapes and text) and text boxes are placed as the viewer places them. One page of the outline per slide title.
- **Text**: lines broken, aligned and spaced as the viewer's CSS does (`pre-wrap`, a unitless `line-height`), bullets in their hanging indent, underline, strikethrough, highlight, superscript and subscript, links, vertical text. Text no declared file serves is set in Helvetica (the PDF's own font, Windows-1252 characters only) and a warning names the font, so a server should declare its font files: Carlito, Caladea and Liberation stand in for Calibri, Cambria, Arial and Times New Roman.
- **Fonts' licenses**: a font that may not be embedded, or only as bitmaps, has its text set in Helvetica with a warning; one that may not be subset is embedded whole (but a CJK font keyed by CID, whose glyphs are not its characters, is set in Helvetica too). A damaged font file falls back the same way and the rest of the file is written.
- **Pictures**: JPEG copied as it is, PNG and GIF with their transparency, SVG as shapes and text. EMF, WMF, TIFF, BMP and WebP are left out with a warning. A picture is unpacked to at most 64 million pixels, an SVG picture draws at most 100,000 elements and 1,000,000 path segments (counted as drawn, references included); one past them, or one that cannot be read, is left out with a warning.
- **Not yet**: shadows and other effects, pattern fills (drawn in their foreground color), tiled pictures (stretched), text columns, autofit that shrinks text (the file's own scale is used) or grows the shape to its text, notes pages, SmartArt and OLE objects.

## Standalone packages

Diagrams and Markdown without the viewer (no DOM needed, no dependency): [`@p10node/diagram`](../packages/diagram), [`@p10node/mermaid`](../packages/mermaid), [`@p10node/plantuml`](../packages/plantuml), [`@p10node/graphviz`](../packages/graphviz), [`@p10node/markdown`](../packages/markdown).
Expand Down
1 change: 1 addition & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- **DOCX pages** are cut the way Word cuts them, with the lines the browser lays out: a paragraph that does not fit the rest of a page goes on at the top of the next one (split between two lines, the same lines as when whole; a justified paragraph's last line on the first page stays justified), a table between two rows, its header rows (`w:tblHeader`) repeated at the top of each following page; a merged cell cut by the page end goes on as an empty cell. Keep rules choose the cut: widow / orphan control (on unless the paragraph, its style or the document defaults turn it off), keep lines together, keep with next (a chain of headings goes with the first lines after it; a chain taller than a page is cut like any text). Only one line or one row taller than a page makes its page taller. Text columns are filled the same way, column after column
- **Fonts**: the host can supply font files (`fonts`: URLs or bytes, loaded with the `FontFace` API when a document uses the family); a declared fallback serves the fonts named before it (Carlito for Calibri, Caladea for Cambria, Liberation for Arial, Times New Roman and Courier New). DOCX pages are measured once those fonts are loaded (at most 3 s, not past a failure; a font that comes later has the pages measured again); a late web font of the page also has the pages measured again; XLSX fits its numbers again. No font file ships with the library, and fonts embedded in a document are not read. See [API](api.md#fonts)
- **Font files read without a browser** (core API): glyph widths, kerning, ligatures and line metrics from TTF, OTF, TTC, WOFF and WOFF2 files, the file a text is set in chosen as CSS chooses, each result marked exact or not (and why); subsets to embed, with the text of their glyphs for a PDF; a host's shaping engine for the scripts that need one. In the worker (`fonts.set`, `fonts.measure`) and on a server. See [API](api.md#reading-font-files)
- **PDF output without a browser** (core API): a presentation written as a PDF file by the library itself, in the worker or on a server: slides laid out as pages of drawing commands, text set in the host's font files and embedded as subsets that can be copied and searched, JPEG / PNG / GIF / SVG pictures, charts, links and an outline. Spreadsheets and documents follow. See [API](api.md#pdf-output)

## Format support

Expand Down
20 changes: 20 additions & 0 deletions src/core/draw/glyphs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** What each glyph of a text command stands for: copy, search and the SVG backend's characters. */
import type { DrawItem } from './types'

type Text = Extract<DrawItem, { t: 'text' }>

/**
* For each glyph, the text it stands for: its cluster's characters (to the next cluster's start) for the first glyph
* of a cluster, nothing for the others. (Worked out each time it is asked for: a command can be changed between two
* exports.)
*/
export function glyphTexts(it: Text): string[] {
const starts = [...new Set(it.clusters)].sort((a, b) => a - b)
const end = new Map(starts.map((c, k) => [c, starts[k + 1] ?? it.text.length]))
const seen = new Set<number>()
return it.clusters.map((c) => {
if (seen.has(c)) return ''
seen.add(c)
return it.text.slice(c, end.get(c))
})
}
Loading