Documentation
¶
Overview ¶
Package typst sets paged documents with Typst and hands the pages to gift to draw. It is a spike: it proves the path from source to pixels and to PDF, and leaves out much of what a product needs; see "Not yet" below.
No cgo ¶
Typst is a Rust library. It is compiled into libgifttypst, a shared library with a small C ABI, from the crate in typst/ffi, and opened at run time with purego, the way asset/turbojpeg opens libturbojpeg. gift itself keeps building with CGO_ENABLED=0; a system without the library reports ErrUnavailable and nothing else changes.
The library is some 35 MB, so this package does not carry it. A program that wants it carried imports typst/embedded for its side effect, which embeds it for macOS on arm64 and Linux on amd64 and arm64:
import _ "github.com/worldiety/gift/typst/embedded"
The library is looked for at the path given to Open, in GIFT_TYPST_LIB, in typst/embedded if the program imports it, next to the executable and on the dynamic linker's search path, in this order. A change to the crate is built and tried with
(cd typst/ffi && cargo build --release) GIFT_TYPST_LIB=$PWD/typst/ffi/target/release/libgifttypst.dylib go run ./cmd/example-typst
and embedded with typst/ffi/build.sh.
What crosses the boundary ¶
Into the library go font files and the source. An Engine keeps both between compilations, and Typst memoizes its intermediate results, so a compile after an edit redoes only what the edit touched.
Out of it comes each page as a flat display list in points: glyph runs, filled rectangles — every horizontal and vertical rule, which is what a table's strokes are — and triangles for every other shape. A glyph carries its font, its id and its position as Typst shaped and placed it, so gift draws it through its own glyph atlas without shaping anything again. That works because Engine.AddFont loads the same bytes into gift's text stack: a glyph id means the same glyph on both sides only if both read one file.
Every glyph also carries the byte offset in the source of the text it was shaped from, or -1 for text that was not written there — numbering, page numbers, text from a show rule. Page.SourceAt and Document.CaretAt map between the page and the source with it, which is what editing on the page needs: a click becomes a source offset, a source offset a caret.
Engine.PDF exports the last document that compiled, with Typst's own PDF writer.
Not yet ¶
Images, data files and packages are not supplied to Typst, so documents cannot use them. Gradients and tilings are drawn grey, clips are ignored, rotated text is drawn upright, filled curves are triangulated as fans, which is right only for convex outlines. Every page crosses the boundary on every compile, where only the changed ones would have to. The caret heuristics know nothing of bidirectional text.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("typst: engine is closed")
ErrClosed reports a call on an Engine after Engine.Close.
ErrUnavailable reports that libgifttypst could not be loaded.
Functions ¶
func Open ¶
Open loads the library from path before anything else would look for it, or, with an empty path, from where the package looks by itself; see the package documentation. It must be called before the first use of the package to have an effect; afterwards it reports what that first use found.
Types ¶
type CompileError ¶
type CompileError struct{ Diagnostics []Diagnostic }
CompileError is the error of a document that did not compile.
func (*CompileError) Error ¶
func (e *CompileError) Error() string
type Diagnostic ¶
type Diagnostic struct {
// Start and End are byte offsets into the source, -1 if Typst could
// not attribute the error to it.
Start, End int
Message string
}
Diagnostic is one error in the source.
type Document ¶
type Document struct {
Pages []Page
}
Document is a laid out document: its pages, in points.
func (*Document) CaretAt ¶
CaretAt returns the caret for a byte offset in the source: at the left edge of the glyph that starts there, or at the right edge of the glyph that ends there. It reports false for an offset no glyph starts or ends at, which is one inside markup, inside code or in text the document does not show.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a Typst compiler with its fonts and one document.
It keeps everything between compilations: the parsed source, the fonts and Typst's memoized intermediate results. A compile after a small edit therefore redoes only what the edit touched. An Engine is safe for concurrent use; calls are serialised.
func New ¶
New creates an engine without fonts. Typst draws nothing without a font, so add at least one with Engine.AddFont.
func (*Engine) AddFont ¶
AddFont makes the faces of a TrueType or OpenType file available to documents, which select them by family, weight and style with Typst's own text function.
The same bytes are loaded into gift's text stack, so that the glyphs Typst positions are drawn by gift's glyph atlas from the very same outlines: a glyph id means the same glyph on both sides only if both read one file. A collection (.ttc) is accepted by Typst but not drawn by gift.
func (*Engine) Compile ¶
Compile lays the document out. On errors in the source it returns a *CompileError.
type Glyph ¶
type Glyph struct {
ID render.GlyphID
// X and Y are the origin on the baseline, Advance the width.
X, Y, Advance float32
// Source is the byte offset in the source of the text the glyph was
// shaped from, or -1 for text not written there: numbering, text from a
// show rule. Len is the length of that text in bytes.
Source, Len int
}
Glyph is one positioned glyph of a text run.
type Item ¶
type Item struct {
Kind ItemKind
// Font and Size are those of a text run: a font of gift's text stack,
// zero for a face it could not load, and the em size in points.
Font render.FontID
Size float32
// Color is premultiplied, as everywhere in gift.
Color render.Color
// Bounds is the rectangle the item covers, roughly for text.
Bounds geom.Rect
First, Count int
}
Item is one entry of a page's display list.
type ItemKind ¶
type ItemKind uint8
ItemKind says what an Item draws.
const ( // ItemText is a run of glyphs in one font, size and colour, // Page.Glyphs[First:First+Count]. ItemText ItemKind = iota + 1 // ItemRect is a filled rectangle: backgrounds, and every horizontal or // vertical rule, which is what a table's strokes are. ItemRect // ItemTriangles is a filled triangle list, Page.Vertices[First:First+Count], // three per triangle: everything else Typst draws, flattened. ItemTriangles // ItemImage is the place of a picture. The picture itself is not // transferred. ItemImage )
type Page ¶
Page is one page as a flat display list, in points from its top left corner, painted in the order of Items.
func (*Page) SourceAt ¶
SourceAt returns the byte offset in the source that a click at (x, y) on the page means: before the glyph under the point if its left half was hit, after it otherwise. Off the text it takes the nearest glyph of the line the point is on, and off every line the nearest glyph at all. It reports false for a page without text that came from the source.