Documentation
¶
Overview ¶
Package offscreen renders a gfx scene to an image without a window, a display or a build tag: it picks the headless gfx.Renderer of the running OS (CoreGraphics on macOS, cairo and Pango on Linux, Direct2D on Windows). On any other OS every call returns an error.
PDF and NewPDF write the same scene as a vector PDF.
PNG is the whole flow for one image:
png, err := offscreen.PNG(scene, node, 2, gfx.White)
New gives the renderer itself, for gfx.RenderSubtree with a fixed area, for a Raster, or for measuring text.
Index ¶
- func Default() (gfx.Renderer, error)
- func New(o Options) (gfx.Renderer, error)
- func NewPDF(w io.Writer) (gfx.Document, error)
- func PDF(w io.Writer, s *gfx.Scene, root *gfx.Node, o gfx.PDFOptions) error
- func PNG(s *gfx.Scene, root *gfx.Node, scale float64, bg gfx.Color) ([]byte, error)
- func RegisterFonts(dir string) (int, error)
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
Default returns the renderer of the process, made on first use with the zero Options. Use it where one renderer is enough; it is safe for concurrent use.
func New ¶
New returns the cairo and Pango renderer; it fails when those libraries are not installed.
Example ¶
New is for a fixed area, a Raster, or font registration; Default shares one renderer.
package main
import (
"log"
"github.com/haiodo/gowt/gfx"
"github.com/haiodo/gowt/gfx/offscreen"
)
func main() {
r, err := offscreen.New(offscreen.Options{FontDir: "fonts"})
if err != nil {
log.Fatal(err) // no cairo and Pango on this Linux, or an OS without a backend
}
pic := gfx.NewRecorder()
pic.Text("Hi", 0, 12, gfx.Font{Size: 14}, gfx.Black)
raster, err := gfx.RenderPicture(r, pic.Finish(), 1, gfx.White)
if err != nil {
log.Fatal(err)
}
_ = raster.Image() // an *image.NRGBA
}
Output:
func NewPDF ¶
NewPDF starts a PDF file written to w by Close, with a page for each NewPage call: CoreGraphics on macOS, cairo on Linux, the PDF writer of gfx/pdf on Windows.
func PDF ¶
PDF writes root and its subtree (the whole scene when root is nil) as a one-page PDF; o sizes the page and fits the subtree on it. Text stays text and shapes stay vector.
func PNG ¶
PNG draws root and its subtree (the whole scene when root is nil) at scale device pixels per scene unit on the background bg and returns the PNG file. The image has the size of the subtree bounds; the zero Color as bg leaves it transparent. It commits the scene's pending changes first.
Example ¶
One call from a scene to the bytes of a PNG, on any OS, without a window.
package main
import (
"log"
"os"
"github.com/haiodo/gowt/gfx"
"github.com/haiodo/gowt/gfx/offscreen"
)
func main() {
rec := gfx.NewRecorder()
rec.FillRRect(gfx.R(0, 0, 160, 80), 10, gfx.RGB(30, 120, 220))
rec.Text("Hello", 14, 46, gfx.Font{Size: 16}, gfx.White)
n := gfx.NewNode(0)
scene := gfx.NewScene()
scene.Update(func(tx *gfx.Tx) {
tx.Add(nil, n)
tx.SetPicture(n, rec.Finish())
})
png, err := offscreen.PNG(scene, n, 2, gfx.White) // nil instead of n: the whole scene
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("hello.png", png, 0o644); err != nil {
log.Fatal(err)
}
}
Output:
func RegisterFonts ¶
RegisterFonts registers the font files of dir (or the one file) for this process only and returns how many were new; call it before the first text is measured.
Types ¶
type Options ¶
type Options struct {
// LOD skips text, shadows and dashes that are unreadable at the scale drawn.
LOD bool
// FontDir is a directory (or one file) of fonts to register for this process; the environment
// variable GFX_FONT_DIR stands in when it is empty.
FontDir string
}
Options are the settings every backend understands.