Skip to content

feature/character-breakline-strategy#561

Open
joeyave wants to merge 2 commits intojohnfercher:masterfrom
joeyave:master
Open

feature/character-breakline-strategy#561
joeyave wants to merge 2 commits intojohnfercher:masterfrom
joeyave:master

Conversation

@joeyave
Copy link
Copy Markdown

@joeyave joeyave commented Mar 23, 2026

Description
This PR adds breakline.CharacterStrategy, a new break line strategy that wraps text at character boundaries without appending a dash.

This is useful for languages and content where breaking by spaces is not appropriate, but adding hyphens is also undesirable.

The implementation also makes line splitting consistent between rendering and height calculation by reusing the same strategy path in Add and GetLinesQuantity, including Unicode-translated text. This avoids mismatches between rendered output and auto-row sizing.

In addition, this PR updates the text documentation, README, Go examples, and the textgrid example assets to show how CharacterStrategy behaves compared with DashStrategy.

Related Issue
N/A

Checklist

  • All methods associated with structs has func (<first letter of struct> *struct) method() {} name style.
  • Wrote unit tests for new/changed features.
  • Followed the unit test when,should naming pattern.
  • All mocks created with m := mocks.NewConstructor(t).
  • All mocks using m.EXPECT().MethodName() method to mock methods.
  • Updated docs/*
  • Updated example_test.go.
  • Updated README.md
  • New public methods/structs/interfaces has comments upside them explaining they responsibilities
  • Executed make dod with none issues pointed out by golangci-lint

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7c6c6317-0ad0-4229-862e-2402471bf429

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@johnfercher
Copy link
Copy Markdown
Owner

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/providers/gofpdf/text.go (1)

96-102: ⚠️ Potential issue | 🟡 Minor

Keep GetLinesQuantity consistent with Add for fit-case inputs.

At Line 101, counting only via getLines(...) can return 0 for empty/whitespace-only strings, while Add renders a single line when the text width fits (Line 75). This can still desync auto-row height vs rendered output.

💡 Proposed fix
 func (s *Text) GetLinesQuantity(text string, textProp *props.Text, colWidth float64) int {
 	s.font.SetFont(textProp.Family, textProp.Style, textProp.Size)

 	textTranslated := s.textToUnicode(text, textProp)

-	return len(s.getLines(textTranslated, textProp.BreakLineStrategy, colWidth))
+	if s.pdf.GetStringWidth(textTranslated) <= colWidth {
+		return 1
+	}
+
+	return len(s.getLines(textTranslated, textProp.BreakLineStrategy, colWidth))
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/providers/gofpdf/text.go` around lines 96 - 102, GetLinesQuantity
currently returns len(s.getLines(...)) which can be 0 for empty or
whitespace-only inputs whereas Add will render a single line when the text fits;
update GetLinesQuantity (after calling s.textToUnicode and s.getLines) to handle
the fit-case: if the computed lines slice is empty but the (trimmed or original)
text width fits within colWidth (use the same width-measure logic as Add after
s.font.SetFont), return 1; otherwise return the length of the lines slice.
Ensure you reuse s.textToUnicode, s.font.SetFont and s.getLines logic so
behavior stays consistent with Add.
🧹 Nitpick comments (1)
internal/providers/gofpdf/text_test.go (1)

27-88: Add a regression subtest for empty/whitespace-only fit-case line counting.

A small case asserting GetLinesQuantity matches Add behavior for fit-path empty/space-only text would lock in the render/height consistency goal.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/providers/gofpdf/text_test.go` around lines 27 - 88, Add a
regression subtest in TestGetLinesHeight that verifies GetLinesQuantity handles
empty or whitespace-only strings the same way the Add rendering path does:
create a props.Text (valid font), set up mocks for font.SetFont and
pdf.UnicodeTranslatorFromDescriptor and any expected pdf.GetStringWidth calls
for spaces, then call text.GetLinesQuantity with inputs like "" and "   " and
assert the returned line count equals what the Add/renderer would produce for
those inputs (use the same NewText constructor: gofpdf.NewText(pdf,
mocks.NewMath(t), font) and call GetLinesQuantity to compare). Ensure the test
name explains it's for empty/whitespace-only fit-case regression and mark it
t.Parallel().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@internal/providers/gofpdf/text.go`:
- Around line 96-102: GetLinesQuantity currently returns len(s.getLines(...))
which can be 0 for empty or whitespace-only inputs whereas Add will render a
single line when the text fits; update GetLinesQuantity (after calling
s.textToUnicode and s.getLines) to handle the fit-case: if the computed lines
slice is empty but the (trimmed or original) text width fits within colWidth
(use the same width-measure logic as Add after s.font.SetFont), return 1;
otherwise return the length of the lines slice. Ensure you reuse
s.textToUnicode, s.font.SetFont and s.getLines logic so behavior stays
consistent with Add.

---

Nitpick comments:
In `@internal/providers/gofpdf/text_test.go`:
- Around line 27-88: Add a regression subtest in TestGetLinesHeight that
verifies GetLinesQuantity handles empty or whitespace-only strings the same way
the Add rendering path does: create a props.Text (valid font), set up mocks for
font.SetFont and pdf.UnicodeTranslatorFromDescriptor and any expected
pdf.GetStringWidth calls for spaces, then call text.GetLinesQuantity with inputs
like "" and "   " and assert the returned line count equals what the
Add/renderer would produce for those inputs (use the same NewText constructor:
gofpdf.NewText(pdf, mocks.NewMath(t), font) and call GetLinesQuantity to
compare). Ensure the test name explains it's for empty/whitespace-only fit-case
regression and mark it t.Parallel().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e0aae567-68e2-4437-9af1-7933b34e8bdd

📥 Commits

Reviewing files that changed from the base of the PR and between 4f0b54a and c7fc9ac.

⛔ Files ignored due to path filters (1)
  • docs/assets/pdf/textgridv2.pdf is excluded by !**/*.pdf
📒 Files selected for processing (9)
  • README.md
  • docs/assets/examples/textgrid/v2/main.go
  • docs/assets/text/textgridv2.txt
  • docs/v2/features/text.md
  • internal/providers/gofpdf/text.go
  • internal/providers/gofpdf/text_test.go
  • pkg/components/text/example_test.go
  • pkg/consts/breakline/breakline.go
  • test/maroto/examples/textgrid.json

@johnfercher
Copy link
Copy Markdown
Owner

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants