In the same place, you can find a slightly modified version of StyledText. When I say “slightly modified”, then I mean “at the API level”. I’m currently heavily refactoring the code inside to make it maintainable and more easily extendable.
Some achievements from the latest hacking session:
- It’s now possible to write tests that verify the rendering of StyledText
- Bullets are managed and rendered in their own special classes. Same goes for StyleRanges. That reduced the size from 1’700 lines to just about 1’100 lines – not that much but a good start. This also means I can write tests that just check the management of StyleRanges – without bothering with a main loop, resource management, etc.
- There are a bunch of helper methods to debug the model behind your text. They can dump the text in readable form.
What’s next? I’m really starting to think about turning
TextStyle into independent classes. Right now, the former inherits from the latter which means there is some really ugly code when it comes to reusing styles, merging styles, font handling, etc. One effect is that the current implementation violates the contract of
equals() takes the range into account while
hashCode() doesn’t. While that may not have an effect (StyleRanges with the same style just pile up in a hash map), it’s still a sign of a skewed API.
The API would be much more clean if there was a style manager (which you could dispose to free all fonts, colors, etc, at once) and when
StyleRanges were just tiny classes that contain a range and a pointer to a style. That would allow to get rid of the
ranges int array, we could use
List to manage style ranges, etc.
Something that I’ve been testing is hierarchical styles (i.e. styles that inherit from other styles). Works like in word processors where you define a basic style and then derive from that. I’m not happy with the performance right now but I’ve got some ideas.
How to use my version? Simple: Clone the StyledText project and install it. Check the POM to see which dependencies you need in your project. Now replace