Skip to content

Commit

Permalink
Math without margin applying value responsibility (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Sep 24, 2023
1 parent d38470c commit fe92d09
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 482 deletions.
154 changes: 11 additions & 143 deletions docs/assets/examples/list/v2/main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
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"

"github.com/johnfercher/maroto/v2/pkg/components/code"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/image"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/components/signature"
"github.com/johnfercher/maroto/v2/pkg/components/text"

"github.com/johnfercher/maroto/v2/pkg/consts/align"
"github.com/johnfercher/maroto/v2/pkg/consts/extension"

"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/props"
)

var dummyText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac condimentum sem."
"github.com/johnfercher/maroto/v2/pkg"
"github.com/johnfercher/maroto/v2/pkg/components/text"
)

var background = &props.Color{
Red: 200,
Expand All @@ -35,148 +21,30 @@ var background = &props.Color{
}

func main() {
var content string
for i := 0; i < 20; i++ {
fmt.Println(i)
content += fmt.Sprintf("%f", run().Value) + "\n"
}

err := os.WriteFile("docs/assets/text/benchmark.txt", []byte(content), os.ModePerm)
if err != nil {
log.Fatal(err.Error())
}
}

func run() *metrics.Time {
cfg := config.NewBuilder().
WithPageNumber("Page {current} of {total}", props.South).
Build()

mrt := pkg.NewMaroto(cfg)
mrt := pkg.NewMaroto()
m := pkg.NewMetricsDecorator(mrt)

err := m.RegisterHeader(buildHeader()...)
if err != nil {
log.Fatal(err.Error())
}

err = m.RegisterFooter(buildFooter()...)
if err != nil {
log.Fatal(err.Error())
}

m.AddRows(
text.NewRow(20, "Main features", props.Text{Size: 15, Top: 6.5}),
)

objects := getObjects(1158)
objects := getObjects(100)
rows, err := list.Build[Object](objects)
if err != nil {
log.Fatal(err.Error())
}

m.AddRows(rows...)

for i := 0; i < 1158; i++ {
m.AddRows(buildCodesRow()...)
m.AddRows(buildImagesRow()...)
m.AddRows(buildTextsRow()...)
}

m.AddRows(
text.NewRow(15, "Dummy Data", props.Text{Size: 12, Top: 5, Align: align.Center}),
)

for i := 0; i < 1158; i++ {
m.AddRows(text.NewRow(20, dummyText+dummyText+dummyText+dummyText+dummyText))
}

document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

return document.GetReport().TimeMetrics[0].Avg
}

func buildCodesRow() []core.Row {
return []core.Row{
row.New(20).Add(
text.NewCol(4, "Barcode:", props.Text{Size: 15, Top: 6, Align: align.Center}),
code.NewBarCol(8, "barcode", props.Barcode{Center: true, Percent: 70}),
),
row.New(20).Add(
text.NewCol(4, "QrCode:", props.Text{Size: 15, Top: 6, Align: align.Center}),
code.NewQrCol(8, "qrcode", props.Rect{Center: true, Percent: 70}),
),
row.New(20).Add(
text.NewCol(4, "MatrixCode:", props.Text{Size: 15, Top: 6, Align: align.Center}),
code.NewMatrixCol(8, "matrixcode", props.Rect{Center: true, Percent: 70}),
),
}
}

func buildImagesRow() []core.Row {
byteSlices, err := os.ReadFile("docs/assets/images/frontpage.png")
err = document.Save("docs/assets/pdf/listv2.pdf")
if err != nil {
fmt.Println("Got error while opening file:", err)
os.Exit(1)
}
stringBase64 := base64.StdEncoding.EncodeToString(byteSlices)

return []core.Row{
row.New(20).Add(
text.NewCol(4, "Image From File:", props.Text{Size: 15, Top: 6, Align: align.Center}),
image.NewFromFileCol(8, "docs/assets/images/biplane.jpg", props.Rect{Center: true, Percent: 90}),
),
row.New(20).Add(
text.NewCol(4, "Image From Base64::", props.Text{Size: 15, Top: 6, Align: align.Center}),
image.NewFromBase64Col(8, stringBase64, extension.Png, props.Rect{Center: true, Percent: 90}),
),
}
}

func buildTextsRow() []core.Row {
colText := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac condimentum sem."

return []core.Row{
row.New(20).Add(
text.NewCol(4, "Text:", props.Text{Size: 15, Top: 6, Align: align.Center}),
text.NewCol(8, colText, props.Text{Size: 12, Top: 5, Align: align.Center}),
),
row.New(40).Add(
text.NewCol(4, "Signature:", props.Text{Size: 15, Top: 17, Align: align.Center}),
signature.NewCol(8, "Name", props.Font{Size: 10}),
),
log.Fatal(err.Error())
}
}

func buildHeader() []core.Row {
r1 := row.New(30).Add(
col.New(12).Add(
text.New("Config V2", props.Text{
Top: 5,
Size: 15,
Align: align.Center,
}),
text.New("Grid system, fast generation, embedded metrics and testable.", props.Text{
Top: 13,
Size: 13,
Align: align.Center,
}),
),
)

return []core.Row{r1}
}

func buildFooter() []core.Row {
return []core.Row{
row.New(10).Add(
text.NewCol(2, "Site: https://maroto.io/"),
text.NewCol(5, "Discussions: https://github.com/johnfercher/maroto/issues/257"),
text.NewCol(5, "Branch: https://github.com/johnfercher/maroto/tree/v2"),
),
err = document.GetReport().Save("docs/assets/text/listv2.txt")
if err != nil {
log.Fatal(err.Error())
}
}

Expand Down
Binary file modified docs/assets/pdf/barcodegridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/datamatrixgridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/imagegridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/qrgridv2.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/assets/text/barcodegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 1.70ms, executions: [1.70ms]
add_cols -> avg: 193.00ns, executions: [436.00ns, 137.00ns, 125.00ns, 74.00ns]
generate -> avg: 1.66ms, executions: [1.66ms]
add_cols -> avg: 171.00ns, executions: [389.00ns, 118.00ns, 112.00ns, 65.00ns]
file_size -> 5.63Kb
4 changes: 2 additions & 2 deletions docs/assets/text/datamatrixgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 1.14ms, executions: [1.14ms]
add_cols -> avg: 208.00ns, executions: [469.00ns, 145.00ns, 136.00ns, 82.00ns]
generate -> avg: 926.69μs, executions: [926.69μs]
add_cols -> avg: 176.00ns, executions: [391.00ns, 133.00ns, 108.00ns, 72.00ns]
file_size -> 4.58Kb
4 changes: 2 additions & 2 deletions docs/assets/text/imagegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 5.46ms, executions: [5.46ms]
add_cols -> avg: 168.83ns, executions: [518.00ns, 129.00ns, 131.00ns, 68.00ns, 103.00ns, 64.00ns]
generate -> avg: 4.94ms, executions: [4.94ms]
add_cols -> avg: 150.83ns, executions: [401.00ns, 136.00ns, 123.00ns, 75.00ns, 104.00ns, 66.00ns]
file_size -> 222.72Kb
4 changes: 2 additions & 2 deletions docs/assets/text/qrgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 48.63ms, executions: [48.63ms]
add_cols -> avg: 166.25ns, executions: [366.00ns, 138.00ns, 97.00ns, 64.00ns]
generate -> avg: 18.31ms, executions: [18.31ms]
add_cols -> avg: 201.50ns, executions: [480.00ns, 151.00ns, 109.00ns, 66.00ns]
file_size -> 5.21Kb
19 changes: 10 additions & 9 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (s *code) AddDataMatrix(code string, cell *core.Cell, margins *config.Margi

var rectCell *core.Cell
if prop.Center {
rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent)
rectCell = s.math.GetInnerCenterCell(dimensions, cell.GetDimensions(), prop.Percent)
} else {
rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, prop)
rectCell = s.math.GetInnerNonCenterCell(dimensions, cell.GetDimensions(), prop)
}

barcode.Barcode(s.pdf, key, rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false)
barcode.Barcode(s.pdf, key, cell.X+rectCell.X+margins.Left, cell.Y+rectCell.Y+margins.Top, rectCell.Width, rectCell.Height, false)
}

// AddQr create a QrCode inside a cell.
Expand All @@ -53,12 +53,12 @@ func (s *code) AddQr(code string, cell *core.Cell, margins *config.Margins, prop

var rectCell *core.Cell
if prop.Center {
rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent)
rectCell = s.math.GetInnerCenterCell(dimensions, cell.GetDimensions(), prop.Percent)
} else {
rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, prop)
rectCell = s.math.GetInnerNonCenterCell(dimensions, cell.GetDimensions(), prop)
}

barcode.Barcode(s.pdf, key, rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false)
barcode.Barcode(s.pdf, key, cell.X+rectCell.X+margins.Left, cell.Y+rectCell.Y+margins.Top, rectCell.Width, rectCell.Height, false)
}

// AddBar create a Barcode inside a cell.
Expand All @@ -73,12 +73,13 @@ func (s *code) AddBar(code string, cell *core.Cell, margins *config.Margins, pro

var rectCell *core.Cell
if prop.Center {
rectCell = s.math.GetRectCenterColProperties(dimensions, cell, margins, prop.Percent)
rectCell = s.math.GetInnerCenterCell(dimensions, cell.GetDimensions(), prop.Percent)
} else {
rectProps := &props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
rectCell = s.math.GetRectNonCenterColProperties(dimensions, cell, margins, rectProps)
rectCell = s.math.GetInnerNonCenterCell(dimensions, cell.GetDimensions(), rectProps)
}

barcode.Barcode(s.pdf, barcode.Register(bcode), rectCell.X, rectCell.Y+cell.Y, rectCell.Width, rectCell.Height, false)
barcode.Barcode(s.pdf, barcode.Register(bcode), cell.X+rectCell.X+margins.Left,
cell.Y+rectCell.Y+margins.Top, rectCell.Width, rectCell.Height, false)
return
}
Loading

0 comments on commit fe92d09

Please sign in to comment.