Following the last few entries on using SWT for native text rendering in Swing applications under the Substance look-and-feel, i was contacted by Steve Northover (technical lead of the SWT project) with a few suggestions. First, to recap the relevant entries up until now:

  • The introduction showed a few screenshots of rasterizing the default Vista Segoe UI font in Swing and SWT.
  • The next entry showed some of the implementation details of the interaction between the two UI toolkits.
  • The “ready, steady, go” announced the official stable plugin version, along with much simpler deployment, support for WebStart environment and more.
  • The last entry showed some quirks of rendering vertical texts.

Steve’s first suggestion was to go over the code and make sure that all SWT resources are properly disposed. While this has been considered as one of SWT’s weaker points (something along the lines of “it’s Java, why do i need to explicitly dispose resources”), this is hardly any different from Graphics2D.dispose or even explicitly closing DB connections, streams, files etc in usual Java programs.

If you don’t call the dispose() method of all allocated SWT resources, you can run into the “No more handles” error. While usually it takes quite a while to get there, the native text rasterizer would encounter it fairly quickly (about 5-6 resources are allocated for each text painting in the current implementation). While the text rasterizer itself is disposing all its resources, the test applications did not. This has now been fixed.

The second suggestion was to strip away unnecessary classes from the SWT jar. While this jar is by no means huge (clocking in at about 1.3MB), some people would cringe at this figure. First, i would like to enumerate the reasons why i don’t recommend stripping away the contents of swt.jar (as detailed below):

  1. While the license permits it (and the whole plugin is under EPL), i’m rarely comfortable in stripping away contents of third-party libraries. When i upgrade to the latest release, i can eliminate at least one possible source of incorrect behavior (if something weird happens).
  2. The swt.jar for Windows has quite an acceptable size for the functionality that it provides. For example, the main Substance look-and-feel jar is about 1.7MB.
  3. An application that decides to use this plugin might as well “profit” from other parts of SWT, as illustrated by Chris Deckers’ posting on Javalobby. This way, the two libraries can share the same swt.jar.

Having said that, it is possible to strip the swt.jar to as little as 430KB. There are two ways to do so – the approach recommended by Steve, and the aggressive approach that resulted in the current swt-win-stripped.jar available from the CVS repository.

The recommended approach is to work on the package level, removing those packages that clearly are not required for the text rendering. The minimum set according to Steve is org.eclipse.swt.graphics, org.eclipse.swt.widgets, org.eclipse.swt.internal, org.eclipse.swt.internal.win32 and org.eclipse.swt.internal.gdip (for GDI+ operations such as tranformations for the vertical texts). In addition, the swt-win32 and swt-win32-gdip DLLs (for Windows) need to be in the stripped jar. Furthermore, Steve mentions that the org.eclipse.swt.events and org.eclipse.swt.accessibility might be needed for corner-case scenarios, such as switching the OS theme while the program is running (correction from Steve – these are needed to compile a stripped SWT jar from sources).

However, you can take it a little bit further and remove additional classes from the org.eclipse.swt.widgets (pretty much everything that concerns actual components, since these are not needed for the text painting and image manipulation for this plugin). Take a look at the contents of this package in swt-win-stripped.jar in the CVS or the distribution zip to see what has been left. This is what brings the jar size down to 430KB.

As an “extreme” measure, one can go even further and start stripping unnecessary functionality from the DLLs, org.eclipse.swt.SWT class and org.eclipse.swt.internal.win32.OS class. Do at your own risk.

Note that while this entry talks about the Windows version of SWT, the same applies to other platforms as well (which would have a different structure of org.eclipse.swt.internal package and different bundled native libraries).

I’d like to thank Steve Northover for his comments and time.

Here are some Swing links that you might have missed during this week:

  • Luan O’Carroll writes about updates to the rollup bar component from the XUI framework. The functionality of the component seems to lie somewhere between the task pane container / collapsible panels of SwingX and the collapsed panels of docking frameworks. The links to the demo videos seem to be down, and it would be interesting to see a WebStart demo and learn how well the component integrates with core and third-party look-and-feels.
  • Andres Almiray continues his work on the GraphicsBuilder for Groovy. This week he talks about advanced strokes (based on the code by Jerry Huxtable) and the wide variety of geometrical shapes.
  • John Zukowski provides a simple overview of placing custom components on tabbed panes (available starting from JDK 6.0). Unfortunately, all the tutorials i have seen on the subject deal exclusively with putting close buttons on tabs. I would be very interested to hear about at least one another valid scenario for using custom components on tabs. If there is none – why this functionality hasn’t been restricted to, well, putting only close buttons?
  • Angelo De Caro writes about release 1.4.0 of MyDoggy docking framework.
  • software4java company announced availability of the adaptive menu component. The functionality of adaptive menus introduced in the Microsoft Office 2000 to Swing application has a few significant shortcomings addressed at length in this posting by the lead designer of Office 2007, Jensen Harris. While it is still available in Office 2007, it is turned off by default – if you’re thinking about including this functionality in your applications, you might want to reconsider it.
  • Christopher Deckers makes quite an unexpected announcement on library that brings integration with an impressive collection of native components to Swing applications. Based on SWT (with the applying restrictions of target platforms and bundled native libraries), it already provides solutions for embedding web browser, Flash player and multi media player, as well as querying the native file associations and launching registered applications (which looks very much like the Desktop class introduced in JDK 6.0).

Sometimes, the application design requires displaying vertical (rotated) texts. While this is not a common requirement, and the usage of vertical texts must be kept to an absolute minimum, one should be aware of the possible technical solutions and the problems associated with them. This entry shows two possible solutions, accompanied by the screenshots and short technical explanations specific to LCD monitors with enabled subpixel anti-aliasing.

One way to draw rotated texts (by either 90 or 270 degrees) is to draw a horizontal text on an offscreen image, rotate that image by the relevant amount and draw it on the screen. Here is how it looks like (using the native text rendering provided by SWT):

If the texts look a little fuzzy, it’s not because your monitor has different subpixel settings. Here is a 400% zoom on one text:

What’s with the extra horizontal bluish lines? Those come from the original subpixel rendering of the horizontal text that takes advantage of the LCD physical structure, as explained and illustrated in this java.net article by Chet Haase. Unfortunately, this doesn’t work so well on image rotation – the”extra” blue pixels simply make the text fuzzy and much less readable.

What’s the alternative? Another way is to install an affine transformation on the graphic context that rotates all graphic operations by 90 or 270 degrees. The implementation here is a little bit more complicated, since the transformation needs to be preceded by matching offset translation. And here is how it looks like:

Zooming a little on one of the texts:

Now this is crisper than the first technique, it still is far from perfect. For comparison, here is the same exact text drawn unrotated:

Note the width of digit 0 – when it’s drawn horizontally, it has three pixels between the outer sides. However, when it’s drawn vertically, it has only two pixels. Looking back at the screenshot of vertical sliders, the labels look much less defined as compared to the usual Vista texts (under Segoe UI font).

I’m not really sure what causes the loss of pixel in digit 0. During the past few months working with Vista, i have to say that i never have seen a single native application (including Office 2007 and SWT-based Eclipse) using vertical texts anywhere in the UI. Perhaps the native font rasterizer was never optimized to handle this case at all…

Substance theme pack updated

January 4th, 2008

In addition to about twenty core Substance themes, the theme pack plugin provides even more themes to create a unique look for your Swing applications. When you put the binary plugin in the classpath, the additional themes become available in the getAllThemes() API, and you can use them just like any other core theme.

As part of updating the licensing headers to 2008, i’ve also updated the screenshots of extra themes to reflect the latest visuals of Substance. Here are some of my favourites:

Belize

Brick Wall

Fauve Mauve

Mahogany

Orchid Alloy

Yellow Marine