Jump to content
Changes to the Jaspersoft community edition download ×

Text wrapping, easy way to enable break on character vs word?


jpauze

Recommended Posts

This one has been bugging me for sometime, throw it out there in case anybody else has come across it and happens to know of a relatively simple solution.

See if I can explain properly

Text field with Stretch with Overflow enabled works really nice with the exception of one scenario...  When the text exceeds the limit of the field it will automatically wrap the text and auto stretches the field and band provided the text in the field actually contains a word boundary to break on.  If there is no word boundary then what happens is that for that detail line the field stretches horizontally and pushes the columns on the right over to accomodate the additional width but only for that detail line, what you end up with is a jagged looking report where the column data get's all misaligned and scattered.

I have set net.sf.jasperreports.text.truncate.at.char=true but that property only applies when Stretch with Overflow has been disabled, BTW,  works nicely with net.sf.jasperreports.text.truncate.suffix=... so that you get a nice visual, hey textistolongtodisplay... but I REALY WANT TO DISPLAY AND WRAP THE ENTIRE TEXT.

I see 3 potential options, that I can think of...

1) wait for dev team to include a "Break on Character" option which which can be applied when Stretch with Overflow enabled

2) Implement some sort of measurer extenstion through net.sf.jasperreports.text.measurer.factory but don't really want to do that because I have limited development skills

3) Check for some sort of max length and insert characters to force word break,  easily done but not a fan of this for many reasons, primarily that fonts are not fixed character width so 20 '.' are not the same as 20 'Z's.  This is my last resort.

Thoughts?

 

 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

It's an uncommon requirement to break in the middle of words.

There's nothing wrong with it. It makes sense. It's just not common. For example: web browsers have lots of components for displaying text, but they never (almost never?) break in the middle of words unless a word is longer than the full line width. Word processor are the same.

The most common way of addressing is with soft-hyphenation. The application needs to be language-aware enough to know where it makes sense to divide a word. This is probably out of scope for our discussion here. But it's possible that your source text would have soft hyphen characters in it which would be respected by the browser.

I had exactly this requirement in the past. In that case we went for a brute force method. We put a Zero Width Space (ZWSP) between all characters. Then lines always break without respecting word boundaries. It's easy to add this with a utility class like this:

public class StringHelperUtils {	public static String addZWSP(String string) {		char[] chars = string.toCharArray();		StringBuilder builder = new StringBuilder(string.substring(0, 1));		for (int i = 1; i < chars.length; i++) {			builder.append("u200B");			builder.append(chars[i]);		}		return builder.toString();	}}

Caveat: I had problems with PDF. They seemed to be version-specific based on iText changing its behavior so that JR's generated PDF was no longer the intended result. You should search for the bug if you need PDF. But for HTML or other export formats it should be great.

Regards,
Matt

 



Post Edited by mdahlman at 04/22/2011 20:39



Post Edited by mdahlman at 04/22/2011 20:40
Link to comment
Share on other sites

David, first off, thanks for the suggestion,  in my case using the <BR> is not ideal but do appreciate you taking the time to offer up an option.

Matt, totally agree not common however, in my case our product collects a lot of configuration data from hardware servers/devices and we commonly come across really long server names as customers get crazy with thier naming conventions, pain but what can you do.   

I really like your suggestion of using zero width spaces, never even occured to me and I think this will definitely do what I need it to and in my case I am only worried about HTML/XHTML so the PDF caveat is not a concern.

Let me give it a shot and I will post back how it works.

Thanks again, this one was bugging me for awhile.

 

Link to comment
Share on other sites

Matt, you are a rockstar!

I implemented the scriptlet to add the Zero Width Space as suggested and it works exactly as advertised.  I have attached a sample image showing the before and after effect of using this. In the before you will see how the right column shifts for only certain details rows that couldn't break on word, after, everything is perfectly aligned. 

Note:  Your sample code was adding "u200B" to the string but it should be "\u200B",  still totally appreciate the extra time you spent adding this sample for me.

For all those newbies out there, I have to add that my entire Java development experience is a result of what I learned in expressions within iReport and the fact that I could install NetBeans and figure out how to actually create a scriptlet class from the sample provided by the documentation is pretty cool.   May have just unleashed the beast.

Jeff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...