unipdf/pdf/creator/invoice.go

708 lines
16 KiB
Go
Raw Normal View History

2018-10-26 12:29:40 +03:00
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package creator
import "fmt"
2018-10-30 20:43:10 +02:00
// InvoiceAddress.
2018-10-26 12:29:40 +03:00
type InvoiceAddress struct {
Name string
Street string
Zip string
City string
Country string
Phone string
Email string
}
2018-10-30 20:43:10 +02:00
// InvoiceCellProps.
2018-10-26 12:29:40 +03:00
type InvoiceCellProps struct {
2018-10-30 20:43:10 +02:00
TextStyle TextStyle
2018-10-26 12:29:40 +03:00
Alignment CellHorizontalAlignment
BackgroundColor Color
2018-10-30 20:43:10 +02:00
BorderColor Color
BorderWidth int
BorderStyle int
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
// InvoiceCell.
2018-10-26 12:29:40 +03:00
type InvoiceCell struct {
InvoiceCellProps
Value string
}
2018-10-30 20:43:10 +02:00
// Invoice represents a configurable invoice template.
2018-10-26 12:29:40 +03:00
type Invoice struct {
2018-10-30 20:43:10 +02:00
// Invoice title.
2018-10-26 12:29:40 +03:00
title string
2018-10-30 20:43:10 +02:00
// Invoice logo.
2018-10-26 12:29:40 +03:00
logo *Image
2018-10-30 20:43:10 +02:00
// Invoice addresses.
buyerAddress *InvoiceAddress
sellerAddress *InvoiceAddress
2018-10-26 12:29:40 +03:00
// Invoice information.
2018-10-30 20:43:10 +02:00
number [2]*InvoiceCell
date [2]*InvoiceCell
dueDate [2]*InvoiceCell
info [][2]*InvoiceCell
2018-10-26 12:29:40 +03:00
// Invoice lines.
2018-10-30 20:43:10 +02:00
columns []*InvoiceCell
lines [][]*InvoiceCell
2018-10-26 12:29:40 +03:00
// Invoice totals.
2018-10-30 20:43:10 +02:00
subtotal [2]*InvoiceCell
total [2]*InvoiceCell
2018-10-29 20:18:32 +02:00
totals [][2]*InvoiceCell
2018-10-30 20:43:10 +02:00
// Invoice note sections.
notes [2]string
terms [2]string
sections [][2]string
// Invoice styles.
defaultStyle TextStyle
headingStyle TextStyle
titleStyle TextStyle
infoProps InvoiceCellProps
colProps InvoiceCellProps
itemProps InvoiceCellProps
totalProps InvoiceCellProps
2018-10-26 12:29:40 +03:00
// Positioning: relative/absolute.
positioning positioning
}
// newInvoice returns an instance of an empty invoice.
func newInvoice(defaultStyle, headingStyle TextStyle) *Invoice {
i := &Invoice{
// Title.
title: "INVOICE",
// Addresses.
2018-10-30 20:43:10 +02:00
sellerAddress: &InvoiceAddress{},
buyerAddress: &InvoiceAddress{},
// Styles.
defaultStyle: defaultStyle,
headingStyle: headingStyle,
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
// Default colors.
2018-10-26 12:29:40 +03:00
lightGrey := ColorRGBFrom8bit(245, 245, 245)
mediumGrey := ColorRGBFrom8bit(155, 155, 155)
2018-10-30 20:43:10 +02:00
// Default title style.
2018-10-26 12:29:40 +03:00
i.titleStyle = headingStyle
i.titleStyle.Color = mediumGrey
i.titleStyle.FontSize = 20
2018-10-30 20:43:10 +02:00
// Invoice information default properties.
i.infoProps = i.NewCellProps()
i.infoProps.BackgroundColor = lightGrey
i.infoProps.TextStyle = headingStyle
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
// Invoice line items default properties.
i.colProps = i.NewCellProps()
i.colProps.TextStyle = headingStyle
i.colProps.BackgroundColor = lightGrey
i.colProps.BorderColor = lightGrey
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
i.itemProps = i.NewCellProps()
2018-10-29 20:18:32 +02:00
i.itemProps.Alignment = CellHorizontalAlignmentRight
2018-10-30 20:43:10 +02:00
// Invoice totals default properties.
i.totalProps = i.NewCellProps()
2018-10-29 20:18:32 +02:00
i.totalProps.Alignment = CellHorizontalAlignmentRight
2018-10-30 20:43:10 +02:00
// Invoice information fields.
i.number = [2]*InvoiceCell{
i.newCell("Invoice number", i.infoProps),
i.newCell("", i.infoProps),
}
2018-10-29 20:18:32 +02:00
2018-10-30 20:43:10 +02:00
i.date = [2]*InvoiceCell{
i.newCell("Date", i.infoProps),
i.newCell("", i.infoProps),
}
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
i.dueDate = [2]*InvoiceCell{
i.newCell("Date", i.infoProps),
i.newCell("", i.infoProps),
}
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
// Invoice totals fields.
i.subtotal = [2]*InvoiceCell{
i.newCell("Subtotal", i.totalProps),
i.newCell("", i.totalProps),
}
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
totalProps := i.totalProps
totalProps.TextStyle = headingStyle
totalProps.BackgroundColor = lightGrey
totalProps.BorderColor = lightGrey
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
i.total = [2]*InvoiceCell{
i.newCell("Total", totalProps),
i.newCell("", totalProps),
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
// Invoice notes fields.
i.notes = [2]string{"Notes", ""}
i.terms = [2]string{"Terms and conditions", ""}
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
// Default item columns.
i.columns = []*InvoiceCell{
i.newColumn("Description", CellHorizontalAlignmentLeft),
i.newColumn("Quantity", CellHorizontalAlignmentRight),
i.newColumn("Unit price", CellHorizontalAlignmentRight),
i.newColumn("Amount", CellHorizontalAlignmentRight),
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
return i
2018-10-26 12:29:40 +03:00
}
func (i *Invoice) Title() string {
return i.title
}
func (i *Invoice) SetTitle(title string) {
i.title = title
}
func (i *Invoice) Logo() *Image {
return i.logo
}
func (i *Invoice) SetLogo(logo *Image) {
i.logo = logo
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SellerAddress() *InvoiceAddress {
return i.sellerAddress
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetSellerAddress(address *InvoiceAddress) {
i.sellerAddress = address
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) BuyerAddress() *InvoiceAddress {
return i.buyerAddress
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetBuyerAddress(address *InvoiceAddress) {
i.buyerAddress = address
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Number() (*InvoiceCell, *InvoiceCell) {
return i.number[0], i.number[1]
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetNumber(number string) {
i.number[1].Value = number
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Date() (*InvoiceCell, *InvoiceCell) {
return i.date[0], i.date[1]
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetDate(date string) {
i.date[1].Value = date
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) DueDate() (*InvoiceCell, *InvoiceCell) {
return i.dueDate[0], i.dueDate[1]
2018-10-26 12:29:40 +03:00
}
func (i *Invoice) SetDueDate(dueDate string) {
2018-10-30 20:43:10 +02:00
i.dueDate[1].Value = dueDate
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) AddInvoiceInfo(description, value string) (*InvoiceCell, *InvoiceCell) {
info := [2]*InvoiceCell{
i.newCell(description, i.infoProps),
i.newCell(value, i.infoProps),
}
i.info = append(i.info, info)
return info[0], info[1]
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) AppendColumn(description string) *InvoiceCell {
2018-10-26 12:29:40 +03:00
return nil
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) InsertColumn(description string) *InvoiceCell {
2018-10-26 12:29:40 +03:00
return nil
}
func (i *Invoice) Lines() [][]*InvoiceCell {
return i.lines
}
func (i *Invoice) AddLine(values ...string) {
lenCols := len(i.columns)
var line []*InvoiceCell
for j, value := range values {
2018-10-30 20:43:10 +02:00
itemCell := i.newCell(value, i.itemProps)
2018-10-26 12:29:40 +03:00
if j < lenCols {
itemCell.Alignment = i.columns[j].Alignment
}
line = append(line, itemCell)
}
i.lines = append(i.lines, line)
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Subtotal() (*InvoiceCell, *InvoiceCell) {
return i.subtotal[0], i.subtotal[1]
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetSubtotal(value string) {
i.subtotal[1].Value = value
2018-10-26 12:29:40 +03:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Total() (*InvoiceCell, *InvoiceCell) {
return i.total[0], i.total[1]
2018-10-29 20:18:32 +02:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetTotal(value string) {
i.total[1].Value = value
2018-10-29 20:18:32 +02:00
}
func (i *Invoice) TotalLines() [][2]*InvoiceCell {
return i.totals
}
func (i *Invoice) AddTotalLine(desc, value string) (*InvoiceCell, *InvoiceCell) {
descCell := &InvoiceCell{
i.totalProps,
desc,
}
valueCell := &InvoiceCell{
i.totalProps,
value,
}
i.totals = append(i.totals, [2]*InvoiceCell{descCell, valueCell})
return descCell, valueCell
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Notes() (string, string) {
return i.notes[0], i.notes[1]
2018-10-29 20:18:32 +02:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetNotes(title, content string) {
i.notes = [2]string{
title,
content,
}
2018-10-29 20:18:32 +02:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) Terms() (string, string) {
return i.terms[0], i.terms[1]
2018-10-29 20:18:32 +02:00
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) SetTerms(title, content string) {
i.terms = [2]string{
title,
content,
}
}
func (i *Invoice) Sections() [][2]string {
return i.sections
}
func (i *Invoice) AddSection(title, content string) {
i.sections = append(i.sections, [2]string{
title,
content,
})
}
func (i *Invoice) NewCellProps() InvoiceCellProps {
white := ColorRGBFrom8bit(255, 255, 255)
return InvoiceCellProps{
TextStyle: i.defaultStyle,
Alignment: CellHorizontalAlignmentLeft,
BackgroundColor: white,
BorderColor: white,
BorderWidth: 1,
BorderStyle: int(CellBorderSideAll),
}
2018-10-26 12:29:40 +03:00
}
func (i *Invoice) NewCell(value string) *InvoiceCell {
2018-10-30 20:43:10 +02:00
return i.newCell(value, i.NewCellProps())
}
func (i *Invoice) newCell(value string, props InvoiceCellProps) *InvoiceCell {
2018-10-26 12:29:40 +03:00
return &InvoiceCell{
2018-10-30 20:43:10 +02:00
props,
2018-10-26 12:29:40 +03:00
value,
}
}
2018-10-30 20:43:10 +02:00
func (i *Invoice) NewColumn(description string) *InvoiceCell {
return i.newColumn(description, CellHorizontalAlignmentLeft)
}
func (i *Invoice) newColumn(description string, alignment CellHorizontalAlignment) *InvoiceCell {
col := &InvoiceCell{i.colProps, description}
col.Alignment = alignment
return col
2018-10-26 12:29:40 +03:00
}
func (i *Invoice) drawAddress(title, name string, addr *InvoiceAddress) []*StyledParagraph {
var paragraphs []*StyledParagraph
// Address title.
if title != "" {
titleParagraph := newStyledParagraph(i.headingStyle)
titleParagraph.SetMargins(0, 0, 0, 7)
titleParagraph.Append(title)
paragraphs = append(paragraphs, titleParagraph)
}
// Address information.
addressParagraph := newStyledParagraph(i.defaultStyle)
addressParagraph.SetLineHeight(1.2)
city := addr.City
if addr.Zip != "" {
if city != "" {
city += ", "
}
city += addr.Zip
}
if name != "" {
addressParagraph.Append(addr.Name + "\n")
}
if addr.Street != "" {
addressParagraph.Append(addr.Street + "\n")
}
if city != "" {
addressParagraph.Append(city + "\n")
}
if addr.Country != "" {
addressParagraph.Append(addr.Country + "\n")
}
2018-10-30 20:43:10 +02:00
// Contact information.
2018-10-26 12:29:40 +03:00
contactParagraph := newStyledParagraph(i.defaultStyle)
contactParagraph.SetLineHeight(1.2)
contactParagraph.SetMargins(0, 0, 7, 0)
if addr.Phone != "" {
contactParagraph.Append(fmt.Sprintf("Phone: %s\n", addr.Phone))
}
if addr.Email != "" {
contactParagraph.Append(fmt.Sprintf("Email: %s\n", addr.Email))
}
paragraphs = append(paragraphs, addressParagraph, contactParagraph)
return paragraphs
}
2018-10-29 20:18:32 +02:00
func (i *Invoice) drawSection(title, content string) []*StyledParagraph {
var paragraphs []*StyledParagraph
// Title paragraph.
if title != "" {
titleParagraph := newStyledParagraph(i.headingStyle)
titleParagraph.SetMargins(0, 0, 0, 5)
titleParagraph.Append(title)
paragraphs = append(paragraphs, titleParagraph)
}
// Content paragraph.
if content != "" {
contentParagraph := newStyledParagraph(i.defaultStyle)
contentParagraph.Append(content)
paragraphs = append(paragraphs, contentParagraph)
}
return paragraphs
}
2018-10-26 12:29:40 +03:00
func (i *Invoice) drawInformation() *Table {
table := newTable(2)
2018-10-30 20:43:10 +02:00
info := append([][2]*InvoiceCell{
i.number,
i.date,
i.dueDate,
}, i.info...)
2018-10-26 12:29:40 +03:00
for _, v := range info {
description, value := v[0], v[1]
2018-10-30 20:43:10 +02:00
if value.Value == "" {
2018-10-26 12:29:40 +03:00
continue
}
// Add description.
cell := table.NewCell()
2018-10-30 20:43:10 +02:00
cell.SetBackgroundColor(description.BackgroundColor)
2018-10-26 12:29:40 +03:00
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
2018-10-30 20:43:10 +02:00
cell.SetBorderColor(description.BorderColor)
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
p := newStyledParagraph(description.TextStyle)
p.Append(description.Value)
2018-10-26 12:29:40 +03:00
p.SetMargins(0, 0, 2, 0)
cell.SetContent(p)
// Add value.
cell = table.NewCell()
2018-10-30 20:43:10 +02:00
cell.SetBackgroundColor(value.BackgroundColor)
2018-10-26 12:29:40 +03:00
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
2018-10-30 20:43:10 +02:00
cell.SetBorderColor(value.BorderColor)
2018-10-26 12:29:40 +03:00
2018-10-30 20:43:10 +02:00
p = newStyledParagraph(value.TextStyle)
p.Append(value.Value)
2018-10-26 12:29:40 +03:00
p.SetMargins(0, 0, 2, 0)
cell.SetContent(p)
}
return table
}
2018-10-29 20:18:32 +02:00
func (i *Invoice) drawTotals() *Table {
table := newTable(2)
2018-10-30 20:43:10 +02:00
totals := [][2]*InvoiceCell{i.subtotal}
2018-10-29 20:18:32 +02:00
totals = append(totals, i.totals...)
2018-10-30 20:43:10 +02:00
totals = append(totals, i.total)
2018-10-29 20:18:32 +02:00
for _, total := range totals {
description, value := total[0], total[1]
2018-10-30 20:43:10 +02:00
if value.Value == "" {
continue
}
2018-10-29 20:18:32 +02:00
// Add description.
cell := table.NewCell()
cell.SetBackgroundColor(description.BackgroundColor)
cell.SetHorizontalAlignment(value.Alignment)
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
cell.SetBorderColor(value.BorderColor)
2018-10-30 20:43:10 +02:00
p := newStyledParagraph(description.TextStyle)
2018-10-29 20:18:32 +02:00
p.SetMargins(0, 0, 1, 1)
p.Append(description.Value)
cell.SetContent(p)
// Add value.
cell = table.NewCell()
cell.SetBackgroundColor(value.BackgroundColor)
cell.SetHorizontalAlignment(value.Alignment)
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
cell.SetBorderColor(value.BorderColor)
2018-10-30 20:43:10 +02:00
p = newStyledParagraph(value.TextStyle)
2018-10-29 20:18:32 +02:00
p.SetMargins(0, 0, 1, 1)
p.Append(value.Value)
cell.SetContent(p)
}
return table
}
2018-10-26 12:29:40 +03:00
func (i *Invoice) generateHeaderBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
// Create title paragraph.
titleParagraph := newStyledParagraph(i.titleStyle)
titleParagraph.SetEnableWrap(true)
titleParagraph.Append(i.title)
// Add invoice logo.
table := newTable(2)
if i.logo != nil {
cell := table.NewCell()
cell.SetHorizontalAlignment(CellHorizontalAlignmentLeft)
cell.SetVerticalAlignment(CellVerticalAlignmentMiddle)
cell.SetContent(i.logo)
i.logo.ScaleToHeight(titleParagraph.Height() + 20)
} else {
table.SkipCells(1)
}
// Add invoice title.
cell := table.NewCell()
cell.SetHorizontalAlignment(CellHorizontalAlignmentRight)
cell.SetVerticalAlignment(CellVerticalAlignmentMiddle)
cell.SetContent(titleParagraph)
// Generate blocks.
return table.GeneratePageBlocks(ctx)
}
func (i *Invoice) generateInformationBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
// Draw addresses.
separatorParagraph := newStyledParagraph(i.defaultStyle)
separatorParagraph.SetMargins(0, 0, 0, 20)
2018-10-30 20:43:10 +02:00
addrParagraphs := i.drawAddress(i.sellerAddress.Name, "", i.sellerAddress)
2018-10-26 12:29:40 +03:00
addrParagraphs = append(addrParagraphs, separatorParagraph)
addrParagraphs = append(addrParagraphs,
2018-10-30 20:43:10 +02:00
i.drawAddress("Bill to", i.buyerAddress.Name, i.buyerAddress)...)
2018-10-26 12:29:40 +03:00
addrDivision := newDivision()
for _, addrParagraph := range addrParagraphs {
addrDivision.Add(addrParagraph)
}
// Draw invoice information.
information := i.drawInformation()
// Generate blocks.
table := newTable(2)
table.SetMargins(0, 0, 25, 0)
cell := table.NewCell()
cell.SetContent(addrDivision)
cell = table.NewCell()
cell.SetContent(information)
return table.GeneratePageBlocks(ctx)
}
func (i *Invoice) generateLineBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
table := newTable(4)
table.SetMargins(0, 0, 25, 0)
2018-10-30 20:43:10 +02:00
// Draw item columns.
2018-10-26 12:29:40 +03:00
for _, col := range i.columns {
2018-10-30 20:43:10 +02:00
paragraph := newStyledParagraph(col.TextStyle)
2018-10-29 20:18:32 +02:00
paragraph.SetMargins(0, 0, 1, 0)
2018-10-30 20:43:10 +02:00
paragraph.Append(col.Value)
2018-10-26 12:29:40 +03:00
cell := table.NewCell()
cell.SetHorizontalAlignment(col.Alignment)
cell.SetBackgroundColor(col.BackgroundColor)
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
cell.SetBorderColor(col.BorderColor)
cell.SetContent(paragraph)
}
2018-10-30 20:43:10 +02:00
// Draw item lines.
2018-10-26 12:29:40 +03:00
for _, line := range i.lines {
for _, itemCell := range line {
2018-10-30 20:43:10 +02:00
paragraph := newStyledParagraph(itemCell.TextStyle)
2018-10-29 20:18:32 +02:00
paragraph.SetMargins(0, 0, 1, 0)
2018-10-26 12:29:40 +03:00
paragraph.Append(itemCell.Value)
cell := table.NewCell()
cell.SetHorizontalAlignment(itemCell.Alignment)
cell.SetBackgroundColor(itemCell.BackgroundColor)
cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
cell.SetBorderColor(itemCell.BorderColor)
cell.SetContent(paragraph)
}
}
return table.GeneratePageBlocks(ctx)
}
2018-10-29 20:18:32 +02:00
func (i *Invoice) generateTotalBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
table := newTable(2)
table.SetMargins(0, 0, 5, 35)
2018-10-30 20:43:10 +02:00
table.SkipCells(1)
2018-10-29 20:18:32 +02:00
totalsTable := i.drawTotals()
totalsTable.SetMargins(0, 0, 5, 0)
2018-10-30 20:43:10 +02:00
cell := table.NewCell()
2018-10-29 20:18:32 +02:00
cell.SetContent(totalsTable)
return table.GeneratePageBlocks(ctx)
}
func (i *Invoice) generateNoteBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
division := newDivision()
2018-10-30 20:43:10 +02:00
sections := [][2]string{
i.notes,
i.terms,
}
sections = append(sections, i.sections...)
for _, section := range sections {
if section[1] != "" {
paragraphs := i.drawSection(section[0], section[1])
for _, paragraph := range paragraphs {
division.Add(paragraph)
}
sepParagraph := newStyledParagraph(i.defaultStyle)
sepParagraph.SetMargins(0, 0, 20, 0)
division.Add(sepParagraph)
2018-10-29 20:18:32 +02:00
}
}
return division.GeneratePageBlocks(ctx)
}
2018-10-26 12:29:40 +03:00
// GeneratePageBlocks generate the Page blocks. Multiple blocks are generated
// if the contents wrap over multiple pages.
func (i *Invoice) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
origCtx := ctx
2018-10-29 20:18:32 +02:00
blockFuncs := []func(ctx DrawContext) ([]*Block, DrawContext, error){
i.generateHeaderBlocks,
i.generateInformationBlocks,
i.generateLineBlocks,
i.generateTotalBlocks,
i.generateNoteBlocks,
2018-10-26 12:29:40 +03:00
}
2018-10-29 20:18:32 +02:00
var blocks []*Block
for _, blockFunc := range blockFuncs {
newBlocks, c, err := blockFunc(ctx)
if err != nil {
return blocks, ctx, err
}
2018-10-26 12:29:40 +03:00
2018-10-29 20:18:32 +02:00
if len(blocks) == 0 {
blocks = newBlocks
} else if len(newBlocks) > 0 {
blocks[len(blocks)-1].mergeBlocks(newBlocks[0])
blocks = append(blocks, newBlocks[1:]...)
}
2018-10-26 12:29:40 +03:00
2018-10-29 20:18:32 +02:00
ctx = c
2018-10-26 12:29:40 +03:00
}
if i.positioning.isRelative() {
// Move back X to same start of line.
ctx.X = origCtx.X
}
if i.positioning.isAbsolute() {
// If absolute: return original context.
return blocks, origCtx, nil
}
return blocks, ctx, nil
}