Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageNumber support margin top/left #502

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/assets/examples/pagenumber/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func main() {

func GetMaroto() core.Maroto {
pageNumber := props.PageNumber{
Pattern: "Page {current} of {total}",
Place: props.Bottom,
Family: fontfamily.Courier,
Style: fontstyle.Bold,
Size: 9,
Pattern: "Page {current} of {total}",
Place: props.Bottom,
Family: fontfamily.Courier,
Style: fontstyle.Bold,
MarginTop: 26,
MarginLeft: 188,
Size: 9,
Color: &props.Color{
Red: 255,
},
Expand Down
Binary file modified docs/assets/pdf/pagenumberv2.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/assets/text/pagenumberv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 4.08ms, executions: [4.08ms]
add_rows -> avg: 167.53ns, executions: [920.00ns, 141.00ns, 91.00ns, 56.00ns, 132.00ns, 44.00ns, 36.00ns, 35.00ns, 121.00ns, 36.00ns, 35.00ns, 35.00ns, 34.00ns, 724.00ns, 73.00ns]
file_size -> 5.72Kb
generate -> avg: 12.32ms, executions: [12.32ms]
add_rows -> avg: 649.87ns, executions: [2.58μs, 0.41μs, 0.39μs, 0.19μs, 0.45μs, 0.15μs, 0.15μs, 0.15μs, 0.44μs, 0.16μs, 0.15μs, 0.16μs, 0.15μs, 3.75μs, 0.46μs]
file_size -> 5.71Kb
4 changes: 2 additions & 2 deletions internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(dep *Dependencies) core.Provider {
}
}

func (g *provider) AddText(text string, cell *entity.Cell, prop *props.Text) {
g.text.Add(text, cell, prop)
func (g *provider) AddText(text string, cell *entity.Cell, prop *props.Text, usePageMargin ...bool) {
g.text.Add(text, cell, prop, usePageMargin...)
}

func (g *provider) GetLinesQuantity(text string, textProp *props.Text, colWidth float64) int {
Expand Down
97 changes: 54 additions & 43 deletions internal/providers/gofpdf/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
}
}

// Add a text inside a cell.
func (s *text) Add(text string, cell *entity.Cell, textProp *props.Text) {
s.font.SetFont(textProp.Family, textProp.Style, textProp.Size)
fontHeight := s.font.GetHeight(textProp.Family, textProp.Style, textProp.Size)

// validates whether the text position respects the cell limits
func validTextPosition(textProp *props.Text, cell *entity.Cell) {
if textProp.Top > cell.Height {
textProp.Top = cell.Height
}
Expand All @@ -45,6 +42,20 @@
if textProp.Right > cell.Width {
textProp.Right = cell.Width
}
}

// Add a text inside a cell.
// When usePageMargin is false, text can be positioned at any position in the cell
func (t *text) Add(text string, cell *entity.Cell, textProp *props.Text, usePageMargin ...bool) {
if len(usePageMargin) > 0 && !usePageMargin[0] {
left, top, right, _ := t.pdf.GetMargins()
defer t.pdf.SetMargins(left, top, right)
t.pdf.SetMargins(0.0, 0.0, 0.0)
}
t.font.SetFont(textProp.Family, textProp.Style, textProp.Size)
fontHeight := t.font.GetHeight(textProp.Family, textProp.Style, textProp.Size)

validTextPosition(textProp, cell)

width := cell.Width - textProp.Left - textProp.Right
if width < 0 {
Expand All @@ -54,27 +65,27 @@
x := cell.X + textProp.Left
y := cell.Y + textProp.Top

originalColor := s.font.GetColor()
originalColor := t.font.GetColor()
if textProp.Color != nil {
s.font.SetColor(textProp.Color)
t.font.SetColor(textProp.Color)

Check warning on line 70 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L70

Added line #L70 was not covered by tests
}

// override style if hyperlink is set
if textProp.Hyperlink != nil {
s.font.SetColor(&props.BlueColor)
t.font.SetColor(&props.BlueColor)

Check warning on line 75 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L75

Added line #L75 was not covered by tests
}

y += fontHeight

// Apply Unicode before calc spaces
unicodeText := s.textToUnicode(text, textProp)
stringWidth := s.pdf.GetStringWidth(unicodeText)
unicodeText := t.textToUnicode(text, textProp)
stringWidth := t.pdf.GetStringWidth(unicodeText)

// If should add one line
if stringWidth < width {
s.addLine(textProp, x, width, y, stringWidth, unicodeText)
t.addLine(textProp, x, width, y, stringWidth, unicodeText)
if textProp.Color != nil {
s.font.SetColor(originalColor)
t.font.SetColor(originalColor)

Check warning on line 88 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L88

Added line #L88 was not covered by tests
}
return
}
Expand All @@ -83,66 +94,66 @@

if textProp.BreakLineStrategy == breakline.EmptySpaceStrategy {
words := strings.Split(unicodeText, " ")
lines = s.getLinesBreakingLineFromSpace(words, width)
lines = t.getLinesBreakingLineFromSpace(words, width)

Check warning on line 97 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L97

Added line #L97 was not covered by tests
} else {
lines = s.getLinesBreakingLineWithDash(unicodeText, width)
lines = t.getLinesBreakingLineWithDash(unicodeText, width)

Check warning on line 99 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L99

Added line #L99 was not covered by tests
}

accumulateOffsetY := 0.0

for index, line := range lines {
lineWidth := s.pdf.GetStringWidth(line)
lineWidth := t.pdf.GetStringWidth(line)

Check warning on line 105 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L105

Added line #L105 was not covered by tests

s.addLine(textProp, x, width, y+float64(index)*fontHeight+accumulateOffsetY, lineWidth, line)
t.addLine(textProp, x, width, y+float64(index)*fontHeight+accumulateOffsetY, lineWidth, line)

Check warning on line 107 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L107

Added line #L107 was not covered by tests
accumulateOffsetY += textProp.VerticalPadding
}

if textProp.Color != nil {
s.font.SetColor(originalColor)
t.font.SetColor(originalColor)

Check warning on line 112 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L112

Added line #L112 was not covered by tests
}
}

// GetLinesQuantity retrieve the quantity of lines which a text will occupy to avoid that text to extrapolate a cell.
func (s *text) GetLinesQuantity(text string, textProp *props.Text, colWidth float64) int {
s.font.SetFont(textProp.Family, textProp.Style, textProp.Size)
func (t *text) GetLinesQuantity(text string, textProp *props.Text, colWidth float64) int {
t.font.SetFont(textProp.Family, textProp.Style, textProp.Size)

textTranslated := s.textToUnicode(text, textProp)
textTranslated := t.textToUnicode(text, textProp)

if textProp.BreakLineStrategy == breakline.DashStrategy {
return len(s.getLinesBreakingLineWithDash(text, colWidth))
return len(t.getLinesBreakingLineWithDash(text, colWidth))
} else {
return len(s.getLinesBreakingLineFromSpace(strings.Split(textTranslated, " "), colWidth))
return len(t.getLinesBreakingLineFromSpace(strings.Split(textTranslated, " "), colWidth))
}
}

func (s *text) getLinesBreakingLineFromSpace(words []string, colWidth float64) []string {
func (t *text) getLinesBreakingLineFromSpace(words []string, colWidth float64) []string {
currentlySize := 0.0
actualLine := 0

lines := []string{}
lines = append(lines, "")

for _, word := range words {
if s.pdf.GetStringWidth(word+" ")+currentlySize < colWidth {
if t.pdf.GetStringWidth(word+" ")+currentlySize < colWidth {
lines[actualLine] = lines[actualLine] + word + " "
currentlySize += s.pdf.GetStringWidth(word + " ")
currentlySize += t.pdf.GetStringWidth(word + " ")
} else {
lines = append(lines, "")
actualLine++
lines[actualLine] = lines[actualLine] + word + " "
currentlySize = s.pdf.GetStringWidth(word + " ")
currentlySize = t.pdf.GetStringWidth(word + " ")
}
}

return lines
}

func (s *text) getLinesBreakingLineWithDash(words string, colWidth float64) []string {
func (t *text) getLinesBreakingLineWithDash(words string, colWidth float64) []string {
currentlySize := 0.0

lines := []string{}

dashSize := s.pdf.GetStringWidth(" - ")
dashSize := t.pdf.GetStringWidth(" - ")

var content string
for _, letter := range words {
Expand All @@ -154,7 +165,7 @@
}

letterString := fmt.Sprintf("%c", letter)
width := s.pdf.GetStringWidth(letterString)
width := t.pdf.GetStringWidth(letterString)
content += letterString
currentlySize += width
}
Expand All @@ -166,16 +177,16 @@
return lines
}

func (s *text) addLine(textProp *props.Text, xColOffset, colWidth, yColOffset, textWidth float64, text string) {
left, top, _, _ := s.pdf.GetMargins()
func (t *text) addLine(textProp *props.Text, xColOffset, colWidth, yColOffset, textWidth float64, text string) {
left, top, _, _ := t.pdf.GetMargins()

fontHeight := s.font.GetHeight(textProp.Family, textProp.Style, textProp.Size)
fontHeight := t.font.GetHeight(textProp.Family, textProp.Style, textProp.Size)

if textProp.Align == align.Left {
s.pdf.Text(xColOffset+left, yColOffset+top, text)
t.pdf.Text(xColOffset+left, yColOffset+top, text)

Check warning on line 186 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L186

Added line #L186 was not covered by tests

if textProp.Hyperlink != nil {
s.pdf.LinkString(xColOffset+left, yColOffset+top-fontHeight, textWidth, fontHeight, *textProp.Hyperlink)
t.pdf.LinkString(xColOffset+left, yColOffset+top-fontHeight, textWidth, fontHeight, *textProp.Hyperlink)

Check warning on line 189 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L189

Added line #L189 was not covered by tests
}

return
Expand All @@ -187,8 +198,8 @@

text = strings.TrimRight(text, spaceString)
textNotSpaces := strings.ReplaceAll(text, spaceString, emptyString)
textWidth = s.pdf.GetStringWidth(textNotSpaces)
defaultSpaceWidth := s.pdf.GetStringWidth(spaceString)
textWidth = t.pdf.GetStringWidth(textNotSpaces)
defaultSpaceWidth := t.pdf.GetStringWidth(spaceString)

Check warning on line 202 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L201-L202

Added lines #L201 - L202 were not covered by tests
words := strings.Fields(text)

numSpaces := max(len(words)-1, 1)
Expand All @@ -201,13 +212,13 @@
initX := x
var finishX float64
for _, word := range words {
s.pdf.Text(x, yColOffset+top, word)
finishX = x + s.pdf.GetStringWidth(word)
t.pdf.Text(x, yColOffset+top, word)
finishX = x + t.pdf.GetStringWidth(word)

Check warning on line 216 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L215-L216

Added lines #L215 - L216 were not covered by tests
x = finishX + spaceWidth
}

if textProp.Hyperlink != nil {
s.pdf.LinkString(initX, yColOffset+top-fontHeight, finishX-initX, fontHeight, *textProp.Hyperlink)
t.pdf.LinkString(initX, yColOffset+top-fontHeight, finishX-initX, fontHeight, *textProp.Hyperlink)

Check warning on line 221 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L221

Added line #L221 was not covered by tests
}

return
Expand All @@ -222,19 +233,19 @@
dx := (colWidth - textWidth) / modifier

if textProp.Hyperlink != nil {
s.pdf.LinkString(dx+xColOffset+left, yColOffset+top-fontHeight, textWidth, fontHeight, *textProp.Hyperlink)
t.pdf.LinkString(dx+xColOffset+left, yColOffset+top-fontHeight, textWidth, fontHeight, *textProp.Hyperlink)

Check warning on line 236 in internal/providers/gofpdf/text.go

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L236

Added line #L236 was not covered by tests
}

s.pdf.Text(dx+xColOffset+left, yColOffset+top, text)
t.pdf.Text(dx+xColOffset+left, yColOffset+top, text)
}

func (s *text) textToUnicode(txt string, props *props.Text) string {
func (t *text) textToUnicode(txt string, props *props.Text) string {
if props.Family == fontfamily.Arial ||
props.Family == fontfamily.Helvetica ||
props.Family == fontfamily.Symbol ||
props.Family == fontfamily.ZapBats ||
props.Family == fontfamily.Courier {
translator := s.pdf.UnicodeTranslatorFromDescriptor("")
translator := t.pdf.UnicodeTranslatorFromDescriptor("")
return translator(txt)
}

Expand Down
28 changes: 28 additions & 0 deletions internal/providers/gofpdf/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"fmt"
"testing"

"github.com/johnfercher/maroto/v2/internal/fixture"
"github.com/johnfercher/maroto/v2/internal/providers/gofpdf"
"github.com/johnfercher/maroto/v2/pkg/consts/align"
"github.com/johnfercher/maroto/v2/pkg/consts/breakline"
"github.com/johnfercher/maroto/v2/pkg/consts/fontfamily"
"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/props"

"github.com/johnfercher/maroto/v2/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestNewText(t *testing.T) {
Expand All @@ -21,6 +25,30 @@ func TestNewText(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%T", text), "*gofpdf.text")
}

func TestAdd(t *testing.T) {
t.Run("when usePageMargin is false, should set the page margin to 0", func(t *testing.T) {
color := fixture.ColorProp()

pdf := mocks.NewFpdf(t)
pdf.EXPECT().SetMargins(0.0, 0.0, 0.0)
pdf.EXPECT().SetMargins(10.0, 10.0, 10.0)
pdf.EXPECT().GetMargins().Return(10.0, 10.0, 10.0, 10.0)
pdf.EXPECT().GetStringWidth(mock.Anything).Return(4)
pdf.EXPECT().Text(mock.Anything, mock.Anything, mock.Anything)

font := mocks.NewFont(t)
font.EXPECT().SetFont(mock.Anything, mock.Anything, mock.Anything)
font.EXPECT().GetHeight(mock.Anything, mock.Anything, mock.Anything).Return(10.0)
font.EXPECT().GetColor().Return(&color)

text := gofpdf.NewText(pdf, mocks.NewMath(t), font)
text.Add("test", &entity.Cell{X: 0, Y: 0, Width: 100, Height: 290}, &props.Text{Align: align.Center}, false)

pdf.AssertNumberOfCalls(t, "SetMargins", 2)
pdf.AssertNumberOfCalls(t, "GetMargins", 2)
})
}

func TestGetLinesheight(t *testing.T) {
t.Run("when a text that occupies two lines is sent with EmptySpaceStrategy, should two is returned", func(t *testing.T) {
textProp := &props.Text{}
Expand Down
8 changes: 7 additions & 1 deletion maroto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

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

"github.com/johnfercher/maroto/v2/pkg/components/code"
"github.com/johnfercher/maroto/v2/pkg/components/text"

Expand Down Expand Up @@ -397,9 +399,13 @@ func TestMaroto_Generate(t *testing.T) {
assert.Equal(t, initialGoroutines, finalGoroutines)
})
t.Run("page number", func(t *testing.T) {
pageNumber := props.PageNumber{
MarginTop: 1,
MarginLeft: 2,
}
// Arrange
cfg := config.NewBuilder().
WithPageNumber().
WithPageNumber(pageNumber).
Build()

sut := maroto.New(cfg)
Expand Down
Loading
Loading