graphbox

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SolidArrowStem draws a solid arrow stem
	SolidArrowStem ActivityArrowStem = iota

	// DashedArrowStem draws a dashed arrow stem
	DashedArrowStem = iota

	// ThickArrowStem draws a thick arrow stem
	ThickArrowStem = iota
)
View Source
const (
	LeftActorBox   ActorBoxPos = (iota << 8)
	MiddleActorBox             = (iota << 8)
	RightActorBox              = (iota << 8)
)

These flags are for the horizontal position

View Source
const (
	CenterNotePos NoteBoxPos = iota
	LeftNotePos              = iota
	RightNotePos             = iota
)
View Source
const (
	LeftTextAlign   TextAlign = iota
	MiddleTextAlign           = iota
	RightTextAlign            = iota
)
View Source
const (
	LINE_GAP = 2
)

Variables

View Source
var CloudPathData = PathIconData{
	Width:          365.0,
	Height:         185.0,
	TargetIconSize: 44.0,
	IconPadding:    -18.0,
	Path: `
		m299.75 60.587c-4.108 0-8.123 0.411-12.001 1.188-11.95-35.875-46.03-61.775-86.23-61.775-33.761
		0-63.196 18.27-78.847 45.363-0.919-0.036-1.84-0.07-2.769-0.07-23.494 0-44.202 11.816-56.409
		29.777-3.263-0.617-6.627-0.953-10.071-0.953-29.503 0-53.42 23.702-53.42 52.943 0 29.24 23.917
		52.94 53.421 52.94h66.435 0.045 0.045 81.525 0.045 0.047 98.145 0.046c33.28 0 60.25-26.73
		60.25-59.71 0-32.972-26.97-59.703-60.25-59.703z
	`,
}

CloudPathData represents the path data for the cloud icon. The image was adapted from the one at https://freesvg.org/simple-white-cloud-icon-vector-graphics

Functions

func MeasureFontRect

func MeasureFontRect(font Font, size int, text string, x, y int, gravity Gravity) (Rect, Point)

Given a font, font size, points and gravity, returns a rectangle which will contain the text centered. The point and gravity describes the location of the rect. The second point is where the text is to start given that it is to be rendered to fill the rectangle with default anchoring and alignment

Types

type ActivityArrowStem

type ActivityArrowStem int

ActivityArrowStem is the type of arrow stem to use for activity arrows

type ActivityLine

type ActivityLine struct {
	TC int
	// contains filtered or unexported fields
}

ActivityLine is an activity line graphical object

func NewActivityLine

func NewActivityLine(toCol int, selfRef bool, text string, style ActivityLineStyle) *ActivityLine

NewActivityLine constructs a new ActivityLine

func (*ActivityLine) Constraint

func (al *ActivityLine) Constraint(r, c int, applier ConstraintApplier)

Constraint returns the constraints of the graphics object

func (*ActivityLine) Draw

func (al *ActivityLine) Draw(ctx DrawContext, point Point)

Draw draws the graphics object

type ActivityLineStyle

type ActivityLineStyle struct {
	Font          Font
	FontSize      int
	Margin        Point
	TextGap       int
	SelfRefWidth  int
	SelfRefHeight int
	//ArrowHead       ActivityArrowHead
	ArrowHead *ArrowHeadStyle
	ArrowStem ActivityArrowStem
}

ActivityLineStyle defines the style to use for an activity line

type ActorBox

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

ActorBox represents an a actor

func NewActorBox

func NewActorBox(text string, style ActorBoxStyle, pos ActorBoxPos) *ActorBox

NewActorBox returns a new actor

func (*ActorBox) Constraint

func (tr *ActorBox) Constraint(r, c int, applier ConstraintApplier)

func (*ActorBox) Draw

func (r *ActorBox) Draw(ctx DrawContext, point Point)

type ActorBoxPos

type ActorBoxPos int

ActorBoxPos is used to manage the flags representing the actor boxes position

const (
	TopActorBox    ActorBoxPos = iota
	BottomActorBox             = iota
)

These flags are for the vertical position

type ActorBoxStyle

type ActorBoxStyle struct {
	Font      Font
	FontSize  int
	Padding   Point
	Margin    Point
	Color     string
	TextColor string
}

ActorBoxStyle defines styling options for the actor boxes

type ActorIconBox

type ActorIconBox struct {
	Icon Icon
	// contains filtered or unexported fields
}

ActorIconBox represents an actor icon

func NewActorIconBox

func NewActorIconBox(text string, icon Icon, style ActorIconBoxStyle, pos ActorBoxPos) *ActorIconBox

NewActorIconBox constructs a new actor icon

func (*ActorIconBox) Constraint

func (tr *ActorIconBox) Constraint(r, c int, applier ConstraintApplier)

func (*ActorIconBox) Draw

func (tr *ActorIconBox) Draw(ctx DrawContext, point Point)

type ActorIconBoxStyle

type ActorIconBoxStyle struct {
	Font      Font
	FontSize  int
	Padding   Point
	Margin    Point
	IconGap   int
	Color     string
	TextColor string
}

ActorIconBoxStyle defines styling options for an actor icon

type AddSizeConstraint

type AddSizeConstraint struct {
	R, C        int
	Left, Right int
	Top, Bottom int
}

Adds a size constraint which requests a minimum gap between points

func (AddSizeConstraint) Apply

func (sc AddSizeConstraint) Apply(cm ConstraintChanger)

type ArrowHeadStyle

type ArrowHeadStyle struct {
	// Points from the origin
	Xs []int
	Ys []int

	// Base style for the arrow head
	BaseStyle string
}

ArrowHeadStyle defines style information for the arrow heads

type Block

type Block struct {
	TR int
	TC int

	MarginMup   int
	IsLast      bool
	ShowPrefix  bool
	ShowMessage bool
	Style       BlockStyle
	// contains filtered or unexported fields
}

A block

func NewBlock

func NewBlock(toRow int, toCol int, marginMup int, isLast bool, prefix string, showPrefix bool, text string, style BlockStyle) *Block

func (*Block) Constraint

func (block *Block) Constraint(r, c int, applier ConstraintApplier)

func (*Block) Draw

func (block *Block) Draw(ctx DrawContext, point Point)

type BlockStyle

type BlockStyle struct {
	Margin Point

	Font     Font
	FontSize int

	TextPadding    Point
	MessagePadding Point

	PrefixExtraWidth int
	GapWidth         int
	MidMargin        int
}

A block stype

type BoxFrame

type BoxFrame struct {
	// The outer rectangle.  This encompasses margins, etc.
	OuterRect Rect

	// The inner rectangle.
	InnerRect Rect
}

A box frame

type Constraint

type Constraint interface {
	Apply(cm ConstraintChanger)
}

type ConstraintApplier

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

func (ConstraintApplier) Apply

func (ca ConstraintApplier) Apply(constraint Constraint)

func (ConstraintApplier) Cols

func (ca ConstraintApplier) Cols() int

type ConstraintChanger

type ConstraintChanger interface {

	// Number of columns in constraint changer
	Cols() int

	// Calculate the current size between the two grid points
	GridPointRect(fr, fc, tr, tc int) (int, int)

	// Ensure that the left side of this column has this much space.
	// Provide space if needed.
	EnsureLeftIsAtleast(col, newLeft int)

	// Ensure that the top side of this row has this much space.
	// Provide space if needed.
	EnsureTopIsAtLeast(row, newTop int)

	AddLeftToCol(col, newLeft int)

	AddTopToRow(row, newTop int)
}

type CylinderIcon

type CylinderIcon int

func (CylinderIcon) Draw

func (ci CylinderIcon) Draw(ctx DrawContext, x int, y int, lineStyle *SvgStyle)

func (CylinderIcon) Size

func (ci CylinderIcon) Size() (width int, height int)

type Divider

type Divider struct {
	TC int
	// contains filtered or unexported fields
}

Divider is a divider graphics object. This spans the entire diagram.

func NewDivider

func NewDivider(toCol int, text string, style DividerStyle) *Divider

NewDivider creates a new divider

func (*Divider) Constraint

func (div *Divider) Constraint(r, c int, applier ConstraintApplier)

Constraint returns the constraints of the graphics object

func (*Divider) Draw

func (div *Divider) Draw(ctx DrawContext, point Point)

Draw draws the graphics object

type DividerShape

type DividerShape int

DividerShape determines which shape to use for the divider

const (
	// DSFullRect is a rectangle which will span the entire graphic from end to end.
	// The text will be centered in front of it.
	DSFullRect DividerShape = iota

	// DSFramedRect is like FullRect but using a framed rectangle
	DSFramedRect

	// DSSpacerRect is like FullRect but "transparent".  If there is any text, it will be
	// blocked out.
	DSSpacerRect

	// DSFullLine is a line which will span the entire grapic.  The text will be
	// centered in front of it.
	DSFullLine
)

type DividerStyle

type DividerStyle struct {
	Font        Font
	FontSize    int
	Padding     Point
	Margin      Point
	TextPadding Point
	Overlap     int
	Shape       DividerShape
}

DividerStyle defines the style of the divider

type DrawContext

type DrawContext struct {
	Canvas  *svg.SVG
	Graphic *Graphic
	R, C    int
}

A drawing context

func (*DrawContext) PointAt

func (dc *DrawContext) PointAt(r, c int) (Point, bool)

Returns the outer rectangle of a particular cell

type Font

type Font interface {
	// Returns the appropriate name of this font in the SVG
	SvgName() string

	// Measures the size of the particular line of text
	Measure(txt string, size float64) (int, int)
}

type GraphboxItem

type GraphboxItem interface {
	// Defines a constraint.  It is provided with the coordinates
	// of the item.
	Constraint(r, c int, applier ConstraintApplier)

	// Call to draw this box
	Draw(ctx DrawContext, point Point)
}

type Graphic

type Graphic struct {

	// The margin between items
	Margin Point

	// Show the grid
	ShowGrid bool

	// If true, generate a 'viewport' attribute with the image size and
	// use percentages for the original image size
	Viewport bool
	// contains filtered or unexported fields
}

A graphbox diagram. This diagram is made up of uniform points. Each item on the diagram has the option of describing a constraint such as the amount of spacing around the point it requires.

func NewGraphic

func NewGraphic(rows, cols int) *Graphic

func (*Graphic) AddLeftToCol

func (g *Graphic) AddLeftToCol(col, newLeft int)

func (*Graphic) AddTopToRow

func (g *Graphic) AddTopToRow(row, newTop int)

func (*Graphic) Cols

func (g *Graphic) Cols() int

Returns the number of columns within the matrix

func (*Graphic) DrawSVG

func (g *Graphic) DrawSVG(w io.Writer)

Draws the graphics as an SVG

func (*Graphic) EnsureLeftIsAtleast

func (g *Graphic) EnsureLeftIsAtleast(col, newLeft int)

func (*Graphic) EnsureTopIsAtLeast

func (g *Graphic) EnsureTopIsAtLeast(row, newTop int)

func (*Graphic) GridPointRect

func (g *Graphic) GridPointRect(fr, fc, tr, tc int) (int, int)

Implementation of ConstrantModifier.

While updating constraints:

  • The first column is used to maintain Y deltas
  • The first row is used to maintain X deltas

TODO: Instead of using loops, add cheats like only using the first row/column

func (*Graphic) PointAt

func (g *Graphic) PointAt(r, c int) (Point, bool)

func (*Graphic) Put

func (g *Graphic) Put(r, c int, item GraphboxItem) bool

Sets a point in the matrix. If the point is beyond the scope of the matrix, returns false.

func (*Graphic) Rows

func (g *Graphic) Rows() int

Returns the number of items within the matrix

type Gravity

type Gravity func(w, h int) (int, int)

An anchor point located in a rectangle at 0, 0 with the w, h passed in

var CenterGravity Gravity = func(w, h int) (int, int) { return w / 2, h / 2 }
var EastGravity Gravity = func(w, h int) (int, int) { return w, h / 2 }
var NorthGravity Gravity = func(w, h int) (int, int) { return w / 2, 0 }
var NorthWestGravity Gravity = func(w, h int) (int, int) { return 0, 0 }
var SouthGravity Gravity = func(w, h int) (int, int) { return w / 2, h }
var SouthWestGravity Gravity = func(w, h int) (int, int) { return 0, h }
var WestGravity Gravity = func(w, h int) (int, int) { return 0, h / 2 }

func AtSpecificGravity

func AtSpecificGravity(fx, fy float64) Gravity

A specific gravity

type Icon

type Icon interface {

	// Return the size of the icon
	Size() (width int, height int)

	// Draw the icon onto the draw context centered at x and y
	Draw(ctx DrawContext, x int, y int, lineStyle *SvgStyle)
}

An icon which can be added to actors

type LifeLine

type LifeLine struct {
	TR, TC int
	Style  LifeLineStyle
}

The object lifeline

func (*LifeLine) Constraint

func (ll *LifeLine) Constraint(r, c int, applier ConstraintApplier)

func (*LifeLine) Draw

func (ll *LifeLine) Draw(ctx DrawContext, point Point)

type LifeLineStyle

type LifeLineStyle struct {
	Color string
}

type NoteBox

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

Draws an object instance

func NewNoteBox

func NewNoteBox(text string, style NoteBoxStyle, pos NoteBoxPos) *NoteBox

func (*NoteBox) Constraint

func (tr *NoteBox) Constraint(r, c int, applier ConstraintApplier)

func (*NoteBox) Draw

func (r *NoteBox) Draw(ctx DrawContext, point Point)

type NoteBoxPos

type NoteBoxPos int

type NoteBoxStyle

type NoteBoxStyle struct {
	Font     Font
	FontSize int
	Padding  Point
	Margin   Point
	Position NoteBoxPos
}

Styling options for the actor rect

type PathIcon added in v0.0.3

type PathIcon struct {
	Data PathIconData
}

func (PathIcon) Draw added in v0.0.3

func (pi PathIcon) Draw(ctx DrawContext, x int, y int, lineStyle *SvgStyle)

func (PathIcon) Size added in v0.0.3

func (pi PathIcon) Size() (width int, height int)

type PathIconData added in v0.0.3

type PathIconData struct {
	Width          float64
	Height         float64
	TargetIconSize float64
	IconPadding    float64
	Path           string
}

type Point

type Point struct {
	X, Y int
}

A point

type Rect

type Rect struct {
	X, Y int
	W, H int
}

A rectangle

func (Rect) AddSize

func (r Rect) AddSize(w, h int) Rect

Increase the rect size

func (Rect) BlowOut

func (r Rect) BlowOut(dims Point) Rect

Returns a rectangle blown out by a given size

func (Rect) PointAt

func (r Rect) PointAt(gravity Gravity) (int, int)

Returns a point located at a specific gravity within the rectangle

func (Rect) PositionAt

func (r Rect) PositionAt(x, y int, gravity Gravity) Rect

Returns a rectangle position at a specific point and a gravity relative

type SizeConstraint

type SizeConstraint struct {
	R, C        int
	Left, Right int
	Top, Bottom int
}

Apply a size constraint which requests a minimum gap between points

func (SizeConstraint) Apply

func (sc SizeConstraint) Apply(cm ConstraintChanger)

type Spacer

type Spacer struct {
	Margin Point
}

func (*Spacer) Constraint

func (sp *Spacer) Constraint(r, c int, applier ConstraintApplier)

func (*Spacer) Draw

func (sp *Spacer) Draw(ctx DrawContext, point Point)

type StickPersonIcon

type StickPersonIcon int

func (StickPersonIcon) Draw

func (spi StickPersonIcon) Draw(ctx DrawContext, x int, y int, lineStyle *SvgStyle)

func (StickPersonIcon) Size

func (spi StickPersonIcon) Size() (width int, height int)

type SvgStyle

type SvgStyle map[string]string

A SVG style

func StyleFromString

func StyleFromString(str string) SvgStyle

Converts a style in a CSS base string to a SvgStyle

func (SvgStyle) Extend

func (ss SvgStyle) Extend(other SvgStyle)

Add the styles from the other svgstyle to this one

func (SvgStyle) Set

func (ss SvgStyle) Set(key, value string)

func (SvgStyle) ToStyle

func (ss SvgStyle) ToStyle() string

type TTFFont

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

A true-type font

func NewTTFFont

func NewTTFFont(path string) (*TTFFont, error)

Returns a new TTFFont struct

func NewTTFFontFromByteSlice

func NewTTFFontFromByteSlice(bytes []byte, fontName string) (*TTFFont, error)

Loads a TTF font from a byte slice

func (*TTFFont) Measure

func (ttf *TTFFont) Measure(txt string, size float64) (int, int)

Measures the size of a font TODO: For long bits of text, the measurement can be slightly off

func (*TTFFont) SvgName

func (ttf *TTFFont) SvgName() string

Return the SVG Name

type TextAlign

type TextAlign int

type TextBox

type TextBox struct {
	Lines    []string
	Font     Font
	FontSize int
	Align    TextAlign

	Color string
}

A block of prose

func NewTextBox

func NewTextBox(font Font, fontSize int, align TextAlign) *TextBox

Returns a new text box

func (*TextBox) AddText

func (tb *TextBox) AddText(text string)

Adds some text

func (*TextBox) BoundingRect

func (tb *TextBox) BoundingRect() Rect

Given a font, font size, points and gravity, returns a rectangle which will contain the text centered. The point and gravity describes the location of the rect. The second point is where the text is to start given that it is to be rendered to fill the rectangle with default anchoring and alignment

func (*TextBox) Measure

func (tb *TextBox) Measure() (int, int)

Returns the width and height of the text box.

func (*TextBox) Render

func (tb *TextBox) Render(svg *svg.SVG, x, y int, gravity Gravity)

Renders the text from the given point and gravity

type Title

type Title struct {
	TC int
	// contains filtered or unexported fields
}

A title

func NewTitle

func NewTitle(toCol int, text string, style TitleStyle) *Title

func (*Title) Constraint

func (al *Title) Constraint(r, c int, applier ConstraintApplier)

func (*Title) Draw

func (al *Title) Draw(ctx DrawContext, point Point)

type TitleStyle

type TitleStyle struct {
	Font     Font
	FontSize int
	Padding  Point
}

type TotalSizeConstraint

type TotalSizeConstraint struct {
	FR, FC        int
	TR, TC        int
	Width, Height int
}

Ensures that the total size between the two points is big enough for the rectangle. If not, resize the grid points uniformally.

func (TotalSizeConstraint) Apply

Jump to

Keyboard shortcuts

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