Skip to content

Commit

Permalink
Add content vertical padding (#209)
Browse files Browse the repository at this point in the history
* Update Makefile to improve commands.
* Update install.sh to improve installation.
  • Loading branch information
johnfercher authored May 9, 2022
1 parent 303a313 commit 609811d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ fmt:
goimports -w ${GO_PATHS}

lint:
golangci-lint run --config=.golangci.yml ./...
goreportcard-cli -v
golangci-lint run --config=.golangci.yml ./...

install:
bash install.sh
Expand Down
6 changes: 5 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env bash

go install golang.org/x/tools/cmd/goimports@latest
sudo cp $GOPATH/bin/goimports /usr/local/bin/

go install mvdan.cc/gofumpt@latest
sudo cp $GOPATH/bin/gofumpt /usr/local/bin/

curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0
sudo cp $GOPATH/bin/golangci-lint /usr/local/bin/

git clone https://github.com/gojp/goreportcard.git
cd goreportcard
make install
go install ./cmd/goreportcard-cli
cd ..
sudo rm -R goreportcard
sudo rm -R goreportcard
sudo cp $GOPATH/bin/goreportcard-cli /usr/local/bin/
Binary file modified internal/examples/pdfs/sample1.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions internal/examples/sample1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func main() {
Green: 200,
Blue: 200,
},
VerticalContentPadding: 10,
})

m.AddPage()
Expand Down
7 changes: 4 additions & 3 deletions internal/tablelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *tableList) BindGrid(pdf MarotoGridPart) {
s.pdf = pdf
}

// Create create a header section with a list of strings and
// Create method creates a header section with a list of strings and
// create many rows with contents.
func (s *tableList) Create(header []string, contents [][]string, defaultFontFamily string, prop ...props.TableList) {
if len(header) == 0 {
Expand Down Expand Up @@ -95,17 +95,18 @@ func (s *tableList) Create(header []string, contents [][]string, defaultFontFami
// Draw contents.
for index, content := range contents {
contentHeight := s.calcLinesHeight(content, tableProp.ContentProp, tableProp.Align)
contentHeightPadded := contentHeight + tableProp.VerticalContentPadding

if tableProp.AlternatedBackground != nil && index%2 == 0 {
s.pdf.SetBackgroundColor(*tableProp.AlternatedBackground)
}

s.pdf.Row(contentHeight+1, func() {
s.pdf.Row(contentHeightPadded+1, func() {
for i, c := range content {
cs := c

s.pdf.Col(tableProp.ContentProp.GridSizes[i], func() {
s.pdf.Text(cs, tableProp.ContentProp.ToTextProp(tableProp.Align, 0, false, 0.0))
s.pdf.Text(cs, tableProp.ContentProp.ToTextProp(tableProp.Align, tableProp.VerticalContentPadding/2.0, false, 0.0))
})
}
})
Expand Down
37 changes: 37 additions & 0 deletions internal/tablelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@ func TestTableList_Create_Happy_Without_Line(t *testing.T) {
marotoGrid.AssertNumberOfCalls(t, "Line", 0)
}

func TestTableList_Create_HappyWithVerticalContentPadding(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)
marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0)
marotoGrid.On("GetPageSize").Return(200.0, 600.0)

sut := internal.NewTableList(text, font)
sut.BindGrid(marotoGrid)

headers, contents := getContents()

// Act
sut.Create(headers, contents, consts.Arial, props.TableList{
VerticalContentPadding: 4.0,
})

// 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", 0)
}

func TestTableList_Create_WhenContentIsEmptyWithLine(t *testing.T) {
// Arrange
text := &mocks.Text{}
Expand Down
6 changes: 4 additions & 2 deletions pkg/pdf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func ExamplePdfMaroto_TableList() {
// Not passing Content.GridSizes, make the method use an array with same length
// of content array in the first line, the values will be perfectly divided to make all columns with the same size.
// Not passing HeaderContentSpace, will make the method use 4.
// Not passing VerticalContentPadding, will make the method use 0. This prop cannot be less than 0.

m := pdf.NewMaroto(consts.Portrait, consts.A4)

Expand Down Expand Up @@ -218,8 +219,9 @@ func ExamplePdfMaroto_TableList() {
Green: 20,
Blue: 255,
},
HeaderContentSpace: 10.0,
Line: false,
HeaderContentSpace: 10.0,
Line: false,
VerticalContentPadding: 4.0,
})

// TableList have to be used at same level as row
Expand Down
14 changes: 10 additions & 4 deletions pkg/props/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type TableList struct {
AlternatedBackground *color.Color
// HeaderContentSpace is the space between the header and the contents.
HeaderContentSpace float64
// VerticalContentPadding define the space between lines in content.
VerticalContentPadding float64
// Line adds a line after every content-row to separate rows. The line's spaceHeight is set to 1.0.
Line bool
}
Expand Down Expand Up @@ -276,8 +278,8 @@ func (s *TableListContent) ToTextProp(align consts.Align, top float64, extrapola

// MakeValid from TableList define default values for a TableList.
func (s *TableList) MakeValid(header []string, defaultFamily string) {
undefinedValue := 0.0
if s.HeaderProp.Size == undefinedValue {
zeroValue := 0.0
if s.HeaderProp.Size == zeroValue {
s.HeaderProp.Size = 10.0
}

Expand All @@ -302,7 +304,7 @@ func (s *TableList) MakeValid(header []string, defaultFamily string) {
s.Align = consts.Left
}

if s.ContentProp.Size == undefinedValue {
if s.ContentProp.Size == zeroValue {
s.ContentProp.Size = 10.0
}

Expand All @@ -323,9 +325,13 @@ func (s *TableList) MakeValid(header []string, defaultFamily string) {
}
}

if s.HeaderContentSpace == undefinedValue {
if s.HeaderContentSpace == zeroValue {
s.HeaderContentSpace = 4.0
}

if s.VerticalContentPadding < zeroValue {
s.VerticalContentPadding = zeroValue
}
}

// MakeValid from Line define default values for a Line.
Expand Down
9 changes: 9 additions & 0 deletions pkg/props/prop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ func TestTableListProp_MakeValid(t *testing.T) {
assert.Equal(t, m.HeaderContentSpace, 4.0)
},
},
{
"When VerticalContentPadding is less than 0.0",
&props.TableList{
VerticalContentPadding: -4.0,
},
func(t *testing.T, m *props.TableList) {
assert.Equal(t, m.VerticalContentPadding, 0.0)
},
},
}

for _, c := range cases {
Expand Down

0 comments on commit 609811d

Please sign in to comment.