offscreen

package
v0.16.4 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: EPL-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() (gfx.Renderer, error)

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

func New(o Options) (gfx.Renderer, error)

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
}

func NewPDF

func NewPDF(w io.Writer) (gfx.Document, error)

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

func PDF(w io.Writer, s *gfx.Scene, root *gfx.Node, o gfx.PDFOptions) error

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

func PNG(s *gfx.Scene, root *gfx.Node, scale float64, bg gfx.Color) ([]byte, error)

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)
	}
}

func RegisterFonts

func RegisterFonts(dir string) (int, error)

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.

Jump to

Keyboard shortcuts

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