diff --git a/cmd/benchmark/main.go b/cmd/benchmark/main.go index 31bcdf69..1b9225bc 100644 --- a/cmd/benchmark/main.go +++ b/cmd/benchmark/main.go @@ -3,13 +3,14 @@ package main import ( "encoding/base64" "fmt" + "log" + "os" + "github.com/johnfercher/maroto/v2/pkg" "github.com/johnfercher/maroto/v2/pkg/components/list" "github.com/johnfercher/maroto/v2/pkg/config" "github.com/johnfercher/maroto/v2/pkg/consts/fontstyle" "github.com/johnfercher/maroto/v2/pkg/metrics" - "log" - "os" "github.com/johnfercher/maroto/v2/pkg/components/code" "github.com/johnfercher/maroto/v2/pkg/components/col" diff --git a/cmd/seriesanalyzer/main.go b/cmd/seriesanalyzer/main.go index 0bbc50bf..f00e9dd3 100644 --- a/cmd/seriesanalyzer/main.go +++ b/cmd/seriesanalyzer/main.go @@ -35,7 +35,6 @@ func main() { values = MergeFloat64(values) fmt.Printf("min: %f, max: %f, avg: %f", values[0], values[len(values)-1], sum/float64(len(values))) - } func MergeFloat64(arr []float64) []float64 { diff --git a/docs/assets/pdf/v2.pdf b/docs/assets/pdf/v2.pdf index b64547e6..41ff2845 100644 Binary files a/docs/assets/pdf/v2.pdf and b/docs/assets/pdf/v2.pdf differ diff --git a/docs/assets/text/benchmark.txt b/docs/assets/text/benchmark.txt index 08b226f2..e21f05d3 100644 --- a/docs/assets/text/benchmark.txt +++ b/docs/assets/text/benchmark.txt @@ -1,100 +1,100 @@ -5515.022022 -5461.417454 -5783.617374 -5939.406124 -5684.499158 -4424.539152 -5920.815225 -5830.644510 -5765.540092 -5707.093462 -5669.253572 -4503.201213 -5564.258567 -5334.571851 -5504.792733 -5497.962803 -5529.068299 -5877.839985 -5735.885533 -5574.879309 -5564.302229 -5614.734553 -5496.695531 -5478.404788 -5764.265337 -5662.622510 -5617.628917 -5371.190130 -5954.118575 -5463.248509 -5554.750588 -5861.176554 -5648.693982 -5650.326523 -5811.580272 -5731.005895 -5558.532850 -5601.577597 -5489.724021 -5543.991777 -5705.157780 -5681.463877 -5526.128160 -5499.149390 -5907.563152 -5791.913570 -5325.729492 -5907.386188 -5533.287981 -5205.516260 -4862.429075 -4897.987021 -5452.731946 -5248.854952 -5672.461781 -5123.021582 -5339.021071 -5397.399020 -5381.361534 -5745.743182 -5511.414987 -5752.520206 -5702.828402 -5496.030245 -5444.854650 -5527.963920 -5587.664519 -5547.919667 -5460.504717 -5509.720280 -5452.043404 -5439.028055 -5672.496564 -5868.281863 -5534.729776 -5500.486685 -5455.039711 -5558.785145 -5581.402456 -5556.771049 -5863.934637 -5524.878669 -5547.294041 -5586.700511 -5487.428624 -5458.483113 -5835.450671 -5463.818765 -5569.337687 -5586.768473 -5342.935136 -5609.942063 -5540.945568 -5813.500062 -5516.432577 -5591.527467 -5636.041784 -5940.720009 -5875.524006 -5602.912555 +5223.628784 +5575.578389 +5410.718548 +5398.338943 +5262.264245 +5712.424899 +5438.601524 +5593.661505 +5227.655421 +5532.560877 +5650.654550 +5641.232854 +5581.619865 +5196.614951 +5140.590926 +5328.126879 +5225.411856 +5401.480260 +5474.679002 +5509.033807 +5742.595669 +5126.245241 +5552.133071 +5460.226937 +5543.177412 +5438.257995 +5181.284297 +5627.871531 +5258.040991 +5354.266922 +5411.510394 +5321.331024 +5254.987849 +5347.744059 +5335.948834 +5635.848712 +4974.168542 +5396.796540 +5476.675788 +5420.781391 +5646.839641 +5630.585459 +5420.969948 +5330.762390 +5548.827074 +5337.213714 +5666.176975 +5364.715929 +5427.974671 +5419.308755 +5296.183883 +5784.326939 +5607.713724 +5544.878717 +5818.564467 +5468.927676 +5108.837690 +5511.177886 +5590.841133 +5662.542433 +5575.006457 +5260.929705 +5412.395735 +5349.055622 +5501.830339 +5339.860254 +5306.706313 +5158.078028 +5316.305182 +5226.683079 +5442.055879 +5503.493854 +5274.212995 +5465.321695 +5299.267289 +5358.990717 +5248.681769 +5569.302136 +5633.104135 +5402.807802 +5554.677969 +5427.837244 +5533.140406 +5303.087471 +5424.740884 +5484.484348 +5454.072626 +5046.794184 +5530.307697 +5827.249457 +5393.897909 +5152.378420 +5481.813430 +5270.336527 +5386.750988 +5377.800257 +5405.899201 +5316.287864 +5289.427599 +5428.795759 diff --git a/docs/assets/text/v2.txt b/docs/assets/text/v2.txt index ac84ff8f..2703f6f8 100644 --- a/docs/assets/text/v2.txt +++ b/docs/assets/text/v2.txt @@ -1,5 +1,5 @@ -generate -> avg: 17.96ms, executions: [17.96ms] -header -> avg: 456.00ns, executions: [456.00ns] -footer -> avg: 60.00ns, executions: [60.00ns] -add_row -> avg: 63.35ns, executions: [153.00ns, 154.00ns, 69.00ns, 83.00ns, 24.00ns, 19.00ns, 586.00ns, 60.00ns, 25.00ns, 62.00ns, 26.00ns, 16.00ns, 17.00ns, 51.00ns, 16.00ns, 17.00ns, 24.00ns, 296.00ns, 51.00ns, 17.00ns, 57.00ns, 19.00ns, 17.00ns, 19.00ns, 63.00ns, 17.00ns, 16.00ns, 16.00ns, 249.00ns, 97.00ns, 21.00ns, 77.00ns, 17.00ns, 19.00ns, 18.00ns, 48.00ns, 19.00ns, 18.00ns, 16.00ns, 212.00ns, 51.00ns, 16.00ns, 62.00ns, 19.00ns, 16.00ns, 15.00ns, 57.00ns, 18.00ns, 19.00ns, 17.00ns, 239.00ns, 54.00ns, 19.00ns, 64.00ns, 17.00ns] +generate -> avg: 18.16ms, executions: [18.16ms] +header -> avg: 321.00ns, executions: [321.00ns] +footer -> avg: 69.00ns, executions: [69.00ns] +add_row -> avg: 63.65ns, executions: [138.00ns, 135.00ns, 46.00ns, 80.00ns, 23.00ns, 24.00ns, 629.00ns, 78.00ns, 24.00ns, 65.00ns, 23.00ns, 16.00ns, 17.00ns, 59.00ns, 19.00ns, 17.00ns, 23.00ns, 298.00ns, 54.00ns, 14.00ns, 53.00ns, 16.00ns, 16.00ns, 23.00ns, 64.00ns, 19.00ns, 17.00ns, 16.00ns, 271.00ns, 80.00ns, 20.00ns, 74.00ns, 16.00ns, 18.00ns, 17.00ns, 57.00ns, 18.00ns, 20.00ns, 18.00ns, 209.00ns, 55.00ns, 16.00ns, 53.00ns, 18.00ns, 15.00ns, 16.00ns, 50.00ns, 19.00ns, 17.00ns, 17.00ns, 250.00ns, 52.00ns, 20.00ns, 63.00ns, 16.00ns] file_size -> 260.96Kb diff --git a/internal/code.go b/internal/code.go index 92a9e6b8..c3361713 100644 --- a/internal/code.go +++ b/internal/code.go @@ -4,6 +4,7 @@ import ( "github.com/boombuler/barcode/code128" "github.com/boombuler/barcode/qr" "github.com/johnfercher/maroto/v2/internal/fpdf" + "github.com/johnfercher/maroto/v2/internal/math" "github.com/johnfercher/maroto/v2/pkg/config" "github.com/johnfercher/maroto/v2/pkg/core" "github.com/johnfercher/maroto/v2/pkg/props" @@ -12,18 +13,18 @@ import ( // Code is the abstraction which deals of how to add QrCodes or Barcode in a PDF. type Code interface { - AddQr(code string, cell *core.Cell, prop *props.Rect) - AddBar(code string, cell *core.Cell, prop *props.Barcode) (err error) - AddDataMatrix(code string, cell *core.Cell, prop *props.Rect) + AddQr(code string, cell *core.Cell, margins *config.Margins, prop *props.Rect) + AddBar(code string, cell *core.Cell, margins *config.Margins, prop *props.Barcode) (err error) + AddDataMatrix(code string, cell *core.Cell, margins *config.Margins, prop *props.Rect) } type code struct { pdf fpdf.Fpdf - math Math + math math.Math } // NewCode create a Code. -func NewCode(pdf fpdf.Fpdf, math Math) *code { +func NewCode(pdf fpdf.Fpdf, math math.Math) *code { return &code{ pdf, math, @@ -31,52 +32,53 @@ func NewCode(pdf fpdf.Fpdf, math Math) *code { } // AddDataMatrix creates a DataMatrix code inside a cell. -func (s *code) AddDataMatrix(code string, cell *core.Cell, prop *props.Rect) { +func (s *code) AddDataMatrix(code string, cell *core.Cell, margins *config.Margins, prop *props.Rect) { key := barcode.RegisterDataMatrix(s.pdf, code) + dimensions := &config.Dimensions{Width: cell.Width, Height: cell.Width} - var x, y, w, h float64 + var rectCell *core.Cell if prop.Center { - dimensions := &config.Dimensions{Width: cell.Width, Height: cell.Width} - x, y, w, h = s.math.GetRectCenterColProperties(dimensions, cell, prop.Percent) + rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent) } else { - x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop) + rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, prop) } - barcode.Barcode(s.pdf, key, x, y+cell.Y, w, h, false) + + barcode.Barcode(s.pdf, key, rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false) } // AddQr create a QrCode inside a cell. -func (s *code) AddQr(code string, cell *core.Cell, prop *props.Rect) { +func (s *code) AddQr(code string, cell *core.Cell, margins *config.Margins, prop *props.Rect) { key := barcode.RegisterQR(s.pdf, code, qr.H, qr.Unicode) + dimensions := &config.Dimensions{Width: cell.Width, Height: cell.Width} - var x, y, w, h float64 + var rectCell *core.Cell if prop.Center { - dimensions := &config.Dimensions{Width: cell.Width, Height: cell.Width} - x, y, w, h = s.math.GetRectCenterColProperties(dimensions, cell, prop.Percent) + rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent) } else { - x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop) + rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, prop) } - barcode.Barcode(s.pdf, key, x, y+cell.Y, w, h, false) + barcode.Barcode(s.pdf, key, rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false) } // AddBar create a Barcode inside a cell. -func (s *code) AddBar(code string, cell *core.Cell, prop *props.Barcode) (err error) { +func (s *code) AddBar(code string, cell *core.Cell, margins *config.Margins, prop *props.Barcode) (err error) { bcode, err := code128.Encode(code) if err != nil { return } heightPercentFromWidth := prop.Proportion.Height / prop.Proportion.Width - var x, y, w, h float64 + dimensions := &config.Dimensions{cell.Width, cell.Width * heightPercentFromWidth} + + var rectCell *core.Cell if prop.Center { - dimensions := &config.Dimensions{cell.Width, cell.Width * heightPercentFromWidth} - x, y, w, h = s.math.GetRectCenterColProperties(dimensions, cell, prop.Percent) + rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, 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(cell.Width, cell.Width*heightPercentFromWidth, cell.Width, - cell.Height, cell.X, rectProps) + rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, rectProps) } - barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+cell.Y, w, h, false) + barcode.Barcode(s.pdf, barcode.Register(bcode), rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false) return } diff --git a/internal/image.go b/internal/image.go index 34ecee5a..dea06a34 100644 --- a/internal/image.go +++ b/internal/image.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/base64" "errors" + + "github.com/johnfercher/maroto/v2/internal/math" "github.com/johnfercher/maroto/v2/pkg/config" "github.com/johnfercher/maroto/v2/pkg/core" @@ -17,17 +19,17 @@ import ( // Image is the abstraction which deals of how to add images in a PDF. type Image interface { - AddFromFile(path string, cell *core.Cell, prop *props.Rect) (err error) - AddFromBase64(stringBase64 string, cell *core.Cell, prop *props.Rect, extension extension.Type) (err error) + AddFromFile(path string, cell *core.Cell, margins *config.Margins, prop *props.Rect) (err error) + AddFromBase64(stringBase64 string, cell *core.Cell, margins *config.Margins, prop *props.Rect, extension extension.Type) (err error) } type image struct { pdf fpdf.Fpdf - math Math + math math.Math } // NewImage create an Image. -func NewImage(pdf fpdf.Fpdf, math Math) *image { +func NewImage(pdf fpdf.Fpdf, math math.Math) *image { return &image{ pdf, math, @@ -35,7 +37,7 @@ func NewImage(pdf fpdf.Fpdf, math Math) *image { } // AddFromFile open an image from disk and add to PDF. -func (s *image) AddFromFile(path string, cell *core.Cell, prop *props.Rect) error { +func (s *image) AddFromFile(path string, cell *core.Cell, margins *config.Margins, prop *props.Rect) error { info := s.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{ ReadDpi: false, ImageType: "", @@ -45,12 +47,12 @@ func (s *image) AddFromFile(path string, cell *core.Cell, prop *props.Rect) erro return errors.New("could not register image options, maybe path/name is wrong") } - s.addImageToPdf(path, info, cell, prop) + s.addImageToPdf(path, info, cell, margins, prop) return nil } // AddFromBase64 use a base64 string to add to PDF. -func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, prop *props.Rect, extension extension.Type) error { +func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, margins *config.Margins, prop *props.Rect, extension extension.Type) error { imageID, _ := uuid.NewRandom() ss, _ := base64.StdEncoding.DecodeString(stringBase64) @@ -68,17 +70,18 @@ func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, prop *props. return errors.New("could not register image options, maybe path/name is wrong") } - s.addImageToPdf(imageID.String(), info, cell, prop) + s.addImageToPdf(imageID.String(), info, cell, margins, prop) return nil } -func (s *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, cell *core.Cell, prop *props.Rect) { - var x, y, w, h float64 +func (s *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, cell *core.Cell, margins *config.Margins, prop *props.Rect) { + rectCell := &core.Cell{} + dimensions := &config.Dimensions{Width: info.Width(), Height: info.Height()} + if prop.Center { - dimensions := &config.Dimensions{Width: info.Width(), Height: info.Height()} - x, y, w, h = s.math.GetRectCenterColProperties(dimensions, cell, prop.Percent) + rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent) } else { - x, y, w, h = s.math.GetRectNonCenterColProperties(info.Width(), info.Height(), cell.Width, cell.Height, cell.X, prop) + rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, prop) } - s.pdf.Image(imageLabel, x, y+cell.Y+prop.Top, w, h, false, "", 0, "") + s.pdf.Image(imageLabel, rectCell.X, rectCell.Y+cell.Y+prop.Top, rectCell.Width, rectCell.Height, false, "", 0, "") } diff --git a/internal/math.go b/internal/math.go deleted file mode 100644 index 3eec6e33..00000000 --- a/internal/math.go +++ /dev/null @@ -1,104 +0,0 @@ -package internal - -import ( - "github.com/johnfercher/maroto/v2/internal/fpdf" - "github.com/johnfercher/maroto/v2/pkg/config" - "github.com/johnfercher/maroto/v2/pkg/core" - "github.com/johnfercher/maroto/v2/pkg/props" -) - -const ( - maxPercent = 100.0 -) - -// Math is the abstraction which deals with useful calc. -type Math interface { - GetRectCenterColProperties(dimensions *config.Dimensions, cell *core.Cell, percent float64) (x float64, y float64, w float64, h float64) - GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, - xColOffset float64, prop *props.Rect) (x float64, y float64, w float64, h float64) - GetCenterCorrection(outerSize, innerSize float64) float64 -} - -type math struct { - pdf fpdf.Fpdf -} - -// NewMath create a Math. -func NewMath(pdf fpdf.Fpdf) *math { - return &math{ - pdf, - } -} - -func (s *math) GetRectCenterColProperties(dimensions *config.Dimensions, cell *core.Cell, percent float64, -) (x float64, y float64, w float64, h float64) { - percent /= 100.0 - left, top, _, _ := s.pdf.GetMargins() - - imageProportion := dimensions.Height / dimensions.Width - celProportion := cell.Height / cell.Width - - if imageProportion > celProportion { - newImageWidth := cell.Height / imageProportion * percent - newImageHeight := newImageWidth * imageProportion - - widthCorrection := s.GetCenterCorrection(cell.Width, newImageWidth) - heightCorrection := s.GetCenterCorrection(cell.Height, newImageHeight) - - x = cell.X + left + widthCorrection - y = top + heightCorrection - w = newImageWidth - h = newImageHeight - } else { - newImageWidth := cell.Width * percent - newImageHeight := newImageWidth * imageProportion - - widthCorrection := s.GetCenterCorrection(cell.Width, newImageWidth) - heightCorrection := s.GetCenterCorrection(cell.Height, newImageHeight) - - x = cell.X + left + widthCorrection - y = top + heightCorrection - w = newImageWidth - h = newImageHeight - } - - return x, y, w, h -} - -// GetRectNonCenterColProperties define Width, Height to and rectangle (QrCode, Barcode, Image) inside a cell. -func (s *math) GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, - xColOffset float64, prop *props.Rect, -) (x float64, y float64, w float64, h float64) { - percent := prop.Percent / maxPercent - left, top, _, _ := s.pdf.GetMargins() - - imageProportion := imageHeight / imageWidth - celProportion := colHeight / colWidth - - if imageProportion > celProportion { - newImageWidth := colHeight / imageProportion * percent - newImageHeight := newImageWidth * imageProportion - - x = xColOffset + left + prop.Left - y = top - w = newImageWidth - h = newImageHeight - } else { - newImageWidth := colWidth * percent - newImageHeight := newImageWidth * imageProportion - - x = xColOffset + left + prop.Left - y = top - w = newImageWidth - h = newImageHeight - } - - return -} - -// GetCenterCorrection return the correction of space in X or Y to -// centralize a line in relation with another line. -func (s *math) GetCenterCorrection(outerSize, innerSize float64) float64 { - const divisorToGetHalf = 2.0 - return (outerSize - innerSize) / divisorToGetHalf -} diff --git a/internal/math/math.go b/internal/math/math.go new file mode 100644 index 00000000..1c136af3 --- /dev/null +++ b/internal/math/math.go @@ -0,0 +1,97 @@ +package math + +import ( + "github.com/johnfercher/maroto/v2/pkg/config" + "github.com/johnfercher/maroto/v2/pkg/core" + "github.com/johnfercher/maroto/v2/pkg/props" +) + +const ( + maxPercent = 100.0 +) + +// Math is the abstraction which deals with useful calc. +type Math interface { + GetRectCenterColProperties(rectDimensions *config.Dimensions, cell *core.Cell, margins *config.Margins, percent float64) *core.Cell + GetRectNonCenterColProperties(rectDimensions *config.Dimensions, cell *core.Cell, margins *config.Margins, prop *props.Rect) *core.Cell + GetCenterCorrection(outerSize, innerSize float64) float64 +} + +type math struct{} + +// NewMath create a Math. +func NewMath() *math { + return &math{} +} + +func (s *math) GetRectCenterColProperties(rectDimensions *config.Dimensions, cell *core.Cell, margins *config.Margins, percent float64) *core.Cell { + percent /= 100.0 + left, top := margins.Left, margins.Top + + imageProportion := rectDimensions.Height / rectDimensions.Width + celProportion := cell.Height / cell.Width + + rectCell := &core.Cell{} + if imageProportion > celProportion { + newImageWidth := cell.Height / imageProportion * percent + newImageHeight := newImageWidth * imageProportion + + widthCorrection := s.GetCenterCorrection(cell.Width, newImageWidth) + heightCorrection := s.GetCenterCorrection(cell.Height, newImageHeight) + + rectCell.X = cell.X + left + widthCorrection + rectCell.Y = top + heightCorrection + rectCell.Width = newImageWidth + rectCell.Height = newImageHeight + } else { + newImageWidth := cell.Width * percent + newImageHeight := newImageWidth * imageProportion + + widthCorrection := s.GetCenterCorrection(cell.Width, newImageWidth) + heightCorrection := s.GetCenterCorrection(cell.Height, newImageHeight) + + rectCell.X = cell.X + left + widthCorrection + rectCell.Y = top + heightCorrection + rectCell.Width = newImageWidth + rectCell.Height = newImageHeight + } + + return rectCell +} + +// GetRectNonCenterColProperties define Width, Height to and rectangle (QrCode, Barcode, Image) inside a cell. +func (s *math) GetRectNonCenterColProperties(rectDimensions *config.Dimensions, cell *core.Cell, margins *config.Margins, prop *props.Rect) *core.Cell { + percent := prop.Percent / maxPercent + left, top := margins.Left, margins.Top + + imageProportion := rectDimensions.Height / rectDimensions.Width + celProportion := cell.Height / cell.Width + + rectCell := &core.Cell{} + if imageProportion > celProportion { + newImageWidth := cell.Height / imageProportion * percent + newImageHeight := newImageWidth * imageProportion + + rectCell.X = cell.X + left + prop.Left + rectCell.Y = top + rectCell.Width = newImageWidth + rectCell.Height = newImageHeight + } else { + newImageWidth := cell.Width * percent + newImageHeight := newImageWidth * imageProportion + + rectCell.X = cell.X + left + prop.Left + rectCell.Y = top + rectCell.Width = newImageWidth + rectCell.Height = newImageHeight + } + + return rectCell +} + +// GetCenterCorrection return the correction of space in X or Y to +// centralize a line in relation with another line. +func (s *math) GetCenterCorrection(outerSize, innerSize float64) float64 { + const divisorToGetHalf = 2.0 + return (outerSize - innerSize) / divisorToGetHalf +} diff --git a/internal/math_test.go b/internal/math/math_test.go similarity index 99% rename from internal/math_test.go rename to internal/math/math_test.go index 708ba191..8a8e155a 100644 --- a/internal/math_test.go +++ b/internal/math/math_test.go @@ -1,4 +1,4 @@ -package internal_test +package math_test /*func TestNewMath(t *testing.T) { math := internal.NewMath(&mocks.Fpdf{}) diff --git a/internal/signature.go b/internal/signature.go index 86e1a19d..f66c5eca 100644 --- a/internal/signature.go +++ b/internal/signature.go @@ -2,6 +2,7 @@ package internal import ( "github.com/johnfercher/maroto/v2/internal/fpdf" + "github.com/johnfercher/maroto/v2/internal/math" "github.com/johnfercher/maroto/v2/pkg/core" "github.com/johnfercher/maroto/v2/pkg/props" ) @@ -13,12 +14,12 @@ type Signature interface { type signature struct { pdf fpdf.Fpdf - math Math + math math.Math text Text } // NewSignature create a Signature. -func NewSignature(pdf fpdf.Fpdf, math Math, text Text) *signature { +func NewSignature(pdf fpdf.Fpdf, math math.Math, text Text) *signature { return &signature{ pdf, math, diff --git a/internal/text.go b/internal/text.go index 250268cc..24ff9997 100644 --- a/internal/text.go +++ b/internal/text.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" + "github.com/johnfercher/maroto/v2/internal/math" + "github.com/johnfercher/maroto/v2/pkg/consts/breakline" "github.com/johnfercher/maroto/v2/pkg/core" @@ -23,12 +25,12 @@ type Text interface { type text struct { pdf fpdf.Fpdf - math Math + math math.Math font Font } // NewText create a Text. -func NewText(pdf fpdf.Fpdf, math Math, font Font) *text { +func NewText(pdf fpdf.Fpdf, math math.Math, font Font) *text { return &text{ pdf, math, diff --git a/pkg/config/margins.go b/pkg/config/margins.go index dcf299d1..ed3d26d3 100644 --- a/pkg/config/margins.go +++ b/pkg/config/margins.go @@ -1,7 +1,18 @@ package config +import "fmt" + type Margins struct { - Left float64 - Right float64 - Top float64 + Left float64 + Right float64 + Top float64 + Bottom float64 +} + +func (m *Margins) Print() { + if m == nil { + return + } + + fmt.Printf("Left: %1.f, Right: %1.f, Top: %1.f, Bottom: %1.f\n", m.Left, m.Right, m.Left, m.Bottom) } diff --git a/pkg/core/cell.go b/pkg/core/cell.go index 18aa5d6b..c4f979f2 100644 --- a/pkg/core/cell.go +++ b/pkg/core/cell.go @@ -1,5 +1,7 @@ package core +import "github.com/johnfercher/maroto/v2/pkg/config" + // Cell represents a cell inside the PDF. type Cell struct { X float64 @@ -17,7 +19,7 @@ func (c Cell) Copy() Cell { } } -func NewRootContext(pageWidth, pageHeight float64, margins Margins) Cell { +func NewRootContext(pageWidth, pageHeight float64, margins config.Margins) Cell { return Cell{ X: 0, Y: 0, diff --git a/pkg/core/margins.go b/pkg/core/margins.go deleted file mode 100644 index 57b1c77b..00000000 --- a/pkg/core/margins.go +++ /dev/null @@ -1,18 +0,0 @@ -package core - -import "fmt" - -type Margins struct { - Left float64 - Right float64 - Top float64 - Bottom float64 -} - -func (m *Margins) Print() { - if m == nil { - return - } - - fmt.Printf("Left: %1.f, Right: %1.f, Top: %1.f, Bottom: %1.f\n", m.Left, m.Right, m.Left, m.Bottom) -} diff --git a/pkg/maroto.go b/pkg/maroto.go index 624bcc48..92af06f5 100644 --- a/pkg/maroto.go +++ b/pkg/maroto.go @@ -46,9 +46,9 @@ type maroto struct { pool async.Processor[[]core.Page, []byte] } -func NewMaroto(config ...*config.Config) core.Maroto { +func NewMaroto(cfgs ...*config.Config) core.Maroto { cache := cache.New() - cfg := getConfig(config...) + cfg := getConfig(cfgs...) provider := getProvider(cache, cfg) width, height := provider.GetDimensions() @@ -56,7 +56,7 @@ func NewMaroto(config ...*config.Config) core.Maroto { m := &maroto{ provider: provider, - cell: core.NewRootContext(width, height, core.Margins{ + cell: core.NewRootContext(width, height, config.Margins{ Left: left, Top: top, Right: right, diff --git a/pkg/providers/gofpdf/gofpdf.go b/pkg/providers/gofpdf/gofpdf.go index 4c50bfbd..a826001d 100644 --- a/pkg/providers/gofpdf/gofpdf.go +++ b/pkg/providers/gofpdf/gofpdf.go @@ -3,6 +3,8 @@ package gofpdf import ( "bytes" + "github.com/johnfercher/maroto/v2/internal/math" + "github.com/johnfercher/maroto/v2/internal" "github.com/johnfercher/maroto/v2/pkg/cache" "github.com/johnfercher/maroto/v2/pkg/config" @@ -30,7 +32,7 @@ var defaultErrorColor = &props.Font{ type gofpdfProvider struct { fpdf *gofpdf.Fpdf - math internal.Math + math math.Math font internal.Font text internal.Text signature internal.Signature @@ -39,6 +41,7 @@ type gofpdfProvider struct { line internal.Line imageCache cache.Cache cellWriter cellwriter.CellWriter + cfg *config.Config } func New(cfg *config.Config, options ...providers.ProviderOption) core.Provider { @@ -60,7 +63,7 @@ func New(cfg *config.Config, options ...providers.ProviderOption) core.Provider fpdf.AddPage() font := internal.NewFont(fpdf, cfg.DefaultFont.Size, cfg.DefaultFont.Family, cfg.DefaultFont.Style) - math := internal.NewMath(fpdf) + math := math.NewMath() text := internal.NewText(fpdf, math, font) signature := internal.NewSignature(fpdf, math, text) code := internal.NewCode(fpdf, math) @@ -78,6 +81,7 @@ func New(cfg *config.Config, options ...providers.ProviderOption) core.Provider image: image, line: line, cellWriter: cellWriter, + cfg: cfg, } for _, option := range options { @@ -108,15 +112,15 @@ func (g *gofpdfProvider) AddSignature(text string, cell *core.Cell, prop *props. } func (g *gofpdfProvider) AddMatrixCode(code string, cell *core.Cell, prop *props.Rect) { - g.code.AddDataMatrix(code, cell, prop) + g.code.AddDataMatrix(code, cell, g.cfg.Margins, prop) } func (g *gofpdfProvider) AddQrCode(code string, cell *core.Cell, rect *props.Rect) { - g.code.AddQr(code, cell, rect) + g.code.AddQr(code, cell, g.cfg.Margins, rect) } func (g *gofpdfProvider) AddBarCode(code string, cell *core.Cell, prop *props.Barcode) { - err := g.code.AddBar(code, cell, prop) + err := g.code.AddBar(code, cell, g.cfg.Margins, prop) if err != nil { textProp := &props.Text{} textProp.MakeValid(defaultErrorColor) @@ -135,7 +139,7 @@ func (g *gofpdfProvider) AddImage(file string, cell *core.Cell, prop *props.Rect return } - err = g.image.AddFromBase64(img.Value, cell, prop, img.Extension) + err = g.image.AddFromBase64(img.Value, cell, g.cfg.Margins, prop, img.Extension) if err != nil { textProp := &props.Text{} textProp.MakeValid(defaultErrorColor)