Skip to content

Commit

Permalink
Refactor math, cell and props (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Sep 24, 2023
1 parent f925b6e commit d38470c
Show file tree
Hide file tree
Showing 36 changed files with 374 additions and 275 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ always when a new page appear, in this case, a header may have many rows, lines
* With `go get`:

```bash
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.23
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.24
```


Expand Down
7 changes: 4 additions & 3 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 All @@ -35,7 +36,7 @@ var background = &props.Color{

func main() {
var content string
for i := 0; i < 20; i++ {
for i := 0; i < 100; i++ {
fmt.Println(i)
content += fmt.Sprintf("%f", run().Value) + "\n"
}
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
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#template-engines) [![Branch](https://img.shields.io/badge/V2-Branch-pink)](https://github.com/johnfercher/maroto/tree/v2) [![Roadmap](https://img.shields.io/badge/V2-Roadmap-purple)](https://github.com/users/johnfercher/projects/1) [![Discussion](https://img.shields.io/badge/V2-Discussion-blue)](https://github.com/johnfercher/maroto/issues/257) [![Release Notes](https://img.shields.io/badge/Release-Notes-cyan)](https://github.com/johnfercher/maroto/releases) [![Visits Badge](https://badges.pufler.dev/visits/johnfercher/maroto)](https://badges.pufler.dev)

#### Maroto`v2.0.0-alpha.23`is here! Try out:
#### Maroto`v2.0.0-alpha.24`is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.23
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.24
```

The public API was completely redesigned with the aim of enhancing the
Expand Down
5 changes: 3 additions & 2 deletions docs/assets/examples/list/v2/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
Binary file modified docs/assets/pdf/v2.pdf
Binary file not shown.
120 changes: 100 additions & 20 deletions docs/assets/text/benchmark.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,100 @@
5713.058155
5604.562240
6185.930860
5831.065336
5913.167839
6053.554586
6034.108635
5584.093034
5581.348188
5893.622080
5911.445907
5950.391652
6102.767394
5885.246285
5898.916533
6214.413742
5619.153454
5933.690091
5934.488651
5956.515246
5317.953807
5315.037820
5164.163855
5198.180831
5237.379406
5322.056371
5338.671536
4837.652953
4720.954979
5119.040083
5298.798362
5120.403483
5227.477424
5162.407316
5447.440712
5467.580919
5313.827204
5335.735310
5205.362338
5374.806570
5173.248838
5369.851606
5331.566468
5124.031579
5106.242516
5218.097750
5295.609238
5285.710100
5253.334516
5247.828504
5310.875216
5255.361052
5148.246356
5454.810704
5347.372197
5288.712899
5238.118029
5236.236376
5369.347216
5588.524662
5389.948522
5275.753759
4939.716533
5206.020321
5160.954472
5558.478398
5316.437244
5320.318437
5359.820991
5295.662023
5211.191388
5445.538520
5403.318623
5421.800879
5639.864653
5325.896143
5390.202025
5348.761704
5078.703688
5599.018133
5062.741334
5424.028018
5559.508133
5448.089469
5201.023734
5565.419011
5226.516382
5235.296242
5264.377833
5279.394461
5490.793322
5277.064203
5496.977589
5363.291172
5444.443503
5204.142776
5427.393976
5077.196057
5508.293089
5312.960908
5560.476656
5101.234052
5150.452483
5429.566926
5391.574596
5246.817435
5511.685803
5327.040004
5144.837634
5395.723487
5452.607187
5413.555648
5233.978467
5204.215993
5277.073030
5292.669027
5375.699958
5297.210985
5015.364772
5299.180017
9 changes: 5 additions & 4 deletions docs/assets/text/v2.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
generate -> avg: 21.90ms, executions: [21.90ms]
header -> avg: 394.00ns, executions: [394.00ns]
footer -> avg: 61.00ns, executions: [61.00ns]
add_row -> avg: 136.89ns, executions: [0.16μs, 0.15μs, 0.06μs, 0.10μs, 0.03μs, 0.03μs, 3.85μs, 0.08μs, 0.03μs, 0.74μs, 0.03μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.03μs, 0.36μs, 0.05μs, 0.01μs, 0.05μs, 0.02μs, 0.02μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.01μs, 0.24μs, 0.05μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.02μs, 0.20μs, 0.05μs, 0.01μs, 0.05μs, 0.02μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.02μs, 0.23μs, 0.07μs, 0.02μs, 0.12μs, 0.02μs]
generate -> avg: 18.58ms, executions: [18.58ms]
header -> avg: 522.00ns, executions: [522.00ns]
footer -> avg: 64.00ns, executions: [64.00ns]
add_row -> avg: 64.78ns, executions: [121.00ns, 146.00ns, 52.00ns, 107.00ns, 22.00ns, 18.00ns, 683.00ns, 73.00ns, 23.00ns, 66.00ns, 24.00ns, 16.00ns, 15.00ns, 54.00ns, 17.00ns, 16.00ns, 17.00ns, 345.00ns, 50.00ns, 16.00ns, 57.00ns, 15.00ns, 16.00ns, 22.00ns, 64.00ns, 16.00ns, 17.00ns, 17.00ns, 283.00ns, 49.00ns, 15.00ns, 58.00ns, 17.00ns, 19.00ns, 17.00ns, 60.00ns, 16.00ns, 19.00ns, 17.00ns, 209.00ns, 52.00ns, 17.00ns, 54.00ns, 18.00ns, 15.00ns, 15.00ns, 54.00ns, 16.00ns, 30.00ns, 18.00ns, 246.00ns, 52.00ns, 16.00ns, 60.00ns, 16.00ns]
file_size -> 260.96Kb
51 changes: 28 additions & 23 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,81 @@ 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"
"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, 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 {
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, 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 {
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, 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{Width: cell.Width, Height: cell.Width * heightPercentFromWidth}

var rectCell *core.Cell
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width*heightPercentFromWidth, cell.Width,
cell.Height, cell.X, 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)
rectProps := &props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
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
}
33 changes: 20 additions & 13 deletions internal/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/base64"
"errors"

"github.com/johnfercher/maroto/v2/internal/math"
"github.com/johnfercher/maroto/v2/pkg/config"

"github.com/johnfercher/maroto/v2/pkg/core"

"github.com/google/uuid"
Expand All @@ -16,25 +19,25 @@ 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,
}
}

// 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: "",
Expand All @@ -44,12 +47,14 @@ func (s *image) AddFromFile(path string, cell core.Cell, prop props.Rect) error
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)
Expand All @@ -67,16 +72,18 @@ func (s *image) AddFromBase64(stringBase64 string, cell core.Cell, prop props.Re
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 {
x, y, w, h = s.math.GetRectCenterColProperties(info.Width(), info.Height(), cell.Width, cell.Height, cell.X, 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, "")
}
Loading

0 comments on commit d38470c

Please sign in to comment.