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

Add align type Justify to texts #460

Merged
merged 4 commits into from
Jun 25, 2024
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
7 changes: 7 additions & 0 deletions docs/assets/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,12 @@ func GetMaroto() core.Maroto {

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

m.AddRow(45,
text.NewCol(2, longText, props.Text{Top: 3, Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.DashStrategy}),
text.NewCol(4, longText+" "+longText, props.Text{Top: 10, Left: 3, Right: 3, Align: align.Justify}),
text.NewCol(6, longText+" "+longText, props.Text{Hyperlink: &google, Top: 10, Left: 10, Right: 10, Align: align.Justify}),
)
m.AddRows(text.NewRow(10, "Justify-aligned text", props.Text{Align: align.Justify}))

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: 16.01ms, executions: [16.01ms]
add_row -> avg: 755.00ns, executions: [1.49μs, 0.51μs, 0.37μs, 0.35μs, 0.33μs, 1.49μs]
add_rows -> avg: 261.67ns, executions: [323.00ns, 322.00ns, 143.00ns, 364.00ns, 144.00ns, 274.00ns]
file_size -> 16.45Kb
42 changes: 42 additions & 0 deletions internal/providers/gofpdf/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"fmt"
"strings"
"unicode"

"github.com/johnfercher/maroto/v2/internal/providers/gofpdf/gofpdfwrapper"
"github.com/johnfercher/maroto/v2/pkg/consts/align"
Expand Down Expand Up @@ -187,6 +188,38 @@
return
}

if textProp.Align == align.Justify {
const spaceString = " "
const emptyString = ""

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L191-L193

Added lines #L191 - L193 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L195 - L199 were not covered by tests

numSpaces := max(len(words)-1, 1)
spaceWidth := (colWidth - textWidth) / float64(numSpaces)
x := xColOffset + left

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L201 - L203 were not covered by tests

if isIncorrectSpaceWidth(textWidth, spaceWidth, defaultSpaceWidth, textNotSpaces) {
spaceWidth = defaultSpaceWidth

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L205-L206

Added lines #L205 - L206 were not covered by tests
}
initX := x
var finishX float64
for _, word := range words {
s.pdf.Text(x, yColOffset+top, word)
finishX = x + s.pdf.GetStringWidth(word)
x = finishX + spaceWidth

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L208-L213

Added lines #L208 - L213 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L216 - L217 were not covered by tests
}

return

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L220

Added line #L220 was not covered by tests
}

var modifier float64 = 2

if textProp.Align == align.Right {
Expand Down Expand Up @@ -214,3 +247,12 @@

return txt
}

func isIncorrectSpaceWidth(textWidth, spaceWidth, defaultSpaceWidth float64, text string) bool {
if textWidth <= 0 || spaceWidth <= defaultSpaceWidth*10 {
return false

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L251-L253

Added lines #L251 - L253 were not covered by tests
}

lastChar := rune(text[len(text)-1])
return !unicode.IsLetter(lastChar) && !unicode.IsNumber(lastChar)

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

View check run for this annotation

Codecov / codecov/patch

internal/providers/gofpdf/text.go#L256-L257

Added lines #L256 - L257 were not covered by tests
}
3 changes: 3 additions & 0 deletions pkg/consts/align/align.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ const (
Bottom Type = "B"
// Middle represents a middle align (from gofpdf).
Middle Type = "M"
// Justify represents a horizontal alignment that distributes
// the text evenly between the left and right margins.
Justify = "J"
)
92 changes: 92 additions & 0 deletions test/maroto/examples/textgrid.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,98 @@
]
}
]
},
{
"type": "page",
"nodes": [
{
"value": 45,
"type": "row",
"nodes": [
{
"value": 2,
"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.",
"type": "text",
"details": {
"prop_align": "J",
"prop_breakline_strategy": "dash_strategy",
"prop_left": 3,
"prop_right": 3,
"prop_top": 3
}
}
]
},
{
"value": 4,
"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.",
"type": "text",
"details": {
"prop_align": "J",
"prop_left": 3,
"prop_right": 3,
"prop_top": 10
}
}
]
},
{
"value": 6,
"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.",
"type": "text",
"details": {
"prop_align": "J",
"prop_hyperlink": "https://google.com",
"prop_left": 10,
"prop_right": 10,
"prop_top": 10
}
}
]
}
]
},
{
"value": 10,
"type": "row",
"nodes": [
{
"value": 0,
"type": "col",
"details": {
"is_max": true
},
"nodes": [
{
"value": "Justify-aligned text" ,
"type": "text",
"details": {
"prop_align": "J"
} }
]
}
]
},
{
"value": 211.9975,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
}
]
}
]
}
Loading