Documentation
¶
Overview ¶
Package render turns a page of a PDF into pixels.
It reads with github.com/go-pdfkit/reader and rasterises with github.com/go-gfx/gfx. Nothing outside those and the standard library is used, so it builds for js/wasm and for every architecture the fleet targets — which is what lets a PDF be shown in a browser tab with nothing on the far end.
Index ¶
- Variables
- func Page(d *reader.Document, i int, opt Options) (*raster.Image, error)
- func PageSVG(d *reader.Document, i int, w io.Writer, opt SVGOptions) error
- type GlyphFont
- func (g *GlyphFont) Advance(code int) float64
- func (g *GlyphFont) Codes(s []byte) []int
- func (g *GlyphFont) Embolden() bool
- func (g *GlyphFont) FontMatrix() [6]float64
- func (g *GlyphFont) Outline(code int) ([]GlyphSeg, bool)
- func (g *GlyphFont) Resources() reader.Dict
- func (g *GlyphFont) Simple() bool
- func (g *GlyphFont) Type3() bool
- func (g *GlyphFont) Type3Proc(code int) (*reader.Stream, bool)
- func (g *GlyphFont) Vertical() bool
- type GlyphOp
- type GlyphSeg
- type Image
- type Options
- type Outliner
- type SVGOptions
Constants ¶
This section is empty.
Variables ¶
var ErrSVGTooDeep = errors.New("render: the SVG would nest clips deeper than its limit")
ErrSVGTooDeep says the page nests clips deeper than the SVG can hold: a rectangle inside a rectangle is one clip, but sixty-five clips of any other shape, one inside the next, are refused. Nothing was written.
var ErrSVGTooLarge = errors.New("render: the SVG would be larger than its limit")
ErrSVGTooLarge says the page would have come out past SVGOptions.MaxBytes. Nothing was written.
var ErrTimedOut = errors.New("render: the page was still being drawn when its time ran out")
ErrTimedOut says a page was still being drawn when its time ran out. The image returned with it holds what had been drawn by then.
var ErrTooMuchToDecode = errors.New("render: the page names more picture than may be decoded at once")
ErrTooMuchToDecode says a page names more picture than Images will decode at once. Nothing comes back with it: half the pictures of a page would be read as the whole of them by anything counting.
Functions ¶
func PageSVG ¶ added in v0.70.0
PageSVG writes the i'th page of a document, counting from one, as SVG.
Paths, text, clips, images and transparency groups become SVG elements; everything that has no equivalent in SVG 1.1 — tiling and shading patterns, shadings, soft masks — is drawn by the pixel renderer and embedded as an image where it falls. Text is written as outlines, so the output depends on no font. On any error but a failing writer nothing is written to w. The document holds only numbers, generated ids and base64 data: nothing the PDF says is copied into it as text.
Types ¶
type GlyphFont ¶ added in v0.71.0
type GlyphFont struct {
// contains filtered or unexported fields
}
A GlyphFont is one font of a document, ready to be asked for glyphs.
func (*GlyphFont) Advance ¶ added in v0.71.0
Advance is how far the pen moves for a code, in text space: a fraction of the font size, before the character and word spacing are added.
func (*GlyphFont) Codes ¶ added in v0.71.0
Codes splits a string shown with the font into its character codes.
func (*GlyphFont) Embolden ¶ added in v0.71.0
Embolden says the font stands in for a bold face the document does not carry and the renderer draws it heavier than its outline.
func (*GlyphFont) FontMatrix ¶ added in v0.71.0
FontMatrix is a Type 3 font's glyph space to text space.
func (*GlyphFont) Outline ¶ added in v0.71.0
Outline is the shape of a code in text space, and false when the font has none (a code it does not carry, a font program that cannot be read, a Type 3 font). A stand-in for an italic is leaned here as it is drawn. The slice is shared: it must not be written to.
func (*GlyphFont) Resources ¶ added in v0.71.0
Resources is the /Resources of a Type 3 font, nil when it has none.
func (*GlyphFont) Simple ¶ added in v0.71.0
Simple says codes are single bytes, which is when the word space applies.
func (*GlyphFont) Type3 ¶ added in v0.71.0
Type3 says the glyphs are content streams (GlyphFont.Type3Proc) and not outlines.
func (*GlyphFont) Type3Proc ¶ added in v0.71.0
Type3Proc is the glyph procedure of a Type 3 font for a code: a content stream that draws the glyph in glyph space, which GlyphFont.FontMatrix maps to text space.
func (*GlyphFont) Vertical ¶ added in v0.71.0
Vertical says the font is written top to bottom, which this package does not draw: a composite font with a vertical CMap (Identity-V, a name ending in -V, or a CMap stream with WMode 1). A caller that cannot reproduce what the renderer draws should refuse it.
type GlyphSeg ¶ added in v0.71.0
A GlyphSeg is one command of an outline and its points, in text space: one unit is the font size, so the glyph is placed by scaling it by the size.
type Image ¶ added in v0.17.0
type Image struct {
// Name is the resource name the page draws it by.
Name string
// Object is the number of the indirect object the picture was stored in,
// and nought when it has none: an inline image, or a stream written
// directly into the resource dictionary rather than referred to.
//
// It is here because it is the one identity a picture and another tool's
// extraction of it can be matched on, and this is the only place that
// knows it without guessing. A caller left with the NAME has to walk the
// document again to recover it, and a name is not an identity: it is
// unique within one resource dictionary and not across the several a page
// reaches through its forms, so `Im1` can mean two different objects on
// one page. go-pdfkit/conformance rebuilt this number that way and needed
// three mechanisms to do it -- a walk, an ambiguity rule for names that
// reached two objects, and a mapping from a mask to the picture that names
// it -- and each of the three produced a defect.
//
// A MASK carries the object of the picture that NAMES it, not its own.
// That is what pdfimages publishes on an smask row, and it is the number a
// mask can actually be paired on.
Object int
// Filter is the image format the picture was stored in — DCTDecode,
// JPXDecode, JBIG2Decode, CCITTFaxDecode — and is empty when the bytes
// were samples that no image codec had to read.
Filter string
// Decoded says the dictionary carried a /Decode array, which maps the
// stored samples onto the range the colour space wants and is applied
// here. It is worth knowing about because it is the one thing that makes
// this picture differ from the same picture as another tool EXTRACTS it:
// pdfimages writes the samples as stored, so a /Decode of [1 0] on a
// one-bit mask makes the two exact complements of each other, and every
// pixel differs for a reason that is not a disagreement.
Decoded bool
// Stencil says the picture is a one-bit mask, which carries no colours of
// its own: it paints whatever colour is in force through its own shape.
// Its Pic has that shape in the alpha channel and black everywhere else,
// because the colour is not the image's to know.
Stencil bool
// Pic is the decoded picture.
Pic *raster.Image
}
Image is one picture a page draws, decoded.
func Images ¶ added in v0.17.0
Images decodes the pictures the i'th page draws, counting from one, in the order of the names it draws them by.
It exists because a page is the wrong unit for two jobs. Getting the pictures out of a document is one. Finding out whether a codec is right is the other: a page is a composition, so one image decoded wrongly can be averaged away by everything drawn over and around it, and a per-page difference of a few percent says nothing about which image was wrong. That mattered here — a release went out drawing scanned pages dark because "ink appeared" was measured instead of "the right ink".
What comes back is what each CODEC produced, not what the page draws. A picture that names a mask is returned unmasked, and the mask is returned beside it as its own entry, named for the key that named it. That is what pdfimages does, and it is the only way the two can be compared: applying a mask to one side and not the other made 21 of 22 JPEG 2000 pictures in a corpus of scanned pages look wrong, when 11 of them differed by nothing but the /SMask.
It also means a picture whose mask cannot be read still comes back. Page declines to draw that one, because how much of it shows is unknown — but the codec read it, and this is about the codec.
A picture nothing here can decode is left out rather than returned empty, which is the same answer Page gives by not drawing it. Inline images — the ones written into the content stream with BI — are not returned: they are named by no resource and are objects of nothing, so there is nothing for a tool that extracts a file's objects to hand back beside them.
It is the CONTENT STREAM that is walked, not the page's /Resources. A resource dictionary is a catalogue of what a page may draw, and PDF lets every page in a file share one; see [renderer.imagesDrawn] for what walking the catalogue instead did to a measurement.
Each picture comes back once, however many of the page's forms reach it, and a page that draws more picture than [maxImagesPixels] is refused whole with ErrTooMuchToDecode rather than decoded until the machine gives out.
type Options ¶
type Options struct {
// Scale is device pixels per point. Zero means one, which is seventy-two
// to the inch; [Options.DPI] is the same thing said the other way.
Scale float64
// DPI sets the scale from dots per inch. It is ignored when Scale is set.
DPI float64
// Background is painted before anything else. The zero value is opaque
// white, which is what paper is.
Background *color.RGBA
// MaxPixels refuses a page that would come out larger than this, so a
// media box a file made up cannot be asked to fill memory. Zero means
// forty million, which is a little over A4 at 600 dots to the inch.
MaxPixels int
// NoAnnotations leaves out what the page carries beside its content: the
// notes, the highlights, the stamps, and the fields of a form. They are
// drawn by default, because a filled-in form drawn without them is a form
// with nothing in it.
NoAnnotations bool
// MaxDuration is how long the page may be drawn for. Zero means as long as
// it takes, which is what this did before there was anywhere to say
// otherwise.
//
// Some pages take a very long time. Of 59 432 corpus pages drawn at half
// as much again as their own size, 1 131 were still going after twenty
// seconds, and one figure took two hundred and seventy-three. A caller
// drawing somebody else's file — a browser tab, a queue of them, a server
// — cannot afford to wait for the worst of those and has, until now, had
// no way to say so: MaxPixels bounds what comes out, and nothing bounded
// the work of making it.
//
// When the time runs out the page stops being drawn and comes back as far
// as it got, with ErrTimedOut. That is deliberately not a blank: half a
// page is worth more than nothing to somebody scrolling, and the error
// says plainly that it is half.
MaxDuration time.Duration
// AllLayers draws every optional-content layer, including the ones the
// document's default configuration turns off.
//
// The default is to draw what the document says it shows when it is
// opened. On the corpus that is the same picture either way: 275 of the
// 1 633 real forms use optional content and 161 hide a group, but not one
// mark on any page of any of the 8 300 files measured falls inside a layer
// its own configuration hides. See optional.go for the numbers and for why
// the mechanism is read all the same.
//
// A caller that wants everything — a tool showing a layer panel, an
// archive comparing what a file holds against what it displays — sets
// this and gets the behaviour from before optional content was read.
AllLayers bool
}
Options say how a page is to be drawn.
type Outliner ¶ added in v0.71.0
type Outliner struct {
// contains filtered or unexported fields
}
An Outliner reads the fonts of a document the way the renderer does, and hands their glyphs out as shapes instead of drawing them. It is how a tool that rewrites a content stream turns text into paths without a second implementation of which glyph a code stands for: the answer is the one this package draws, so a page rewritten with it looks like the page rendered.
An Outliner is not safe for concurrent use.
func NewOutliner ¶ added in v0.71.0
NewOutliner reads the fonts of d.
type SVGOptions ¶ added in v0.70.0
type SVGOptions struct {
// MaxBytes refuses a document that would come out larger than this, with
// [ErrSVGTooLarge], and writes nothing. Zero means 64 MiB. A page a file
// made up can be asked for any number of marks, and each is a few dozen
// bytes of text: this is what bounds the output.
MaxBytes int
// RasterScale is how many pixels a point gets in the pictures embedded for
// what SVG cannot say as a shape: a tiling or shading pattern, a soft mask,
// a transparency group under a mask. Zero means two. The page's own
// coordinates are unaffected; only these pictures change in sharpness (and
// in size).
RasterScale float64
// MaxDuration is how long the page may be interpreted for, as for
// [Options.MaxDuration] except that zero means ten seconds, not no limit.
// When it runs out [ErrTimedOut] is returned and
// nothing is written: unlike a raster, a document cut short is not a
// smaller picture but malformed.
MaxDuration time.Duration
}
SVGOptions say how a page is written as SVG.