imagetemplate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2019 License: MIT Imports: 32 Imported by: 0

README

Image Template

This project defines a template file for drawing custom images from pre-defined components. The intended application is smartcard printing, and some assumptions may be made with that in mind, but this format should be appropriate for general use.

GoDoc Build Status Coverage Status Go Report Card

Testing

go test . -covermode=count -coverprofile="coverage.out"; go tool cover -html="coverage.out"

Documentation

Overview

Package imagetemplate defines a template for drawing custom images from pre-defined components, and provides to tools to load and implement that template.

Index

Constants

View Source
const (
	// BarcodeTypeAztec           is an alias for an imported barcode type
	BarcodeTypeAztec = barcode.TypeAztec
	// BarcodeTypeCodabar         is an alias for an imported barcode type
	BarcodeTypeCodabar = barcode.TypeCodabar
	// BarcodeTypeCode128         is an alias for an imported barcode type
	BarcodeTypeCode128 = barcode.TypeCode128
	// BarcodeTypeCode39          is an alias for an imported barcode type
	BarcodeTypeCode39 = barcode.TypeCode39
	// BarcodeTypeCode93          is an alias for an imported barcode type
	BarcodeTypeCode93 = barcode.TypeCode93
	// BarcodeTypeDataMatrix      is an alias for an imported barcode type
	BarcodeTypeDataMatrix = barcode.TypeDataMatrix
	// BarcodeTypeEAN8            is an alias for an imported barcode type
	BarcodeTypeEAN8 = barcode.TypeEAN8
	// BarcodeTypeEAN13           is an alias for an imported barcode type
	BarcodeTypeEAN13 = barcode.TypeEAN13
	// BarcodeTypePDF             is an alias for an imported barcode type
	BarcodeTypePDF = barcode.TypePDF
	// BarcodeTypeQR              is an alias for an imported barcode type
	BarcodeTypeQR = barcode.TypeQR
	// BarcodeType2of5            is an alias for an imported barcode type
	BarcodeType2of5 = barcode.Type2of5
	// BarcodeType2of5Interleaved is an alias for an imported barcode type
	BarcodeType2of5Interleaved = barcode.Type2of5Interleaved
)

Variables

This section is empty.

Functions

func StandardSetNamedProperties

func StandardSetNamedProperties(properties NamedProperties, propMap map[string][]string, setFunc PropertySetFunc) (leftovers map[string][]string, err error)

StandardSetNamedProperties iterates over all named properties, retrieves their value, and calls the provided function to map properties to inner component properties. Each implementation of Component should call this within its SetNamedProperties function.

Types

type BarcodeComponent added in v0.3.0

type BarcodeComponent struct {
	NamedPropertiesMap map[string][]string
	Content            string
	Type               BarcodeType
	TopLeft            image.Point
	Width              int
	Height             int
	DataColour         color.NRGBA
	BackgroundColour   color.NRGBA
	Extra              BarcodeExtraData
}

BarcodeComponent implements the Component interface for images

func (BarcodeComponent) GetJSONFormat added in v0.3.0

func (component BarcodeComponent) GetJSONFormat() interface{}

GetJSONFormat returns the JSON structure of a barcode component

func (BarcodeComponent) SetNamedProperties added in v0.3.0

func (component BarcodeComponent) SetNamedProperties(properties NamedProperties) (Component, error)

SetNamedProperties proceses the named properties and sets them into the barcode properties

func (BarcodeComponent) VerifyAndSetJSONData added in v0.3.0

func (component BarcodeComponent) VerifyAndSetJSONData(data interface{}) (Component, NamedProperties, error)

VerifyAndSetJSONData processes the data parsed from JSON and uses it to set barcode properties and fill the named properties map

func (BarcodeComponent) Write added in v0.3.0

func (component BarcodeComponent) Write(canvas Canvas) (Canvas, error)

Write draws a barcode on the canvas

type BarcodeExtraData

type BarcodeExtraData struct {
	// AztecMinECCPercent       is required for aztec barcodes
	AztecMinECCPercent int
	// AztecUserSpecifiedLayers is required for aztec barcodes
	AztecUserSpecifiedLayers int
	// Code39IncludeChecksum    is required for code39 barcodes
	Code39IncludeChecksum bool
	// Code39FullASCIIMode      is required for code39 barcodes
	Code39FullASCIIMode bool
	// Code93IncludeChecksum    is required for code93 barcodes
	Code93IncludeChecksum bool
	// Code93FullASCIIMode      is required for code93 barcodes
	Code93FullASCIIMode bool
	// PDFSecurityLevel         is required for pdf417 barcodes
	PDFSecurityLevel byte
	// QRLevel                  is required for qr barcodes
	QRLevel qr.ErrorCorrectionLevel
	// QRMode                   is required for qr barcodes
	QRMode qr.Encoding
}

BarcodeExtraData contains additional data required for some barcode formats, leave any fields not named for the type in use alone

type BarcodeType

type BarcodeType string

BarcodeType wraps the barcode types into a single enum

type Builder

type Builder interface {
	GetCanvas() Canvas
	SetCanvas(newCanvas Canvas) Builder
	GetComponents() []Component
	SetComponents(components []ToggleableComponent) Builder
	GetNamedPropertiesList() NamedProperties
	SetNamedProperties(properties NamedProperties) (Builder, error)
	ApplyComponents() (Builder, error)
	LoadComponentsFile(fileName string) (Builder, error)
	LoadComponentsData(fileData []byte) (Builder, error)
	WriteToBMP() ([]byte, error)
}

Builder manipulates Canvas objects and outputs to a bitmap

type Canvas

type Canvas interface {
	SetUnderlyingImage(newImage image.Image) Canvas
	GetUnderlyingImage() image.Image
	GetWidth() int
	GetHeight() int
	GetPPI() int
	Rectangle(topLeft image.Point, width, height int, colour color.Color) (Canvas, error)
	Circle(centre image.Point, radius int, colour color.Color) (Canvas, error)
	Text(text string, start image.Point, typeFace font.Face, colour color.Color, maxWidth int) (Canvas, error)
	TryText(text string, start image.Point, typeFace font.Face, colour color.Color, maxWidth int) (bool, int)
	DrawImage(start image.Point, subImage image.Image) (Canvas, error)
	Barcode(codeType BarcodeType, content []byte, extra BarcodeExtraData, start image.Point, width, height int, dataColour color.Color, bgColour color.Color) (Canvas, error)
}

Canvas holds the image struct and associated properties

type CircleComponent

type CircleComponent struct {
	NamedPropertiesMap map[string][]string
	Centre             image.Point
	Radius             int
	Colour             color.NRGBA
}

CircleComponent implements the Component interface for circles

func (CircleComponent) GetJSONFormat

func (component CircleComponent) GetJSONFormat() interface{}

GetJSONFormat returns the JSON structure of a circle component

func (CircleComponent) SetNamedProperties

func (component CircleComponent) SetNamedProperties(properties NamedProperties) (Component, error)

SetNamedProperties processes the named properties and sets them into the circle properties

func (CircleComponent) VerifyAndSetJSONData

func (component CircleComponent) VerifyAndSetJSONData(data interface{}) (Component, NamedProperties, error)

VerifyAndSetJSONData processes the data parsed from JSON and uses it to set circle properties and fill the named properties map

func (CircleComponent) Write

func (component CircleComponent) Write(canvas Canvas) (Canvas, error)

Write draws a circle on the canvas

type Component

type Component interface {
	Write(canvas Canvas) (Canvas, error)
	SetNamedProperties(properties NamedProperties) (Component, error)
	GetJSONFormat() interface{}
	VerifyAndSetJSONData(interface{}) (Component, NamedProperties, error)
}

Component provides a generic interface for operations to perform on a canvas

type ComponentConditional

type ComponentConditional struct {
	Name     string              `json:"name"`
	Not      bool                `json:"boolNot"`
	Operator conditionalOperator `json:"operator"`
	Value    string              `json:"value"`
	Group    conditionalGroup    `json:"group"`
	// contains filtered or unexported fields
}

ComponentConditional enables or disables a component based on named properties.

All properties will be assumed to be either strings or floats based on the operator.

String operators: "equals", "contains", "startswith", "endswith", "ci_equals", "ci_contains", "ci_startswith", "ci_endswith". Operators including "ci_" are case-insensitive variants.

Float operators: "=", ">", "<", "<=", ">=".

Group operators can be "and", "or", "nand", "nor", "xor".

func (ComponentConditional) GetNamedPropertiesList

func (c ComponentConditional) GetNamedPropertiesList() NamedProperties

GetNamedPropertiesList returns a list of all named props found in the conditional

func (ComponentConditional) SetValue

func (c ComponentConditional) SetValue(name string, value interface{}) (ComponentConditional, error)

SetValue sets the value of a specific named property through this conditional chain, evaluating any conditions along the way

func (ComponentConditional) Validate

func (c ComponentConditional) Validate() (bool, error)

Validate validates this conditional chain, erroring if a value down the line has not been set and evaluated

type ComponentTemplate

type ComponentTemplate struct {
	Type        string               `json:"type"`
	Conditional ComponentConditional `json:"conditional"`
	Properties  json.RawMessage      `json:"properties"`
}

ComponentTemplate is a partial unmarshalled Component, with its properties left in raw form to be handled by each known type of Component.

type DeconstructedDataValue

type DeconstructedDataValue struct {
	StaticValues []string
	PropNames    []string
}

DeconstructedDataValue is a string broken down into static values and property names. The reconstruction always starts with a static value, always has one more static value than props, and always alternates static, prop, static, prop... if any props exist.

func ParseDataValue

func ParseDataValue(value string) (hasNamedProperties bool, deconstructed DeconstructedDataValue, err error)

ParseDataValue determines whether a string represents raw data or a named variable and returns this information as well as the data cleaned of any variable definitions

type ImageBuilder

type ImageBuilder struct {
	Canvas          Canvas
	Components      []ToggleableComponent
	NamedProperties NamedProperties
	// contains filtered or unexported fields
}

ImageBuilder uses golang's native Image package to implement the Builder interface

func NewBuilder

func NewBuilder(canvas Canvas, startingColour color.Color) (ImageBuilder, error)

NewBuilder generates a new ImageBuilder with an internal canvas of the specified width and height, and optionally the specified starting colour. No provided colour will result in defaults for Image.

func (ImageBuilder) ApplyComponents

func (builder ImageBuilder) ApplyComponents() (Builder, error)

ApplyComponents iterates over the internal Component array, applying each in turn to the Canvas

func (ImageBuilder) GetCanvas

func (builder ImageBuilder) GetCanvas() Canvas

GetCanvas returns the internal Canvas object

func (ImageBuilder) GetComponents

func (builder ImageBuilder) GetComponents() []Component

GetComponents gets the internal Component array

func (ImageBuilder) GetNamedPropertiesList

func (builder ImageBuilder) GetNamedPropertiesList() NamedProperties

GetNamedPropertiesList returns the list of named properties in the builder object

func (ImageBuilder) LoadComponentsData

func (builder ImageBuilder) LoadComponentsData(fileData []byte) (Builder, error)

LoadComponentsData sets the internal component array based on the contents of the specified JSON data

func (ImageBuilder) LoadComponentsFile

func (builder ImageBuilder) LoadComponentsFile(fileName string) (Builder, error)

LoadComponentsFile sets the internal Component array based on the contents of the specified JSON file

func (ImageBuilder) SetCanvas

func (builder ImageBuilder) SetCanvas(newCanvas Canvas) Builder

SetCanvas sets the internal Canvas object

func (ImageBuilder) SetComponents

func (builder ImageBuilder) SetComponents(components []ToggleableComponent) Builder

SetComponents sets the internal Component array

func (ImageBuilder) SetNamedProperties

func (builder ImageBuilder) SetNamedProperties(properties NamedProperties) (Builder, error)

SetNamedProperties sets the values of names properties in all components and conditionals in the builder

func (ImageBuilder) WriteToBMP

func (builder ImageBuilder) WriteToBMP() ([]byte, error)

WriteToBMP outputs the contents of the builder to a BMP byte array

type ImageCanvas

type ImageCanvas struct {
	Image draw.Image
	// contains filtered or unexported fields
}

ImageCanvas uses golang's native Image package to implement the Canvas interface

func NewCanvas

func NewCanvas(width, height int) (ImageCanvas, error)

NewCanvas generates a new canvas of the given width and height

func (ImageCanvas) Barcode

func (canvas ImageCanvas) Barcode(codeType BarcodeType, content []byte, extra BarcodeExtraData, start image.Point, width, height int, dataColour color.Color, backgroundColour color.Color) (Canvas, error)

Barcode draws a barcode on the canvas

func (ImageCanvas) Circle

func (canvas ImageCanvas) Circle(centre image.Point, radius int, colour color.Color) (Canvas, error)

Circle draws a circle of a specific colour on the canvas

func (ImageCanvas) DrawImage

func (canvas ImageCanvas) DrawImage(start image.Point, subImage image.Image) (Canvas, error)

DrawImage draws another image on the canvas

func (ImageCanvas) GetHeight

func (canvas ImageCanvas) GetHeight() int

GetHeight returns the height of the underlying Image. Returns 0 if no canvas is set.

func (ImageCanvas) GetPPI added in v1.0.0

func (canvas ImageCanvas) GetPPI() int

GetPPI returns the pixels per inch of the canvas

func (ImageCanvas) GetUnderlyingImage

func (canvas ImageCanvas) GetUnderlyingImage() image.Image

GetUnderlyingImage gets the internal Image property

func (ImageCanvas) GetWidth

func (canvas ImageCanvas) GetWidth() int

GetWidth returns the width of the underlying Image. Returns 0 if no canvas is set.

func (ImageCanvas) Rectangle

func (canvas ImageCanvas) Rectangle(topLeft image.Point, width, height int, colour color.Color) (Canvas, error)

Rectangle draws a rectangle of a specific colour on the canvas

func (ImageCanvas) SetPPI added in v1.0.0

func (canvas ImageCanvas) SetPPI(ppi int)

SetPPI sets the pixels per inch of the canvas

func (ImageCanvas) SetUnderlyingImage

func (canvas ImageCanvas) SetUnderlyingImage(newImage image.Image) Canvas

SetUnderlyingImage sets the internal Image property to the given object

func (ImageCanvas) Text

func (canvas ImageCanvas) Text(text string, start image.Point, typeFace font.Face, colour color.Color, maxWidth int) (Canvas, error)

Text draws text on the canvas

func (ImageCanvas) TryText added in v0.3.0

func (canvas ImageCanvas) TryText(text string, start image.Point, typeFace font.Face, colour color.Color, maxWidth int) (bool, int)

TryText returns whether the text would fit on the canvas, and the width the text would currently use up

type ImageComponent

type ImageComponent struct {
	NamedPropertiesMap map[string][]string
	Image              image.Image
	TopLeft            image.Point
	Width              int
	Height             int
	// contains filtered or unexported fields
}

ImageComponent implements the Component interface for images

func (ImageComponent) GetJSONFormat

func (component ImageComponent) GetJSONFormat() interface{}

GetJSONFormat returns the JSON structure of a image component

func (ImageComponent) SetNamedProperties

func (component ImageComponent) SetNamedProperties(properties NamedProperties) (Component, error)

SetNamedProperties proceses the named properties and sets them into the image properties

func (ImageComponent) VerifyAndSetJSONData

func (component ImageComponent) VerifyAndSetJSONData(data interface{}) (Component, NamedProperties, error)

VerifyAndSetJSONData processes the data parsed from JSON and uses it to set image properties and fill the named properties map

func (ImageComponent) Write

func (component ImageComponent) Write(canvas Canvas) (Canvas, error)

Write draws an image on the canvas

type NamedProperties

type NamedProperties map[string]interface{}

NamedProperties is a map of property names to property values - application variables to be set

type PropertySetFunc

type PropertySetFunc func(string, interface{}) error

PropertySetFunc maps property names and values to component inner properties

type RectangleComponent

type RectangleComponent struct {
	NamedPropertiesMap map[string][]string
	TopLeft            image.Point
	Width              int
	Height             int
	Colour             color.NRGBA
}

RectangleComponent implements the Component interface for rectangles

func (RectangleComponent) GetJSONFormat

func (component RectangleComponent) GetJSONFormat() interface{}

GetJSONFormat returns the JSON structure of a rectangle component

func (RectangleComponent) SetNamedProperties

func (component RectangleComponent) SetNamedProperties(properties NamedProperties) (Component, error)

SetNamedProperties proceses the named properties and sets them into the rectangle properties

func (RectangleComponent) VerifyAndSetJSONData

func (component RectangleComponent) VerifyAndSetJSONData(data interface{}) (Component, NamedProperties, error)

VerifyAndSetJSONData processes the data parsed from JSON and uses it to set rectangle properties and fill the named properties map

func (RectangleComponent) Write

func (component RectangleComponent) Write(canvas Canvas) (Canvas, error)

Write draws a rectangle on the canvas

type Template

type Template struct {
	BaseImage struct {
		FileName   string `json:"fileName"`
		Data       string `json:"data"`
		BaseColour struct {
			Red   string `json:"R"`
			Green string `json:"G"`
			Blue  string `json:"B"`
			Alpha string `json:"A"`
		} `json:"baseColour"`
		BaseWidth  string `json:"width"`
		BaseHeight string `json:"height"`
	} `json:"baseImage"`
	Components []ComponentTemplate `json:"components"`
}

Template is the format of the JSON file used as a template for building images. See samples.json for examples, each element in the samples array is a complete and valid template object.

type TextAlignment added in v1.0.0

type TextAlignment int

TextAlignment is a text alignment

const (
	// TextAlignmentLeft aligns text left
	TextAlignmentLeft TextAlignment = iota
	// TextAlignmentRight aligns text right
	TextAlignmentRight
	// TextAlignmentCentre aligns text centrally
	TextAlignmentCentre
)

type TextComponent

type TextComponent struct {
	NamedPropertiesMap map[string][]string
	Content            string
	Start              image.Point
	Size               float64
	MaxWidth           int
	Alignment          TextAlignment
	PixelsPerInch      int //Should default to 72
	Font               *truetype.Font
	Colour             color.NRGBA
	// contains filtered or unexported fields
}

TextComponent implements the Component interface for text

func (TextComponent) GetJSONFormat

func (component TextComponent) GetJSONFormat() interface{}

GetJSONFormat returns the JSON structure of a text component

func (TextComponent) SetNamedProperties

func (component TextComponent) SetNamedProperties(properties NamedProperties) (Component, error)

SetNamedProperties proceses the named properties and sets them into the text properties

func (TextComponent) VerifyAndSetJSONData

func (component TextComponent) VerifyAndSetJSONData(data interface{}) (Component, NamedProperties, error)

VerifyAndSetJSONData processes the data parsed from JSON and uses it to set text properties and fill the named properties map

func (TextComponent) Write

func (component TextComponent) Write(canvas Canvas) (Canvas, error)

Write draws text on the canvas

type ToggleableComponent

type ToggleableComponent struct {
	Conditional ComponentConditional
	Component   Component
}

ToggleableComponent is a component with its conditional

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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