svg

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 12 Imported by: 1

README

gg-svg

SVG export backend for gg's recording system.

Part of the GoGPU ecosystem.

Installation

go get github.com/gogpu/gg-svg

Usage

Import with a blank identifier to register the SVG backend:

import (
    "github.com/gogpu/gg/recording"
    _ "github.com/gogpu/gg-svg" // Register SVG backend
)

func main() {
    // Create a recorder
    rec := recording.NewRecorder(800, 600)

    // Draw something
    rec.SetFillRGBA(1, 0, 0, 1) // Red
    rec.DrawRectangle(100, 100, 200, 150)
    rec.Fill()

    // Finish recording
    r := rec.FinishRecording()

    // Create SVG backend
    backend, err := recording.NewBackend("svg")
    if err != nil {
        log.Fatal(err)
    }

    // Playback to SVG
    r.Playback(backend)

    // Save to file
    if fb, ok := backend.(recording.FileBackend); ok {
        fb.SaveToFile("output.svg")
    }
}

Features

  • Solid color fills and strokes
  • Linear and radial gradients (with spread modes)
  • Path operations (fill, stroke, clip)
  • Transformations (matrix)
  • Stroke styles (width, cap, join, dash patterns)
  • State management (Save/Restore via groups)
  • Text rendering
  • Image embedding (PNG data URI)
  • XML escaping for security

Output Format

The backend generates standard SVG 1.1 with:

  • XML declaration
  • SVG namespace
  • Definitions section for gradients and clip paths
  • Proper attribute encoding

Example output:

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="800" height="600" viewBox="0 0 800 600">
<defs>...</defs>
<path d="M100 100L300 100L300 250L100 250Z" fill="rgb(255,0,0)" stroke="none"/>
</svg>

Limitations

  • Sweep gradients fallback to first stop color (SVG limitation)
  • Text uses default font (custom font embedding not supported)
  • Images are embedded as PNG data URIs (increases file size)

License

MIT License - see LICENSE for details.

Documentation

Overview

Package svg provides an SVG export backend for gg's recording system.

This package registers an "svg" backend that can be used to export recorded drawing operations to SVG format.

Usage

Import this package with a blank identifier to register the SVG backend:

import _ "github.com/gogpu/gg-svg"

Then use recording.NewBackend("svg") to create an SVG backend:

backend, err := recording.NewBackend("svg")
if err != nil {
    log.Fatal(err)
}

// Record drawing operations
rec := recording.NewRecorder(800, 600)
// ... draw ...
r := rec.Finish()

// Playback to SVG backend
r.Playback(backend)

// Save to file
if fb, ok := backend.(recording.FileBackend); ok {
    fb.SaveToFile("output.svg")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

Backend implements recording.Backend for SVG output. It generates SVG XML from recorded drawing commands.

func NewBackend

func NewBackend() *Backend

NewBackend creates a new SVG backend. The backend starts in an uninitialized state. Call Begin() to initialize with specific dimensions before drawing.

func (*Backend) Begin

func (b *Backend) Begin(width, height int) error

Begin initializes the backend for rendering at the given dimensions.

func (*Backend) ClearClip

func (b *Backend) ClearClip()

ClearClip removes any clipping region.

func (*Backend) DrawImage

func (b *Backend) DrawImage(img image.Image, src, dst recording.Rect, opts recording.ImageOptions)

DrawImage draws an image from the source rectangle to the destination rectangle.

func (*Backend) DrawText

func (b *Backend) DrawText(s string, x, y float64, face text.Face, brush recording.Brush)

DrawText draws text at the given position with the specified font face and brush.

func (*Backend) End

func (b *Backend) End() error

End finalizes the rendering.

func (*Backend) FillPath

func (b *Backend) FillPath(path *gg.Path, brush recording.Brush, rule recording.FillRule)

FillPath fills the given path with the brush color/pattern.

func (*Backend) FillRect

func (b *Backend) FillRect(rect recording.Rect, brush recording.Brush)

FillRect fills an axis-aligned rectangle with the brush.

func (*Backend) Restore

func (b *Backend) Restore()

Restore restores the graphics state from the stack.

func (*Backend) Save

func (b *Backend) Save()

Save saves the current graphics state onto a stack.

func (*Backend) SaveToFile

func (b *Backend) SaveToFile(path string) error

SaveToFile saves the SVG to a file at the given path. This implements recording.FileBackend.

func (*Backend) SetClip

func (b *Backend) SetClip(path *gg.Path, rule recording.FillRule)

SetClip sets the clipping region to the given path.

func (*Backend) SetTransform

func (b *Backend) SetTransform(m recording.Matrix)

SetTransform sets the current transformation matrix.

func (*Backend) StrokePath

func (b *Backend) StrokePath(path *gg.Path, brush recording.Brush, stroke recording.Stroke)

StrokePath strokes the given path with the brush and stroke style.

func (*Backend) WriteTo

func (b *Backend) WriteTo(w io.Writer) (int64, error)

WriteTo writes the SVG to the given writer. This implements recording.WriterBackend.

Jump to

Keyboard shortcuts

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