Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/custom-margins #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified internal/examples/pdfs/sample1.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion internal/examples/sample1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions internal/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
26 changes: 25 additions & 1 deletion internal/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions internal/tablelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,6 +112,10 @@ func (s *tableList) Create(header []string, contents [][]string, prop ...props.T
})
}
})

if tableProp.Line {
s.pdf.Line(1.0)
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions internal/tablelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/pdf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/props/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down