Documentation
¶
Overview ¶
Package odf converts between ODT (OpenDocument Text) packages and the neutral github.com/go-richdoc/richdoc document model, and reads ODS (OpenDocument Spreadsheet), ODP (OpenDocument Presentation) and ODG (OpenDocument Graphics) packages as well. WritePresentation writes a deck of rendered slides as an ODP package, one full-page picture per slide with its speaker notes.
An ODT file is a ZIP container of XML parts. Parse opens the container, reads content.xml (resolving inline formatting against the automatic styles there and the named styles in styles.xml), consults META-INF/manifest.xml for embedded image media types, and maps the office:text body onto richdoc blocks and inlines. Write produces a minimal, valid OpenDocument Text package: the mimetype entry is written first and stored uncompressed, as the OpenDocument packaging specification requires.
The two directions are designed as a faithful round-trip: Parse(Write(d)) is semantically equal to d for the supported model.
Presentations and drawings ¶
office:presentation and office:drawing are the same document: draw:page elements holding shapes, with the words inside them. Each page becomes a heading of its own draw:name followed by the blocks found in its shapes, so a reader can tell which slide a sentence was on.
The rules were read off files LibreOffice wrote, and four of them contradict what the specification alone suggests. The title is not the frame with presentation:class="title" — Impress strips that attribute from any frame it does not treat as a master-page placeholder, so a converted deck has none. Speaker notes are skipped: presentation:notes holds a frame whose text reads exactly like body text. A picture is emitted only if its shape held nothing else, because Impress writes a draw:image preview of a table into the same frame as the table. And svg:title and svg:desc are the frame's own children, beside draw:image rather than inside it.
Spreadsheets ¶
A spreadsheet is the same package as a text document, inside an office:spreadsheet body instead of an office:text one, so Parse reads one too: each sheet comes back as a level-1 richdoc.Heading carrying its table:name, followed by the sheet's richdoc.Table. Write always produces ODT.
Only a spreadsheet body honours table:number-columns-repeated and table:number-rows-repeated, and only a spreadsheet body has the padding at the end of each row dropped — an empty cell between two full ones is a gap in the data and stays. A repeat is a count in a FILE, so what it asks for is an allocation the input decides: it is capped at 1024 columns and 65536 rows.
Footnotes, bookmarks and cross-references map onto native ODF elements: richdoc.Footnote ↔ text:note, richdoc.Anchor ↔ text:bookmark, and richdoc.CrossRef ↔ text:bookmark-ref (text:reference-ref is also accepted on parse). ODF has no first-class citation element, so a CrossRef of kind richdoc.RefCite is written as a bookmark-ref tagged with the private attribute odfgo:cite="true" that Parse restores to RefCite; an ODF endnote is normalized to a Footnote on the round-trip.
OpenDocument has no dedicated element for several other richdoc nodes (code blocks, block quotes, thematic breaks, inline/display math); those are written using standard ODF container elements tagged with attributes in a private namespace ("https://github.com/go-odf/odf"), which ODF consumers ignore and which let Parse re-recognize the node. Any remaining unrecognized element is preserved verbatim through richdoc.RawInline / richdoc.RawBlock with Format "odf", so nothing in the source is lost.
The package is pure Go and builds with CGO disabled, including for GOOS=js/GOARCH=wasm.
Index ¶
Constants ¶
const MaxGroupDepth = 32
MaxGroupDepth bounds how deep groups nest: a reader walks the tree recursively.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse converts the bytes of an ODT (OpenDocument Text) package into a richdoc.Document.
It opens the ZIP container, resolves inline formatting in content.xml against the automatic styles there and the named styles in styles.xml, reads embedded image bytes (surfaced as data: URIs) using META-INF/manifest.xml for their media types, and reads document metadata from meta.xml. Unrecognized elements are preserved verbatim as richdoc.RawBlock / richdoc.RawInline with Format "odf". A container that is not a ZIP, that lacks content.xml, that holds a corrupt entry, or whose XML is malformed returns an error.
func Write ¶
Write renders a richdoc.Document as the bytes of a minimal, valid OpenDocument Text (.odt) package.
The returned ZIP always begins with a stored (uncompressed) mimetype entry, as the OpenDocument packaging specification requires, followed by META-INF/manifest.xml, content.xml, styles.xml, an optional meta.xml (when the document carries metadata) and any embedded Pictures/. Inline formatting is generated as automatic text styles in content.xml.
Write is the inverse of Parse over the supported model: parsing Write's output reproduces the input tree. An richdoc.Image whose URL is a data: URI is decoded and re-embedded; a malformed data: URI is the only input that makes Write fail.
func WritePresentation ¶ added in v0.8.0
func WritePresentation(p Presentation) ([]byte, error)
WritePresentation writes a deck as an OpenDocument Presentation package (.odp): mimetype first and STORED, the manifest, content.xml, styles.xml, meta.xml when there are properties, and the pictures under Pictures/.
A slide is a picture (as in marp-cli's --pptx), editable shapes (Shapes: rectangles, ellipses, paths, text boxes and pictures, in fonts the package can embed), or both. Parse reads back each slide's name, pictures and the text boxes' words.
⛔ The output is deterministic: the same Presentation gives the same bytes. No clock is read (see Presentation.Created) and every name is a position.
Types ¶
type Font ¶ added in v0.9.0
Font is a typeface embedded in the package (style:font-face with an svg:font-face-uri, §16.23–16.26): TrueType data for each of its faces, any of which may be nil. Embed only fonts whose licence allows it.
type Image ¶ added in v0.9.0
Image is a picture shapes refer to by its position in Presentation.Images, plus one, stored once however many slides show it.
type PathCmd ¶ added in v0.9.0
PathCmd is one command of a path: 'M' move to P[0], 'L' line to P[0], 'C' cubic Bézier through P[0] and P[1] to P[2], 'Z' close; in pixels.
type Presentation ¶ added in v0.8.0
type Presentation struct {
Width, Height int
Slides []PresentationSlide
// The document properties, written to meta.xml when set: dc:title,
// dc:creator, dc:subject, and one meta:keyword for each comma-separated
// item of Keywords.
Title, Author, Subject, Keywords string
// Created is written as meta:creation-date and dc:date. The zero time
// writes neither, which is what makes the output the same bytes for the
// same input.
Created time.Time
// Images are the pictures editable slides' shapes show (Shape.Image),
// each stored once however many slides show it.
Images []Image
// Fonts are typefaces embedded in the package for viewers that do not
// have them.
Fonts []Font
}
A Presentation is a deck to write as an .odp: one full-slide picture per slide, with its speaker notes.
Width and Height are the slide size in CSS pixels, 96 to the inch — the unit the slides were rendered in. They are written in points, three quarters of a pixel each, which is exact: a whole number of pixels is at most two decimals of a point, where centimetres would round.
type PresentationSlide ¶ added in v0.8.0
type PresentationSlide struct {
// Image is the picture, stretched over the whole slide; render it at the
// slide's aspect ratio. ImageType is "png" or "jpeg", and is checked
// against the image's own signature. It may be left out when the slide
// has Shapes or a Background.
Image []byte
ImageType string
// Background is the page's fill; nil leaves it unfilled.
Background *RGBA
// Shapes are the slide's editable content, back to front.
Shapes []Shape
// Notes are the speaker notes, one paragraph per line. Empty writes no
// presentation:notes.
Notes string
// Name is the page's draw:name — what Impress shows in its slide pane and
// what [Parse] reads back as the slide's heading — and the picture's
// svg:desc, which is what a screen reader says for a slide that is a
// picture. Empty gives "Slide N".
Name string
}
A PresentationSlide is one slide: a picture that covers it, or editable shapes, or both (the picture at the back), and its notes.
type RGBA ¶ added in v0.9.0
type RGBA struct{ R, G, B, A uint8 }
RGBA is a colour with an alpha: 255 is opaque.
type Shape ¶ added in v0.9.0
type Shape struct {
X, Y, W, H float64
// Kind selects the element: ShapeRect (draw:rect, corners of Radius
// pixels), ShapeEllipse (draw:ellipse), ShapePath (draw:path, §10.3.7:
// Paths, closed outlines relative to the box, whose overlaps are holes,
// svg:fill-rule evenodd), ShapeText (a draw:frame holding a
// draw:text-box, §10.4.2–10.4.3, one paragraph of Text with no padding
// and no wrapping) or ShapeImage (a draw:frame holding a draw:image of
// Presentation.Images[Image-1]).
Kind ShapeKind
Radius float64
Paths [][]PathCmd
Text []TextRun
Image int
// Fill is the inside's colour; a zero alpha is no fill.
Fill RGBA
// NonZero fills a ShapePath by the nonzero rule instead of evenodd
// (svg:fill-rule, §20.406): what an SVG path filled by its default rule
// needs, where subpaths drawn the same way round overlap without a hole.
NonZero bool
// Name is the draw:name; for a picture it is also its svg:desc.
Name string
// Group, when not empty, makes the shape a group (draw:g, §10.3.15) of
// these shapes, which a viewer selects and moves as one: an SVG drawing,
// a formula. A draw:g has no box of its own: its members are placed in
// page coordinates like any shape, and Kind and the rest are ignored. A
// group may hold groups, MaxGroupDepth deep; every shape in it counts
// towards the slide's limit.
Group []Shape
}
A Shape is one element of an editable slide (ODF 1.3 Part 3 §10.3, §10.4): a rectangle, an ellipse or a path filled with a colour, a text box, or a picture. Shapes are drawn in order, the first at the back.
Positions and sizes are in CSS pixels, as the slide's own size is.
type TextRun ¶ added in v0.9.0
type TextRun struct {
Text string
Font string
Size float64
Bold, Italic bool
Color RGBA
Link string
// Spacing is added after each character, in points (fo:letter-spacing).
Spacing float64
}
TextRun is a stretch of text in one style. Font is a typeface name (an embedded one, see Presentation.Fonts, or one the viewer has); Size is in points. Link, when set, is an http, https or mailto URL.