Skip to content

Commit

Permalink
Remove gofpdf dependency from math
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher committed Sep 24, 2023
1 parent e6c33bc commit eb06728
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 285 deletions.
5 changes: 3 additions & 2 deletions cmd/benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion cmd/seriesanalyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file modified docs/assets/pdf/v2.pdf
Binary file not shown.
200 changes: 100 additions & 100 deletions docs/assets/text/benchmark.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions docs/assets/text/v2.txt
Original file line number Diff line number Diff line change
@@ -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
50 changes: 26 additions & 24 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,71 +13,72 @@ 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,
}
}

// 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
}
Loading

0 comments on commit eb06728

Please sign in to comment.