Skip to content

Commit

Permalink
Add MaxGridSum const
Browse files Browse the repository at this point in the history
  • Loading branch information
johnferchermeli committed Mar 28, 2020
1 parent bfe790f commit 89803ff
Show file tree
Hide file tree
Showing 31 changed files with 349 additions and 790 deletions.
11 changes: 5 additions & 6 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Code is the abstraction which deals of how to add QrCodes or Barcode in a PDF
type Code interface {
AddQr(code string, yColOffset float64, xColOffset float64, colWidth float64, colHeight float64, prop props.Rect)
AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Barcode) (err error)
AddBar(code string, yColOffset float64, xColOffset float64, colWidth float64, colHeight float64, prop props.Barcode) (err error)
}

type code struct {
Expand Down Expand Up @@ -42,23 +42,22 @@ func (s *code) AddQr(code string, yColOffset float64, xColOffset float64, colWid
}

// AddBar create a Barcode inside a cell
func (s *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Barcode) (err error) {
func (s *code) AddBar(code string, yColOffset float64, xColOffset float64, colWidth float64, colHeight float64, prop props.Barcode) (err error) {
bcode, err := code128.Encode(code)

if err != nil {
return
}

actualWidthPerCol := s.math.GetWidthPerCol(qtdCols)
heightPercentFromWidth := prop.Proportion.Height / prop.Proportion.Width
var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*heightPercentFromWidth, qtdCols, colHeight, indexCol, prop.Percent)
x, y, w, h = s.math.GetRectCenterColProperties(colWidth, colWidth*heightPercentFromWidth, colWidth, colHeight, xColOffset, prop.Percent)
} else {
rectProps := props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
x, y, w, h = s.math.GetRectNonCenterColProperties(actualWidthPerCol, actualWidthPerCol*heightPercentFromWidth, qtdCols, colHeight, indexCol, rectProps)
x, y, w, h = s.math.GetRectNonCenterColProperties(colWidth, colWidth*heightPercentFromWidth, colWidth, colHeight, xColOffset, rectProps)
}

barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false)
barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+yColOffset, w, h, false)
return
}
26 changes: 4 additions & 22 deletions internal/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestCode_AddBar(t *testing.T) {
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
math.On("GetRectNonCenterColProperties", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(100.0, 20.0, 33.0, 0.0)
return math
},
Expand All @@ -52,11 +51,8 @@ func TestCode_AddBar(t *testing.T) {
pdf.AssertCalled(t, "Image", "", 100, 30, 33, 0)
},
func(t *testing.T, math *mocks.Math) {
math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
math.AssertCalled(t, "GetWidthPerCol", 5.0)

math.AssertNumberOfCalls(t, "GetRectNonCenterColProperties", 1)
math.AssertCalled(t, "GetRectNonCenterColProperties", 50, 28, 5, 40, 2, props.Rect{Center: false, Left: 10, Top: 10})
math.AssertCalled(t, "GetRectNonCenterColProperties", 5, 2, 5, 40, 2, props.Rect{Center: false, Left: 10, Top: 10})
},
func(t *testing.T, err error) {
assert.Nil(t, err)
Expand All @@ -74,7 +70,6 @@ func TestCode_AddBar(t *testing.T) {
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
math.On("GetRectCenterColProperties", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(100.0, 20.0, 33.0, 0.0)
return math
},
Expand All @@ -86,11 +81,8 @@ func TestCode_AddBar(t *testing.T) {
pdf.AssertCalled(t, "Image", "", 100, 30, 33, 0)
},
func(t *testing.T, math *mocks.Math) {
math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
math.AssertCalled(t, "GetWidthPerCol", 5.0)

math.AssertNumberOfCalls(t, "GetRectCenterColProperties", 1)
math.AssertCalled(t, "GetRectCenterColProperties", 50, 50, 5, 40, 2, 100)
math.AssertCalled(t, "GetRectCenterColProperties", 5, 5, 5, 40, 2, 100)
},
func(t *testing.T, err error) {
assert.Nil(t, err)
Expand All @@ -108,7 +100,6 @@ func TestCode_AddBar(t *testing.T) {
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
math.On("GetRectCenterColProperties", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(100.0, 20.0, 33.0, 0.0)
return math
},
Expand All @@ -117,7 +108,6 @@ func TestCode_AddBar(t *testing.T) {
pdf.AssertNotCalled(t, "Image")
},
func(t *testing.T, math *mocks.Math) {
math.AssertNotCalled(t, "GetWidthPerCol")
math.AssertNotCalled(t, "GetRectCenterColProperties")
},
func(t *testing.T, err error) {
Expand Down Expand Up @@ -165,7 +155,6 @@ func TestCode_AddQr(t *testing.T) {
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
math.On("GetRectCenterColProperties", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(100.0, 20.0, 33.0, 0.0)
return math
},
Expand All @@ -177,11 +166,8 @@ func TestCode_AddQr(t *testing.T) {
pdf.AssertCalled(t, "Image", "", 100, 30, 33, 0)
},
func(t *testing.T, math *mocks.Math) {
math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
math.AssertCalled(t, "GetWidthPerCol", 5.0)

math.AssertNumberOfCalls(t, "GetRectCenterColProperties", 1)
math.AssertCalled(t, "GetRectCenterColProperties", 50, 50, 5, 40, 2, 100)
math.AssertCalled(t, "GetRectCenterColProperties", 5, 5, 5, 40, 2, 100)
},
props.Rect{Center: true, Percent: 100},
},
Expand All @@ -196,7 +182,6 @@ func TestCode_AddQr(t *testing.T) {
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
math.On("GetRectNonCenterColProperties", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(100.0, 20.0, 33.0, 0.0)
return math
},
Expand All @@ -208,11 +193,8 @@ func TestCode_AddQr(t *testing.T) {
pdf.AssertCalled(t, "Image", "", 100, 30, 33, 0)
},
func(t *testing.T, math *mocks.Math) {
math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
math.AssertCalled(t, "GetWidthPerCol", 5.0)

math.AssertNumberOfCalls(t, "GetRectNonCenterColProperties", 1)
math.AssertCalled(t, "GetRectNonCenterColProperties", 50, 50, 5, 40, 2, props.Rect{Center: false, Left: 10, Top: 10})
math.AssertCalled(t, "GetRectNonCenterColProperties", 5, 5, 5, 40, 2, props.Rect{Center: false, Left: 10, Top: 10})
},
props.Rect{Center: false, Left: 10, Top: 10},
},
Expand Down
35 changes: 20 additions & 15 deletions internal/examples/billing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ func main() {
Percent: 80,
})
})
m.Col(6, func() {})

m.ColSpace(6)

m.Col(3, func() {
m.Text("AnyCompany Name Inc. 851 Any Street Name, Suite 120, Any City, CA 45123.", props.Text{
Top: 4,
Size: 8,
Align: consts.Right,
Extrapolate: false,
})
m.Text("Tel: 55 024 12345-1234", props.Text{
Top: 16,
Top: 12,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
})
m.Text("www.mycompany.com", props.Text{
Top: 19,
Top: 15,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
Expand Down Expand Up @@ -85,25 +86,27 @@ func main() {
m.Row(10, func() {
m.Col(12, func() {
m.Text("Invoice ABC123456789", props.Text{
Top: 6,
Top: 3,
Style: consts.Bold,
Align: consts.Center,
})
})
})

m.SetBackgroundColor(darkGrayColor)

m.Row(7, func() {
m.Col(12, func() {
m.Col(3, func() {
m.Text("Transactions", props.Text{
Top: 4.5,
Top: 1.5,
Size: 9,
Style: consts.Bold,
Align: consts.Center,
})
})
m.ColSpaces(3)
m.ColSpace(9)
})

m.SetBackgroundColor(whiteColor)

header := []string{"", "Product", "Quantity", "Price"}
Expand Down Expand Up @@ -138,11 +141,13 @@ func main() {
}

m.TableList(header, contents, props.TableList{
HeaderProp: props.Font{
Size: 9,
HeaderProp: props.TableListContent{
Size: 9,
GridSizes: []uint{3, 4, 2, 3},
},
ContentProp: props.Font{
Size: 8,
ContentProp: props.TableListContent{
Size: 8,
GridSizes: []uint{3, 4, 2, 3},
},
Align: consts.Center,
AlternatedBackground: &grayColor,
Expand All @@ -151,8 +156,8 @@ func main() {
})

m.Row(20, func() {
m.Col(6, func() {})
m.Col(3, func() {
m.ColSpace(7)
m.Col(2, func() {
m.Text("Total:", props.Text{
Top: 5,
Style: consts.Bold,
Expand Down Expand Up @@ -187,7 +192,7 @@ func main() {
Align: consts.Center,
})
})
m.Col(6, func() {})
m.ColSpace(6)
})

err := m.OutputFileAndClose("internal/examples/pdfs/billing.pdf")
Expand Down
4 changes: 2 additions & 2 deletions internal/examples/certificate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
})
m.Col(4, func() {
m.Text("Golang Certificate", props.Text{
Top: 12,
Top: 6,
Align: consts.Center,
Size: 20,
Style: consts.BoldItalic,
Expand All @@ -43,7 +43,7 @@ func main() {
m.Text(text, props.Text{
Size: 13,
Align: consts.Center,
Top: 60,
Top: 50,
VerticalPadding: 2.0,
})
})
Expand Down
Loading

0 comments on commit 89803ff

Please sign in to comment.