gg

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package gg renders refract charts through github.com/gogpu/gg.

This is a separate Go module. Importing refract's core gets you a stdlib-only dependency graph and SVG output; importing this module adds the GoGPU stack and gets you raster output — still with zero CGO, so CGO_ENABLED=0 builds and cross-compiles exactly as before.

import ggbackend "github.com/timzifer/refract/backend/gg"

err := p.Render(ggbackend.PNG("chart.png"))

What this backend uses, and what it deliberately does not

It touches only the gg root package and gg/text. It does not import gg/gpu, gg/scene or gg/recording. That is a deliberate limit on the coupling surface (docs/adr/0006): gg is young and moves fast, so the narrower the adapter's contact with it, the cheaper it is to follow. It also means the CPU rasterizer is what runs — no GPU device is ever created, and CI needs no graphics hardware.

The GPU tier, PDF output and a native window are later milestones. When they arrive they will arrive here, behind the same ir.Backend interface.

Text

gg ships no default font, so this backend embeds Go Regular and Go Bold (golang.org/x/image/font/gofont, BSD-3-Clause) and uses them unless a font is supplied with WithFont. Measurement comes from the same face that draws the text, so metrics here are exact — unlike the built-in SVG backend, which approximates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JPEG

func JPEG(path string, opts ...Option) ir.Target

JPEG returns a target that writes a JPEG file.

func PNG

func PNG(path string, opts ...Option) ir.Target

PNG returns a target that writes a PNG file.

func Writer

func Writer(w io.Writer, format Format, opts ...Option) ir.Target

Writer returns a target that encodes into w in the given format.

Types

type Format

type Format uint8

Format is an output image format.

const (
	FormatPNG Format = iota
	FormatJPEG
)

The supported formats.

type Option

type Option func(*options)

Option configures a target.

func JPEGQuality

func JPEGQuality(q int) Option

JPEGQuality sets the JPEG encoder quality, 1 to 100. The default is 90.

func WithFont

func WithFont(regular, bold, italic []byte) Option

WithFont replaces the embedded Go fonts with supplied TrueType or OpenType files.

Pass bold or italic as nil to synthesise nothing and reuse regular for that style. Italic is worth supplying for a chart whose labels carry notation: a typesetter sets variables italic, and the vector emitters ask the viewer for an italic face whether or not one is given here.

type Surface added in v1.0.0

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

Surface is a target that draws into memory rather than into a file.

PNG and JPEG encode a chart and are done with it. A Surface keeps the pixels: it stays open, its image can be read after every frame, and it can be resized and repainted in place. That is what a window needs — see github.com/timzifer/refract/backend/window, which presents this image as a texture — and what any caller compositing a chart into a larger picture needs too.

s := gg.NewSurface()
live, err := p.Live(s)
// ... draw, resize, draw again ...
img := s.Image()

A Surface draws one chart at a time and is not safe for concurrent use.

func NewSurface added in v1.0.0

func NewSurface(opts ...Option) *Surface

NewSurface returns an in-memory target. It takes the same options as the file targets: the font is the one thing a raster backend has to be told about.

func (*Surface) Close added in v1.0.0

func (s *Surface) Close() error

Close releases the pixel buffer. The image is not available afterwards.

func (*Surface) Generation added in v1.0.0

func (s *Surface) Generation() uint64

Generation reports a counter that changes whenever the pixels do.

It answers "is this frame the same as the last one" without comparing two buffers, which is what lets a window skip re-uploading a chart nobody has touched. It is zero for a surface that has not been opened, and it is not a frame number: a frame that painted nothing does not advance it.

func (*Surface) Image added in v1.0.0

func (s *Surface) Image() image.Image

Image returns what has been drawn, or nil before the first Surface.Open.

The image is the surface's own buffer rather than a copy, so it is valid until the next frame overwrites it — which is what makes reading it every frame free. A caller keeping one past that must copy it.

func (*Surface) Open added in v1.0.0

func (s *Surface) Open(widthPx, heightPx int, dpr float64) (ir.Backend, error)

Open prepares the surface for a chart of the given size. Opening a surface that is already open replaces what it held.

func (*Surface) Size added in v1.0.0

func (s *Surface) Size() (w, h int, dpr float64)

Size reports the surface's logical size, and the device pixel ratio its buffer is scaled by.

Directories

Path Synopsis
cmd
gallery command
Command gallery renders every figure used in the README and the docs.
Command gallery renders every figure used in the README and the docs.
gpu module

Jump to

Keyboard shortcuts

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