officecanvas

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package officecanvas records a laid-out page as the editable objects an office presentation is made of — filled shapes, gradient-filled shapes, lines of text in text boxes, and pictures — instead of drawing it. It is a paint.Canvas: PageLayout.PaintVector draws through it exactly as it draws a PDF page through pdfcanvas, so an editable slide has the layout of the image and PDF exports by construction. The recorded Page is format-neutral, in device pixels; a writer maps it to DrawingML (PresentationML) or to ODF draw elements.

What a slide format cannot hold the paint layer already turned into pictures (an element under opacity, a filter, a blurred shadow); those arrive here as images, with their text as invisible runs over them.

Things a slide cannot do that a page can: clip. A gradient or a picture inside a clip takes the clip's outline (a rounded box) or is cropped to its bounds; a filled shape ignores the clips around it, which are the overflow clips of its ancestors and cut nothing on a slide that fits.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SFNT

func SFNT(f *opentype.Font) []byte

SFNT reassembles a parsed font into a font file — the offset table, the table directory with each table's checksum, the tables padded to four bytes — from the tables it carries (OpenType "Organization of an OpenType Font"), so that the very font a page was laid out with can be embedded whole. It returns nil for a font past 32 MiB.

Types

type Canvas

type Canvas struct {
	// contains filtered or unexported fields
}

Canvas records a page. Build one per page with New; it is not safe for concurrent use.

func New

func New(page image.Rectangle, links []Link, faces map[*opentype.Font]*Face) *Canvas

New starts recording a page of the given device-pixel size. links are the page's clickable areas (engine.PageLayout.Links). faces, shared across the pages of one document, maps each font to its Face; nil gets a fresh map.

func (*Canvas) Clip

func (c *Canvas) Clip(p *paint.Path, evenOdd bool)

Clip implements paint.Canvas: the clip is remembered, to shape what is drawn in it.

func (*Canvas) Fill

func (c *Canvas) Fill(p *paint.Path, col css.Color, evenOdd bool)

Fill implements paint.Canvas.

func (*Canvas) Glyphs

func (c *Canvas) Glyphs(run *paint.GlyphRun)

Glyphs implements paint.Canvas: the run becomes text, joined to the line being built when it continues it — the same baseline, a word's space further — so that a line of words is one editable text box.

func (*Canvas) Image

func (c *Canvas) Image(img image.Image, dst paint.Rect)

Image implements paint.Canvas: a picture, cropped to the innermost clip's bounds when the clip cuts it (a background image sized to cover its box).

func (*Canvas) Page

func (c *Canvas) Page() Page

Page returns what was recorded.

func (*Canvas) Restore

func (c *Canvas) Restore()

Restore implements paint.Canvas.

func (*Canvas) Save

func (c *Canvas) Save()

Save implements paint.Canvas.

func (*Canvas) Shade

func (c *Canvas) Shade(s paint.Shading)

Shade implements paint.Canvas: the gradient fills the innermost clip's outline. PaintVector splits one CSS gradient into shadings of constant opacity, all under one clip; they are merged back into one gradient here, a stop per stop, transparent across the gaps between them.

type Face

type Face struct {
	Font         *opentype.Font
	Family       string // the name a document names it by (name ID 1)
	Bold, Italic bool   // its style within that family (name ID 2)
	// Embeddable is false when the font's OS/2 fsType forbids embedding it
	// in an editable document (restricted, or preview & print only).
	Embeddable bool
}

Face is a font face the runs are set in.

type Gradient

type Gradient struct {
	Radial   bool
	Angle    float64
	Center   [2]float64
	Stops    []paint.GradientStop
	Shadings []paint.Shading
}

Gradient is a gradient fill in office terms: stops along a direction across the shape's box, or around a centre.

Linear: Angle is the direction of colour change in degrees clockwise from the x axis (y down), and the stops run across the projection of the box on that direction — DrawingML's unscaled a:lin, and CSS's gradient line. Radial: the stops run from Center (fractions of the box) outwards.

Shadings are the paint-level shadings it was made from, for a writer that cannot express it and draws it as a picture (Shape.Render).

type Kind

type Kind uint8

Kind is what a recorded Shape is.

const (
	KindFill     Kind = iota // Path filled with Color
	KindGradient             // Path filled with Gradient
	KindText                 // a line of Runs in Bounds
	KindImage                // Image stretched over Bounds, after Crop
)

The shapes.

type Link struct {
	Rect image.Rectangle
	Href string
}

Link is a clickable area: text drawn inside it links to Href.

type Page

type Page struct {
	// Background is the colour of a fill covering the whole page drawn
	// before anything else: the slide's background. Nil when there is none.
	Background *css.Color
	Shapes     []Shape
}

Page is what was drawn on one page.

type Run

type Run struct {
	Text      string
	Face      *Face
	Size      float64
	Color     css.Color
	Link      string
	Invisible bool
	// Spacing is the space added after each character beyond its own
	// advance (CSS letter-spacing), in device pixels.
	Spacing float64
}

Run is a stretch of a text line in one face, size and colour. Size is in device pixels. Invisible text is there to be found and copied over a picture of the same words; it is drawn with zero alpha.

type Shape

type Shape struct {
	Kind   Kind
	Bounds paint.Rect

	// Path is the outline of a fill or gradient, absolute, and EvenOdd its
	// rule (overlaps are holes). Rect is set when the outline is Bounds
	// itself.
	Path    *paint.Path
	EvenOdd bool
	Rect    bool

	Color    css.Color // KindFill
	Gradient *Gradient // KindGradient

	Runs []Run // KindText

	Image image.Image // KindImage
	// Crop is what to remove from the picture's left, top, right and
	// bottom, as fractions, before it is stretched over Bounds.
	Crop [4]float64
}

Shape is one recorded object. Bounds is its box in device pixels.

func (*Shape) Baseline

func (s *Shape) Baseline() float64

Baseline is a text shape's baseline, in device pixels from the page top.

func (*Shape) Render

func (s *Shape) Render(scale float64) *image.NRGBA

Render draws a gradient shape as a picture of its Bounds, scale pixels per device pixel: the shadings it was made from, through its outline, antialiased, transparent outside. It is for a format whose gradients cannot say what this one says (more than two stops, varying opacity). It returns nil for another kind of shape, or one past 16 Mpx.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL