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

release/09-01-2020 #134

Merged
merged 3 commits into from
Jan 10, 2020
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/certificate.pdf
Binary file not shown.
Binary file modified internal/examples/pdfs/sample1.pdf
Binary file not shown.
Binary file modified internal/examples/pdfs/zpl.pdf
Binary file not shown.
25 changes: 7 additions & 18 deletions internal/examples/sample1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/base64"
"fmt"
"github.com/johnfercher/maroto/pkg/color"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/pdf"
"github.com/johnfercher/maroto/pkg/props"
Expand All @@ -28,7 +29,6 @@ func main() {
headerMedium, mediumContent := getMediumContent()

m.RegisterHeader(func() {

m.Row(20, func() {
m.Col(func() {
m.Base64Image(base64, consts.Jpg, props.Rect{
Expand Down Expand Up @@ -144,7 +144,11 @@ func main() {
})

m.TableList(headerSmall, smallContent, props.TableList{
Line: true,
AlternatedBackground: &color.Color{
Red: 200,
Green: 200,
Blue: 200,
},
})

m.Row(15, func() {
Expand All @@ -158,6 +162,7 @@ func main() {

m.TableList(headerMedium, mediumContent, props.TableList{
Align: consts.Center,
Line: true,
HeaderProp: props.Font{
Family: consts.Courier,
Style: consts.BoldItalic,
Expand Down Expand Up @@ -204,22 +209,6 @@ func getSmallContent() ([]string, [][]string) {
contents = append(contents, []string{"Natal", "Santo André", "", "R$ 198,00"})
contents = append(contents, []string{"Rio Grande do Norte", "Sorocaba", "", "R$ 42,00"})
contents = append(contents, []string{"Campinas", "Recife", "", "R$ 58,00"})
contents = append(contents, []string{"São Vicente", "Juiz de Fora", "", "R$ 39,00"})
contents = append(contents, []string{"Taubaté", "Rio de Janeiro", "", "R$ 77,00"})
contents = append(contents, []string{"Suzano", "Petrópolis", "", "R$ 64,00"})
contents = append(contents, []string{"Jundiaí", "Florianópolis", "", "R$ 20,00"})
contents = append(contents, []string{"Natal", "Jundiaí", "", "R$ 18,00"})
contents = append(contents, []string{"Niterói", "Itapevi", "", "R$ 24,00"})
contents = append(contents, []string{"Jundiaí", "Florianópolis", "", "R$ 23,00"})
contents = append(contents, []string{"Natal", "Jundiaí", "", "R$ 11,00"})
contents = append(contents, []string{"Niterói", "Itapevi", "", "R$ 28,00"})
contents = append(contents, []string{"São Paulo", "Rio de Janeiro", "", "R$ 19,00"})
contents = append(contents, []string{"São Carlos", "Petrópolis", "", "R$ 23,00"})
contents = append(contents, []string{"Florianópolis", "Osasco", "", "R$ 21,00"})
contents = append(contents, []string{"Osasco", "São Paulo", "", "R$ 6,00"})
contents = append(contents, []string{"Congonhas", "Fortaleza", "", "R$ 109,00"})
contents = append(contents, []string{"Natal", "Santo André", "", "R$ 244,00"})
contents = append(contents, []string{"São Carlos", "Petrópolis", "", "R$ 34,00"})
contents = append(contents, []string{"Florianópolis", "Osasco", "", "R$ 21,00"})

return header, contents
Expand Down
46 changes: 46 additions & 0 deletions internal/mocks/maroto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion internal/tablelist.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/johnfercher/maroto/pkg/color"
"github.com/johnfercher/maroto/pkg/props"
)

Expand All @@ -12,6 +13,7 @@ type MarotoGridPart interface {
ColSpace()

// Helpers
SetBackgroundColor(color color.Color)
GetCurrentOffset() float64

// Outside Col/Row Components
Expand Down Expand Up @@ -55,6 +57,7 @@ func (s *tableList) Create(header []string, contents [][]string, prop ...props.T
}

tableProp := props.TableList{}

if len(prop) > 0 {
tableProp = prop[0]
}
Expand Down Expand Up @@ -96,10 +99,14 @@ func (s *tableList) Create(header []string, contents [][]string, prop ...props.T
contentMarginTop := 2.0

// Draw contents
for _, content := range contents {
for index, content := range contents {
contentTextProp := tableProp.ContentProp.ToTextProp(tableProp.Align, 0.0, false, 1.0)
contentHeight := s.calcLinesHeight(content, contentTextProp, qtdCols)

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

s.pdf.Row(contentHeight, func() {
for j, c := range content {
cs := c
Expand All @@ -113,6 +120,10 @@ func (s *tableList) Create(header []string, contents [][]string, prop ...props.T
}
})

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

if tableProp.Line {
s.pdf.Line(1.0)
}
Expand Down
48 changes: 48 additions & 0 deletions internal/tablelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/johnfercher/maroto/internal"
"github.com/johnfercher/maroto/internal/mocks"
"github.com/johnfercher/maroto/pkg/color"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/props"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,6 +93,7 @@ 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)
marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil)

sut := internal.NewTableList(text, font)
sut.BindGrid(marotoGrid)
Expand All @@ -113,6 +115,52 @@ func TestTableList_Create_Happy(t *testing.T) {
marotoGrid.AssertCalled(t, "Row", mock.Anything, mock.Anything)
marotoGrid.AssertNumberOfCalls(t, "Row", 22)
marotoGrid.AssertNumberOfCalls(t, "Line", 20)
marotoGrid.AssertNotCalled(t, "SetBackgroundColor")
}

func TestTableList_Create_HappyWithBackgroundColor(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("SetBackgroundColor", mock.Anything).Return(nil)

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

headers, contents := getContents()
color := color.Color{
Red: 200,
Green: 200,
Blue: 200,
}

// Act
sut.Create(headers, contents, props.TableList{
AlternatedBackground: &color,
})

// 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.AssertNotCalled(t, "Line")

marotoGrid.AssertCalled(t, "SetBackgroundColor", color)
marotoGrid.AssertNumberOfCalls(t, "SetBackgroundColor", 20)
}

func TestTableList_Create_Happy_Without_Line(t *testing.T) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package color

// Color represents a color in the RGB (Red, Green, Blue) space,
// is possible mix values, when all values are 0 the result color is black
// when all values are 255 the result color is white
type Color struct {
// Red is the amount of red
Red int
// Green is the amount of red
Green int
// Blue is the amount of red
Blue int
}

// IsWhite from Color will return true if all components of color
// are in the maximum value
func (s *Color) IsWhite() bool {
return s.Red == 255 && s.Green == 255 && s.Blue == 255
}

// NewWhite return a Color with all components (red, green and blue) as 255
func NewWhite() Color {
return Color{
Red: 255,
Green: 255,
Blue: 255,
}
}
25 changes: 25 additions & 0 deletions pkg/color/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package color_test

import (
"github.com/johnfercher/maroto/pkg/color"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewWhite(t *testing.T) {
// Act
white := color.NewWhite()

// Assert
assert.Equal(t, 255, white.Red)
assert.Equal(t, 255, white.Green)
assert.Equal(t, 255, white.Blue)
}

func TestColor_IsWhite(t *testing.T) {
// Act
white := color.NewWhite()

// Assert
assert.True(t, white.IsWhite())
}
Loading