diff --git a/docs/assets/examples/textgrid/v2/main.go b/docs/assets/examples/textgrid/v2/main.go index 8653ed80..ff52e1d0 100644 --- a/docs/assets/examples/textgrid/v2/main.go +++ b/docs/assets/examples/textgrid/v2/main.go @@ -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 } diff --git a/docs/assets/pdf/textgridv2.pdf b/docs/assets/pdf/textgridv2.pdf index 8542775a..b82ddb71 100644 Binary files a/docs/assets/pdf/textgridv2.pdf and b/docs/assets/pdf/textgridv2.pdf differ diff --git a/docs/assets/text/textgridv2.txt b/docs/assets/text/textgridv2.txt index 426c6faf..cc003675 100644 --- a/docs/assets/text/textgridv2.txt +++ b/docs/assets/text/textgridv2.txt @@ -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 diff --git a/internal/fixture/fixture.go b/internal/fixture/fixture.go index 0ce68976..8eebebf6 100644 --- a/internal/fixture/fixture.go +++ b/internal/fixture/fixture.go @@ -17,6 +17,9 @@ import ( func TextProp() props.Text { fontProp := FontProp() + + google := "https://www.google.com" + prop := props.Text{ Top: 12, Left: 3, @@ -27,6 +30,7 @@ func TextProp() props.Text { BreakLineStrategy: breakline.DashStrategy, VerticalPadding: 20, Color: fontProp.Color, + Hyperlink: &google, } prop.MakeValid(&fontProp) return prop diff --git a/internal/providers/gofpdf/text.go b/internal/providers/gofpdf/text.go index 2391f300..c753a6a2 100644 --- a/internal/providers/gofpdf/text.go +++ b/internal/providers/gofpdf/text.go @@ -63,6 +63,11 @@ func (s *text) Add(text string, cell *entity.Cell, textProp *props.Text) { s.font.SetColor(textProp.Color) } + // override style if hyperlink is set + if textProp.Hyperlink != nil { + s.font.SetColor(&props.BlueColor) + } + y += fontHeight // Apply Unicode before calc spaces @@ -171,8 +176,15 @@ func (s *text) getLinesBreakingLineWithDash(words string, colWidth float64) []st 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) + 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) + } + return } @@ -184,6 +196,10 @@ func (s *text) addLine(textProp *props.Text, xColOffset, colWidth, yColOffset, t dx := (colWidth - textWidth) / modifier + if textProp.Hyperlink != nil { + s.pdf.LinkString(dx+xColOffset+left, yColOffset+top-fontHeight, textWidth, fontHeight, *textProp.Hyperlink) + } + s.pdf.Text(dx+xColOffset+left, yColOffset+top, text) } diff --git a/pkg/props/text.go b/pkg/props/text.go index 98b375a8..5c5af3f4 100644 --- a/pkg/props/text.go +++ b/pkg/props/text.go @@ -27,6 +27,8 @@ type Text struct { 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{} { @@ -71,6 +73,10 @@ func (t *Text) ToMap() map[string]interface{} { m["prop_color"] = t.Color.ToString() } + if t.Hyperlink != nil { + m["prop_hyperlink"] = *t.Hyperlink + } + return m } diff --git a/test/maroto/components/texts/new_text_col_custom_prop.json b/test/maroto/components/texts/new_text_col_custom_prop.json index 67f83f5b..833bb01f 100755 --- a/test/maroto/components/texts/new_text_col_custom_prop.json +++ b/test/maroto/components/texts/new_text_col_custom_prop.json @@ -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" } } ] diff --git a/test/maroto/components/texts/new_text_custom_prop.json b/test/maroto/components/texts/new_text_custom_prop.json index 27b39bf4..f6470044 100755 --- a/test/maroto/components/texts/new_text_custom_prop.json +++ b/test/maroto/components/texts/new_text_custom_prop.json @@ -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" } } \ No newline at end of file diff --git a/test/maroto/components/texts/new_text_row_custom_prop.json b/test/maroto/components/texts/new_text_row_custom_prop.json index 451707ad..6213f8fe 100755 --- a/test/maroto/components/texts/new_text_row_custom_prop.json +++ b/test/maroto/components/texts/new_text_row_custom_prop.json @@ -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" } } ]