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

feature/Support for multiple styles in same text #447

Open
wants to merge 15 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
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 @@ -4,11 +4,14 @@ import (
"log"

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

"github.com/johnfercher/maroto/v2"

"github.com/johnfercher/maroto/v2/pkg/consts/breakline"

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

"github.com/johnfercher/maroto/v2/pkg/consts/align"
Expand Down Expand Up @@ -88,5 +91,11 @@ func GetMaroto() core.Maroto {

m.AddRows(text.NewRow(10, "text with hyperlink", props.Text{Hyperlink: &google}))

subText1 := entity.NewSubText("This is a text", props.SubText{Color: &props.BlueColor})
subText2 := entity.NewSubText(" with multiple", props.SubText{Size: 7})
subText3 := entity.NewSubText(" styles", props.SubText{Color: &props.RedColor})
Comment on lines +94 to +96
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe NewSubText should be in text package also as NewCustomText.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do it this way, but the provider depends on the subText structure and if I declare the subtext in the Text package, it generates a cyclical dependency.
I think this would be possible if we remove the dependency that the provider has on subtext, but I don't know how to do this in a nice way


customText := col.New(12).Add(text.NewCustomText([]*entity.SubText{subText1, subText2, subText3}))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could change the Add([]text) to Add(text...). With a vargar we can still pass an array with as NewCustomText(arr...) and we can pass each one as this NewCustomText(subText1, subText2, subText3) this is a more extensible and idiomatic way to work with collections in go.

Copy link
Collaborator Author

@Fernando-hub527 Fernando-hub527 Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this way is better. The only issue is that NewCustomText also needs to receive text.Props and I'm already using vararg in this parameter, but I agree that using vargar in the subText parameter is better. The function call would look like this: NewCustomText(props.Text{}, subText1, subText2, subText3), do you think it looks good this way?

m.AddRows(row.New(10).Add(customText))
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: 21.04ms, executions: [21.04ms]
add_row -> avg: 675.40ns, executions: [2.01μs, 0.50μs, 0.30μs, 0.30μs, 0.26μs]
add_rows -> avg: 260.40ns, executions: [331.00ns, 301.00ns, 100.00ns, 470.00ns, 100.00ns]
file_size -> 8.74Kb
generate -> avg: 11.28ms, executions: [11.28ms]
add_row -> avg: 420.80ns, executions: [1.14μs, 0.30μs, 0.27μs, 0.21μs, 0.18μs]
add_rows -> avg: 131.83ns, executions: [230.00ns, 190.00ns, 60.00ns, 221.00ns, 40.00ns, 50.00ns]
file_size -> 15.78Kb
14 changes: 13 additions & 1 deletion docs/v2/features/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@
* [props : Text](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/props#Text)
* [component : Text](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/text#Text)

## Overview

Using the Text component, you can add personalized texts to your PDF. Possible customizations include: font, color, style and margin. The component also allows you to add links and use different styles in the same text.
Different builders are offered to facilitate use, they are:

* [constructor : New](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/text#New)
* [constructor : NewCol](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/text#NewCol)
* [constructor : NewRow](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/text#NewRow)
* [constructor : NewCustomText](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/text#NewCustomText)

In Maroto, texts can contain one or more styles, if your text does not need to mix several styles, you can use the constructors **New**, **NewCol** or **NewRow**; However, if you need to merge different styles, this can be done with the **NewCustomText** constructor, unlike the previous constructors, you will need to pass a list of [Sub texts](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/core/entity#SubText), each subtext will have its own [style](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/props#SubText)


## Code Example
[filename](../../assets/examples/textgrid/v2/main.go ':include :type=code')

## PDF Generated
```pdf
assets/pdf/textgridv2.pdf
assets/pdf/textgridv2.pdf
```

## Time Execution
Expand Down
4 changes: 4 additions & 0 deletions internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (g *provider) AddText(text string, cell *entity.Cell, prop *props.Text) {
g.text.Add(text, cell, prop)
}

func (g *provider) AddCustomText(subs []*entity.SubText, cell *entity.Cell, textPs *props.Text) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here. vararg instea of array.

g.text.AddCustomText(subs, cell, textPs)
}

func (g *provider) GetTextHeight(prop *props.Font) float64 {
return g.font.GetHeight(prop.Family, prop.Style, prop.Size)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/providers/gofpdf/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

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

"github.com/johnfercher/maroto/v2/internal/fixture"
"github.com/johnfercher/maroto/v2/internal/merror"
Expand Down Expand Up @@ -55,6 +56,27 @@ func TestProvider_AddText(t *testing.T) {
text.AssertNumberOfCalls(t, "Add", 1)
}

func TestProvider_AddCustomText(t *testing.T) {
// Arrange
cell := &entity.Cell{}
prop := fixture.TextProp()
subs := []*entity.SubText{entity.NewSubText("text", props.NewSubText(&prop))}

text := mocks.NewText(t)
text.EXPECT().AddCustomText(subs, cell, &prop)

dep := &gofpdf.Dependencies{
Text: text,
}
sut := gofpdf.New(dep)

// Act
sut.AddCustomText(subs, cell, &prop)

// Assert
text.AssertNumberOfCalls(t, "AddCustomText", 1)
}

func TestProvider_GetTextHeight(t *testing.T) {
// Arrange
fontHeightToReturn := 10.0
Expand Down
Loading
Loading