crazy_grafica

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

README

receipt

Introduction

Once I needed to develop a system in a short time that could print a receipt. To do this, you can use the standard library "image/draw". But I always don't like to write a lot of the same type of code by hand and I tried to implement basic primitives such as "text in a rectangle", "rows" and "columns". The result is this library. Perhaps the approach that is used here does not fit into GO canons, but it does its little functionality well.

Usages

All drawing operations on the canvas are performed using the document structure description. Similar to DOM for HTML. Canvas is the main object which simply stores a link to the image and the dimensions of the work area rectangle.

    import cg "github.com/iv-menshenin/receipt"

	width := cg.Millimeters(210.0)
	height := cg.Millimeters(297.0)
	canvasRect := cg.NewRectangle(cg.ZeroPixel(), cg.ZeroPixel(), width, height)
	img := image.NewRGBA(canvasRect)
	canv := cg.NewCanvas(img, img.Rect)

In order to render the necessary information, we must parse the document into structures: rows and columns.

    pt := canv.Write(
        cg.Padding(
            cg.Millimeters(5),
            cg.Lines(
    		    cg.PaddingLeftRight(
                    cg.Millimeters(10),
                    cg.Lines(
    			        cg.Cols(
                            cg.Text(fmt.Sprintf("Order #%d %s", rand.Int(), time.Now().Format("01.02.2006 15:04")), cg.OptionAlignment(cg.AlignLeft), fontOpt),
                            cg.Text("DRAFT", cg.OptionAlignment(cg.AlignRight), fontAccentOpt),
    			        ),
    			        cg.Text("555-345-65-66 Menshenin Igor", fontOpt),
    		        )
                ),
    		    cg.FixedY(cg.Millimeters(2)),
    ...

canv.Write renders the object to the Canvas, drawing is performed sequentially - from top to bottom from left to right. The canv.Write method returns the coordinate of the bottom-right point at which drawing ended.
You can see how it works with an example.

Elements

Measurements

Three objects represent distance measures on the Canvas:

  • Millimeters
  • Inches
  • Pixels
Lines and Columns

The two objects represent vertical and horizontal layouts on the Canvas. They are inherently containers.

  • Lines
  • Cols
Text

This object renders the text. It takes as arguments the text itself to be placed and options that allow you to control alignment and font.

  • Text

Options:

  • OptionFont
  • OptionAlignment
  • OptionCentered

Please note that if the text does not fit in length into the container in which it is located, then the lines will wrap by words.

Fillers and Paddings

The following structures allow you to set padding inside the container

  • Padding
  • PaddingLeftRight

The following structures allow you to fill a container with a void. Behavior similar to previous functions, but not containers.

  • Fixed
  • FixedY
  • Empty
Table

The most complex (to date) structure allows you to implement the drawing of a table while maintaining the width of the columns in each row and applying fonts to all cells of the column.

  • Table

Font, heading, and relative column width settings are performed by the function:

  • Column

Line-by-line filling of the table with data (in text format) is performed using the following combinations of structures:

  • Cols > Text
  • Cols > ColSpan > Text

Example

example result

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPen

func NewPen(color color.Color, w Measure) pen

func NewRectangle

func NewRectangle(x0, y0, x1, y1 Measure) image.Rectangle

NewRectangle allows you to create a rectangle using length measures:

Millimeters, Inches, Pixels

example

NewRectangle(ZeroPixel(), ZeroPixel(), Inches(1.0), Inches(1.0)) // box with sides 1 inch

func SetDPI

func SetDPI(newDpi float64)

SetDPI allows you to set the resolution (dots per inch)

Types

type Alignment

type Alignment int
const (
	AlignLeft Alignment = iota
	AlignRight
	AlignCenter
)

type Canvas

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

func NewCanvas

func NewCanvas(img draw.Image, rect image.Rectangle) Canvas

func (*Canvas) Write

func (c *Canvas) Write(d DrawStruct) image.Point

Write will draw the block of objects, starting from the vertical position at which drawing of the previous block of objects was completed

type ColumnOption

type ColumnOption interface {
	// contains filtered or unexported methods
}

type ColumnSpan

type ColumnSpan interface {
	// contains filtered or unexported methods
}

type DrawColumns

type DrawColumns interface {
	WriteTo(Canvas, image.Rectangle) image.Point
	// contains filtered or unexported methods
}

func Cols

func Cols(d ...DrawStruct) DrawColumns

type DrawStruct

type DrawStruct interface {
	WriteTo(Canvas, image.Rectangle) image.Point
}

func ColSpan

func ColSpan(d DrawStruct, span int) DrawStruct

func Empty

func Empty() DrawStruct

func Fixed

func Fixed(x, y Measure) DrawStruct

func FixedY

func FixedY(y Measure) DrawStruct

func Lines

func Lines(d ...DrawStruct) DrawStruct

func Padding

func Padding(l, t, r, b Measure, d DrawStruct) DrawStruct

PaddingLeftRight allows to adjust the padding on each side of the object border

func Padding4

func Padding4(pad Measure, d DrawStruct) DrawStruct

PaddingLeftRight adds padding on all sides to the drawing object

func PaddingLeftRight

func PaddingLeftRight(pad Measure, d DrawStruct) DrawStruct

PaddingLeftRight adds left and right padding to the drawing object

func PaddingTopBottom

func PaddingTopBottom(pad Measure, d DrawStruct) DrawStruct

PaddingLeftRight adds left and right padding to the drawing object

func Table

func Table(columns []TableColumn, data ...TableRow) DrawStruct

func Text

func Text(s string, options ...TextOption) DrawStruct

Text renders text to Canvas

type DrawText

type DrawText interface {
	WriteTo(Canvas, image.Rectangle) image.Point
	// contains filtered or unexported methods
}

type Measure

type Measure interface {
	// contains filtered or unexported methods
}

Measure is a universal unit of measurement on canvas. Use this:

Millimeters, Inches, Pixels

func Inches

func Inches(i float64) Measure

Inches - measures the distance in inches

func Millimeters

func Millimeters(m float64) Measure

Millimeters - measures the distance in millimeters

func Pixels

func Pixels(pix int) Measure

Pixels - measures the distance in pixels

func ZeroPixel

func ZeroPixel() Measure

ZeroPixel is a distance of 0 pixels long

type PaddingStruct

type PaddingStruct interface {
	// contains filtered or unexported methods
}

type TableColumn

type TableColumn interface {
	// contains filtered or unexported methods
}

func Column

func Column(caption string, pie float64, options ...ColumnOption) TableColumn

type TableRow

type TableRow interface {
	// contains filtered or unexported methods
}

type TextOption

type TextOption interface {
	// contains filtered or unexported methods
}

func OptionAlignment

func OptionAlignment(a Alignment) TextOption

OptionAlignment lets you set horizontal alignment

AlignLeft, AlignRight, AlignCenter

func OptionCentered

func OptionCentered() TextOption

OptionCentered means that the text needs to be vertically aligned in the center

func OptionFont

func OptionFont(font *truetype.Font, fontSize float64, usePen pen) TextOption

OptionFont contains font settings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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