Skip to content

Commit

Permalink
feature/Automatic row height calculation (#464)
Browse files Browse the repository at this point in the history
* feat/Provides method for calculating lines required for a text

Refactor the GetLinesQuantity method so that it supports all Maroto line wrapping strategies
Provide a method to count the number of lines occupied by a text so that it is possible
to calculate the height that the text occupies

* feat/Control resizing reference through parameter

Add a parameter to control whether internal resizing will reference only
the available width or also the height. This will allow inner to use all
available height
Refactor math so that resizing is done in a separate method

* feat/Provide method to query image dimensions

Add method that loads the image into the PDF and returns its dimensions,
this is necessary so that it is possible to calculate the height needed
to add the image to the height

* feat/Configure Maroto to calculate row height

Configure Marado to calculate the height of the line when it is added
Configure getHeight on row to save and return the height of your largest column
Configure getHeight in col to return the height of the largest component

* feat/Provide necessary height for rendering codes

Implement methods to calculate the height required for codes

* feat/Provide the height required to render an image

* feat/Provide the height required to render a line

* feat/Provide the height required to render a signature

* feat/Provide the height required to render text

* fix/Reset height only if row has automatic height

Validate whether the row was created with automatic height before the row height remains.
Reset row height whenever a new column is added

* test/Update mocks

* feat/Add method to generate line with automatic height

* test/ Update tests

Add text details to the structure file used by test

* test/Update mocks

* docs/Update examples

* fix/Consider top margin when calculating line height

* test/update testing framework

Update test framework to validate updates to example pdfs

* style/Make corrections highlighted by the lint
  • Loading branch information
Fernando-hub527 authored Aug 1, 2024
1 parent 6408f46 commit b02da4b
Show file tree
Hide file tree
Showing 123 changed files with 8,125 additions and 1,273 deletions.
15 changes: 15 additions & 0 deletions docs/assets/examples/barcodegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,20 @@ func GetMaroto() core.Maroto {
}),
)

m.AddAutoRow(
code.NewBarCol(2, "123456789123", props.Barcode{
Center: true,
Type: barcode.EAN,
}),
code.NewBarCol(4, "123456789123", props.Barcode{
Center: true,
Type: barcode.EAN,
}),
code.NewBarCol(6, "123456789123", props.Barcode{
Center: true,
Type: barcode.EAN,
}),
)

return m
}
17 changes: 17 additions & 0 deletions docs/assets/examples/datamatrixgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,22 @@ func GetMaroto() core.Maroto {
}),
)

m.AddAutoRow(
code.NewMatrixCol(6, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 20,
JustReferenceWidth: true,
}),
code.NewMatrixCol(4, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 75,
JustReferenceWidth: true,
}),
code.NewMatrixCol(2, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 100,
JustReferenceWidth: true,
}),
)
return m
}
33 changes: 33 additions & 0 deletions docs/assets/examples/imagegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ func GetMaroto() core.Maroto {
Percent: 80,
}),
)
m.AddAutoRow(
image.NewFromFileCol(4, "docs/assets/images/frontpage.png", props.Rect{
Center: true,
Percent: 20,
JustReferenceWidth: true,
}),
image.NewFromFileCol(8, "docs/assets/images/frontpage.png", props.Rect{
Center: true,
Percent: 30,
JustReferenceWidth: true,
}),
)

m.AddAutoRow(
image.NewFromFileCol(2, "docs/assets/images/biplane.jpg", props.Rect{
Center: false,
Percent: 50,
Left: 10,
JustReferenceWidth: true,
}),
image.NewFromFileCol(4, "docs/assets/images/biplane.jpg", props.Rect{
Center: false,
Percent: 50,
Top: 10,
JustReferenceWidth: true,
}),
image.NewFromFileCol(6, "docs/assets/images/biplane.jpg", props.Rect{
Center: false,
Percent: 50,
Left: 15,
Top: 15,
JustReferenceWidth: true,
}),
)
return m
}
5 changes: 5 additions & 0 deletions docs/assets/examples/line/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ func GetMaroto() core.Maroto {
line.NewCol(6, props.Line{Color: &props.RedColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Horizontal, OffsetPercent: 40, SizePercent: 40}),
)

m.AddAutoRow(
line.NewCol(2, props.Line{Style: linestyle.Dashed}),
line.NewCol(4, props.Line{Color: &props.RedColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Vertical, OffsetPercent: 70, SizePercent: 70}),
line.NewCol(6, props.Line{Color: &props.RedColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Horizontal, OffsetPercent: 40, SizePercent: 40}),
)
return m
}
17 changes: 17 additions & 0 deletions docs/assets/examples/qrgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,22 @@ func GetMaroto() core.Maroto {
}),
)

m.AddAutoRow(
code.NewQrCol(6, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 30,
JustReferenceWidth: true,
}),
code.NewQrCol(4, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 75,
JustReferenceWidth: true,
}),
code.NewQrCol(2, "https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 100,
JustReferenceWidth: true,
}),
)
return m
}
6 changes: 6 additions & 0 deletions docs/assets/examples/signaturegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ func GetMaroto() core.Maroto {
signature.NewCol(4, "Signature 9", props.Signature{LineThickness: 0.5}),
)

m.AddAutoRow(
signature.NewCol(4, "Signature 7", props.Signature{LineColor: &props.RedColor}),
signature.NewCol(4, "Signature 8", props.Signature{LineStyle: linestyle.Dashed}),
signature.NewCol(4, "Signature 9", props.Signature{LineThickness: 0.5}),
)

return m
}
9 changes: 9 additions & 0 deletions docs/assets/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,14 @@ func GetMaroto() core.Maroto {
)
m.AddRows(text.NewRow(10, "Justify-aligned text", props.Text{Align: align.Justify}))

m.AddAutoRow(
text.NewCol(2, longText, props.Text{Top: 0, Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.DashStrategy}),
text.NewCol(4, longText+" "+longText, props.Text{Top: 0, Left: 3, Right: 3, Align: align.Justify}),
text.NewCol(6, longText+" "+longText+" "+longText, props.Text{Hyperlink: &google, Top: 0, Left: 10, Right: 10, Align: align.Justify}),
)

m.AddAutoRow(
text.NewCol(12, longText+" "+longText+" "+longText, props.Text{Top: 15, Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.EmptySpaceStrategy}),
)
return m
}
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/linegridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/qrgridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/signaturegridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/textgridv2.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/assets/text/barcodegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 8.15ms, executions: [8.15ms]
add_row -> avg: 895.60ns, executions: [2.03μs, 0.62μs, 0.69μs, 0.44μs, 0.69μs]
file_size -> 16.01Kb
generate -> avg: 6.84ms, executions: [6.84ms]
add_row -> avg: 1122.20ns, executions: [2.65μs, 0.81μs, 0.80μs, 0.53μs, 0.82μs]
file_size -> 16.54Kb
6 changes: 3 additions & 3 deletions docs/assets/text/datamatrixgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 2.42ms, executions: [2.42ms]
add_row -> avg: 1197.50ns, executions: [2.60μs, 0.78μs, 0.77μs, 0.63μs]
file_size -> 4.58Kb
generate -> avg: 535.83μs, executions: [535.83μs]
add_row -> avg: 819.00ns, executions: [1.95μs, 0.51μs, 0.49μs, 0.32μs]
file_size -> 5.27Kb
6 changes: 3 additions & 3 deletions docs/assets/text/imagegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 11.10ms, executions: [11.10ms]
add_row -> avg: 606.00ns, executions: [1.52μs, 0.48μs, 0.49μs, 0.31μs, 0.54μs, 0.29μs]
file_size -> 222.72Kb
generate -> avg: 15.02ms, executions: [15.02ms]
add_row -> avg: 938.83ns, executions: [2.50μs, 0.75μs, 0.77μs, 0.47μs, 0.71μs, 0.42μs]
file_size -> 224.21Kb
6 changes: 3 additions & 3 deletions docs/assets/text/linegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 948.67μs, executions: [948.67μs]
add_row -> avg: 845.40ns, executions: [1.72μs, 0.71μs, 0.60μs, 0.46μs, 0.73μs]
file_size -> 2.48Kb
generate -> avg: 827.33μs, executions: [827.33μs]
add_row -> avg: 887.00ns, executions: [2.02μs, 0.65μs, 0.66μs, 0.43μs, 0.67μs]
file_size -> 2.89Kb
6 changes: 3 additions & 3 deletions docs/assets/text/qrgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 6.85ms, executions: [6.85ms]
add_row -> avg: 881.50ns, executions: [1.91μs, 0.62μs, 0.58μs, 0.41μs]
file_size -> 4.65Kb
generate -> avg: 432.17μs, executions: [432.17μs]
add_row -> avg: 1224.75ns, executions: [2.75μs, 0.89μs, 0.77μs, 0.48μs]
file_size -> 5.34Kb
6 changes: 3 additions & 3 deletions docs/assets/text/signaturegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 8.37ms, executions: [8.37ms]
add_row -> avg: 665.00ns, executions: [1.13μs, 0.43μs, 0.43μs]
file_size -> 4.99Kb
generate -> avg: 13.45ms, executions: [13.45ms]
add_row -> avg: 1295.67ns, executions: [2.56μs, 0.72μs, 0.60μs]
file_size -> 6.27Kb
8 changes: 4 additions & 4 deletions docs/assets/text/textgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
generate -> avg: 16.01ms, executions: [16.01ms]
add_row -> avg: 755.00ns, executions: [1.49μs, 0.51μs, 0.37μs, 0.35μs, 0.33μs, 1.49μs]
add_rows -> avg: 261.67ns, executions: [323.00ns, 322.00ns, 143.00ns, 364.00ns, 144.00ns, 274.00ns]
file_size -> 16.45Kb
generate -> avg: 35.46ms, executions: [35.46ms]
add_row -> avg: 1743.00ns, executions: [4.08μs, 1.14μs, 0.74μs, 0.68μs, 0.70μs, 3.12μs]
add_rows -> avg: 485.83ns, executions: [641.00ns, 561.00ns, 280.00ns, 621.00ns, 211.00ns, 601.00ns]
file_size -> 28.15Kb
75 changes: 26 additions & 49 deletions internal/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package math

import (
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/props"
)

type math struct{}
Expand All @@ -12,68 +11,46 @@ func New() *math {
return &math{}
}

// GetInnerCenterCell define a inner cell formatted inside outer cell centered.
func (s *math) GetInnerCenterCell(inner *entity.Dimensions, outer *entity.Dimensions, percent float64) *entity.Cell {
// Resize adjusts the internal dimension of an element to occupy a percentage of the available space
// - inner: The inner dimensions of the element
// - outer: The outer dimensions of the element
// - percent: The percentage of the external dimension that can be occupied
// - justReferenceWidth: Indicates whether resizing should be done only in relation to width or in relation to width and height
func (s *math) Resize(inner *entity.Dimensions, outer *entity.Dimensions, percent float64, justReferenceWidth bool) *entity.Dimensions {
percent /= 100.0

innerProportion := inner.Height / inner.Width
outerProportion := outer.Height / outer.Width

innerCell := &entity.Cell{}
if innerProportion > outerProportion {
newInnerWidth := outer.Height / innerProportion * percent
newInnerHeight := newInnerWidth * innerProportion

widthCorrection := s.GetCenterCorrection(outer.Width, newInnerWidth)
heightCorrection := s.GetCenterCorrection(outer.Height, newInnerHeight)
newInnerWidth := 0.0

innerCell.X = widthCorrection
innerCell.Y = heightCorrection
innerCell.Width = newInnerWidth
innerCell.Height = newInnerHeight
if innerProportion > outerProportion && !justReferenceWidth {
newInnerWidth = outer.Height / innerProportion * percent
} else {
newInnerWidth := outer.Width * percent
newInnerHeight := newInnerWidth * innerProportion
newInnerWidth = outer.Width * percent
}

widthCorrection := s.GetCenterCorrection(outer.Width, newInnerWidth)
heightCorrection := s.GetCenterCorrection(outer.Height, newInnerHeight)
newInnerHeight := newInnerWidth * innerProportion

innerCell.X = widthCorrection
innerCell.Y = heightCorrection
innerCell.Width = newInnerWidth
innerCell.Height = newInnerHeight
if justReferenceWidth && newInnerHeight > outer.Height {
newInnerWidth = outer.Height / innerProportion * 1
newInnerHeight = newInnerWidth * innerProportion
}

return innerCell
return &entity.Dimensions{Width: newInnerWidth, Height: newInnerHeight}
}

// GetInnerNonCenterCell define a inner cell formatted inside outer cell non centered.
func (s *math) GetInnerNonCenterCell(inner *entity.Dimensions, outer *entity.Dimensions, prop *props.Rect) *entity.Cell {
percent := prop.Percent / 100.0

innerProportion := inner.Height / inner.Width
outerProportion := outer.Height / outer.Width

innerCell := &entity.Cell{}
if innerProportion > outerProportion {
newInnerWidth := outer.Height / innerProportion * percent
newInnerHeight := newInnerWidth * innerProportion

innerCell.X = prop.Left
innerCell.Y = prop.Top
innerCell.Width = newInnerWidth
innerCell.Height = newInnerHeight
} else {
newInnerWidth := outer.Width * percent
newInnerHeight := newInnerWidth * innerProportion

innerCell.X = prop.Left
innerCell.Y = prop.Top
innerCell.Width = newInnerWidth
innerCell.Height = newInnerHeight
// GetInnerCenterCell define a inner cell formatted inside outer cell centered.
func (s *math) GetInnerCenterCell(inner *entity.Dimensions, outer *entity.Dimensions) *entity.Cell {
widthCorrection := s.GetCenterCorrection(outer.Width, inner.Width)
heightCorrection := s.GetCenterCorrection(outer.Height, inner.Height)

return &entity.Cell{
X: widthCorrection,
Y: heightCorrection,
Width: inner.Width,
Height: inner.Height,
}

return innerCell
}

// GetCenterCorrection return the correction of space in X or Y to
Expand Down
Loading

0 comments on commit b02da4b

Please sign in to comment.