iconvg

package
v0.0.0-...-04b2079 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: BSD-3-Clause Imports: 10 Imported by: 34

Documentation

Overview

Package iconvg implements a compact, binary format for simple vector graphics: icons, logos, glyphs and emoji.

WARNING: THIS FORMAT IS EXPERIMENTAL AND SUBJECT TO INCOMPATIBLE CHANGES.

A longer overview is at https://github.com/google/iconvg

The file format is specified at https://github.com/google/iconvg/blob/main/spec/iconvg-spec.md

This package's encoder emits byte-identical output for the same input, independent of the platform (and specifically its floating-point hardware).

Example
package main

import (
	"image"
	"image/draw"
	"log"
	"os"
	"path/filepath"

	"golang.org/x/exp/shiny/iconvg"
)

func main() {
	ivgData, err := os.ReadFile(filepath.FromSlash("testdata/action-info.lores.ivg"))
	if err != nil {
		log.Fatal(err)
	}

	const width = 24
	dst := image.NewAlpha(image.Rect(0, 0, width, width))
	var z iconvg.Rasterizer
	z.SetDstImage(dst, dst.Bounds(), draw.Src)
	if err := iconvg.Decode(&z, ivgData, nil); err != nil {
		log.Fatal(err)
	}

	const asciiArt = ".++8"
	buf := make([]byte, 0, width*(width+1))
	for y := 0; y < width; y++ {
		for x := 0; x < width; x++ {
			a := dst.AlphaAt(x, y).A
			buf = append(buf, asciiArt[a>>6])
		}
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)

}
Output:

........................
........................
........++8888++........
......+8888888888+......
.....+888888888888+.....
....+88888888888888+....
...+8888888888888888+...
...88888888..88888888...
..+88888888..88888888+..
..+888888888888888888+..
..88888888888888888888..
..888888888..888888888..
..888888888..888888888..
..888888888..888888888..
..+88888888..88888888+..
..+88888888..88888888+..
...88888888..88888888...
...+8888888888888888+...
....+88888888888888+....
.....+888888888888+.....
......+8888888888+......
........++8888++........
........................
........................

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPalette = Palette{
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
	color.RGBA{0x00, 0x00, 0x00, 0xff},
}

DefaultPalette is the default Palette. Its values should not be modified.

View Source
var DefaultViewBox = Rectangle{
	Min: f32.Vec2{-32, -32},
	Max: f32.Vec2{+32, +32},
}

DefaultViewBox is the default ViewBox. Its values should not be modified.

Functions

func Decode

func Decode(dst Destination, src []byte, opts *DecodeOptions) error

Decode decodes an IconVG graphic.

func UpgradeToFileFormatVersion1

func UpgradeToFileFormatVersion1(v0 []byte, opts *UpgradeToFileFormatVersion1Options) (v1 []byte, retErr error)

UpgradeToFileFormatVersion1 upgrades IconVG data from the 2016 experimental "File Format Version 0" to the 2021 "File Format Version 1".

This package (golang.org/x/exp/shiny/iconvg) holds a decoder for FFV0, including this function to convert from FFV0 to FFV1. Different packages (github.com/google/iconvg/src/go/*) decode FFV1.

Amongst some new features and other clean-ups, FFV1 sets up the capability for animated vector graphics, therefore removing some FFV0 features (such as arc segments) that can be hard to animate smoothly. The IconvG FFV1 format and its design decisions are discussed at https://github.com/google/iconvg/issues/4#issuecomment-874105547

Types

type Color

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

Color is an IconVG color, whose RGBA values can depend on context. Some Colors are direct RGBA values. Other Colors are indirect, referring to an index of the custom palette, a color register of the decoder virtual machine, or a blend of two other Colors.

See the "Colors" section in the package documentation for details.

func BlendColor

func BlendColor(t, c0, c1 uint8) Color

BlendColor returns an indirect Color that blends two other Colors. Those two other Colors must both be encodable as a 1 byte color.

To blend a Color that is not encodable as a 1 byte color, first load that Color into a CREG color register, then call CRegColor to produce a Color that is encodable as a 1 byte color. See testdata/favicon.ivg for an example.

See the "Colors" section in the package documentation for details.

func CRegColor

func CRegColor(i uint8) Color

CRegColor returns an indirect Color referring to a color register of the decoder virtual machine.

func PaletteIndexColor

func PaletteIndexColor(i uint8) Color

PaletteIndexColor returns an indirect Color referring to an index of the custom palette.

func RGBAColor

func RGBAColor(c color.RGBA) Color

RGBAColor returns a direct Color.

func (Color) Resolve

func (c Color) Resolve(pal *Palette, cReg *[64]color.RGBA) color.RGBA

Resolve resolves the Color's RGBA value, given its context: the custom palette and the color registers of the decoder virtual machine.

type ColorType

type ColorType uint8

ColorType distinguishes types of Colors.

const (
	// ColorTypeRGBA is a direct RGBA color.
	ColorTypeRGBA ColorType = iota

	// ColorTypePaletteIndex is an indirect color, indexing the custom palette.
	ColorTypePaletteIndex

	// ColorTypeCReg is an indirect color, indexing the CREG color registers.
	ColorTypeCReg

	// ColorTypeBlend is an indirect color, blending two other colors.
	ColorTypeBlend
)

type DecodeOptions

type DecodeOptions struct {
	// Palette is an optional 64 color palette. If one isn't provided, the
	// IconVG graphic's suggested palette will be used.
	Palette *Palette
}

DecodeOptions are the optional parameters to the Decode function.

type Destination

type Destination interface {
	Reset(m Metadata)

	SetCSel(cSel uint8)
	SetNSel(nSel uint8)
	SetCReg(adj uint8, incr bool, c Color)
	SetNReg(adj uint8, incr bool, f float32)
	SetLOD(lod0, lod1 float32)

	StartPath(adj uint8, x, y float32)
	ClosePathEndPath()
	ClosePathAbsMoveTo(x, y float32)
	ClosePathRelMoveTo(x, y float32)

	AbsHLineTo(x float32)
	RelHLineTo(x float32)
	AbsVLineTo(y float32)
	RelVLineTo(y float32)
	AbsLineTo(x, y float32)
	RelLineTo(x, y float32)
	AbsSmoothQuadTo(x, y float32)
	RelSmoothQuadTo(x, y float32)
	AbsQuadTo(x1, y1, x, y float32)
	RelQuadTo(x1, y1, x, y float32)
	AbsSmoothCubeTo(x2, y2, x, y float32)
	RelSmoothCubeTo(x2, y2, x, y float32)
	AbsCubeTo(x1, y1, x2, y2, x, y float32)
	RelCubeTo(x1, y1, x2, y2, x, y float32)
	AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
	RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)
}

Destination handles the actions decoded from an IconVG graphic's opcodes.

When passed to Decode, the first method called (if any) will be Reset. No methods will be called at all if an error is encountered in the encoded form before the metadata is fully decoded.

type Encoder

type Encoder struct {
	// HighResolutionCoordinates is whether the encoder should encode
	// coordinate numbers for subsequent paths at the best possible resolution
	// afforded by the underlying graphic format.
	//
	// By default (false), the encoder quantizes coordinates to 1/64th of a
	// unit if possible (the default graphic size is 64 by 64 units, so
	// 1/4096th of the default width or height). Each such coordinate can
	// therefore be encoded in either 1 or 2 bytes. If true, some coordinates
	// will be encoded in 4 bytes, giving greater accuracy but larger file
	// sizes. On the Material Design icon set, the 950 or so icons take up
	// around 40% more bytes (172K vs 123K) at high resolution.
	//
	// See the package documentation for more details on the coordinate number
	// encoding format.
	HighResolutionCoordinates bool
	// contains filtered or unexported fields
}

Encoder is an IconVG encoder.

The zero value is usable. Calling Reset, which is optional, sets the Metadata for the subsequent encoded form. If Reset is not called before other Encoder methods, the default metadata is implied.

It aims to emit byte-identical Bytes output for the same input, independent of the platform (and specifically its floating-point hardware).

func (*Encoder) AbsArcTo

func (e *Encoder) AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)

func (*Encoder) AbsCubeTo

func (e *Encoder) AbsCubeTo(x1, y1, x2, y2, x, y float32)

func (*Encoder) AbsHLineTo

func (e *Encoder) AbsHLineTo(x float32)

func (*Encoder) AbsLineTo

func (e *Encoder) AbsLineTo(x, y float32)

func (*Encoder) AbsQuadTo

func (e *Encoder) AbsQuadTo(x1, y1, x, y float32)

func (*Encoder) AbsSmoothCubeTo

func (e *Encoder) AbsSmoothCubeTo(x2, y2, x, y float32)

func (*Encoder) AbsSmoothQuadTo

func (e *Encoder) AbsSmoothQuadTo(x, y float32)

func (*Encoder) AbsVLineTo

func (e *Encoder) AbsVLineTo(y float32)

func (*Encoder) Bytes

func (e *Encoder) Bytes() ([]byte, error)

Bytes returns the encoded form.

func (*Encoder) CSel

func (e *Encoder) CSel() uint8

func (*Encoder) ClosePathAbsMoveTo

func (e *Encoder) ClosePathAbsMoveTo(x, y float32)

func (*Encoder) ClosePathEndPath

func (e *Encoder) ClosePathEndPath()

func (*Encoder) ClosePathRelMoveTo

func (e *Encoder) ClosePathRelMoveTo(x, y float32)

func (*Encoder) LOD

func (e *Encoder) LOD() (lod0, lod1 float32)

func (*Encoder) NSel

func (e *Encoder) NSel() uint8

func (*Encoder) RelArcTo

func (e *Encoder) RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)

func (*Encoder) RelCubeTo

func (e *Encoder) RelCubeTo(x1, y1, x2, y2, x, y float32)

func (*Encoder) RelHLineTo

func (e *Encoder) RelHLineTo(x float32)

func (*Encoder) RelLineTo

func (e *Encoder) RelLineTo(x, y float32)

func (*Encoder) RelQuadTo

func (e *Encoder) RelQuadTo(x1, y1, x, y float32)

func (*Encoder) RelSmoothCubeTo

func (e *Encoder) RelSmoothCubeTo(x2, y2, x, y float32)

func (*Encoder) RelSmoothQuadTo

func (e *Encoder) RelSmoothQuadTo(x, y float32)

func (*Encoder) RelVLineTo

func (e *Encoder) RelVLineTo(y float32)

func (*Encoder) Reset

func (e *Encoder) Reset(m Metadata)

Reset resets the Encoder for the given Metadata.

This includes setting e.HighResolutionCoordinates to false.

func (*Encoder) SetCReg

func (e *Encoder) SetCReg(adj uint8, incr bool, c Color)

func (*Encoder) SetCSel

func (e *Encoder) SetCSel(cSel uint8)

func (*Encoder) SetCircularGradient

func (e *Encoder) SetCircularGradient(cBase, nBase uint8, cx, cy, rx, ry float32, spread GradientSpread, stops []GradientStop)

SetCircularGradient is like SetGradient with radial=true except that the transformation matrix is implicitly defined by a center (cx, cy) and a radius vector (rx, ry) such that (cx+rx, cy+ry) is on the circle.

func (*Encoder) SetEllipticalGradient

func (e *Encoder) SetEllipticalGradient(cBase, nBase uint8, cx, cy, rx, ry, sx, sy float32, spread GradientSpread, stops []GradientStop)

SetEllipticalGradient is like SetGradient with radial=true except that the transformation matrix is implicitly defined by a center (cx, cy) and two axis vectors (rx, ry) and (sx, sy) such that (cx+rx, cy+ry) and (cx+sx, cy+sy) are on the ellipse.

func (*Encoder) SetGradient

func (e *Encoder) SetGradient(cBase, nBase uint8, radial bool, transform f32.Aff3, spread GradientSpread, stops []GradientStop)

SetGradient sets CREG[CSEL] to encode the gradient whose colors defined by spread and stops. Its geometry is either linear or radial, depending on the radial argument, and the given affine transformation matrix maps from graphic coordinate space defined by the metadata's viewBox (e.g. from (-32, -32) to (+32, +32)) to gradient coordinate space. Gradient coordinate space is where a linear gradient ranges from x=0 to x=1, and a radial gradient has center (0, 0) and radius 1.

The colors of the n stops are encoded at CREG[cBase+0], CREG[cBase+1], ..., CREG[cBase+n-1]. Similarly, the offsets of the n stops are encoded at NREG[nBase+0], NREG[nBase+1], ..., NREG[nBase+n-1]. Additional parameters are stored at NREG[nBase-4], NREG[nBase-3], NREG[nBase-2] and NREG[nBase-1].

The CSEL and NSEL selector registers maintain the same values after the method returns as they had when the method was called.

See the package documentation for more details on the gradient encoding format and the derivation of common transformation matrices.

func (*Encoder) SetLOD

func (e *Encoder) SetLOD(lod0, lod1 float32)

func (*Encoder) SetLinearGradient

func (e *Encoder) SetLinearGradient(cBase, nBase uint8, x1, y1, x2, y2 float32, spread GradientSpread, stops []GradientStop)

SetLinearGradient is like SetGradient with radial=false except that the transformation matrix is implicitly defined by two boundary points (x1, y1) and (x2, y2).

func (*Encoder) SetNReg

func (e *Encoder) SetNReg(adj uint8, incr bool, f float32)

func (*Encoder) SetNSel

func (e *Encoder) SetNSel(nSel uint8)

func (*Encoder) StartPath

func (e *Encoder) StartPath(adj uint8, x, y float32)

type GradientSpread

type GradientSpread uint8

GradientSpread is how to spread a gradient past its nominal bounds (from offset being 0.0 to offset being 1.0).

const (
	GradientSpreadNone    GradientSpread = 0
	GradientSpreadPad     GradientSpread = 1
	GradientSpreadReflect GradientSpread = 2
	GradientSpreadRepeat  GradientSpread = 3
)

type GradientStop

type GradientStop struct {
	Offset float32
	Color  color.Color
}

GradientStop is a color/offset gradient stop.

type Metadata

type Metadata struct {
	ViewBox Rectangle

	// Palette is a 64 color palette. When encoding, it is the suggested
	// palette to place within the IconVG graphic. When decoding, it is either
	// the optional palette passed to Decode, or if no optional palette was
	// given, the suggested palette within the IconVG graphic.
	Palette Palette
}

Metadata is an IconVG's metadata.

func DecodeMetadata

func DecodeMetadata(src []byte) (m Metadata, err error)

DecodeMetadata decodes only the metadata in an IconVG graphic.

type Palette

type Palette [64]color.RGBA

Palette is an IconVG palette.

type Rasterizer

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

Rasterizer is a Destination that draws an IconVG graphic onto a raster image.

The zero value is usable, in that it has no raster image to draw onto, so that calling Decode with this Destination is a no-op (other than checking the encoded form for errors in the byte code). Call SetDstImage to change the raster image, before calling Decode or between calls to Decode.

func (*Rasterizer) AbsArcTo

func (z *Rasterizer) AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)

func (*Rasterizer) AbsCubeTo

func (z *Rasterizer) AbsCubeTo(x1, y1, x2, y2, x, y float32)

func (*Rasterizer) AbsHLineTo

func (z *Rasterizer) AbsHLineTo(x float32)

func (*Rasterizer) AbsLineTo

func (z *Rasterizer) AbsLineTo(x, y float32)

func (*Rasterizer) AbsQuadTo

func (z *Rasterizer) AbsQuadTo(x1, y1, x, y float32)

func (*Rasterizer) AbsSmoothCubeTo

func (z *Rasterizer) AbsSmoothCubeTo(x2, y2, x, y float32)

func (*Rasterizer) AbsSmoothQuadTo

func (z *Rasterizer) AbsSmoothQuadTo(x, y float32)

func (*Rasterizer) AbsVLineTo

func (z *Rasterizer) AbsVLineTo(y float32)

func (*Rasterizer) ClosePathAbsMoveTo

func (z *Rasterizer) ClosePathAbsMoveTo(x, y float32)

func (*Rasterizer) ClosePathEndPath

func (z *Rasterizer) ClosePathEndPath()

func (*Rasterizer) ClosePathRelMoveTo

func (z *Rasterizer) ClosePathRelMoveTo(x, y float32)

func (*Rasterizer) RelArcTo

func (z *Rasterizer) RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32)

func (*Rasterizer) RelCubeTo

func (z *Rasterizer) RelCubeTo(x1, y1, x2, y2, x, y float32)

func (*Rasterizer) RelHLineTo

func (z *Rasterizer) RelHLineTo(x float32)

func (*Rasterizer) RelLineTo

func (z *Rasterizer) RelLineTo(x, y float32)

func (*Rasterizer) RelQuadTo

func (z *Rasterizer) RelQuadTo(x1, y1, x, y float32)

func (*Rasterizer) RelSmoothCubeTo

func (z *Rasterizer) RelSmoothCubeTo(x2, y2, x, y float32)

func (*Rasterizer) RelSmoothQuadTo

func (z *Rasterizer) RelSmoothQuadTo(x, y float32)

func (*Rasterizer) RelVLineTo

func (z *Rasterizer) RelVLineTo(y float32)

func (*Rasterizer) Reset

func (z *Rasterizer) Reset(m Metadata)

Reset resets the Rasterizer for the given Metadata.

func (*Rasterizer) SetCReg

func (z *Rasterizer) SetCReg(adj uint8, incr bool, c Color)

func (*Rasterizer) SetCSel

func (z *Rasterizer) SetCSel(cSel uint8)

func (*Rasterizer) SetDstImage

func (z *Rasterizer) SetDstImage(dst draw.Image, r image.Rectangle, drawOp draw.Op)

SetDstImage sets the Rasterizer to draw onto a destination image, given by dst and r, with the given compositing operator.

The IconVG graphic (which does not have a fixed size in pixels) will be scaled in the X and Y dimensions to fit the rectangle r. The scaling factors may differ in the two dimensions.

func (*Rasterizer) SetLOD

func (z *Rasterizer) SetLOD(lod0, lod1 float32)

func (*Rasterizer) SetNReg

func (z *Rasterizer) SetNReg(adj uint8, incr bool, f float32)

func (*Rasterizer) SetNSel

func (z *Rasterizer) SetNSel(nSel uint8)

func (*Rasterizer) StartPath

func (z *Rasterizer) StartPath(adj uint8, x, y float32)

type Rectangle

type Rectangle struct {
	Min, Max f32.Vec2
}

Rectangle is defined by its minimum and maximum coordinates.

func (*Rectangle) AspectRatio

func (r *Rectangle) AspectRatio() (dx, dy float32)

AspectRatio returns the Rectangle's aspect ratio. An IconVG graphic is scalable; these dimensions do not necessarily map 1:1 to pixels.

type UpgradeToFileFormatVersion1Options

type UpgradeToFileFormatVersion1Options struct {
	// ArcsExpandWithHighResolutionCoordinates is like the
	// Encoder.HighResolutionCoordinates field. It controls whether to favor
	// file size (false) or precision (true) when replacing File Format Version
	// 0's arcs with cubic Bézier curves.
	ArcsExpandWithHighResolutionCoordinates bool
}

UpgradeToFileFormatVersion1Options are the options to the UpgradeToFileFormatVersion1 function.

Directories

Path Synopsis
internal
gradient
Package gradient provides linear and radial gradient images.
Package gradient provides linear and radial gradient images.

Jump to

Keyboard shortcuts

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