[
https://issues.apache.org/jira/browse/CAMEL-27934?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Work on CAMEL-27934 started by Andrea Cosentino.
------------------------------------------------
> camel-pdf - autoFormatting line builder can loop on a word that does not fit
> ----------------------------------------------------------------------------
>
> Key: CAMEL-27934
> URL: https://issues.apache.org/jira/browse/CAMEL-27934
> Project: Camel
> Issue Type: Improvement
> Components: camel-pdf
> Reporter: Andrea Cosentino
> Assignee: Andrea Cosentino
> Priority: Major
> Fix For: 4.25.0
>
>
> In the camel-pdf producer, when {{textProcessingFactory=autoFormatting}} is
> configured (the default is {{lineTermination}}), the {{create}} and
> {{append}} operations split the message body into words and lay them out into
> lines with
> {{org.apache.camel.component.pdf.text.DefaultLineBuilderStrategy.buildLines()}}.
> h3. Current behaviour
> When a single word does not fit on an otherwise empty line, {{buildLines()}}
> calls {{findSplitIndex(word, font, allowedLineWidth)}} to decide how many
> leading characters of the word fit on the line. {{findSplitIndex()}} is a
> hand-rolled binary search whose loop continues until {{isSplitIndexFound()}}
> returns true, i.e. until it finds an index {{i}} where the prefix of length
> {{i}} fits but the prefix of length {{i+1}} does not.
> Two cases make that loop, or the surrounding loop in {{buildLines()}}, never
> complete:
> # The fit test used by the caller, {{isWordFitInCurrentLine()}}, measures the
> word *with* a trailing space (see {{LineBuilder.appendWord()}}, which appends
> {{" "}}), while {{findSplitIndex()}} / {{isSplitIndexFound()}} measure
> prefixes of the word *without* that trailing space. For a word whose rendered
> width falls in the narrow window where the whole word fits on the line but
> the word plus a trailing space does not, there is no boundary {{i}} with
> "fits at i, does not fit at i+1", so {{isSplitIndexFound()}} is never
> satisfied and {{findSplitIndex()}} does not return. This is reachable with
> the default page size and font.
> # When not even the first character fits on the line (an unusual
> configuration, e.g. a very large {{fontSize}} or an {{allowedLineWidth}}
> close to {{MIN_CONTENT_WIDTH}}), {{findSplitIndex()}} returns {{0}}.
> {{buildLines()}} then adds an empty prefix as a line and re-queues the whole
> word via {{wordsList.addFirst(word.substring(0))}}, so the outer {{while}}
> loop makes no progress and the {{lines}} collection grows without bound
> (eventually an {{OutOfMemoryError}}).
> h3. After the change
> {{findSplitIndex()}} is rewritten as a bounded search that returns the
> largest prefix length in {{[1, word.length()]}} that fits, and always returns
> at least {{1}}. Prefix widths are monotonic non-decreasing in length, so a
> simple scan is correct and clear. The now-unused {{isSplitIndexFound()}}
> helper is removed.
> {{buildLines()}} re-queues a remainder only when the word was actually split,
> i.e. when the returned split index is less than the word length; a word that
> fully fits is emitted as its own line with no empty remainder re-queued. A
> word (or a single glyph) wider than the line is placed on its own line
> instead of causing the layout to never complete.
> Both the inner search and the outer line-building loop then always terminate,
> and valid input lays out exactly as before.
> h3. Classes involved
> * {{org.apache.camel.component.pdf.text.DefaultLineBuilderStrategy}}
> ({{buildLines()}} and {{findSplitIndex()}} changed; {{isSplitIndexFound()}}
> removed) in {{components/camel-pdf}}.
> * Only the {{autoFormatting}} path is touched. The default
> {{textProcessingFactory=lineTermination}} path
> ({{LineTerminationWriterAbstractFactory}}) does not split words to fit the
> line width and is unchanged.
> h3. Tests
> A new package-private {{DefaultLineBuilderStrategyTest}} under
> {{components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/}}:
> * a word longer than the line width wraps across several lines and
> {{buildLines()}} returns;
> * a word sized to fit on the line on its own, but not with a following space,
> is emitted on its own line and {{buildLines()}} returns promptly (the
> regression guard for case 1);
> * an edge case where a single glyph is wider than the line still terminates
> rather than growing the output without bound.
> The regression tests carry a JUnit 5 {{@Timeout}} so a reintroduced
> non-terminating loop fails fast instead of blocking the build. Tests use
> JUnit 5 assertions to match the sibling tests in the same package
> ({{PatternSplitStrategyTest}}, {{WordSplitStrategyTest}}); no
> {{Thread.sleep}} is used.
> _Filed with Claude Code on behalf of oscerd._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)