google-code-export / piccolo2d

Automatically exported from code.google.com/p/piccolo2d
2 stars 1 forks source link

PStyledText breaks when using Insets and unconstrained width #192

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
PStyledText throws exception when using insets with unconstrained width:

public void testInsetsWithUnconstrainedWidth() {
    PStyledText text = new PStyledText();
    text.setInsets(new Insets(10, 10, 10, 10));
    text.setConstrainWidthToTextWidth(false);
    text.setDocument(new DefaultStyledDocument());
}

Exception in thread "AWT-EventQueue-0" 
java.lang.ArrayIndexOutOfBoundsException: -1
    at java.awt.font.LineBreakMeasurer.nextOffset(LineBreakMeasurer.java:346)
    at java.awt.font.LineBreakMeasurer.nextLayout(LineBreakMeasurer.java:422)
    at java.awt.font.LineBreakMeasurer.nextLayout(LineBreakMeasurer.java:395)
    at 
PStyledText.extractLineBreaks(_PStyledText.java:)

When determining line breaks with LineBreakMeasurer, we can call when the 
PStyledText's node answers a value < insets/margins for getWidth(). An update 
to PStyledText like the following fixes the issue:

551 | measurer.nextLayout((float) Math.max(0, Math.ceil(getWidth() - 
insets.left - insets.right)));

Going to think on the fix/await comments.

Original issue reported on code.google.com by atdi...@gmail.com on 8 Nov 2010 at 1:57

GoogleCodeExporter commented 9 years ago

Original comment by atdi...@gmail.com on 8 Nov 2010 at 2:04

GoogleCodeExporter commented 9 years ago

Original comment by atdi...@gmail.com on 21 Nov 2010 at 6:25

GoogleCodeExporter commented 9 years ago

Original comment by atdi...@gmail.com on 21 Nov 2010 at 6:38

GoogleCodeExporter commented 9 years ago
I have a more specific analysis of this issue.

* You cannot setWidth() on a PStyledText until after you've given it a document 
(see PST.setBounds()).
* You cannot setInsets() on a PStyledText if the inset values produce a total 
width margin greater than the width of the PStyledText. (Else you get the 
exception reported in this issue.)

To use insets with unconstrained width you must first set the document, then 
set the width, then set the insets:

        PStyledText t = new PStyledText();
        t.setConstrainWidthToTextWidth(false);
        t.setDocument(new DefaultStyledDocument());
        t.setWidth(50);
        t.setInsets(new Insets(10,10,10,10));

So it is possible to get the desired behavior, albeit awkward, undocumented 
construction.

I would thus not qualify this as a defect after all but I propose more robust 
semantics for future (2.0) behavior:

That is, I propose that PST allows for a width less than the provided insets 
with the result being a graceful layout degradation (single character per line 
which is what LineBreakMeasurer.nextLayout(0) produces) and not the 
LineBreakMeasurer exception we see above.

Original comment by atdi...@gmail.com on 21 Nov 2010 at 6:54

GoogleCodeExporter commented 9 years ago

Original comment by atdi...@gmail.com on 22 Nov 2010 at 4:37

GoogleCodeExporter commented 9 years ago
Based on recent discussion on piccolo2d-user@, I would suggest that PText 
receives the same analysis.  Making the same calls in a different order 
shouldn't result in different behaviour.

See e.g. r1078.

Original comment by heue...@gmail.com on 14 Dec 2010 at 3:18

GoogleCodeExporter commented 9 years ago
I added a comment on the piccolo2d-user thread (goo.gl/BXmlP)... PText is a 
slightly different issue and could argue that it is reasonable behavior.

PStyledText on the other hand is throwing an exception if calls are in a 
different order, but it could just as easily degrade more gracefully wrt how it 
handles Insets that exceed its width...

Original comment by atdi...@gmail.com on 15 Dec 2010 at 2:32

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:24

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:28

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:30

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 26 Nov 2013 at 9:11