tableimage

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

README

tableimage generator

Generates a table inside of a image based on the provided data

Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

Features
  • support border, font, color, padding, margin etc. style settings
  • style inherit, could set style in cell, row, table level
  • support image in table
  • support text dpi setting, font size setting based on 72dpi
  • support inline text style (inline text format styled text.)
Example:

Example

Usage
package main

import (
	"image"
	"log"
	"os"

	"github.com/bububa/tableimage"
	"github.com/llgcode/draw2d"
)

func main() {
	imageURL := "https://images.pexels.com/photos/906052/pexels-photo-906052.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=200"

	ti, err := tableimage.New(
		tableimage.WithBgColor("#FFFFFF"),
		tableimage.WithBorderColor("#0277BD"),
		tableimage.WithFontSize(11),
		tableimage.WithDPI(144),
		tableimage.WithFontData(&draw2d.FontData{
			Name:   "NotoSansCJKsc",
			Family: draw2d.FontFamilySans,
			Style:  draw2d.FontStyleNormal,
		}),
		tableimage.WithFontFolder("./font"),
	)
	if err != nil {
		log.Fatalln(err)
		return
	}

	headerFont := &tableimage.Font{
		Size: 13,
		Data: &draw2d.FontData{
			Name:   "Roboto",
			Family: draw2d.FontFamilySans,
			Style:  draw2d.FontStyleBold,
		},
	}

	footerFont := &tableimage.Font{
		Size: 10,
		Data: &draw2d.FontData{
			Name:   "NotoSansCJKsc",
			Family: draw2d.FontFamilySans,
			Style:  draw2d.FontStyleNormal,
		},
	}

	headerStyle := &tableimage.Style{
		Font: headerFont,
	}

	caption := &tableimage.Cell{
		Text: "this is a caption very long long long ago adsfasdfwe;alsdkfasdfasf asdfajsdf",
		Style: &tableimage.Style{
			Color: "#3F51B5",
			Font:  headerFont,
		},
	}

	tfooter := &tableimage.Cell{
		Text: "this is a tabel footer very long long long ago adsfasdfwe;alsdkfasdfasf asdfajsdf",
		Style: &tableimage.Style{
			Color: "#D7CCC8",
			Font:  footerFont,
		},
	}

	rows := []tableimage.Row{
		{
			Style: headerStyle,
			Cells: []tableimage.Cell{
				{
					Text: "Id",
				},
				{
					Text: "Name",
				},
				{
					Style: &tableimage.Style{
						Color: "#008000",
					},
					Text: "Price",
				},
			},
		},
		{
			Cells: []tableimage.Cell{
				{
					Text: "2223",
				},
				{
					Style: &tableimage.Style{
						Color:    "#000",
						MaxWidth: 100,
						Align:    tableimage.CENTER,
					},
					Text: "这是一个真正的table图片",
				},
				{
					Style: &tableimage.Style{
						Color: "#0000ff",
					},
					Text: "2000$",
				},
			},
		},
		{
			Cells: []tableimage.Cell{
				{
					Style: &tableimage.Style{
						Color:  "#6A1B9A",
						VAlign: tableimage.TOP,
					},
					Text: "11",
				},
				{
					Style: &tableimage.Style{
						Color:    "#FFF",
						MaxWidth: 100,
						BgColor:  "#D32F2F",
					},
                    IgnoreInlineStyle: false,
					Text: "A more <text bgcolor='#8BC34A' color='#000' padding='4'>cooler product this</text> time on 3 lines",
				},
				{
					Style: &tableimage.Style{
						Color:  "#0000ff",
						Align:  tableimage.RIGHT,
						VAlign: tableimage.BOTTOM,
					},
					Text: "200$",
				},
			},
		},
		{
			Cells: []tableimage.Cell{
				{
					Text: "2223",
					Image: &tableimage.Image{
						URL:     imageURL,
						Size:    image.Pt(80, 0),
						VAlign:  tableimage.BOTTOM,
						Padding: tableimage.NewPaddingY(4),
					},
					Style: &tableimage.Style{
						Align: tableimage.RIGHT,
					},
				},
				{
					Style: &tableimage.Style{
						Color:    "#000",
						MaxWidth: 100,
						Align:    tableimage.CENTER,
					},
					Image: &tableimage.Image{
						URL:     imageURL,
						Size:    image.Pt(80, 0),
						VAlign:  tableimage.TOP,
						Padding: tableimage.NewPaddingY(4),
					},
					Text: "这是一个真正的table图片",
				},
				{
					Style: &tableimage.Style{
						Color: "#0000ff",
					},
					Text: "2000$",
				},
			},
		},
		{
			Cells: []tableimage.Cell{
				{
					Text: "2223",
					Image: &tableimage.Image{
						URL:     imageURL,
						Size:    image.Pt(80, 0),
						Align:   tableimage.LEFT,
						Padding: tableimage.NewPaddingX(4),
					},
					Style: &tableimage.Style{
						VAlign: tableimage.BOTTOM,
					},
				},
				{
					Style: &tableimage.Style{
						Color:    "#000",
						MaxWidth: 150,
						Align:    tableimage.RIGHT,
						VAlign:   tableimage.MIDDLE,
					},
					Image: &tableimage.Image{
						URL:     imageURL,
						Size:    image.Pt(80, 0),
						Align:   tableimage.RIGHT,
						Padding: tableimage.NewPaddingX(4),
					},
					Text: "这是一个真正的table图片",
				},
				{
					Style: &tableimage.Style{
						Color: "#0000ff",
					},
					Text: "2000$",
				},
			},
		},
	}
	img, err := ti.Draw(rows, caption, tfooter)
	if err != nil {
		log.Fatalln(err)
		return
	}
	f, err := os.Create("./test.png")
	if err != nil {
		log.Fatalln(err)
		return
	}
	defer f.Close()
	tableimage.Write(f, img, tableimage.PNG)
}

Documentation

Overview

Package tableimage generates a table inside of a image (png or jpg format) based on the provided data

Index

Constants

View Source
const (
	// DefaultLineHeight default row space
	DefaultLineHeight = 1.2
	// DefaultFontSize default font size
	DefaultFontSize = 13
	// DefaultPadding default padding
	DefaultPadding = 10
	// DefaultWrapWords default wrap words count
	DefaultWrapWords = 20
	// DefaultColor default text color
	DefaultColor = "#212121"
	// DefaultBorderWidth default stroke line width
	DefaultBorderWidth = 1
	// DefaultDPI default font dpi
	DefaultDPI = 72
)

Variables

View Source
var DefaultBorder = func() *Border {
	return &Border{
		Top:    DefaultLine(),
		Right:  DefaultLine(),
		Bottom: DefaultLine(),
		Left:   DefaultLine(),
	}
}

DefaultBorder default border setting

View Source
var DefaultCaptionStyle = func() *Style {
	return &Style{
		Color:      DefaultColor,
		Border:     NoBorder(),
		LineHeight: DefaultLineHeight,
		Padding:    NewPaddingY(DefaultPadding),
		Align:      LEFT,
		VAlign:     TOP,
		Font: &Font{
			Size: DefaultFontSize,
		},
	}
}

DefaultCaptionStyle default caption style setting

View Source
var DefaultFooterStyle = func() *Style {
	return &Style{
		Color:      DefaultColor,
		Border:     NoBorder(),
		LineHeight: DefaultLineHeight,
		Padding:    NewPaddingY(DefaultPadding),
		Align:      RIGHT,
		VAlign:     TOP,
		Font: &Font{
			Size: DefaultFontSize,
		},
	}
}

DefaultFooterStyle default table footer style setting

View Source
var DefaultLine = func() Line {
	return Line{
		Color: DefaultColor,
		Width: DefaultBorderWidth,
	}
}

DefaultLine default line setting

View Source
var DefaultStyle = func() *Style {
	return &Style{
		Color:      DefaultColor,
		Border:     DefaultBorder(),
		LineHeight: DefaultLineHeight,
		Padding:    NewPadding(DefaultPadding),
		Align:      LEFT,
		VAlign:     MIDDLE,
		Font: &Font{
			Size: DefaultFontSize,
		},
	}
}

DefaultStyle default style setting

View Source
var NoBorder = func() *Border {
	return &Border{}
}

NoBorder no border setting

View Source
var ZeroPadding = Padding{}

ZeroPadding zero padding object

Functions

func ColorFromHex

func ColorFromHex(hexColor string) color.RGBA

ColorFromHex get color from hex

func Save

func Save(filepath string, img *image.RGBA, imageType ImageType) error

Save an image to file

func Write

func Write(w io.Writer, img *image.RGBA, imageType ImageType) error

Write witer image to io Writer

Types

type Align

type Align int

Align Alignment

const (
	// UnknownAlign unknown alignment
	UnknownAlign Align = iota
	// LEFT align left
	LEFT
	// RIGHT align right
	RIGHT
	// CENTER align center
	CENTER
)

type Border

type Border struct {
	Top    Line `json:"top,omitempty"`
	Right  Line `json:"right,omitempty"`
	Bottom Line `json:"bottom,omitempty"`
	Left   Line `json:"left,omitempty"`
}

Border border setting

func (*Border) ChangeColor

func (b *Border) ChangeColor(color string)

ChangeColor change border color

func (*Border) ChangeWidth

func (b *Border) ChangeWidth(width int)

ChangeWidth change border width

func (Border) Draw

func (b Border) Draw(img *image.RGBA, bounds image.Rectangle)

Draw a border

func (Border) Padding

func (b Border) Padding() Padding

Padding border padding

func (Border) Size

func (b Border) Size() image.Point

Size border border size

type Cell

type Cell struct {
	// Text content of a cell
	Text string `json:"text,omitempty"`
	// Image image for a cell
	Image *Image `json:"image,omitempty"`
	// Style for cell
	Style *Style `json:"style,omitempty"`
	// IgnoreInlineStyle ignore inline text style parsing
	IgnoreInlineStyle bool `json:"ignore_inline_style,omitempty"`
}

Cell in table

func (Cell) Draw

func (c Cell) Draw(img *image.RGBA, bounds image.Rectangle)

Draw render cell to image

func (Cell) GetImage

func (c Cell) GetImage(cache ImageCache) error

GetImage download cell image

func (Cell) ImageSize

func (c Cell) ImageSize() image.Point

ImageSize get image size, will update Image.Size based on max width setting

func (Cell) InnerBounds

func (c Cell) InnerBounds(bounds image.Rectangle) image.Rectangle

InnerBounds cell content bounds

func (Cell) Size

func (c Cell) Size() image.Point

Size returns cell width/height

func (Cell) Wrap

func (c Cell) Wrap(xOffset int) ([]Word, int)

Wrap wraps cell content returns paragraphs, and max content width

type DefaultImageCache

type DefaultImageCache map[string]image.Image

DefaultImageCache default ImageCache implement

func (DefaultImageCache) Get

Get implement ImageCache

func (DefaultImageCache) Set

func (c DefaultImageCache) Set(k string, img image.Image) error

Set implement ImageCache

type Font

type Font struct {
	// Size font size
	Size float64 `json:"size,omitempty"`
	// Data font setting
	Data *draw2d.FontData `json:"data,omitempty"`
	// Font
	Font *truetype.Font `json:"-"`
	// DPI
	DPI int `json:"dpi,omitempty"`
}

Font font info

func (*Font) Load

func (f *Font) Load(cache draw2d.FontCache) error

Load font from font cache

type Image

type Image struct {
	// URL image link
	URL string `json:"url,omitempty"`
	// Data image data
	Data image.Image
	// Inline display inline
	Inline bool `json:"inline,omitempty"`
	// Size image width/height
	Size image.Point `json:"size,omitempty"`
	// Align image text alignment
	Align Align `json:"align,omitempty"`
	// VAlign image text vertical alignment
	VAlign VAlign `json:"valign,omitempty"`
	// Padding image padding
	Padding *Padding `json:"padding,omitempty"`
}

Image image setting

func (Image) BoundSize

func (i Image) BoundSize() image.Point

BoundSize get Image width/height

func (*Image) Download

func (i *Image) Download() error

Download image data

func (Image) PaddingLeft

func (i Image) PaddingLeft() int

PaddingLeft left padding

func (Image) PaddingTop

func (i Image) PaddingTop() int

PaddingTop top padding

func (Image) PaddingX

func (i Image) PaddingX() int

PaddingX horizontal padding

func (Image) PaddingY

func (i Image) PaddingY() int

PaddingY vertical padding

func (Image) Scale

func (i Image) Scale() float64

Scale get image scale

func (*Image) UpdateSize

func (i *Image) UpdateSize()

UpdateSize update Size based on image bounds

type ImageCache

type ImageCache interface {
	// Get get image from cache
	Get(k string) (image.Image, error)
	// Set set image to cache
	Set(k string, img image.Image) error
}

ImageCache image cache interface

type ImageType

type ImageType int

ImageType image type for writer

const (

	// JPEG jpeg image
	JPEG ImageType
	// PNG png image
	PNG
)

type Line

type Line struct {
	Color string `json:"color,omitempty"`
	Width int    `json:"width,omitempty"`
}

Line border line

func (Line) ChangeColor

func (l Line) ChangeColor(color string) Line

ChangeColor return a line with new color

func (Line) ChangeWidth

func (l Line) ChangeWidth(width int) Line

ChangeWidth return a line with new width

func (Line) Draw

func (l Line) Draw(img *image.RGBA, bounds image.Rectangle)

Draw a new line

type Option

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

Option tableimage option interface

func WithAlign

func WithAlign(align Align) Option

WithAlign set alignment

func WithBgColor

func WithBgColor(bgColor string) Option

WithBgColor set background color

func WithBorder

func WithBorder(border *Border) Option

WithBorder set border setting

func WithBorderColor

func WithBorderColor(color string) Option

WithBorderColor set border color

func WithBorderWidth

func WithBorderWidth(width int) Option

WithBorderWidth set border width

func WithColor

func WithColor(color string) Option

WithColor set text color

func WithDPI added in v1.0.1

func WithDPI(dpi int) Option

WithDPI set font dpi

func WithFont

func WithFont(font *truetype.Font) Option

WithFont set font setting

func WithFontCache

func WithFontCache(cache draw2d.FontCache) Option

WithFontCache set font cache

func WithFontData

func WithFontData(font *draw2d.FontData) Option

WithFontData set font data

func WithFontFolder

func WithFontFolder(fontFolder string) Option

WithFontFolder set font folder

func WithFontSize

func WithFontSize(size float64) Option

WithFontSize set font size

func WithLineHeight

func WithLineHeight(height float64) Option

WithLineHeight set line height

func WithMargin

func WithMargin(margin *Padding) Option

WithMargin set margin

func WithPadding

func WithPadding(padding *Padding) Option

WithPadding set padding

func WithStyle

func WithStyle(style *Style) Option

WithStyle set style setting

func WithVAlign

func WithVAlign(align VAlign) Option

WithVAlign set vertical alignment

type Padding

type Padding struct {
	// Top padding
	Top int `json:"top,omitempty"`
	// Right padding
	Right int `json:"right,omitempty"`
	// Bottom padding
	Bottom int `json:"bottom,omitempty"`
	// Left padding
	Left int `json:"left,omitempty"`
}

Padding cell padding

func NewPadding

func NewPadding(padding int) *Padding

NewPadding init padding with same value

func NewPaddingX

func NewPaddingX(x int) *Padding

NewPaddingX init padding with left/right

func NewPaddingXY

func NewPaddingXY(x int, y int) *Padding

NewPaddingXY init padding for both x / y

func NewPaddingY

func NewPaddingY(y int) *Padding

NewPaddingY init padding with top/bottom

func (Padding) Add

func (p Padding) Add(p2 Padding) Padding

Add merget tow paddings

func (Padding) Size

func (p Padding) Size() image.Point

Size padding size

type Row

type Row struct {
	// Cells in row
	Cells []Cell `json:"cells,omitempty"`
	// Style for row
	Style *Style `json:"style,omitempty"`
}

Row in table

type Style

type Style struct {
	// Color text color
	Color string `json:"color,omitempty"`
	// Border cell border setting
	Border *Border `json:"border,omitempty"`
	// BgColor cell background color
	BgColor string `json:"bg_color,omitempty"`
	// Lineheight lineheight for paragraph
	LineHeight float64 `json:"line_height,omitempty"`
	// Margin cell margin
	Margin *Padding `json:"margin,omitempty"`
	// Padding cell padding
	Padding *Padding `json:"padding,omitempty"`
	// MaxWidth max width
	MaxWidth int `json:"max_width,omitempty"`
	// Align alignment
	Align Align `json:"align,omitempty"`
	// VAlign vertical alignment
	VAlign VAlign `json:"valign,omitempty"`
	// Font font setting
	Font *Font `json:"font,omitempty"`
}

Style for drawing

func (Style) BorderPadding

func (s Style) BorderPadding() Padding

BorderPadding border padding

func (Style) BorderSize

func (s Style) BorderSize() image.Point

BorderSize outer bound size

func (*Style) Inherit

func (s *Style) Inherit(s1 *Style, cache draw2d.FontCache) error

Inherit from other style

func (Style) InnerBounds

func (s Style) InnerBounds(bounds image.Rectangle) image.Rectangle

InnerBounds content bounds

func (Style) InnerEnd

func (s Style) InnerEnd() image.Point

InnerEnd content end point

func (Style) InnerStart

func (s Style) InnerStart() image.Point

InnerStart content start point

func (*Style) LoadFont

func (s *Style) LoadFont(cache draw2d.FontCache) error

LoadFont load font with fontCache

type Table added in v1.0.1

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

Table table struct

func NewTable added in v1.0.1

func NewTable(ti *TableImage, rows []Row, caption *Cell, footer *Cell) (*Table, error)

NewTable create Table instance

func (Table) CellBounds added in v1.0.1

func (r Table) CellBounds(rowIdx int, cellIdx int) image.Rectangle

CellBounds get a cell bounds

func (Table) DrawCaption added in v1.0.1

func (r Table) DrawCaption(img *image.RGBA, pt image.Point)

DrawCaption draw table caption

func (Table) DrawFooter added in v1.0.1

func (r Table) DrawFooter(img *image.RGBA, pt image.Point)

DrawFooter draw table footer

func (Table) Rows added in v1.0.1

func (r Table) Rows() []Row

Rows get rows

func (Table) RowsSize added in v1.0.1

func (r Table) RowsSize() image.Point

RowsSize get rows size

func (Table) RowsStartPoint added in v1.0.1

func (r Table) RowsStartPoint() image.Point

RowsStartPoint table rows start point

func (Table) Size added in v1.0.1

func (r Table) Size() image.Point

Size get bound size

type TableImage

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

TableImage core struct

func New

func New(options ...Option) (*TableImage, error)

New init a TableImage object

func (*TableImage) BorderSize

func (ti *TableImage) BorderSize() image.Point

BorderSize get border width of tableimage

func (*TableImage) CacheImage

func (ti *TableImage) CacheImage(k string, img image.Image) error

CacheImage cache image

func (*TableImage) Draw

func (ti *TableImage) Draw(rows []Row, caption *Cell, footer *Cell) (*image.RGBA, error)

Draw draw table image

func (*TableImage) GetImage

func (ti *TableImage) GetImage(k string) (image.Image, error)

GetImage get image from cache

func (*TableImage) Size

func (ti *TableImage) Size(table *Table) image.Point

Size get tableimage width/height

type Text

type Text struct {
	Value   string
	Width   int
	Color   string
	BgColor string
	Padding int
	Pos     [2]int
}

Text string with width

func TextFromText added in v1.0.1

func TextFromText(value string, txt Text, fontFace font.Face) Text

TextFromText create Text from Text

func (Text) SameStyle added in v1.0.1

func (t Text) SameStyle(t2 Text) bool

SameStyle check if two Text style is same

type VAlign

type VAlign int

VAlign vertical alignment

const (
	// UnknownVAlign unknown alignment
	UnknownVAlign VAlign = iota
	// TOP vertical align top
	TOP
	// BOTTOM vertical align bottom
	BOTTOM
	// MIDDLE vertical align bottom
	MIDDLE
)

type Word added in v1.0.1

type Word []Text

Word text array

func (Word) Width added in v1.0.1

func (w Word) Width() int

Width calculate word length

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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