draw2dsvg

package
v0.0.0-...-0ed1ff1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: BSD-2-Clause Imports: 19 Imported by: 13

Documentation

Overview

Package draw2svg provides a graphic context that can draw vector graphics and text on svg file.

Quick Start The following Go code geneartes a simple drawing and saves it to a svg document: TODO

NOTE that this is identical copy of draw2dgl/text.go and draw2dimg/text.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrawContour

func DrawContour(path draw2d.PathBuilder, ps []truetype.Point, dx, dy float64)

DrawContour draws the given closed contour at the given sub-pixel offset.

func SaveToSvgFile

func SaveToSvgFile(filePath string, svg *Svg) error

func WriteSvg

func WriteSvg(w io.Writer, svg *Svg) error

Types

type Dimension

type Dimension struct {
	Width  string `xml:"width,attr"`
	Height string `xml:"height,attr"`
}

type Face

type Face struct {
	Family    string  `xml:"font-family,attr"`
	Units     int     `xml:"units-per-em,attr"`
	HorizAdvX float64 `xml:"horiz-adv-x,attr"`
}

type FillStroke

type FillStroke struct {
	Fill     string `xml:"fill,attr,omitempty"`
	FillRule string `xml:"fill-rule,attr,omitempty"`

	Stroke           string `xml:"stroke,attr,omitempty"`
	StrokeWidth      string `xml:"stroke-width,attr,omitempty"`
	StrokeLinecap    string `xml:"stroke-linecap,attr,omitempty"`
	StrokeLinejoin   string `xml:"stroke-linejoin,attr,omitempty"`
	StrokeDasharray  string `xml:"stroke-dasharray,attr,omitempty"`
	StrokeDashoffset string `xml:"stroke-dashoffset,attr,omitempty"`
}

type Font

type Font struct {
	Identity
	Face   *Face    `xml:"font-face"`
	Glyphs []*Glyph `xml:"glyph"`
}

type FontExtents

type FontExtents struct {
	// Ascent is the distance that the text
	// extends above the baseline.
	Ascent float64

	// Descent is the distance that the text
	// extends below the baseline.  The descent
	// is given as a negative value.
	Descent float64

	// Height is the distance from the lowest
	// descending point to the highest ascending
	// point.
	Height float64
}

FontExtents contains font metric information.

func Extents

func Extents(font *truetype.Font, size float64) FontExtents

Extents returns the FontExtents for a font. TODO needs to read this https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/Chap2.html#intro

type FontMode

type FontMode int
const (
	// Does nothing special
	// Makes sense only for common system fonts
	SysFontMode FontMode = 1 << iota

	// Links font files in css def
	// Requires distribution of font files with outputed svg
	LinkFontMode // TODO implement

	// Embeds glyphs definition in svg file itself in svg font format
	// Has poor browser support
	SvgFontMode

	// Embeds font definiton in svg file itself in woff format as part of css def
	CssFontMode // TODO implement

	// Converts texts to paths
	PathFontMode
)

Modes of font handling in svg

type Glyph

type Glyph struct {
	Rune      Rune    `xml:"unicode,attr"`
	Desc      string  `xml:"d,attr"`
	HorizAdvX float64 `xml:"horiz-adv-x,attr"`
}

type GraphicContext

type GraphicContext struct {
	*draw2dbase.StackGraphicContext
	FontCache draw2d.FontCache

	DPI int
	// contains filtered or unexported fields
}

GraphicContext implements the draw2d.GraphicContext interface It provides draw2d with a svg backend

func NewGraphicContext

func NewGraphicContext(svg *Svg) *GraphicContext

func (*GraphicContext) Clear

func (gc *GraphicContext) Clear()

Clear fills the current canvas with a default transparent color

func (*GraphicContext) ClearRect

func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int)

ClearRect fills the specified rectangle with a default transparent color

func (*GraphicContext) CreateStringPath

func (gc *GraphicContext) CreateStringPath(s string, x, y float64) (cursor float64)

CreateStringPath creates a path from the string s at x, y, and returns the string width. The text is placed so that the left edge of the em square of the first character of s and the baseline intersect at x, y. The majority of the affected pixels will be above and to the right of the point, but some may be below or to the left. For example, drawing a string that starts with a 'J' in an italic font may affect pixels below and left of the point.

func (*GraphicContext) DrawImage

func (gc *GraphicContext) DrawImage(image image.Image)

DrawImage draws the raster image in the current canvas

func (*GraphicContext) Fill

func (gc *GraphicContext) Fill(paths ...*draw2d.Path)

Fill fills the paths with the color specified by SetFillColor

func (*GraphicContext) FillString

func (gc *GraphicContext) FillString(text string) (cursor float64)

FillString draws the text at point (0, 0)

func (*GraphicContext) FillStringAt

func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64)

FillStringAt draws the text at the specified point (x, y)

func (*GraphicContext) FillStroke

func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path)

FillStroke first fills the paths and than strokes them

func (*GraphicContext) GetDPI

func (gc *GraphicContext) GetDPI() int

func (*GraphicContext) GetStringBounds

func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64)

GetStringBounds returns the approximate pixel bounds of the string s at x, y. The the left edge of the em square of the first character of s and the baseline intersect at 0, 0 in the returned coordinates. Therefore the top and left coordinates may well be negative.

func (*GraphicContext) Restore

func (gc *GraphicContext) Restore()

Restore remove the current context and restore the last one

func (*GraphicContext) Save

func (gc *GraphicContext) Save()

Save the context and push it to the context stack

func (*GraphicContext) SetDPI

func (gc *GraphicContext) SetDPI(dpi int)

func (*GraphicContext) SetFont

func (gc *GraphicContext) SetFont(font *truetype.Font)

SetFont sets the font used to draw text.

func (*GraphicContext) SetFontSize

func (gc *GraphicContext) SetFontSize(fontSize float64)

SetFontSize sets the font size in points (as in “a 12 point font”).

func (*GraphicContext) Stroke

func (gc *GraphicContext) Stroke(paths ...*draw2d.Path)

Stroke strokes the paths with the color specified by SetStrokeColor

func (*GraphicContext) StrokeString

func (gc *GraphicContext) StrokeString(text string) (cursor float64)

StrokeString draws the contour of the text at point (0, 0)

func (*GraphicContext) StrokeStringAt

func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64)

StrokeStringAt draws the contour of the text at point (x, y)

type Group

type Group struct {
	FillStroke
	Transform string   `xml:"transform,attr,omitempty"`
	Groups    []*Group `xml:"g"`
	Paths     []*Path  `xml:"path"`
	Texts     []*Text  `xml:"text"`
	Image     *Image   `xml:"image"`
	Mask      string   `xml:"mask,attr,omitempty"`
}

type Identity

type Identity struct {
	Id   string `xml:"id,attr"`
	Name string `xml:"name,attr"`
}

type Image

type Image struct {
	Position
	Dimension
	Href string `xml:"href,attr"`
}

type Mask

type Mask struct {
	Identity
	Position
	Dimension
}

func (Mask) MarshalXML

func (m Mask) MarshalXML(e *xml.Encoder, start xml.StartElement) error

type Path

type Path struct {
	FillStroke
	Desc string `xml:"d,attr"`
}

type Position

type Position struct {
	X float64 `xml:"x,attr,omitempty"`
	Y float64 `xml:"y,attr,omitempty"`
}

type Rect

type Rect struct {
	Position
	Dimension
	FillStroke
}

type Rune

type Rune rune

func (Rune) MarshalXMLAttr

func (r Rune) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

type Svg

type Svg struct {
	XMLName  xml.Name `xml:"svg"`
	Xmlns    string   `xml:"xmlns,attr"`
	Width    string   `xml:"width,attr,omitempty"`
	Height   string   `xml:"height,attr,omitempty"`
	ViewBox  string   `xml:"viewBox,attr,omitempty"`
	Fonts    []*Font  `xml:"defs>font"`
	Masks    []*Mask  `xml:"defs>mask"`
	Groups   []*Group `xml:"g"`
	FontMode FontMode `xml:"-"`
	FillStroke
}

func NewSvg

func NewSvg() *Svg

type Text

type Text struct {
	FillStroke
	Position
	FontSize   float64 `xml:"font-size,attr,omitempty"`
	FontFamily string  `xml:"font-family,attr,omitempty"`
	Text       string  `xml:",innerxml"`
	Style      string  `xml:"style,attr,omitempty"`
}

Jump to

Keyboard shortcuts

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