• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Undesired word break on curly apostrophe in JTextPane

 
Ranch Hand
Posts: 133
1
Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be in the running for Most Obscure Problem ever reported. I have a JTextPane; Java inserts line breaks in ALMOST all of the right places. It correctly does NOT insert a line break on a flat apostrophe (the character ') but it DOES insert a line break after a curly apostrophe, which I cannot insert with this editor. Thus, the word we'll with a flat apostrophe is not broken, but the version with the curly apostrophe is broken so that it looks like this: we'
ll
Obviously, this is unacceptable. I have searched the web and been hobbled by a huge number of hits on discussions of how to turn word breaking on and off. There's a topic here https://coderanch.com/t/380931/java/java/Parse-Smart-Quotes-regular-quotes from seven years ago about using smart quotes (curly double quotes), but it wasn't about line breaks.

There is something called DefaultEditorKit.insertBreakAction, but that does not seem to address this problem.

Here are the possibilities I am considering:
1. Hope, hope, hope, that there's a method along the lines of JTextPane.setWordBreakOnCurlyApostrophe(boolean). (Yeah, right.)
2. Replace all curly apostrophes with flat apostrophes (ugly!)
3. Accept the rare case of the improperly broken word (it's rather jarring).
4. Write special code that fakes Java into doing the right thing (probably a lot of work).
5. Find another Swing object that can do this correctly. So far, no luck.

Does anybody have any better ideas?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Crawford wrote:4. Write special code that fakes Java into doing the right thing (probably a lot of work).


Having spent a couple of hours trying to find out where things happen, I'd say it's not possible.

FWIW -- creating a line break starts getting serious in public static final int javax,swing.text.Utilities#getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset) (line 421) which makes use of a BreakIterator#getLineInstance() (another static method), which returns a java.text.RuleBasedBreakIterator (default-access aka package-private class) which uses sun/text/resources/LineBreakIteratorData in jre/lib/resources.jar. Between all the statics, limited access and proprietary stuff that seems to rule out subclassing for desired behavior.

There just might be a way to replace the with a ' for computing the line break, and using the original curly quote for rendering, but I couldn't trace that path of execution ... got stuck at a firePropertyChange somewhere and couldn't find where the listener was that picked up on the property change.

Sorry, but it rather looks like you have to make a choice between 2 and 3.
 
Chris Crawford
Ranch Hand
Posts: 133
1
Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. It's good to have my ignorant superstitions verified by rational analysis, for once.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that this is of any help to you but it is a known bug with the BreakIterator. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6609664
And as it was first reported in 2007 and is still open it looks like there is no rush to fix this.
 
Chris Crawford
Ranch Hand
Posts: 133
1
Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I was only 5 and a half years late in noticing. From my perspective, I think that represents an improvement.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic