Skip to content

Commit

Permalink
Merge pull request johnfercher#111 from johnfercher/dev
Browse files Browse the repository at this point in the history
release/v0.15.6
  • Loading branch information
johnfercher authored Oct 24, 2019
2 parents 008f53d + 43908ce commit 5f5bc21
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 39 deletions.
26 changes: 19 additions & 7 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package internal
import (
"github.com/boombuler/barcode/code128"
"github.com/boombuler/barcode/qr"
"github.com/johnfercher/maroto/pkg/props"
"github.com/jung-kurt/gofpdf"
"github.com/jung-kurt/gofpdf/contrib/barcode"
)

// Code is the abstraction which deals of how to add QrCodes or Barcode in a PDF
type Code interface {
AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64)
AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, rectPercent float64, heightPercentFromWidth float64) (err error)
AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect)
AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Barcode) (err error)
}

type code struct {
Expand All @@ -27,27 +28,38 @@ func NewCode(pdf gofpdf.Pdf, math Math) *code {
}

// AddQr create a QrCode inside a cell
func (s *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) {
func (s *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect) {
key := barcode.RegisterQR(s.pdf, code, qr.H, qr.Unicode)

actualWidthPerCol := s.math.GetWidthPerCol(qtdCols)

x, y, w, h := s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, percent)
var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, prop.Percent)
} else {
x, y, w, h = s.math.GetRectNonCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, prop)
}

barcode.Barcode(s.pdf, key, x, y+marginTop, w, h, false)
}

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

if err != nil {
return
}

actualWidthPerCol := s.math.GetWidthPerCol(qtdCols)

x, y, w, h := s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*heightPercentFromWidth, qtdCols, colHeight, indexCol, rectPercent)
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)
} 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)
}

barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false)
return
Expand Down
80 changes: 76 additions & 4 deletions internal/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package internal_test

import (
"fmt"
"testing"

"github.com/johnfercher/maroto/internal"
"github.com/johnfercher/maroto/internal/mocks"
"github.com/johnfercher/maroto/pkg/props"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)

func TestNewCode(t *testing.T) {
Expand All @@ -25,6 +27,7 @@ func TestCode_AddBar(t *testing.T) {
assertPdf func(t *testing.T, pdf *mocks.Pdf)
assertMath func(t *testing.T, math *mocks.Math)
assertError func(t *testing.T, err error)
prop props.Barcode
}{
{
"When everything works",
Expand All @@ -35,6 +38,40 @@ func TestCode_AddBar(t *testing.T) {
pdf.On("Image", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
return pdf
},
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
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetImageInfo", 1)
pdf.AssertCalled(t, "GetImageInfo", "barcode-Code 128AnyCode-1E+023E+01")

pdf.AssertNumberOfCalls(t, "Image", 1)
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})
},
func(t *testing.T, err error) {
assert.Nil(t, err)
},
props.Barcode{Center: false, Left: 10, Top: 10, Proportion: props.Proportion{Width: 16, Height: 9}},
},
{
"When everything works and code centered",
"AnyCode",
func() *mocks.Pdf {
pdf := &mocks.Pdf{}
pdf.On("GetImageInfo", mock.Anything).Return(widthGreaterThanHeightImageInfo())
pdf.On("Image", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
return pdf
},
func() *mocks.Math {
math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)
Expand All @@ -53,11 +90,12 @@ func TestCode_AddBar(t *testing.T) {
math.AssertCalled(t, "GetWidthPerCol", 5.0)

math.AssertNumberOfCalls(t, "GetRectCenterColProperties", 1)
math.AssertCalled(t, "GetRectCenterColProperties", 50, 0, 5, 40, 2, 100)
math.AssertCalled(t, "GetRectCenterColProperties", 50, 50, 5, 40, 2, 100)
},
func(t *testing.T, err error) {
assert.Nil(t, err)
},
props.Barcode{Center: true, Percent: 100, Proportion: props.Proportion{Width: 1, Height: 1}},
},
{
"When cannot generate QrCode",
Expand Down Expand Up @@ -85,6 +123,7 @@ func TestCode_AddBar(t *testing.T) {
func(t *testing.T, err error) {
assert.NotNil(t, err)
},
props.Barcode{Center: true, Percent: 100},
},
}

Expand All @@ -96,7 +135,7 @@ func TestCode_AddBar(t *testing.T) {
code := internal.NewCode(pdf, math)

// Act
err := code.AddBar(c.code, 10, 2, 5, 40, 100, 0)
err := code.AddBar(c.code, 10, 2, 5, 40, c.prop)

// Assert
c.assertPdf(t, pdf)
Expand All @@ -113,6 +152,7 @@ func TestCode_AddQr(t *testing.T) {
math func() *mocks.Math
assertPdf func(t *testing.T, pdf *mocks.Pdf)
assertMath func(t *testing.T, math *mocks.Math)
prop props.Rect
}{
{
"When everything works",
Expand Down Expand Up @@ -143,6 +183,38 @@ func TestCode_AddQr(t *testing.T) {
math.AssertNumberOfCalls(t, "GetRectCenterColProperties", 1)
math.AssertCalled(t, "GetRectCenterColProperties", 50, 50, 5, 40, 2, 100)
},
props.Rect{Center: true, Percent: 100},
},
{
"When everything works not-centered",
"AnyCode",
func() *mocks.Pdf {
pdf := &mocks.Pdf{}
pdf.On("GetImageInfo", mock.Anything).Return(widthGreaterThanHeightImageInfo())
pdf.On("Image", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
return pdf
},
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
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetImageInfo", 1)
pdf.AssertCalled(t, "GetImageInfo", "barcode-QR CodeAnyCode-1E+023E+01")

pdf.AssertNumberOfCalls(t, "Image", 1)
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})
},
props.Rect{Center: false, Left: 10, Top: 10},
},
}

Expand All @@ -154,7 +226,7 @@ func TestCode_AddQr(t *testing.T) {
code := internal.NewCode(pdf, math)

// Act
code.AddQr(c.code, 10, 2, 5, 40, 100)
code.AddQr(c.code, 10, 2, 5, 40, c.prop)

// Assert
c.assertPdf(t, pdf)
Expand Down
21 changes: 12 additions & 9 deletions internal/mocks/code.go

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

14 changes: 3 additions & 11 deletions pkg/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pdf

import (
"bytes"

"github.com/johnfercher/maroto/internal"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/props"
Expand Down Expand Up @@ -405,11 +406,7 @@ func (s *PdfMaroto) Barcode(code string, prop ...props.Barcode) (err error) {
qtdCols := float64(len(s.colsClosures))
sumOfyOffsets := s.offsetY + barcodeProp.Top

if barcodeProp.Center {
err = s.Code.AddBar(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, barcodeProp.Percent, barcodeProp.Proportion.Height/barcodeProp.Proportion.Width)
} else {
err = s.Code.AddBar(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, barcodeProp.Percent, barcodeProp.Proportion.Height/barcodeProp.Proportion.Width)
}
err = s.Code.AddBar(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, barcodeProp)

return
}
Expand All @@ -425,12 +422,7 @@ func (s *PdfMaroto) QrCode(code string, prop ...props.Rect) {

qtdCols := float64(len(s.colsClosures))
sumOfyOffsets := s.offsetY + rectProp.Top

if rectProp.Center {
s.Code.AddQr(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, rectProp.Percent)
} else {
s.Code.AddQr(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, rectProp.Percent)
}
s.Code.AddQr(code, sumOfyOffsets, s.rowColCount, qtdCols, s.rowHeight, rectProp)
}

func (s *PdfMaroto) createColSpace(actualWidthPerCol float64) {
Expand Down
Loading

0 comments on commit 5f5bc21

Please sign in to comment.