Skip to content

Commit

Permalink
Fix/vertical padding (#471)
Browse files Browse the repository at this point in the history
* style:Update method name to represent its operation

* test:Consider verticalPadding in row height

* fix:Consider verticalPadding in row height

* style: update test description

* docs:Add example of usage of verticalPadding

* test:Update the expected structure of the sample pdf
  • Loading branch information
Fernando-hub527 committed Sep 1, 2024
1 parent 8a057a1 commit 0ba580b
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 60 deletions.
4 changes: 4 additions & 0 deletions docs/assets/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,9 @@ func GetMaroto() core.Maroto {
m.AddAutoRow(
text.NewCol(12, longText+" "+longText+" "+longText, props.Text{Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.EmptySpaceStrategy}),
)

m.AddAutoRow(
text.NewCol(12, longText+" "+longText+" "+longText, props.Text{VerticalPadding: 10, Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.EmptySpaceStrategy}),
)
return m
}
Binary file modified docs/assets/pdf/textgridv2.pdf
Binary file not shown.
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: 4.97ms, executions: [4.97ms]
add_row -> avg: 325.33ns, executions: [946.00ns, 211.00ns, 103.00ns, 91.00ns, 92.00ns, 509.00ns]
add_rows -> avg: 76.50ns, executions: [127.00ns, 85.00ns, 41.00ns, 87.00ns, 36.00ns, 83.00ns]
file_size -> 28.15Kb
generate -> avg: 36.66ms, executions: [36.66ms]
add_row -> avg: 1401.00ns, executions: [3.05μs, 0.88μs, 0.61μs, 0.58μs, 0.66μs, 2.62μs]
add_rows -> avg: 368.83ns, executions: [511.00ns, 430.00ns, 190.00ns, 441.00ns, 130.00ns, 511.00ns]
file_size -> 30.68Kb
2 changes: 1 addition & 1 deletion internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (g *provider) GetLinesQuantity(text string, textProp *props.Text, colWidth
return g.text.GetLinesQuantity(text, textProp, colWidth)
}

func (g *provider) GetTextHeight(prop *props.Font) float64 {
func (g *provider) GetFontHeight(prop *props.Font) float64 {
return g.font.GetHeight(prop.Family, prop.Style, prop.Size)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/providers/gofpdf/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestProvider_GetTextHeight(t *testing.T) {
sut := gofpdf.New(dep)

// Act
fontHeight := sut.GetTextHeight(&prop)
fontHeight := sut.GetFontHeight(&prop)

// Assert
font.AssertNumberOfCalls(t, "GetHeight", 1)
Expand Down
76 changes: 38 additions & 38 deletions mocks/Provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/components/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewAutoRow(value string, ps ...props.Signature) core.Row {

// Render renders a Signature into a PDF context.
func (s *Signature) Render(provider core.Provider, cell *entity.Cell) {
fontSize := provider.GetTextHeight(s.prop.ToFontProp()) * s.prop.SafePadding
fontSize := provider.GetFontHeight(s.prop.ToFontProp()) * s.prop.SafePadding

textProp := s.prop.ToTextProp(align.Center, cell.Height-fontSize, 0)

Expand All @@ -78,7 +78,7 @@ func (s *Signature) GetStructure() *node.Node[core.Structure] {

// GetHeight returns the height that the signature will have in the PDF
func (s *Signature) GetHeight(provider core.Provider, cell *entity.Cell) float64 {
return s.prop.LineThickness + provider.GetTextHeight(s.prop.ToFontProp())*s.prop.SafePadding
return s.prop.LineThickness + provider.GetFontHeight(s.prop.ToFontProp())*s.prop.SafePadding
}

// SetConfig sets the config.
Expand Down
6 changes: 3 additions & 3 deletions pkg/components/signature/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func TestSignature_Render(t *testing.T) {

provider := mocks.NewProvider(t)
provider.On("AddText", mock.Anything, mock.Anything, mock.Anything).Return(10.0)
provider.On("GetTextHeight", mock.Anything).Return(10.0)
provider.On("GetFontHeight", mock.Anything).Return(10.0)
provider.On("AddLine", mock.Anything, mock.Anything)

// Act
sut.Render(provider, &cell)

// Assert
provider.AssertNumberOfCalls(t, "AddText", 1)
provider.AssertNumberOfCalls(t, "GetTextHeight", 1)
provider.AssertNumberOfCalls(t, "GetFontHeight", 1)
provider.AssertNumberOfCalls(t, "AddLine", 1)
})
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestSignature_GetHeight(t *testing.T) {
})

provider := mocks.NewProvider(t)
provider.EXPECT().GetTextHeight(&font).Return(5.0)
provider.EXPECT().GetFontHeight(&font).Return(5.0)

// Act
height := sut.GetHeight(provider, &cell)
Expand Down
7 changes: 4 additions & 3 deletions pkg/components/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func (t *Text) GetStructure() *node.Node[core.Structure] {

// GetHeight returns the height that the text will have in the PDF
func (t *Text) GetHeight(provider core.Provider, cell *entity.Cell) float64 {
lines := provider.GetLinesQuantity(t.value, &t.prop, cell.Width-t.prop.Left-t.prop.Right)
height := provider.GetTextHeight(&props.Font{Family: t.prop.Family, Style: t.prop.Style, Size: t.prop.Size, Color: t.prop.Color})
return (float64(lines) * height) + t.prop.Top
amountLines := provider.GetLinesQuantity(t.value, &t.prop, cell.Width-t.prop.Left-t.prop.Right)
fontHeight := provider.GetFontHeight(&props.Font{Family: t.prop.Family, Style: t.prop.Style, Size: t.prop.Size, Color: t.prop.Color})
textHeight := float64(amountLines)*fontHeight + float64(amountLines-1)*t.prop.VerticalPadding
return textHeight + t.prop.Top
}

// SetConfig sets the config.
Expand Down
47 changes: 41 additions & 6 deletions pkg/components/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,54 @@ func TestText_SetConfig(t *testing.T) {
}

func TestText_GetHeight(t *testing.T) {
t.Run("When text has a height of 22, should return 22", func(t *testing.T) {
t.Run("When top margin is sent, should increment row height with top margin", func(t *testing.T) {
cell := fixture.CellEntity()
textProp := fixture.TextProp()
font := props.Font{Family: textProp.Family, Style: textProp.Style, Size: textProp.Size, Color: textProp.Color}
font := fixture.FontProp()
textProp := props.Text{Top: 10}
textProp.MakeValid(&font)

sut := text.New("text", textProp)

provider := mocks.NewProvider(t)
provider.EXPECT().GetLinesQuantity("text", &textProp, 97.0).Return(5.0)
provider.EXPECT().GetTextHeight(&font).Return(2.0)
provider.EXPECT().GetLinesQuantity("text", &textProp, 100.0).Return(5.0)
provider.EXPECT().GetFontHeight(&font).Return(2.0)

// Act
height := sut.GetHeight(provider, &cell)
assert.Equal(t, 22.0, height)
assert.Equal(t, 20.0, height)
})

t.Run("When vertical padding is sent, should increment row height with vertical padding", func(t *testing.T) {
cell := fixture.CellEntity()
font := fixture.FontProp()
textProp := props.Text{VerticalPadding: 5}
textProp.MakeValid(&font)

sut := text.New("text", textProp)

provider := mocks.NewProvider(t)
provider.EXPECT().GetLinesQuantity("text", &textProp, 100.0).Return(5.0)
provider.EXPECT().GetFontHeight(&font).Return(2.0)

// Act
height := sut.GetHeight(provider, &cell)
assert.Equal(t, 30.0, height)
})

t.Run("When font has a height of 2, should return 10", func(t *testing.T) {
cell := fixture.CellEntity()
font := fixture.FontProp()
textProp := props.Text{}
textProp.MakeValid(&font)

sut := text.New("text", textProp)

provider := mocks.NewProvider(t)
provider.EXPECT().GetLinesQuantity("text", &textProp, 100.0).Return(5.0)
provider.EXPECT().GetFontHeight(&font).Return(2.0)

// Act
height := sut.GetHeight(provider, &cell)
assert.Equal(t, 10.0, height)
})
}
2 changes: 1 addition & 1 deletion pkg/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Provider interface {
// Features
AddLine(cell *entity.Cell, prop *props.Line)
AddText(text string, cell *entity.Cell, prop *props.Text)
GetTextHeight(prop *props.Font) float64
GetFontHeight(prop *props.Font) float64
GetLinesQuantity(text string, textProp *props.Text, colWidth float64) int
AddMatrixCode(code string, cell *entity.Cell, prop *props.Rect)
AddQrCode(code string, cell *entity.Cell, rect *props.Rect)
Expand Down
28 changes: 27 additions & 1 deletion test/maroto/examples/textgrid.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,33 @@
]
},
{
"value": 169.6641666666667,
"value": 30.583333333333336,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col",
"nodes": [
{
"value": "This is a longer sentence that will be broken into multiple lines as it does not fit into the column otherwise. This is a longer sentence that will be broken into multiple lines as it does not fit into the column otherwise. This is a longer sentence that will be broken into multiple lines as it does not fit into the column otherwise.",
"type": "text",
"details": {
"prop_align": "J",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10,
"prop_left": 3,
"prop_right": 3,
"prop_vertical_padding": 10
}
}
]
}
]
},
{
"value": 139.08083333333335,
"type": "row",
"nodes": [
{
Expand Down

0 comments on commit 0ba580b

Please sign in to comment.