diff --git a/internal/examples/pdfs/sample1.pdf b/internal/examples/pdfs/sample1.pdf index 36d8989b..8a2a309e 100644 Binary files a/internal/examples/pdfs/sample1.pdf and b/internal/examples/pdfs/sample1.pdf differ diff --git a/internal/examples/sample1/main.go b/internal/examples/sample1/main.go index 71cca724..5ff7812b 100644 --- a/internal/examples/sample1/main.go +++ b/internal/examples/sample1/main.go @@ -143,7 +143,9 @@ func main() { }) }) - m.TableList(headerSmall, smallContent) + m.TableList(headerSmall, smallContent, props.TableList{ + Line: true, + }) m.Row(15, func() { m.Col(func() { diff --git a/internal/signature.go b/internal/signature.go index c9755c92..7f35417a 100644 --- a/internal/signature.go +++ b/internal/signature.go @@ -28,9 +28,9 @@ func NewSignature(pdf gofpdf.Pdf, math Math, text Text) *signature { // AddSpaceFor create a space for a signature inside a cell func (s *signature) AddSpaceFor(label string, textProp props.Text, qtdCols float64, marginTop float64, actualCol float64) { widthPerCol := s.math.GetWidthPerCol(qtdCols) - left, _, right, _ := s.pdf.GetMargins() + left, _, _, _ := s.pdf.GetMargins() space := 4.0 - s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+right-space, marginTop+5.0) + s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+left-space, marginTop+5.0) s.text.Add(label, textProp, marginTop, actualCol, qtdCols) } diff --git a/internal/signature_test.go b/internal/signature_test.go index b935a896..30017b2c 100644 --- a/internal/signature_test.go +++ b/internal/signature_test.go @@ -17,7 +17,7 @@ func TestNewSignature(t *testing.T) { assert.Equal(t, fmt.Sprintf("%T", signature), "*internal.signature") } -func TestSignature_AddSpaceFor(t *testing.T) { +func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) { // Arrange pdf := &mocks.Pdf{} pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) @@ -40,3 +40,27 @@ func TestSignature_AddSpaceFor(t *testing.T) { text.AssertNumberOfCalls(t, "Add", 1) text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0) } + +func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) { + // Arrange + pdf := &mocks.Pdf{} + pdf.On("GetMargins").Return(20.0, 10.0, 10.0, 10.0) + pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything) + + math := &mocks.Math{} + 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) + + signature := internal.NewSignature(pdf, math, text) + + // Act + signature.AddSpaceFor("label", props.Text{Size: 10.0}, 5, 5, 2) + + // Assert + pdf.AssertNumberOfCalls(t, "Line", 1) + pdf.AssertCalled(t, "Line", 124.0, 10.0, 166.0, 10.0) + text.AssertNumberOfCalls(t, "Add", 1) + text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0) +} diff --git a/internal/tablelist.go b/internal/tablelist.go index 850ab7d6..c6d1cb93 100644 --- a/internal/tablelist.go +++ b/internal/tablelist.go @@ -13,6 +13,9 @@ type MarotoGridPart interface { // Helpers GetCurrentOffset() float64 + + // Outside Col/Row Components + Line(spaceHeight float64) } // TableList is the abstraction to create a table with header and contents @@ -109,6 +112,10 @@ func (s *tableList) Create(header []string, contents [][]string, prop ...props.T }) } }) + + if tableProp.Line { + s.pdf.Line(1.0) + } } } diff --git a/internal/tablelist_test.go b/internal/tablelist_test.go index 751496c3..cd49ffc6 100644 --- a/internal/tablelist_test.go +++ b/internal/tablelist_test.go @@ -5,6 +5,7 @@ import ( "github.com/johnfercher/maroto/internal" "github.com/johnfercher/maroto/internal/mocks" "github.com/johnfercher/maroto/pkg/consts" + "github.com/johnfercher/maroto/pkg/props" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "testing" @@ -90,6 +91,42 @@ func TestTableList_Create_Happy(t *testing.T) { marotoGrid := &mocks.Maroto{} marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("Line", mock.Anything).Return(nil) + + sut := internal.NewTableList(text, font) + sut.BindGrid(marotoGrid) + + headers, contents := getContents() + + // Act + sut.Create(headers, contents, props.TableList{ + Line: true, + }) + + // Assert + text.AssertNotCalled(t, "GetLinesQuantity") + text.AssertNumberOfCalls(t, "GetLinesQuantity", 84) + + font.AssertCalled(t, "GetFont") + font.AssertNumberOfCalls(t, "GetFont", 21) + + marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) + marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertNumberOfCalls(t, "Line", 20) +} + +func TestTableList_Create_Happy_Without_Line(t *testing.T) { + // Arrange + text := &mocks.Text{} + text.On("GetLinesQuantity", mock.Anything, mock.Anything, mock.Anything).Return(1) + + font := &mocks.Font{} + font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) + font.On("GetScaleFactor").Return(1.5) + + marotoGrid := &mocks.Maroto{} + marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil) + marotoGrid.On("Line", mock.Anything).Return(nil) sut := internal.NewTableList(text, font) sut.BindGrid(marotoGrid) @@ -108,6 +145,28 @@ func TestTableList_Create_Happy(t *testing.T) { marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything) marotoGrid.AssertNumberOfCalls(t, "Row", 22) + marotoGrid.AssertNumberOfCalls(t, "Line", 0) +} + +func TestTableList_Create_WhenContentIsEmptyWithLine(t *testing.T) { + // Arrange + text := &mocks.Text{} + text.On("GetLinesQuantity", mock.Anything, mock.Anything, mock.Anything).Return(1) + sut := internal.NewTableList(text, nil) + + marotoGrid := mocks.Maroto{} + marotoGrid.On("Line", mock.Anything).Return(nil) + + headers, _ := getContents() + + // Act + sut.Create(headers, [][]string{}, props.TableList{ + Line: true, + }) + + // Assert + text.AssertNotCalled(t, "GetLinesQuantity") + marotoGrid.AssertNotCalled(t, "Line") } func getContents() ([]string, [][]string) { diff --git a/pkg/pdf/example_test.go b/pkg/pdf/example_test.go index 4c8778f2..372fdb04 100644 --- a/pkg/pdf/example_test.go +++ b/pkg/pdf/example_test.go @@ -166,7 +166,9 @@ func ExamplePdfMaroto_TableList() { // 1 Row of header // 2 Rows of contents // Each row have 2 columns - m.TableList(headers, contents) + m.TableList(headers, contents, props.TableList{ + Line: false, + }) // Do more things and save... } diff --git a/pkg/props/prop.go b/pkg/props/prop.go index 4b019d1a..97d4645a 100644 --- a/pkg/props/prop.go +++ b/pkg/props/prop.go @@ -82,6 +82,8 @@ type TableList struct { Align consts.Align // HeaderContentSpace is the space between the header and the contents HeaderContentSpace float64 + // Line adds a line after every content-row to separate rows. The line's spaceHeight is set to 1.0 + Line bool } // MakeValid from Rect means will make the properties from a rectangle reliable to fit inside a cell