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

Features: Add hyperlink to text props #372

Merged
merged 2 commits into from
Nov 27, 2023
Merged
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
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 @@ -84,5 +84,9 @@ func GetMaroto() core.Maroto {

m.AddRows(text.NewRow(10, "Multiline text with indentation"))

google := "https://google.com"

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

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: 3.15ms, executions: [3.15ms]
add_row -> avg: 67.50ns, executions: [111.00ns, 62.00ns, 25.00ns, 72.00ns]
add_cols -> avg: 199.60ns, executions: [706.00ns, 119.00ns, 56.00ns, 67.00ns, 50.00ns]
file_size -> 5.98Kb
generate -> avg: 9.09ms, executions: [9.09ms]
add_row -> avg: 325.00ns, executions: [667.00ns, 333.00ns, 250.00ns, 208.00ns, 167.00ns]
add_rows -> avg: 141.60ns, executions: [208.00ns, 167.00ns, 83.00ns, 209.00ns, 41.00ns]
file_size -> 8.66Kb
4 changes: 4 additions & 0 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (

func TextProp() props.Text {
fontProp := FontProp()

google := "https://www.google.com"

prop := props.Text{
Top: 12,
Left: 3,
Expand All @@ -27,6 +30,7 @@ func TextProp() props.Text {
BreakLineStrategy: breakline.DashStrategy,
VerticalPadding: 20,
Color: fontProp.Color,
Hyperlink: &google,
}
prop.MakeValid(&fontProp)
return prop
Expand Down
16 changes: 16 additions & 0 deletions internal/providers/gofpdf/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
s.font.SetColor(textProp.Color)
}

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

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}

y += fontHeight

// Apply Unicode before calc spaces
Expand Down Expand Up @@ -171,8 +176,15 @@
func (s *text) addLine(textProp *props.Text, xColOffset, colWidth, yColOffset, textWidth float64, text string) {
left, top, _, _ := s.pdf.GetMargins()

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

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L179

Added line #L179 was not covered by tests

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

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

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L184-L185

Added lines #L184 - L185 were not covered by tests
}

return
}

Expand All @@ -184,6 +196,10 @@

dx := (colWidth - textWidth) / modifier

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

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L199-L200

Added lines #L199 - L200 were not covered by tests
}

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

Expand Down
6 changes: 6 additions & 0 deletions pkg/props/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
VerticalPadding float64
// Color define the fontstyle color.
Color *Color
// Hyperlink define a link to be opened when the text is clicked.
Hyperlink *string
}

func (t *Text) ToMap() map[string]interface{} {
Expand Down Expand Up @@ -71,6 +73,10 @@
m["prop_color"] = t.Color.ToString()
}

if t.Hyperlink != nil {
m["prop_hyperlink"] = *t.Hyperlink

Check warning on line 77 in pkg/props/text.go

View check run for this annotation

Codecov / codecov/patch

pkg/props/text.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

return m
}

Expand Down
3 changes: 2 additions & 1 deletion test/maroto/components/texts/new_text_col_custom_prop.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"prop_font_style": "B",
"prop_left": 3,
"prop_top": 12,
"prop_vertical_padding": 20
"prop_vertical_padding": 20,
"prop_hyperlink": "https://www.google.com"
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion test/maroto/components/texts/new_text_custom_prop.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"prop_font_style": "B",
"prop_left": 3,
"prop_top": 12,
"prop_vertical_padding": 20
"prop_vertical_padding": 20,
"prop_hyperlink": "https://www.google.com"
}
}
3 changes: 2 additions & 1 deletion test/maroto/components/texts/new_text_row_custom_prop.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"prop_font_style": "B",
"prop_left": 3,
"prop_top": 12,
"prop_vertical_padding": 20
"prop_vertical_padding": 20,
"prop_hyperlink": "https://www.google.com"
}
}
]
Expand Down
Loading