rough

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 7 Imported by: 0

README

rough-go

rough-go is a native Go compatibility port of the Rough.js 4.6.6 rendering surface used by D2. It creates the same ordered drawing operations and SVG path descriptions without a JavaScript runtime.

The compatibility target is Rough.js 4.6.6 at commit 56a2762171b1294d643501e8d14f120db6b27bd7. The port covers D2's generator methods (line, rectangle, ellipse, linearPath, polygon, and SVG path) and the solid and zigzag option profiles D2 emits. Other public methods remain available for source compatibility, but arbitrary expert Rough.js options outside that profile are not an exact compatibility contract.

package main

import (
	"fmt"

	rough "github.com/d2lang/rough-go"
)

func main() {
	g := rough.NewGenerator(nil)
	d := g.Rectangle(0, 0, 120, 60, &rough.Options{
		Fill: rough.String("#f6d32d"),
		Seed: rough.Float64(1),
	})
	for _, path := range g.ToPaths(d) {
		fmt.Println(path.D)
	}
}

Pointer-valued option fields preserve the JavaScript distinction between an omitted option and an explicit zero. Rough.js 4.6.6's new dash, multi-stroke, vertex-preservation, fixed-decimal, and fill-roughness options are represented.

Compatibility tests

The regular suite is pure Go:

go test ./...
go test -race ./...
go vet ./...

The optional differential suite runs the Go port against the pristine Rough.js 4.6.6 browser bundle. Node.js is used only by the test oracle; the library has no JavaScript dependency.

ROUGH_GO_ROUGH_JS=/absolute/path/to/rough-4.6.6.js go test ./...

The release gate includes 1,000 deterministic randomized D2-profile calls and D2's full sketch SVG suite. Every rough-generated SVG path in that suite matches official Rough.js 4.6.6 after D2's normal six-decimal serialization.

License

MIT. See LICENSE and NOTICE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v0.2.0

func Bool(v bool) *bool

Bool returns a pointer suitable for a boolean Options field.

func Float64

func Float64(v float64) *float64

Float64 returns a pointer suitable for a numeric Options field.

func NewSeed

func NewSeed() float64

NewSeed returns a nondeterministic seed in Rough.js's supported range.

func NumberString

func NumberString(v float64) string

NumberString formats v the way JavaScript coerces a Number to a string. It is useful when surrounding SVG serialization must remain byte-compatible with Rough.js output.

func String

func String(v string) *string

String returns a pointer suitable for a string Options field.

Types

type Config

type Config struct {
	Options *Options `json:"options,omitempty"`
}

Config configures a Generator.

type Drawable

type Drawable struct {
	Shape   string
	Options *ResolvedOptions
	Sets    []OpSet
}

Drawable is the source-shaped output of Generator drawing methods.

type Generator

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

Generator creates Rough.js-compatible drawing operations.

A Generator retains the same mutable default-option random state as Rough.js. Its methods are safe for concurrent use, though callers that need independent deterministic streams should use separate generators.

func NewGenerator

func NewGenerator(config *Config, surface ...*Surface) *Generator

NewGenerator creates a generator. A nil config uses Rough.js 4.6.6's defaults. The optional surface supplies patterned-path fallback dimensions.

func (*Generator) Arc

func (g *Generator) Arc(x, y, width, height, start, stop float64, closed bool, options *Options) Drawable

Arc creates a rough elliptical arc.

func (*Generator) Circle

func (g *Generator) Circle(x, y, diameter float64, options *Options) Drawable

Circle creates a rough circle centered at x,y.

func (*Generator) Curve

func (g *Generator) Curve(points []Point, options *Options) Drawable

Curve creates a rough curve through points.

func (*Generator) DefaultOptions

func (g *Generator) DefaultOptions() ResolvedOptions

DefaultOptions returns a value copy of the generator's current defaults.

func (*Generator) Ellipse

func (g *Generator) Ellipse(x, y, width, height float64, options *Options) Drawable

Ellipse creates a rough ellipse centered at x,y.

func (*Generator) Line

func (g *Generator) Line(x1, y1, x2, y2 float64, options *Options) Drawable

Line creates a rough line.

func (*Generator) LinearPath

func (g *Generator) LinearPath(points []Point, options *Options) Drawable

LinearPath creates an open rough path through points.

func (*Generator) OpsToPath

func (g *Generator) OpsToPath(drawing OpSet) string

OpsToPath serializes operations using Rough.js's SVG punctuation and number formatting.

func (*Generator) Path

func (g *Generator) Path(pathData string, options *Options) Drawable

Path creates a rough rendering of SVG path data.

func (*Generator) Polygon

func (g *Generator) Polygon(points []Point, options *Options) Drawable

Polygon creates a closed rough polygon.

func (*Generator) Rectangle

func (g *Generator) Rectangle(x, y, width, height float64, options *Options) Drawable

Rectangle creates a rough rectangle.

func (*Generator) ToPaths

func (g *Generator) ToPaths(d Drawable) []PathInfo

ToPaths converts a Drawable into the ordered SVG path descriptions emitted by Rough.js's generator API.

type Line

type Line [2]Point

Line is a line segment represented by its two endpoints.

type Op

type Op struct {
	Op   OpType    `json:"op"`
	Data []float64 `json:"data"`
}

Op is one path operation and its ordered numeric arguments.

type OpSet

type OpSet struct {
	Type OpSetType `json:"type"`
	Ops  []Op      `json:"ops"`
	Size *Point    `json:"size,omitempty"`
	Path string    `json:"path,omitempty"`
}

OpSet is one ordered set of path operations.

type OpSetType

type OpSetType string

OpSetType identifies the rendering role of an operation set.

const (
	OpSetPath          OpSetType = "path"
	OpSetFillPath      OpSetType = "fillPath"
	OpSetFillSketch    OpSetType = "fillSketch"
	OpSetPath2DFill    OpSetType = "path2Dfill"
	OpSetPath2DPattern OpSetType = "path2Dpattern"
)

type OpType

type OpType string

OpType identifies a primitive path operation.

const (
	OpMove     OpType = "move"
	OpBCurveTo OpType = "bcurveTo"
	OpLineTo   OpType = "lineTo"
	OpQCurveTo OpType = "qcurveTo"
)

type Options

type Options struct {
	MaxRandomnessOffset     *float64  `json:"maxRandomnessOffset,omitempty"`
	Roughness               *float64  `json:"roughness,omitempty"`
	Bowing                  *float64  `json:"bowing,omitempty"`
	Stroke                  *string   `json:"stroke,omitempty"`
	StrokeWidth             *float64  `json:"strokeWidth,omitempty"`
	CurveFitting            *float64  `json:"curveFitting,omitempty"`
	CurveTightness          *float64  `json:"curveTightness,omitempty"`
	CurveStepCount          *float64  `json:"curveStepCount,omitempty"`
	Fill                    *string   `json:"fill,omitempty"`
	FillStyle               *string   `json:"fillStyle,omitempty"`
	FillWeight              *float64  `json:"fillWeight,omitempty"`
	HachureAngle            *float64  `json:"hachureAngle,omitempty"`
	HachureGap              *float64  `json:"hachureGap,omitempty"`
	Simplification          *float64  `json:"simplification,omitempty"`
	DashOffset              *float64  `json:"dashOffset,omitempty"`
	DashGap                 *float64  `json:"dashGap,omitempty"`
	ZigzagOffset            *float64  `json:"zigzagOffset,omitempty"`
	Seed                    *float64  `json:"seed,omitempty"`
	StrokeLineDash          []float64 `json:"strokeLineDash,omitempty"`
	StrokeLineDashOffset    *float64  `json:"strokeLineDashOffset,omitempty"`
	FillLineDash            []float64 `json:"fillLineDash,omitempty"`
	FillLineDashOffset      *float64  `json:"fillLineDashOffset,omitempty"`
	DisableMultiStroke      *bool     `json:"disableMultiStroke,omitempty"`
	DisableMultiStrokeFill  *bool     `json:"disableMultiStrokeFill,omitempty"`
	PreserveVertices        *bool     `json:"preserveVertices,omitempty"`
	FixedDecimalPlaceDigits *float64  `json:"fixedDecimalPlaceDigits,omitempty"`
	FillShapeRoughnessGain  *float64  `json:"fillShapeRoughnessGain,omitempty"`
}

Options contains optional Rough.js drawing options. Pointer fields preserve the distinction between an omitted option and an explicit zero value.

type PathInfo

type PathInfo struct {
	D           string
	Stroke      string
	StrokeWidth float64
	Fill        string
	Pattern     *PatternInfo
}

PathInfo is the SVG-oriented representation of an operation set.

type PatternInfo

type PatternInfo struct {
	X            float64
	Y            float64
	Width        float64
	Height       float64
	ViewBox      string
	PatternUnits string
	Path         PathInfo
}

PatternInfo describes the pattern used by a patterned SVG path fill.

type Point

type Point [2]float64

Point is a two-dimensional coordinate.

type Rectangle

type Rectangle struct {
	X      float64
	Y      float64
	Width  float64
	Height float64
}

Rectangle is an axis-aligned rectangle.

type ResolvedOptions

type ResolvedOptions struct {
	MaxRandomnessOffset     float64
	Roughness               float64
	Bowing                  float64
	Stroke                  string
	StrokeWidth             float64
	CurveFitting            float64
	CurveTightness          float64
	CurveStepCount          float64
	Fill                    string
	FillStyle               string
	FillWeight              float64
	HachureAngle            float64
	HachureGap              float64
	Simplification          float64
	DashOffset              float64
	DashGap                 float64
	ZigzagOffset            float64
	Seed                    float64
	StrokeLineDash          []float64
	StrokeLineDashOffset    float64
	FillLineDash            []float64
	FillLineDashOffset      float64
	DisableMultiStroke      bool
	DisableMultiStrokeFill  bool
	PreserveVertices        bool
	FixedDecimalPlaceDigits *float64
	FillShapeRoughnessGain  float64
	// RoughnessGain is retained for source compatibility with v0.1.0. Rough.js
	// 4.6.6 computes the gain per line and no longer mutates this field.
	RoughnessGain float64
	// contains filtered or unexported fields
}

ResolvedOptions is the complete option set used by the renderer.

type Surface

type Surface struct {
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

Surface supplies the fallback dimensions used for patterned SVG path fills.

Jump to

Keyboard shortcuts

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