TextUtilities wrap wrong

A discussion forum for the JCommon class library.
Locked
Werner.Chladek
Posts: 1
Joined: Fri Apr 17, 2009 2:05 pm

TextUtilities wrap wrong

Post by Werner.Chladek » Tue Apr 21, 2009 8:35 am

Hi,

I found a constalation in which TextUtilities wrap wrong.

The Test class:

Code: Select all

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.text.G2TextMeasurer;
import org.jfree.text.TextBlock;
import org.jfree.text.TextBlockAnchor;
import org.jfree.text.TextUtilities;
import org.jfree.ui.HorizontalAlignment;

public class Test extends JFrame {

	private static final long serialVersionUID = 1L;

	private class TestPanel extends JPanel {

		private static final long serialVersionUID = 1L;
		private TextBlock block;

		public void paint(Graphics g) {
			super.paint(g);
			paintMe((Graphics2D) g);
		}

		private void paintMe(Graphics2D g2) {
			if (block == null) {
				Font font = Font.decode("Arial-BOLD-10");
				block = TextUtilities
						.createTextBlock(
								"A Fox and a dog jumping round the wood\nCap and Capper is a nice movie.",
								font, Color.blue, 195, 3,
								new G2TextMeasurer(g2));
				block.setLineAlignment(HorizontalAlignment.LEFT);
			}
			block.draw(g2, 10, 10, TextBlockAnchor.TOP_LEFT);
			g2.setColor(Color.green);
			g2.drawRect(10, 10, 195, 50);
		}
	}

	public Test(String title) throws HeadlessException {
		super(title);
	}

	public void init() {
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(new TestPanel(), BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		Test test = new Test("Test Bounding box and text");
		test.init();
		test.setPreferredSize(new Dimension(230, 105));
		test.setMinimumSize(new Dimension(230, 105));
		test.setMaximumSize(new Dimension(230, 105));
		test.setSize(new Dimension(230, 105));
		test.show();
	}

}
I have a solution for this Problem in TextUtilities:
you need to go to nextLineBreak(...)

Code: Select all

while (((end = iterator.next()) != BreakIterator.DONE)) {
// old code begin
//            if (end > newline) {
//                return newline;
//            }
// old code end
            x += measurer.getStringWidth(text, current, end);
            if (x > width) {
                if (firstWord) {
                    while (measurer.getStringWidth(text, start, end) >width) {
                        end--;
                        if (end <= start) {
                            return end;
                        }
                    }
                    return end;
                }
                else {
                    end = iterator.previous();
                    return end;
                }
            } else {
// new code begin
                if (end > newline) {
                    return newline;
                }
            }
// new code end
            // we found at least one word that fits ...
            firstWord = false;
            current = end;
        }

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: TextUtilities wrap wrong

Post by david.gilbert » Mon Apr 27, 2009 8:26 pm

Thanks for the report. I committed your fix for inclusion in the next release (1.0.17).

Best regards,

Dave Gilbert

Locked