From 52e3699d2477d161f4306df2054931b8cd07381b Mon Sep 17 00:00:00 2001 From: Johnathan Fercher Date: Sun, 16 Jun 2019 19:17:47 -0300 Subject: [PATCH 1/3] feature/remove-label-add-line-config Done * Remove label from Col, Row and TableList * Rename RowTableList to TableList * Add parameter on Line Why * Label did nothing, code comment will do the same * TableList is simpler to write * It's a good thing the line receive the size of space --- README.md | 49 ++- code.go | 18 +- example_test.go | 30 +- font.go | 44 +- image.go | 18 +- maroto.go | 257 ++++++------ maroto_test.go | 558 ++++++++++++++++++++++---- math.go | 22 +- mocks/maroto.go | 2 +- structs.go => properties.go | 0 structs_test.go => properties_test.go | 0 signature.go | 10 +- text.go | 14 +- 13 files changed, 723 insertions(+), 299 deletions(-) rename structs.go => properties.go (100%) rename structs_test.go => properties_test.go (100%) diff --git a/README.md b/README.md index c70e35d1..5b950b0e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Maroto [![Travis](https://img.shields.io/badge/coverage-77.2%25-green.svg)][travis] +# Maroto [![Travis](https://img.shields.io/badge/coverage-84.4%25-green.svg)][travis] A Maroto way to create PDFs. Maroto is inspired in Bootstrap and uses [Gofpdf](https://github.com/jung-kurt/gofpdf). Fast and simple. > Maroto definition: Brazilian expression, means an astute/clever/intelligent person. @@ -14,6 +14,7 @@ Here is the [pdf](assets/pdf/maroto.pdf) generated. ```go func main() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) + //m.SetDebugMode(true) byteSlices, _ := ioutil.ReadFile("assets/images/gopher2.png") @@ -21,8 +22,9 @@ func main() { header, contents := getContents() - m.Row("Codes", 20, func() { - m.Col("Logo", func() { + // Header 1 + m.Row(20, func() { + m.Col(func() { m.Base64Image(base64, maroto.Png, &maroto.RectProp{ Percent: 85, }) @@ -30,13 +32,13 @@ func main() { m.ColSpaces(2) - m.Col("Link", func() { + m.Col(func() { m.QrCode("https://github.com/johnfercher/maroto", &maroto.RectProp{ Percent: 75, }) }) - m.Col("Barcode", func() { + m.Col(func() { id := "123456789" _ = m.Barcode(id, &maroto.RectProp{ Percent: 70, @@ -49,39 +51,41 @@ func main() { }) }) - m.Line() + m.Line(1.0) - m.Row("Logo", 12, func() { - m.Col("Logo", func() { + // Header 2 + m.Row(12, func() { + m.Col(func() { m.FileImage("assets/images/gopher1.jpg", nil) }) m.ColSpace() - m.Col("Definition", func() { + m.Col(func() { m.Text("PDFGenerator: Maroto", &maroto.TextProp{ Top: 4, }) m.Text("Type: Easy & Fast", &maroto.TextProp{ - Top: 9.5, + Top: 10, }) }) m.ColSpace() - m.Col("Speed", func() { + m.Col(func() { m.Text("GPL3", &maroto.TextProp{ Size: 16, Style: maroto.Bold, - Top: 7.5, + Top: 8, }) }) }) - m.Line() + m.Line(1.0) - m.Row("SubTitle", 22, func() { - m.Col("Packages", func() { + // Header 3 + m.Row(22, func() { + m.Col(func() { m.Text("Grid System", &maroto.TextProp{ Size: 18, Style: maroto.Bold, @@ -91,25 +95,26 @@ func main() { m.Text("Bootstrap Like", &maroto.TextProp{ Size: 12, Align: maroto.Center, - Top: 16, + Top: 17, }) }) }) - m.Line() + m.Line(1.0) - m.RowTableList("List", header, contents, nil) + m.TableList(header, contents, nil) - m.Row("Signature", 30, func() { - m.Col("Nick", func() { + // Signatures + m.Row(30, func() { + m.Col(func() { m.Signature("Nick Fury", nil) }) - m.Col("Thanos", func() { + m.Col(func() { m.Signature("Thanos", nil) }) - m.Col("Collector", func() { + m.Col(func() { m.Signature("Collector", nil) }) }) diff --git a/code.go b/code.go index 91c09cf7..005f85db 100644 --- a/code.go +++ b/code.go @@ -24,27 +24,27 @@ func NewCode(pdf gofpdf.Pdf, math Math) Code { } } -func (c *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) { - key := barcode.RegisterQR(c.pdf, code, qr.H, qr.Unicode) +func (self *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) { + key := barcode.RegisterQR(self.pdf, code, qr.H, qr.Unicode) - actualWidthPerCol := c.math.GetWidthPerCol(qtdCols) + actualWidthPerCol := self.math.GetWidthPerCol(qtdCols) - x, y, w, h := c.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, percent) + x, y, w, h := self.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, percent) - barcode.Barcode(c.pdf, key, x, y+marginTop, w, h, false) + barcode.Barcode(self.pdf, key, x, y+marginTop, w, h, false) } -func (c *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) (err error) { +func (self *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) (err error) { bcode, err := code128.Encode(code) if err != nil { return } - actualWidthPerCol := c.math.GetWidthPerCol(qtdCols) + actualWidthPerCol := self.math.GetWidthPerCol(qtdCols) - x, y, w, h := c.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*0.33, qtdCols, colHeight, indexCol, percent) + x, y, w, h := self.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*0.33, qtdCols, colHeight, indexCol, percent) - barcode.Barcode(c.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false) + barcode.Barcode(self.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false) return } diff --git a/example_test.go b/example_test.go index 92335d6c..641b3b2e 100644 --- a/example_test.go +++ b/example_test.go @@ -7,7 +7,7 @@ import "github.com/johnfercher/maroto" func ExamplePdfMaroto_Line() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) - m.Line() + m.Line(1.0) // Do more things and save... } @@ -17,7 +17,7 @@ func ExamplePdfMaroto_Row() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { + m.Row(rowHeight, func() { // ... Add some columns }) @@ -30,7 +30,7 @@ func ExamplePdfMaroto_ColSpace() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { + m.Row(rowHeight, func() { m.ColSpace() }) @@ -43,7 +43,7 @@ func ExamplePdfMaroto_ColSpaces() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { + m.Row(rowHeight, func() { m.ColSpaces(2) }) @@ -56,8 +56,8 @@ func ExamplePdfMaroto_Col() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { - m.Col("MyCol", func() { + m.Row(rowHeight, func() { + m.Col(func() { // Add Image, Text, Signature, QrCode or Barcode... }) }) @@ -109,8 +109,8 @@ func ExamplePdfMaroto_Text() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { - m.Col("MyCol", func() { + m.Row(rowHeight, func() { + m.Col(func() { m.Text("TextContent", &maroto.TextProp{ Size: 12.0, Style: maroto.BoldItalic, @@ -134,8 +134,8 @@ func ExamplePdfMaroto_Signature() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { - m.Col("MyCol", func() { + m.Row(rowHeight, func() { + m.Col(func() { m.Signature("LabelForSignature", &maroto.SignatureProp{ Size: 12.0, Style: maroto.BoldItalic, @@ -161,7 +161,7 @@ func ExamplePdfMaroto_RowTableList() { // 1 Row of header // 2 Rows of contents // Each row have 2 columns - m.RowTableList("RowTableList1", headers, contents, nil) + m.TableList(headers, contents, nil) // Do more things and save... } @@ -179,8 +179,8 @@ func ExamplePdfMaroto_FileImage() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) rowHeight := 5.0 - m.Row("MyRow", rowHeight, func() { - m.Col("MyCol", func() { + m.Row(rowHeight, func() { + m.Col(func() { m.FileImage("path/Image.jpg", &maroto.RectProp{ Left: 5, Top: 5, @@ -207,8 +207,8 @@ func ExamplePdfMaroto_Base64Image() { rowHeight := 5.0 base64String := "y7seWGHE923Sdgs..." - m.Row("MyRow", rowHeight, func() { - m.Col("MyCol", func() { + m.Row(rowHeight, func() { + m.Col(func() { m.Base64Image(base64String, maroto.Png, &maroto.RectProp{ Left: 5, Top: 5, diff --git a/font.go b/font.go index 7842b2ff..fba20e7f 100644 --- a/font.go +++ b/font.go @@ -34,50 +34,50 @@ func NewFont(pdf gofpdf.Pdf, size float64, family Family, style Style) Font { } // Get the currently Font family configured -func (f *font) GetFamily() Family { - return f.family +func (self *font) GetFamily() Family { + return self.family } // Get the currently Font style configured -func (f *font) GetStyle() Style { - return f.style +func (self *font) GetStyle() Style { + return self.style } // Get the currently Font size configured -func (f *font) GetSize() float64 { - return f.size +func (self *font) GetSize() float64 { + return self.size } // Get all the currently Font properties configured -func (f *font) GetFont() (Family, Style, float64) { - return f.family, f.style, f.size +func (self *font) GetFont() (Family, Style, float64) { + return self.family, self.style, self.size } // Set the Font family -func (f *font) SetFamily(family Family) { - f.family = family +func (self *font) SetFamily(family Family) { + self.family = family - f.pdf.SetFont(string(f.family), string(f.style), f.size) + self.pdf.SetFont(string(self.family), string(self.style), self.size) } // Set the Font style -func (f *font) SetStyle(style Style) { - f.style = style +func (self *font) SetStyle(style Style) { + self.style = style - f.pdf.SetFontStyle(string(f.style)) + self.pdf.SetFontStyle(string(self.style)) } // Set the Font size -func (f *font) SetSize(size float64) { - f.size = size - f.pdf.SetFontSize(f.size) +func (self *font) SetSize(size float64) { + self.size = size + self.pdf.SetFontSize(self.size) } // Set all the Font properties -func (f *font) SetFont(family Family, style Style, size float64) { - f.family = family - f.style = style - f.size = size +func (self *font) SetFont(family Family, style Style, size float64) { + self.family = family + self.style = style + self.size = size - f.pdf.SetFont(string(f.family), string(f.style), f.size) + self.pdf.SetFont(string(self.family), string(self.style), self.size) } diff --git a/image.go b/image.go index 923cffa4..93b0a542 100644 --- a/image.go +++ b/image.go @@ -26,21 +26,21 @@ func NewImage(pdf gofpdf.Pdf, math Math) Image { } } -func (i *image) AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) { - info := i.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{ +func (self *image) AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) { + info := self.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{ ReadDpi: false, ImageType: "", }) - i.addImageToPdf(path, info, marginTop, qtdCols, colHeight, indexCol, percent) + self.addImageToPdf(path, info, marginTop, qtdCols, colHeight, indexCol, percent) } -func (i *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64, extension Extension) { +func (self *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64, extension Extension) { imageId, _ := uuid.NewRandom() ss, _ := base64.StdEncoding.DecodeString(b64) - info := i.pdf.RegisterImageOptionsReader( + info := self.pdf.RegisterImageOptionsReader( imageId.String(), gofpdf.ImageOptions{ ReadDpi: false, @@ -49,10 +49,10 @@ func (i *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, q bytes.NewReader(ss), ) - i.addImageToPdf(imageId.String(), info, marginTop, qtdCols, colHeight, indexCol, percent) + self.addImageToPdf(imageId.String(), info, marginTop, qtdCols, colHeight, indexCol, percent) } -func (i *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, marginTop, qtdCols, colHeight, indexCol, percent float64) { - x, y, w, h := i.math.GetRectCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, percent) - i.pdf.Image(imageLabel, x, y+marginTop, w, h, false, "", 0, "") +func (self *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, marginTop, qtdCols, colHeight, indexCol, percent float64) { + x, y, w, h := self.math.GetRectCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, percent) + self.pdf.Image(imageLabel, x, y+marginTop, w, h, false, "", 0, "") } diff --git a/maroto.go b/maroto.go index 2fc9af7e..c3848d36 100644 --- a/maroto.go +++ b/maroto.go @@ -5,11 +5,13 @@ import ( "github.com/jung-kurt/gofpdf" ) +const tableListTag = "header" + // Maroto is the principal abstraction to create a PDF document. type Maroto interface { // Grid System - Row(label string, height float64, closure func()) - Col(label string, closure func()) + Row(height float64, closure func()) + Col(closure func()) ColSpace() ColSpaces(qtd int) @@ -18,15 +20,17 @@ type Maroto interface { GetDebugMode() bool GetPageSize() (float64, float64) - // Components - RowTableList(label string, headers []string, contents [][]string, tableListProp *TableListProp) - Text(text string, fontProp *TextProp) - FileImage(filePathName string, rectProp *RectProp) - Base64Image(base64 string, extension Extension, rectProp *RectProp) - Barcode(code string, rectProp *RectProp) error - QrCode(code string, rectProp *RectProp) - Signature(label string, signatureProp *SignatureProp) - Line() + // Outside Col/Row Components + TableList(header []string, contents [][]string, prop *TableListProp) + Line(spaceHeight float64) + + // Inside Col/Row Components + Text(text string, prop *TextProp) + FileImage(filePathName string, prop *RectProp) + Base64Image(base64 string, extension Extension, prop *RectProp) + Barcode(code string, prop *RectProp) error + QrCode(code string, prop *RectProp) + Signature(label string, prop *SignatureProp) // File System OutputFileAndClose(filePathName string) error @@ -88,68 +92,76 @@ func NewMaroto(orientation Orientation, pageSize PageSize) Maroto { // Add a Signature space with a label TextHelper inside a column. // Create a line with the width from a column // and add a Text at the bottom of the line. -func (m *PdfMaroto) Signature(label string, signatureProp *SignatureProp) { - if signatureProp == nil { - signatureProp = &SignatureProp{} +func (self *PdfMaroto) Signature(label string, prop *SignatureProp) { + if prop == nil { + prop = &SignatureProp{} } - signatureProp.MakeValid() + prop.MakeValid() - qtdCols := float64(len(m.colsClosures)) - sumOfYOffsets := m.offsetY + m.rowHeight + qtdCols := float64(len(self.colsClosures)) + sumOfYOffsets := self.offsetY + self.rowHeight - m.SignHelper.AddSpaceFor(label, signatureProp.Family, signatureProp.Style, signatureProp.Size, qtdCols, sumOfYOffsets, m.rowColCount) + self.SignHelper.AddSpaceFor(label, prop.Family, prop.Style, prop.Size, qtdCols, sumOfYOffsets, self.rowColCount) } // Create a table with multiple rows and columns. // Headers define the amount of columns from each row. // Headers have bold style, and localized at the top of table. // Contents are array of arrays. Each array is one line. -func (m *PdfMaroto) RowTableList(label string, headers []string, contents [][]string, tableListProp *TableListProp) { - if tableListProp == nil { - tableListProp = &TableListProp{} +func (self *PdfMaroto) TableList(header []string, contents [][]string, prop *TableListProp) { + if header == nil || len(header) == 0 { + return + } + + if contents == nil || len(contents) == 0 { + return } - tableListProp.MakeValid() + if prop == nil { + prop = &TableListProp{} + } + + prop.MakeValid() - m.Row("", tableListProp.HHeight, func() { + self.Row(prop.HHeight, func() { headerMarginTop := 2.0 - qtdCols := float64(len(headers)) + qtdCols := float64(len(header)) - for i, h := range headers { + for i, h := range header { hs := h is := i - m.Col("", func() { - if headerMarginTop > m.rowHeight { - headerMarginTop = m.rowHeight + self.Col(func() { + if headerMarginTop > self.rowHeight { + headerMarginTop = self.rowHeight } reason := hs - sumOyYOffesets := headerMarginTop + m.offsetY + 2.5 + sumOyYOffesets := headerMarginTop + self.offsetY + 2.5 - m.TextHelper.Add(reason, tableListProp.HFontFamily, tableListProp.HFontStyle, tableListProp.HFontSize, sumOyYOffesets, tableListProp.Align, float64(is), qtdCols) + self.TextHelper.Add(reason, prop.HFontFamily, prop.HFontStyle, prop.HFontSize, sumOyYOffesets, prop.Align, float64(is), qtdCols) }) } }) - m.Row("", tableListProp.Space, func() { - m.ColSpace() + self.Row(prop.Space, func() { + self.ColSpace() }) contentMarginTop := 2.0 for _, content := range contents { - m.Row("", tableListProp.CHeight, func() { + self.Row(prop.CHeight, func() { for j, c := range content { cs := c js := j - hs := float64(len(headers)) - sumOyYOffesets := contentMarginTop + m.offsetY + 2.0 + hs := float64(len(header)) + sumOyYOffesets := contentMarginTop + self.offsetY + 2.0 - m.Col("", func() { - m.TextHelper.Add(cs, tableListProp.CFontFamily, tableListProp.CFontStyle, tableListProp.CFontSize, sumOyYOffesets, tableListProp.Align, float64(js), hs) + self.Col(func() { + self.TextHelper.Add(cs, prop.CFontFamily, prop.CFontStyle, prop.CFontSize, sumOyYOffesets, prop.Align, float64(js), hs) }) } }) @@ -158,188 +170,193 @@ func (m *PdfMaroto) RowTableList(label string, headers []string, contents [][]st // Enable debug mode. // Draw borders in all columns created. -func (m *PdfMaroto) SetDebugMode(on bool) { - m.DebugMode = on +func (self *PdfMaroto) SetDebugMode(on bool) { + self.DebugMode = on } // Get actual debug mode. -func (m *PdfMaroto) GetDebugMode() bool { - return m.DebugMode +func (self *PdfMaroto) GetDebugMode() bool { + return self.DebugMode } // Get actual page size -func (m *PdfMaroto) GetPageSize() (float64, float64) { - return m.Pdf.GetPageSize() +func (self *PdfMaroto) GetPageSize() (float64, float64) { + return self.Pdf.GetPageSize() } // Draw a line from margin left to margin right // in the currently row. -func (m *PdfMaroto) Line() { - m.Row("", 1, func() { - m.Col("", func() { - width, _ := m.Pdf.GetPageSize() - left, top, right, _ := m.Pdf.GetMargins() +func (self *PdfMaroto) Line(spaceHeight float64) { + self.Row(spaceHeight, func() { + self.Col(func() { + width, _ := self.Pdf.GetPageSize() + left, top, right, _ := self.Pdf.GetMargins() - m.Pdf.Line(left, m.offsetY+top, width-right, m.offsetY+top) + self.Pdf.Line(left, self.offsetY+top+(spaceHeight/2.0), width-right, self.offsetY+top+(spaceHeight/2.0)) }) }) } // Add a row and enable add columns inside the row. -func (m *PdfMaroto) Row(label string, height float64, closure func()) { - m.rowHeight = height - m.rowColCount = 0 +func (self *PdfMaroto) Row(height float64, closure func()) { + self.rowHeight = height + self.rowColCount = 0 - _, pageHeight := m.Pdf.GetPageSize() - _, top, _, bottom := m.Pdf.GetMargins() + _, pageHeight := self.Pdf.GetPageSize() + _, top, _, bottom := self.Pdf.GetMargins() - if m.offsetY > pageHeight-bottom-top-m.rowHeight { - m.offsetY = 0 + if self.offsetY > pageHeight-bottom-top-self.rowHeight { + self.offsetY = 0 } closure() - for _, colClosure := range m.colsClosures { + for _, colClosure := range self.colsClosures { colClosure() } - m.colsClosures = nil - m.offsetY += m.rowHeight - m.Pdf.Ln(m.rowHeight) + self.colsClosures = nil + self.offsetY += self.rowHeight + self.Pdf.Ln(self.rowHeight) } // Create a column inside a row and enable to add // components inside. -func (m *PdfMaroto) Col(label string, closure func()) { - m.colsClosures = append(m.colsClosures, func() { - widthPerCol := m.Math.GetWidthPerCol(float64(len(m.colsClosures))) - m.createColSpace(widthPerCol) +func (self *PdfMaroto) Col(closure func()) { + self.colsClosures = append(self.colsClosures, func() { + widthPerCol := self.Math.GetWidthPerCol(float64(len(self.colsClosures))) + self.createColSpace(widthPerCol) closure() - m.rowColCount++ + self.rowColCount++ }) } // Create an empty column inside a row. -func (m *PdfMaroto) ColSpace() { - m.colsClosures = append(m.colsClosures, func() { - widthPerCol := m.Math.GetWidthPerCol(float64(len(m.colsClosures))) - m.createColSpace(widthPerCol) - m.rowColCount++ +func (self *PdfMaroto) ColSpace() { + self.colsClosures = append(self.colsClosures, func() { + widthPerCol := self.Math.GetWidthPerCol(float64(len(self.colsClosures))) + self.createColSpace(widthPerCol) + self.rowColCount++ }) } // Create some empty columns. -func (m *PdfMaroto) ColSpaces(qtd int) { +func (self *PdfMaroto) ColSpaces(qtd int) { for i := 0; i < qtd; i++ { - m.ColSpace() + self.ColSpace() } } // Add a Text inside a column. -func (m *PdfMaroto) Text(text string, fontProp *TextProp) { - if fontProp == nil { - fontProp = &TextProp{} +func (self *PdfMaroto) Text(text string, prop *TextProp) { + if prop == nil { + prop = &TextProp{} } - fontProp.MakeValid() + prop.MakeValid() - if fontProp.Top > m.rowHeight { - fontProp.Top = m.rowHeight + if prop.Top > self.rowHeight { + prop.Top = self.rowHeight } - sumOfYOffsets := fontProp.Top + m.offsetY + sumOfYOffsets := prop.Top + self.offsetY - m.TextHelper.Add(text, fontProp.Family, fontProp.Style, fontProp.Size, sumOfYOffsets, fontProp.Align, m.rowColCount, float64(len(m.colsClosures))) + self.TextHelper.Add(text, prop.Family, prop.Style, prop.Size, sumOfYOffsets, prop.Align, self.rowColCount, float64(len(self.colsClosures))) } // Add an Image reading from disk inside a column. // Defining Image properties. -func (m *PdfMaroto) FileImage(filePathName string, rectProp *RectProp) { - if rectProp == nil { - rectProp = &RectProp{} +func (self *PdfMaroto) FileImage(filePathName string, prop *RectProp) { + if prop == nil { + prop = &RectProp{} } - rectProp.MakeValid() + prop.MakeValid() - qtdCols := float64(len(m.colsClosures)) + qtdCols := float64(len(self.colsClosures)) + sumOfyOffsets := self.offsetY + prop.Top - if rectProp.Center { - m.Image.AddFromFile(filePathName, m.offsetY, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent) + if prop.Center { + self.Image.AddFromFile(filePathName, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) } else { - m.Image.AddFromFile(filePathName, m.offsetY, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent) + self.Image.AddFromFile(filePathName, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) } } // Add an Image reading byte slices. // Defining Image properties. -func (m *PdfMaroto) Base64Image(base64 string, extension Extension, rectProp *RectProp) { - if rectProp == nil { - rectProp = &RectProp{} +func (self *PdfMaroto) Base64Image(base64 string, extension Extension, prop *RectProp) { + if prop == nil { + prop = &RectProp{} } - rectProp.MakeValid() + prop.MakeValid() - qtdCols := float64(len(m.colsClosures)) - sumOfyOffsets := m.offsetY + rectProp.Top + qtdCols := float64(len(self.colsClosures)) + sumOfyOffsets := self.offsetY + prop.Top - if rectProp.Center { - m.Image.AddFromBase64(base64, sumOfyOffsets, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent, extension) + if prop.Center { + self.Image.AddFromBase64(base64, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent, extension) } else { - m.Image.AddFromBase64(base64, sumOfyOffsets, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent, extension) + self.Image.AddFromBase64(base64, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent, extension) } } // Save pdf in disk. -func (m *PdfMaroto) OutputFileAndClose(filePathName string) (err error) { - err = m.Pdf.OutputFileAndClose(filePathName) +func (self *PdfMaroto) OutputFileAndClose(filePathName string) (err error) { + err = self.Pdf.OutputFileAndClose(filePathName) return } // Get PDF in byte slices -func (m *PdfMaroto) Output() (bytes.Buffer, error) { +func (self *PdfMaroto) Output() (bytes.Buffer, error) { var buffer bytes.Buffer - err := m.Pdf.Output(&buffer) + err := self.Pdf.Output(&buffer) return buffer, err } -func (m *PdfMaroto) Barcode(code string, rectProp *RectProp) (err error) { - if rectProp == nil { - rectProp = &RectProp{} +func (self *PdfMaroto) Barcode(code string, prop *RectProp) (err error) { + if prop == nil { + prop = &RectProp{} } - rectProp.MakeValid() + prop.MakeValid() - qtdCols := float64(len(m.colsClosures)) - sumOfyOffsets := m.offsetY + rectProp.Top + qtdCols := float64(len(self.colsClosures)) + sumOfyOffsets := self.offsetY + prop.Top - err = m.Code.AddBar(code, sumOfyOffsets, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent) + if prop.Center { + err = self.Code.AddBar(code, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) + } else { + err = self.Code.AddBar(code, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) + } return } -func (m *PdfMaroto) QrCode(code string, rectProp *RectProp) { - if rectProp == nil { - rectProp = &RectProp{} +func (self *PdfMaroto) QrCode(code string, prop *RectProp) { + if prop == nil { + prop = &RectProp{} } - rectProp.MakeValid() + prop.MakeValid() - qtdCols := float64(len(m.colsClosures)) - sumOfyOffsets := m.offsetY + rectProp.Top + qtdCols := float64(len(self.colsClosures)) + sumOfyOffsets := self.offsetY + prop.Top - if rectProp.Center { - m.Code.AddQr(code, sumOfyOffsets, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent) + if prop.Center { + self.Code.AddQr(code, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) } else { - m.Code.AddQr(code, sumOfyOffsets, m.rowColCount, qtdCols, m.rowHeight, rectProp.Percent) + self.Code.AddQr(code, sumOfyOffsets, self.rowColCount, qtdCols, self.rowHeight, prop.Percent) } } -func (m *PdfMaroto) createColSpace(actualWidthPerCol float64) { +func (self *PdfMaroto) createColSpace(actualWidthPerCol float64) { border := "" - if m.DebugMode { + if self.DebugMode { border = "1" } - m.Pdf.CellFormat(actualWidthPerCol, m.rowHeight, "", border, 0.0, "C", false, 0.0, "") + self.Pdf.CellFormat(actualWidthPerCol, self.rowHeight, "", border, 0.0, "C", false, 0.0, "") } diff --git a/maroto_test.go b/maroto_test.go index c0551986..08d78268 100644 --- a/maroto_test.go +++ b/maroto_test.go @@ -164,10 +164,10 @@ func TestPdfMaroto_SetGetDebugMode(t *testing.T) { func TestPdfMaroto_Signature(t *testing.T) { cases := []struct { - name string - signature func() *mocks.Signature - assertSignature func(t *testing.T, signature *mocks.Signature) - actSignature func(m maroto.Maroto) + name string + signature func() *mocks.Signature + assert func(t *testing.T, signature *mocks.Signature) + act func(m maroto.Maroto) }{ { "One signature inside one column, inside a row, without props", @@ -181,8 +181,8 @@ func TestPdfMaroto_Signature(t *testing.T) { signature.AssertCalled(t, "AddSpaceFor", "Signature", maroto.Arial, maroto.Bold, 8.0, 1.0, 40.0, 0.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Signature("Signature", nil) }) }) @@ -201,8 +201,8 @@ func TestPdfMaroto_Signature(t *testing.T) { signature.AssertCalled(t, "AddSpaceFor", "Signature2", maroto.Courier, maroto.BoldItalic, 9.5, 1.0, 40.0, 0.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Signature("Signature", nil) m.Signature("Signature2", &maroto.SignatureProp{ Family: maroto.Courier, @@ -226,11 +226,11 @@ func TestPdfMaroto_Signature(t *testing.T) { signature.AssertCalled(t, "AddSpaceFor", "Signature2", maroto.Courier, maroto.BoldItalic, 9.5, 2.0, 40.0, 1.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Signature("Signature", nil) }) - m.Col("Col", func() { + m.Col(func() { m.Signature("Signature2", &maroto.SignatureProp{ Family: maroto.Courier, Style: maroto.BoldItalic, @@ -253,13 +253,13 @@ func TestPdfMaroto_Signature(t *testing.T) { signature.AssertCalled(t, "AddSpaceFor", "Signature2", maroto.Courier, maroto.BoldItalic, 9.5, 1.0, 80.0, 0.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Signature("Signature", nil) }) }) - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Signature("Signature2", &maroto.SignatureProp{ Family: maroto.Courier, Style: maroto.BoldItalic, @@ -279,19 +279,19 @@ func TestPdfMaroto_Signature(t *testing.T) { m := newMarotoTest(pdf, math, nil, nil, signature, nil, nil) // Act - c.actSignature(m) + c.act(m) // Assert - c.assertSignature(t, signature) + c.assert(t, signature) } } func TestPdfMaroto_Text(t *testing.T) { cases := []struct { - name string - text func() *mocks.Text - assertSignature func(t *testing.T, signature *mocks.Text) - actSignature func(m maroto.Maroto) + name string + text func() *mocks.Text + assert func(t *testing.T, signature *mocks.Text) + act func(m maroto.Maroto) }{ { "One text inside one column, inside a row, without props", @@ -305,8 +305,8 @@ func TestPdfMaroto_Text(t *testing.T) { text.AssertCalled(t, "Add", "Text", maroto.Arial, maroto.Normal, 10.0, 0.0, maroto.Left, 0.0, 1.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text", nil) }) }) @@ -325,8 +325,8 @@ func TestPdfMaroto_Text(t *testing.T) { text.AssertCalled(t, "Add", "Text2", maroto.Courier, maroto.BoldItalic, 9.5, 5.0, maroto.Center, 0.0, 1.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text", nil) m.Text("Text2", &maroto.TextProp{ Family: maroto.Courier, @@ -352,11 +352,11 @@ func TestPdfMaroto_Text(t *testing.T) { text.AssertCalled(t, "Add", "Text2", maroto.Helvetica, maroto.Italic, 8.5, 4.4, maroto.Center, 1.0, 2.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text", nil) }) - m.Col("Col", func() { + m.Col(func() { m.Text("Text2", &maroto.TextProp{ Family: maroto.Helvetica, Style: maroto.Italic, @@ -381,13 +381,13 @@ func TestPdfMaroto_Text(t *testing.T) { text.AssertCalled(t, "Add", "Text2", maroto.Courier, maroto.BoldItalic, 9.5, 40.0, maroto.Left, 0.0, 1.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text", nil) }) }) - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text2", &maroto.TextProp{ Family: maroto.Courier, Style: maroto.BoldItalic, @@ -409,8 +409,8 @@ func TestPdfMaroto_Text(t *testing.T) { text.AssertCalled(t, "Add", "Text", maroto.Arial, maroto.Normal, 10.0, 40.0, maroto.Left, 0.0, 1.0) }, func(m maroto.Maroto) { - m.Row("Row", 40, func() { - m.Col("Col", func() { + m.Row(40, func() { + m.Col(func() { m.Text("Text", &maroto.TextProp{ Top: 50, }) @@ -428,24 +428,426 @@ func TestPdfMaroto_Text(t *testing.T) { m := newMarotoTest(pdf, math, nil, text, nil, nil, nil) // Act - c.actSignature(m) + c.act(m) // Assert - c.assertSignature(t, text) + c.assert(t, text) + } +} + +func TestPdfMaroto_FileImage(t *testing.T) { + cases := []struct { + name string + image func() *mocks.Image + assert func(t *testing.T, image *mocks.Image) + act func(m maroto.Maroto) + }{ + { + "One code inside a col inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromFile", 1) + image.AssertCalled(t, "AddFromFile", "Image1", 0.0, 0.0, 1.0, 20.0, 100.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.FileImage("Image1", nil) + }) + }) + }, + }, + { + "Two images inside a col inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromFile", 2) + image.AssertCalled(t, "AddFromFile", "Image2", 4.0, 0.0, 1.0, 20.0, 40.0) + image.AssertCalled(t, "AddFromFile", "Image3", 0.0, 0.0, 1.0, 20.0, 40.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.FileImage("Image2", &maroto.RectProp{ + Left: 2.0, + Top: 4.0, + Percent: 40.0, + }) + m.FileImage("Image3", &maroto.RectProp{ + Percent: 40.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two images inside two cols inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromFile", 2) + image.AssertCalled(t, "AddFromFile", "Image4", 4.5, 0.0, 2.0, 20.0, 55.0) + image.AssertCalled(t, "AddFromFile", "Image5", 0.0, 1.0, 2.0, 20.0, 53.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.FileImage("Image4", &maroto.RectProp{ + Left: 4.0, + Top: 4.5, + Percent: 55.0, + }) + }) + m.Col(func() { + m.FileImage("Image5", &maroto.RectProp{ + Percent: 53.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two images inside one col inside two rows", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromFile", 2) + image.AssertCalled(t, "AddFromFile", "Image6", 8.5, 0.0, 1.0, 20.0, 66.0) + image.AssertCalled(t, "AddFromFile", "Image7", 20.0, 0.0, 1.0, 20.0, 98.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.FileImage("Image6", &maroto.RectProp{ + Left: 7.0, + Top: 8.5, + Percent: 66.0, + }) + }) + }) + m.Row(20, func() { + m.Col(func() { + m.FileImage("Image7", &maroto.RectProp{ + Percent: 98.0, + Center: true, + }) + }) + }) + }, + }, + } + + for _, c := range cases { + // Arrange + pdf := basePdfTest() + math := baseMathTest() + image := c.image() + + m := newMarotoTest(pdf, math, nil, nil, nil, image, nil) + + // Act + c.act(m) + + // Assert + c.assert(t, image) + } +} + +func TestPdfMaroto_Base64Image(t *testing.T) { + cases := []struct { + name string + image func() *mocks.Image + assert func(t *testing.T, image *mocks.Image) + act func(m maroto.Maroto) + }{ + { + "One code inside a col inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromBase64", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromBase64", 1) + image.AssertCalled(t, "AddFromBase64", "Image1", 0.0, 0.0, 1.0, 20.0, 100.0, maroto.Jpg) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.Base64Image("Image1", maroto.Jpg, nil) + }) + }) + }, + }, + { + "Two images inside a col inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromBase64", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromBase64", 2) + image.AssertCalled(t, "AddFromBase64", "Image2", 4.0, 0.0, 1.0, 20.0, 40.0, maroto.Png) + image.AssertCalled(t, "AddFromBase64", "Image3", 0.0, 0.0, 1.0, 20.0, 40.0, maroto.Jpg) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.Base64Image("Image2", maroto.Png, &maroto.RectProp{ + Left: 2.0, + Top: 4.0, + Percent: 40.0, + }) + m.Base64Image("Image3", maroto.Jpg, &maroto.RectProp{ + Percent: 40.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two images inside two cols inside a row", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromBase64", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromBase64", 2) + image.AssertCalled(t, "AddFromBase64", "Image4", 4.5, 0.0, 2.0, 20.0, 55.0, maroto.Png) + image.AssertCalled(t, "AddFromBase64", "Image5", 0.0, 1.0, 2.0, 20.0, 53.0, maroto.Jpg) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.Base64Image("Image4", maroto.Png, &maroto.RectProp{ + Left: 4.0, + Top: 4.5, + Percent: 55.0, + }) + }) + m.Col(func() { + m.Base64Image("Image5", maroto.Jpg, &maroto.RectProp{ + Percent: 53.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two images inside one col inside two rows", + func() *mocks.Image { + image := &mocks.Image{} + image.On("AddFromBase64", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return image + }, + func(t *testing.T, image *mocks.Image) { + image.AssertNumberOfCalls(t, "AddFromBase64", 2) + image.AssertCalled(t, "AddFromBase64", "Image6", 8.5, 0.0, 1.0, 20.0, 66.0, maroto.Png) + image.AssertCalled(t, "AddFromBase64", "Image7", 20.0, 0.0, 1.0, 20.0, 98.0, maroto.Jpg) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.Base64Image("Image6", maroto.Png, &maroto.RectProp{ + Left: 7.0, + Top: 8.5, + Percent: 66.0, + }) + }) + }) + m.Row(20, func() { + m.Col(func() { + m.Base64Image("Image7", maroto.Jpg, &maroto.RectProp{ + Percent: 98.0, + Center: true, + }) + }) + }) + }, + }, + } + + for _, c := range cases { + // Arrange + pdf := basePdfTest() + math := baseMathTest() + image := c.image() + + m := newMarotoTest(pdf, math, nil, nil, nil, image, nil) + + // Act + c.act(m) + + // Assert + c.assert(t, image) + } +} + +func TestPdfMaroto_QrCode(t *testing.T) { + cases := []struct { + name string + code func() *mocks.Code + assert func(t *testing.T, image *mocks.Code) + act func(m maroto.Maroto) + }{ + { + "One code inside a col inside a row", + func() *mocks.Code { + code := &mocks.Code{} + code.On("AddQr", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return code + }, + func(t *testing.T, code *mocks.Code) { + code.AssertNumberOfCalls(t, "AddQr", 1) + code.AssertCalled(t, "AddQr", "Code1", 0.0, 0.0, 1.0, 20.0, 100.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.QrCode("Code1", nil) + }) + }) + }, + }, + { + "Two codes inside a col inside a row", + func() *mocks.Code { + code := &mocks.Code{} + code.On("AddQr", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return code + }, + func(t *testing.T, code *mocks.Code) { + code.AssertNumberOfCalls(t, "AddQr", 2) + code.AssertCalled(t, "AddQr", "Code2", 4.0, 0.0, 1.0, 20.0, 40.0) + code.AssertCalled(t, "AddQr", "Code3", 0.0, 0.0, 1.0, 20.0, 40.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.QrCode("Code2", &maroto.RectProp{ + Left: 2.0, + Top: 4.0, + Percent: 40.0, + }) + m.QrCode("Code3", &maroto.RectProp{ + Percent: 40.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two codes inside two cols inside a row", + func() *mocks.Code { + code := &mocks.Code{} + code.On("AddQr", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return code + }, + func(t *testing.T, code *mocks.Code) { + code.AssertNumberOfCalls(t, "AddQr", 2) + code.AssertCalled(t, "AddQr", "Code4", 4.5, 0.0, 2.0, 20.0, 55.0) + code.AssertCalled(t, "AddQr", "Code5", 0.0, 1.0, 2.0, 20.0, 53.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.QrCode("Code4", &maroto.RectProp{ + Left: 4.0, + Top: 4.5, + Percent: 55.0, + }) + }) + m.Col(func() { + m.QrCode("Code5", &maroto.RectProp{ + Percent: 53.0, + Center: true, + }) + }) + }) + }, + }, + { + "Two codes inside one col inside two rows", + func() *mocks.Code { + code := &mocks.Code{} + code.On("AddQr", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + return code + }, + func(t *testing.T, code *mocks.Code) { + code.AssertNumberOfCalls(t, "AddQr", 2) + code.AssertCalled(t, "AddQr", "Code6", 8.5, 0.0, 1.0, 20.0, 66.0) + code.AssertCalled(t, "AddQr", "Code7", 20.0, 0.0, 1.0, 20.0, 98.0) + }, + func(m maroto.Maroto) { + m.Row(20, func() { + m.Col(func() { + m.QrCode("Code6", &maroto.RectProp{ + Left: 7.0, + Top: 8.5, + Percent: 66.0, + }) + }) + }) + m.Row(20, func() { + m.Col(func() { + m.QrCode("Code7", &maroto.RectProp{ + Percent: 98.0, + Center: true, + }) + }) + }) + }, + }, + } + + for _, c := range cases { + // Arrange + pdf := basePdfTest() + math := baseMathTest() + code := c.code() + + m := newMarotoTest(pdf, math, nil, nil, nil, nil, code) + + // Act + c.act(m) + + // Assert + c.assert(t, code) } } func TestPdfMaroto_Row(t *testing.T) { cases := []struct { name string - rowAct func(m maroto.Maroto, calledTimes *int) + act func(m maroto.Maroto, calledTimes *int) assertRowCalledTimes func(t *testing.T, calledTimes int) assertPdfCalls func(t *testing.T, pdf *mocks.Pdf) }{ { "One row", func(m maroto.Maroto, calledTimes *int) { - m.Row("AnyRow", 30, func() { + m.Row(30, func() { *calledTimes++ }) }, @@ -462,10 +864,10 @@ func TestPdfMaroto_Row(t *testing.T) { { "Two rows", func(m maroto.Maroto, calledTimes *int) { - m.Row("AnyRow", 30, func() { + m.Row(30, func() { *calledTimes++ }) - m.Row("AnyRow", 40, func() { + m.Row(40, func() { *calledTimes++ }) }, @@ -484,13 +886,13 @@ func TestPdfMaroto_Row(t *testing.T) { { "Three rows", func(m maroto.Maroto, calledTimes *int) { - m.Row("AnyRow", 30, func() { + m.Row(30, func() { *calledTimes++ }) - m.Row("AnyRow", 40, func() { + m.Row(40, func() { *calledTimes++ }) - m.Row("AnyRow", 10, func() { + m.Row(10, func() { *calledTimes++ }) }, @@ -510,13 +912,13 @@ func TestPdfMaroto_Row(t *testing.T) { { "Rows to add new page", func(m maroto.Maroto, calledTimes *int) { - m.Row("AnyRow", 50, func() { + m.Row(50, func() { *calledTimes++ }) - m.Row("AnyRow", 40, func() { + m.Row(40, func() { *calledTimes++ }) - m.Row("AnyRow", 45, func() { + m.Row(45, func() { *calledTimes++ }) }, @@ -544,7 +946,7 @@ func TestPdfMaroto_Row(t *testing.T) { calledTimes := 0 // Act - c.rowAct(m, &calledTimes) + c.act(m, &calledTimes) // Assert c.assertRowCalledTimes(t, calledTimes) @@ -554,10 +956,10 @@ func TestPdfMaroto_Row(t *testing.T) { func TestPdfMaroto_Line(t *testing.T) { cases := []struct { - name string - pdf func() *mocks.Pdf - assertPdfCalls func(t *testing.T, pdf *mocks.Pdf) - actLine func(m maroto.Maroto) + name string + pdf func() *mocks.Pdf + assert func(t *testing.T, pdf *mocks.Pdf) + act func(m maroto.Maroto) }{ { "One line", @@ -571,10 +973,10 @@ func TestPdfMaroto_Line(t *testing.T) { pdf.AssertNumberOfCalls(t, "GetPageSize", 2) pdf.AssertNumberOfCalls(t, "Line", 1) - pdf.AssertCalled(t, "Line", 10.0, 10.0, 90.0, 10.0) + pdf.AssertCalled(t, "Line", 10.0, 10.5, 90.0, 10.5) }, func(m maroto.Maroto) { - m.Line() + m.Line(1.0) }, }, { @@ -589,12 +991,12 @@ func TestPdfMaroto_Line(t *testing.T) { pdf.AssertNumberOfCalls(t, "GetPageSize", 4) pdf.AssertNumberOfCalls(t, "Line", 2) - pdf.AssertCalled(t, "Line", 10.0, 10.0, 90.0, 10.0) pdf.AssertCalled(t, "Line", 10.0, 11.0, 90.0, 11.0) + pdf.AssertCalled(t, "Line", 10.0, 14.0, 90.0, 14.0) }, func(m maroto.Maroto) { - m.Line() - m.Line() + m.Line(2.0) + m.Line(4.0) }, }, } @@ -607,23 +1009,23 @@ func TestPdfMaroto_Line(t *testing.T) { m := newMarotoTest(pdf, math, nil, nil, nil, nil, nil) // Act - c.actLine(m) + c.act(m) // Assert - c.assertPdfCalls(t, pdf) + c.assert(t, pdf) } } func TestPdfMaroto_ColSpace(t *testing.T) { cases := []struct { - name string - actColSpaces func(m maroto.Maroto) - assertPdfCalls func(t *testing.T, pdf *mocks.Pdf) + name string + act func(m maroto.Maroto) + assert func(t *testing.T, pdf *mocks.Pdf) }{ { "One ColSpace inside one Row", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpace() }) }, @@ -635,7 +1037,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) { { "Two ColSpace inside one Row", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpace() m.ColSpace() }) @@ -648,10 +1050,10 @@ func TestPdfMaroto_ColSpace(t *testing.T) { { "Two ColSpace inside two Rows", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpace() }) - m.Row("AnyRow", 35.0, func() { + m.Row(35.0, func() { m.ColSpace() }) }, @@ -665,7 +1067,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) { "ColSpace with Debug", func(m maroto.Maroto) { m.SetDebugMode(true) - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpace() }) }, @@ -684,23 +1086,23 @@ func TestPdfMaroto_ColSpace(t *testing.T) { m := newMarotoTest(pdf, math, nil, nil, nil, nil, nil) // Act - c.actColSpaces(m) + c.act(m) // Assert - c.assertPdfCalls(t, pdf) + c.assert(t, pdf) } } func TestPdfMaroto_ColSpaces(t *testing.T) { cases := []struct { - name string - actColSpaces func(m maroto.Maroto) - assertPdfCalls func(t *testing.T, pdf *mocks.Pdf) + name string + act func(m maroto.Maroto) + assert func(t *testing.T, pdf *mocks.Pdf) }{ { "One ColSpaces inside one Row", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpaces(2) }) }, @@ -712,7 +1114,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) { { "Two ColSpaces inside one Row", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpaces(2) m.ColSpaces(2) }) @@ -725,10 +1127,10 @@ func TestPdfMaroto_ColSpaces(t *testing.T) { { "Two ColSpaces inside two Rows", func(m maroto.Maroto) { - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpaces(2) }) - m.Row("AnyRow", 35.0, func() { + m.Row(35.0, func() { m.ColSpaces(2) }) }, @@ -742,7 +1144,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) { "ColSpaces with Debug", func(m maroto.Maroto) { m.SetDebugMode(true) - m.Row("AnyRow", 40.0, func() { + m.Row(40.0, func() { m.ColSpaces(2) }) }, @@ -761,10 +1163,10 @@ func TestPdfMaroto_ColSpaces(t *testing.T) { m := newMarotoTest(pdf, math, nil, nil, nil, nil, nil) // Act - c.actColSpaces(m) + c.act(m) // Assert - c.assertPdfCalls(t, pdf) + c.assert(t, pdf) } } diff --git a/math.go b/math.go index d32acca4..228551a5 100644 --- a/math.go +++ b/math.go @@ -22,16 +22,16 @@ func NewMath(pdf gofpdf.Pdf) Math { } } -func (m *math) GetWidthPerCol(qtdCols float64) float64 { - width, _ := m.pdf.GetPageSize() - left, _, right, _ := m.pdf.GetMargins() +func (self *math) GetWidthPerCol(qtdCols float64) float64 { + width, _ := self.pdf.GetPageSize() + left, _, right, _ := self.pdf.GetMargins() return (width - right - left) / qtdCols } -func (m *math) GetRectCenterColProperties(imageWidth float64, imageHeight float64, qtdCols float64, colHeight float64, indexCol float64, percent float64) (x float64, y float64, w float64, h float64) { +func (self *math) GetRectCenterColProperties(imageWidth float64, imageHeight float64, qtdCols float64, colHeight float64, indexCol float64, percent float64) (x float64, y float64, w float64, h float64) { percent = percent / 100.0 - width, _ := m.pdf.GetPageSize() - left, top, right, _ := m.pdf.GetMargins() + width, _ := self.pdf.GetPageSize() + left, top, right, _ := self.pdf.GetMargins() widthPerCol := ((width - right - left) / qtdCols) proportion := imageHeight / imageWidth @@ -43,16 +43,16 @@ func (m *math) GetRectCenterColProperties(imageWidth float64, imageHeight float6 newImageWidth := colHeight / proportion * percent newImageHeight := newImageWidth * proportion - widthCorrection := m.GetCenterCorrection(widthPerCol, newImageWidth) - heightCorrection := m.GetCenterCorrection(colHeight, newImageHeight) + widthCorrection := self.GetCenterCorrection(widthPerCol, newImageWidth) + heightCorrection := self.GetCenterCorrection(colHeight, newImageHeight) x = (widthPerCol * indexCol) + left + widthCorrection y = top + heightCorrection w = newImageWidth h = newImageHeight } else { - widthCorrection := m.GetCenterCorrection(widthPerCol, newImageWidth) - heightCorrection := m.GetCenterCorrection(colHeight, newImageHeight) + widthCorrection := self.GetCenterCorrection(widthPerCol, newImageWidth) + heightCorrection := self.GetCenterCorrection(colHeight, newImageHeight) x = (widthPerCol * indexCol) + left + widthCorrection y = top + heightCorrection @@ -63,6 +63,6 @@ func (m *math) GetRectCenterColProperties(imageWidth float64, imageHeight float6 return } -func (m *math) GetCenterCorrection(outerSize, innerSize float64) float64 { +func (self *math) GetCenterCorrection(outerSize, innerSize float64) float64 { return (outerSize - innerSize) / 2.0 } diff --git a/mocks/maroto.go b/mocks/maroto.go index feced258..5fd23262 100644 --- a/mocks/maroto.go +++ b/mocks/maroto.go @@ -95,7 +95,7 @@ func (_m *Maroto) Row(label string, height float64, closure func()) { _m.Called(label, height, closure) } -// RowTableList provides a mock function with given fields: label, header, contents +// TableList provides a mock function with given fields: label, header, contents func (_m *Maroto) RowTableList(label string, header []string, contents [][]string) { _m.Called(label, header, contents) } diff --git a/structs.go b/properties.go similarity index 100% rename from structs.go rename to properties.go diff --git a/structs_test.go b/properties_test.go similarity index 100% rename from structs_test.go rename to properties_test.go diff --git a/signature.go b/signature.go index c114458a..46949e63 100644 --- a/signature.go +++ b/signature.go @@ -22,11 +22,11 @@ func NewSignature(pdf gofpdf.Pdf, math Math, text Text) Signature { } } -func (s *signature) AddSpaceFor(label string, fontFamily Family, fontStyle Style, fontSize float64, qtdCols float64, marginTop float64, actualCol float64) { - widthPerCol := s.math.GetWidthPerCol(qtdCols) - left, _, right, _ := s.pdf.GetMargins() +func (self *signature) AddSpaceFor(label string, fontFamily Family, fontStyle Style, fontSize float64, qtdCols float64, marginTop float64, actualCol float64) { + widthPerCol := self.math.GetWidthPerCol(qtdCols) + left, _, right, _ := self.pdf.GetMargins() space := 4.0 - s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+right-space, marginTop+5.0) - s.text.Add(label, fontFamily, fontStyle, fontSize, marginTop, Center, actualCol, qtdCols) + self.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+right-space, marginTop+5.0) + self.text.Add(label, fontFamily, fontStyle, fontSize, marginTop, Center, actualCol, qtdCols) } diff --git a/text.go b/text.go index bb058e45..2540b56b 100644 --- a/text.go +++ b/text.go @@ -22,19 +22,19 @@ func NewText(pdf gofpdf.Pdf, math Math, font Font) Text { } } -func (m *text) Add(text string, fontFamily Family, fontStyle Style, fontSize float64, marginTop float64, align Align, actualCol float64, qtdCols float64) { - actualWidthPerCol := m.math.GetWidthPerCol(qtdCols) +func (self *text) Add(text string, fontFamily Family, fontStyle Style, fontSize float64, marginTop float64, align Align, actualCol float64, qtdCols float64) { + actualWidthPerCol := self.math.GetWidthPerCol(qtdCols) - m.font.SetFont(fontFamily, fontStyle, fontSize) + self.font.SetFont(fontFamily, fontStyle, fontSize) - left, top, _, _ := m.pdf.GetMargins() + left, top, _, _ := self.pdf.GetMargins() if align == Left { - m.pdf.Text(actualCol*actualWidthPerCol+left, marginTop+top, text) + self.pdf.Text(actualCol*actualWidthPerCol+left, marginTop+top, text) return } - stringWidth := m.pdf.GetStringWidth(text) + stringWidth := self.pdf.GetStringWidth(text) dx := (actualWidthPerCol - stringWidth) / 2 - m.pdf.Text(dx+actualCol*actualWidthPerCol+left, marginTop+top, text) + self.pdf.Text(dx+actualCol*actualWidthPerCol+left, marginTop+top, text) } From cb10ce89a3492bd5a08034608bb30f944dcddcb0 Mon Sep 17 00:00:00 2001 From: Johnathan Fercher Date: Sun, 16 Jun 2019 19:27:23 -0300 Subject: [PATCH 2/3] feature/remove-label-add-line-config Done * Remove label from Col, Row and TableList * Rename RowTableList to TableList * Add parameter on Line Why * Label did nothing, code comment will do the same * TableList is simpler to write * It's a good thing the line receive the size of space --- maroto_test.go | 160 +++++-------------------------------------------- 1 file changed, 15 insertions(+), 145 deletions(-) diff --git a/maroto_test.go b/maroto_test.go index 356af714..d0225ec4 100644 --- a/maroto_test.go +++ b/maroto_test.go @@ -183,7 +183,7 @@ func TestPdfMaroto_Signature(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Signature("Signature", nil) + m.Signature("Signature1", nil) }) }) }, @@ -203,8 +203,8 @@ func TestPdfMaroto_Signature(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Signature("Signature", nil) - m.Signature("Signature2", &maroto.SignatureProp{ + m.Signature("Signature2", nil) + m.Signature("Signature3", &maroto.SignatureProp{ Family: maroto.Courier, Style: maroto.BoldItalic, Size: 9.5, @@ -228,10 +228,10 @@ func TestPdfMaroto_Signature(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Signature("Signature", nil) + m.Signature("Signature4", nil) }) m.Col(func() { - m.Signature("Signature2", &maroto.SignatureProp{ + m.Signature("Signature5", &maroto.SignatureProp{ Family: maroto.Courier, Style: maroto.BoldItalic, Size: 9.5, @@ -255,12 +255,12 @@ func TestPdfMaroto_Signature(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Signature("Signature", nil) + m.Signature("Signature6", nil) }) }) m.Row(40, func() { m.Col(func() { - m.Signature("Signature2", &maroto.SignatureProp{ + m.Signature("Signature7", &maroto.SignatureProp{ Family: maroto.Courier, Style: maroto.BoldItalic, Size: 9.5, @@ -307,7 +307,7 @@ func TestPdfMaroto_Text(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Text("Text", nil) + m.Text("Text1", nil) }) }) }, @@ -327,8 +327,8 @@ func TestPdfMaroto_Text(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Text("Text", nil) - m.Text("Text2", &maroto.TextProp{ + m.Text("Text2", nil) + m.Text("Text3", &maroto.TextProp{ Family: maroto.Courier, Style: maroto.BoldItalic, Size: 9.5, @@ -354,10 +354,10 @@ func TestPdfMaroto_Text(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Text("Text", nil) + m.Text("Text4", nil) }) m.Col(func() { - m.Text("Text2", &maroto.TextProp{ + m.Text("Text5", &maroto.TextProp{ Family: maroto.Helvetica, Style: maroto.Italic, Size: 8.5, @@ -383,12 +383,12 @@ func TestPdfMaroto_Text(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Text("Text", nil) + m.Text("Text6", nil) }) }) m.Row(40, func() { m.Col(func() { - m.Text("Text2", &maroto.TextProp{ + m.Text("Text7", &maroto.TextProp{ Family: maroto.Courier, Style: maroto.BoldItalic, Size: 9.5, @@ -411,7 +411,7 @@ func TestPdfMaroto_Text(t *testing.T) { func(m maroto.Maroto) { m.Row(40, func() { m.Col(func() { - m.Text("Text", &maroto.TextProp{ + m.Text("Text8", &maroto.TextProp{ Top: 50, }) }) @@ -1293,136 +1293,6 @@ func TestPdfMaroto_OutputFileAndClose(t *testing.T) { } } -func TestPdfMaroto_FileImage(t *testing.T) { - cases := []struct { - name string - image func() *mocks.Image - act func(m maroto.Maroto) - assertImage func(t *testing.T, image *mocks.Image) - }{ - { - "One image in one col inside a row", - func() *mocks.Image { - image := &mocks.Image{} - image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) - return image - }, - func(m maroto.Maroto) { - m.Row("Row", 33, func() { - m.Col("Col", func() { - m.FileImage("image1.jpg", nil) - }) - }) - }, - func(t *testing.T, image *mocks.Image) { - image.AssertNumberOfCalls(t, "AddFromFile", 1) - image.AssertCalled(t, "AddFromFile", "image1.jpg", 0.0, 0.0, 1.0, 33.0, 100.0) - }, - }, - { - "Twos images in one col inside a row", - func() *mocks.Image { - image := &mocks.Image{} - image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) - return image - }, - func(m maroto.Maroto) { - m.Row("Row", 33, func() { - m.Col("Col", func() { - m.FileImage("image2.jpg", nil) - m.FileImage("image3.jpg", &maroto.RectProp{ - Percent: 50.0, - Center: true, - Top: 5.0, - }) - }) - }) - }, - func(t *testing.T, image *mocks.Image) { - image.AssertNumberOfCalls(t, "AddFromFile", 2) - image.AssertCalled(t, "AddFromFile", "image2.jpg", 0.0, 0.0, 1.0, 33.0, 100.0) - image.AssertCalled(t, "AddFromFile", "image3.jpg", 0.0, 0.0, 1.0, 33.0, 50.0) - }, - }, - { - "Twos images in two cols inside a row", - func() *mocks.Image { - image := &mocks.Image{} - image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) - return image - }, - func(m maroto.Maroto) { - m.Row("Row", 33, func() { - m.Col("Col", func() { - m.FileImage("image4.jpg", &maroto.RectProp{ - Center: false, - Left: 5.0, - }) - }) - m.Col("Col", func() { - m.FileImage("image5.jpg", &maroto.RectProp{ - Percent: 77.0, - Center: true, - Top: 6.0, - }) - }) - }) - }, - func(t *testing.T, image *mocks.Image) { - image.AssertNumberOfCalls(t, "AddFromFile", 2) - image.AssertCalled(t, "AddFromFile", "image4.jpg", 0.0, 0.0, 2.0, 33.0, 100.0) - image.AssertCalled(t, "AddFromFile", "image5.jpg", 0.0, 1.0, 2.0, 33.0, 77.0) - }, - }, - { - "Two images in two cols inside two rows", - func() *mocks.Image { - image := &mocks.Image{} - image.On("AddFromFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) - return image - }, - func(m maroto.Maroto) { - m.Row("Row", 33, func() { - m.Col("Col", func() { - m.FileImage("image6.jpg", &maroto.RectProp{ - Center: false, - Left: 5.0, - }) - }) - }) - m.Row("Row", 33, func() { - m.Col("Col", func() { - m.FileImage("image7.jpg", &maroto.RectProp{ - Center: false, - Left: 5.0, - }) - }) - }) - }, - func(t *testing.T, image *mocks.Image) { - image.AssertNumberOfCalls(t, "AddFromFile", 2) - image.AssertCalled(t, "AddFromFile", "image6.jpg", 0.0, 0.0, 1.0, 33.0, 100.0) - image.AssertCalled(t, "AddFromFile", "image7.jpg", 33.0, 0.0, 1.0, 33.0, 100.0) - }, - }, - } - - for _, c := range cases { - // Arrange - pdf := basePdfTest() - math := baseMathTest() - image := c.image() - - m := newMarotoTest(pdf, math, nil, nil, nil, image, nil) - - // Act - c.act(m) - - // Assert - c.assertImage(t, image) - } -} - func newMarotoTest(fpdf *mocks.Pdf, math *mocks.Math, font *mocks.Font, text *mocks.Text, signature *mocks.Signature, image *mocks.Image, code *mocks.Code) maroto.Maroto { m := &maroto.PdfMaroto{ Pdf: fpdf, From cf111f85c4ad93347f39e34c61b9b482f7dccb86 Mon Sep 17 00:00:00 2001 From: Johnathan Fercher Date: Sun, 16 Jun 2019 19:33:17 -0300 Subject: [PATCH 3/3] correction --- example_test.go | 4 ++-- maroto.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/example_test.go b/example_test.go index 641b3b2e..272262ac 100644 --- a/example_test.go +++ b/example_test.go @@ -147,9 +147,9 @@ func ExamplePdfMaroto_Signature() { // Do more things and save... } -// ExamplePdfMaroto_RowTableList demonstrates how to add a table +// ExamplePdfMaroto_TableList demonstrates how to add a table // with multiple rows and columns -func ExamplePdfMaroto_RowTableList() { +func ExamplePdfMaroto_TableList() { m := maroto.NewMaroto(maroto.Portrait, maroto.A4) headers := []string{"Header1", "Header2"} diff --git a/maroto.go b/maroto.go index c3848d36..af9054ef 100644 --- a/maroto.go +++ b/maroto.go @@ -5,8 +5,6 @@ import ( "github.com/jung-kurt/gofpdf" ) -const tableListTag = "header" - // Maroto is the principal abstraction to create a PDF document. type Maroto interface { // Grid System @@ -110,11 +108,11 @@ func (self *PdfMaroto) Signature(label string, prop *SignatureProp) { // Headers have bold style, and localized at the top of table. // Contents are array of arrays. Each array is one line. func (self *PdfMaroto) TableList(header []string, contents [][]string, prop *TableListProp) { - if header == nil || len(header) == 0 { + if len(header) == 0 { return } - if contents == nil || len(contents) == 0 { + if len(contents) == 0 { return }