From 89d9882c343b4359072521d7fe556139d903057d Mon Sep 17 00:00:00 2001 From: John Fercher Date: Mon, 11 Sep 2023 20:19:09 -0300 Subject: [PATCH] keep --- README.md | 2 +- cmd/v22/main.go | 35 ++++++++++++++++++++++ docs/doc.go | 49 +++++++++++++------------------ go.mod | 18 ++++++++++-- go.sum | 10 +++++++ internal/signature_test.go | 12 ++++---- internal/tablelist.go | 4 +-- internal/tablelist_test.go | 32 ++++++++++---------- pkg/pdf/example_test.go | 60 +++++++++++++++++++------------------- pkg/pdf/pdf.go | 8 ++--- pkg/pdf/pdf_test.go | 60 +++++++++++++++++++------------------- pkg/phil/compliance.go | 36 +++++++++++++++++++++++ pkg/phil/consumers.go | 53 +++++++++++++++++++++++++++++++++ pkg/phil/consumers_test.go | 19 ++++++++++++ pkg/v22/col/col.go | 43 +++++++++++++++++++++++++++ pkg/v22/component.go | 7 +++++ pkg/v22/document.go | 40 +++++++++++++++++++++++++ pkg/v22/image/image.go | 34 +++++++++++++++++++++ pkg/v22/row/row.go | 43 +++++++++++++++++++++++++++ pkg/v22/types.go | 8 +++++ 20 files changed, 452 insertions(+), 121 deletions(-) create mode 100644 cmd/v22/main.go create mode 100644 pkg/phil/compliance.go create mode 100644 pkg/phil/consumers.go create mode 100644 pkg/phil/consumers_test.go create mode 100644 pkg/v22/col/col.go create mode 100644 pkg/v22/component.go create mode 100644 pkg/v22/document.go create mode 100644 pkg/v22/image/image.go create mode 100644 pkg/v22/row/row.go create mode 100644 pkg/v22/types.go diff --git a/README.md b/README.md index 2a336f3e..72759a5f 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ always when a new page appear, in this case, a header may have many rows, lines * With `go get`: ```bash -go get -u github.com/johnfercher/maroto/internal +go get -u github.com/johnfercher/pdf/internal ``` ## Contributing diff --git a/cmd/v22/main.go b/cmd/v22/main.go new file mode 100644 index 00000000..84d07c45 --- /dev/null +++ b/cmd/v22/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/johnfercher/maroto/pkg/v22" + "github.com/johnfercher/maroto/pkg/v22/col" + "github.com/johnfercher/maroto/pkg/v22/image" + "github.com/johnfercher/maroto/pkg/v22/row" +) + +func main() { + pdf := v22.NewDocument("pdfzin") + + header := buildRow() + content := buildRow() + footer := buildRow() + + pdf.Add(header, content, footer) + + pdf.Render() +} + +func buildRow() v22.Component { + row := row.New(20) + + image := image.New("image1") + + col1 := col.New(4) + col1.Add(image) + + col2 := col.New(4) + col3 := col.New(4) + + row.Add(col1, col2, col3) + return row +} diff --git a/docs/doc.go b/docs/doc.go index ef237661..e0a459e5 100644 --- a/docs/doc.go +++ b/docs/doc.go @@ -1,10 +1,9 @@ /* -Package docs is the documentation of maroto. Maroto is a package which +Package docs is the documentation of pdf. Maroto is a package which provide a simple way to generate PDF documents. The project is inspired in Bootstrap and uses gofpdf. Simple and Fast - -Features and Components +# Features and Components - Grid system with rows and columns @@ -24,43 +23,39 @@ Features and Components Maroto has only gofpdf dependency. All tests pass on Linux and Mac. - -Installation +# Installation To install the package on your system, run - go get github.com/johnfercher/maroto + go get github.com/johnfercher/pdf Later, to receive updates, run - go get -u -v github.com/johnfercher/maroto/... - + go get -u -v github.com/johnfercher/pdf/... -Quick Start +# Quick Start The following Go Code generates a simple PDF file. + m := pdf.NewMaroto(consts.Portrait, consts.Letter) - m := pdf.NewMaroto(consts.Portrait, consts.Letter) - - m.Row(10, func() { - m.Col(12, func() { - m.Text(props.Text{ - Size: 18, - Style: consts.Bold, - Align: consts.Center, - Top: 9, + m.AddRow(10, func() { + m.AddCol(12, func() { + m.Text(props.Text{ + Size: 18, + Style: consts.Bold, + Align: consts.Center, + Top: 9, + }) }) }) - }) - m.OutputFileAndClose("maroto.pdf") + m.OutputFileAndClose("pdf.pdf") See the functions in the maroto_test.go file (shown as examples in this documentation) for more advanced PDF examples. - -Conversion Notes +# Conversion Notes This package is an high level API from gofpdf. The original API names have been slightly adapted. And the package search to be @@ -69,19 +64,15 @@ simpler to use. The main contribution upside gofpdf is the grid system with high level components. - -License +# License Maroto is released under the MIT License. - -Acknowledgments +# Acknowledgments This package’s Code and documentation are based on gofpdf. - -Roadmap - +# Roadmap - Improve test coverage as reported by the coverage tool. */ diff --git a/go.mod b/go.mod index cd133d47..9de26c31 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,25 @@ module github.com/johnfercher/maroto -go 1.13 +go 1.21.1 require ( github.com/boombuler/barcode v1.0.1 github.com/google/uuid v1.3.0 github.com/jung-kurt/gofpdf v1.16.2 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.8.4 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/johnfercher/go-tree v1.0.2 // indirect + github.com/phpdave11/gofpdf v1.4.2 // indirect + github.com/phpdave11/gofpdi v1.0.12 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245 // indirect - github.com/stretchr/objx v0.2.0 // indirect - github.com/stretchr/testify v1.3.0 + github.com/stretchr/objx v0.5.0 // indirect + golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a // indirect + golang.org/x/text v0.3.0 // indirect + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0c170f49..e14c1c21 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/johnfercher/go-tree v1.0.2 h1:9dZVcUjTAOpjC1Qemuxtl/xJRJjRpc3GOLOc/BOTyo0= +github.com/johnfercher/go-tree v1.0.2/go.mod h1:DUO6QkXIFh1K7jeGBIkLCZaeUgnkdQAsB64FDSoHswg= github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc= github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0= @@ -23,8 +25,16 @@ github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/signature_test.go b/internal/signature_test.go index 163ec096..d4d04b17 100644 --- a/internal/signature_test.go +++ b/internal/signature_test.go @@ -30,7 +30,7 @@ func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) { math.On("GetWidthPerCol", mock.Anything).Return(50.0) text := &mocks.Text{} - text.On("Add", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + text.On("Rows", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) signature := internal.NewSignature(pdf, math, text) @@ -40,8 +40,8 @@ func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) { // Assert pdf.AssertNumberOfCalls(t, "Line", 1) pdf.AssertCalled(t, "Line", 19.0, 15.0, 13.0, 15.0) - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "label", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "label", internal.Cell{ 5.0, 7.0, 2.0, @@ -67,7 +67,7 @@ func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) { math.On("GetWidthPerCol", mock.Anything).Return(50.0) text := &mocks.Text{} - text.On("Add", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + text.On("Rows", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) signature := internal.NewSignature(pdf, math, text) @@ -77,8 +77,8 @@ func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) { // Assert pdf.AssertNumberOfCalls(t, "Line", 1) pdf.AssertCalled(t, "Line", 26.0, 15.0, 23.0, 15.0) - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "label", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "label", internal.Cell{ 2.0, 7.0, 5.0, diff --git a/internal/tablelist.go b/internal/tablelist.go index fc7a9d42..355da043 100644 --- a/internal/tablelist.go +++ b/internal/tablelist.go @@ -23,10 +23,10 @@ type MarotoGridPart interface { GetPageSize() (width float64, height float64) GetPageMargins() (left float64, top float64, right float64, bottom float64) - // Outside Col/Row Components. + // Outside Col/AddRow Components. Line(spaceHeight float64, line ...props.Line) - // Inside Col/Row Components. + // Inside Col/AddRow Components. Text(text string, prop ...props.Text) } diff --git a/internal/tablelist_test.go b/internal/tablelist_test.go index 29e67060..b7873064 100644 --- a/internal/tablelist_test.go +++ b/internal/tablelist_test.go @@ -93,7 +93,7 @@ func TestTableList_Create_Happy(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) @@ -120,9 +120,9 @@ func TestTableList_Create_Happy(t *testing.T) { font.AssertCalled(t, "GetFont") font.AssertNumberOfCalls(t, "GetFont", 21) - marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) + marotoGrid.AssertCalled(t, "AddRow", mock.Anything, mock.Anything) marotoGrid.AssertCalled(t, "Line", mock.Anything) - marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertNumberOfCalls(t, "AddRow", 22) marotoGrid.AssertNumberOfCalls(t, "Line", 20) marotoGrid.AssertNotCalled(t, "SetBackgroundColor") } @@ -137,7 +137,7 @@ func TestTableList_Create_HappyWithBackgroundColor(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) @@ -165,8 +165,8 @@ func TestTableList_Create_HappyWithBackgroundColor(t *testing.T) { font.AssertCalled(t, "GetFont") font.AssertNumberOfCalls(t, "GetFont", 21) - marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) - marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertCalled(t, "AddRow", mock.Anything, mock.Anything) + marotoGrid.AssertNumberOfCalls(t, "AddRow", 22) marotoGrid.AssertNotCalled(t, "Line") @@ -184,7 +184,7 @@ func TestTableList_Create_Happy_Without_Line(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) marotoGrid.On("GetPageSize").Return(200.0, 600.0) @@ -204,8 +204,8 @@ func TestTableList_Create_Happy_Without_Line(t *testing.T) { font.AssertCalled(t, "GetFont") font.AssertNumberOfCalls(t, "GetFont", 21) - marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) - marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertCalled(t, "AddRow", mock.Anything, mock.Anything) + marotoGrid.AssertNumberOfCalls(t, "AddRow", 22) marotoGrid.AssertNumberOfCalls(t, "Line", 0) } @@ -219,7 +219,7 @@ func TestTableList_Create_HappyWithVerticalContentPadding(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) marotoGrid.On("GetPageSize").Return(200.0, 600.0) @@ -241,8 +241,8 @@ func TestTableList_Create_HappyWithVerticalContentPadding(t *testing.T) { font.AssertCalled(t, "GetFont") font.AssertNumberOfCalls(t, "GetFont", 21) - marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) - marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertCalled(t, "AddRow", mock.Anything, mock.Anything) + marotoGrid.AssertNumberOfCalls(t, "AddRow", 22) marotoGrid.AssertNumberOfCalls(t, "Line", 0) } @@ -256,7 +256,7 @@ func TestTableList_Create_HappyWithCustomCellColor(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) @@ -298,9 +298,9 @@ func TestTableList_Create_HappyWithCustomCellColor(t *testing.T) { font.AssertCalled(t, "GetFont") font.AssertNumberOfCalls(t, "GetFont", 21) - marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) + marotoGrid.AssertCalled(t, "AddRow", mock.Anything, mock.Anything) marotoGrid.AssertCalled(t, "Line", mock.Anything) - marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertNumberOfCalls(t, "AddRow", 22) marotoGrid.AssertNumberOfCalls(t, "Line", 20) marotoGrid.AssertNotCalled(t, "SetBackgroundColor") } @@ -336,7 +336,7 @@ func TestTableList_Create_WithLineProp(t *testing.T) { font.On("GetScaleFactor").Return(1.5) marotoGrid := &mocks.Maroto{} - marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("AddRow", mock.Anything, mock.Anything).Return(nil) marotoGrid.On("Line", mock.Anything).Return(nil) marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil) marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0) diff --git a/pkg/pdf/example_test.go b/pkg/pdf/example_test.go index 6f51d0f3..9b36c114 100644 --- a/pkg/pdf/example_test.go +++ b/pkg/pdf/example_test.go @@ -12,7 +12,7 @@ import ( "github.com/johnfercher/maroto/pkg/props" ) -// ExampleNewMaroto demonstrates how to create maroto. +// ExampleNewMaroto demonstrates how to create pdf. func ExampleNewMaroto() { m := pdf.NewMaroto(consts.Portrait, consts.A4) @@ -22,7 +22,7 @@ func ExampleNewMaroto() { // Do more things and save... } -// ExampleNewMaroto demonstrates how to create maroto with custom page size. +// ExampleNewMaroto demonstrates how to create pdf with custom page size. func ExampleNewMarotoCustomSize() { m := pdf.NewMarotoCustomSize(consts.Landscape, "C6", "mm", 114.0, 162.0) @@ -36,10 +36,10 @@ func ExampleNewMarotoCustomSize() { func ExamplePdfMaroto_AddPage() { m := pdf.NewMaroto(consts.Portrait, consts.A4) - // Add rows, cols and components + // Rows rows, cols and components m.AddPage() - // Add rows, col and components in a new page + // Rows rows, col and components in a new page // Do more things and save... } @@ -51,14 +51,14 @@ func ExamplePdfMaroto_Row() { // Warning: There is no way to use a row inside a row or a row inside a col. m.Row(rowHeight, func() { m.Col(12, func() { - // Add a component + // Rows a component }) }) // Warning: There is no way to use a row inside a row or a row inside a col. m.Row(rowHeight, func() { m.Col(12, func() { - // Add another component + // Rows another component }) }) @@ -75,7 +75,7 @@ func ExamplePdfMaroto_Col() { // Warning: There is no way to use a row inside a row or a row inside a col. m.Row(rowHeight, func() { m.Col(12, func() { - // Add Image, Text, Signature, QrCode or Barcode... + // Rows Image, Text, Signature, QrCode or Barcode... }) }) @@ -83,10 +83,10 @@ func ExamplePdfMaroto_Col() { // Warning: There is no way to use a row inside a row or a row inside a col. m.Row(rowHeight, func() { m.Col(6, func() { - // Add Image, Text, Signature, QrCode or Barcode... + // Rows Image, Text, Signature, QrCode or Barcode... }) m.Col(6, func() { - // Add Image, Text, Signature, QrCode or Barcode... + // Rows Image, Text, Signature, QrCode or Barcode... }) }) @@ -120,8 +120,8 @@ func ExamplePdfMaroto_RegisterHeader() { // In this closure you are free to set any components you want to compose // your header. // In this example there is a two texts with different props and one image. - // It is important to remember that it is recommended to create Row's and - // Col's if necessary. + // It is important to remember that it is recommended to create AddRow's and + // AddCol's if necessary. // You have to register the header immediately after the Maroto m := pdf.NewMaroto(consts.Portrait, consts.A4) @@ -151,8 +151,8 @@ func ExamplePdfMaroto_RegisterFooter() { // In this closure you are free to set any components you want to compose // your footer. // In this example there is a signature and a text with right align. - // It is important to remember that it is recommended to create Row's and - // Col's if necessary. + // It is important to remember that it is recommended to create AddRow's and + // AddCol's if necessary. // You have to register the footer immediately after the Maroto // All footers will be rendered at the bottom of all pages @@ -240,7 +240,7 @@ func ExamplePdfMaroto_TableList() { // TableList have to be used at same level as row m.Row(10, func() { m.Col(12, func() { - // Add a component + // Rows a component }) }) @@ -366,7 +366,7 @@ func ExamplePdfMaroto_Base64Image() { } // ExamplePdfMaroto_Barcode demonstrates how to place a barcode inside -// a Col. +// a AddCol. func ExamplePdfMaroto_Barcode() { // Passing nil on barcode props parameter implies the Barcode fills it's // context cell depending on it's size. @@ -374,7 +374,7 @@ func ExamplePdfMaroto_Barcode() { // the top and left parameters unless center parameter is true. // In brief, when center parameter equals true, left and top parameters has no effect. // Percent parameter represents the Barcode's width/height inside the cell. - // i.e. Percent: 75 means that the Barcode will take up 75% of Col's width + // i.e. Percent: 75 means that the Barcode will take up 75% of AddCol's width // There is a constraint in the proportion defined, height cannot be greater than 20% of // the width, and height cannot be smaller than 10% of the width. @@ -394,13 +394,13 @@ func ExamplePdfMaroto_Barcode() { } // ExamplePdfMaroto_QrCode demonstrates how to add -// a QR Code inside a Col. +// a QR Code inside a AddCol. func ExamplePdfMaroto_QrCode() { // Passing nil on rectProps makes // the QR Code fills the context cell. // When center is true, left and top has no effect. // Percent represents the width/height of the QR Code inside the cell. - // i.e. 80 means that the QR Code will take up 80% of Col's width + // i.e. 80 means that the QR Code will take up 80% of AddCol's width // When center is false, positioning of the QR Code can be done through // left and top. @@ -420,13 +420,13 @@ func ExamplePdfMaroto_QrCode() { } // ExamplePdfMaroto_DataMatrixCode demonstrates how to add -// a DataMatrixCode inside a Col. +// a DataMatrixCode inside a AddCol. func ExamplePdfMaroto_DataMatrixCode() { // Passing nil on rectProps makes // the DataMatrixCode fill the context cell. // When center is true, left and top has no effect. // Percent represents the width/height of the DataMatrixCode inside the cell. - // i.e. 80 means that the DataMatrixCode will take up 80% of Col's width + // i.e. 80 means that the DataMatrixCode will take up 80% of AddCol's width // When center is false, positioning of the DataMatrixCode can be done through // left and top. @@ -507,12 +507,12 @@ func ExamplePdfMaroto_SetBorder() { m := pdf.NewMaroto(consts.Portrait, consts.A4) m.SetBorder(true) - // Add some Rows, Cols, Lines and etc... + // Rows some Rows, Cols, Lines and etc... // Here will be drawn borders in every cell. m.SetBorder(false) - // Add some Rows, Cols, Lines and etc... + // Rows some Rows, Cols, Lines and etc... // Here will not be drawn borders. // Do more things and save... @@ -554,20 +554,20 @@ func ExamplePdfMaroto_SetBackgroundColor() { Blue: 30, }) - // This Row will be filled with the color. + // This AddRow will be filled with the color. m.Row(20, func() { m.Col(12, func() { - // Add components. + // Rows components. }) }) m.SetBackgroundColor(color.NewWhite()) - // Note: The default value is White (255, 255, 255), if maroto see this value it will ignore not will the cell with any color. + // Note: The default value is White (255, 255, 255), if pdf see this value it will ignore not will the cell with any color. - // This Row will not be filled with the color. + // This AddRow will not be filled with the color. m.Row(20, func() { m.Col(12, func() { - // Add components. + // Rows components. }) }) @@ -609,7 +609,7 @@ func ExamplePdfMaroto_GetCurrentPage() { // Index here will be 0. _ = m.GetCurrentPage() - // Add Rows, Cols and Components. + // Rows Rows, Cols and Components. // Index here will not be 0. _ = m.GetCurrentPage() @@ -625,12 +625,12 @@ func ExamplePdfMaroto_GetCurrentOffset() { // Offset here will be 0. _ = m.GetCurrentOffset() - // Add Rows, Cols and Components until maroto add a new page. + // Rows Rows, Cols and Components until pdf add a new page. // Offset here will not be 0. _ = m.GetCurrentOffset() - // Add Rows, Cols and Components to maroto add a new page. + // Rows Rows, Cols and Components to pdf add a new page. // Offset here will be 0. _ = m.GetCurrentOffset() diff --git a/pkg/pdf/pdf.go b/pkg/pdf/pdf.go index c0ce6d08..1a26d5ee 100644 --- a/pkg/pdf/pdf.go +++ b/pkg/pdf/pdf.go @@ -31,11 +31,11 @@ type Maroto interface { RegisterHeader(closure func()) RegisterFooter(closure func()) - // Outside Col/Row Components + // Outside Col/AddRow Components TableList(header []string, contents [][]string, prop ...props.TableList) Line(spaceHeight float64, prop ...props.Line) - // Inside Col/Row Components + // Inside Col/AddRow Components Text(text string, prop ...props.Text) FileImage(filePathName string, prop ...props.Rect) (err error) Base64Image(base64 string, extension consts.Extension, prop ...props.Rect) (err error) @@ -371,7 +371,7 @@ func (s *PdfMaroto) Row(height float64, closure func()) { // Note: The headerFooterContextActive is needed to avoid recursive // calls without end, because footerClosure and headerClosure actually - // have Row calls too. + // have AddRow calls too. // If the new cell to be added pass the useful space counting the // height of the footer, add the footer. @@ -589,7 +589,7 @@ func (s *PdfMaroto) Output() (bytes.Buffer, error) { return buffer, err } -// AddUTF8Font add a custom utf8 font. familyStr is the name of the custom font registered in maroto. +// AddUTF8Font add a custom utf8 font. familyStr is the name of the custom font registered in pdf. // styleStr is the style of the font and fileStr is the path to the .ttf file. func (s *PdfMaroto) AddUTF8Font(familyStr string, styleStr consts.Style, fileStr string) { s.Pdf.AddUTF8Font(familyStr, string(styleStr), fileStr) diff --git a/pkg/pdf/pdf_test.go b/pkg/pdf/pdf_test.go index 42b08d64..60cff49c 100644 --- a/pkg/pdf/pdf_test.go +++ b/pkg/pdf/pdf_test.go @@ -533,8 +533,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "One text inside one column, inside a row, without props", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text1", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text1", internal.Cell{ X: 0.0, Y: 0.0, Width: 80.0, @@ -559,8 +559,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Two different text inside one colum, inside one row", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 2) - text.AssertCalled(t, "Add", "Text2", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 2) + text.AssertCalled(t, "Rows", "Text2", internal.Cell{ X: 0.0, Y: 0.0, Width: 80.0, @@ -573,7 +573,7 @@ func TestFpdfMaroto_Text(t *testing.T) { Extrapolate: false, Size: 10.0, }) - text.AssertCalled(t, "Add", "Text3", internal.Cell{ + text.AssertCalled(t, "Rows", "Text3", internal.Cell{ X: 0.0, Y: 5.0, Width: 80.0, @@ -605,8 +605,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Two different text with different columns, inside one row", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 2) - text.AssertCalled(t, "Add", "Text4", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 2) + text.AssertCalled(t, "Rows", "Text4", internal.Cell{ X: 0.0, Y: 0.0, Width: 80.0, @@ -619,7 +619,7 @@ func TestFpdfMaroto_Text(t *testing.T) { Extrapolate: false, Size: 10.0, }) - text.AssertCalled(t, "Add", "Text5", internal.Cell{ + text.AssertCalled(t, "Rows", "Text5", internal.Cell{ X: 80.0, Y: 4.4, Width: 80.0, @@ -653,8 +653,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Two different text with different columns, inside one row", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 2) - text.AssertCalled(t, "Add", "Text6", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 2) + text.AssertCalled(t, "Rows", "Text6", internal.Cell{ X: 0.0, Y: 0.0, Width: 80.0, @@ -667,7 +667,7 @@ func TestFpdfMaroto_Text(t *testing.T) { Extrapolate: false, Size: 10.0, }) - text.AssertCalled(t, "Add", "Text7", internal.Cell{ + text.AssertCalled(t, "Rows", "Text7", internal.Cell{ X: 0.0, Y: 40.0, Width: 80.0, @@ -701,8 +701,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Left-indented text", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 10.0, Y: 0.0, Width: 70.0, @@ -728,8 +728,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Right-indented text", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 0.0, Y: 0.0, Width: 70.0, @@ -755,8 +755,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Left- and right-indented text", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 10.0, Y: 0.0, Width: 60.0, @@ -784,8 +784,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Left-indent larger than col width", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 80.0, Y: 0.0, Width: 0.0, @@ -811,8 +811,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Right-indent larger than col width", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 0.0, Y: 0.0, Width: 0.0, @@ -838,8 +838,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "Indents larger than col width", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text", internal.Cell{ X: 40.0, Y: 0.0, Width: 0.0, @@ -867,8 +867,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "When top is greater than row height", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text8", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text8", internal.Cell{ X: 0.0, Y: 40.0, Width: 80.0, @@ -894,8 +894,8 @@ func TestFpdfMaroto_Text(t *testing.T) { { "custom color", func(t *testing.T, text *mocks.Text) { - text.AssertNumberOfCalls(t, "Add", 1) - text.AssertCalled(t, "Add", "Text1", internal.Cell{ + text.AssertNumberOfCalls(t, "Rows", 1) + text.AssertCalled(t, "Rows", "Text1", internal.Cell{ X: 0.0, Y: 0.0, Width: 80.0, @@ -2017,7 +2017,7 @@ func TestFpdfMaroto_ColSpace(t *testing.T) { assert func(t *testing.T, Fpdf *mocks.Fpdf) }{ { - "One ColSpace inside one Row", + "One ColSpace inside one AddRow", func(m pdf.Maroto) { m.Row(40.0, func() { m.ColSpace(12) @@ -2029,7 +2029,7 @@ func TestFpdfMaroto_ColSpace(t *testing.T) { }, }, { - "Two ColSpace inside one Row", + "Two ColSpace inside one AddRow", func(m pdf.Maroto) { m.Row(40.0, func() { m.ColSpace(5) @@ -2307,7 +2307,7 @@ func baseMathTest() *mocks.Math { func baseTextTest() *mocks.Text { text := &mocks.Text{} - text.On("Add", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + text.On("Rows", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) text.On("GetLinesQuantity", mock.Anything, mock.Anything, mock.Anything).Return(1) return text } diff --git a/pkg/phil/compliance.go b/pkg/phil/compliance.go new file mode 100644 index 00000000..f854e946 --- /dev/null +++ b/pkg/phil/compliance.go @@ -0,0 +1,36 @@ +package phil + +type ComplianceRequestedEvent struct { + RemitoID string +} + +type complianceRequestedObserver struct { + subject Subject + readCh chan interface{} +} + +func NewComplianceRequestedObserver(subject Subject) Observer[ComplianceRequestedEvent] { + c := &complianceRequestedObserver{ + subject: subject, + readCh: make(chan interface{}), + } + + RegisterObserver(c.subject, c.readCh, c.Notify) + + return c + +} + +func (c complianceRequestedObserver) Close() { + close(c.readCh) +} + +func (c complianceRequestedObserver) Notify(event ComplianceRequestedEvent) { + println("event:", event.RemitoID) +} + +func DoService(subject Subject) { + subject.Notify(ComplianceRequestedEvent{ + RemitoID: "123", + }) +} diff --git a/pkg/phil/consumers.go b/pkg/phil/consumers.go new file mode 100644 index 00000000..c035c8b0 --- /dev/null +++ b/pkg/phil/consumers.go @@ -0,0 +1,53 @@ +package phil + +type Observer[T any] interface { + Notify(event T) + Close() +} + +type Subject interface { + Notify(interface{}) + register(chan<- interface{}) + deregister(chan<- interface{}) +} + +type subject struct { + observers []chan<- interface{} +} + +func (s *subject) register(observer chan<- interface{}) { + s.observers = append(s.observers, observer) +} + +func (s *subject) deregister(observer chan<- interface{}) { + for i, o := range s.observers { + if o == observer { + s.observers = append(s.observers[:i], s.observers[i+1:]...) + return + } + } +} + +func (s *subject) Notify(event interface{}) { + for _, o := range s.observers { + o <- event + } +} + +func RegisterObserver[T any](subject Subject, channel chan interface{}, fnNotify func(T)) { + subject.register(channel) + go func() { + for { + read, more := <-channel + if more { + if event, ok := read.(T); ok { + fnNotify(event) + } + } else { + subject.deregister(channel) + println("deregistered observer") + return + } + } + }() +} diff --git a/pkg/phil/consumers_test.go b/pkg/phil/consumers_test.go new file mode 100644 index 00000000..a98fd9a5 --- /dev/null +++ b/pkg/phil/consumers_test.go @@ -0,0 +1,19 @@ +package phil + +import ( + "testing" + "time" +) + +func TestObserver(t *testing.T) { + t.Run("Test Observer", func(t *testing.T) { + s := &subject{} + + observer := NewComplianceRequestedObserver(s) + defer observer.Close() + + DoService(s) + + time.Sleep(1 * time.Second) + }) +} diff --git a/pkg/v22/col/col.go b/pkg/v22/col/col.go new file mode 100644 index 00000000..934ffe90 --- /dev/null +++ b/pkg/v22/col/col.go @@ -0,0 +1,43 @@ +package col + +import ( + "fmt" + "github.com/johnfercher/maroto/pkg/v22" +) + +type col struct { + size int + _type string + accept map[string]bool + components []v22.Component +} + +func New(size int) *col { + accept := make(map[string]bool) + accept[v22.Image] = true + + return &col{ + _type: v22.Col, + accept: accept, + size: size, + } +} + +func (d *col) Render() { + fmt.Println(d.size) + for _, component := range d.components { + component.Render() + } +} + +func (d *col) GetType() string { + return d._type +} + +func (d *col) Add(components ...v22.Component) { + for _, component := range components { + if _, ok := d.accept[component.GetType()]; ok { + d.components = append(d.components, component) + } + } +} diff --git a/pkg/v22/component.go b/pkg/v22/component.go new file mode 100644 index 00000000..824f51cc --- /dev/null +++ b/pkg/v22/component.go @@ -0,0 +1,7 @@ +package v22 + +type Component interface { + Render() + GetType() string + Add(component ...Component) +} diff --git a/pkg/v22/document.go b/pkg/v22/document.go new file mode 100644 index 00000000..40fd4f4c --- /dev/null +++ b/pkg/v22/document.go @@ -0,0 +1,40 @@ +package v22 + +import "fmt" + +type document struct { + value string + _type string + accept map[string]bool + components []Component +} + +func NewDocument(value string) *document { + accept := make(map[string]bool) + accept[Row] = true + + return &document{ + _type: Document, + accept: accept, + value: value, + } +} + +func (d *document) Render() { + fmt.Println(d.value) + for _, component := range d.components { + component.Render() + } +} + +func (d *document) GetType() string { + return d._type +} + +func (d *document) Add(components ...Component) { + for _, component := range components { + if _, ok := d.accept[component.GetType()]; ok { + d.components = append(d.components, component) + } + } +} diff --git a/pkg/v22/image/image.go b/pkg/v22/image/image.go new file mode 100644 index 00000000..99f4b63f --- /dev/null +++ b/pkg/v22/image/image.go @@ -0,0 +1,34 @@ +package image + +import ( + "fmt" + "github.com/johnfercher/maroto/pkg/v22" +) + +type image struct { + path string + _type string + components []v22.Component +} + +func New(path string) *image { + return &image{ + _type: v22.Image, + path: path, + } +} + +func (d *image) Render() { + fmt.Println(d.path) + for _, component := range d.components { + component.Render() + } +} + +func (d *image) GetType() string { + return d._type +} + +func (d *image) Add(_ ...v22.Component) { + return +} diff --git a/pkg/v22/row/row.go b/pkg/v22/row/row.go new file mode 100644 index 00000000..dd2f3120 --- /dev/null +++ b/pkg/v22/row/row.go @@ -0,0 +1,43 @@ +package row + +import ( + "fmt" + "github.com/johnfercher/maroto/pkg/v22" +) + +type row struct { + height int + _type string + accept map[string]bool + components []v22.Component +} + +func New(height int) *row { + accept := make(map[string]bool) + accept[v22.Col] = true + + return &row{ + _type: v22.Row, + accept: accept, + height: height, + } +} + +func (d *row) Render() { + fmt.Println(d.height) + for _, component := range d.components { + component.Render() + } +} + +func (d *row) GetType() string { + return d._type +} + +func (d *row) Add(components ...v22.Component) { + for _, component := range components { + if _, ok := d.accept[component.GetType()]; ok { + d.components = append(d.components, component) + } + } +} diff --git a/pkg/v22/types.go b/pkg/v22/types.go new file mode 100644 index 00000000..5e5855e9 --- /dev/null +++ b/pkg/v22/types.go @@ -0,0 +1,8 @@ +package v22 + +const ( + Document = "document" + Row = "row" + Col = "col" + Image = "image" +)