Search code examples
jasper-reports

JasperReports: Prevent textField to split on space or hyphen


I have a JasperReports template that contains a textField element that will contain a variable length of strings. If the string is too long to fit the width, it'll be split which is fine except when the string contains a hyphen or a space char. In this case the string is split from that char. Below are some example of what's the input, observed outcome and wanted outcome + summary to make my point easier to understand.

input      observed   wanted   summary
_____________________________________
overflow  -> overfl -> overfl : OK
over flow -> over   -> over f : NOT OK
over-flow -> over-  -> over-f : NOT OK

Setting the reportElement to have isStretchWithOverflow="true" attribute will split the text on two lines, but this is not wanted behaviour.

Is there anyway to fix this?

Thanks.

EDIT: The input data comes from an external source, so I cannot directly change that. I ran some tests and noticed that using non-breaking space will do for spaces. non-breaking hyphen on the other hand is not printed at all, i.e. text 'over-flow' becomes 'overflow'. Not quite what is wanted.

Despite the input source not in my control, I could fix this problem by writing a Scriptlet that'll change spaces to non-breaking spaces and hyphens to non-breaking hyphens, only if those darn non-breaking hyphens would be printed.

Printing to PDF by the way, in case that gives some hints of the problem.


Solution

  • It's a font issue. Non-breaking hyphen works(\u2011) fine, when the font supports it. See fonts sample on how load other than the default font (DejaVu Sans for example).

    If anyone has a better option for the input source modification than the Scriptlet, please let me know.

    (Marking my own answer as correct in order to get this topic closed.)

    EDIT: Have to wait for two days in order to mark this answer correct.